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()); |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 38 | dbus_adaptor_.SetProgress( |
| 39 | ObjectToDBusVariant(command_instance_->GetProgress())); |
Alex Vakulenko | f784e21 | 2015-04-20 12:33:52 -0700 | [diff] [blame] | 40 | dbus_adaptor_.SetOrigin(command_instance_->GetOrigin()); |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 41 | |
| 42 | dbus_adaptor_.SetParameters(ObjectToDBusVariant( |
| 43 | command_instance_->GetParameters())); |
| 44 | dbus_adaptor_.SetResults(ObjectToDBusVariant( |
| 45 | command_instance_->GetResults())); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 46 | |
| 47 | // Register the command DBus object and expose its methods and properties. |
| 48 | dbus_object_.RegisterAsync(completion_callback); |
| 49 | } |
| 50 | |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 51 | void DBusCommandProxy::OnResultsChanged() { |
| 52 | dbus_adaptor_.SetResults( |
| 53 | ObjectToDBusVariant(command_instance_->GetResults())); |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 54 | } |
| 55 | |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 56 | void DBusCommandProxy::OnStatusChanged() { |
| 57 | dbus_adaptor_.SetStatus(command_instance_->GetStatus()); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Alex Vakulenko | b211c10 | 2015-04-21 11:43:23 -0700 | [diff] [blame] | 60 | void DBusCommandProxy::OnProgressChanged() { |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 61 | dbus_adaptor_.SetProgress( |
| 62 | ObjectToDBusVariant(command_instance_->GetProgress())); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 65 | bool DBusCommandProxy::SetProgress( |
| 66 | chromeos::ErrorPtr* error, |
| 67 | const chromeos::VariantDictionary& progress) { |
| 68 | LOG(INFO) << "Received call to Command<" << command_instance_->GetName() |
| 69 | << ">::SetProgress()"; |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 70 | |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 71 | auto progress_schema = |
| 72 | command_instance_->GetCommandDefinition()->GetProgress(); |
| 73 | native_types::Object obj; |
| 74 | if (!ObjectFromDBusVariant(progress_schema, progress, &obj, error)) |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 75 | return false; |
| 76 | |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 77 | command_instance_->SetProgress(obj); |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 78 | return true; |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 81 | bool DBusCommandProxy::SetResults(chromeos::ErrorPtr* error, |
| 82 | const chromeos::VariantDictionary& results) { |
| 83 | LOG(INFO) << "Received call to Command<" |
| 84 | << command_instance_->GetName() << ">::SetResults()"; |
| 85 | |
| 86 | auto results_schema = command_instance_->GetCommandDefinition()->GetResults(); |
| 87 | native_types::Object obj; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 88 | if (!ObjectFromDBusVariant(results_schema, results, &obj, error)) |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 89 | return false; |
| 90 | |
| 91 | command_instance_->SetResults(obj); |
| 92 | return true; |
| 93 | } |
| 94 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 95 | void DBusCommandProxy::Abort() { |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 96 | LOG(INFO) << "Received call to Command<" |
| 97 | << command_instance_->GetName() << ">::Abort()"; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 98 | command_instance_->Abort(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 101 | void DBusCommandProxy::Cancel() { |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 102 | LOG(INFO) << "Received call to Command<" |
| 103 | << command_instance_->GetName() << ">::Cancel()"; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 104 | command_instance_->Cancel(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 107 | void DBusCommandProxy::Done() { |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 108 | LOG(INFO) << "Received call to Command<" |
| 109 | << command_instance_->GetName() << ">::Done()"; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 110 | command_instance_->Done(); |
Alex Vakulenko | 4866ac9 | 2014-08-20 12:53:33 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
| 114 | } // namespace buffet |