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> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 13 | #include <chromeos/dbus/async_event_sequencer.h> |
| 14 | #include <chromeos/dbus/exported_object_manager.h> |
| 15 | #include <chromeos/errors/error.h> |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 16 | #include <dbus/bus.h> |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 17 | #include <dbus/object_path.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 18 | #include <dbus/values_util.h> |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 19 | |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 20 | #include "buffet/commands/command_manager.h" |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 21 | #include "buffet/dbus_constants.h" |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 22 | |
Christopher Wiley | 2d2d92b | 2014-07-29 14:07:10 -0700 | [diff] [blame] | 23 | using chromeos::dbus_utils::AsyncEventSequencer; |
Christopher Wiley | b5dd5ea | 2014-08-11 10:51:20 -0700 | [diff] [blame] | 24 | using chromeos::dbus_utils::ExportedObjectManager; |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 25 | |
| 26 | namespace buffet { |
| 27 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 28 | Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager) |
| 29 | : dbus_object_(object_manager.get(), |
| 30 | object_manager->GetBus(), |
| 31 | dbus::ObjectPath(dbus_constants::kManagerServicePath)) {} |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 32 | |
Alex Vakulenko | be9c383 | 2014-08-24 15:05:06 -0700 | [diff] [blame] | 33 | void Manager::RegisterAsync(const AsyncEventSequencer::CompletionAction& cb) { |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 34 | chromeos::dbus_utils::DBusInterface* itf = |
| 35 | dbus_object_.AddOrGetInterface(dbus_constants::kManagerInterface); |
| 36 | itf->AddMethodHandler(dbus_constants::kManagerCheckDeviceRegistered, |
| 37 | base::Unretained(this), |
| 38 | &Manager::HandleCheckDeviceRegistered); |
| 39 | itf->AddMethodHandler(dbus_constants::kManagerGetDeviceInfo, |
| 40 | base::Unretained(this), |
| 41 | &Manager::HandleGetDeviceInfo); |
| 42 | itf->AddMethodHandler(dbus_constants::kManagerStartRegisterDevice, |
| 43 | base::Unretained(this), |
| 44 | &Manager::HandleStartRegisterDevice); |
| 45 | itf->AddMethodHandler(dbus_constants::kManagerFinishRegisterDevice, |
| 46 | base::Unretained(this), |
| 47 | &Manager::HandleFinishRegisterDevice); |
| 48 | itf->AddMethodHandler(dbus_constants::kManagerUpdateStateMethod, |
| 49 | base::Unretained(this), |
| 50 | &Manager::HandleUpdateState); |
| 51 | itf->AddMethodHandler(dbus_constants::kManagerTestMethod, |
| 52 | base::Unretained(this), |
| 53 | &Manager::HandleTestMethod); |
| 54 | itf->AddProperty("State", &state_); |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 55 | // TODO(wiley): Initialize all properties appropriately before claiming |
| 56 | // the properties interface. |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 57 | state_.SetValue("{}"); |
| 58 | dbus_object_.RegisterAsync(cb); |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 59 | command_manager_ = |
| 60 | std::make_shared<CommandManager>(dbus_object_.GetObjectManager()); |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 61 | command_manager_->Startup(); |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 62 | device_info_ = std::unique_ptr<DeviceRegistrationInfo>( |
| 63 | new DeviceRegistrationInfo(command_manager_)); |
| 64 | device_info_->Load(); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 67 | std::string Manager::HandleCheckDeviceRegistered(chromeos::ErrorPtr* error) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 68 | LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()"; |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 69 | std::string device_id; |
| 70 | bool registered = device_info_->CheckRegistration(error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 71 | // If it fails due to any reason other than 'device not registered', |
| 72 | // treat it as a real error and report it to the caller. |
| 73 | if (!registered && |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 74 | !(*error)->HasError(kErrorDomainGCD, "device_not_registered")) { |
| 75 | return device_id; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 76 | } |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 77 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 78 | error->reset(); |
| 79 | |
| 80 | if (registered) |
| 81 | device_id = device_info_->GetDeviceId(error); |
| 82 | |
| 83 | return device_id; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 86 | std::string Manager::HandleGetDeviceInfo(chromeos::ErrorPtr* error) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 87 | LOG(INFO) << "Received call to Manager.GetDeviceInfo()"; |
| 88 | |
| 89 | std::string device_info_str; |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 90 | auto device_info = device_info_->GetDeviceInfo(error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 91 | if (!device_info) |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 92 | return device_info_str; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 93 | |
| 94 | base::JSONWriter::Write(device_info.get(), &device_info_str); |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 95 | return device_info_str; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 98 | std::string Manager::HandleStartRegisterDevice( |
| 99 | chromeos::ErrorPtr* error, |
Alex Vakulenko | a904434 | 2014-08-23 19:31:27 -0700 | [diff] [blame] | 100 | const std::map<std::string, std::string>& params) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 101 | LOG(INFO) << "Received call to Manager.StartRegisterDevice()"; |
| 102 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 103 | return device_info_->StartRegistration(params, error); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 106 | std::string Manager::HandleFinishRegisterDevice( |
| 107 | chromeos::ErrorPtr* error, const std::string& user_auth_code) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 108 | LOG(INFO) << "Received call to Manager.FinishRegisterDevice()"; |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 109 | if (!device_info_->FinishRegistration(user_auth_code, error)) |
| 110 | return std::string(); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 111 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 112 | return device_info_->GetDeviceId(error); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 115 | void Manager::HandleUpdateState( |
| 116 | chromeos::ErrorPtr* error, const std::string& json_state_fragment) { |
Christopher Wiley | aa3f29c | 2014-03-27 14:51:26 -0700 | [diff] [blame] | 117 | // TODO(wiley): Merge json state blobs intelligently. |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 118 | state_.SetValue(json_state_fragment); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Alex Vakulenko | 7a1dc0b | 2014-08-15 11:45:46 -0700 | [diff] [blame] | 121 | std::string Manager::HandleTestMethod(chromeos::ErrorPtr* error, |
| 122 | const std::string& message) { |
| 123 | LOG(INFO) << "Received call to test method: " << message; |
| 124 | return message; |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 127 | } // namespace buffet |