Try to work around that clang/win bug in another file.
[chromium-blink-merge.git] / components / proximity_auth / bluetooth_connection_finder.cc
blob3ab43a4ad6ce34430fe72109cb8a1b39e09180ed
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 "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),
24 uuid_(uuid),
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.";
38 return;
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() {
65 if (!IsReadyToPoll())
66 return;
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_)
72 return;
74 // If the |connection_| exists, wait for it to connect or fail prior to
75 // polling again.
76 if (connection_)
77 return;
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
87 // permitted.
88 has_delayed_poll_scheduled_ = false;
89 PollIfReady();
92 void BluetoothConnectionFinder::UnregisterAsObserver() {
93 if (connection_) {
94 connection_->RemoveObserver(this);
95 // The connection is about to be released or destroyed, so no need to clear
96 // it explicitly here.
99 if (adapter_.get()) {
100 adapter_->RemoveObserver(this);
101 adapter_ = NULL;
105 void BluetoothConnectionFinder::OnAdapterInitialized(
106 scoped_refptr<BluetoothAdapter> adapter) {
107 adapter_ = adapter;
108 adapter_->AddObserver(this);
109 PollIfReady();
112 void BluetoothConnectionFinder::AdapterPresentChanged(BluetoothAdapter* adapter,
113 bool present) {
114 PollIfReady();
117 void BluetoothConnectionFinder::AdapterPoweredChanged(BluetoothAdapter* adapter,
118 bool powered) {
119 PollIfReady();
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.";
136 connection_.reset();
137 has_delayed_poll_scheduled_ = true;
138 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
139 FROM_HERE, base::Bind(&BluetoothConnectionFinder::DelayedPollIfReady,
140 weak_ptr_factory_.GetWeakPtr()),
141 polling_interval_);
145 } // namespace proximity_auth