Christopher Wiley | 4b5f04c | 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 | 7c3226e | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 7 | #include <map> |
| 8 | #include <string> |
| 9 | |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 10 | #include <base/bind.h> |
| 11 | #include <base/bind_helpers.h> |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 12 | #include <base/json/json_reader.h> |
Christopher Wiley | 2ffa004 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 13 | #include <base/json/json_writer.h> |
Christopher Wiley | 8994e01 | 2015-02-06 17:51:43 -0800 | [diff] [blame^] | 14 | #include <base/time/time.h> |
Alex Vakulenko | 24e5f5d | 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 | 53e882a | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 18 | #include <chromeos/key_value_store.h> |
Christopher Wiley | 2ffa004 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 19 | #include <dbus/bus.h> |
Christopher Wiley | 54028f9 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 20 | #include <dbus/object_path.h> |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 21 | #include <dbus/values_util.h> |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 22 | |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 23 | #include "buffet/commands/command_instance.h" |
Alex Vakulenko | cf9c146 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 24 | #include "buffet/commands/command_manager.h" |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 25 | #include "buffet/states/state_change_queue.h" |
Alex Vakulenko | 95877b5 | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 26 | #include "buffet/states/state_manager.h" |
Christopher Wiley | 357deca | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 27 | #include "buffet/storage_impls.h" |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 28 | |
Christopher Wiley | 68c07cc | 2014-07-29 14:07:10 -0700 | [diff] [blame] | 29 | using chromeos::dbus_utils::AsyncEventSequencer; |
Christopher Wiley | 1aa980e | 2014-08-11 10:51:20 -0700 | [diff] [blame] | 30 | using chromeos::dbus_utils::ExportedObjectManager; |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 31 | |
| 32 | namespace buffet { |
| 33 | |
Alex Vakulenko | ecf961a | 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 | b635153 | 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(), |
Alex Vakulenko | 130edb0 | 2014-11-25 14:04:27 -0800 | [diff] [blame] | 42 | org::chromium::Buffet::ManagerAdaptor::GetObjectPath()) {} |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 43 | |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 44 | Manager::~Manager() {} |
| 45 | |
Christopher Wiley | 357deca | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 46 | void Manager::RegisterAsync( |
| 47 | const base::FilePath& config_path, |
| 48 | const base::FilePath& state_path, |
Christopher Wiley | c39f4a3 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 49 | const base::FilePath& test_definitions_path, |
Christopher Wiley | 357deca | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 50 | const AsyncEventSequencer::CompletionAction& cb) { |
Alex Vakulenko | 05fa681 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 51 | command_manager_ = |
| 52 | std::make_shared<CommandManager>(dbus_object_.GetObjectManager()); |
Christopher Wiley | c39f4a3 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 53 | command_manager_->Startup(base::FilePath{"/etc/buffet"}, |
| 54 | test_definitions_path); |
Alex Vakulenko | ecf961a | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 55 | state_change_queue_ = std::unique_ptr<StateChangeQueue>( |
| 56 | new StateChangeQueue(kMaxStateChangeQueueSize)); |
| 57 | state_manager_ = std::make_shared<StateManager>(state_change_queue_.get()); |
Alex Vakulenko | 95877b5 | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 58 | state_manager_->Startup(); |
Anton Muhin | 53e882a | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 59 | std::unique_ptr<chromeos::KeyValueStore> config_store{ |
Christopher Wiley | 357deca | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 60 | new chromeos::KeyValueStore}; |
| 61 | std::unique_ptr<FileStorage> state_store{new FileStorage{state_path}}; |
| 62 | config_store->Load(config_path); |
| 63 | // TODO(avakulenko): Figure out security implications of storing |
| 64 | // device info state data unencrypted. |
Alex Vakulenko | 98025c2 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 65 | device_info_ = std::unique_ptr<DeviceRegistrationInfo>( |
Anton Muhin | 53e882a | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 66 | new DeviceRegistrationInfo(command_manager_, |
| 67 | state_manager_, |
Christopher Wiley | 357deca | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 68 | std::move(config_store), |
| 69 | chromeos::http::Transport::CreateDefault(), |
| 70 | std::move(state_store))); |
Alex Vakulenko | 98025c2 | 2014-07-23 11:13:15 -0700 | [diff] [blame] | 71 | device_info_->Load(); |
Christopher Wiley | 8994e01 | 2015-02-06 17:51:43 -0800 | [diff] [blame^] | 72 | // Wait a significant amount of time for local daemons to publish their |
| 73 | // state to Buffet before publishing it to the cloud. |
| 74 | // TODO(wiley) We could do a lot of things here to either expose this |
| 75 | // timeout as a configurable knob or allow local |
| 76 | // daemons to signal that their state is up to date so that |
| 77 | // we need not wait for them. |
| 78 | device_info_->ScheduleStartDevice(base::TimeDelta::FromSeconds(5)); |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 79 | dbus_adaptor_.RegisterWithDBusObject(&dbus_object_); |
| 80 | dbus_object_.RegisterAsync(cb); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 83 | void Manager::StartDevice(DBusMethodResponse<> response) { |
Anton Muhin | 914b138 | 2014-10-01 18:06:54 +0400 | [diff] [blame] | 84 | LOG(INFO) << "Received call to Manager.StartDevice()"; |
Anton Muhin | dd6d828 | 2014-10-02 20:37:00 +0400 | [diff] [blame] | 85 | |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 86 | chromeos::ErrorPtr error; |
| 87 | device_info_->StartDevice(&error); |
| 88 | if (error) |
| 89 | response->ReplyWithError(error.get()); |
| 90 | else |
| 91 | response->Return(); |
Anton Muhin | 914b138 | 2014-10-01 18:06:54 +0400 | [diff] [blame] | 92 | } |
| 93 | |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 94 | void Manager::CheckDeviceRegistered(DBusMethodResponse<std::string> response) { |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 95 | LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()"; |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 96 | chromeos::ErrorPtr error; |
| 97 | bool registered = device_info_->CheckRegistration(&error); |
Alex Vakulenko | 7c3226e | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 98 | // If it fails due to any reason other than 'device not registered', |
| 99 | // treat it as a real error and report it to the caller. |
| 100 | if (!registered && |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 101 | !error->HasError(kErrorDomainGCD, "device_not_registered")) { |
| 102 | response->ReplyWithError(error.get()); |
| 103 | return; |
Alex Vakulenko | 7c3226e | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 104 | } |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 105 | |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 106 | std::string device_id; |
| 107 | if (registered) { |
| 108 | error.reset(); |
| 109 | device_id = device_info_->GetDeviceId(&error); |
| 110 | if (error) { |
| 111 | response->ReplyWithError(error.get()); |
| 112 | return; |
| 113 | } |
| 114 | } |
Alex Vakulenko | b635153 | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 115 | |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 116 | response->Return(device_id); |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 119 | void Manager::GetDeviceInfo(DBusMethodResponse<std::string> response) { |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 120 | LOG(INFO) << "Received call to Manager.GetDeviceInfo()"; |
| 121 | |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 122 | chromeos::ErrorPtr error; |
| 123 | auto device_info = device_info_->GetDeviceInfo(&error); |
| 124 | if (!device_info) { |
| 125 | response->ReplyWithError(error.get()); |
| 126 | return; |
| 127 | } |
Alex Vakulenko | 7c3226e | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 128 | |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 129 | std::string device_info_str; |
Alex Vakulenko | 7c3226e | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 130 | base::JSONWriter::Write(device_info.get(), &device_info_str); |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 131 | response->Return(device_info_str); |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 134 | void Manager::RegisterDevice(DBusMethodResponse<std::string> response, |
| 135 | const chromeos::VariantDictionary& params) { |
Anton Muhin | 9cc03fd | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 136 | LOG(INFO) << "Received call to Manager.RegisterDevice()"; |
Alex Vakulenko | f3d77e5 | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 137 | |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 138 | chromeos::ErrorPtr error; |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 139 | std::map<std::string, std::string> str_params; |
| 140 | for (const auto& pair : params) { |
| 141 | if (!pair.second.IsTypeCompatible<std::string>()) { |
| 142 | response->ReplyWithError(FROM_HERE, chromeos::errors::dbus::kDomain, |
| 143 | DBUS_ERROR_INVALID_ARGS, |
| 144 | "String value expected"); |
| 145 | return; |
| 146 | } |
| 147 | str_params.emplace_hint(str_params.end(), |
| 148 | pair.first, pair.second.Get<std::string>()); |
| 149 | } |
| 150 | std::string device_id = device_info_->RegisterDevice(str_params, &error); |
David Zeuthen | 2101c08 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 151 | if (!device_id.empty()) { |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 152 | response->Return(device_id); |
David Zeuthen | 2101c08 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 153 | return; |
| 154 | } |
| 155 | if (!error) { |
| 156 | // TODO(zeuthen): This can be changed to CHECK(error) once |
| 157 | // RegisterDevice() has been fixed to set |error| when failing. |
| 158 | chromeos::Error::AddTo(&error, FROM_HERE, kErrorDomainGCD, |
| 159 | "internal_error", |
| 160 | "device_id empty but error not set"); |
| 161 | } |
| 162 | response->ReplyWithError(error.get()); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 165 | void Manager::UpdateState(DBusMethodResponse<> response, |
| 166 | const chromeos::VariantDictionary& property_set) { |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 167 | chromeos::ErrorPtr error; |
Alex Vakulenko | bfc70ad | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 168 | base::Time timestamp = base::Time::Now(); |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 169 | bool all_success = true; |
Alex Vakulenko | bfc70ad | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 170 | for (const auto& pair : property_set) { |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 171 | if (!state_manager_->SetPropertyValue(pair.first, pair.second, |
| 172 | timestamp, &error)) { |
| 173 | // Remember that an error occurred but keep going and update the rest of |
| 174 | // the properties if possible. |
| 175 | all_success = false; |
| 176 | } |
Alex Vakulenko | bfc70ad | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 177 | } |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 178 | if (!all_success) |
| 179 | response->ReplyWithError(error.get()); |
| 180 | else |
| 181 | response->Return(); |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Alex Vakulenko | ceab177 | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 184 | bool Manager::GetState(chromeos::ErrorPtr* error, std::string* state) { |
| 185 | auto json = state_manager_->GetStateValuesAsJson(error); |
| 186 | if (!json) |
| 187 | return false; |
| 188 | base::JSONWriter::Write(json.get(), state); |
| 189 | return true; |
| 190 | } |
| 191 | |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 192 | void Manager::AddCommand(DBusMethodResponse<> response, |
| 193 | const std::string& json_command) { |
Anton Muhin | d014d8b | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 194 | static int next_id = 0; |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 195 | std::string error_message; |
| 196 | std::unique_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
| 197 | json_command, base::JSON_PARSE_RFC, nullptr, &error_message)); |
| 198 | if (!value) { |
Alex Vakulenko | 90b3da9 | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 199 | response->ReplyWithError(FROM_HERE, chromeos::errors::json::kDomain, |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 200 | chromeos::errors::json::kParseError, |
| 201 | error_message); |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 202 | return; |
| 203 | } |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 204 | chromeos::ErrorPtr error; |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 205 | auto command_instance = buffet::CommandInstance::FromJson( |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 206 | value.get(), command_manager_->GetCommandDictionary(), &error); |
| 207 | if (!command_instance) { |
| 208 | response->ReplyWithError(error.get()); |
| 209 | return; |
| 210 | } |
Anton Muhin | d014d8b | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 211 | command_instance->SetID(std::to_string(++next_id)); |
Alex Vakulenko | b91dcfe | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 212 | command_manager_->AddCommand(std::move(command_instance)); |
| 213 | response->Return(); |
Alex Vakulenko | 4f7778e | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Alex Vakulenko | 12e2c1a | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 216 | std::string Manager::TestMethod(const std::string& message) { |
Alex Vakulenko | 35e3bab | 2014-08-15 11:45:46 -0700 | [diff] [blame] | 217 | LOG(INFO) << "Received call to test method: " << message; |
| 218 | return message; |
Christopher Wiley | 2ffa004 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Christopher Wiley | 4b5f04c | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 221 | } // namespace buffet |