blob: 90562aa7c0de926ad4b2568be7a0ba5280be6e46 [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>
Alex Vakulenko9ea5a322015-04-17 15:35:34 -07008#include <set>
Alex Vakulenkob3aac252014-05-07 17:35:24 -07009#include <string>
10
Christopher Wileya4915c42014-03-27 14:45:37 -070011#include <base/bind.h>
12#include <base/bind_helpers.h>
Alex Vakulenko665c8852014-09-11 16:57:24 -070013#include <base/json/json_reader.h>
Christopher Wileyb76eb292014-05-05 16:09:16 -070014#include <base/json/json_writer.h>
Christopher Wileycd419662015-02-06 17:51:43 -080015#include <base/time/time.h>
Alex Vakulenkoa8b95bc2014-08-27 11:00:57 -070016#include <chromeos/dbus/async_event_sequencer.h>
17#include <chromeos/dbus/exported_object_manager.h>
18#include <chromeos/errors/error.h>
Anton Muhin332df192014-11-22 05:59:14 +040019#include <chromeos/key_value_store.h>
Christopher Wileyb76eb292014-05-05 16:09:16 -070020#include <dbus/bus.h>
Christopher Wiley90016242014-04-01 17:33:29 -070021#include <dbus/object_path.h>
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070022#include <dbus/values_util.h>
Christopher Wileya4915c42014-03-27 14:45:37 -070023
Vitaly Buka72410b22015-05-13 13:48:59 -070024#include "buffet/base_api_handler.h"
Alex Vakulenko665c8852014-09-11 16:57:24 -070025#include "buffet/commands/command_instance.h"
Alex Vakulenkoe03af6d2015-04-20 11:00:54 -070026#include "buffet/commands/schema_constants.h"
Alex Vakulenko57123b22014-10-28 13:50:16 -070027#include "buffet/states/state_change_queue.h"
Alex Vakulenko07216fe2014-09-19 15:31:09 -070028#include "buffet/states/state_manager.h"
Christopher Wileye0fdeee2015-02-07 18:29:32 -080029#include "buffet/storage_impls.h"
Christopher Wileya4915c42014-03-27 14:45:37 -070030
Christopher Wiley2d2d92b2014-07-29 14:07:10 -070031using chromeos::dbus_utils::AsyncEventSequencer;
Christopher Wileyb5dd5ea2014-08-11 10:51:20 -070032using chromeos::dbus_utils::ExportedObjectManager;
Christopher Wileya4915c42014-03-27 14:45:37 -070033
34namespace buffet {
35
Alex Vakulenko57123b22014-10-28 13:50:16 -070036namespace {
37// Max of 100 state update events should be enough in the queue.
38const size_t kMaxStateChangeQueueSize = 100;
39} // anonymous namespace
40
Alex Vakulenkof2784de2014-08-15 11:49:35 -070041Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager)
42 : dbus_object_(object_manager.get(),
43 object_manager->GetBus(),
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -070044 org::chromium::Buffet::ManagerAdaptor::GetObjectPath()) {
45}
Christopher Wileya4915c42014-03-27 14:45:37 -070046
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -070047Manager::~Manager() {
48}
Alex Vakulenko57123b22014-10-28 13:50:16 -070049
Vitaly Buka76e70592015-04-16 11:39:02 -070050void Manager::Start(const base::FilePath& config_path,
51 const base::FilePath& state_path,
52 const base::FilePath& test_definitions_path,
53 bool xmpp_enabled,
54 const AsyncEventSequencer::CompletionAction& cb) {
Alex Vakulenko95110752014-09-03 16:27:21 -070055 command_manager_ =
56 std::make_shared<CommandManager>(dbus_object_.GetObjectManager());
Vitaly Buka5e6ff6c2015-05-11 15:41:33 -070057 command_manager_->AddOnCommandDefChanged(base::Bind(
58 &Manager::OnCommandDefsChanged, weak_ptr_factory_.GetWeakPtr()));
Christopher Wileybb5b8482015-02-12 13:42:16 -080059 command_manager_->Startup(base::FilePath{"/etc/buffet"},
60 test_definitions_path);
Vitaly Buka72410b22015-05-13 13:48:59 -070061 state_change_queue_.reset(new StateChangeQueue(kMaxStateChangeQueueSize));
Alex Vakulenko57123b22014-10-28 13:50:16 -070062 state_manager_ = std::make_shared<StateManager>(state_change_queue_.get());
Alex Vakulenko07216fe2014-09-19 15:31:09 -070063 state_manager_->Startup();
Christopher Wiley583d64b2015-03-24 14:30:17 -070064 std::unique_ptr<BuffetConfig> config{new BuffetConfig};
65 config->Load(config_path);
Christopher Wileye0fdeee2015-02-07 18:29:32 -080066 std::unique_ptr<FileStorage> state_store{new FileStorage{state_path}};
Christopher Wileye0fdeee2015-02-07 18:29:32 -080067 // TODO(avakulenko): Figure out security implications of storing
68 // device info state data unencrypted.
Vitaly Buka72410b22015-05-13 13:48:59 -070069 device_info_.reset(new DeviceRegistrationInfo(
70 command_manager_, state_manager_, std::move(config),
71 chromeos::http::Transport::CreateDefault(), std::move(state_store),
72 xmpp_enabled, &dbus_adaptor_));
73
74 base_api_handler_.reset(
75 new BaseApiHandler{device_info_->AsWeakPtr(), command_manager_});
76
Vitaly Bukab055f152015-03-12 13:41:43 -070077 device_info_->Load();
Alex Vakulenko2348e422014-11-21 08:57:57 -080078 dbus_adaptor_.RegisterWithDBusObject(&dbus_object_);
79 dbus_object_.RegisterAsync(cb);
Christopher Wileya4915c42014-03-27 14:45:37 -070080}
81
Alex Vakulenko2348e422014-11-21 08:57:57 -080082void Manager::CheckDeviceRegistered(DBusMethodResponse<std::string> response) {
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070083 LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()";
Alex Vakulenko5c7bf012014-10-30 16:28:38 -070084 chromeos::ErrorPtr error;
Nathan Bullock5e022a32015-04-08 15:13:07 -040085 bool registered = device_info_->HaveRegistrationCredentials(&error);
Alex Vakulenkob3aac252014-05-07 17:35:24 -070086 // 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 Vakulenko5c7bf012014-10-30 16:28:38 -070089 !error->HasError(kErrorDomainGCD, "device_not_registered")) {
90 response->ReplyWithError(error.get());
91 return;
Alex Vakulenkob3aac252014-05-07 17:35:24 -070092 }
Christopher Wileya4915c42014-03-27 14:45:37 -070093
Vitaly Buka620bd7e2015-03-16 01:07:01 -070094 response->Return(registered ? device_info_->GetDeviceId() : std::string());
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070095}
96
Alex Vakulenko2348e422014-11-21 08:57:57 -080097void Manager::GetDeviceInfo(DBusMethodResponse<std::string> response) {
Alex Vakulenko3cb466c2014-04-15 11:36:32 -070098 LOG(INFO) << "Received call to Manager.GetDeviceInfo()";
99
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700100 chromeos::ErrorPtr error;
101 auto device_info = device_info_->GetDeviceInfo(&error);
102 if (!device_info) {
103 response->ReplyWithError(error.get());
104 return;
105 }
Alex Vakulenkob3aac252014-05-07 17:35:24 -0700106
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700107 std::string device_info_str;
Nathan Bullock4b6c0fb2015-04-01 15:32:58 -0400108 base::JSONWriter::WriteWithOptions(device_info.get(),
109 base::JSONWriter::OPTIONS_PRETTY_PRINT, &device_info_str);
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700110 response->Return(device_info_str);
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700111}
112
Alex Vakulenko2348e422014-11-21 08:57:57 -0800113void Manager::RegisterDevice(DBusMethodResponse<std::string> response,
114 const chromeos::VariantDictionary& params) {
Anton Muhinbeb1c5b2014-10-16 18:59:57 +0400115 LOG(INFO) << "Received call to Manager.RegisterDevice()";
Alex Vakulenko3cb466c2014-04-15 11:36:32 -0700116
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700117 chromeos::ErrorPtr error;
Alex Vakulenko2348e422014-11-21 08:57:57 -0800118 std::map<std::string, std::string> str_params;
119 for (const auto& pair : params) {
120 if (!pair.second.IsTypeCompatible<std::string>()) {
121 response->ReplyWithError(FROM_HERE, chromeos::errors::dbus::kDomain,
122 DBUS_ERROR_INVALID_ARGS,
123 "String value expected");
124 return;
125 }
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -0700126 str_params.emplace_hint(str_params.end(), pair.first,
127 pair.second.Get<std::string>());
Alex Vakulenko2348e422014-11-21 08:57:57 -0800128 }
129 std::string device_id = device_info_->RegisterDevice(str_params, &error);
David Zeuthen1dbad472015-02-12 15:24:21 -0500130 if (!device_id.empty()) {
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700131 response->Return(device_id);
David Zeuthen1dbad472015-02-12 15:24:21 -0500132 return;
133 }
134 if (!error) {
135 // TODO(zeuthen): This can be changed to CHECK(error) once
136 // RegisterDevice() has been fixed to set |error| when failing.
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -0700137 chromeos::Error::AddTo(&error, FROM_HERE, kErrorDomainGCD, "internal_error",
David Zeuthen1dbad472015-02-12 15:24:21 -0500138 "device_id empty but error not set");
139 }
140 response->ReplyWithError(error.get());
Christopher Wileya4915c42014-03-27 14:45:37 -0700141}
142
Alex Vakulenko2348e422014-11-21 08:57:57 -0800143void Manager::UpdateState(DBusMethodResponse<> response,
144 const chromeos::VariantDictionary& property_set) {
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700145 chromeos::ErrorPtr error;
Alex Vakulenkoff73cf22014-10-29 09:53:52 -0700146 base::Time timestamp = base::Time::Now();
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700147 bool all_success = true;
Alex Vakulenkoff73cf22014-10-29 09:53:52 -0700148 for (const auto& pair : property_set) {
Vitaly Buka7ad8ffb2015-03-20 09:46:57 -0700149 if (!state_manager_->SetPropertyValue(pair.first, pair.second, timestamp,
150 &error)) {
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700151 // Remember that an error occurred but keep going and update the rest of
152 // the properties if possible.
153 all_success = false;
154 }
Alex Vakulenkoff73cf22014-10-29 09:53:52 -0700155 }
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700156 if (!all_success)
157 response->ReplyWithError(error.get());
158 else
159 response->Return();
Christopher Wileya4915c42014-03-27 14:45:37 -0700160}
161
Alex Vakulenko61ad4db2015-01-20 10:50:04 -0800162bool Manager::GetState(chromeos::ErrorPtr* error, std::string* state) {
163 auto json = state_manager_->GetStateValuesAsJson(error);
164 if (!json)
165 return false;
Nathan Bullock4b6c0fb2015-04-01 15:32:58 -0400166 base::JSONWriter::WriteWithOptions(
167 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, state);
Alex Vakulenko61ad4db2015-01-20 10:50:04 -0800168 return true;
169}
170
Vitaly Buka64fc5fc2015-03-24 12:42:24 -0700171void Manager::AddCommand(DBusMethodResponse<std::string> response,
Alex Vakulenko2348e422014-11-21 08:57:57 -0800172 const std::string& json_command) {
Anton Muhin5191e812014-10-30 17:49:48 +0400173 static int next_id = 0;
Alex Vakulenko665c8852014-09-11 16:57:24 -0700174 std::string error_message;
175 std::unique_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
176 json_command, base::JSON_PARSE_RFC, nullptr, &error_message));
177 if (!value) {
Alex Vakulenkoac8037d2014-11-11 11:42:05 -0800178 response->ReplyWithError(FROM_HERE, chromeos::errors::json::kDomain,
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700179 chromeos::errors::json::kParseError,
180 error_message);
Alex Vakulenko665c8852014-09-11 16:57:24 -0700181 return;
182 }
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700183 chromeos::ErrorPtr error;
Alex Vakulenko665c8852014-09-11 16:57:24 -0700184 auto command_instance = buffet::CommandInstance::FromJson(
Alex Vakulenkof784e212015-04-20 12:33:52 -0700185 value.get(), commands::attributes::kCommand_Visibility_Local,
Alex Vakulenkod1978d32015-04-29 17:33:26 -0700186 command_manager_->GetCommandDictionary(), nullptr, &error);
Alex Vakulenko5c7bf012014-10-30 16:28:38 -0700187 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 Vakulenkoe03af6d2015-04-20 11:00:54 -0700211void Manager::SetCommandVisibility(
Alex Vakulenko41f73a92015-04-24 18:09:32 -0700212 std::unique_ptr<chromeos::dbus_utils::DBusMethodResponse<>> response,
Alex Vakulenkoe03af6d2015-04-20 11:00:54 -0700213 const std::vector<std::string>& in_names,
214 const std::string& in_visibility) {
215 CommandDefinition::Visibility visibility;
216 chromeos::ErrorPtr error;
217 if (!visibility.FromString(in_visibility, &error)) {
218 response->ReplyWithError(error.get());
219 return;
220 }
221 if (!command_manager_->SetCommandVisibility(in_names, visibility, &error)) {
222 response->ReplyWithError(error.get());
223 return;
224 }
225 response->Return();
226}
227
Alex Vakulenko2348e422014-11-21 08:57:57 -0800228std::string Manager::TestMethod(const std::string& message) {
Alex Vakulenko7a1dc0b2014-08-15 11:45:46 -0700229 LOG(INFO) << "Received call to test method: " << message;
230 return message;
Christopher Wileyb76eb292014-05-05 16:09:16 -0700231}
232
Vitaly Bukafa947062015-04-17 00:41:31 -0700233bool Manager::UpdateDeviceInfo(chromeos::ErrorPtr* error,
234 const std::string& in_name,
235 const std::string& in_description,
236 const std::string& in_location) {
237 return device_info_->UpdateDeviceInfo(in_name, in_description, in_location,
238 error);
Christopher Wileyc900e482015-02-15 15:42:04 -0800239}
240
Vitaly Bukaaabadee2015-03-18 23:33:44 -0700241void Manager::OnCommandDefsChanged() {
242 chromeos::ErrorPtr error;
Alex Vakulenko9ea5a322015-04-17 15:35:34 -0700243 // Limit only to commands that are visible to the local clients.
244 auto commands = command_manager_->GetCommandDictionary().GetCommandsAsJson(
245 [](const buffet::CommandDefinition* def) {
246 return def->GetVisibility().local;
247 }, true, &error);
Vitaly Bukaaabadee2015-03-18 23:33:44 -0700248 CHECK(commands);
249 std::string json;
Nathan Bullock4b6c0fb2015-04-01 15:32:58 -0400250 base::JSONWriter::WriteWithOptions(commands.get(),
251 base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
Vitaly Bukaaabadee2015-03-18 23:33:44 -0700252 dbus_adaptor_.SetCommandDefs(json);
253}
254
Christopher Wileya4915c42014-03-27 14:45:37 -0700255} // namespace buffet