blob: 4daa9fc54ddb75a25e56419634d1953047d459e1 [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)));
31}
32
33Manager::~Manager() {
34 // Unregister ourselves from the Bus. This prevents the bus from calling
35 // our callbacks in between the Manager's death and the bus unregistering
36 // our exported object on shutdown. Unretained makes no promises of memory
37 // management.
38 auto exported_object = dbus_manager_->GetExportedObject(
39 dbus_constants::kManagerServicePath);
40 exported_object->Unregister();
41}
42
43scoped_ptr<dbus::Response> Manager::HandleRegisterDevice(
44 dbus::MethodCall* method_call) {
45 // Read the parameters to the method.
46 dbus::MessageReader reader(method_call);
47 if (!reader.HasMoreData()) {
48 return GetBadArgsError(method_call, "No parameters to RegisterDevice");
49 }
50 std::string client_id, client_secret, api_key;
51 if (!reader.PopString(&client_id)) {
52 return GetBadArgsError(method_call, "Failed to read client_id");
53 }
54 if (!reader.PopString(&client_secret)) {
55 return GetBadArgsError(method_call, "Failed to read client_secret");
56 }
57 if (!reader.PopString(&api_key)) {
58 return GetBadArgsError(method_call, "Failed to read api_key");
59 }
60 if (reader.HasMoreData()) {
61 return GetBadArgsError(
62 method_call, "Too many parameters to RegisterDevice");
63 }
64
65 LOG(INFO) << "Received call to Manager.RegisterDevice()";
66 // TODO(wiley): Do something with these parameters to register the device.
67
68 // Send back our response.
69 scoped_ptr<dbus::Response> response(
70 dbus::Response::FromMethodCall(method_call));
71 dbus::MessageWriter writer(response.get());
72 writer.AppendString("<registration ticket id>");
73 return response.Pass();
74}
75
76scoped_ptr<dbus::Response> Manager::HandleUpdateState(
77 dbus::MethodCall *method_call) {
78 // Read the parameters to the method.
79 dbus::MessageReader reader(method_call);
80 if (!reader.HasMoreData()) {
81 return GetBadArgsError(method_call, "No parameters to UpdateState");
82 }
83 std::string json_state_fragment;
84 if (!reader.PopString(&json_state_fragment)) {
85 return GetBadArgsError(method_call, "Failed to read json_state_fragment");
86 }
87 if (reader.HasMoreData()) {
88 return GetBadArgsError(method_call, "Too many parameters to UpdateState");
89 }
90
91 LOG(INFO) << "Received call to Manager.UpdateState()";
92 // TODO(wiley): Do something with these parameters to update state.
93
94 // Send back our response.
95 return dbus::Response::FromMethodCall(method_call);
96}
97
98} // namespace buffet