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/proximity_auth/bluetooth_connection_finder.h" | 5 #include "components/proximity_auth/bluetooth_connection_finder.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
12 #include "components/proximity_auth/bluetooth_connection.h" | 12 #include "components/proximity_auth/bluetooth_connection.h" |
| 13 #include "components/proximity_auth/logging/logging.h" |
13 #include "device/bluetooth/bluetooth_adapter_factory.h" | 14 #include "device/bluetooth/bluetooth_adapter_factory.h" |
14 | 15 |
15 using device::BluetoothAdapter; | 16 using device::BluetoothAdapter; |
16 | 17 |
17 namespace proximity_auth { | 18 namespace proximity_auth { |
18 | 19 |
19 BluetoothConnectionFinder::BluetoothConnectionFinder( | 20 BluetoothConnectionFinder::BluetoothConnectionFinder( |
20 const RemoteDevice& remote_device, | 21 const RemoteDevice& remote_device, |
21 const device::BluetoothUUID& uuid, | 22 const device::BluetoothUUID& uuid, |
22 const base::TimeDelta& polling_interval) | 23 const base::TimeDelta& polling_interval) |
23 : remote_device_(remote_device), | 24 : remote_device_(remote_device), |
24 uuid_(uuid), | 25 uuid_(uuid), |
25 polling_interval_(polling_interval), | 26 polling_interval_(polling_interval), |
26 has_delayed_poll_scheduled_(false), | 27 has_delayed_poll_scheduled_(false), |
27 weak_ptr_factory_(this) { | 28 weak_ptr_factory_(this) { |
28 } | 29 } |
29 | 30 |
30 BluetoothConnectionFinder::~BluetoothConnectionFinder() { | 31 BluetoothConnectionFinder::~BluetoothConnectionFinder() { |
31 UnregisterAsObserver(); | 32 UnregisterAsObserver(); |
32 } | 33 } |
33 | 34 |
34 void BluetoothConnectionFinder::Find( | 35 void BluetoothConnectionFinder::Find( |
35 const ConnectionCallback& connection_callback) { | 36 const ConnectionCallback& connection_callback) { |
36 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 37 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
37 VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting."; | 38 PA_LOG(WARNING) << "Bluetooth is unsupported on this platform. Aborting."; |
38 return; | 39 return; |
39 } | 40 } |
40 | 41 |
41 DCHECK(start_time_.is_null()); | 42 DCHECK(start_time_.is_null()); |
42 VLOG(1) << "[BCF] Finding Bluetooth connection..."; | 43 PA_LOG(WARNING) << "Finding Bluetooth connection..."; |
43 | 44 |
44 start_time_ = base::TimeTicks::Now(); | 45 start_time_ = base::TimeTicks::Now(); |
45 connection_callback_ = connection_callback; | 46 connection_callback_ = connection_callback; |
46 | 47 |
47 device::BluetoothAdapterFactory::GetAdapter( | 48 device::BluetoothAdapterFactory::GetAdapter( |
48 base::Bind(&BluetoothConnectionFinder::OnAdapterInitialized, | 49 base::Bind(&BluetoothConnectionFinder::OnAdapterInitialized, |
49 weak_ptr_factory_.GetWeakPtr())); | 50 weak_ptr_factory_.GetWeakPtr())); |
50 } | 51 } |
51 | 52 |
52 scoped_ptr<Connection> BluetoothConnectionFinder::CreateConnection() { | 53 scoped_ptr<Connection> BluetoothConnectionFinder::CreateConnection() { |
53 return scoped_ptr<Connection>(new BluetoothConnection(remote_device_, uuid_)); | 54 return scoped_ptr<Connection>(new BluetoothConnection(remote_device_, uuid_)); |
54 } | 55 } |
55 | 56 |
56 bool BluetoothConnectionFinder::IsReadyToPoll() { | 57 bool BluetoothConnectionFinder::IsReadyToPoll() { |
57 bool is_adapter_available = | 58 bool is_adapter_available = |
58 adapter_.get() && adapter_->IsPresent() && adapter_->IsPowered(); | 59 adapter_.get() && adapter_->IsPresent() && adapter_->IsPowered(); |
59 VLOG(1) << "[BCF] Readiness: adapter=" | 60 PA_LOG(INFO) << "Readiness: adapter=" |
60 << (is_adapter_available ? "available" : "unavailable"); | 61 << (is_adapter_available ? "available" : "unavailable"); |
61 return is_adapter_available; | 62 return is_adapter_available; |
62 } | 63 } |
63 | 64 |
64 void BluetoothConnectionFinder::PollIfReady() { | 65 void BluetoothConnectionFinder::PollIfReady() { |
65 if (!IsReadyToPoll()) | 66 if (!IsReadyToPoll()) |
66 return; | 67 return; |
67 | 68 |
68 // If there is a pending task to poll at a later time, the time requisite | 69 // If there is a pending task to poll at a later time, the time requisite |
69 // timeout has not yet elapsed since the previous polling attempt. In that | 70 // timeout has not yet elapsed since the previous polling attempt. In that |
70 // case, keep waiting until the delayed task comes in. | 71 // case, keep waiting until the delayed task comes in. |
71 if (has_delayed_poll_scheduled_) | 72 if (has_delayed_poll_scheduled_) |
72 return; | 73 return; |
73 | 74 |
74 // If the |connection_| exists, wait for it to connect or fail prior to | 75 // If the |connection_| exists, wait for it to connect or fail prior to |
75 // polling again. | 76 // polling again. |
76 if (connection_) | 77 if (connection_) |
77 return; | 78 return; |
78 | 79 |
79 VLOG(1) << "[BCF] Polling for connection..."; | 80 PA_LOG(INFO) << "Polling for connection..."; |
80 connection_ = CreateConnection(); | 81 connection_ = CreateConnection(); |
81 connection_->AddObserver(this); | 82 connection_->AddObserver(this); |
82 connection_->Connect(); | 83 connection_->Connect(); |
83 } | 84 } |
84 | 85 |
85 void BluetoothConnectionFinder::DelayedPollIfReady() { | 86 void BluetoothConnectionFinder::DelayedPollIfReady() { |
86 // Note that there is no longer a pending task, and therefore polling is | 87 // Note that there is no longer a pending task, and therefore polling is |
87 // permitted. | 88 // permitted. |
88 has_delayed_poll_scheduled_ = false; | 89 has_delayed_poll_scheduled_ = false; |
89 PollIfReady(); | 90 PollIfReady(); |
(...skipping 30 matching lines...) Expand all Loading... |
120 } | 121 } |
121 | 122 |
122 void BluetoothConnectionFinder::OnConnectionStatusChanged( | 123 void BluetoothConnectionFinder::OnConnectionStatusChanged( |
123 Connection* connection, | 124 Connection* connection, |
124 Connection::Status old_status, | 125 Connection::Status old_status, |
125 Connection::Status new_status) { | 126 Connection::Status new_status) { |
126 DCHECK_EQ(connection, connection_.get()); | 127 DCHECK_EQ(connection, connection_.get()); |
127 | 128 |
128 if (connection_->IsConnected()) { | 129 if (connection_->IsConnected()) { |
129 base::TimeDelta elapsed = base::TimeTicks::Now() - start_time_; | 130 base::TimeDelta elapsed = base::TimeTicks::Now() - start_time_; |
130 VLOG(1) << "[BCF] Connection found! Elapsed Time: " | 131 PA_LOG(WARNING) << "Connection found! Elapsed Time: " |
131 << elapsed.InMilliseconds() << "ms."; | 132 << elapsed.InMilliseconds() << "ms."; |
132 UnregisterAsObserver(); | 133 UnregisterAsObserver(); |
133 connection_callback_.Run(connection_.Pass()); | 134 connection_callback_.Run(connection_.Pass()); |
134 } else if (old_status == Connection::IN_PROGRESS) { | 135 } else if (old_status == Connection::IN_PROGRESS) { |
135 VLOG(1) << "[BCF] Connection failed! Scheduling another polling iteration."; | 136 PA_LOG(WARNING) |
| 137 << "Connection failed! Scheduling another polling iteration."; |
136 connection_.reset(); | 138 connection_.reset(); |
137 has_delayed_poll_scheduled_ = true; | 139 has_delayed_poll_scheduled_ = true; |
138 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 140 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
139 FROM_HERE, base::Bind(&BluetoothConnectionFinder::DelayedPollIfReady, | 141 FROM_HERE, base::Bind(&BluetoothConnectionFinder::DelayedPollIfReady, |
140 weak_ptr_factory_.GetWeakPtr()), | 142 weak_ptr_factory_.GetWeakPtr()), |
141 polling_interval_); | 143 polling_interval_); |
142 } | 144 } |
143 } | 145 } |
144 | 146 |
145 } // namespace proximity_auth | 147 } // namespace proximity_auth |
OLD | NEW |