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 | |
Vitaly Buka | f9d5029 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 5 | #include "buffet/dbus_command_proxy.h" |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 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 | |
Vitaly Buka | f9d5029 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 10 | #include "buffet/dbus_conversion.h" |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 11 | #include "weave/enum_to_string.h" |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 12 | |
| 13 | using chromeos::dbus_utils::AsyncEventSequencer; |
| 14 | using chromeos::dbus_utils::ExportedObjectManager; |
| 15 | |
Vitaly Buka | f9d5029 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 16 | namespace buffet { |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 17 | |
| 18 | DBusCommandProxy::DBusCommandProxy(ExportedObjectManager* object_manager, |
| 19 | const scoped_refptr<dbus::Bus>& bus, |
Vitaly Buka | f9d5029 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 20 | weave::Command* command, |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 21 | std::string object_path) |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 22 | : command_{command}, |
Vitaly Buka | 12affd8 | 2015-07-23 18:45:35 -0700 | [diff] [blame] | 23 | dbus_object_{object_manager, bus, dbus::ObjectPath{object_path}} {} |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 24 | |
| 25 | void DBusCommandProxy::RegisterAsync( |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 26 | const AsyncEventSequencer::CompletionAction& completion_callback) { |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 27 | dbus_adaptor_.RegisterWithDBusObject(&dbus_object_); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 28 | |
| 29 | // Set the initial property values before registering the DBus object. |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 30 | dbus_adaptor_.SetName(command_->GetName()); |
| 31 | dbus_adaptor_.SetCategory(command_->GetCategory()); |
| 32 | dbus_adaptor_.SetId(command_->GetID()); |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 33 | dbus_adaptor_.SetStatus(EnumToString(command_->GetStatus())); |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 34 | dbus_adaptor_.SetProgress( |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 35 | DictionaryToDBusVariantDictionary(*command_->GetProgress())); |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 36 | dbus_adaptor_.SetOrigin(EnumToString(command_->GetOrigin())); |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 37 | dbus_adaptor_.SetParameters( |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 38 | DictionaryToDBusVariantDictionary(*command_->GetParameters())); |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 39 | dbus_adaptor_.SetResults( |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 40 | DictionaryToDBusVariantDictionary(*command_->GetResults())); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 41 | |
| 42 | // Register the command DBus object and expose its methods and properties. |
| 43 | dbus_object_.RegisterAsync(completion_callback); |
| 44 | } |
| 45 | |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 46 | void DBusCommandProxy::OnResultsChanged() { |
| 47 | dbus_adaptor_.SetResults( |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 48 | DictionaryToDBusVariantDictionary(*command_->GetResults())); |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 49 | } |
| 50 | |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 51 | void DBusCommandProxy::OnStatusChanged() { |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 52 | dbus_adaptor_.SetStatus(EnumToString(command_->GetStatus())); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 55 | void DBusCommandProxy::OnProgressChanged() { |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 56 | dbus_adaptor_.SetProgress( |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 57 | DictionaryToDBusVariantDictionary(*command_->GetProgress())); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Vitaly Buka | c3d4e97 | 2015-07-21 09:55:25 -0700 | [diff] [blame] | 60 | void DBusCommandProxy::OnCommandDestroyed() { |
| 61 | delete this; |
| 62 | } |
| 63 | |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 64 | bool DBusCommandProxy::SetProgress( |
| 65 | chromeos::ErrorPtr* error, |
| 66 | const chromeos::VariantDictionary& progress) { |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 67 | LOG(INFO) << "Received call to Command<" << command_->GetName() |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 68 | << ">::SetProgress()"; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 69 | auto dictionary = DictionaryFromDBusVariantDictionary(progress, error); |
| 70 | if (!dictionary) |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 71 | return false; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 72 | return command_->SetProgress(*dictionary, error); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 75 | bool DBusCommandProxy::SetResults(chromeos::ErrorPtr* error, |
| 76 | const chromeos::VariantDictionary& results) { |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 77 | LOG(INFO) << "Received call to Command<" << command_->GetName() |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 78 | << ">::SetResults()"; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 79 | auto dictionary = DictionaryFromDBusVariantDictionary(results, error); |
| 80 | if (!dictionary) |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 81 | return false; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 82 | return command_->SetResults(*dictionary, error); |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 83 | } |
| 84 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 85 | void DBusCommandProxy::Abort() { |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 86 | LOG(INFO) << "Received call to Command<" << command_->GetName() |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 87 | << ">::Abort()"; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 88 | command_->Abort(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 91 | void DBusCommandProxy::Cancel() { |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 92 | LOG(INFO) << "Received call to Command<" << command_->GetName() |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 93 | << ">::Cancel()"; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 94 | command_->Cancel(); |
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::Done() { |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 98 | LOG(INFO) << "Received call to Command<" << command_->GetName() |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 99 | << ">::Done()"; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 100 | command_->Done(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Vitaly Buka | f9d5029 | 2015-07-27 16:08:51 -0700 | [diff] [blame] | 103 | } // namespace buffet |