Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "buffet/commands/dbus_command_proxy.h" |
| 6 | |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 7 | #include <chromeos/dbus/async_event_sequencer.h> |
| 8 | #include <chromeos/dbus/exported_object_manager.h> |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 9 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 10 | #include "buffet/commands/command_definition.h" |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 11 | #include "buffet/commands/command_instance.h" |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 12 | #include "buffet/commands/object_schema.h" |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 13 | #include "buffet/commands/prop_constraints.h" |
| 14 | #include "buffet/commands/prop_types.h" |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 15 | |
| 16 | using chromeos::dbus_utils::AsyncEventSequencer; |
| 17 | using chromeos::dbus_utils::ExportedObjectManager; |
| 18 | |
| 19 | namespace buffet { |
| 20 | |
| 21 | DBusCommandProxy::DBusCommandProxy(ExportedObjectManager* object_manager, |
| 22 | const scoped_refptr<dbus::Bus>& bus, |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 23 | CommandInstance* command_instance, |
| 24 | std::string object_path) |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 25 | : command_instance_{command_instance}, |
| 26 | dbus_object_{object_manager, bus, dbus::ObjectPath{object_path}} { |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void DBusCommandProxy::RegisterAsync( |
| 30 | const AsyncEventSequencer::CompletionAction& completion_callback) { |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 31 | dbus_adaptor_.RegisterWithDBusObject(&dbus_object_); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 32 | |
| 33 | // Set the initial property values before registering the DBus object. |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 34 | dbus_adaptor_.SetName(command_instance_->GetName()); |
| 35 | dbus_adaptor_.SetCategory(command_instance_->GetCategory()); |
| 36 | dbus_adaptor_.SetId(command_instance_->GetID()); |
| 37 | dbus_adaptor_.SetStatus(command_instance_->GetStatus()); |
| 38 | dbus_adaptor_.SetProgress(command_instance_->GetProgress()); |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 39 | |
| 40 | dbus_adaptor_.SetParameters(ObjectToDBusVariant( |
| 41 | command_instance_->GetParameters())); |
| 42 | dbus_adaptor_.SetResults(ObjectToDBusVariant( |
| 43 | command_instance_->GetResults())); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 44 | |
| 45 | // Register the command DBus object and expose its methods and properties. |
| 46 | dbus_object_.RegisterAsync(completion_callback); |
| 47 | } |
| 48 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 49 | void DBusCommandProxy::OnResultsChanged(const native_types::Object& results) { |
| 50 | dbus_adaptor_.SetResults(ObjectToDBusVariant(results)); |
| 51 | } |
| 52 | |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 53 | void DBusCommandProxy::OnStatusChanged(const std::string& status) { |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 54 | dbus_adaptor_.SetStatus(status); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void DBusCommandProxy::OnProgressChanged(int progress) { |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 58 | dbus_adaptor_.SetProgress(progress); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 61 | bool DBusCommandProxy::SetProgress(chromeos::ErrorPtr* error, |
| 62 | int32_t progress) { |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 63 | LOG(INFO) << "Received call to Command<" |
| 64 | << command_instance_->GetName() << ">::SetProgress(" |
| 65 | << progress << ")"; |
| 66 | |
| 67 | // Validate |progress| parameter. Its value must be between 0 and 100. |
| 68 | IntPropType progress_type; |
| 69 | progress_type.AddMinMaxConstraint(0, 100); |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 70 | if (!progress_type.ValidateValue(progress, error)) |
| 71 | return false; |
| 72 | |
| 73 | command_instance_->SetProgress(progress); |
| 74 | return true; |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 77 | bool DBusCommandProxy::SetResults(chromeos::ErrorPtr* error, |
| 78 | const chromeos::VariantDictionary& results) { |
| 79 | LOG(INFO) << "Received call to Command<" |
| 80 | << command_instance_->GetName() << ">::SetResults()"; |
| 81 | |
| 82 | auto results_schema = command_instance_->GetCommandDefinition()->GetResults(); |
| 83 | native_types::Object obj; |
| 84 | if (!ObjectFromDBusVariant(results_schema.get(), results, &obj, error)) |
| 85 | return false; |
| 86 | |
| 87 | command_instance_->SetResults(obj); |
| 88 | return true; |
| 89 | } |
| 90 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 91 | void DBusCommandProxy::Abort() { |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 92 | LOG(INFO) << "Received call to Command<" |
| 93 | << command_instance_->GetName() << ">::Abort()"; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 94 | command_instance_->Abort(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 97 | void DBusCommandProxy::Cancel() { |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 98 | LOG(INFO) << "Received call to Command<" |
| 99 | << command_instance_->GetName() << ">::Cancel()"; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 100 | command_instance_->Cancel(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 103 | void DBusCommandProxy::Done() { |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 104 | LOG(INFO) << "Received call to Command<" |
| 105 | << command_instance_->GetName() << ">::Done()"; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 106 | command_instance_->Done(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
| 110 | } // namespace buffet |