blob: d079392dd7098d2d7a025f7bab4602fa0f92e5e4 [file] [log] [blame]
Vitaly Bukaa0305d32015-07-27 16:08:51 -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/dbus_command_proxy.h"
6
7#include <chromeos/dbus/async_event_sequencer.h>
8#include <chromeos/dbus/exported_object_manager.h>
Vitaly Bukae2713ac2015-08-03 13:50:01 -07009#include <weave/enum_to_string.h>
Vitaly Bukaa0305d32015-07-27 16:08:51 -070010
11#include "buffet/dbus_conversion.h"
Vitaly Buka4f771532015-08-14 14:58:39 -070012#include "buffet/weave_error_conversion.h"
Vitaly Bukaa0305d32015-07-27 16:08:51 -070013
14using chromeos::dbus_utils::AsyncEventSequencer;
15using chromeos::dbus_utils::ExportedObjectManager;
16
17namespace buffet {
18
19DBusCommandProxy::DBusCommandProxy(ExportedObjectManager* object_manager,
20 const scoped_refptr<dbus::Bus>& bus,
21 weave::Command* command,
22 std::string object_path)
23 : command_{command},
Vitaly Bukaf6027cb2015-07-31 16:20:48 -070024 dbus_object_{object_manager, bus, dbus::ObjectPath{object_path}} {
25 observer_.Add(command);
26}
Vitaly Bukaa0305d32015-07-27 16:08:51 -070027
28void 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
49void DBusCommandProxy::OnResultsChanged() {
50 dbus_adaptor_.SetResults(
51 DictionaryToDBusVariantDictionary(*command_->GetResults()));
52}
53
54void DBusCommandProxy::OnStatusChanged() {
55 dbus_adaptor_.SetStatus(EnumToString(command_->GetStatus()));
56}
57
58void DBusCommandProxy::OnProgressChanged() {
59 dbus_adaptor_.SetProgress(
60 DictionaryToDBusVariantDictionary(*command_->GetProgress()));
61}
62
63void DBusCommandProxy::OnCommandDestroyed() {
64 delete this;
65}
66
67bool 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 Buka4f771532015-08-14 14:58:39 -070075 weave::ErrorPtr weave_error;
76 if (!command_->SetProgress(*dictionary, &weave_error)) {
77 ConvertError(*weave_error, error);
78 return false;
79 }
80 return true;
Vitaly Bukaa0305d32015-07-27 16:08:51 -070081}
82
83bool 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 Buka4f771532015-08-14 14:58:39 -070090 weave::ErrorPtr weave_error;
91 if (!command_->SetResults(*dictionary, &weave_error)) {
92 ConvertError(*weave_error, error);
93 return false;
94 }
95 return true;
Vitaly Bukaa0305d32015-07-27 16:08:51 -070096}
97
98void DBusCommandProxy::Abort() {
99 LOG(INFO) << "Received call to Command<" << command_->GetName()
100 << ">::Abort()";
101 command_->Abort();
102}
103
104void DBusCommandProxy::Cancel() {
105 LOG(INFO) << "Received call to Command<" << command_->GetName()
106 << ">::Cancel()";
107 command_->Cancel();
108}
109
110void DBusCommandProxy::Done() {
111 LOG(INFO) << "Received call to Command<" << command_->GetName()
112 << ">::Done()";
113 command_->Done();
114}
115
116} // namespace buffet