Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / proximity_auth / bluetooth_throttler_impl.cc
bloba44ce1e33bcb54fef8cb3fc352ca82354b90bb81
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.
5 #include "components/proximity_auth/bluetooth_throttler_impl.h"
7 #include "base/stl_util.h"
8 #include "base/time/tick_clock.h"
9 #include "components/proximity_auth/connection.h"
11 namespace proximity_auth {
12 namespace {
14 // Time to wait after disconnect before reconnecting.
15 const int kCooldownTimeSecs = 7;
17 } // namespace
19 BluetoothThrottlerImpl::BluetoothThrottlerImpl(
20 scoped_ptr<base::TickClock> clock)
21 : clock_(clock.Pass()) {
24 BluetoothThrottlerImpl::~BluetoothThrottlerImpl() {
25 for (Connection* connection : connections_) {
26 connection->RemoveObserver(this);
30 base::TimeDelta BluetoothThrottlerImpl::GetDelay() const {
31 if (last_disconnect_time_.is_null())
32 return base::TimeDelta();
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();
40 return throttled_start_time - now;
43 void BluetoothThrottlerImpl::OnConnection(Connection* connection) {
44 DCHECK(!ContainsKey(connections_, connection));
45 connections_.insert(connection);
46 connection->AddObserver(this);
49 base::TimeDelta BluetoothThrottlerImpl::GetCooldownTimeDelta() const {
50 return base::TimeDelta::FromSeconds(kCooldownTimeSecs);
53 void BluetoothThrottlerImpl::OnConnectionStatusChanged(
54 Connection* connection,
55 Connection::Status old_status,
56 Connection::Status new_status) {
57 DCHECK(ContainsKey(connections_, connection));
58 if (new_status == Connection::DISCONNECTED) {
59 last_disconnect_time_ = clock_->NowTicks();
60 connection->RemoveObserver(this);
61 connections_.erase(connection);
65 } // namespace proximity_auth