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