| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/pairing/bluetooth_controller_pairing_controller.h" | 5 #include "components/pairing/bluetooth_controller_pairing_controller.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 DCHECK(thread_checker_.CalledOnValidThread()); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
| 86 std::set<std::string>::iterator ix = | 86 std::set<std::string>::iterator ix = |
| 87 discovered_devices_.find(device->GetAddress()); | 87 discovered_devices_.find(device->GetAddress()); |
| 88 if (ix != discovered_devices_.end()) { | 88 if (ix != discovered_devices_.end()) { |
| 89 discovered_devices_.erase(ix); | 89 discovered_devices_.erase(ix); |
| 90 FOR_EACH_OBSERVER(ControllerPairingController::Observer, observers_, | 90 FOR_EACH_OBSERVER(ControllerPairingController::Observer, observers_, |
| 91 DiscoveredDevicesListChanged()); | 91 DiscoveredDevicesListChanged()); |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 | 94 |
| 95 void BluetoothControllerPairingController::SendBuffer( |
| 96 int size, scoped_refptr<net::IOBuffer> io_buffer) { |
| 97 socket_->Send( |
| 98 io_buffer, size, |
| 99 base::Bind(&BluetoothControllerPairingController::OnSendComplete, |
| 100 ptr_factory_.GetWeakPtr()), |
| 101 base::Bind(&BluetoothControllerPairingController::OnErrorWithMessage, |
| 102 ptr_factory_.GetWeakPtr())); |
| 103 } |
| 104 |
| 95 void BluetoothControllerPairingController::OnSetPowered() { | 105 void BluetoothControllerPairingController::OnSetPowered() { |
| 96 DCHECK(thread_checker_.CalledOnValidThread()); | 106 DCHECK(thread_checker_.CalledOnValidThread()); |
| 97 adapter_->StartDiscoverySession( | 107 adapter_->StartDiscoverySession( |
| 98 base::Bind(&BluetoothControllerPairingController::OnStartDiscoverySession, | 108 base::Bind(&BluetoothControllerPairingController::OnStartDiscoverySession, |
| 99 ptr_factory_.GetWeakPtr()), | 109 ptr_factory_.GetWeakPtr()), |
| 100 base::Bind(&BluetoothControllerPairingController::OnError, | 110 base::Bind(&BluetoothControllerPairingController::OnError, |
| 101 ptr_factory_.GetWeakPtr())); | 111 ptr_factory_.GetWeakPtr())); |
| 102 } | 112 } |
| 103 | 113 |
| 104 void BluetoothControllerPairingController::OnGetAdapter( | 114 void BluetoothControllerPairingController::OnGetAdapter( |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 178 |
| 169 socket_->Receive( | 179 socket_->Receive( |
| 170 kReceiveSize, | 180 kReceiveSize, |
| 171 base::Bind(&BluetoothControllerPairingController::OnReceiveComplete, | 181 base::Bind(&BluetoothControllerPairingController::OnReceiveComplete, |
| 172 ptr_factory_.GetWeakPtr()), | 182 ptr_factory_.GetWeakPtr()), |
| 173 base::Bind(&BluetoothControllerPairingController::OnReceiveError, | 183 base::Bind(&BluetoothControllerPairingController::OnReceiveError, |
| 174 ptr_factory_.GetWeakPtr())); | 184 ptr_factory_.GetWeakPtr())); |
| 175 } | 185 } |
| 176 | 186 |
| 177 void BluetoothControllerPairingController::OnError() { | 187 void BluetoothControllerPairingController::OnError() { |
| 178 // TODO(zork): Add a stage for initialization error. (http://crbug.com/405744) | |
| 179 LOG(ERROR) << "Pairing initialization failed"; | 188 LOG(ERROR) << "Pairing initialization failed"; |
| 189 ChangeStage(STAGE_INITIALIZATION_ERROR); |
| 180 Reset(); | 190 Reset(); |
| 181 } | 191 } |
| 182 | 192 |
| 183 void BluetoothControllerPairingController::OnErrorWithMessage( | 193 void BluetoothControllerPairingController::OnErrorWithMessage( |
| 184 const std::string& message) { | 194 const std::string& message) { |
| 185 // TODO(zork): Add a stage for initialization error. (http://crbug.com/405744) | |
| 186 LOG(ERROR) << message; | 195 LOG(ERROR) << message; |
| 196 ChangeStage(STAGE_INITIALIZATION_ERROR); |
| 187 Reset(); | 197 Reset(); |
| 188 } | 198 } |
| 189 | 199 |
| 190 void BluetoothControllerPairingController::OnConnectError( | 200 void BluetoothControllerPairingController::OnConnectError( |
| 191 device::BluetoothDevice::ConnectErrorCode error_code) { | 201 device::BluetoothDevice::ConnectErrorCode error_code) { |
| 192 DCHECK(thread_checker_.CalledOnValidThread()); | 202 DCHECK(thread_checker_.CalledOnValidThread()); |
| 193 device::BluetoothDevice* device = GetController(); | 203 device::BluetoothDevice* device = GetController(); |
| 194 | 204 |
| 195 if (device && device->IsPaired()) { | 205 if (device && device->IsPaired()) { |
| 196 // The connection attempt is only used to start the pairing between the | 206 // The connection attempt is only used to start the pairing between the |
| (...skipping 24 matching lines...) Expand all Loading... |
| 221 BluetoothControllerPairingController::GetCurrentStage() { | 231 BluetoothControllerPairingController::GetCurrentStage() { |
| 222 return current_stage_; | 232 return current_stage_; |
| 223 } | 233 } |
| 224 | 234 |
| 225 void BluetoothControllerPairingController::StartPairing() { | 235 void BluetoothControllerPairingController::StartPairing() { |
| 226 DCHECK(thread_checker_.CalledOnValidThread()); | 236 DCHECK(thread_checker_.CalledOnValidThread()); |
| 227 DCHECK(current_stage_ == STAGE_NONE || | 237 DCHECK(current_stage_ == STAGE_NONE || |
| 228 current_stage_ == STAGE_DEVICE_NOT_FOUND || | 238 current_stage_ == STAGE_DEVICE_NOT_FOUND || |
| 229 current_stage_ == STAGE_ESTABLISHING_CONNECTION_ERROR || | 239 current_stage_ == STAGE_ESTABLISHING_CONNECTION_ERROR || |
| 230 current_stage_ == STAGE_HOST_ENROLLMENT_ERROR); | 240 current_stage_ == STAGE_HOST_ENROLLMENT_ERROR); |
| 231 // TODO(zork): Add a stage for no bluetooth. (http://crbug.com/405744) | |
| 232 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 241 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| 233 ChangeStage(STAGE_DEVICE_NOT_FOUND); | 242 ChangeStage(STAGE_INITIALIZATION_ERROR); |
| 234 return; | 243 return; |
| 235 } | 244 } |
| 236 | 245 |
| 237 device::BluetoothAdapterFactory::GetAdapter( | 246 device::BluetoothAdapterFactory::GetAdapter( |
| 238 base::Bind(&BluetoothControllerPairingController::OnGetAdapter, | 247 base::Bind(&BluetoothControllerPairingController::OnGetAdapter, |
| 239 ptr_factory_.GetWeakPtr())); | 248 ptr_factory_.GetWeakPtr())); |
| 240 | 249 |
| 241 } | 250 } |
| 242 | 251 |
| 243 ControllerPairingController::DeviceIdList | 252 ControllerPairingController::DeviceIdList |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 RepeatDiscovery(); | 311 RepeatDiscovery(); |
| 303 } | 312 } |
| 304 } | 313 } |
| 305 | 314 |
| 306 void BluetoothControllerPairingController::SetHostConfiguration( | 315 void BluetoothControllerPairingController::SetHostConfiguration( |
| 307 bool accepted_eula, | 316 bool accepted_eula, |
| 308 const std::string& lang, | 317 const std::string& lang, |
| 309 const std::string& timezone, | 318 const std::string& timezone, |
| 310 bool send_reports, | 319 bool send_reports, |
| 311 const std::string& keyboard_layout) { | 320 const std::string& keyboard_layout) { |
| 312 // TODO(zork): Get configuration from UI and send to Host. | 321 |
| 313 // (http://crbug.com/405744) | 322 pairing_api::ConfigureHost host_config; |
| 323 host_config.set_api_version(kPairingAPIVersion); |
| 324 host_config.mutable_parameters()->set_accepted_eula(accepted_eula); |
| 325 host_config.mutable_parameters()->set_lang(lang); |
| 326 host_config.mutable_parameters()->set_timezone(timezone); |
| 327 host_config.mutable_parameters()->set_send_reports(send_reports); |
| 328 host_config.mutable_parameters()->set_keyboard_layout(keyboard_layout); |
| 329 |
| 330 int size = 0; |
| 331 scoped_refptr<net::IOBuffer> io_buffer( |
| 332 ProtoDecoder::SendConfigureHost(host_config, &size)); |
| 333 |
| 334 SendBuffer(size, io_buffer); |
| 314 } | 335 } |
| 315 | 336 |
| 316 void BluetoothControllerPairingController::OnAuthenticationDone( | 337 void BluetoothControllerPairingController::OnAuthenticationDone( |
| 317 const std::string& domain, | 338 const std::string& domain, |
| 318 const std::string& auth_token) { | 339 const std::string& auth_token) { |
| 319 DCHECK_EQ(current_stage_, STAGE_WAITING_FOR_CREDENTIALS); | 340 DCHECK_EQ(current_stage_, STAGE_WAITING_FOR_CREDENTIALS); |
| 320 | 341 |
| 321 pairing_api::PairDevices pair_devices; | 342 pairing_api::PairDevices pair_devices; |
| 322 pair_devices.set_api_version(kPairingAPIVersion); | 343 pair_devices.set_api_version(kPairingAPIVersion); |
| 323 pair_devices.mutable_parameters()->set_admin_access_token(auth_token); | 344 pair_devices.mutable_parameters()->set_admin_access_token(auth_token); |
| 324 | 345 |
| 325 int size = 0; | 346 int size = 0; |
| 326 scoped_refptr<net::IOBuffer> io_buffer( | 347 scoped_refptr<net::IOBuffer> io_buffer( |
| 327 ProtoDecoder::SendPairDevices(pair_devices, &size)); | 348 ProtoDecoder::SendPairDevices(pair_devices, &size)); |
| 328 | 349 |
| 329 socket_->Send( | 350 SendBuffer(size, io_buffer); |
| 330 io_buffer, size, | |
| 331 base::Bind(&BluetoothControllerPairingController::OnSendComplete, | |
| 332 ptr_factory_.GetWeakPtr()), | |
| 333 base::Bind(&BluetoothControllerPairingController::OnErrorWithMessage, | |
| 334 ptr_factory_.GetWeakPtr())); | |
| 335 ChangeStage(STAGE_HOST_ENROLLMENT_IN_PROGRESS); | 351 ChangeStage(STAGE_HOST_ENROLLMENT_IN_PROGRESS); |
| 336 } | 352 } |
| 337 | 353 |
| 338 void BluetoothControllerPairingController::StartSession() { | 354 void BluetoothControllerPairingController::StartSession() { |
| 339 DCHECK_EQ(current_stage_, STAGE_PAIRING_DONE); | 355 DCHECK_EQ(current_stage_, STAGE_PAIRING_DONE); |
| 340 ChangeStage(STAGE_FINISHED); | 356 ChangeStage(STAGE_FINISHED); |
| 341 } | 357 } |
| 342 | 358 |
| 343 // ProtoDecoder::Observer: | 359 // ProtoDecoder::Observer: |
| 344 void BluetoothControllerPairingController::OnHostStatusMessage( | 360 void BluetoothControllerPairingController::OnHostStatusMessage( |
| 345 const pairing_api::HostStatus& message) { | 361 const pairing_api::HostStatus& message) { |
| 346 if (got_initial_status_) { | 362 if (got_initial_status_) { |
| 347 // TODO(zork): Check that the domain matches. (http://crbug.com/405761) | 363 // TODO(zork): Check that the domain matches. (http://crbug.com/405761) |
| 348 // TODO(zork): Handling updating stages (http://crbug.com/405754). | 364 // TODO(zork): Handling updating stages (http://crbug.com/405754). |
| 349 pairing_api::CompleteSetup complete_setup; | 365 pairing_api::CompleteSetup complete_setup; |
| 350 complete_setup.set_api_version(kPairingAPIVersion); | 366 complete_setup.set_api_version(kPairingAPIVersion); |
| 351 // TODO(zork): Get AddAnother from UI (http://crbug.com/405757) | 367 // TODO(zork): Get AddAnother from UI (http://crbug.com/405757) |
| 352 complete_setup.mutable_parameters()->set_add_another(false); | 368 complete_setup.mutable_parameters()->set_add_another(false); |
| 353 | 369 |
| 354 int size = 0; | 370 int size = 0; |
| 355 scoped_refptr<net::IOBuffer> io_buffer( | 371 scoped_refptr<net::IOBuffer> io_buffer( |
| 356 ProtoDecoder::SendCompleteSetup(complete_setup, &size)); | 372 ProtoDecoder::SendCompleteSetup(complete_setup, &size)); |
| 357 | 373 |
| 358 socket_->Send( | 374 SendBuffer(size, io_buffer); |
| 359 io_buffer, size, | |
| 360 base::Bind(&BluetoothControllerPairingController::OnSendComplete, | |
| 361 ptr_factory_.GetWeakPtr()), | |
| 362 base::Bind( | |
| 363 &BluetoothControllerPairingController::OnErrorWithMessage, | |
| 364 ptr_factory_.GetWeakPtr())); | |
| 365 ChangeStage(STAGE_PAIRING_DONE); | 375 ChangeStage(STAGE_PAIRING_DONE); |
| 366 } else { | 376 } else { |
| 367 got_initial_status_ = true; | 377 got_initial_status_ = true; |
| 368 | 378 |
| 369 // TODO(zork): Check domain. (http://crbug.com/405761) | 379 // TODO(zork): Check domain. (http://crbug.com/405761) |
| 370 ChangeStage(STAGE_WAITING_FOR_CREDENTIALS); | 380 ChangeStage(STAGE_WAITING_FOR_CREDENTIALS); |
| 371 } | 381 } |
| 372 } | 382 } |
| 373 | 383 |
| 374 void BluetoothControllerPairingController::OnConfigureHostMessage( | 384 void BluetoothControllerPairingController::OnConfigureHostMessage( |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); | 457 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); |
| 448 } | 458 } |
| 449 | 459 |
| 450 void BluetoothControllerPairingController::AuthorizePairing( | 460 void BluetoothControllerPairingController::AuthorizePairing( |
| 451 device::BluetoothDevice* device) { | 461 device::BluetoothDevice* device) { |
| 452 // Disallow unknown device. | 462 // Disallow unknown device. |
| 453 device->RejectPairing(); | 463 device->RejectPairing(); |
| 454 } | 464 } |
| 455 | 465 |
| 456 } // namespace pairing_chromeos | 466 } // namespace pairing_chromeos |
| OLD | NEW |