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> |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 12 | #include <base/json/json_reader.h> |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 13 | #include <base/json/json_writer.h> |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 14 | #include <base/time/time.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 15 | #include <chromeos/dbus/async_event_sequencer.h> |
| 16 | #include <chromeos/dbus/exported_object_manager.h> |
| 17 | #include <chromeos/errors/error.h> |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 18 | #include <chromeos/key_value_store.h> |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 19 | #include <dbus/bus.h> |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 20 | #include <dbus/object_path.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 21 | #include <dbus/values_util.h> |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 22 | |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 23 | #include "buffet/commands/command_instance.h" |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 24 | #include "buffet/commands/command_manager.h" |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 25 | #include "buffet/states/state_change_queue.h" |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 26 | #include "buffet/states/state_manager.h" |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 27 | #include "buffet/storage_impls.h" |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 28 | |
Christopher Wiley | 2d2d92b | 2014-07-29 14:07:10 -0700 | [diff] [blame] | 29 | using chromeos::dbus_utils::AsyncEventSequencer; |
Christopher Wiley | b5dd5ea | 2014-08-11 10:51:20 -0700 | [diff] [blame] | 30 | using chromeos::dbus_utils::ExportedObjectManager; |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 31 | |
| 32 | namespace buffet { |
| 33 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 34 | namespace { |
| 35 | // Max of 100 state update events should be enough in the queue. |
| 36 | const size_t kMaxStateChangeQueueSize = 100; |
| 37 | } // anonymous namespace |
| 38 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 39 | Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager) |
| 40 | : dbus_object_(object_manager.get(), |
| 41 | object_manager->GetBus(), |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 42 | org::chromium::Buffet::ManagerAdaptor::GetObjectPath()) { |
| 43 | } |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 44 | |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 45 | Manager::~Manager() { |
| 46 | } |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 47 | |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 48 | void Manager::RegisterAsync(const base::FilePath& config_path, |
| 49 | const base::FilePath& state_path, |
| 50 | const base::FilePath& test_definitions_path, |
| 51 | const AsyncEventSequencer::CompletionAction& cb) { |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 52 | command_manager_ = |
| 53 | std::make_shared<CommandManager>(dbus_object_.GetObjectManager()); |
Vitaly Buka | aabadee | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 54 | command_manager_->SetOnCommandDefChanged( |
| 55 | base::Bind(&Manager::OnCommandDefsChanged, base::Unretained(this))); |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 56 | command_manager_->Startup(base::FilePath{"/etc/buffet"}, |
| 57 | test_definitions_path); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 58 | state_change_queue_ = std::unique_ptr<StateChangeQueue>( |
| 59 | new StateChangeQueue(kMaxStateChangeQueueSize)); |
| 60 | state_manager_ = std::make_shared<StateManager>(state_change_queue_.get()); |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 61 | state_manager_->Startup(); |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 62 | std::unique_ptr<chromeos::KeyValueStore> config_store{ |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 63 | new chromeos::KeyValueStore}; |
| 64 | std::unique_ptr<FileStorage> state_store{new FileStorage{state_path}}; |
| 65 | config_store->Load(config_path); |
| 66 | // TODO(avakulenko): Figure out security implications of storing |
| 67 | // device info state data unencrypted. |
Alex Vakulenko | 1f30a62 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 68 | device_info_ = std::unique_ptr<DeviceRegistrationInfo>( |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 69 | new DeviceRegistrationInfo( |
| 70 | command_manager_, |
| 71 | state_manager_, |
| 72 | std::move(config_store), |
| 73 | chromeos::http::Transport::CreateDefault(), |
| 74 | std::move(state_store), |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 75 | base::Bind(&Manager::OnRegistrationStatusChanged, |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 76 | base::Unretained(this)))); |
Vitaly Buka | b055f15 | 2015-03-12 13:41:43 -0700 | [diff] [blame] | 77 | device_info_->Load(); |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 78 | dbus_adaptor_.RegisterWithDBusObject(&dbus_object_); |
| 79 | dbus_object_.RegisterAsync(cb); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 82 | void Manager::CheckDeviceRegistered(DBusMethodResponse<std::string> response) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 83 | LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()"; |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 84 | chromeos::ErrorPtr error; |
| 85 | bool registered = device_info_->CheckRegistration(&error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 86 | // If it fails due to any reason other than 'device not registered', |
| 87 | // treat it as a real error and report it to the caller. |
| 88 | if (!registered && |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 89 | !error->HasError(kErrorDomainGCD, "device_not_registered")) { |
| 90 | response->ReplyWithError(error.get()); |
| 91 | return; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 92 | } |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 93 | |
Vitaly Buka | 620bd7e | 2015-03-16 01:07:01 -0700 | [diff] [blame] | 94 | response->Return(registered ? device_info_->GetDeviceId() : std::string()); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 97 | void Manager::GetDeviceInfo(DBusMethodResponse<std::string> response) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 98 | LOG(INFO) << "Received call to Manager.GetDeviceInfo()"; |
| 99 | |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 100 | chromeos::ErrorPtr error; |
| 101 | auto device_info = device_info_->GetDeviceInfo(&error); |
| 102 | if (!device_info) { |
| 103 | response->ReplyWithError(error.get()); |
| 104 | return; |
| 105 | } |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 106 | |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 107 | std::string device_info_str; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 108 | base::JSONWriter::Write(device_info.get(), &device_info_str); |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 109 | response->Return(device_info_str); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 112 | void Manager::RegisterDevice(DBusMethodResponse<std::string> response, |
| 113 | const chromeos::VariantDictionary& params) { |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 114 | LOG(INFO) << "Received call to Manager.RegisterDevice()"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 115 | |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 116 | chromeos::ErrorPtr error; |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 117 | std::map<std::string, std::string> str_params; |
| 118 | for (const auto& pair : params) { |
| 119 | if (!pair.second.IsTypeCompatible<std::string>()) { |
| 120 | response->ReplyWithError(FROM_HERE, chromeos::errors::dbus::kDomain, |
| 121 | DBUS_ERROR_INVALID_ARGS, |
| 122 | "String value expected"); |
| 123 | return; |
| 124 | } |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 125 | str_params.emplace_hint(str_params.end(), pair.first, |
| 126 | pair.second.Get<std::string>()); |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 127 | } |
| 128 | std::string device_id = device_info_->RegisterDevice(str_params, &error); |
David Zeuthen | 1dbad47 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 129 | if (!device_id.empty()) { |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 130 | response->Return(device_id); |
David Zeuthen | 1dbad47 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | if (!error) { |
| 134 | // TODO(zeuthen): This can be changed to CHECK(error) once |
| 135 | // RegisterDevice() has been fixed to set |error| when failing. |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 136 | chromeos::Error::AddTo(&error, FROM_HERE, kErrorDomainGCD, "internal_error", |
David Zeuthen | 1dbad47 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 137 | "device_id empty but error not set"); |
| 138 | } |
| 139 | response->ReplyWithError(error.get()); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 142 | void Manager::UpdateState(DBusMethodResponse<> response, |
| 143 | const chromeos::VariantDictionary& property_set) { |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 144 | chromeos::ErrorPtr error; |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 145 | base::Time timestamp = base::Time::Now(); |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 146 | bool all_success = true; |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 147 | for (const auto& pair : property_set) { |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 148 | if (!state_manager_->SetPropertyValue(pair.first, pair.second, timestamp, |
| 149 | &error)) { |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 150 | // Remember that an error occurred but keep going and update the rest of |
| 151 | // the properties if possible. |
| 152 | all_success = false; |
| 153 | } |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 154 | } |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 155 | if (!all_success) |
| 156 | response->ReplyWithError(error.get()); |
| 157 | else |
| 158 | response->Return(); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Alex Vakulenko | 61ad4db | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 161 | bool Manager::GetState(chromeos::ErrorPtr* error, std::string* state) { |
| 162 | auto json = state_manager_->GetStateValuesAsJson(error); |
| 163 | if (!json) |
| 164 | return false; |
| 165 | base::JSONWriter::Write(json.get(), state); |
| 166 | return true; |
| 167 | } |
| 168 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 169 | void Manager::AddCommand(DBusMethodResponse<> response, |
| 170 | const std::string& json_command) { |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 171 | static int next_id = 0; |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 172 | std::string error_message; |
| 173 | std::unique_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
| 174 | json_command, base::JSON_PARSE_RFC, nullptr, &error_message)); |
| 175 | if (!value) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 176 | response->ReplyWithError(FROM_HERE, chromeos::errors::json::kDomain, |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 177 | chromeos::errors::json::kParseError, |
| 178 | error_message); |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 179 | return; |
| 180 | } |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 181 | chromeos::ErrorPtr error; |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 182 | auto command_instance = buffet::CommandInstance::FromJson( |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 183 | value.get(), command_manager_->GetCommandDictionary(), &error); |
| 184 | if (!command_instance) { |
| 185 | response->ReplyWithError(error.get()); |
| 186 | return; |
| 187 | } |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 188 | command_instance->SetID(std::to_string(++next_id)); |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 189 | command_manager_->AddCommand(std::move(command_instance)); |
| 190 | response->Return(); |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 193 | std::string Manager::TestMethod(const std::string& message) { |
Alex Vakulenko | 7a1dc0b | 2014-08-15 11:45:46 -0700 | [diff] [blame] | 194 | LOG(INFO) << "Received call to test method: " << message; |
| 195 | return message; |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 198 | void Manager::OnRegistrationStatusChanged() { |
Vitaly Buka | 620bd7e | 2015-03-16 01:07:01 -0700 | [diff] [blame] | 199 | dbus_adaptor_.SetStatus( |
| 200 | StatusToString(device_info_->GetRegistrationStatus())); |
| 201 | dbus_adaptor_.SetDeviceId(device_info_->GetDeviceId()); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Vitaly Buka | aabadee | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 204 | void Manager::OnCommandDefsChanged() { |
| 205 | chromeos::ErrorPtr error; |
| 206 | std::unique_ptr<base::DictionaryValue> commands = |
| 207 | command_manager_->GetCommandDictionary().GetCommandsAsJson(true, &error); |
| 208 | CHECK(commands); |
| 209 | std::string json; |
| 210 | base::JSONWriter::Write(commands.get(), &json); |
| 211 | dbus_adaptor_.SetCommandDefs(json); |
| 212 | } |
| 213 | |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 214 | } // namespace buffet |