OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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_impl.h" |
| 6 |
| 7 #include "base/stl_util.h" |
| 8 #include "base/time/tick_clock.h" |
| 9 #include "components/proximity_auth/connection.h" |
| 10 |
| 11 namespace proximity_auth { |
| 12 namespace { |
| 13 |
| 14 // Time to wait after disconnect before reconnecting. |
| 15 const int kCooldownTimeSecs = 7; |
| 16 |
| 17 } // namespace |
| 18 |
| 19 BluetoothThrottlerImpl::BluetoothThrottlerImpl( |
| 20 scoped_ptr<base::TickClock> clock) |
| 21 : clock_(clock.Pass()) { |
| 22 } |
| 23 |
| 24 BluetoothThrottlerImpl::~BluetoothThrottlerImpl() { |
| 25 for (Connection* connection : connections_) { |
| 26 connection->RemoveObserver(this); |
| 27 } |
| 28 } |
| 29 |
| 30 base::TimeDelta BluetoothThrottlerImpl::GetDelay() const { |
| 31 if (last_disconnect_time_.is_null()) |
| 32 return base::TimeDelta(); |
| 33 |
| 34 base::TimeTicks now = clock_->NowTicks(); |
| 35 base::TimeTicks throttled_start_time = |
| 36 last_disconnect_time_ + GetCooldownTimeDelta(); |
| 37 if (now >= throttled_start_time) |
| 38 return base::TimeDelta(); |
| 39 |
| 40 return throttled_start_time - now; |
| 41 } |
| 42 |
| 43 void BluetoothThrottlerImpl::OnConnection(Connection* connection) { |
| 44 DCHECK(!ContainsKey(connections_, connection)); |
| 45 connections_.insert(connection); |
| 46 connection->AddObserver(this); |
| 47 } |
| 48 |
| 49 base::TimeDelta BluetoothThrottlerImpl::GetCooldownTimeDelta() const { |
| 50 return base::TimeDelta::FromSeconds(kCooldownTimeSecs); |
| 51 } |
| 52 |
| 53 void BluetoothThrottlerImpl::OnConnectionStatusChanged( |
| 54 Connection* connection, |
| 55 Connection::Status old_status, |
| 56 Connection::Status new_status) { |
| 57 DCHECK(ContainsKey(connections_, connection)); |
| 58 if (old_status == Connection::CONNECTED && |
| 59 new_status == Connection::DISCONNECTED) { |
| 60 last_disconnect_time_ = clock_->NowTicks(); |
| 61 connection->RemoveObserver(this); |
| 62 connections_.erase(connection); |
| 63 } |
| 64 } |
| 65 |
| 66 } // namespace proximity_auth |
OLD | NEW |