blob: 00c5ad2bb2cb0520365ccdc5fea08700007c3552 [file] [log] [blame]
Christopher Wileya4915c42014-03-27 14:45:37 -07001// 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 Vakulenkob3aac252014-05-07 17:35:24 -07007#include <map>
8#include <string>
9
Christopher Wileya4915c42014-03-27 14:45:37 -070010#include <base/bind.h>
11#include <base/bind_helpers.h>
Alex Vakulenko665c8852014-09-11 16:57:24 -070012#include <base/json/json_reader.h>
Christopher Wileyb76eb292014-05-05 16:09:16 -070013#include <base/json/json_writer.h>
Christopher Wileycd419662015-02-06 17:51:43 -080014#include <base/time/time.h>
Alex Vakulenkoa8b95bc2014-08-27 11:00:57 -070015#include <chromeos/dbus/async_event_sequencer.h>
16#include <chromeos/dbus/exported_object_manager.h>
17#include <chromeos/errors/error.h>
Anton Muhin332df192014-11-22 05:59:14 +040018#include <chromeos/key_value_store.h>
Christopher Wileyb76eb292014-05-05 16:09:16 -070019#include <dbus/bus.h>
Christopher Wiley90016242014-04-01 17:33:29 -070020#include <dbus/object_path.h>
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070021#include <dbus/values_util.h>
Christopher Wileya4915c42014-03-27 14:45:37 -070022
Alex Vakulenko665c8852014-09-11 16:57:24 -070023#include "buffet/commands/command_instance.h"
Alex Vakulenkoc2bc9a42014-07-23 10:57:58 -070024#include "buffet/commands/command_manager.h"
Alex Vakulenko57123b22014-10-28 13:50:16 -070025#include "buffet/states/state_change_queue.h"
Alex Vakulenko07216fe2014-09-19 15:31:09 -070026#include "buffet/states/state_manager.h"
Christopher Wileye0fdeee2015-02-07 18:29:32 -080027#include "buffet/storage_impls.h"
Christopher Wileya4915c42014-03-27 14:45:37 -070028
Christopher Wiley2d2d92b2014-07-29 14:07:10 -070029using chromeos::dbus_utils::AsyncEventSequencer;
Christopher Wileyb5dd5ea2014-08-11 10:51:20 -070030using chromeos::dbus_utils::ExportedObjectManager;
Christopher Wileya4915c42014-03-27 14:45:37 -070031
32namespace buffet {
33
Alex Vakulenko57123b22014-10-28 13:50:16 -070034namespace {
35// Max of 100 state update events should be enough in the queue.
36const size_t kMaxStateChangeQueueSize = 100;
37} // anonymous namespace
38
Alex Vakulenkof2784de2014-08-15 11:49:35 -070039Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager)
40 : dbus_object_(object_manager.get(),
41 object_manager->GetBus(),
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -070042 org::chromium::Buffet::ManagerAdaptor::GetObjectPath()) {
43}
Christopher Wileya4915c42014-03-27 14:45:37 -070044
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -070045Manager::~Manager() {
46}
Alex Vakulenko57123b22014-10-28 13:50:16 -070047
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -070048void Manager::RegisterAsync(const base::FilePath& config_path,
49 const base::FilePath& state_path,
50 const base::FilePath& test_definitions_path,
Christopher Wileyd732bd02015-04-07 11:11:18 -070051 bool xmpp_enabled,
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -070052 const AsyncEventSequencer::CompletionAction& cb) {
Alex Vakulenko95110752014-09-03 16:27:21 -070053 command_manager_ =
54 std::make_shared<CommandManager>(dbus_object_.GetObjectManager());
Vitaly Bukaaabadee2015-03-18 23:33:44 -070055 command_manager_->SetOnCommandDefChanged(
56 base::Bind(&Manager::OnCommandDefsChanged, base::Unretained(this)));
Christopher Wileybb5b8482015-02-12 13:42:16 -080057 command_manager_->Startup(base::FilePath{"/etc/buffet"},
58 test_definitions_path);
Alex Vakulenko57123b22014-10-28 13:50:16 -070059 state_change_queue_ = std::unique_ptr<StateChangeQueue>(
60 new StateChangeQueue(kMaxStateChangeQueueSize));
61 state_manager_ = std::make_shared<StateManager>(state_change_queue_.get());
Alex Vakulenko07216fe2014-09-19 15:31:09 -070062 state_manager_->Startup();
Christopher Wiley583d64b2015-03-24 14:30:17 -070063 std::unique_ptr<BuffetConfig> config{new BuffetConfig};
64 config->Load(config_path);
Christopher Wileye0fdeee2015-02-07 18:29:32 -080065 std::unique_ptr<FileStorage> state_store{new FileStorage{state_path}};
Christopher Wileye0fdeee2015-02-07 18:29:32 -080066 // TODO(avakulenko): Figure out security implications of storing
67 // device info state data unencrypted.
Alex Vakulenko1f30a622014-07-23 11:13:15 -070068 device_info_ = std::unique_ptr<DeviceRegistrationInfo>(
Christopher Wileyc900e482015-02-15 15:42:04 -080069 new DeviceRegistrationInfo(
70 command_manager_,
71 state_manager_,
Christopher Wiley583d64b2015-03-24 14:30:17 -070072 std::move(config),
Christopher Wileyc900e482015-02-15 15:42:04 -080073 chromeos::http::Transport::CreateDefault(),
74 std::move(state_store),
Christopher Wileyd732bd02015-04-07 11:11:18 -070075 xmpp_enabled,
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -070076 base::Bind(&Manager::OnRegistrationStatusChanged,
Christopher Wileyc900e482015-02-15 15:42:04 -080077 base::Unretained(this))));
Vitaly Bukab055f152015-03-12 13:41:43 -070078 device_info_->Load();
Alex Vakulenko2348e422014-11-21 08:57:57 -080079 dbus_adaptor_.RegisterWithDBusObject(&dbus_object_);
80 dbus_object_.RegisterAsync(cb);
Christopher Wileya4915c42014-03-27 14:45:37 -070081}
82
Alex Vakulenko2348e422014-11-21 08:57:57 -080083void Manager::CheckDeviceRegistered(DBusMethodResponse<std::string> response) {
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070084 LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()";
Alex Vakulenko5c7bf012014-10-30 16:28:38 -070085 chromeos::ErrorPtr error;
86 bool registered = device_info_->CheckRegistration(&error);
Alex Vakulenkob3aac252014-05-07 17:35:24 -070087 // If it fails due to any reason other than 'device not registered',
88 // treat it as a real error and report it to the caller.
89 if (!registered &&
Alex Vakulenko5c7bf012014-10-30 16:28:38 -070090 !error->HasError(kErrorDomainGCD, "device_not_registered")) {
91 response->ReplyWithError(error.get());
92 return;
Alex Vakulenkob3aac252014-05-07 17:35:24 -070093 }
Christopher Wileya4915c42014-03-27 14:45:37 -070094
Vitaly Buka620bd7e2015-03-16 01:07:01 -070095 response->Return(registered ? device_info_->GetDeviceId() : std::string());
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070096}
97
Alex Vakulenko2348e422014-11-21 08:57:57 -080098void Manager::GetDeviceInfo(DBusMethodResponse<std::string> response) {
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070099 LOG(INFO) << "Received call to Manager.GetDeviceInfo()";
100
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700101 chromeos::ErrorPtr error;
102 auto device_info = device_info_->GetDeviceInfo(&error);
103 if (!device_info) {
104 response->ReplyWithError(error.get());
105 return;
106 }
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700107
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700108 std::string device_info_str;
Nathan Bullock4b6c0fb2015-04-01 15:32:58 -0400109 base::JSONWriter::WriteWithOptions(device_info.get(),
110 base::JSONWriter::OPTIONS_PRETTY_PRINT, &device_info_str);
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700111 response->Return(device_info_str);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700112}
113
Alex Vakulenko2348e422014-11-21 08:57:57 -0800114void Manager::RegisterDevice(DBusMethodResponse<std::string> response,
115 const chromeos::VariantDictionary& params) {
Anton Muhinbeb1c5b2014-10-16 18:59:57 +0400116 LOG(INFO) << "Received call to Manager.RegisterDevice()";
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700117
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700118 chromeos::ErrorPtr error;
Alex Vakulenko2348e422014-11-21 08:57:57 -0800119 std::map<std::string, std::string> str_params;
120 for (const auto& pair : params) {
121 if (!pair.second.IsTypeCompatible<std::string>()) {
122 response->ReplyWithError(FROM_HERE, chromeos::errors::dbus::kDomain,
123 DBUS_ERROR_INVALID_ARGS,
124 "String value expected");
125 return;
126 }
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -0700127 str_params.emplace_hint(str_params.end(), pair.first,
128 pair.second.Get<std::string>());
Alex Vakulenko2348e422014-11-21 08:57:57 -0800129 }
130 std::string device_id = device_info_->RegisterDevice(str_params, &error);
David Zeuthen1dbad472015-02-12 15:24:21 -0500131 if (!device_id.empty()) {
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700132 response->Return(device_id);
David Zeuthen1dbad472015-02-12 15:24:21 -0500133 return;
134 }
135 if (!error) {
136 // TODO(zeuthen): This can be changed to CHECK(error) once
137 // RegisterDevice() has been fixed to set |error| when failing.
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -0700138 chromeos::Error::AddTo(&error, FROM_HERE, kErrorDomainGCD, "internal_error",
David Zeuthen1dbad472015-02-12 15:24:21 -0500139 "device_id empty but error not set");
140 }
141 response->ReplyWithError(error.get());
Christopher Wileya4915c42014-03-27 14:45:37 -0700142}
143
Alex Vakulenko2348e422014-11-21 08:57:57 -0800144void Manager::UpdateState(DBusMethodResponse<> response,
145 const chromeos::VariantDictionary& property_set) {
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700146 chromeos::ErrorPtr error;
Alex Vakulenkoff73cf22014-10-29 09:53:52 -0700147 base::Time timestamp = base::Time::Now();
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700148 bool all_success = true;
Alex Vakulenkoff73cf22014-10-29 09:53:52 -0700149 for (const auto& pair : property_set) {
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -0700150 if (!state_manager_->SetPropertyValue(pair.first, pair.second, timestamp,
151 &error)) {
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700152 // Remember that an error occurred but keep going and update the rest of
153 // the properties if possible.
154 all_success = false;
155 }
Alex Vakulenkoff73cf22014-10-29 09:53:52 -0700156 }
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700157 if (!all_success)
158 response->ReplyWithError(error.get());
159 else
160 response->Return();
Christopher Wileya4915c42014-03-27 14:45:37 -0700161}
162
Alex Vakulenko61ad4db2015-01-20 10:50:04 -0800163bool Manager::GetState(chromeos::ErrorPtr* error, std::string* state) {
164 auto json = state_manager_->GetStateValuesAsJson(error);
165 if (!json)
166 return false;
Nathan Bullock4b6c0fb2015-04-01 15:32:58 -0400167 base::JSONWriter::WriteWithOptions(
168 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, state);
Alex Vakulenko61ad4db2015-01-20 10:50:04 -0800169 return true;
170}
171
Vitaly Buka64fc5fc2015-03-24 12:42:24 -0700172void Manager::AddCommand(DBusMethodResponse<std::string> response,
Alex Vakulenko2348e422014-11-21 08:57:57 -0800173 const std::string& json_command) {
Anton Muhin5191e812014-10-30 17:49:48 +0400174 static int next_id = 0;
Alex Vakulenko665c8852014-09-11 16:57:24 -0700175 std::string error_message;
176 std::unique_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
177 json_command, base::JSON_PARSE_RFC, nullptr, &error_message));
178 if (!value) {
Alex Vakulenkoac8037d2014-11-11 11:42:05 -0800179 response->ReplyWithError(FROM_HERE, chromeos::errors::json::kDomain,
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700180 chromeos::errors::json::kParseError,
181 error_message);
Alex Vakulenko665c8852014-09-11 16:57:24 -0700182 return;
183 }
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700184 chromeos::ErrorPtr error;
Alex Vakulenko665c8852014-09-11 16:57:24 -0700185 auto command_instance = buffet::CommandInstance::FromJson(
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700186 value.get(), command_manager_->GetCommandDictionary(), &error);
187 if (!command_instance) {
188 response->ReplyWithError(error.get());
189 return;
190 }
Vitaly Buka64fc5fc2015-03-24 12:42:24 -0700191 std::string id = std::to_string(++next_id);
192 command_instance->SetID(id);
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700193 command_manager_->AddCommand(std::move(command_instance));
Vitaly Buka64fc5fc2015-03-24 12:42:24 -0700194 response->Return(id);
Alex Vakulenko665c8852014-09-11 16:57:24 -0700195}
196
Vitaly Buka3886e8f2015-03-24 11:39:40 -0700197void Manager::GetCommand(DBusMethodResponse<std::string> response,
198 const std::string& id) {
199 const CommandInstance* command = command_manager_->FindCommand(id);
200 if (!command) {
201 response->ReplyWithError(FROM_HERE, kErrorDomainGCD, "unknown_command",
202 "Can't find command with id: " + id);
203 return;
204 }
205 std::string command_str;
Nathan Bullock4b6c0fb2015-04-01 15:32:58 -0400206 base::JSONWriter::WriteWithOptions(command->ToJson().get(),
207 base::JSONWriter::OPTIONS_PRETTY_PRINT, &command_str);
Vitaly Buka3886e8f2015-03-24 11:39:40 -0700208 response->Return(command_str);
209}
210
Alex Vakulenko2348e422014-11-21 08:57:57 -0800211std::string Manager::TestMethod(const std::string& message) {
Alex Vakulenko7a1dc0b2014-08-15 11:45:46 -0700212 LOG(INFO) << "Received call to test method: " << message;
213 return message;
Christopher Wileyb76eb292014-05-05 16:09:16 -0700214}
215
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -0700216void Manager::OnRegistrationStatusChanged() {
Vitaly Buka620bd7e2015-03-16 01:07:01 -0700217 dbus_adaptor_.SetStatus(
218 StatusToString(device_info_->GetRegistrationStatus()));
219 dbus_adaptor_.SetDeviceId(device_info_->GetDeviceId());
Christopher Wileyc900e482015-02-15 15:42:04 -0800220}
221
Vitaly Bukaaabadee2015-03-18 23:33:44 -0700222void Manager::OnCommandDefsChanged() {
223 chromeos::ErrorPtr error;
224 std::unique_ptr<base::DictionaryValue> commands =
225 command_manager_->GetCommandDictionary().GetCommandsAsJson(true, &error);
226 CHECK(commands);
227 std::string json;
Nathan Bullock4b6c0fb2015-04-01 15:32:58 -0400228 base::JSONWriter::WriteWithOptions(commands.get(),
229 base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
Vitaly Bukaaabadee2015-03-18 23:33:44 -0700230 dbus_adaptor_.SetCommandDefs(json);
231}
232
Christopher Wileya4915c42014-03-27 14:45:37 -0700233} // namespace buffet