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 <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "components/cryptauth/connection.h" |
14 #include "components/proximity_auth/bluetooth_connection.h" | 15 #include "components/proximity_auth/bluetooth_connection.h" |
15 #include "components/proximity_auth/logging/logging.h" | 16 #include "components/proximity_auth/logging/logging.h" |
16 #include "device/bluetooth/bluetooth_adapter_factory.h" | 17 #include "device/bluetooth/bluetooth_adapter_factory.h" |
17 | 18 |
18 using device::BluetoothAdapter; | 19 using device::BluetoothAdapter; |
19 | 20 |
20 namespace proximity_auth { | 21 namespace proximity_auth { |
21 | 22 |
22 BluetoothConnectionFinder::BluetoothConnectionFinder( | 23 BluetoothConnectionFinder::BluetoothConnectionFinder( |
23 const cryptauth::RemoteDevice& remote_device, | 24 const cryptauth::RemoteDevice& remote_device, |
24 const device::BluetoothUUID& uuid, | 25 const device::BluetoothUUID& uuid, |
25 const base::TimeDelta& polling_interval) | 26 const base::TimeDelta& polling_interval) |
26 : remote_device_(remote_device), | 27 : remote_device_(remote_device), |
27 uuid_(uuid), | 28 uuid_(uuid), |
28 polling_interval_(polling_interval), | 29 polling_interval_(polling_interval), |
29 has_delayed_poll_scheduled_(false), | 30 has_delayed_poll_scheduled_(false), |
30 weak_ptr_factory_(this) {} | 31 weak_ptr_factory_(this) {} |
31 | 32 |
32 BluetoothConnectionFinder::~BluetoothConnectionFinder() { | 33 BluetoothConnectionFinder::~BluetoothConnectionFinder() { |
33 UnregisterAsObserver(); | 34 UnregisterAsObserver(); |
34 } | 35 } |
35 | 36 |
36 void BluetoothConnectionFinder::Find( | 37 void BluetoothConnectionFinder::Find( |
37 const ConnectionCallback& connection_callback) { | 38 const cryptauth::ConnectionFinder::ConnectionCallback& |
| 39 connection_callback) { |
38 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 40 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
39 PA_LOG(WARNING) << "Bluetooth is unsupported on this platform. Aborting."; | 41 PA_LOG(WARNING) << "Bluetooth is unsupported on this platform. Aborting."; |
40 return; | 42 return; |
41 } | 43 } |
42 | 44 |
43 DCHECK(start_time_.is_null()); | 45 DCHECK(start_time_.is_null()); |
44 | 46 |
45 start_time_ = base::TimeTicks::Now(); | 47 start_time_ = base::TimeTicks::Now(); |
46 connection_callback_ = connection_callback; | 48 connection_callback_ = connection_callback; |
47 | 49 |
48 device::BluetoothAdapterFactory::GetAdapter( | 50 device::BluetoothAdapterFactory::GetAdapter( |
49 base::Bind(&BluetoothConnectionFinder::OnAdapterInitialized, | 51 base::Bind(&BluetoothConnectionFinder::OnAdapterInitialized, |
50 weak_ptr_factory_.GetWeakPtr())); | 52 weak_ptr_factory_.GetWeakPtr())); |
51 } | 53 } |
52 | 54 |
53 std::unique_ptr<Connection> BluetoothConnectionFinder::CreateConnection() { | 55 std::unique_ptr<cryptauth::Connection> |
54 return std::unique_ptr<Connection>( | 56 BluetoothConnectionFinder::CreateConnection() { |
| 57 return std::unique_ptr<cryptauth::Connection>( |
55 new BluetoothConnection(remote_device_, uuid_)); | 58 new BluetoothConnection(remote_device_, uuid_)); |
56 } | 59 } |
57 | 60 |
58 void BluetoothConnectionFinder::SeekDeviceByAddress( | 61 void BluetoothConnectionFinder::SeekDeviceByAddress( |
59 const std::string& bluetooth_address, | 62 const std::string& bluetooth_address, |
60 const base::Closure& callback, | 63 const base::Closure& callback, |
61 const bluetooth_util::ErrorCallback& error_callback) { | 64 const bluetooth_util::ErrorCallback& error_callback) { |
62 bluetooth_util::SeekDeviceByAddress( | 65 bluetooth_util::SeekDeviceByAddress( |
63 bluetooth_address, callback, error_callback, | 66 bluetooth_address, callback, error_callback, |
64 base::ThreadTaskRunnerHandle::Get().get()); | 67 base::ThreadTaskRunnerHandle::Get().get()); |
(...skipping 12 matching lines...) Expand all Loading... |
77 return; | 80 return; |
78 | 81 |
79 // If there is a pending task to poll at a later time, the time requisite | 82 // If there is a pending task to poll at a later time, the time requisite |
80 // timeout has not yet elapsed since the previous polling attempt. In that | 83 // timeout has not yet elapsed since the previous polling attempt. In that |
81 // case, keep waiting until the delayed task comes in. | 84 // case, keep waiting until the delayed task comes in. |
82 if (has_delayed_poll_scheduled_) | 85 if (has_delayed_poll_scheduled_) |
83 return; | 86 return; |
84 | 87 |
85 // If the |connection_| is pending, wait for it to connect or fail prior to | 88 // If the |connection_| is pending, wait for it to connect or fail prior to |
86 // polling again. | 89 // polling again. |
87 if (connection_ && connection_->status() != Connection::DISCONNECTED) | 90 if (connection_ && |
| 91 connection_->status() != cryptauth::Connection::DISCONNECTED) |
88 return; | 92 return; |
89 | 93 |
90 // This SeekDeviceByAddress operation is needed to connect to a device if | 94 // This SeekDeviceByAddress operation is needed to connect to a device if |
91 // it is not already known to the adapter. | 95 // it is not already known to the adapter. |
92 if (!adapter_->GetDevice(remote_device_.bluetooth_address)) { | 96 if (!adapter_->GetDevice(remote_device_.bluetooth_address)) { |
93 PA_LOG(INFO) << "Remote device [" << remote_device_.bluetooth_address | 97 PA_LOG(INFO) << "Remote device [" << remote_device_.bluetooth_address |
94 << "] is not known. " | 98 << "] is not known. " |
95 << "Seeking device directly by address..."; | 99 << "Seeking device directly by address..."; |
96 | 100 |
97 SeekDeviceByAddress( | 101 SeekDeviceByAddress( |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 bool present) { | 171 bool present) { |
168 PollIfReady(); | 172 PollIfReady(); |
169 } | 173 } |
170 | 174 |
171 void BluetoothConnectionFinder::AdapterPoweredChanged(BluetoothAdapter* adapter, | 175 void BluetoothConnectionFinder::AdapterPoweredChanged(BluetoothAdapter* adapter, |
172 bool powered) { | 176 bool powered) { |
173 PollIfReady(); | 177 PollIfReady(); |
174 } | 178 } |
175 | 179 |
176 void BluetoothConnectionFinder::OnConnectionStatusChanged( | 180 void BluetoothConnectionFinder::OnConnectionStatusChanged( |
177 Connection* connection, | 181 cryptauth::Connection* connection, |
178 Connection::Status old_status, | 182 cryptauth::Connection::Status old_status, |
179 Connection::Status new_status) { | 183 cryptauth::Connection::Status new_status) { |
180 DCHECK_EQ(connection, connection_.get()); | 184 DCHECK_EQ(connection, connection_.get()); |
181 | 185 |
182 if (connection_->IsConnected()) { | 186 if (connection_->IsConnected()) { |
183 base::TimeDelta elapsed = base::TimeTicks::Now() - start_time_; | 187 base::TimeDelta elapsed = base::TimeTicks::Now() - start_time_; |
184 PA_LOG(WARNING) << "Connection found! Elapsed Time: " | 188 PA_LOG(WARNING) << "Connection found! Elapsed Time: " |
185 << elapsed.InMilliseconds() << "ms."; | 189 << elapsed.InMilliseconds() << "ms."; |
186 UnregisterAsObserver(); | 190 UnregisterAsObserver(); |
187 | 191 |
188 // If we invoke the callback now, the callback function may install its own | 192 // If we invoke the callback now, the callback function may install its own |
189 // observer to |connection_|. Because we are in the ConnectionObserver | 193 // observer to |connection_|. Because we are in the |
| 194 // cryptauth::ConnectionObserver |
190 // callstack, this new observer will receive this connection event. | 195 // callstack, this new observer will receive this connection event. |
191 // Therefore, we need to invoke the callback asynchronously. | 196 // Therefore, we need to invoke the callback asynchronously. |
192 base::ThreadTaskRunnerHandle::Get()->PostTask( | 197 base::ThreadTaskRunnerHandle::Get()->PostTask( |
193 FROM_HERE, base::Bind(&BluetoothConnectionFinder::InvokeCallbackAsync, | 198 FROM_HERE, base::Bind(&BluetoothConnectionFinder::InvokeCallbackAsync, |
194 weak_ptr_factory_.GetWeakPtr())); | 199 weak_ptr_factory_.GetWeakPtr())); |
195 } else if (old_status == Connection::IN_PROGRESS) { | 200 } else if (old_status == cryptauth::Connection::IN_PROGRESS) { |
196 PA_LOG(WARNING) | 201 PA_LOG(WARNING) |
197 << "Connection failed! Scheduling another polling iteration."; | 202 << "Connection failed! Scheduling another polling iteration."; |
198 PostDelayedPoll(); | 203 PostDelayedPoll(); |
199 } | 204 } |
200 } | 205 } |
201 | 206 |
202 void BluetoothConnectionFinder::InvokeCallbackAsync() { | 207 void BluetoothConnectionFinder::InvokeCallbackAsync() { |
203 connection_callback_.Run(std::move(connection_)); | 208 connection_callback_.Run(std::move(connection_)); |
204 } | 209 } |
205 | 210 |
206 } // namespace proximity_auth | 211 } // namespace proximity_auth |
OLD | NEW |