Index: components/proximity_auth/bluetooth_throttler.cc |
diff --git a/components/proximity_auth/bluetooth_throttler.cc b/components/proximity_auth/bluetooth_throttler.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b101071435f19b450491a961487e3659f31b4016 |
--- /dev/null |
+++ b/components/proximity_auth/bluetooth_throttler.cc |
@@ -0,0 +1,114 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/proximity_auth/bluetooth_throttler.h" |
+ |
+#include <algorithm> |
+ |
+#include "base/bind.h" |
+#include "base/location.h" |
+#include "base/logging.h" |
+#include "base/message_loop/message_loop_proxy.h" |
+#include "base/task_runner.h" |
+#include "components/proximity_auth/bluetooth_connection_finder.h" |
+#include "components/proximity_auth/connection.h" |
+ |
+namespace proximity_auth { |
+namespace { |
+ |
+// Time to wait after disconnect before reconnecting. |
+const int kCooldownTimeSecs = 7; |
+ |
+// Duration of idle interval between iterations of polling for a connection. |
+const int kConnectionPollingIntervalSecs = 3; |
+ |
+} // namespace |
+ |
+BluetoothThrottler::BluetoothThrottler(const RemoteDevice& remote_device) |
+ : remote_device_(remote_device), |
+ connection_(nullptr), |
+ weak_ptr_factory_(this) { |
+} |
+ |
+BluetoothThrottler::~BluetoothThrottler() { |
+ if (connection_) |
+ connection_->RemoveObserver(this); |
+} |
+ |
+void BluetoothThrottler::FindConnection( |
+ const device::BluetoothUUID& uuid, |
+ const ConnectionFinder::ConnectionCallback& connection_callback) { |
+ DCHECK(!connection_finder_); |
+ |
+ base::TimeTicks now = GetTickClock()->NowTicks(); |
+ base::TimeTicks throttled_start_time = |
+ last_disconnect_time_ + GetCooldownTimeDelta(); |
+ if (!last_disconnect_time_.is_null() && now < throttled_start_time) { |
+ base::TimeDelta delay = throttled_start_time - now; |
+ GetTaskRunner()->PostDelayedTask( |
+ FROM_HERE, |
+ base::Bind(&BluetoothThrottler::FindConnection, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ uuid, |
+ connection_callback), |
+ delay); |
+ return; |
+ } |
+ |
+ connection_finder_ = CreateConnectionFinder(uuid); |
+ connection_finder_->Find(base::Bind(&BluetoothThrottler::OnConnection, |
+ weak_ptr_factory_.GetWeakPtr(), |
+ connection_callback)); |
+} |
+ |
+void BluetoothThrottler::StopFindingConnections() { |
+ weak_ptr_factory_.InvalidateWeakPtrs(); |
+ connection_finder_.reset(); |
+} |
+ |
+scoped_ptr<ConnectionFinder> BluetoothThrottler::CreateConnectionFinder( |
+ const device::BluetoothUUID& uuid) { |
+ base::TimeDelta polling_interval = |
+ base::TimeDelta::FromSeconds(kConnectionPollingIntervalSecs); |
+ return make_scoped_ptr( |
+ new BluetoothConnectionFinder(remote_device_, uuid, polling_interval)); |
+} |
+ |
+base::TimeDelta BluetoothThrottler::GetCooldownTimeDelta() const { |
+ return base::TimeDelta::FromSeconds(kCooldownTimeSecs); |
+} |
+ |
+scoped_refptr<base::TaskRunner> BluetoothThrottler::GetTaskRunner() const { |
+ return base::MessageLoopProxy::current(); |
+} |
+ |
+base::TickClock* BluetoothThrottler::GetTickClock() { |
+ return &clock_; |
+} |
+ |
+void BluetoothThrottler::OnConnection( |
+ const ConnectionFinder::ConnectionCallback& connection_callback, |
+ scoped_ptr<Connection> connection) { |
+ DCHECK(!connection_); |
+ connection_ = connection.get(); |
+ connection_->AddObserver(this); |
+ |
+ connection_finder_.reset(); |
+ connection_callback.Run(connection.Pass()); |
+} |
+ |
+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
|
+ const Connection& connection, |
+ Connection::Status old_status, |
+ Connection::Status new_status) { |
+ DCHECK_EQ(&connection, connection_); |
+ if (old_status == Connection::CONNECTED && |
+ new_status == Connection::DISCONNECTED) { |
+ last_disconnect_time_ = GetTickClock()->NowTicks(); |
+ connection_->RemoveObserver(this); |
+ connection_ = nullptr; |
+ } |
+} |
+ |
+} // namespace proximity_auth |