Roll src/third_party/WebKit c8d1660:ae3684d (svn 197337:197343)
[chromium-blink-merge.git] / components / proximity_auth / bluetooth_throttler_impl.h
blob3ba71541def03e9476a9829e06c97daec13c110d
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.
5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_THROTTLER_IMPL_H
6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_THROTTLER_IMPL_H
8 #include <set>
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "components/proximity_auth/bluetooth_throttler.h"
14 #include "components/proximity_auth/connection_observer.h"
16 namespace base {
17 class TickClock;
20 namespace proximity_auth {
22 class Connection;
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(scoped_ptr<base::TickClock> clock);
34 ~BluetoothThrottlerImpl() override;
36 // BluetoothThrottler:
37 base::TimeDelta GetDelay() const override;
38 void OnConnection(Connection* connection) override;
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;
45 private:
46 // ConnectionObserver:
47 void OnConnectionStatusChanged(Connection* connection,
48 Connection::Status old_status,
49 Connection::Status new_status) override;
51 // Tracks the last seen disconnect time for the |remote_device_|.
52 base::TimeTicks last_disconnect_time_;
54 // The time source.
55 scoped_ptr<base::TickClock> clock_;
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_;
64 DISALLOW_COPY_AND_ASSIGN(BluetoothThrottlerImpl);
67 } // namespace proximity_auth
69 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_THROTTLER_IMPL_H