blob: 2002eb25771ab089d3f9858d18c16f2e0030d3b5 [file] [log] [blame]
Christopher Wiley4b5f04c2014-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 Vakulenko7c3226e2014-05-07 17:35:24 -07007#include <map>
Alex Vakulenkoec41a0c2015-04-17 15:35:34 -07008#include <set>
Alex Vakulenko7c3226e2014-05-07 17:35:24 -07009#include <string>
10
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070011#include <base/bind.h>
12#include <base/bind_helpers.h>
Alex Vakulenko4f7778e2014-09-11 16:57:24 -070013#include <base/json/json_reader.h>
Christopher Wiley2ffa0042014-05-05 16:09:16 -070014#include <base/json/json_writer.h>
Christopher Wiley8994e012015-02-06 17:51:43 -080015#include <base/time/time.h>
Alex Vakulenko24e5f5d2014-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 Muhin53e882a2014-11-22 05:59:14 +040019#include <chromeos/key_value_store.h>
Christopher Wiley2ffa0042014-05-05 16:09:16 -070020#include <dbus/bus.h>
Christopher Wiley54028f92014-04-01 17:33:29 -070021#include <dbus/object_path.h>
Alex Vakulenkof3d77e52014-04-15 11:36:32 -070022#include <dbus/values_util.h>
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070023
Vitaly Bukae74fe3c2015-05-13 13:48:59 -070024#include "buffet/base_api_handler.h"
Alex Vakulenko4f7778e2014-09-11 16:57:24 -070025#include "buffet/commands/command_instance.h"
Alex Vakulenkoacec6aa2015-04-20 11:00:54 -070026#include "buffet/commands/schema_constants.h"
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070027#include "buffet/states/state_change_queue.h"
Alex Vakulenko95877b52014-09-19 15:31:09 -070028#include "buffet/states/state_manager.h"
Christopher Wiley357deca2015-02-07 18:29:32 -080029#include "buffet/storage_impls.h"
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070030
Christopher Wiley68c07cc2014-07-29 14:07:10 -070031using chromeos::dbus_utils::AsyncEventSequencer;
Christopher Wiley1aa980e2014-08-11 10:51:20 -070032using chromeos::dbus_utils::ExportedObjectManager;
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070033
34namespace buffet {
35
Alex Vakulenkoecf961a2014-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 Vakulenkob6351532014-08-15 11:49:35 -070041Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager)
42 : dbus_object_(object_manager.get(),
43 object_manager->GetBus(),
Vitaly Buka91cc7152015-03-20 09:46:57 -070044 org::chromium::Buffet::ManagerAdaptor::GetObjectPath()) {
45}
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070046
Vitaly Buka91cc7152015-03-20 09:46:57 -070047Manager::~Manager() {
48}
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070049
Vitaly Bukae46525b2015-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 Vakulenko05fa6812014-09-03 16:27:21 -070055 command_manager_ =
56 std::make_shared<CommandManager>(dbus_object_.GetObjectManager());
Vitaly Bukae43871f2015-05-11 15:41:33 -070057 command_manager_->AddOnCommandDefChanged(base::Bind(
58 &Manager::OnCommandDefsChanged, weak_ptr_factory_.GetWeakPtr()));
Christopher Wileyc39f4a32015-02-12 13:42:16 -080059 command_manager_->Startup(base::FilePath{"/etc/buffet"},
60 test_definitions_path);
Vitaly Bukae74fe3c2015-05-13 13:48:59 -070061 state_change_queue_.reset(new StateChangeQueue(kMaxStateChangeQueueSize));
Alex Vakulenkoecf961a2014-10-28 13:50:16 -070062 state_manager_ = std::make_shared<StateManager>(state_change_queue_.get());
Alex Vakulenko95877b52014-09-19 15:31:09 -070063 state_manager_->Startup();
Vitaly Bukabf4ba652015-05-14 16:57:23 -070064
65 std::unique_ptr<BuffetConfig> config{new BuffetConfig{state_path}};
66 config->AddOnChangedCallback(
67 base::Bind(&Manager::OnConfigChanged, weak_ptr_factory_.GetWeakPtr()));
Christopher Wileyb024ca92015-03-24 14:30:17 -070068 config->Load(config_path);
Vitaly Bukabf4ba652015-05-14 16:57:23 -070069
Christopher Wiley357deca2015-02-07 18:29:32 -080070 // TODO(avakulenko): Figure out security implications of storing
71 // device info state data unencrypted.
Vitaly Bukae74fe3c2015-05-13 13:48:59 -070072 device_info_.reset(new DeviceRegistrationInfo(
73 command_manager_, state_manager_, std::move(config),
Vitaly Bukabf4ba652015-05-14 16:57:23 -070074 chromeos::http::Transport::CreateDefault(), xmpp_enabled));
75 device_info_->AddOnRegistrationChangedCallback(base::Bind(
76 &Manager::OnRegistrationChanged, weak_ptr_factory_.GetWeakPtr()));
Vitaly Bukae74fe3c2015-05-13 13:48:59 -070077
78 base_api_handler_.reset(
79 new BaseApiHandler{device_info_->AsWeakPtr(), command_manager_});
80
Vitaly Bukabf4ba652015-05-14 16:57:23 -070081 device_info_->Start();
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -080082 dbus_adaptor_.RegisterWithDBusObject(&dbus_object_);
83 dbus_object_.RegisterAsync(cb);
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070084}
85
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -080086void Manager::CheckDeviceRegistered(DBusMethodResponse<std::string> response) {
Alex Vakulenkof3d77e52014-04-15 11:36:32 -070087 LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()";
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -070088 chromeos::ErrorPtr error;
Nathan Bullockeec58462015-04-08 15:13:07 -040089 bool registered = device_info_->HaveRegistrationCredentials(&error);
Alex Vakulenko7c3226e2014-05-07 17:35:24 -070090 // If it fails due to any reason other than 'device not registered',
91 // treat it as a real error and report it to the caller.
92 if (!registered &&
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -070093 !error->HasError(kErrorDomainGCD, "device_not_registered")) {
94 response->ReplyWithError(error.get());
95 return;
Alex Vakulenko7c3226e2014-05-07 17:35:24 -070096 }
Christopher Wiley4b5f04c2014-03-27 14:45:37 -070097
Vitaly Bukabf4ba652015-05-14 16:57:23 -070098 response->Return(registered ? device_info_->GetConfig().device_id()
99 : std::string());
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700100}
101
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800102void Manager::GetDeviceInfo(DBusMethodResponse<std::string> response) {
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700103 LOG(INFO) << "Received call to Manager.GetDeviceInfo()";
104
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700105 chromeos::ErrorPtr error;
106 auto device_info = device_info_->GetDeviceInfo(&error);
107 if (!device_info) {
108 response->ReplyWithError(error.get());
109 return;
110 }
Alex Vakulenko7c3226e2014-05-07 17:35:24 -0700111
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700112 std::string device_info_str;
Nathan Bullockf5b91bf2015-04-01 15:32:58 -0400113 base::JSONWriter::WriteWithOptions(device_info.get(),
114 base::JSONWriter::OPTIONS_PRETTY_PRINT, &device_info_str);
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700115 response->Return(device_info_str);
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700116}
117
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800118void Manager::RegisterDevice(DBusMethodResponse<std::string> response,
119 const chromeos::VariantDictionary& params) {
Anton Muhin9cc03fd2014-10-16 18:59:57 +0400120 LOG(INFO) << "Received call to Manager.RegisterDevice()";
Alex Vakulenkof3d77e52014-04-15 11:36:32 -0700121
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700122 chromeos::ErrorPtr error;
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800123 std::map<std::string, std::string> str_params;
124 for (const auto& pair : params) {
125 if (!pair.second.IsTypeCompatible<std::string>()) {
126 response->ReplyWithError(FROM_HERE, chromeos::errors::dbus::kDomain,
127 DBUS_ERROR_INVALID_ARGS,
128 "String value expected");
129 return;
130 }
Vitaly Buka91cc7152015-03-20 09:46:57 -0700131 str_params.emplace_hint(str_params.end(), pair.first,
132 pair.second.Get<std::string>());
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800133 }
134 std::string device_id = device_info_->RegisterDevice(str_params, &error);
David Zeuthen2101c082015-02-12 15:24:21 -0500135 if (!device_id.empty()) {
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700136 response->Return(device_id);
David Zeuthen2101c082015-02-12 15:24:21 -0500137 return;
138 }
139 if (!error) {
140 // TODO(zeuthen): This can be changed to CHECK(error) once
141 // RegisterDevice() has been fixed to set |error| when failing.
Vitaly Buka91cc7152015-03-20 09:46:57 -0700142 chromeos::Error::AddTo(&error, FROM_HERE, kErrorDomainGCD, "internal_error",
David Zeuthen2101c082015-02-12 15:24:21 -0500143 "device_id empty but error not set");
144 }
145 response->ReplyWithError(error.get());
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700146}
147
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800148void Manager::UpdateState(DBusMethodResponse<> response,
149 const chromeos::VariantDictionary& property_set) {
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700150 chromeos::ErrorPtr error;
Alex Vakulenkobfc70ad2014-10-29 09:53:52 -0700151 base::Time timestamp = base::Time::Now();
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700152 bool all_success = true;
Alex Vakulenkobfc70ad2014-10-29 09:53:52 -0700153 for (const auto& pair : property_set) {
Vitaly Buka91cc7152015-03-20 09:46:57 -0700154 if (!state_manager_->SetPropertyValue(pair.first, pair.second, timestamp,
155 &error)) {
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700156 // Remember that an error occurred but keep going and update the rest of
157 // the properties if possible.
158 all_success = false;
159 }
Alex Vakulenkobfc70ad2014-10-29 09:53:52 -0700160 }
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700161 if (!all_success)
162 response->ReplyWithError(error.get());
163 else
164 response->Return();
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700165}
166
Alex Vakulenkoceab1772015-01-20 10:50:04 -0800167bool Manager::GetState(chromeos::ErrorPtr* error, std::string* state) {
168 auto json = state_manager_->GetStateValuesAsJson(error);
169 if (!json)
170 return false;
Nathan Bullockf5b91bf2015-04-01 15:32:58 -0400171 base::JSONWriter::WriteWithOptions(
172 json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, state);
Alex Vakulenkoceab1772015-01-20 10:50:04 -0800173 return true;
174}
175
Vitaly Buka59af7ac2015-03-24 12:42:24 -0700176void Manager::AddCommand(DBusMethodResponse<std::string> response,
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800177 const std::string& json_command) {
Anton Muhind014d8b2014-10-30 17:49:48 +0400178 static int next_id = 0;
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700179 std::string error_message;
180 std::unique_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
181 json_command, base::JSON_PARSE_RFC, nullptr, &error_message));
182 if (!value) {
Alex Vakulenko90b3da92014-11-11 11:42:05 -0800183 response->ReplyWithError(FROM_HERE, chromeos::errors::json::kDomain,
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700184 chromeos::errors::json::kParseError,
185 error_message);
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700186 return;
187 }
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700188 chromeos::ErrorPtr error;
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700189 auto command_instance = buffet::CommandInstance::FromJson(
Alex Vakulenkod4190442015-04-20 12:33:52 -0700190 value.get(), commands::attributes::kCommand_Visibility_Local,
Alex Vakulenkoa031e1b2015-04-29 17:33:26 -0700191 command_manager_->GetCommandDictionary(), nullptr, &error);
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700192 if (!command_instance) {
193 response->ReplyWithError(error.get());
194 return;
195 }
Vitaly Buka59af7ac2015-03-24 12:42:24 -0700196 std::string id = std::to_string(++next_id);
197 command_instance->SetID(id);
Alex Vakulenkob91dcfe2014-10-30 16:28:38 -0700198 command_manager_->AddCommand(std::move(command_instance));
Vitaly Buka59af7ac2015-03-24 12:42:24 -0700199 response->Return(id);
Alex Vakulenko4f7778e2014-09-11 16:57:24 -0700200}
201
Vitaly Buka5515de02015-03-24 11:39:40 -0700202void Manager::GetCommand(DBusMethodResponse<std::string> response,
203 const std::string& id) {
204 const CommandInstance* command = command_manager_->FindCommand(id);
205 if (!command) {
206 response->ReplyWithError(FROM_HERE, kErrorDomainGCD, "unknown_command",
207 "Can't find command with id: " + id);
208 return;
209 }
210 std::string command_str;
Nathan Bullockf5b91bf2015-04-01 15:32:58 -0400211 base::JSONWriter::WriteWithOptions(command->ToJson().get(),
212 base::JSONWriter::OPTIONS_PRETTY_PRINT, &command_str);
Vitaly Buka5515de02015-03-24 11:39:40 -0700213 response->Return(command_str);
214}
215
Alex Vakulenkoacec6aa2015-04-20 11:00:54 -0700216void Manager::SetCommandVisibility(
Alex Vakulenko8d78ebf2015-04-24 18:09:32 -0700217 std::unique_ptr<chromeos::dbus_utils::DBusMethodResponse<>> response,
Alex Vakulenkoacec6aa2015-04-20 11:00:54 -0700218 const std::vector<std::string>& in_names,
219 const std::string& in_visibility) {
220 CommandDefinition::Visibility visibility;
221 chromeos::ErrorPtr error;
222 if (!visibility.FromString(in_visibility, &error)) {
223 response->ReplyWithError(error.get());
224 return;
225 }
226 if (!command_manager_->SetCommandVisibility(in_names, visibility, &error)) {
227 response->ReplyWithError(error.get());
228 return;
229 }
230 response->Return();
231}
232
Alex Vakulenko12e2c1a2014-11-21 08:57:57 -0800233std::string Manager::TestMethod(const std::string& message) {
Alex Vakulenko35e3bab2014-08-15 11:45:46 -0700234 LOG(INFO) << "Received call to test method: " << message;
235 return message;
Christopher Wiley2ffa0042014-05-05 16:09:16 -0700236}
237
Vitaly Buka760d6322015-04-17 00:41:31 -0700238bool Manager::UpdateDeviceInfo(chromeos::ErrorPtr* error,
239 const std::string& in_name,
240 const std::string& in_description,
241 const std::string& in_location) {
242 return device_info_->UpdateDeviceInfo(in_name, in_description, in_location,
243 error);
Christopher Wiley2f772932015-02-15 15:42:04 -0800244}
245
Vitaly Buka1fa69012015-03-18 23:33:44 -0700246void Manager::OnCommandDefsChanged() {
247 chromeos::ErrorPtr error;
Alex Vakulenkoec41a0c2015-04-17 15:35:34 -0700248 // Limit only to commands that are visible to the local clients.
249 auto commands = command_manager_->GetCommandDictionary().GetCommandsAsJson(
250 [](const buffet::CommandDefinition* def) {
251 return def->GetVisibility().local;
252 }, true, &error);
Vitaly Buka1fa69012015-03-18 23:33:44 -0700253 CHECK(commands);
254 std::string json;
Nathan Bullockf5b91bf2015-04-01 15:32:58 -0400255 base::JSONWriter::WriteWithOptions(commands.get(),
256 base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
Vitaly Buka1fa69012015-03-18 23:33:44 -0700257 dbus_adaptor_.SetCommandDefs(json);
258}
259
Vitaly Bukabf4ba652015-05-14 16:57:23 -0700260void Manager::OnRegistrationChanged(RegistrationStatus status) {
261 dbus_adaptor_.SetStatus(StatusToString(status));
262}
263
264void Manager::OnConfigChanged(const BuffetConfig& config) {
265 dbus_adaptor_.SetDeviceId(config.device_id());
266 dbus_adaptor_.SetOemName(config.oem_name());
267 dbus_adaptor_.SetModelName(config.model_name());
268 dbus_adaptor_.SetModelId(config.model_id());
269 dbus_adaptor_.SetName(config.name());
270 dbus_adaptor_.SetDescription(config.description());
271 dbus_adaptor_.SetLocation(config.location());
272 dbus_adaptor_.SetAnonymousAccessRole(config.local_anonymous_access_role());
273}
274
Christopher Wiley4b5f04c2014-03-27 14:45:37 -0700275} // namespace buffet