blob: 74278fec434f7e27210bdd10d18ad4a037fc71db [file] [log] [blame]
Alex Vakulenko4866ac92014-08-20 12:53:33 -07001// 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 Vakulenkoa8b95bc2014-08-27 11:00:57 -07007#include <chromeos/dbus/async_event_sequencer.h>
8#include <chromeos/dbus/exported_object_manager.h>
Alex Vakulenko4866ac92014-08-20 12:53:33 -07009
Anton Muhincfde8692014-11-25 03:36:59 +040010#include "buffet/commands/command_definition.h"
Alex Vakulenko4866ac92014-08-20 12:53:33 -070011#include "buffet/commands/command_instance.h"
Anton Muhincfde8692014-11-25 03:36:59 +040012#include "buffet/commands/object_schema.h"
Alex Vakulenko4866ac92014-08-20 12:53:33 -070013#include "buffet/commands/prop_constraints.h"
14#include "buffet/commands/prop_types.h"
Alex Vakulenko4866ac92014-08-20 12:53:33 -070015
16using chromeos::dbus_utils::AsyncEventSequencer;
17using chromeos::dbus_utils::ExportedObjectManager;
18
19namespace buffet {
20
21DBusCommandProxy::DBusCommandProxy(ExportedObjectManager* object_manager,
22 const scoped_refptr<dbus::Bus>& bus,
Anton Muhin5191e812014-10-30 17:49:48 +040023 CommandInstance* command_instance,
24 std::string object_path)
Alex Vakulenko2348e422014-11-21 08:57:57 -080025 : command_instance_{command_instance},
26 dbus_object_{object_manager, bus, dbus::ObjectPath{object_path}} {
Alex Vakulenko4866ac92014-08-20 12:53:33 -070027}
28
29void DBusCommandProxy::RegisterAsync(
30 const AsyncEventSequencer::CompletionAction& completion_callback) {
Alex Vakulenko2348e422014-11-21 08:57:57 -080031 dbus_adaptor_.RegisterWithDBusObject(&dbus_object_);
Alex Vakulenko4866ac92014-08-20 12:53:33 -070032
33 // Set the initial property values before registering the DBus object.
Alex Vakulenko2348e422014-11-21 08:57:57 -080034 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 Buka4129dfa2015-04-29 12:16:58 -070038 dbus_adaptor_.SetProgress(
39 ObjectToDBusVariant(command_instance_->GetProgress()));
Alex Vakulenkof784e212015-04-20 12:33:52 -070040 dbus_adaptor_.SetOrigin(command_instance_->GetOrigin());
Anton Muhincfde8692014-11-25 03:36:59 +040041
42 dbus_adaptor_.SetParameters(ObjectToDBusVariant(
43 command_instance_->GetParameters()));
44 dbus_adaptor_.SetResults(ObjectToDBusVariant(
45 command_instance_->GetResults()));
Alex Vakulenko4866ac92014-08-20 12:53:33 -070046
47 // Register the command DBus object and expose its methods and properties.
48 dbus_object_.RegisterAsync(completion_callback);
49}
50
Alex Vakulenkob211c102015-04-21 11:43:23 -070051void DBusCommandProxy::OnResultsChanged() {
52 dbus_adaptor_.SetResults(
53 ObjectToDBusVariant(command_instance_->GetResults()));
Anton Muhincfde8692014-11-25 03:36:59 +040054}
55
Alex Vakulenkob211c102015-04-21 11:43:23 -070056void DBusCommandProxy::OnStatusChanged() {
57 dbus_adaptor_.SetStatus(command_instance_->GetStatus());
Alex Vakulenkof6b38712014-09-03 16:23:38 -070058}
59
Alex Vakulenkob211c102015-04-21 11:43:23 -070060void DBusCommandProxy::OnProgressChanged() {
Vitaly Buka4129dfa2015-04-29 12:16:58 -070061 dbus_adaptor_.SetProgress(
62 ObjectToDBusVariant(command_instance_->GetProgress()));
Alex Vakulenkof6b38712014-09-03 16:23:38 -070063}
64
Vitaly Buka4129dfa2015-04-29 12:16:58 -070065bool DBusCommandProxy::SetProgress(
66 chromeos::ErrorPtr* error,
67 const chromeos::VariantDictionary& progress) {
68 LOG(INFO) << "Received call to Command<" << command_instance_->GetName()
69 << ">::SetProgress()";
Alex Vakulenko4866ac92014-08-20 12:53:33 -070070
Vitaly Buka4129dfa2015-04-29 12:16:58 -070071 auto progress_schema =
72 command_instance_->GetCommandDefinition()->GetProgress();
73 native_types::Object obj;
74 if (!ObjectFromDBusVariant(progress_schema, progress, &obj, error))
Alex Vakulenko5c7bf012014-10-30 16:28:38 -070075 return false;
76
Vitaly Buka4129dfa2015-04-29 12:16:58 -070077 command_instance_->SetProgress(obj);
Alex Vakulenko5c7bf012014-10-30 16:28:38 -070078 return true;
Alex Vakulenko4866ac92014-08-20 12:53:33 -070079}
80
Anton Muhincfde8692014-11-25 03:36:59 +040081bool 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 Vakulenko5ef75792015-03-19 15:50:44 -070088 if (!ObjectFromDBusVariant(results_schema, results, &obj, error))
Anton Muhincfde8692014-11-25 03:36:59 +040089 return false;
90
91 command_instance_->SetResults(obj);
92 return true;
93}
94
Alex Vakulenko2348e422014-11-21 08:57:57 -080095void DBusCommandProxy::Abort() {
Alex Vakulenko4866ac92014-08-20 12:53:33 -070096 LOG(INFO) << "Received call to Command<"
97 << command_instance_->GetName() << ">::Abort()";
Alex Vakulenkof6b38712014-09-03 16:23:38 -070098 command_instance_->Abort();
Alex Vakulenko4866ac92014-08-20 12:53:33 -070099}
100
Alex Vakulenko2348e422014-11-21 08:57:57 -0800101void DBusCommandProxy::Cancel() {
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700102 LOG(INFO) << "Received call to Command<"
103 << command_instance_->GetName() << ">::Cancel()";
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700104 command_instance_->Cancel();
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700105}
106
Alex Vakulenko2348e422014-11-21 08:57:57 -0800107void DBusCommandProxy::Done() {
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700108 LOG(INFO) << "Received call to Command<"
109 << command_instance_->GetName() << ">::Done()";
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700110 command_instance_->Done();
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700111}
112
113
114} // namespace buffet