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/message_loop/message_loop_proxy.h"
11 #include "components/proximity_auth/bluetooth_connection.h"
12 #include "device/bluetooth/bluetooth_adapter_factory.h"
14 using device::BluetoothAdapter
;
16 namespace proximity_auth
{
18 BluetoothConnectionFinder::BluetoothConnectionFinder(
19 const RemoteDevice
& remote_device
,
20 const device::BluetoothUUID
& uuid
,
21 const base::TimeDelta
& polling_interval
)
22 : remote_device_(remote_device
),
24 polling_interval_(polling_interval
),
25 has_delayed_poll_scheduled_(false),
26 weak_ptr_factory_(this) {
29 BluetoothConnectionFinder::~BluetoothConnectionFinder() {
30 UnregisterAsObserver();
33 void BluetoothConnectionFinder::Find(
34 const ConnectionCallback
& connection_callback
) {
35 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
36 VLOG(1) << "[BCF] Bluetooth is unsupported on this platform. Aborting.";
40 DCHECK(start_time_
.is_null());
41 VLOG(1) << "[BCF] Finding Bluetooth connection...";
43 start_time_
= base::TimeTicks::Now();
44 connection_callback_
= connection_callback
;
46 device::BluetoothAdapterFactory::GetAdapter(
47 base::Bind(&BluetoothConnectionFinder::OnAdapterInitialized
,
48 weak_ptr_factory_
.GetWeakPtr()));
51 scoped_ptr
<Connection
> BluetoothConnectionFinder::CreateConnection() {
52 return scoped_ptr
<Connection
>(new BluetoothConnection(remote_device_
, uuid_
));
55 bool BluetoothConnectionFinder::IsReadyToPoll() {
56 bool is_adapter_available
=
57 adapter_
.get() && adapter_
->IsPresent() && adapter_
->IsPowered();
58 VLOG(1) << "[BCF] Readiness: adapter="
59 << (is_adapter_available
? "available" : "unavailable");
60 return is_adapter_available
;
63 void BluetoothConnectionFinder::PollIfReady() {
67 // If there is a pending task to poll at a later time, the time requisite
68 // timeout has not yet elapsed since the previous polling attempt. In that
69 // case, keep waiting until the delayed task comes in.
70 if (has_delayed_poll_scheduled_
)
73 // If the |connection_| exists, wait for it to connect or fail prior to
78 VLOG(1) << "[BCF] Polling for connection...";
79 connection_
= CreateConnection();
80 connection_
->AddObserver(this);
81 connection_
->Connect();
84 void BluetoothConnectionFinder::DelayedPollIfReady() {
85 // Note that there is no longer a pending task, and therefore polling is
87 has_delayed_poll_scheduled_
= false;
91 void BluetoothConnectionFinder::UnregisterAsObserver() {
93 connection_
->RemoveObserver(this);
94 // The connection is about to be released or destroyed, so no need to clear
95 // it explicitly here.
99 adapter_
->RemoveObserver(this);
104 void BluetoothConnectionFinder::OnAdapterInitialized(
105 scoped_refptr
<BluetoothAdapter
> adapter
) {
107 adapter_
->AddObserver(this);
111 void BluetoothConnectionFinder::AdapterPresentChanged(BluetoothAdapter
* adapter
,
116 void BluetoothConnectionFinder::AdapterPoweredChanged(BluetoothAdapter
* adapter
,
121 void BluetoothConnectionFinder::OnConnectionStatusChanged(
122 const Connection
& connection
,
123 Connection::Status old_status
,
124 Connection::Status new_status
) {
125 DCHECK_EQ(&connection
, connection_
.get());
127 if (connection_
->IsConnected()) {
128 base::TimeDelta elapsed
= base::TimeTicks::Now() - start_time_
;
129 VLOG(1) << "[BCF] Connection found! Elapsed Time: "
130 << elapsed
.InMilliseconds() << "ms.";
131 UnregisterAsObserver();
132 connection_callback_
.Run(connection_
.Pass());
133 } else if (old_status
== Connection::IN_PROGRESS
) {
134 VLOG(1) << "[BCF] Connection failed! Scheduling another polling iteration.";
136 has_delayed_poll_scheduled_
= true;
137 base::MessageLoopProxy::current()->PostDelayedTask(
139 base::Bind(&BluetoothConnectionFinder::DelayedPollIfReady
,
140 weak_ptr_factory_
.GetWeakPtr()),
145 } // namespace proximity_auth