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 #ifndef COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_THROTTLER_IMPL_H | |
6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_THROTTLER_IMPL_H | |
7 | |
8 #include <memory> | |
9 #include <set> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/time/time.h" | |
13 #include "components/proximity_auth/bluetooth_throttler.h" | |
14 #include "components/proximity_auth/connection_observer.h" | |
15 | |
16 namespace base { | |
17 class TickClock; | |
18 } | |
19 | |
20 namespace proximity_auth { | |
21 | |
22 class Connection; | |
23 | |
24 // This class throttles repeated connection attempts to the same device. This | |
25 // throttling is necessary to prevent a kernel race condition when connecting | |
26 // before the previous connection fully closes, putting the connection in a | |
27 // corrupted, and unrecoverable state. http://crbug.com/345232 | |
28 class BluetoothThrottlerImpl : public BluetoothThrottler, | |
29 public ConnectionObserver { | |
30 public: | |
31 // Creates a throttler for connections to a remote device, using the |clock| | |
32 // as a time source. | |
33 explicit BluetoothThrottlerImpl(std::unique_ptr<base::TickClock> clock); | |
34 ~BluetoothThrottlerImpl() override; | |
35 | |
36 // BluetoothThrottler: | |
37 base::TimeDelta GetDelay() const override; | |
38 void OnConnection(Connection* connection) override; | |
39 | |
40 protected: | |
41 // Returns the duration to wait, after disconnecting, before reattempting a | |
42 // connection to the remote device. Exposed for testing. | |
43 base::TimeDelta GetCooldownTimeDelta() const; | |
44 | |
45 private: | |
46 // ConnectionObserver: | |
47 void OnConnectionStatusChanged(Connection* connection, | |
48 Connection::Status old_status, | |
49 Connection::Status new_status) override; | |
50 | |
51 // Tracks the last seen disconnect time for the |remote_device_|. | |
52 base::TimeTicks last_disconnect_time_; | |
53 | |
54 // The time source. | |
55 std::unique_ptr<base::TickClock> clock_; | |
56 | |
57 // The currently connected connections. | |
58 // Each connection is stored as a weak reference, which is safe because |this| | |
59 // instance is registered as an observer, and will unregister when the | |
60 // connection disconnects, which is guaranteed to occur before the connection | |
61 // is destroyed. | |
62 std::set<Connection*> connections_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(BluetoothThrottlerImpl); | |
65 }; | |
66 | |
67 } // namespace proximity_auth | |
68 | |
69 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_THROTTLER_IMPL_H | |
OLD | NEW |