Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / components / proximity_auth / bluetooth_connection_finder.cc
blob7596a11c04f49bd9aa4d9c1df65a1ead3badd37d
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/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "components/proximity_auth/bluetooth_connection.h"
13 #include "components/proximity_auth/logging/logging.h"
14 #include "device/bluetooth/bluetooth_adapter_factory.h"
16 using device::BluetoothAdapter;
18 namespace proximity_auth {
20 BluetoothConnectionFinder::BluetoothConnectionFinder(
21 const RemoteDevice& remote_device,
22 const device::BluetoothUUID& uuid,
23 const base::TimeDelta& polling_interval)
24 : remote_device_(remote_device),
25 uuid_(uuid),
26 polling_interval_(polling_interval),
27 has_delayed_poll_scheduled_(false),
28 weak_ptr_factory_(this) {
31 BluetoothConnectionFinder::~BluetoothConnectionFinder() {
32 UnregisterAsObserver();
35 void BluetoothConnectionFinder::Find(
36 const ConnectionCallback& connection_callback) {
37 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
38 PA_LOG(WARNING) << "Bluetooth is unsupported on this platform. Aborting.";
39 return;
42 DCHECK(start_time_.is_null());
43 PA_LOG(WARNING) << "Finding Bluetooth connection...";
45 start_time_ = base::TimeTicks::Now();
46 connection_callback_ = connection_callback;
48 device::BluetoothAdapterFactory::GetAdapter(
49 base::Bind(&BluetoothConnectionFinder::OnAdapterInitialized,
50 weak_ptr_factory_.GetWeakPtr()));
53 scoped_ptr<Connection> BluetoothConnectionFinder::CreateConnection() {
54 return scoped_ptr<Connection>(new BluetoothConnection(remote_device_, uuid_));
57 bool BluetoothConnectionFinder::IsReadyToPoll() {
58 bool is_adapter_available =
59 adapter_.get() && adapter_->IsPresent() && adapter_->IsPowered();
60 PA_LOG(INFO) << "Readiness: adapter="
61 << (is_adapter_available ? "available" : "unavailable");
62 return is_adapter_available;
65 void BluetoothConnectionFinder::PollIfReady() {
66 if (!IsReadyToPoll())
67 return;
69 // If there is a pending task to poll at a later time, the time requisite
70 // timeout has not yet elapsed since the previous polling attempt. In that
71 // case, keep waiting until the delayed task comes in.
72 if (has_delayed_poll_scheduled_)
73 return;
75 // If the |connection_| exists, wait for it to connect or fail prior to
76 // polling again.
77 if (connection_)
78 return;
80 PA_LOG(INFO) << "Polling for connection...";
81 connection_ = CreateConnection();
82 connection_->AddObserver(this);
83 connection_->Connect();
86 void BluetoothConnectionFinder::DelayedPollIfReady() {
87 // Note that there is no longer a pending task, and therefore polling is
88 // permitted.
89 has_delayed_poll_scheduled_ = false;
90 PollIfReady();
93 void BluetoothConnectionFinder::UnregisterAsObserver() {
94 if (connection_) {
95 connection_->RemoveObserver(this);
96 // The connection is about to be released or destroyed, so no need to clear
97 // it explicitly here.
100 if (adapter_.get()) {
101 adapter_->RemoveObserver(this);
102 adapter_ = NULL;
106 void BluetoothConnectionFinder::OnAdapterInitialized(
107 scoped_refptr<BluetoothAdapter> adapter) {
108 adapter_ = adapter;
109 adapter_->AddObserver(this);
110 PollIfReady();
113 void BluetoothConnectionFinder::AdapterPresentChanged(BluetoothAdapter* adapter,
114 bool present) {
115 PollIfReady();
118 void BluetoothConnectionFinder::AdapterPoweredChanged(BluetoothAdapter* adapter,
119 bool powered) {
120 PollIfReady();
123 void BluetoothConnectionFinder::OnConnectionStatusChanged(
124 Connection* connection,
125 Connection::Status old_status,
126 Connection::Status new_status) {
127 DCHECK_EQ(connection, connection_.get());
129 if (connection_->IsConnected()) {
130 base::TimeDelta elapsed = base::TimeTicks::Now() - start_time_;
131 PA_LOG(WARNING) << "Connection found! Elapsed Time: "
132 << elapsed.InMilliseconds() << "ms.";
133 UnregisterAsObserver();
134 connection_callback_.Run(connection_.Pass());
135 } else if (old_status == Connection::IN_PROGRESS) {
136 PA_LOG(WARNING)
137 << "Connection failed! Scheduling another polling iteration.";
138 connection_.reset();
139 has_delayed_poll_scheduled_ = true;
140 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
141 FROM_HERE, base::Bind(&BluetoothConnectionFinder::DelayedPollIfReady,
142 weak_ptr_factory_.GetWeakPtr()),
143 polling_interval_);
147 } // namespace proximity_auth