Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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_H | |
| 6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_THROTTLER_H | |
| 7 | |
| 8 #include "base/macros.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/time/default_tick_clock.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "components/proximity_auth/connection_finder.h" | |
| 14 #include "components/proximity_auth/connection_observer.h" | |
| 15 #include "components/proximity_auth/remote_device.h" | |
| 16 | |
| 17 namespace base { | |
| 18 class TaskRunner; | |
| 19 class TickClock; | |
| 20 } | |
| 21 | |
| 22 namespace device { | |
| 23 class BluetoothUUID; | |
| 24 } | |
| 25 | |
| 26 namespace proximity_auth { | |
| 27 | |
| 28 class Connection; | |
| 29 | |
| 30 // This class throttles repeated connection attempts to the same device. This | |
| 31 // throttling is necessary to prevent a kernel race condition when connecting | |
| 32 // before the previous connection fully closes, putting the connection in a | |
| 33 // corrupted, and unrecoverable state. http://crbug.com/345232 | |
|
Tim Song
2014/10/27 17:49:25
This is actually not entirely true now that this i
Ilya Sherman
2014/10/28 00:40:47
Hmm, but is that a valid thing to do? What if the
Tim Song
2014/11/20 23:00:09
That's true. Maybe disconnect also works. I'll hav
| |
| 34 class BluetoothThrottler : public ConnectionObserver { | |
| 35 public: | |
| 36 // Creates a throttler for connections to the |remote_device|. | |
| 37 BluetoothThrottler(const RemoteDevice& remote_device); | |
| 38 virtual ~BluetoothThrottler(); | |
| 39 | |
| 40 // Attempts to find a connection to the service with the given |uuid| running | |
| 41 // on the |remote_device| passed to the constructor. This is similar to | |
| 42 // calling |BluetoothConnectionFinder::Find()|, but the connections will be | |
| 43 // throttled to prevent hitting a kernel race condition. Will continue to try | |
| 44 // to find the connection indefinitely until |StopFindingConnections()| is | |
| 45 // called. Calls |connection_callback| with the open connection once the | |
| 46 // remote device is connected. | |
| 47 // Note: This method must not be called a second time prior to calling | |
| 48 // StopFindingConnections(). | |
| 49 void FindConnection( | |
| 50 const device::BluetoothUUID& uuid, | |
| 51 const ConnectionFinder::ConnectionCallback& connection_callback); | |
| 52 | |
| 53 // Cancels any running or pending connection searches. | |
| 54 void StopFindingConnections(); | |
| 55 | |
| 56 protected: | |
| 57 // Creates a connection finder targeting the service with the given |uuid| on | |
| 58 // the |remote_device_|. Exposed for overriding in tests. | |
| 59 virtual scoped_ptr<ConnectionFinder> CreateConnectionFinder( | |
| 60 const device::BluetoothUUID& uuid); | |
| 61 | |
| 62 // Returns the duration to wait before reattempting a connection to the | |
| 63 // |remote_device_|. Exposed for testing. | |
| 64 base::TimeDelta GetCooldownTimeDelta() const; | |
| 65 | |
| 66 // Returns the task runner to use for posting delayed tasks. | |
| 67 // Exposed for overriding in tests. | |
| 68 virtual scoped_refptr<base::TaskRunner> GetTaskRunner() const; | |
| 69 | |
| 70 // Returns the clock to use for computing time ticks. | |
| 71 // Exposed for overriding in tests. | |
| 72 virtual base::TickClock* GetTickClock(); | |
| 73 | |
| 74 private: | |
| 75 // Callback for a connection. | |
| 76 void OnConnection( | |
| 77 const ConnectionFinder::ConnectionCallback& connection_callback, | |
| 78 scoped_ptr<Connection> connection); | |
| 79 | |
| 80 // ConnectionObserver: | |
| 81 virtual void OnConnectionStatusChanged( | |
| 82 const Connection& connection, | |
| 83 Connection::Status old_status, | |
| 84 Connection::Status new_status) override; | |
| 85 | |
| 86 // The device to connect to. | |
| 87 const RemoteDevice remote_device_; | |
| 88 | |
| 89 // Tracks the last seen disconnect time for the |remote_device_|. | |
| 90 base::TimeTicks last_disconnect_time_; | |
| 91 | |
| 92 // The currently running connection finder; null iff there is no currently | |
| 93 // running connection finder. | |
| 94 scoped_ptr<ConnectionFinder> connection_finder_; | |
| 95 | |
| 96 // The most current connection; null iff there is no current connection. | |
| 97 // Stored as a weak reference, which is safe because |this| instance is | |
| 98 // registered as an observer, and will unregister when the connection | |
| 99 // disconnects, which is guaranteed to occur before the connection is | |
| 100 // destroyed. | |
| 101 Connection* connection_; | |
| 102 | |
| 103 // Default time source; overridable by tests. | |
| 104 base::DefaultTickClock clock_; | |
| 105 | |
| 106 base::WeakPtrFactory<BluetoothThrottler> weak_ptr_factory_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(BluetoothThrottler); | |
| 109 }; | |
| 110 | |
| 111 } // namespace proximity_auth | |
| 112 | |
| 113 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_THROTTLER_H | |
| OLD | NEW |