blob: e0a9b7a17288f4a41aaa983a502fe61bd013d5a4 [file] [log] [blame]
Christopher Wiley4b5f04c2014-03-27 14:45:37 -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/manager.h"
6
7#include <base/bind.h>
8#include <base/bind_helpers.h>
9
10#include "buffet/dbus_constants.h"
11#include "buffet/dbus_manager.h"
12#include "buffet/dbus_utils.h"
13
14using buffet::dbus_utils::GetBadArgsError;
15
16namespace buffet {
17
18Manager::Manager(DBusManager* dbus_manager) : dbus_manager_(dbus_manager) {
19 dbus::ExportedObject* exported_object = dbus_manager_->GetExportedObject(
20 dbus_constants::kManagerServicePath);
21 dbus_manager_->ExportDBusMethod(exported_object,
22 dbus_constants::kManagerInterface,
23 dbus_constants::kManagerRegisterDeviceMethod,
24 base::Bind(&Manager::HandleRegisterDevice,
25 base::Unretained(this)));
26 dbus_manager_->ExportDBusMethod(exported_object,
27 dbus_constants::kManagerInterface,
28 dbus_constants::kManagerUpdateStateMethod,
29 base::Bind(&Manager::HandleUpdateState,
30 base::Unretained(this)));
Christopher Wiley106686a2014-03-27 14:51:26 -070031 properties_.reset(new Properties(exported_object));
32 // TODO(wiley): Initialize all properties appropriately before claiming
33 // the properties interface.
34 properties_->state_.SetValue("{}");
35 properties_->ClaimPropertiesInterface();
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070036}
37
38Manager::~Manager() {
Christopher Wiley106686a2014-03-27 14:51:26 -070039 // Prevent the properties object from making calls to the exported object.
40 properties_.reset(nullptr);
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070041 // Unregister ourselves from the Bus. This prevents the bus from calling
42 // our callbacks in between the Manager's death and the bus unregistering
43 // our exported object on shutdown. Unretained makes no promises of memory
44 // management.
45 auto exported_object = dbus_manager_->GetExportedObject(
46 dbus_constants::kManagerServicePath);
47 exported_object->Unregister();
48}
49
50scoped_ptr<dbus::Response> Manager::HandleRegisterDevice(
51 dbus::MethodCall* method_call) {
52 // Read the parameters to the method.
53 dbus::MessageReader reader(method_call);
54 if (!reader.HasMoreData()) {
55 return GetBadArgsError(method_call, "No parameters to RegisterDevice");
56 }
57 std::string client_id, client_secret, api_key;
58 if (!reader.PopString(&client_id)) {
59 return GetBadArgsError(method_call, "Failed to read client_id");
60 }
61 if (!reader.PopString(&client_secret)) {
62 return GetBadArgsError(method_call, "Failed to read client_secret");
63 }
64 if (!reader.PopString(&api_key)) {
65 return GetBadArgsError(method_call, "Failed to read api_key");
66 }
67 if (reader.HasMoreData()) {
68 return GetBadArgsError(
69 method_call, "Too many parameters to RegisterDevice");
70 }
71
72 LOG(INFO) << "Received call to Manager.RegisterDevice()";
73 // TODO(wiley): Do something with these parameters to register the device.
74
75 // Send back our response.
76 scoped_ptr<dbus::Response> response(
77 dbus::Response::FromMethodCall(method_call));
78 dbus::MessageWriter writer(response.get());
79 writer.AppendString("<registration ticket id>");
80 return response.Pass();
81}
82
83scoped_ptr<dbus::Response> Manager::HandleUpdateState(
84 dbus::MethodCall *method_call) {
85 // Read the parameters to the method.
86 dbus::MessageReader reader(method_call);
87 if (!reader.HasMoreData()) {
88 return GetBadArgsError(method_call, "No parameters to UpdateState");
89 }
90 std::string json_state_fragment;
91 if (!reader.PopString(&json_state_fragment)) {
92 return GetBadArgsError(method_call, "Failed to read json_state_fragment");
93 }
94 if (reader.HasMoreData()) {
95 return GetBadArgsError(method_call, "Too many parameters to UpdateState");
96 }
97
98 LOG(INFO) << "Received call to Manager.UpdateState()";
Christopher Wiley106686a2014-03-27 14:51:26 -070099 // TODO(wiley): Merge json state blobs intelligently.
100 properties_->state_.SetValue(json_state_fragment);
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700101
102 // Send back our response.
103 return dbus::Response::FromMethodCall(method_call);
104}
105
106} // namespace buffet