OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H | 5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H |
6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H | 6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "components/cryptauth/connection.h" |
| 16 #include "components/cryptauth/connection_observer.h" |
15 #include "components/cryptauth/remote_device.h" | 17 #include "components/cryptauth/remote_device.h" |
16 #include "components/proximity_auth/bluetooth_util.h" | 18 #include "components/proximity_auth/bluetooth_util.h" |
17 #include "components/proximity_auth/connection_finder.h" | 19 #include "components/proximity_auth/connection_finder.h" |
18 #include "components/proximity_auth/connection_observer.h" | |
19 #include "device/bluetooth/bluetooth_adapter.h" | 20 #include "device/bluetooth/bluetooth_adapter.h" |
20 #include "device/bluetooth/bluetooth_uuid.h" | 21 #include "device/bluetooth/bluetooth_uuid.h" |
21 | 22 |
22 namespace proximity_auth { | 23 namespace proximity_auth { |
23 | 24 |
24 // This ConnectionFinder implementation tries to find a Bluetooth connection to | 25 // This ConnectionFinder implementation tries to find a Bluetooth connection to |
25 // the remote device by polling at a fixed interval. | 26 // the remote device by polling at a fixed interval. |
26 class BluetoothConnectionFinder : public ConnectionFinder, | 27 class BluetoothConnectionFinder : public ConnectionFinder, |
27 public ConnectionObserver, | 28 public cryptauth::ConnectionObserver, |
28 public device::BluetoothAdapter::Observer { | 29 public device::BluetoothAdapter::Observer { |
29 public: | 30 public: |
30 BluetoothConnectionFinder(const cryptauth::RemoteDevice& remote_device, | 31 BluetoothConnectionFinder(const cryptauth::RemoteDevice& remote_device, |
31 const device::BluetoothUUID& uuid, | 32 const device::BluetoothUUID& uuid, |
32 const base::TimeDelta& polling_interval); | 33 const base::TimeDelta& polling_interval); |
33 ~BluetoothConnectionFinder() override; | 34 ~BluetoothConnectionFinder() override; |
34 | 35 |
35 // ConnectionFinder: | 36 // ConnectionFinder: |
36 void Find(const ConnectionCallback& connection_callback) override; | 37 void Find(const ConnectionCallback& connection_callback) override; |
37 | 38 |
38 protected: | 39 protected: |
39 // Exposed for mocking out the connection in tests. | 40 // Exposed for mocking out the connection in tests. |
40 virtual std::unique_ptr<Connection> CreateConnection(); | 41 virtual std::unique_ptr<cryptauth::Connection> CreateConnection(); |
41 | 42 |
42 // Calls bluetooth_util::SeekDeviceByAddress. Exposed for testing, as this | 43 // Calls bluetooth_util::SeekDeviceByAddress. Exposed for testing, as this |
43 // utility function is platform dependent. | 44 // utility function is platform dependent. |
44 virtual void SeekDeviceByAddress( | 45 virtual void SeekDeviceByAddress( |
45 const std::string& bluetooth_address, | 46 const std::string& bluetooth_address, |
46 const base::Closure& callback, | 47 const base::Closure& callback, |
47 const bluetooth_util::ErrorCallback& error_callback); | 48 const bluetooth_util::ErrorCallback& error_callback); |
48 | 49 |
49 // BluetoothAdapter::Observer: | 50 // BluetoothAdapter::Observer: |
50 void AdapterPresentChanged(device::BluetoothAdapter* adapter, | 51 void AdapterPresentChanged(device::BluetoothAdapter* adapter, |
(...skipping 19 matching lines...) Expand all Loading... |
70 void OnSeekedDeviceByAddressError(const std::string& error_message); | 71 void OnSeekedDeviceByAddressError(const std::string& error_message); |
71 | 72 |
72 // Unregisters |this| instance as an observer from all objects that it might | 73 // Unregisters |this| instance as an observer from all objects that it might |
73 // have registered with. | 74 // have registered with. |
74 void UnregisterAsObserver(); | 75 void UnregisterAsObserver(); |
75 | 76 |
76 // Callback to be called when the Bluetooth adapter is initialized. | 77 // Callback to be called when the Bluetooth adapter is initialized. |
77 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); | 78 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); |
78 | 79 |
79 // ConnectionObserver: | 80 // ConnectionObserver: |
80 void OnConnectionStatusChanged(Connection* connection, | 81 void OnConnectionStatusChanged( |
81 Connection::Status old_status, | 82 cryptauth::Connection* connection, |
82 Connection::Status new_status) override; | 83 cryptauth::Connection::Status old_status, |
| 84 cryptauth::Connection::Status new_status) override; |
83 | 85 |
84 // Used to invoke |connection_callback_| asynchronously, decoupling the | 86 // Used to invoke |connection_callback_| asynchronously, decoupling the |
85 // callback invocation from the ConnectionObserver callstack. | 87 // callback invocation from the ConnectionObserver callstack. |
86 void InvokeCallbackAsync(); | 88 void InvokeCallbackAsync(); |
87 | 89 |
88 // The remote device to connect to. | 90 // The remote device to connect to. |
89 const cryptauth::RemoteDevice remote_device_; | 91 const cryptauth::RemoteDevice remote_device_; |
90 | 92 |
91 // The UUID of the service on the remote device. | 93 // The UUID of the service on the remote device. |
92 const device::BluetoothUUID uuid_; | 94 const device::BluetoothUUID uuid_; |
93 | 95 |
94 // The time to wait between polling attempts. | 96 // The time to wait between polling attempts. |
95 const base::TimeDelta polling_interval_; | 97 const base::TimeDelta polling_interval_; |
96 | 98 |
97 // Records the time at which the finder began searching for connections. | 99 // Records the time at which the finder began searching for connections. |
98 base::TimeTicks start_time_; | 100 base::TimeTicks start_time_; |
99 | 101 |
100 // The callback that should be called upon a successful connection. | 102 // The callback that should be called upon a successful connection. |
101 ConnectionCallback connection_callback_; | 103 ConnectionCallback connection_callback_; |
102 | 104 |
103 // The Bluetooth adapter over which the Bluetooth connection will be made. | 105 // The Bluetooth adapter over which the Bluetooth connection will be made. |
104 scoped_refptr<device::BluetoothAdapter> adapter_; | 106 scoped_refptr<device::BluetoothAdapter> adapter_; |
105 | 107 |
106 // The Bluetooth connection that will be opened. | 108 // The Bluetooth connection that will be opened. |
107 std::unique_ptr<Connection> connection_; | 109 std::unique_ptr<cryptauth::Connection> connection_; |
108 | 110 |
109 // Whether there is currently a polling task scheduled. | 111 // Whether there is currently a polling task scheduled. |
110 bool has_delayed_poll_scheduled_; | 112 bool has_delayed_poll_scheduled_; |
111 | 113 |
112 // Used to schedule everything else. | 114 // Used to schedule everything else. |
113 base::WeakPtrFactory<BluetoothConnectionFinder> weak_ptr_factory_; | 115 base::WeakPtrFactory<BluetoothConnectionFinder> weak_ptr_factory_; |
114 | 116 |
115 DISALLOW_COPY_AND_ASSIGN(BluetoothConnectionFinder); | 117 DISALLOW_COPY_AND_ASSIGN(BluetoothConnectionFinder); |
116 }; | 118 }; |
117 | 119 |
118 // TODO(isherman): Make sure to wire up the controller to listen for screen lock | 120 // TODO(isherman): Make sure to wire up the controller to listen for screen lock |
119 // state change events, and create or destroy the connection finder as | 121 // state change events, and create or destroy the connection finder as |
120 // appropriate. | 122 // appropriate. |
121 | 123 |
122 } // namespace proximity_auth | 124 } // namespace proximity_auth |
123 | 125 |
124 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H | 126 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H |
OLD | NEW |