blob: a06777242534366d582de510e7b786ba966f1da5 [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());
38 dbus_adaptor_.SetProgress(command_instance_->GetProgress());
Anton Muhincfde8692014-11-25 03:36:59 +040039
40 dbus_adaptor_.SetParameters(ObjectToDBusVariant(
41 command_instance_->GetParameters()));
42 dbus_adaptor_.SetResults(ObjectToDBusVariant(
43 command_instance_->GetResults()));
Alex Vakulenko4866ac92014-08-20 12:53:33 -070044
45 // Register the command DBus object and expose its methods and properties.
46 dbus_object_.RegisterAsync(completion_callback);
47}
48
Anton Muhincfde8692014-11-25 03:36:59 +040049void DBusCommandProxy::OnResultsChanged(const native_types::Object& results) {
50 dbus_adaptor_.SetResults(ObjectToDBusVariant(results));
51}
52
Alex Vakulenkof6b38712014-09-03 16:23:38 -070053void DBusCommandProxy::OnStatusChanged(const std::string& status) {
Alex Vakulenko2348e422014-11-21 08:57:57 -080054 dbus_adaptor_.SetStatus(status);
Alex Vakulenkof6b38712014-09-03 16:23:38 -070055}
56
57void DBusCommandProxy::OnProgressChanged(int progress) {
Alex Vakulenko2348e422014-11-21 08:57:57 -080058 dbus_adaptor_.SetProgress(progress);
Alex Vakulenkof6b38712014-09-03 16:23:38 -070059}
60
Alex Vakulenko2348e422014-11-21 08:57:57 -080061bool DBusCommandProxy::SetProgress(chromeos::ErrorPtr* error,
62 int32_t progress) {
Alex Vakulenko4866ac92014-08-20 12:53:33 -070063 LOG(INFO) << "Received call to Command<"
64 << command_instance_->GetName() << ">::SetProgress("
65 << progress << ")";
66
67 // Validate |progress| parameter. Its value must be between 0 and 100.
68 IntPropType progress_type;
69 progress_type.AddMinMaxConstraint(0, 100);
Alex Vakulenko5c7bf012014-10-30 16:28:38 -070070 if (!progress_type.ValidateValue(progress, error))
71 return false;
72
73 command_instance_->SetProgress(progress);
74 return true;
Alex Vakulenko4866ac92014-08-20 12:53:33 -070075}
76
Anton Muhincfde8692014-11-25 03:36:59 +040077bool DBusCommandProxy::SetResults(chromeos::ErrorPtr* error,
78 const chromeos::VariantDictionary& results) {
79 LOG(INFO) << "Received call to Command<"
80 << command_instance_->GetName() << ">::SetResults()";
81
82 auto results_schema = command_instance_->GetCommandDefinition()->GetResults();
83 native_types::Object obj;
84 if (!ObjectFromDBusVariant(results_schema.get(), results, &obj, error))
85 return false;
86
87 command_instance_->SetResults(obj);
88 return true;
89}
90
Alex Vakulenko2348e422014-11-21 08:57:57 -080091void DBusCommandProxy::Abort() {
Alex Vakulenko4866ac92014-08-20 12:53:33 -070092 LOG(INFO) << "Received call to Command<"
93 << command_instance_->GetName() << ">::Abort()";
Alex Vakulenkof6b38712014-09-03 16:23:38 -070094 command_instance_->Abort();
Alex Vakulenko4866ac92014-08-20 12:53:33 -070095}
96
Alex Vakulenko2348e422014-11-21 08:57:57 -080097void DBusCommandProxy::Cancel() {
Alex Vakulenko4866ac92014-08-20 12:53:33 -070098 LOG(INFO) << "Received call to Command<"
99 << command_instance_->GetName() << ">::Cancel()";
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700100 command_instance_->Cancel();
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700101}
102
Alex Vakulenko2348e422014-11-21 08:57:57 -0800103void DBusCommandProxy::Done() {
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700104 LOG(INFO) << "Received call to Command<"
105 << command_instance_->GetName() << ">::Done()";
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700106 command_instance_->Done();
Alex Vakulenko4866ac92014-08-20 12:53:33 -0700107}
108
109
110} // namespace buffet