blob: d53355a1b66779c04b2f1db495f2d5d7b0709ae2 [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
Vitaly Bukaf9d50292015-07-27 16:08:51 -07005#include "buffet/dbus_command_proxy.h"
Alex Vakulenko4866ac92014-08-20 12:53:33 -07006
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
Vitaly Bukaf9d50292015-07-27 16:08:51 -070010#include "buffet/dbus_conversion.h"
Vitaly Buka15f59092015-07-24 16:54:32 -070011#include "weave/enum_to_string.h"
Alex Vakulenko4866ac92014-08-20 12:53:33 -070012
13using chromeos::dbus_utils::AsyncEventSequencer;
14using chromeos::dbus_utils::ExportedObjectManager;
15
Vitaly Bukaf9d50292015-07-27 16:08:51 -070016namespace buffet {
Alex Vakulenko4866ac92014-08-20 12:53:33 -070017
18DBusCommandProxy::DBusCommandProxy(ExportedObjectManager* object_manager,
19 const scoped_refptr<dbus::Bus>& bus,
Vitaly Bukaf9d50292015-07-27 16:08:51 -070020 weave::Command* command,
Anton Muhin5191e812014-10-30 17:49:48 +040021 std::string object_path)
Vitaly Buka4f4e2282015-07-23 17:50:07 -070022 : command_{command},
Vitaly Buka12affd82015-07-23 18:45:35 -070023 dbus_object_{object_manager, bus, dbus::ObjectPath{object_path}} {}
Alex Vakulenko4866ac92014-08-20 12:53:33 -070024
25void DBusCommandProxy::RegisterAsync(
Vitaly Bukaa647c852015-07-06 14:51:01 -070026 const AsyncEventSequencer::CompletionAction& completion_callback) {
Alex Vakulenko2348e422014-11-21 08:57:57 -080027 dbus_adaptor_.RegisterWithDBusObject(&dbus_object_);
Alex Vakulenko4866ac92014-08-20 12:53:33 -070028
29 // Set the initial property values before registering the DBus object.
Vitaly Buka4f4e2282015-07-23 17:50:07 -070030 dbus_adaptor_.SetName(command_->GetName());
31 dbus_adaptor_.SetCategory(command_->GetCategory());
32 dbus_adaptor_.SetId(command_->GetID());
Vitaly Buka15f59092015-07-24 16:54:32 -070033 dbus_adaptor_.SetStatus(EnumToString(command_->GetStatus()));
Vitaly Buka4129dfa2015-04-29 12:16:58 -070034 dbus_adaptor_.SetProgress(
Vitaly Buka4f4e2282015-07-23 17:50:07 -070035 DictionaryToDBusVariantDictionary(*command_->GetProgress()));
Vitaly Buka15f59092015-07-24 16:54:32 -070036 dbus_adaptor_.SetOrigin(EnumToString(command_->GetOrigin()));
Vitaly Bukaa647c852015-07-06 14:51:01 -070037 dbus_adaptor_.SetParameters(
Vitaly Buka4f4e2282015-07-23 17:50:07 -070038 DictionaryToDBusVariantDictionary(*command_->GetParameters()));
Vitaly Bukaa647c852015-07-06 14:51:01 -070039 dbus_adaptor_.SetResults(
Vitaly Buka4f4e2282015-07-23 17:50:07 -070040 DictionaryToDBusVariantDictionary(*command_->GetResults()));
Alex Vakulenko4866ac92014-08-20 12:53:33 -070041
42 // Register the command DBus object and expose its methods and properties.
43 dbus_object_.RegisterAsync(completion_callback);
44}
45
Alex Vakulenkob211c102015-04-21 11:43:23 -070046void DBusCommandProxy::OnResultsChanged() {
47 dbus_adaptor_.SetResults(
Vitaly Buka4f4e2282015-07-23 17:50:07 -070048 DictionaryToDBusVariantDictionary(*command_->GetResults()));
Anton Muhincfde8692014-11-25 03:36:59 +040049}
50
Alex Vakulenkob211c102015-04-21 11:43:23 -070051void DBusCommandProxy::OnStatusChanged() {
Vitaly Buka15f59092015-07-24 16:54:32 -070052 dbus_adaptor_.SetStatus(EnumToString(command_->GetStatus()));
Alex Vakulenkof6b38712014-09-03 16:23:38 -070053}
54
Alex Vakulenkob211c102015-04-21 11:43:23 -070055void DBusCommandProxy::OnProgressChanged() {
Vitaly Buka4129dfa2015-04-29 12:16:58 -070056 dbus_adaptor_.SetProgress(
Vitaly Buka4f4e2282015-07-23 17:50:07 -070057 DictionaryToDBusVariantDictionary(*command_->GetProgress()));
Alex Vakulenkof6b38712014-09-03 16:23:38 -070058}
59
Vitaly Bukac3d4e972015-07-21 09:55:25 -070060void DBusCommandProxy::OnCommandDestroyed() {
61 delete this;
62}
63
Vitaly Buka4129dfa2015-04-29 12:16:58 -070064bool DBusCommandProxy::SetProgress(
65 chromeos::ErrorPtr* error,
66 const chromeos::VariantDictionary& progress) {
Vitaly Buka4f4e2282015-07-23 17:50:07 -070067 LOG(INFO) << "Received call to Command<" << command_->GetName()
Vitaly Buka4129dfa2015-04-29 12:16:58 -070068 << ">::SetProgress()";
Vitaly Buka4f4e2282015-07-23 17:50:07 -070069 auto dictionary = DictionaryFromDBusVariantDictionary(progress, error);
70 if (!dictionary)
Alex Vakulenko5c7bf012014-10-30 16:28:38 -070071 return false;
Vitaly Buka4f4e2282015-07-23 17:50:07 -070072 return command_->SetProgress(*dictionary, error);
Alex Vakulenko4866ac92014-08-20 12:53:33 -070073}
74
Anton Muhincfde8692014-11-25 03:36:59 +040075bool DBusCommandProxy::SetResults(chromeos::ErrorPtr* error,
76 const chromeos::VariantDictionary& results) {
Vitaly Buka4f4e2282015-07-23 17:50:07 -070077 LOG(INFO) << "Received call to Command<" << command_->GetName()
Vitaly Bukaa647c852015-07-06 14:51:01 -070078 << ">::SetResults()";
Vitaly Buka4f4e2282015-07-23 17:50:07 -070079 auto dictionary = DictionaryFromDBusVariantDictionary(results, error);
80 if (!dictionary)
Anton Muhincfde8692014-11-25 03:36:59 +040081 return false;
Vitaly Buka4f4e2282015-07-23 17:50:07 -070082 return command_->SetResults(*dictionary, error);
Anton Muhincfde8692014-11-25 03:36:59 +040083}
84
Alex Vakulenko2348e422014-11-21 08:57:57 -080085void DBusCommandProxy::Abort() {
Vitaly Buka4f4e2282015-07-23 17:50:07 -070086 LOG(INFO) << "Received call to Command<" << command_->GetName()
Vitaly Bukaa647c852015-07-06 14:51:01 -070087 << ">::Abort()";
Vitaly Buka4f4e2282015-07-23 17:50:07 -070088 command_->Abort();
Alex Vakulenko4866ac92014-08-20 12:53:33 -070089}
90
Alex Vakulenko2348e422014-11-21 08:57:57 -080091void DBusCommandProxy::Cancel() {
Vitaly Buka4f4e2282015-07-23 17:50:07 -070092 LOG(INFO) << "Received call to Command<" << command_->GetName()
Vitaly Bukaa647c852015-07-06 14:51:01 -070093 << ">::Cancel()";
Vitaly Buka4f4e2282015-07-23 17:50:07 -070094 command_->Cancel();
Alex Vakulenko4866ac92014-08-20 12:53:33 -070095}
96
Alex Vakulenko2348e422014-11-21 08:57:57 -080097void DBusCommandProxy::Done() {
Vitaly Buka4f4e2282015-07-23 17:50:07 -070098 LOG(INFO) << "Received call to Command<" << command_->GetName()
Vitaly Bukaa647c852015-07-06 14:51:01 -070099 << ">::Done()";
Vitaly Buka4f4e2282015-07-23 17:50:07 -0700100 command_->Done();
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700101}
102
Vitaly Bukaf9d50292015-07-27 16:08:51 -0700103} // namespace buffet