Update mojo sdk to rev d459e688a608f6eda850a23bb5e308a76ea51a47
[chromium-blink-merge.git] / components / proximity_auth / bluetooth_connection_finder.cc
blob3fe6567c174a0e76dc338ba8c5da75c20d4e7808
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"
7 #include "base/bind.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),
23 uuid_(uuid),
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.";
37 return;
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() {
64 if (!IsReadyToPoll())
65 return;
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_)
71 return;
73 // If the |connection_| exists, wait for it to connect or fail prior to
74 // polling again.
75 if (connection_)
76 return;
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
86 // permitted.
87 has_delayed_poll_scheduled_ = false;
88 PollIfReady();
91 void BluetoothConnectionFinder::UnregisterAsObserver() {
92 if (connection_) {
93 connection_->RemoveObserver(this);
94 // The connection is about to be released or destroyed, so no need to clear
95 // it explicitly here.
98 if (adapter_.get()) {
99 adapter_->RemoveObserver(this);
100 adapter_ = NULL;
104 void BluetoothConnectionFinder::OnAdapterInitialized(
105 scoped_refptr<BluetoothAdapter> adapter) {
106 adapter_ = adapter;
107 adapter_->AddObserver(this);
108 PollIfReady();
111 void BluetoothConnectionFinder::AdapterPresentChanged(BluetoothAdapter* adapter,
112 bool present) {
113 PollIfReady();
116 void BluetoothConnectionFinder::AdapterPoweredChanged(BluetoothAdapter* adapter,
117 bool powered) {
118 PollIfReady();
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.";
135 connection_.reset();
136 has_delayed_poll_scheduled_ = true;
137 base::MessageLoopProxy::current()->PostDelayedTask(
138 FROM_HERE,
139 base::Bind(&BluetoothConnectionFinder::DelayedPollIfReady,
140 weak_ptr_factory_.GetWeakPtr()),
141 polling_interval_);
145 } // namespace proximity_auth