blob: 0b97c06ab1c975a0b867c8b4b1ab449ae4dc4467 [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
10#include "buffet/commands/command_instance.h"
11#include "buffet/commands/prop_constraints.h"
12#include "buffet/commands/prop_types.h"
Alex Vakulenko89d9d5e2014-09-12 10:27:23 -070013#include "buffet/libbuffet/dbus_constants.h"
Alex Vakulenko4866ac92014-08-20 12:53:33 -070014
15using chromeos::dbus_utils::AsyncEventSequencer;
16using chromeos::dbus_utils::ExportedObjectManager;
17
18namespace buffet {
19
20DBusCommandProxy::DBusCommandProxy(ExportedObjectManager* object_manager,
21 const scoped_refptr<dbus::Bus>& bus,
22 CommandInstance* command_instance)
23 : object_path_(dbus_constants::kCommandServicePathPrefix +
24 command_instance->GetID()),
25 command_instance_(command_instance),
26 dbus_object_(object_manager, bus, object_path_) {
27}
28
29void DBusCommandProxy::RegisterAsync(
30 const AsyncEventSequencer::CompletionAction& completion_callback) {
31 chromeos::dbus_utils::DBusInterface* itf =
32 dbus_object_.AddOrGetInterface(dbus_constants::kCommandInterface);
33
34 // DBus methods.
35 itf->AddMethodHandler(dbus_constants::kCommandSetProgress,
36 base::Unretained(this),
37 &DBusCommandProxy::HandleSetProgress);
38 itf->AddMethodHandler(dbus_constants::kCommandAbort,
39 base::Unretained(this),
40 &DBusCommandProxy::HandleAbort);
41 itf->AddMethodHandler(dbus_constants::kCommandCancel,
42 base::Unretained(this),
43 &DBusCommandProxy::HandleCancel);
44 itf->AddMethodHandler(dbus_constants::kCommandDone,
45 base::Unretained(this),
46 &DBusCommandProxy::HandleDone);
47
48 // DBus properties.
49 itf->AddProperty(dbus_constants::kCommandName, &name_);
50 itf->AddProperty(dbus_constants::kCommandCategory, &category_);
51 itf->AddProperty(dbus_constants::kCommandId, &id_);
52 itf->AddProperty(dbus_constants::kCommandStatus, &status_);
53 itf->AddProperty(dbus_constants::kCommandProgress, &progress_);
Alex Vakulenkof6b38712014-09-03 16:23:38 -070054 itf->AddProperty(dbus_constants::kCommandParameters, &parameters_);
Alex Vakulenko4866ac92014-08-20 12:53:33 -070055
56 // Set the initial property values before registering the DBus object.
57 name_.SetValue(command_instance_->GetName());
58 category_.SetValue(command_instance_->GetCategory());
59 id_.SetValue(command_instance_->GetID());
Alex Vakulenkof6b38712014-09-03 16:23:38 -070060 status_.SetValue(command_instance_->GetStatus());
61 progress_.SetValue(command_instance_->GetProgress());
62 // Convert a string-to-PropValue map into a string-to-Any map which can be
63 // sent over D-Bus.
Alex Vakulenko576c9792014-09-22 16:49:45 -070064 chromeos::VariantDictionary params;
Alex Vakulenkof6b38712014-09-03 16:23:38 -070065 for (const auto& param_pair : command_instance_->GetParameters()) {
66 params.insert(std::make_pair(param_pair.first,
67 param_pair.second->GetValueAsAny()));
68 }
69 parameters_.SetValue(params);
Alex Vakulenko4866ac92014-08-20 12:53:33 -070070
71 // Register the command DBus object and expose its methods and properties.
72 dbus_object_.RegisterAsync(completion_callback);
73}
74
Alex Vakulenkof6b38712014-09-03 16:23:38 -070075void DBusCommandProxy::OnStatusChanged(const std::string& status) {
76 status_.SetValue(status);
77}
78
79void DBusCommandProxy::OnProgressChanged(int progress) {
80 progress_.SetValue(progress);
81}
82
Alex Vakulenko4866ac92014-08-20 12:53:33 -070083void DBusCommandProxy::HandleSetProgress(chromeos::ErrorPtr* error,
84 int32_t progress) {
85 LOG(INFO) << "Received call to Command<"
86 << command_instance_->GetName() << ">::SetProgress("
87 << progress << ")";
88
89 // Validate |progress| parameter. Its value must be between 0 and 100.
90 IntPropType progress_type;
91 progress_type.AddMinMaxConstraint(0, 100);
92 if (progress_type.ValidateValue(progress, error)) {
Alex Vakulenkof6b38712014-09-03 16:23:38 -070093 command_instance_->SetProgress(progress);
Alex Vakulenko4866ac92014-08-20 12:53:33 -070094 }
95}
96
97void DBusCommandProxy::HandleAbort(chromeos::ErrorPtr* error) {
98 LOG(INFO) << "Received call to Command<"
99 << command_instance_->GetName() << ">::Abort()";
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700100 command_instance_->Abort();
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700101}
102
103void DBusCommandProxy::HandleCancel(chromeos::ErrorPtr* error) {
104 LOG(INFO) << "Received call to Command<"
105 << command_instance_->GetName() << ">::Cancel()";
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700106 command_instance_->Cancel();
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700107}
108
109void DBusCommandProxy::HandleDone(chromeos::ErrorPtr* error) {
110 LOG(INFO) << "Received call to Command<"
111 << command_instance_->GetName() << ">::Done()";
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700112 command_instance_->Done();
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700113}
114
115
116} // namespace buffet