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