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/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "components/pairing/bluetooth_pairing_constants.h" | 10 #include "components/pairing/bluetooth_pairing_constants.h" |
11 #include "components/pairing/pairing_api.pb.h" | 11 #include "components/pairing/pairing_api.pb.h" |
12 #include "components/pairing/proto_decoder.h" | 12 #include "components/pairing/proto_decoder.h" |
13 #include "device/bluetooth/bluetooth_adapter_factory.h" | 13 #include "device/bluetooth/bluetooth_adapter_factory.h" |
14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 const int kReceiveSize = 16384; | 17 const int kReceiveSize = 16384; |
18 } | 18 } |
19 | 19 |
20 namespace pairing_chromeos { | 20 namespace pairing_chromeos { |
21 | 21 |
22 BluetoothHostPairingController::BluetoothHostPairingController() | 22 BluetoothHostPairingController::BluetoothHostPairingController() |
23 : current_stage_(STAGE_NONE), | 23 : current_stage_(STAGE_NONE), |
24 device_(NULL), | 24 device_(NULL), |
25 proto_decoder_(new ProtoDecoder(this)), | 25 proto_decoder_(new ProtoDecoder(this)), |
26 ptr_factory_(this) { | 26 ptr_factory_(this) { |
27 } | 27 } |
28 | 28 |
29 BluetoothHostPairingController::~BluetoothHostPairingController() {} | 29 BluetoothHostPairingController::~BluetoothHostPairingController() { |
| 30 if (adapter_.get()) |
| 31 adapter_->RemoveObserver(this); |
| 32 } |
30 | 33 |
31 void BluetoothHostPairingController::ChangeStage(Stage new_stage) { | 34 void BluetoothHostPairingController::ChangeStage(Stage new_stage) { |
32 if (current_stage_ == new_stage) | 35 if (current_stage_ == new_stage) |
33 return; | 36 return; |
34 current_stage_ = new_stage; | 37 current_stage_ = new_stage; |
35 FOR_EACH_OBSERVER(Observer, observers_, PairingStageChanged(new_stage)); | 38 FOR_EACH_OBSERVER(Observer, observers_, PairingStageChanged(new_stage)); |
36 } | 39 } |
37 | 40 |
38 void BluetoothHostPairingController::SendHostStatus() { | 41 void BluetoothHostPairingController::SendHostStatus() { |
39 pairing_api::HostStatus host_status; | 42 pairing_api::HostStatus host_status; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 } | 102 } |
100 ChangeStage(STAGE_NONE); | 103 ChangeStage(STAGE_NONE); |
101 } | 104 } |
102 | 105 |
103 void BluetoothHostPairingController::OnGetAdapter( | 106 void BluetoothHostPairingController::OnGetAdapter( |
104 scoped_refptr<device::BluetoothAdapter> adapter) { | 107 scoped_refptr<device::BluetoothAdapter> adapter) { |
105 DCHECK(thread_checker_.CalledOnValidThread()); | 108 DCHECK(thread_checker_.CalledOnValidThread()); |
106 DCHECK(!adapter_.get()); | 109 DCHECK(!adapter_.get()); |
107 adapter_ = adapter; | 110 adapter_ = adapter; |
108 | 111 |
| 112 if (adapter_->IsPresent()) { |
| 113 SetName(); |
| 114 } else { |
| 115 // Set the name once the adapter is present. |
| 116 adapter_->AddObserver(this); |
| 117 } |
| 118 } |
| 119 |
| 120 void BluetoothHostPairingController::SetName() { |
109 // TODO(zork): Make the device name prettier. (http://crbug.com/405774) | 121 // TODO(zork): Make the device name prettier. (http://crbug.com/405774) |
110 device_name_ = base::StringPrintf("%s%s", kDeviceNamePrefix, | 122 device_name_ = base::StringPrintf("%s%s", kDeviceNamePrefix, |
111 adapter_->GetAddress().c_str()); | 123 adapter_->GetAddress().c_str()); |
| 124 |
112 adapter_->SetName( | 125 adapter_->SetName( |
113 device_name_, | 126 device_name_, |
114 base::Bind(&BluetoothHostPairingController::OnSetName, | 127 base::Bind(&BluetoothHostPairingController::OnSetName, |
115 ptr_factory_.GetWeakPtr()), | 128 ptr_factory_.GetWeakPtr()), |
116 base::Bind(&BluetoothHostPairingController::OnSetError, | 129 base::Bind(&BluetoothHostPairingController::OnSetError, |
117 ptr_factory_.GetWeakPtr())); | 130 ptr_factory_.GetWeakPtr())); |
118 } | 131 } |
119 | 132 |
120 void BluetoothHostPairingController::OnSetName() { | 133 void BluetoothHostPairingController::OnSetName() { |
121 DCHECK(thread_checker_.CalledOnValidThread()); | 134 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 304 |
292 // TODO(zork): Handle adding another controller. (http://crbug.com/405757) | 305 // TODO(zork): Handle adding another controller. (http://crbug.com/405757) |
293 ChangeStage(STAGE_FINISHED); | 306 ChangeStage(STAGE_FINISHED); |
294 } | 307 } |
295 | 308 |
296 void BluetoothHostPairingController::OnErrorMessage( | 309 void BluetoothHostPairingController::OnErrorMessage( |
297 const pairing_api::Error& message) { | 310 const pairing_api::Error& message) { |
298 NOTREACHED(); | 311 NOTREACHED(); |
299 } | 312 } |
300 | 313 |
| 314 void BluetoothHostPairingController::AdapterPresentChanged( |
| 315 device::BluetoothAdapter* adapter, |
| 316 bool present) { |
| 317 DCHECK_EQ(adapter, adapter_.get()); |
| 318 if (present) { |
| 319 adapter_->RemoveObserver(this); |
| 320 SetName(); |
| 321 } |
| 322 } |
| 323 |
301 void BluetoothHostPairingController::AddObserver(Observer* observer) { | 324 void BluetoothHostPairingController::AddObserver(Observer* observer) { |
302 observers_.AddObserver(observer); | 325 observers_.AddObserver(observer); |
303 } | 326 } |
304 | 327 |
305 void BluetoothHostPairingController::RemoveObserver(Observer* observer) { | 328 void BluetoothHostPairingController::RemoveObserver(Observer* observer) { |
306 observers_.RemoveObserver(observer); | 329 observers_.RemoveObserver(observer); |
307 } | 330 } |
308 | 331 |
309 HostPairingController::Stage BluetoothHostPairingController::GetCurrentStage() { | 332 HostPairingController::Stage BluetoothHostPairingController::GetCurrentStage() { |
310 return current_stage_; | 333 return current_stage_; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); | 405 ChangeStage(STAGE_WAITING_FOR_CODE_CONFIRMATION); |
383 } | 406 } |
384 | 407 |
385 void BluetoothHostPairingController::AuthorizePairing( | 408 void BluetoothHostPairingController::AuthorizePairing( |
386 device::BluetoothDevice* device) { | 409 device::BluetoothDevice* device) { |
387 // Disallow unknown device. | 410 // Disallow unknown device. |
388 device->RejectPairing(); | 411 device->RejectPairing(); |
389 } | 412 } |
390 | 413 |
391 } // namespace pairing_chromeos | 414 } // namespace pairing_chromeos |
OLD | NEW |