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
3 // found in the LICENSE file.
5 #include "components/proximity_auth/bluetooth_connection_finder.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "components/proximity_auth/bluetooth_connection.h"
13 #include "device/bluetooth/bluetooth_adapter_factory.h"
15 using device::BluetoothAdapter
;
17 namespace proximity_auth
{
19 BluetoothConnectionFinder::BluetoothConnectionFinder(
20 const RemoteDevice
& remote_device
,
21 const device::BluetoothUUID
& uuid
,
22 const base::TimeDelta
& polling_interval
)
23 : remote_device_(remote_device
),
25 polling_interval_(polling_interval
),
26 has_delayed_poll_scheduled_(false),
27 weak_ptr_factory_(this) {
30 BluetoothConnectionFinder::~BluetoothConnectionFinder() {
31 UnregisterAsObserver();
34 void BluetoothConnectionFinder::Find(
35 const ConnectionCallback
& connection_callback
) {
36 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
37 VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting.";
41 DCHECK(start_time_
.is_null());
42 VLOG(1) << "[BCF] Finding Bluetooth connection...";
44 start_time_
= base::TimeTicks::Now();
45 connection_callback_
= connection_callback
;
47 device::BluetoothAdapterFactory::GetAdapter(
48 base::Bind(&BluetoothConnectionFinder::OnAdapterInitialized
,
49 weak_ptr_factory_
.GetWeakPtr()));
52 scoped_ptr
<Connection
> BluetoothConnectionFinder::CreateConnection() {
53 return scoped_ptr
<Connection
>(new BluetoothConnection(remote_device_
, uuid_
));
56 bool BluetoothConnectionFinder::IsReadyToPoll() {
57 bool is_adapter_available
=
58 adapter_
.get() && adapter_
->IsPresent() && adapter_
->IsPowered();
59 VLOG(1) << "[BCF] Readiness: adapter="
60 << (is_adapter_available
? "available" : "unavailable");
61 return is_adapter_available
;
64 void BluetoothConnectionFinder::PollIfReady() {
68 // 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 // case, keep waiting until the delayed task comes in.
71 if (has_delayed_poll_scheduled_
)
74 // If the |connection_| exists, wait for it to connect or fail prior to
79 VLOG(1) << "[BCF] Polling for connection...";
80 connection_
= CreateConnection();
81 connection_
->AddObserver(this);
82 connection_
->Connect();
85 void BluetoothConnectionFinder::DelayedPollIfReady() {
86 // Note that there is no longer a pending task, and therefore polling is
88 has_delayed_poll_scheduled_
= false;
92 void BluetoothConnectionFinder::UnregisterAsObserver() {
94 connection_
->RemoveObserver(this);
95 // The connection is about to be released or destroyed, so no need to clear
96 // it explicitly here.
100 adapter_
->RemoveObserver(this);
105 void BluetoothConnectionFinder::OnAdapterInitialized(
106 scoped_refptr
<BluetoothAdapter
> adapter
) {
108 adapter_
->AddObserver(this);
112 void BluetoothConnectionFinder::AdapterPresentChanged(BluetoothAdapter
* adapter
,
117 void BluetoothConnectionFinder::AdapterPoweredChanged(BluetoothAdapter
* adapter
,
122 void BluetoothConnectionFinder::OnConnectionStatusChanged(
123 Connection
* connection
,
124 Connection::Status old_status
,
125 Connection::Status new_status
) {
126 DCHECK_EQ(connection
, connection_
.get());
128 if (connection_
->IsConnected()) {
129 base::TimeDelta elapsed
= base::TimeTicks::Now() - start_time_
;
130 VLOG(1) << "[BCF] Connection found! Elapsed Time: "
131 << elapsed
.InMilliseconds() << "ms.";
132 UnregisterAsObserver();
133 connection_callback_
.Run(connection_
.Pass());
134 } else if (old_status
== Connection::IN_PROGRESS
) {
135 VLOG(1) << "[BCF] Connection failed! Scheduling another polling iteration.";
137 has_delayed_poll_scheduled_
= true;
138 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
139 FROM_HERE
, base::Bind(&BluetoothConnectionFinder::DelayedPollIfReady
,
140 weak_ptr_factory_
.GetWeakPtr()),
145 } // namespace proximity_auth