Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(410)

Side by Side Diff: components/proximity_auth/bluetooth_connection_finder.cc

Issue 633253002: [Easy Unlock] Port the BluetoothConnectionFinder class to native code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "components/proximity_auth/bluetooth_connection_finder.h"
6
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"
13
14 using device::BluetoothAdapter;
15
16 namespace proximity_auth {
17
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) {
27 }
28
29 BluetoothConnectionFinder::~BluetoothConnectionFinder() {
30 UnregisterAsObserver();
31 }
32
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;
38 }
39
40 DCHECK(start_time_.is_null());
41 VLOG(1) << "[BCF] Finding Bluetooth connection...";
42
43 start_time_ = base::TimeTicks::Now();
44 connection_callback_ = connection_callback;
45
46 // TODO(isherman): Register as an observer of screen lock changes.
47
48 device::BluetoothAdapterFactory::GetAdapter(
49 base::Bind(&BluetoothConnectionFinder::OnAdapterInitialized,
50 weak_ptr_factory_.GetWeakPtr()));
51 }
52
53 scoped_ptr<Connection> BluetoothConnectionFinder::CreateConnection() {
54 return scoped_ptr<Connection>(new BluetoothConnection(remote_device_, uuid_));
55 }
56
57 bool BluetoothConnectionFinder::IsReadyToPoll() {
58 bool is_adapter_available =
59 adapter_.get() && adapter_->IsPresent() && adapter_->IsPowered();
60 // TODO(isherman): Determine the screen locked state for really reals.
61 bool is_screen_locked = true;
62 VLOG(1) << "[BCF] Readiness: adapter="
63 << (is_adapter_available ? "available" : "unavailable")
64 << ", screen=" << (is_screen_locked ? "locked" : "unlocked");
65 return is_adapter_available && is_screen_locked;
66 }
67
68 void BluetoothConnectionFinder::PollIfReady() {
69 if (!IsReadyToPoll())
70 return;
71
72 // If there is a pending task to poll at a later time, the time requisite
73 // timeout has not yet elapsed since the previous polling attempt. In that
74 // case, keep waiting until the delayed task comes in.
75 if (has_delayed_poll_scheduled_)
76 return;
77
78 // If the |connection_| exists, wait for it to connect or fail prior to
79 // polling again.
80 if (connection_)
81 return;
82
83 VLOG(1) << "[BCF] Polling for connection...";
84 connection_ = CreateConnection();
85 connection_->AddObserver(this);
86 connection_->Connect();
87 }
88
89 void BluetoothConnectionFinder::DelayedPollIfReady() {
90 // Note that there is no longer a pending task, and therefore polling is
91 // permitted.
92 has_delayed_poll_scheduled_ = false;
93 PollIfReady();
94 }
95
96 void BluetoothConnectionFinder::UnregisterAsObserver() {
97 if (connection_) {
98 connection_->RemoveObserver(this);
99 // The connection is about to be released or destroyed, so no need to clear
100 // it explicitly here.
101 }
102
103 if (adapter_.get()) {
104 adapter_->RemoveObserver(this);
105 adapter_ = NULL;
106 }
107
108 // TODO(isherman): Unregister as screen lock observer as well.
109 }
110
111 void BluetoothConnectionFinder::OnAdapterInitialized(
112 scoped_refptr<BluetoothAdapter> adapter) {
113 adapter_ = adapter;
114 adapter_->AddObserver(this);
115 PollIfReady();
116 }
117
118 void BluetoothConnectionFinder::AdapterPresentChanged(BluetoothAdapter* adapter,
119 bool present) {
120 PollIfReady();
121 }
122
123 void BluetoothConnectionFinder::AdapterPoweredChanged(BluetoothAdapter* adapter,
124 bool powered) {
125 PollIfReady();
126 }
127
128 // TODO(isherman): Wire this up.
129 void BluetoothConnectionFinder::OnScreenLockStateChanged() {
130 PollIfReady();
131 }
132
133 void BluetoothConnectionFinder::OnConnectionStatusChanged(
134 const Connection& connection,
135 Connection::Status old_status,
136 Connection::Status new_status) {
137 DCHECK_EQ(&connection, connection_.get());
138
139 if (connection_->IsConnected()) {
140 base::TimeDelta elapsed = base::TimeTicks::Now() - start_time_;
141 VLOG(1) << "[BCF] Connection found! Elapsed Time: "
142 << elapsed.InMilliseconds() << "ms.";
143 UnregisterAsObserver();
144 connection_callback_.Run(connection_.Pass());
145 } else if (old_status == Connection::IN_PROGRESS) {
146 VLOG(1) << "[BCF] Connection failed! Scheduling another polling iteration.";
147 connection_.reset();
148 has_delayed_poll_scheduled_ = true;
149 base::MessageLoopProxy::current()->PostDelayedTask(
150 FROM_HERE,
151 base::Bind(&BluetoothConnectionFinder::DelayedPollIfReady,
152 weak_ptr_factory_.GetWeakPtr()),
153 polling_interval_);
154 }
155 }
156
157 } // namespace proximity_auth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698