| 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_host_pairing_controller.h" | 5 #include "components/pairing/bluetooth_host_pairing_controller.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/hash.h" | 8 #include "base/hash.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
| 14 #include "chromeos/system/devicetype.h" | 14 #include "chromeos/system/devicetype.h" |
| 15 #include "components/pairing/bluetooth_pairing_constants.h" | 15 #include "components/pairing/bluetooth_pairing_constants.h" |
| 16 #include "components/pairing/pairing_api.pb.h" | 16 #include "components/pairing/pairing_api.pb.h" |
| 17 #include "components/pairing/proto_decoder.h" | 17 #include "components/pairing/proto_decoder.h" |
| 18 #include "device/bluetooth/bluetooth_adapter_factory.h" | 18 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 19 #include "net/base/io_buffer.h" | 19 #include "net/base/io_buffer.h" |
| 20 | 20 |
| 21 namespace pairing_chromeos { | 21 namespace pairing_chromeos { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 const int kReceiveSize = 16384; | 24 const int kReceiveSize = 16384; |
| 25 | 25 |
| 26 std::string GetChromeOSDeviceType() { | |
| 27 switch (chromeos::GetDeviceType()) { | |
| 28 case chromeos::DeviceType::kChromebox: | |
| 29 return std::string(kDeviceNamePrefix) + "box"; | |
| 30 case chromeos::DeviceType::kChromebase: | |
| 31 return std::string(kDeviceNamePrefix) + "base"; | |
| 32 case chromeos::DeviceType::kChromebit: | |
| 33 return std::string(kDeviceNamePrefix) + "bit"; | |
| 34 case chromeos::DeviceType::kChromebook: | |
| 35 return std::string(kDeviceNamePrefix) + "book"; | |
| 36 default: | |
| 37 return std::string(kDeviceNamePrefix) + "device"; | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 pairing_api::HostStatusParameters::Connectivity PairingApiConnectivityStatus( | 26 pairing_api::HostStatusParameters::Connectivity PairingApiConnectivityStatus( |
| 42 HostPairingController::Connectivity connectivity_status) { | 27 HostPairingController::Connectivity connectivity_status) { |
| 43 switch (connectivity_status) { | 28 switch (connectivity_status) { |
| 44 case HostPairingController::CONNECTIVITY_UNTESTED: | 29 case HostPairingController::CONNECTIVITY_UNTESTED: |
| 45 return pairing_api::HostStatusParameters::CONNECTIVITY_UNTESTED; | 30 return pairing_api::HostStatusParameters::CONNECTIVITY_UNTESTED; |
| 46 case HostPairingController::CONNECTIVITY_NONE: | 31 case HostPairingController::CONNECTIVITY_NONE: |
| 47 return pairing_api::HostStatusParameters::CONNECTIVITY_NONE; | 32 return pairing_api::HostStatusParameters::CONNECTIVITY_NONE; |
| 48 case HostPairingController::CONNECTIVITY_LIMITED: | 33 case HostPairingController::CONNECTIVITY_LIMITED: |
| 49 return pairing_api::HostStatusParameters::CONNECTIVITY_LIMITED; | 34 return pairing_api::HostStatusParameters::CONNECTIVITY_LIMITED; |
| 50 case HostPairingController::CONNECTIVITY_CONNECTING: | 35 case HostPairingController::CONNECTIVITY_CONNECTING: |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 OnForget(); | 165 OnForget(); |
| 181 } | 166 } |
| 182 | 167 |
| 183 void BluetoothHostPairingController::OnGetAdapter( | 168 void BluetoothHostPairingController::OnGetAdapter( |
| 184 scoped_refptr<device::BluetoothAdapter> adapter) { | 169 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 185 DCHECK(thread_checker_.CalledOnValidThread()); | 170 DCHECK(thread_checker_.CalledOnValidThread()); |
| 186 DCHECK(!adapter_.get()); | 171 DCHECK(!adapter_.get()); |
| 187 adapter_ = adapter; | 172 adapter_ = adapter; |
| 188 | 173 |
| 189 if (adapter_->IsPresent()) { | 174 if (adapter_->IsPresent()) { |
| 190 SetName(); | 175 SetPowered(); |
| 191 } else { | 176 } else { |
| 192 // Set the name once the adapter is present. | 177 // Set the name once the adapter is present. |
| 193 adapter_->AddObserver(this); | 178 adapter_->AddObserver(this); |
| 194 } | 179 } |
| 195 } | 180 } |
| 196 | 181 |
| 197 void BluetoothHostPairingController::SetName() { | 182 void BluetoothHostPairingController::SetPowered() { |
| 198 // Hash the bluetooth address and take the lower 2 bytes to create a human | |
| 199 // readable device name. | |
| 200 const uint32_t device_id = base::Hash(adapter_->GetAddress()) & 0xFFFF; | |
| 201 device_name_ = | |
| 202 base::StringPrintf("%s_%04X", GetChromeOSDeviceType().c_str(), device_id); | |
| 203 | |
| 204 adapter_->SetName( | |
| 205 device_name_, | |
| 206 base::Bind(&BluetoothHostPairingController::OnSetName, | |
| 207 ptr_factory_.GetWeakPtr()), | |
| 208 base::Bind(&BluetoothHostPairingController::OnSetError, | |
| 209 ptr_factory_.GetWeakPtr())); | |
| 210 } | |
| 211 | |
| 212 void BluetoothHostPairingController::OnSetName() { | |
| 213 DCHECK(thread_checker_.CalledOnValidThread()); | 183 DCHECK(thread_checker_.CalledOnValidThread()); |
| 214 if (adapter_->IsPowered()) { | 184 if (adapter_->IsPowered()) { |
| 215 was_powered_ = true; | 185 was_powered_ = true; |
| 216 OnSetPowered(); | 186 OnSetPowered(); |
| 217 } else { | 187 } else { |
| 218 adapter_->SetPowered( | 188 adapter_->SetPowered( |
| 219 true, | 189 true, |
| 220 base::Bind(&BluetoothHostPairingController::OnSetPowered, | 190 base::Bind(&BluetoothHostPairingController::OnSetPowered, |
| 221 ptr_factory_.GetWeakPtr()), | 191 ptr_factory_.GetWeakPtr()), |
| 222 base::Bind(&BluetoothHostPairingController::OnSetError, | 192 base::Bind(&BluetoothHostPairingController::OnSetError, |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 446 for (Observer& observer : observers_) | 416 for (Observer& observer : observers_) |
| 447 observer.AddNetworkRequested(message.parameters().onc_spec()); | 417 observer.AddNetworkRequested(message.parameters().onc_spec()); |
| 448 } | 418 } |
| 449 | 419 |
| 450 void BluetoothHostPairingController::AdapterPresentChanged( | 420 void BluetoothHostPairingController::AdapterPresentChanged( |
| 451 device::BluetoothAdapter* adapter, | 421 device::BluetoothAdapter* adapter, |
| 452 bool present) { | 422 bool present) { |
| 453 DCHECK_EQ(adapter, adapter_.get()); | 423 DCHECK_EQ(adapter, adapter_.get()); |
| 454 if (present) { | 424 if (present) { |
| 455 adapter_->RemoveObserver(this); | 425 adapter_->RemoveObserver(this); |
| 456 SetName(); | 426 SetPowered(); |
| 457 } | 427 } |
| 458 } | 428 } |
| 459 | 429 |
| 460 void BluetoothHostPairingController::AddObserver(Observer* observer) { | 430 void BluetoothHostPairingController::AddObserver(Observer* observer) { |
| 461 observers_.AddObserver(observer); | 431 observers_.AddObserver(observer); |
| 462 } | 432 } |
| 463 | 433 |
| 464 void BluetoothHostPairingController::RemoveObserver(Observer* observer) { | 434 void BluetoothHostPairingController::RemoveObserver(Observer* observer) { |
| 465 observers_.RemoveObserver(observer); | 435 observers_.RemoveObserver(observer); |
| 466 } | 436 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 477 ChangeStage(STAGE_INITIALIZATION_ERROR); | 447 ChangeStage(STAGE_INITIALIZATION_ERROR); |
| 478 return; | 448 return; |
| 479 } | 449 } |
| 480 | 450 |
| 481 device::BluetoothAdapterFactory::GetAdapter( | 451 device::BluetoothAdapterFactory::GetAdapter( |
| 482 base::Bind(&BluetoothHostPairingController::OnGetAdapter, | 452 base::Bind(&BluetoothHostPairingController::OnGetAdapter, |
| 483 ptr_factory_.GetWeakPtr())); | 453 ptr_factory_.GetWeakPtr())); |
| 484 } | 454 } |
| 485 | 455 |
| 486 std::string BluetoothHostPairingController::GetDeviceName() { | 456 std::string BluetoothHostPairingController::GetDeviceName() { |
| 487 return device_name_; | 457 return adapter_.get() ? adapter_->GetName() : std::string(); |
| 488 } | 458 } |
| 489 | 459 |
| 490 std::string BluetoothHostPairingController::GetConfirmationCode() { | 460 std::string BluetoothHostPairingController::GetConfirmationCode() { |
| 491 DCHECK_EQ(current_stage_, STAGE_WAITING_FOR_CODE_CONFIRMATION); | 461 DCHECK_EQ(current_stage_, STAGE_WAITING_FOR_CODE_CONFIRMATION); |
| 492 return confirmation_code_; | 462 return confirmation_code_; |
| 493 } | 463 } |
| 494 | 464 |
| 495 std::string BluetoothHostPairingController::GetEnrollmentDomain() { | 465 std::string BluetoothHostPairingController::GetEnrollmentDomain() { |
| 496 return enrollment_domain_; | 466 return enrollment_domain_; |
| 497 } | 467 } |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); | 547 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); |
| 578 } | 548 } |
| 579 | 549 |
| 580 void BluetoothHostPairingController::AuthorizePairing( | 550 void BluetoothHostPairingController::AuthorizePairing( |
| 581 device::BluetoothDevice* device) { | 551 device::BluetoothDevice* device) { |
| 582 // Disallow unknown device. | 552 // Disallow unknown device. |
| 583 device->RejectPairing(); | 553 device->RejectPairing(); |
| 584 } | 554 } |
| 585 | 555 |
| 586 } // namespace pairing_chromeos | 556 } // namespace pairing_chromeos |
| OLD | NEW |