OLD | NEW |
(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 device::BluetoothAdapterFactory::GetAdapter( |
| 47 base::Bind(&BluetoothConnectionFinder::OnAdapterInitialized, |
| 48 weak_ptr_factory_.GetWeakPtr())); |
| 49 } |
| 50 |
| 51 scoped_ptr<Connection> BluetoothConnectionFinder::CreateConnection() { |
| 52 return scoped_ptr<Connection>(new BluetoothConnection(remote_device_, uuid_)); |
| 53 } |
| 54 |
| 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; |
| 61 } |
| 62 |
| 63 void BluetoothConnectionFinder::PollIfReady() { |
| 64 if (!IsReadyToPoll()) |
| 65 return; |
| 66 |
| 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; |
| 72 |
| 73 // If the |connection_| exists, wait for it to connect or fail prior to |
| 74 // polling again. |
| 75 if (connection_) |
| 76 return; |
| 77 |
| 78 VLOG(1) << "[BCF] Polling for connection..."; |
| 79 connection_ = CreateConnection(); |
| 80 connection_->AddObserver(this); |
| 81 connection_->Connect(); |
| 82 } |
| 83 |
| 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(); |
| 89 } |
| 90 |
| 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. |
| 96 } |
| 97 |
| 98 if (adapter_.get()) { |
| 99 adapter_->RemoveObserver(this); |
| 100 adapter_ = NULL; |
| 101 } |
| 102 } |
| 103 |
| 104 void BluetoothConnectionFinder::OnAdapterInitialized( |
| 105 scoped_refptr<BluetoothAdapter> adapter) { |
| 106 adapter_ = adapter; |
| 107 adapter_->AddObserver(this); |
| 108 PollIfReady(); |
| 109 } |
| 110 |
| 111 void BluetoothConnectionFinder::AdapterPresentChanged(BluetoothAdapter* adapter, |
| 112 bool present) { |
| 113 PollIfReady(); |
| 114 } |
| 115 |
| 116 void BluetoothConnectionFinder::AdapterPoweredChanged(BluetoothAdapter* adapter, |
| 117 bool powered) { |
| 118 PollIfReady(); |
| 119 } |
| 120 |
| 121 void BluetoothConnectionFinder::OnConnectionStatusChanged( |
| 122 const Connection& connection, |
| 123 Connection::Status old_status, |
| 124 Connection::Status new_status) { |
| 125 DCHECK_EQ(&connection, connection_.get()); |
| 126 |
| 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_); |
| 142 } |
| 143 } |
| 144 |
| 145 } // namespace proximity_auth |
OLD | NEW |