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 | |
| 10 | #include "buffet/commands/command_instance.h" |
| 11 | #include "buffet/commands/prop_constraints.h" |
| 12 | #include "buffet/commands/prop_types.h" |
Alex Vakulenko | 89d9d5e | 2014-09-12 10:27:23 -0700 | [diff] [blame] | 13 | #include "buffet/libbuffet/dbus_constants.h" |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 14 | |
| 15 | using chromeos::dbus_utils::AsyncEventSequencer; |
| 16 | using chromeos::dbus_utils::ExportedObjectManager; |
| 17 | |
| 18 | namespace buffet { |
| 19 | |
| 20 | DBusCommandProxy::DBusCommandProxy(ExportedObjectManager* object_manager, |
| 21 | const scoped_refptr<dbus::Bus>& bus, |
| 22 | CommandInstance* command_instance) |
| 23 | : object_path_(dbus_constants::kCommandServicePathPrefix + |
| 24 | command_instance->GetID()), |
| 25 | command_instance_(command_instance), |
| 26 | dbus_object_(object_manager, bus, object_path_) { |
| 27 | } |
| 28 | |
| 29 | void DBusCommandProxy::RegisterAsync( |
| 30 | const AsyncEventSequencer::CompletionAction& completion_callback) { |
| 31 | chromeos::dbus_utils::DBusInterface* itf = |
| 32 | dbus_object_.AddOrGetInterface(dbus_constants::kCommandInterface); |
| 33 | |
| 34 | // DBus methods. |
| 35 | itf->AddMethodHandler(dbus_constants::kCommandSetProgress, |
| 36 | base::Unretained(this), |
| 37 | &DBusCommandProxy::HandleSetProgress); |
| 38 | itf->AddMethodHandler(dbus_constants::kCommandAbort, |
| 39 | base::Unretained(this), |
| 40 | &DBusCommandProxy::HandleAbort); |
| 41 | itf->AddMethodHandler(dbus_constants::kCommandCancel, |
| 42 | base::Unretained(this), |
| 43 | &DBusCommandProxy::HandleCancel); |
| 44 | itf->AddMethodHandler(dbus_constants::kCommandDone, |
| 45 | base::Unretained(this), |
| 46 | &DBusCommandProxy::HandleDone); |
| 47 | |
| 48 | // DBus properties. |
| 49 | itf->AddProperty(dbus_constants::kCommandName, &name_); |
| 50 | itf->AddProperty(dbus_constants::kCommandCategory, &category_); |
| 51 | itf->AddProperty(dbus_constants::kCommandId, &id_); |
| 52 | itf->AddProperty(dbus_constants::kCommandStatus, &status_); |
| 53 | itf->AddProperty(dbus_constants::kCommandProgress, &progress_); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 54 | itf->AddProperty(dbus_constants::kCommandParameters, ¶meters_); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 55 | |
| 56 | // Set the initial property values before registering the DBus object. |
| 57 | name_.SetValue(command_instance_->GetName()); |
| 58 | category_.SetValue(command_instance_->GetCategory()); |
| 59 | id_.SetValue(command_instance_->GetID()); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 60 | status_.SetValue(command_instance_->GetStatus()); |
| 61 | progress_.SetValue(command_instance_->GetProgress()); |
| 62 | // Convert a string-to-PropValue map into a string-to-Any map which can be |
| 63 | // sent over D-Bus. |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 64 | chromeos::VariantDictionary params; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 65 | for (const auto& param_pair : command_instance_->GetParameters()) { |
| 66 | params.insert(std::make_pair(param_pair.first, |
| 67 | param_pair.second->GetValueAsAny())); |
| 68 | } |
| 69 | parameters_.SetValue(params); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 70 | |
| 71 | // Register the command DBus object and expose its methods and properties. |
| 72 | dbus_object_.RegisterAsync(completion_callback); |
| 73 | } |
| 74 | |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 75 | void DBusCommandProxy::OnStatusChanged(const std::string& status) { |
| 76 | status_.SetValue(status); |
| 77 | } |
| 78 | |
| 79 | void DBusCommandProxy::OnProgressChanged(int progress) { |
| 80 | progress_.SetValue(progress); |
| 81 | } |
| 82 | |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 83 | void DBusCommandProxy::HandleSetProgress(chromeos::ErrorPtr* error, |
| 84 | int32_t progress) { |
| 85 | LOG(INFO) << "Received call to Command<" |
| 86 | << command_instance_->GetName() << ">::SetProgress(" |
| 87 | << progress << ")"; |
| 88 | |
| 89 | // Validate |progress| parameter. Its value must be between 0 and 100. |
| 90 | IntPropType progress_type; |
| 91 | progress_type.AddMinMaxConstraint(0, 100); |
| 92 | if (progress_type.ValidateValue(progress, error)) { |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 93 | command_instance_->SetProgress(progress); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | void DBusCommandProxy::HandleAbort(chromeos::ErrorPtr* error) { |
| 98 | LOG(INFO) << "Received call to Command<" |
| 99 | << command_instance_->GetName() << ">::Abort()"; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 100 | command_instance_->Abort(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void DBusCommandProxy::HandleCancel(chromeos::ErrorPtr* error) { |
| 104 | LOG(INFO) << "Received call to Command<" |
| 105 | << command_instance_->GetName() << ">::Cancel()"; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 106 | command_instance_->Cancel(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void DBusCommandProxy::HandleDone(chromeos::ErrorPtr* error) { |
| 110 | LOG(INFO) << "Received call to Command<" |
| 111 | << command_instance_->GetName() << ">::Done()"; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 112 | command_instance_->Done(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | |
| 116 | } // namespace buffet |