QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / components / proximity_auth / throttled_bluetooth_connection_finder.cc
blob252a47f9ec03f37b4ad9994617ccfd0664ed55be
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/throttled_bluetooth_connection_finder.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/task_runner.h"
10 #include "base/time/time.h"
11 #include "components/proximity_auth/bluetooth_connection_finder.h"
12 #include "components/proximity_auth/bluetooth_throttler.h"
14 namespace proximity_auth {
16 ThrottledBluetoothConnectionFinder::ThrottledBluetoothConnectionFinder(
17 scoped_ptr<BluetoothConnectionFinder> connection_finder,
18 scoped_refptr<base::TaskRunner> task_runner,
19 BluetoothThrottler* throttler)
20 : connection_finder_(connection_finder.Pass()),
21 task_runner_(task_runner),
22 throttler_(throttler),
23 weak_ptr_factory_(this) {
26 ThrottledBluetoothConnectionFinder::~ThrottledBluetoothConnectionFinder() {
29 void ThrottledBluetoothConnectionFinder::Find(
30 const ConnectionCallback& connection_callback) {
31 const base::TimeDelta delay = throttler_->GetDelay();
33 // Wait, if needed.
34 if (delay != base::TimeDelta()) {
35 task_runner_->PostDelayedTask(
36 FROM_HERE,
37 base::Bind(&ThrottledBluetoothConnectionFinder::Find,
38 weak_ptr_factory_.GetWeakPtr(), connection_callback),
39 delay);
40 return;
43 connection_finder_->Find(
44 base::Bind(&ThrottledBluetoothConnectionFinder::OnConnection,
45 weak_ptr_factory_.GetWeakPtr(), connection_callback));
48 void ThrottledBluetoothConnectionFinder::OnConnection(
49 const ConnectionCallback& connection_callback,
50 scoped_ptr<Connection> connection) {
51 throttler_->OnConnection(connection.get());
52 connection_callback.Run(connection.Pass());
55 } // namespace proximity_auth