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