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/throttled_bluetooth_connection_finder.h" |
| 6 |
| 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" |
| 13 |
| 14 namespace proximity_auth { |
| 15 |
| 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) { |
| 24 } |
| 25 |
| 26 ThrottledBluetoothConnectionFinder::~ThrottledBluetoothConnectionFinder() { |
| 27 } |
| 28 |
| 29 void ThrottledBluetoothConnectionFinder::Find( |
| 30 const ConnectionCallback& connection_callback) { |
| 31 const base::TimeDelta delay = throttler_->GetDelay(); |
| 32 |
| 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; |
| 41 } |
| 42 |
| 43 connection_finder_->Find( |
| 44 base::Bind(&ThrottledBluetoothConnectionFinder::OnConnection, |
| 45 weak_ptr_factory_.GetWeakPtr(), connection_callback)); |
| 46 } |
| 47 |
| 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()); |
| 53 } |
| 54 |
| 55 } // namespace proximity_auth |
OLD | NEW |