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 #include "components/proximity_auth/connection.h" | |
14 | |
15 namespace proximity_auth { | |
16 | |
17 ThrottledBluetoothConnectionFinder::ThrottledBluetoothConnectionFinder( | |
18 scoped_ptr<BluetoothConnectionFinder> connection_finder, | |
19 scoped_refptr<base::TaskRunner> task_runner, | |
20 BluetoothThrottler* throttler) | |
21 : connection_finder_(connection_finder.Pass()), | |
22 task_runner_(task_runner), | |
23 throttler_(throttler), | |
24 connection_(nullptr), | |
25 weak_ptr_factory_(this) { | |
26 } | |
27 | |
28 ThrottledBluetoothConnectionFinder::~ThrottledBluetoothConnectionFinder() { | |
29 if (connection_) | |
30 connection_->RemoveObserver(this); | |
31 } | |
32 | |
33 void ThrottledBluetoothConnectionFinder::Find( | |
34 const ConnectionCallback& connection_callback) { | |
35 const base::TimeDelta delay = throttler_->GetDelay(); | |
36 | |
37 // Wait, if needed. | |
38 if (delay != base::TimeDelta()) { | |
39 task_runner_->PostDelayedTask( | |
40 FROM_HERE, | |
41 base::Bind(&ThrottledBluetoothConnectionFinder::Find, | |
42 weak_ptr_factory_.GetWeakPtr(), connection_callback), | |
43 delay); | |
44 return; | |
45 } | |
46 | |
47 connection_finder_->Find( | |
48 base::Bind(&ThrottledBluetoothConnectionFinder::OnConnection, | |
49 weak_ptr_factory_.GetWeakPtr(), connection_callback)); | |
50 } | |
51 | |
52 void ThrottledBluetoothConnectionFinder::OnConnection( | |
53 const ConnectionCallback& connection_callback, | |
54 scoped_ptr<Connection> connection) { | |
55 DCHECK(!connection_); | |
56 connection_ = connection.get(); | |
57 connection_->AddObserver(this); | |
58 | |
59 connection_callback.Run(connection.Pass()); | |
60 } | |
61 | |
62 void ThrottledBluetoothConnectionFinder::OnConnectionStatusChanged( | |
63 const Connection& connection, | |
64 Connection::Status old_status, | |
65 Connection::Status new_status) { | |
66 DCHECK_EQ(&connection, connection_); | |
67 if (old_status == Connection::CONNECTED && | |
68 new_status == Connection::DISCONNECTED) { | |
69 throttler_->OnConnectionClosed(); | |
Tim Song
2015/04/17 22:34:31
Using the ThrottledBluetoothConnectionFinder insta
Ilya Sherman
2015/04/22 00:48:59
Good point. Done.
| |
70 connection_->RemoveObserver(this); | |
71 connection_ = nullptr; | |
72 } | |
73 } | |
74 | |
75 } // namespace proximity_auth | |
OLD | NEW |