Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -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/dbus_command_proxy.h" |
| 6 | |
| 7 | #include <chromeos/dbus/async_event_sequencer.h> |
| 8 | #include <chromeos/dbus/exported_object_manager.h> |
Vitaly Buka | e2713ac | 2015-08-03 13:50:01 -0700 | [diff] [blame] | 9 | #include <weave/enum_to_string.h> |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 10 | |
| 11 | #include "buffet/dbus_conversion.h" |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 12 | #include "buffet/weave_error_conversion.h" |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 13 | |
| 14 | using chromeos::dbus_utils::AsyncEventSequencer; |
| 15 | using chromeos::dbus_utils::ExportedObjectManager; |
| 16 | |
| 17 | namespace buffet { |
| 18 | |
| 19 | DBusCommandProxy::DBusCommandProxy(ExportedObjectManager* object_manager, |
| 20 | const scoped_refptr<dbus::Bus>& bus, |
| 21 | weave::Command* command, |
| 22 | std::string object_path) |
| 23 | : command_{command}, |
Vitaly Buka | f6027cb | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 24 | dbus_object_{object_manager, bus, dbus::ObjectPath{object_path}} { |
| 25 | observer_.Add(command); |
| 26 | } |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 27 | |
| 28 | void DBusCommandProxy::RegisterAsync( |
| 29 | const AsyncEventSequencer::CompletionAction& completion_callback) { |
| 30 | dbus_adaptor_.RegisterWithDBusObject(&dbus_object_); |
| 31 | |
| 32 | // Set the initial property values before registering the DBus object. |
| 33 | dbus_adaptor_.SetName(command_->GetName()); |
| 34 | dbus_adaptor_.SetCategory(command_->GetCategory()); |
| 35 | dbus_adaptor_.SetId(command_->GetID()); |
| 36 | dbus_adaptor_.SetStatus(EnumToString(command_->GetStatus())); |
| 37 | dbus_adaptor_.SetProgress( |
| 38 | DictionaryToDBusVariantDictionary(*command_->GetProgress())); |
| 39 | dbus_adaptor_.SetOrigin(EnumToString(command_->GetOrigin())); |
| 40 | dbus_adaptor_.SetParameters( |
| 41 | DictionaryToDBusVariantDictionary(*command_->GetParameters())); |
| 42 | dbus_adaptor_.SetResults( |
| 43 | DictionaryToDBusVariantDictionary(*command_->GetResults())); |
| 44 | |
| 45 | // Register the command DBus object and expose its methods and properties. |
| 46 | dbus_object_.RegisterAsync(completion_callback); |
| 47 | } |
| 48 | |
| 49 | void DBusCommandProxy::OnResultsChanged() { |
| 50 | dbus_adaptor_.SetResults( |
| 51 | DictionaryToDBusVariantDictionary(*command_->GetResults())); |
| 52 | } |
| 53 | |
| 54 | void DBusCommandProxy::OnStatusChanged() { |
| 55 | dbus_adaptor_.SetStatus(EnumToString(command_->GetStatus())); |
| 56 | } |
| 57 | |
| 58 | void DBusCommandProxy::OnProgressChanged() { |
| 59 | dbus_adaptor_.SetProgress( |
| 60 | DictionaryToDBusVariantDictionary(*command_->GetProgress())); |
| 61 | } |
| 62 | |
| 63 | void DBusCommandProxy::OnCommandDestroyed() { |
| 64 | delete this; |
| 65 | } |
| 66 | |
| 67 | bool DBusCommandProxy::SetProgress( |
| 68 | chromeos::ErrorPtr* error, |
| 69 | const chromeos::VariantDictionary& progress) { |
| 70 | LOG(INFO) << "Received call to Command<" << command_->GetName() |
| 71 | << ">::SetProgress()"; |
| 72 | auto dictionary = DictionaryFromDBusVariantDictionary(progress, error); |
| 73 | if (!dictionary) |
| 74 | return false; |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 75 | weave::ErrorPtr weave_error; |
| 76 | if (!command_->SetProgress(*dictionary, &weave_error)) { |
| 77 | ConvertError(*weave_error, error); |
| 78 | return false; |
| 79 | } |
| 80 | return true; |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | bool DBusCommandProxy::SetResults(chromeos::ErrorPtr* error, |
| 84 | const chromeos::VariantDictionary& results) { |
| 85 | LOG(INFO) << "Received call to Command<" << command_->GetName() |
| 86 | << ">::SetResults()"; |
| 87 | auto dictionary = DictionaryFromDBusVariantDictionary(results, error); |
| 88 | if (!dictionary) |
| 89 | return false; |
Vitaly Buka | 4f77153 | 2015-08-14 14:58:39 -0700 | [diff] [blame] | 90 | weave::ErrorPtr weave_error; |
| 91 | if (!command_->SetResults(*dictionary, &weave_error)) { |
| 92 | ConvertError(*weave_error, error); |
| 93 | return false; |
| 94 | } |
| 95 | return true; |
Vitaly Buka | a0305d3 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void DBusCommandProxy::Abort() { |
| 99 | LOG(INFO) << "Received call to Command<" << command_->GetName() |
| 100 | << ">::Abort()"; |
| 101 | command_->Abort(); |
| 102 | } |
| 103 | |
| 104 | void DBusCommandProxy::Cancel() { |
| 105 | LOG(INFO) << "Received call to Command<" << command_->GetName() |
| 106 | << ">::Cancel()"; |
| 107 | command_->Cancel(); |
| 108 | } |
| 109 | |
| 110 | void DBusCommandProxy::Done() { |
| 111 | LOG(INFO) << "Received call to Command<" << command_->GetName() |
| 112 | << ">::Done()"; |
| 113 | command_->Done(); |
| 114 | } |
| 115 | |
| 116 | } // namespace buffet |