Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 1 | // 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> |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 9 | #include <dbus/object_path.h> |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 10 | |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 11 | #include "buffet/async_event_sequencer.h" |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 12 | #include "buffet/dbus_constants.h" |
| 13 | #include "buffet/dbus_manager.h" |
| 14 | #include "buffet/dbus_utils.h" |
| 15 | |
| 16 | using buffet::dbus_utils::GetBadArgsError; |
| 17 | |
| 18 | namespace buffet { |
| 19 | |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 20 | Manager::Manager(dbus::Bus* bus) |
| 21 | : bus_(bus), |
| 22 | exported_object_(bus->GetExportedObject( |
| 23 | dbus::ObjectPath(dbus_constants::kManagerServicePath))) { } |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 24 | |
| 25 | Manager::~Manager() { |
Christopher Wiley | aa3f29c | 2014-03-27 14:51:26 -0700 | [diff] [blame] | 26 | // Prevent the properties object from making calls to the exported object. |
| 27 | properties_.reset(nullptr); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 28 | // Unregister ourselves from the Bus. This prevents the bus from calling |
| 29 | // our callbacks in between the Manager's death and the bus unregistering |
| 30 | // our exported object on shutdown. Unretained makes no promises of memory |
| 31 | // management. |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 32 | exported_object_->Unregister(); |
| 33 | exported_object_ = nullptr; |
| 34 | } |
| 35 | |
| 36 | void Manager::Init(const OnInitFinish& cb) { |
| 37 | scoped_refptr<dbus_utils::AsyncEventSequencer> sequencer( |
| 38 | new dbus_utils::AsyncEventSequencer()); |
| 39 | exported_object_->ExportMethod( |
| 40 | dbus_constants::kManagerInterface, |
| 41 | dbus_constants::kManagerRegisterDeviceMethod, |
| 42 | dbus_utils::GetExportableDBusMethod( |
| 43 | base::Bind(&Manager::HandleRegisterDevice, |
| 44 | base::Unretained(this))), |
| 45 | sequencer->GetExportHandler( |
| 46 | dbus_constants::kManagerInterface, |
| 47 | dbus_constants::kManagerRegisterDeviceMethod, |
| 48 | "Failed exporting RegisterDevice method", |
| 49 | true)); |
| 50 | exported_object_->ExportMethod( |
| 51 | dbus_constants::kManagerInterface, |
| 52 | dbus_constants::kManagerUpdateStateMethod, |
| 53 | dbus_utils::GetExportableDBusMethod( |
| 54 | base::Bind(&Manager::HandleUpdateState, |
| 55 | base::Unretained(this))), |
| 56 | sequencer->GetExportHandler( |
| 57 | dbus_constants::kManagerInterface, |
| 58 | dbus_constants::kManagerUpdateStateMethod, |
| 59 | "Failed exporting UpdateState method", |
| 60 | true)); |
| 61 | properties_.reset(new Properties(bus_)); |
| 62 | // TODO(wiley): Initialize all properties appropriately before claiming |
| 63 | // the properties interface. |
| 64 | properties_->state_.SetValue("{}"); |
| 65 | properties_->Init( |
| 66 | sequencer->GetHandler("Manager properties export failed.", true)); |
| 67 | sequencer->OnAllTasksCompletedCall({cb}); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | scoped_ptr<dbus::Response> Manager::HandleRegisterDevice( |
| 71 | dbus::MethodCall* method_call) { |
| 72 | // Read the parameters to the method. |
| 73 | dbus::MessageReader reader(method_call); |
| 74 | if (!reader.HasMoreData()) { |
| 75 | return GetBadArgsError(method_call, "No parameters to RegisterDevice"); |
| 76 | } |
| 77 | std::string client_id, client_secret, api_key; |
| 78 | if (!reader.PopString(&client_id)) { |
| 79 | return GetBadArgsError(method_call, "Failed to read client_id"); |
| 80 | } |
| 81 | if (!reader.PopString(&client_secret)) { |
| 82 | return GetBadArgsError(method_call, "Failed to read client_secret"); |
| 83 | } |
| 84 | if (!reader.PopString(&api_key)) { |
| 85 | return GetBadArgsError(method_call, "Failed to read api_key"); |
| 86 | } |
| 87 | if (reader.HasMoreData()) { |
| 88 | return GetBadArgsError( |
| 89 | method_call, "Too many parameters to RegisterDevice"); |
| 90 | } |
| 91 | |
| 92 | LOG(INFO) << "Received call to Manager.RegisterDevice()"; |
| 93 | // TODO(wiley): Do something with these parameters to register the device. |
| 94 | |
| 95 | // Send back our response. |
| 96 | scoped_ptr<dbus::Response> response( |
| 97 | dbus::Response::FromMethodCall(method_call)); |
| 98 | dbus::MessageWriter writer(response.get()); |
| 99 | writer.AppendString("<registration ticket id>"); |
| 100 | return response.Pass(); |
| 101 | } |
| 102 | |
| 103 | scoped_ptr<dbus::Response> Manager::HandleUpdateState( |
| 104 | dbus::MethodCall *method_call) { |
| 105 | // Read the parameters to the method. |
| 106 | dbus::MessageReader reader(method_call); |
| 107 | if (!reader.HasMoreData()) { |
| 108 | return GetBadArgsError(method_call, "No parameters to UpdateState"); |
| 109 | } |
| 110 | std::string json_state_fragment; |
| 111 | if (!reader.PopString(&json_state_fragment)) { |
| 112 | return GetBadArgsError(method_call, "Failed to read json_state_fragment"); |
| 113 | } |
| 114 | if (reader.HasMoreData()) { |
| 115 | return GetBadArgsError(method_call, "Too many parameters to UpdateState"); |
| 116 | } |
| 117 | |
| 118 | LOG(INFO) << "Received call to Manager.UpdateState()"; |
Christopher Wiley | aa3f29c | 2014-03-27 14:51:26 -0700 | [diff] [blame] | 119 | // TODO(wiley): Merge json state blobs intelligently. |
| 120 | properties_->state_.SetValue(json_state_fragment); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 121 | |
| 122 | // Send back our response. |
| 123 | return dbus::Response::FromMethodCall(method_call); |
| 124 | } |
| 125 | |
| 126 | } // namespace buffet |