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> |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 8 | #include <set> |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 9 | #include <string> |
| 10 | |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 11 | #include <base/bind.h> |
| 12 | #include <base/bind_helpers.h> |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 13 | #include <base/json/json_reader.h> |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 14 | #include <base/json/json_writer.h> |
Christopher Wiley | cd41966 | 2015-02-06 17:51:43 -0800 | [diff] [blame] | 15 | #include <base/time/time.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 16 | #include <chromeos/dbus/async_event_sequencer.h> |
| 17 | #include <chromeos/dbus/exported_object_manager.h> |
| 18 | #include <chromeos/errors/error.h> |
Garret Kelly | 9a89538 | 2015-05-29 15:56:52 -0400 | [diff] [blame] | 19 | #include <chromeos/http/http_transport.h> |
Anton Muhin | 332df19 | 2014-11-22 05:59:14 +0400 | [diff] [blame] | 20 | #include <chromeos/key_value_store.h> |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 21 | #include <dbus/bus.h> |
Christopher Wiley | 9001624 | 2014-04-01 17:33:29 -0700 | [diff] [blame] | 22 | #include <dbus/object_path.h> |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 23 | #include <dbus/values_util.h> |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 24 | |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 25 | #include "buffet/base_api_handler.h" |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 26 | #include "buffet/commands/command_instance.h" |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 27 | #include "buffet/commands/schema_constants.h" |
Vitaly Buka | 505c413 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 28 | #include "buffet/privet/constants.h" |
| 29 | #include "buffet/privet/security_manager.h" |
| 30 | #include "buffet/privet/wifi_bootstrap_manager.h" |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 31 | #include "buffet/states/state_change_queue.h" |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 32 | #include "buffet/states/state_manager.h" |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 33 | #include "buffet/storage_impls.h" |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 34 | |
Christopher Wiley | 2d2d92b | 2014-07-29 14:07:10 -0700 | [diff] [blame] | 35 | using chromeos::dbus_utils::AsyncEventSequencer; |
Christopher Wiley | b5dd5ea | 2014-08-11 10:51:20 -0700 | [diff] [blame] | 36 | using chromeos::dbus_utils::ExportedObjectManager; |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 37 | |
| 38 | namespace buffet { |
| 39 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 40 | namespace { |
Vitaly Buka | 505c413 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 41 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 42 | // Max of 100 state update events should be enough in the queue. |
| 43 | const size_t kMaxStateChangeQueueSize = 100; |
Garret Kelly | 9a89538 | 2015-05-29 15:56:52 -0400 | [diff] [blame] | 44 | // The number of seconds each HTTP request will be allowed before timing out. |
| 45 | const int kRequestTimeoutSeconds = 30; |
Vitaly Buka | 505c413 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 46 | |
| 47 | const char kPairingSessionIdKey[] = "sessionId"; |
| 48 | const char kPairingModeKey[] = "mode"; |
| 49 | const char kPairingCodeKey[] = "code"; |
| 50 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 51 | } // anonymous namespace |
| 52 | |
Alex Vakulenko | f2784de | 2014-08-15 11:49:35 -0700 | [diff] [blame] | 53 | Manager::Manager(const base::WeakPtr<ExportedObjectManager>& object_manager) |
| 54 | : dbus_object_(object_manager.get(), |
| 55 | object_manager->GetBus(), |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 56 | org::chromium::Buffet::ManagerAdaptor::GetObjectPath()) { |
| 57 | } |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 58 | |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 59 | Manager::~Manager() { |
| 60 | } |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 61 | |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 62 | void Manager::Start(const Options& options, AsyncEventSequencer* sequencer) { |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 63 | command_manager_ = |
| 64 | std::make_shared<CommandManager>(dbus_object_.GetObjectManager()); |
Vitaly Buka | 5e6ff6c | 2015-05-11 15:41:33 -0700 | [diff] [blame] | 65 | command_manager_->AddOnCommandDefChanged(base::Bind( |
| 66 | &Manager::OnCommandDefsChanged, weak_ptr_factory_.GetWeakPtr())); |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 67 | command_manager_->Startup(base::FilePath{"/etc/buffet"}, |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 68 | options.test_definitions_path); |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 69 | state_change_queue_.reset(new StateChangeQueue(kMaxStateChangeQueueSize)); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 70 | state_manager_ = std::make_shared<StateManager>(state_change_queue_.get()); |
Vitaly Buka | dbb8c35 | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 71 | state_manager_->AddOnChangedCallback( |
| 72 | base::Bind(&Manager::OnStateChanged, weak_ptr_factory_.GetWeakPtr())); |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 73 | state_manager_->Startup(); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 74 | |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 75 | std::unique_ptr<BuffetConfig> config{new BuffetConfig{options.state_path}}; |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 76 | config->AddOnChangedCallback( |
| 77 | base::Bind(&Manager::OnConfigChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 78 | config->Load(options.config_path); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 79 | |
Garret Kelly | 9a89538 | 2015-05-29 15:56:52 -0400 | [diff] [blame] | 80 | auto transport = chromeos::http::Transport::CreateDefault(); |
| 81 | transport->SetDefaultTimeout(base::TimeDelta::FromSeconds( |
| 82 | kRequestTimeoutSeconds)); |
| 83 | |
Christopher Wiley | e0fdeee | 2015-02-07 18:29:32 -0800 | [diff] [blame] | 84 | // TODO(avakulenko): Figure out security implications of storing |
| 85 | // device info state data unencrypted. |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 86 | device_info_.reset(new DeviceRegistrationInfo( |
Garret Kelly | 9a89538 | 2015-05-29 15:56:52 -0400 | [diff] [blame] | 87 | command_manager_, state_manager_, std::move(config), transport, |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 88 | options.xmpp_enabled)); |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 89 | device_info_->AddOnRegistrationChangedCallback(base::Bind( |
| 90 | &Manager::OnRegistrationChanged, weak_ptr_factory_.GetWeakPtr())); |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 91 | |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 92 | base_api_handler_.reset(new BaseApiHandler{ |
| 93 | device_info_->AsWeakPtr(), state_manager_, command_manager_}); |
Vitaly Buka | 72410b2 | 2015-05-13 13:48:59 -0700 | [diff] [blame] | 94 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 95 | device_info_->Start(); |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 96 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 97 | dbus_adaptor_.RegisterWithDBusObject(&dbus_object_); |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 98 | dbus_object_.RegisterAsync( |
| 99 | sequencer->GetHandler("Manager.RegisterAsync() failed.", true)); |
| 100 | |
| 101 | if (!options.privet.disable_privet) |
| 102 | StartPrivet(options.privet, sequencer); |
| 103 | } |
| 104 | |
| 105 | void Manager::StartPrivet(const privetd::Manager::Options& options, |
| 106 | AsyncEventSequencer* sequencer) { |
| 107 | privet_.reset(new privetd::Manager{}); |
Vitaly Buka | 7ab89ff | 2015-06-09 22:11:40 -0700 | [diff] [blame^] | 108 | privet_->Start(options, dbus_object_.GetBus(), device_info_.get(), |
| 109 | command_manager_.get(), state_manager_.get(), sequencer); |
Vitaly Buka | ae96cc3 | 2015-06-09 17:22:18 -0700 | [diff] [blame] | 110 | |
| 111 | if (privet_->GetWifiBootstrapManager()) { |
| 112 | privet_->GetWifiBootstrapManager()->RegisterStateListener(base::Bind( |
| 113 | &Manager::UpdateWiFiBootstrapState, weak_ptr_factory_.GetWeakPtr())); |
| 114 | } else { |
| 115 | UpdateWiFiBootstrapState(privetd::WifiBootstrapManager::kDisabled); |
| 116 | } |
| 117 | |
| 118 | privet_->GetSecurityManager()->RegisterPairingListeners( |
| 119 | base::Bind(&Manager::OnPairingStart, weak_ptr_factory_.GetWeakPtr()), |
| 120 | base::Bind(&Manager::OnPairingEnd, weak_ptr_factory_.GetWeakPtr())); |
| 121 | // TODO(wiley) Watch for appropriate state variables from |cloud_delegate|. |
| 122 | } |
| 123 | |
| 124 | void Manager::Stop() { |
| 125 | if (privet_) |
| 126 | privet_->OnShutdown(); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 129 | void Manager::CheckDeviceRegistered(DBusMethodResponse<std::string> response) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 130 | LOG(INFO) << "Received call to Manager.CheckDeviceRegistered()"; |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 131 | chromeos::ErrorPtr error; |
Nathan Bullock | 5e022a3 | 2015-04-08 15:13:07 -0400 | [diff] [blame] | 132 | bool registered = device_info_->HaveRegistrationCredentials(&error); |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 133 | // If it fails due to any reason other than 'device not registered', |
| 134 | // treat it as a real error and report it to the caller. |
| 135 | if (!registered && |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 136 | !error->HasError(kErrorDomainGCD, "device_not_registered")) { |
| 137 | response->ReplyWithError(error.get()); |
| 138 | return; |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 139 | } |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 140 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 141 | response->Return(registered ? device_info_->GetConfig().device_id() |
| 142 | : std::string()); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 145 | void Manager::GetDeviceInfo(DBusMethodResponse<std::string> response) { |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 146 | LOG(INFO) << "Received call to Manager.GetDeviceInfo()"; |
| 147 | |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 148 | chromeos::ErrorPtr error; |
| 149 | auto device_info = device_info_->GetDeviceInfo(&error); |
| 150 | if (!device_info) { |
| 151 | response->ReplyWithError(error.get()); |
| 152 | return; |
| 153 | } |
Alex Vakulenko | b3aac25 | 2014-05-07 17:35:24 -0700 | [diff] [blame] | 154 | |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 155 | std::string device_info_str; |
Nathan Bullock | 4b6c0fb | 2015-04-01 15:32:58 -0400 | [diff] [blame] | 156 | base::JSONWriter::WriteWithOptions(device_info.get(), |
| 157 | base::JSONWriter::OPTIONS_PRETTY_PRINT, &device_info_str); |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 158 | response->Return(device_info_str); |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 161 | void Manager::RegisterDevice(DBusMethodResponse<std::string> response, |
Vitaly Buka | cad2e33 | 2015-05-14 23:33:32 -0700 | [diff] [blame] | 162 | const std::string& ticket_id) { |
Anton Muhin | beb1c5b | 2014-10-16 18:59:57 +0400 | [diff] [blame] | 163 | LOG(INFO) << "Received call to Manager.RegisterDevice()"; |
Alex Vakulenko | 3cb466c | 2014-04-15 11:36:32 -0700 | [diff] [blame] | 164 | |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 165 | chromeos::ErrorPtr error; |
Vitaly Buka | cad2e33 | 2015-05-14 23:33:32 -0700 | [diff] [blame] | 166 | std::string device_id = device_info_->RegisterDevice(ticket_id, &error); |
David Zeuthen | 1dbad47 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 167 | if (!device_id.empty()) { |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 168 | response->Return(device_id); |
David Zeuthen | 1dbad47 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | if (!error) { |
| 172 | // TODO(zeuthen): This can be changed to CHECK(error) once |
| 173 | // RegisterDevice() has been fixed to set |error| when failing. |
Vitaly Buka | 7ad8ffb | 2015-03-20 09:46:57 -0700 | [diff] [blame] | 174 | chromeos::Error::AddTo(&error, FROM_HERE, kErrorDomainGCD, "internal_error", |
David Zeuthen | 1dbad47 | 2015-02-12 15:24:21 -0500 | [diff] [blame] | 175 | "device_id empty but error not set"); |
| 176 | } |
| 177 | response->ReplyWithError(error.get()); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 180 | void Manager::UpdateState(DBusMethodResponse<> response, |
| 181 | const chromeos::VariantDictionary& property_set) { |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 182 | chromeos::ErrorPtr error; |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 183 | if (!state_manager_->SetProperties(property_set, &error)) |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 184 | response->ReplyWithError(error.get()); |
| 185 | else |
| 186 | response->Return(); |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Alex Vakulenko | 61ad4db | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 189 | bool Manager::GetState(chromeos::ErrorPtr* error, std::string* state) { |
| 190 | auto json = state_manager_->GetStateValuesAsJson(error); |
| 191 | if (!json) |
| 192 | return false; |
Nathan Bullock | 4b6c0fb | 2015-04-01 15:32:58 -0400 | [diff] [blame] | 193 | base::JSONWriter::WriteWithOptions( |
| 194 | json.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, state); |
Alex Vakulenko | 61ad4db | 2015-01-20 10:50:04 -0800 | [diff] [blame] | 195 | return true; |
| 196 | } |
| 197 | |
Vitaly Buka | 64fc5fc | 2015-03-24 12:42:24 -0700 | [diff] [blame] | 198 | void Manager::AddCommand(DBusMethodResponse<std::string> response, |
Vitaly Buka | b209842 | 2015-05-31 23:32:46 -0700 | [diff] [blame] | 199 | const std::string& json_command, |
| 200 | const std::string& in_user_role) { |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 201 | std::string error_message; |
| 202 | std::unique_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
| 203 | json_command, base::JSON_PARSE_RFC, nullptr, &error_message)); |
Vitaly Buka | 4547afa | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 204 | const base::DictionaryValue* command{nullptr}; |
| 205 | if (!value || !value->GetAsDictionary(&command)) { |
| 206 | return response->ReplyWithError(FROM_HERE, chromeos::errors::json::kDomain, |
| 207 | chromeos::errors::json::kParseError, |
| 208 | error_message); |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 209 | } |
Vitaly Buka | 4547afa | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 210 | |
Alex Vakulenko | 5c7bf01 | 2014-10-30 16:28:38 -0700 | [diff] [blame] | 211 | chromeos::ErrorPtr error; |
Vitaly Buka | b209842 | 2015-05-31 23:32:46 -0700 | [diff] [blame] | 212 | UserRole role; |
Vitaly Buka | 4547afa | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 213 | if (!FromString(in_user_role, &role, &error)) |
| 214 | return response->ReplyWithError(error.get()); |
Vitaly Buka | b209842 | 2015-05-31 23:32:46 -0700 | [diff] [blame] | 215 | |
Vitaly Buka | 4547afa | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 216 | std::string id; |
| 217 | if (!command_manager_->AddCommand(*command, role, &id, &error)) |
| 218 | return response->ReplyWithError(error.get()); |
Vitaly Buka | b209842 | 2015-05-31 23:32:46 -0700 | [diff] [blame] | 219 | |
Vitaly Buka | 64fc5fc | 2015-03-24 12:42:24 -0700 | [diff] [blame] | 220 | response->Return(id); |
Alex Vakulenko | 665c885 | 2014-09-11 16:57:24 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Vitaly Buka | 3886e8f | 2015-03-24 11:39:40 -0700 | [diff] [blame] | 223 | void Manager::GetCommand(DBusMethodResponse<std::string> response, |
| 224 | const std::string& id) { |
| 225 | const CommandInstance* command = command_manager_->FindCommand(id); |
| 226 | if (!command) { |
| 227 | response->ReplyWithError(FROM_HERE, kErrorDomainGCD, "unknown_command", |
| 228 | "Can't find command with id: " + id); |
| 229 | return; |
| 230 | } |
| 231 | std::string command_str; |
Nathan Bullock | 4b6c0fb | 2015-04-01 15:32:58 -0400 | [diff] [blame] | 232 | base::JSONWriter::WriteWithOptions(command->ToJson().get(), |
| 233 | base::JSONWriter::OPTIONS_PRETTY_PRINT, &command_str); |
Vitaly Buka | 3886e8f | 2015-03-24 11:39:40 -0700 | [diff] [blame] | 234 | response->Return(command_str); |
| 235 | } |
| 236 | |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 237 | void Manager::SetCommandVisibility( |
Alex Vakulenko | 41f73a9 | 2015-04-24 18:09:32 -0700 | [diff] [blame] | 238 | std::unique_ptr<chromeos::dbus_utils::DBusMethodResponse<>> response, |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 239 | const std::vector<std::string>& in_names, |
| 240 | const std::string& in_visibility) { |
| 241 | CommandDefinition::Visibility visibility; |
| 242 | chromeos::ErrorPtr error; |
| 243 | if (!visibility.FromString(in_visibility, &error)) { |
| 244 | response->ReplyWithError(error.get()); |
| 245 | return; |
| 246 | } |
| 247 | if (!command_manager_->SetCommandVisibility(in_names, visibility, &error)) { |
| 248 | response->ReplyWithError(error.get()); |
| 249 | return; |
| 250 | } |
| 251 | response->Return(); |
| 252 | } |
| 253 | |
Alex Vakulenko | 2348e42 | 2014-11-21 08:57:57 -0800 | [diff] [blame] | 254 | std::string Manager::TestMethod(const std::string& message) { |
Alex Vakulenko | 7a1dc0b | 2014-08-15 11:45:46 -0700 | [diff] [blame] | 255 | LOG(INFO) << "Received call to test method: " << message; |
| 256 | return message; |
Christopher Wiley | b76eb29 | 2014-05-05 16:09:16 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Vitaly Buka | 505c413 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 259 | bool Manager::EnableWiFiBootstrapping( |
| 260 | chromeos::ErrorPtr* error, |
| 261 | const dbus::ObjectPath& in_listener_path, |
| 262 | const chromeos::VariantDictionary& in_options) { |
| 263 | chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain, |
| 264 | privetd::errors::kNotImplemented, |
| 265 | "Manual WiFi bootstrapping is not implemented"); |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | bool Manager::DisableWiFiBootstrapping(chromeos::ErrorPtr* error) { |
| 270 | chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain, |
| 271 | privetd::errors::kNotImplemented, |
| 272 | "Manual WiFi bootstrapping is not implemented"); |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | bool Manager::EnableGCDBootstrapping( |
| 277 | chromeos::ErrorPtr* error, |
| 278 | const dbus::ObjectPath& in_listener_path, |
| 279 | const chromeos::VariantDictionary& in_options) { |
| 280 | chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain, |
| 281 | privetd::errors::kNotImplemented, |
| 282 | "Manual GCD bootstrapping is not implemented"); |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | bool Manager::DisableGCDBootstrapping(chromeos::ErrorPtr* error) { |
| 287 | chromeos::Error::AddTo(error, FROM_HERE, privetd::errors::kDomain, |
| 288 | privetd::errors::kNotImplemented, |
| 289 | "Manual GCD bootstrapping is not implemented"); |
| 290 | return false; |
| 291 | } |
| 292 | |
Vitaly Buka | fa94706 | 2015-04-17 00:41:31 -0700 | [diff] [blame] | 293 | bool Manager::UpdateDeviceInfo(chromeos::ErrorPtr* error, |
| 294 | const std::string& in_name, |
| 295 | const std::string& in_description, |
| 296 | const std::string& in_location) { |
| 297 | return device_info_->UpdateDeviceInfo(in_name, in_description, in_location, |
| 298 | error); |
Christopher Wiley | c900e48 | 2015-02-15 15:42:04 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Vitaly Buka | ff81db6 | 2015-05-14 21:25:45 -0700 | [diff] [blame] | 301 | bool Manager::UpdateServiceConfig(chromeos::ErrorPtr* error, |
| 302 | const std::string& client_id, |
| 303 | const std::string& client_secret, |
| 304 | const std::string& api_key, |
| 305 | const std::string& oauth_url, |
| 306 | const std::string& service_url) { |
| 307 | return device_info_->UpdateServiceConfig(client_id, client_secret, api_key, |
| 308 | oauth_url, service_url, error); |
| 309 | } |
| 310 | |
Vitaly Buka | aabadee | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 311 | void Manager::OnCommandDefsChanged() { |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 312 | // Limit only to commands that are visible to the local clients. |
| 313 | auto commands = command_manager_->GetCommandDictionary().GetCommandsAsJson( |
| 314 | [](const buffet::CommandDefinition* def) { |
| 315 | return def->GetVisibility().local; |
Vitaly Buka | dbb8c35 | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 316 | }, true, nullptr); |
Vitaly Buka | aabadee | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 317 | CHECK(commands); |
| 318 | std::string json; |
Nathan Bullock | 4b6c0fb | 2015-04-01 15:32:58 -0400 | [diff] [blame] | 319 | base::JSONWriter::WriteWithOptions(commands.get(), |
| 320 | base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
Vitaly Buka | aabadee | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 321 | dbus_adaptor_.SetCommandDefs(json); |
| 322 | } |
| 323 | |
Vitaly Buka | dbb8c35 | 2015-05-27 09:27:08 -0700 | [diff] [blame] | 324 | void Manager::OnStateChanged() { |
| 325 | auto state = state_manager_->GetStateValuesAsJson(nullptr); |
| 326 | CHECK(state); |
| 327 | std::string json; |
| 328 | base::JSONWriter::WriteWithOptions( |
| 329 | state.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &json); |
| 330 | dbus_adaptor_.SetState(json); |
| 331 | } |
| 332 | |
Vitaly Buka | ee7a3af | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 333 | void Manager::OnRegistrationChanged(RegistrationStatus status) { |
| 334 | dbus_adaptor_.SetStatus(StatusToString(status)); |
| 335 | } |
| 336 | |
| 337 | void Manager::OnConfigChanged(const BuffetConfig& config) { |
| 338 | dbus_adaptor_.SetDeviceId(config.device_id()); |
| 339 | dbus_adaptor_.SetOemName(config.oem_name()); |
| 340 | dbus_adaptor_.SetModelName(config.model_name()); |
| 341 | dbus_adaptor_.SetModelId(config.model_id()); |
| 342 | dbus_adaptor_.SetName(config.name()); |
| 343 | dbus_adaptor_.SetDescription(config.description()); |
| 344 | dbus_adaptor_.SetLocation(config.location()); |
| 345 | dbus_adaptor_.SetAnonymousAccessRole(config.local_anonymous_access_role()); |
| 346 | } |
| 347 | |
Vitaly Buka | 505c413 | 2015-06-09 17:01:54 -0700 | [diff] [blame] | 348 | void Manager::UpdateWiFiBootstrapState( |
| 349 | privetd::WifiBootstrapManager::State state) { |
| 350 | switch (state) { |
| 351 | case privetd::WifiBootstrapManager::kDisabled: |
| 352 | dbus_adaptor_.SetWiFiBootstrapState("disabled"); |
| 353 | break; |
| 354 | case privetd::WifiBootstrapManager::kBootstrapping: |
| 355 | dbus_adaptor_.SetWiFiBootstrapState("waiting"); |
| 356 | break; |
| 357 | case privetd::WifiBootstrapManager::kMonitoring: |
| 358 | dbus_adaptor_.SetWiFiBootstrapState("monitoring"); |
| 359 | break; |
| 360 | case privetd::WifiBootstrapManager::kConnecting: |
| 361 | dbus_adaptor_.SetWiFiBootstrapState("connecting"); |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | void Manager::OnPairingStart(const std::string& session_id, |
| 367 | privetd::PairingType pairing_type, |
| 368 | const std::vector<uint8_t>& code) { |
| 369 | // For now, just overwrite the exposed PairInfo with |
| 370 | // the most recent pairing attempt. |
| 371 | dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{ |
| 372 | {kPairingSessionIdKey, session_id}, |
| 373 | {kPairingModeKey, PairingTypeToString(pairing_type)}, |
| 374 | {kPairingCodeKey, code}, |
| 375 | }); |
| 376 | } |
| 377 | |
| 378 | void Manager::OnPairingEnd(const std::string& session_id) { |
| 379 | auto exposed_pairing_attempt = dbus_adaptor_.GetPairingInfo(); |
| 380 | auto it = exposed_pairing_attempt.find(kPairingSessionIdKey); |
| 381 | if (it == exposed_pairing_attempt.end()) { |
| 382 | return; |
| 383 | } |
| 384 | std::string exposed_session{it->second.TryGet<std::string>()}; |
| 385 | if (exposed_session == session_id) { |
| 386 | dbus_adaptor_.SetPairingInfo(chromeos::VariantDictionary{}); |
| 387 | } |
| 388 | } |
| 389 | |
Christopher Wiley | a4915c4 | 2014-03-27 14:45:37 -0700 | [diff] [blame] | 390 | } // namespace buffet |