Chromium Code Reviews| 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_throttler.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/message_loop/message_loop_proxy.h" | |
| 13 #include "base/task_runner.h" | |
| 14 #include "components/proximity_auth/bluetooth_connection_finder.h" | |
| 15 #include "components/proximity_auth/connection.h" | |
| 16 | |
| 17 namespace proximity_auth { | |
| 18 namespace { | |
| 19 | |
| 20 // Time to wait after disconnect before reconnecting. | |
| 21 const int kCooldownTimeSecs = 7; | |
| 22 | |
| 23 // Duration of idle interval between iterations of polling for a connection. | |
| 24 const int kConnectionPollingIntervalSecs = 3; | |
| 25 | |
| 26 } // namespace | |
| 27 | |
| 28 BluetoothThrottler::BluetoothThrottler(const RemoteDevice& remote_device) | |
| 29 : remote_device_(remote_device), | |
| 30 connection_(nullptr), | |
| 31 weak_ptr_factory_(this) { | |
| 32 } | |
| 33 | |
| 34 BluetoothThrottler::~BluetoothThrottler() { | |
| 35 if (connection_) | |
| 36 connection_->RemoveObserver(this); | |
| 37 } | |
| 38 | |
| 39 void BluetoothThrottler::FindConnection( | |
| 40 const device::BluetoothUUID& uuid, | |
| 41 const ConnectionFinder::ConnectionCallback& connection_callback) { | |
| 42 DCHECK(!connection_finder_); | |
| 43 | |
| 44 base::TimeTicks now = GetTickClock()->NowTicks(); | |
| 45 base::TimeTicks throttled_start_time = | |
| 46 last_disconnect_time_ + GetCooldownTimeDelta(); | |
| 47 if (!last_disconnect_time_.is_null() && now < throttled_start_time) { | |
| 48 base::TimeDelta delay = throttled_start_time - now; | |
| 49 GetTaskRunner()->PostDelayedTask( | |
| 50 FROM_HERE, | |
| 51 base::Bind(&BluetoothThrottler::FindConnection, | |
| 52 weak_ptr_factory_.GetWeakPtr(), | |
| 53 uuid, | |
| 54 connection_callback), | |
| 55 delay); | |
| 56 return; | |
| 57 } | |
| 58 | |
| 59 connection_finder_ = CreateConnectionFinder(uuid); | |
| 60 connection_finder_->Find(base::Bind(&BluetoothThrottler::OnConnection, | |
| 61 weak_ptr_factory_.GetWeakPtr(), | |
| 62 connection_callback)); | |
| 63 } | |
| 64 | |
| 65 void BluetoothThrottler::StopFindingConnections() { | |
| 66 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 67 connection_finder_.reset(); | |
| 68 } | |
| 69 | |
| 70 scoped_ptr<ConnectionFinder> BluetoothThrottler::CreateConnectionFinder( | |
| 71 const device::BluetoothUUID& uuid) { | |
| 72 base::TimeDelta polling_interval = | |
| 73 base::TimeDelta::FromSeconds(kConnectionPollingIntervalSecs); | |
| 74 return make_scoped_ptr( | |
| 75 new BluetoothConnectionFinder(remote_device_, uuid, polling_interval)); | |
| 76 } | |
| 77 | |
| 78 base::TimeDelta BluetoothThrottler::GetCooldownTimeDelta() const { | |
| 79 return base::TimeDelta::FromSeconds(kCooldownTimeSecs); | |
| 80 } | |
| 81 | |
| 82 scoped_refptr<base::TaskRunner> BluetoothThrottler::GetTaskRunner() const { | |
| 83 return base::MessageLoopProxy::current(); | |
| 84 } | |
| 85 | |
| 86 base::TickClock* BluetoothThrottler::GetTickClock() { | |
| 87 return &clock_; | |
| 88 } | |
| 89 | |
| 90 void BluetoothThrottler::OnConnection( | |
| 91 const ConnectionFinder::ConnectionCallback& connection_callback, | |
| 92 scoped_ptr<Connection> connection) { | |
| 93 DCHECK(!connection_); | |
| 94 connection_ = connection.get(); | |
| 95 connection_->AddObserver(this); | |
| 96 | |
| 97 connection_finder_.reset(); | |
| 98 connection_callback.Run(connection.Pass()); | |
| 99 } | |
| 100 | |
| 101 void BluetoothThrottler::OnConnectionStatusChanged( | |
|
Tim Song
2014/11/20 23:00:09
We should probably add the observer to the bluetoo
Ilya Sherman
2015/04/02 22:59:35
I'm not sure what you meant by this comment. I re
Tim Song
2015/04/03 15:55:02
It's possible for the Chromebook to make a connect
Ilya Sherman
2015/04/17 06:10:03
So, the Bluetooth kernel bug is a race condition a
Tim Song
2015/04/17 22:34:31
Are you certain the bug is with respect to the ser
Ilya Sherman
2015/04/22 00:48:59
Well, AFAICT from the bug, the issue is that socke
| |
| 102 const Connection& connection, | |
| 103 Connection::Status old_status, | |
| 104 Connection::Status new_status) { | |
| 105 DCHECK_EQ(&connection, connection_); | |
| 106 if (old_status == Connection::CONNECTED && | |
| 107 new_status == Connection::DISCONNECTED) { | |
| 108 last_disconnect_time_ = GetTickClock()->NowTicks(); | |
| 109 connection_->RemoveObserver(this); | |
| 110 connection_ = nullptr; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 } // namespace proximity_auth | |
| OLD | NEW |