| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/proximity_auth/throttled_bluetooth_connection_finder.h" | 5 #include "components/proximity_auth/throttled_bluetooth_connection_finder.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/task_runner.h" | 11 #include "base/task_runner.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "components/cryptauth/bluetooth_throttler.h" |
| 14 #include "components/cryptauth/connection.h" |
| 13 #include "components/proximity_auth/bluetooth_connection_finder.h" | 15 #include "components/proximity_auth/bluetooth_connection_finder.h" |
| 14 #include "components/proximity_auth/bluetooth_throttler.h" | |
| 15 | 16 |
| 16 namespace proximity_auth { | 17 namespace proximity_auth { |
| 17 | 18 |
| 18 ThrottledBluetoothConnectionFinder::ThrottledBluetoothConnectionFinder( | 19 ThrottledBluetoothConnectionFinder::ThrottledBluetoothConnectionFinder( |
| 19 std::unique_ptr<BluetoothConnectionFinder> connection_finder, | 20 std::unique_ptr<BluetoothConnectionFinder> connection_finder, |
| 20 scoped_refptr<base::TaskRunner> task_runner, | 21 scoped_refptr<base::TaskRunner> task_runner, |
| 21 BluetoothThrottler* throttler) | 22 cryptauth::BluetoothThrottler* throttler) |
| 22 : connection_finder_(std::move(connection_finder)), | 23 : connection_finder_(std::move(connection_finder)), |
| 23 task_runner_(task_runner), | 24 task_runner_(task_runner), |
| 24 throttler_(throttler), | 25 throttler_(throttler), |
| 25 weak_ptr_factory_(this) {} | 26 weak_ptr_factory_(this) {} |
| 26 | 27 |
| 27 ThrottledBluetoothConnectionFinder::~ThrottledBluetoothConnectionFinder() { | 28 ThrottledBluetoothConnectionFinder::~ThrottledBluetoothConnectionFinder() { |
| 28 } | 29 } |
| 29 | 30 |
| 30 void ThrottledBluetoothConnectionFinder::Find( | 31 void ThrottledBluetoothConnectionFinder::Find( |
| 31 const ConnectionCallback& connection_callback) { | 32 const ConnectionCallback& connection_callback) { |
| 32 const base::TimeDelta delay = throttler_->GetDelay(); | 33 const base::TimeDelta delay = throttler_->GetDelay(); |
| 33 | 34 |
| 34 // Wait, if needed. | 35 // Wait, if needed. |
| 35 if (!delay.is_zero()) { | 36 if (!delay.is_zero()) { |
| 36 task_runner_->PostDelayedTask( | 37 task_runner_->PostDelayedTask( |
| 37 FROM_HERE, | 38 FROM_HERE, |
| 38 base::Bind(&ThrottledBluetoothConnectionFinder::Find, | 39 base::Bind(&ThrottledBluetoothConnectionFinder::Find, |
| 39 weak_ptr_factory_.GetWeakPtr(), connection_callback), | 40 weak_ptr_factory_.GetWeakPtr(), connection_callback), |
| 40 delay); | 41 delay); |
| 41 return; | 42 return; |
| 42 } | 43 } |
| 43 | 44 |
| 44 connection_finder_->Find( | 45 connection_finder_->Find( |
| 45 base::Bind(&ThrottledBluetoothConnectionFinder::OnConnection, | 46 base::Bind(&ThrottledBluetoothConnectionFinder::OnConnection, |
| 46 weak_ptr_factory_.GetWeakPtr(), connection_callback)); | 47 weak_ptr_factory_.GetWeakPtr(), connection_callback)); |
| 47 } | 48 } |
| 48 | 49 |
| 49 void ThrottledBluetoothConnectionFinder::OnConnection( | 50 void ThrottledBluetoothConnectionFinder::OnConnection( |
| 50 const ConnectionCallback& connection_callback, | 51 const ConnectionCallback& connection_callback, |
| 51 std::unique_ptr<Connection> connection) { | 52 std::unique_ptr<cryptauth::Connection> connection) { |
| 52 throttler_->OnConnection(connection.get()); | 53 throttler_->OnConnection(connection.get()); |
| 53 connection_callback.Run(std::move(connection)); | 54 connection_callback.Run(std::move(connection)); |
| 54 } | 55 } |
| 55 | 56 |
| 56 } // namespace proximity_auth | 57 } // namespace proximity_auth |
| OLD | NEW |