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 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 7 | #include <map> |
| 8 | #include <string> |
| 9 | |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 10 | #include <base/bind.h> |
| 11 | #include <base/bind_helpers.h> |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 12 | #include <base/json/json_writer.h> |
| 13 | #include <dbus/bus.h> |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 14 | #include <dbus/object_path.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 15 | #include <dbus/values_util.h> |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 16 | |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 17 | #include "buffet/async_event_sequencer.h" |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 18 | #include "buffet/commands/command_manager.h" |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 19 | #include "buffet/dbus_constants.h" |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 20 | #include "buffet/dbus_utils.h" |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 21 | #include "buffet/error.h" |
Christopher Wiley | adb901d | 2014-05-07 09:58:45 -0700 | [diff] [blame] | 22 | #include "buffet/exported_object_manager.h" |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 23 | |
Christopher Wiley | adb901d | 2014-05-07 09:58:45 -0700 | [diff] [blame] | 24 | using buffet::dbus_utils::AsyncEventSequencer; |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 25 | using buffet::dbus_utils::GetBadArgsError; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 26 | using buffet::dbus_utils::GetDBusError; |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 27 | |
| 28 | namespace buffet { |
| 29 | |
Christopher Wiley | adb901d | 2014-05-07 09:58:45 -0700 | [diff] [blame] | 30 | Manager::Manager( |
| 31 | scoped_refptr<dbus::Bus> bus, |
| 32 | base::WeakPtr<dbus_utils::ExportedObjectManager> object_manager) |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 33 | : bus_(bus), |
| 34 | exported_object_(bus->GetExportedObject( |
Christopher Wiley | adb901d | 2014-05-07 09:58:45 -0700 | [diff] [blame] | 35 | dbus::ObjectPath(dbus_constants::kManagerServicePath))), |
| 36 | object_manager_(object_manager) { } |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 37 | |
| 38 | Manager::~Manager() { |
Christopher Wiley | adb901d | 2014-05-07 09:58:45 -0700 | [diff] [blame] | 39 | object_manager_->ReleaseInterface( |
| 40 | dbus::ObjectPath(dbus_constants::kManagerServicePath), |
| 41 | dbus_constants::kManagerInterface); |
Christopher Wiley | aa3f29c | 2014-03-27 14:51:26 -0700 | [diff] [blame] | 42 | // Prevent the properties object from making calls to the exported object. |
| 43 | properties_.reset(nullptr); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 44 | // Unregister ourselves from the Bus. This prevents the bus from calling |
| 45 | // our callbacks in between the Manager's death and the bus unregistering |
| 46 | // our exported object on shutdown. Unretained makes no promises of memory |
| 47 | // management. |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 48 | exported_object_->Unregister(); |
| 49 | exported_object_ = nullptr; |
| 50 | } |
| 51 | |
| 52 | void Manager::Init(const OnInitFinish& cb) { |
Christopher Wiley | adb901d | 2014-05-07 09:58:45 -0700 | [diff] [blame] | 53 | scoped_refptr<AsyncEventSequencer> sequencer( |
| 54 | new AsyncEventSequencer()); |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 55 | exported_object_->ExportMethod( |
| 56 | dbus_constants::kManagerInterface, |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 57 | dbus_constants::kManagerCheckDeviceRegistered, |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 58 | dbus_utils::GetExportableDBusMethod( |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 59 | base::Bind(&Manager::HandleCheckDeviceRegistered, |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 60 | base::Unretained(this))), |
| 61 | sequencer->GetExportHandler( |
| 62 | dbus_constants::kManagerInterface, |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 63 | dbus_constants::kManagerCheckDeviceRegistered, |
| 64 | "Failed exporting CheckDeviceRegistered method", |
| 65 | true)); |
| 66 | exported_object_->ExportMethod( |
| 67 | dbus_constants::kManagerInterface, |
| 68 | dbus_constants::kManagerGetDeviceInfo, |
| 69 | dbus_utils::GetExportableDBusMethod( |
| 70 | base::Bind(&Manager::HandleGetDeviceInfo, |
| 71 | base::Unretained(this))), |
| 72 | sequencer->GetExportHandler( |
| 73 | dbus_constants::kManagerInterface, |
| 74 | dbus_constants::kManagerGetDeviceInfo, |
| 75 | "Failed exporting GetDeviceInfo method", |
| 76 | true)); |
| 77 | exported_object_->ExportMethod( |
| 78 | dbus_constants::kManagerInterface, |
| 79 | dbus_constants::kManagerStartRegisterDevice, |
| 80 | dbus_utils::GetExportableDBusMethod( |
| 81 | base::Bind(&Manager::HandleStartRegisterDevice, |
| 82 | base::Unretained(this))), |
| 83 | sequencer->GetExportHandler( |
| 84 | dbus_constants::kManagerInterface, |
| 85 | dbus_constants::kManagerStartRegisterDevice, |
| 86 | "Failed exporting StartRegisterDevice method", |
| 87 | true)); |
| 88 | exported_object_->ExportMethod( |
| 89 | dbus_constants::kManagerInterface, |
| 90 | dbus_constants::kManagerFinishRegisterDevice, |
| 91 | dbus_utils::GetExportableDBusMethod( |
| 92 | base::Bind(&Manager::HandleFinishRegisterDevice, |
| 93 | base::Unretained(this))), |
| 94 | sequencer->GetExportHandler( |
| 95 | dbus_constants::kManagerInterface, |
| 96 | dbus_constants::kManagerFinishRegisterDevice, |
| 97 | "Failed exporting FinishRegisterDevice method", |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 98 | true)); |
| 99 | exported_object_->ExportMethod( |
| 100 | dbus_constants::kManagerInterface, |
| 101 | dbus_constants::kManagerUpdateStateMethod, |
| 102 | dbus_utils::GetExportableDBusMethod( |
| 103 | base::Bind(&Manager::HandleUpdateState, |
| 104 | base::Unretained(this))), |
| 105 | sequencer->GetExportHandler( |
| 106 | dbus_constants::kManagerInterface, |
| 107 | dbus_constants::kManagerUpdateStateMethod, |
| 108 | "Failed exporting UpdateState method", |
| 109 | true)); |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 110 | exported_object_->ExportMethod( |
| 111 | dbus_constants::kManagerInterface, dbus_constants::kManagerTestMethod, |
| 112 | dbus_utils::GetExportableDBusMethod( |
| 113 | base::Bind(&Manager::HandleTestMethod, base::Unretained(this))), |
| 114 | sequencer->GetExportHandler( |
| 115 | dbus_constants::kManagerInterface, dbus_constants::kManagerTestMethod, |
| 116 | "Failed exporting TestMethod method", |
| 117 | true)); |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 118 | properties_.reset(new Properties(bus_)); |
| 119 | // TODO(wiley): Initialize all properties appropriately before claiming |
| 120 | // the properties interface. |
| 121 | properties_->state_.SetValue("{}"); |
| 122 | properties_->Init( |
| 123 | sequencer->GetHandler("Manager properties export failed.", true)); |
Christopher Wiley | adb901d | 2014-05-07 09:58:45 -0700 | [diff] [blame] | 124 | auto claim_interface_task = sequencer->WrapCompletionTask( |
| 125 | base::Bind(&dbus_utils::ExportedObjectManager::ClaimInterface, |
| 126 | object_manager_->AsWeakPtr(), |
| 127 | dbus::ObjectPath(dbus_constants::kManagerServicePath), |
| 128 | dbus_constants::kManagerInterface, |
| 129 | properties_->GetPropertyWriter( |
| 130 | dbus_constants::kManagerInterface))); |
| 131 | sequencer->OnAllTasksCompletedCall({claim_interface_task, cb}); |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 132 | command_manager_ = std::make_shared<CommandManager>(); |
| 133 | command_manager_->Startup(); |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame^] | 134 | device_info_ = std::unique_ptr<DeviceRegistrationInfo>( |
| 135 | new DeviceRegistrationInfo(command_manager_)); |
| 136 | device_info_->Load(); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 139 | scoped_ptr<dbus::Response> Manager::HandleCheckDeviceRegistered( |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 140 | dbus::MethodCall* method_call) { |
| 141 | // Read the parameters to the method. |
| 142 | dbus::MessageReader reader(method_call); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 143 | if (reader.HasMoreData()) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 144 | return GetBadArgsError(method_call, |
| 145 | "Too many parameters to CheckDeviceRegistered"); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 148 | LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()"; |
| 149 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 150 | buffet::ErrorPtr error; |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame^] | 151 | bool registered = device_info_->CheckRegistration(&error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 152 | // If it fails due to any reason other than 'device not registered', |
| 153 | // treat it as a real error and report it to the caller. |
| 154 | if (!registered && |
| 155 | !error->HasError(kErrorDomainGCD, "device_not_registered")) { |
| 156 | return GetDBusError(method_call, error.get()); |
| 157 | } |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 158 | |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 159 | std::string device_id; |
| 160 | if (registered) { |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame^] | 161 | device_id = device_info_->GetDeviceId(&error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 162 | if (device_id.empty()) |
| 163 | return GetDBusError(method_call, error.get()); |
| 164 | } |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 165 | // Send back our response. |
| 166 | scoped_ptr<dbus::Response> response( |
| 167 | dbus::Response::FromMethodCall(method_call)); |
| 168 | dbus::MessageWriter writer(response.get()); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 169 | writer.AppendString(device_id); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 170 | return response.Pass(); |
| 171 | } |
| 172 | |
| 173 | scoped_ptr<dbus::Response> Manager::HandleGetDeviceInfo( |
| 174 | dbus::MethodCall* method_call) { |
| 175 | // Read the parameters to the method. |
| 176 | dbus::MessageReader reader(method_call); |
| 177 | if (reader.HasMoreData()) { |
| 178 | return GetBadArgsError(method_call, |
| 179 | "Too many parameters to GetDeviceInfo"); |
| 180 | } |
| 181 | |
| 182 | LOG(INFO) << "Received call to Manager.GetDeviceInfo()"; |
| 183 | |
| 184 | std::string device_info_str; |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 185 | buffet::ErrorPtr error; |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame^] | 186 | auto device_info = device_info_->GetDeviceInfo(&error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 187 | if (!device_info) |
| 188 | return GetDBusError(method_call, error.get()); |
| 189 | |
| 190 | base::JSONWriter::Write(device_info.get(), &device_info_str); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 191 | |
| 192 | // Send back our response. |
| 193 | scoped_ptr<dbus::Response> response( |
| 194 | dbus::Response::FromMethodCall(method_call)); |
| 195 | dbus::MessageWriter writer(response.get()); |
| 196 | writer.AppendString(device_info_str); |
| 197 | return response.Pass(); |
| 198 | } |
| 199 | |
| 200 | scoped_ptr<dbus::Response> Manager::HandleStartRegisterDevice( |
| 201 | dbus::MethodCall* method_call) { |
| 202 | // Read the parameters to the method. |
| 203 | dbus::MessageReader reader(method_call); |
| 204 | if (!reader.HasMoreData()) { |
| 205 | return GetBadArgsError(method_call, "No parameters to StartRegisterDevice"); |
| 206 | } |
| 207 | |
| 208 | dbus::MessageReader array_reader(nullptr); |
| 209 | if (!reader.PopArray(&array_reader)) |
| 210 | return GetBadArgsError(method_call, "Failed to read the parameter array"); |
| 211 | std::map<std::string, std::shared_ptr<base::Value>> params; |
| 212 | while (array_reader.HasMoreData()) { |
| 213 | dbus::MessageReader dict_entry_reader(nullptr); |
| 214 | if (!array_reader.PopDictEntry(&dict_entry_reader)) |
| 215 | return GetBadArgsError(method_call, "Failed to get a call parameter"); |
| 216 | std::string key; |
| 217 | if (!dict_entry_reader.PopString(&key)) |
| 218 | return GetBadArgsError(method_call, "Failed to read parameter key"); |
| 219 | base::Value* value = dbus::PopDataAsValue(&dict_entry_reader); |
| 220 | if (!value) |
| 221 | return GetBadArgsError(method_call, "Failed to read parameter value"); |
| 222 | params.insert(std::make_pair(key, std::shared_ptr<base::Value>(value))); |
| 223 | } |
| 224 | if (reader.HasMoreData()) |
| 225 | return GetBadArgsError(method_call, |
| 226 | "Too many parameters to StartRegisterDevice"); |
| 227 | |
| 228 | LOG(INFO) << "Received call to Manager.StartRegisterDevice()"; |
| 229 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 230 | buffet::ErrorPtr error; |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame^] | 231 | std::string id = device_info_->StartRegistration(params, &error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 232 | if (id.empty()) |
| 233 | return GetDBusError(method_call, error.get()); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 234 | |
| 235 | // Send back our response. |
| 236 | scoped_ptr<dbus::Response> response( |
| 237 | dbus::Response::FromMethodCall(method_call)); |
| 238 | dbus::MessageWriter writer(response.get()); |
| 239 | writer.AppendString(id); |
| 240 | return response.Pass(); |
| 241 | } |
| 242 | |
| 243 | scoped_ptr<dbus::Response> Manager::HandleFinishRegisterDevice( |
| 244 | dbus::MethodCall* method_call) { |
| 245 | // Read the parameters to the method. |
| 246 | dbus::MessageReader reader(method_call); |
| 247 | if (!reader.HasMoreData()) { |
| 248 | return GetBadArgsError(method_call, |
| 249 | "No parameters to FinishRegisterDevice"); |
| 250 | } |
| 251 | std::string user_auth_code; |
| 252 | if (!reader.PopString(&user_auth_code)) { |
| 253 | return GetBadArgsError(method_call, "Failed to read UserAuthCode"); |
| 254 | } |
| 255 | if (reader.HasMoreData()) { |
| 256 | return GetBadArgsError(method_call, |
| 257 | "Too many parameters to FinishRegisterDevice"); |
| 258 | } |
| 259 | |
| 260 | LOG(INFO) << "Received call to Manager.FinishRegisterDevice()"; |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 261 | buffet::ErrorPtr error; |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame^] | 262 | if (!device_info_->FinishRegistration(user_auth_code, &error)) |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 263 | return GetDBusError(method_call, error.get()); |
| 264 | |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame^] | 265 | std::string device_id = device_info_->GetDeviceId(&error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 266 | if (device_id.empty()) |
| 267 | return GetDBusError(method_call, error.get()); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 268 | |
| 269 | // Send back our response. |
| 270 | scoped_ptr<dbus::Response> response( |
| 271 | dbus::Response::FromMethodCall(method_call)); |
| 272 | dbus::MessageWriter writer(response.get()); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 273 | writer.AppendString(device_id); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 274 | return response.Pass(); |
| 275 | } |
| 276 | |
| 277 | scoped_ptr<dbus::Response> Manager::HandleUpdateState( |
| 278 | dbus::MethodCall *method_call) { |
| 279 | // Read the parameters to the method. |
| 280 | dbus::MessageReader reader(method_call); |
| 281 | if (!reader.HasMoreData()) { |
| 282 | return GetBadArgsError(method_call, "No parameters to UpdateState"); |
| 283 | } |
| 284 | std::string json_state_fragment; |
| 285 | if (!reader.PopString(&json_state_fragment)) { |
| 286 | return GetBadArgsError(method_call, "Failed to read json_state_fragment"); |
| 287 | } |
| 288 | if (reader.HasMoreData()) { |
| 289 | return GetBadArgsError(method_call, "Too many parameters to UpdateState"); |
| 290 | } |
| 291 | |
| 292 | LOG(INFO) << "Received call to Manager.UpdateState()"; |
Christopher Wiley | aa3f29c | 2014-03-27 14:51:26 -0700 | [diff] [blame] | 293 | // TODO(wiley): Merge json state blobs intelligently. |
| 294 | properties_->state_.SetValue(json_state_fragment); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 295 | |
| 296 | // Send back our response. |
| 297 | return dbus::Response::FromMethodCall(method_call); |
| 298 | } |
| 299 | |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 300 | scoped_ptr<dbus::Response> Manager::HandleTestMethod( |
| 301 | dbus::MethodCall* method_call) { |
| 302 | LOG(INFO) << "Received call to test method."; |
| 303 | return scoped_ptr<dbus::Response>(); |
| 304 | } |
| 305 | |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 306 | } // namespace buffet |