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