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_CONNECTION_FINDER_H | |
6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H | |
7 | |
8 #include "base/callback.h" | |
9 #include "base/macros.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/time/time.h" | |
14 #include "components/proximity_auth/connection_finder.h" | |
15 #include "components/proximity_auth/connection_observer.h" | |
16 #include "components/proximity_auth/remote_device.h" | |
17 #include "device/bluetooth/bluetooth_adapter.h" | |
18 #include "device/bluetooth/bluetooth_uuid.h" | |
19 | |
20 namespace proximity_auth { | |
21 | |
22 class BluetoothConnection; | |
23 | |
24 // This ConnectionFinder implementation tries to find a Bluetooth connection to | |
25 // the remote device by polling at a fixed interval. | |
26 class BluetoothConnectionFinder : public ConnectionFinder, | |
27 public ConnectionObserver, | |
28 public device::BluetoothAdapter::Observer { | |
29 public: | |
30 BluetoothConnectionFinder(const RemoteDevice& remote_device, | |
31 const device::BluetoothUUID& uuid, | |
32 const base::TimeDelta& polling_interval); | |
33 virtual ~BluetoothConnectionFinder(); | |
34 | |
35 // ConnectionFinder: | |
36 virtual void Find(const ConnectionCallback& connection_callback) override; | |
37 | |
38 protected: | |
39 // Exposed for mocking out the connection in tests. | |
40 virtual scoped_ptr<Connection> CreateConnection(); | |
41 | |
42 // BluetoothAdapter::Observer: | |
43 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter, | |
44 bool present) override; | |
45 virtual void AdapterPoweredChanged(device::BluetoothAdapter* adapter, | |
46 bool powered) override; | |
47 | |
48 private: | |
49 // Returns true iff the Bluetooth adapter is ready to make connections. | |
50 bool IsReadyToPoll(); | |
51 | |
52 // Attempts to connect to the |remote_device_| if the system is ready for | |
53 // another iteration of polling. | |
54 void PollIfReady(); | |
55 | |
56 // Wrapper around |PollIfReady()| that can be posted as a delayed task. | |
57 void DelayedPollIfReady(); | |
58 | |
59 // Unregisters |this| instance as an observer from all objects that it might | |
60 // have registered with. | |
61 void UnregisterAsObserver(); | |
62 | |
63 // Callback to be called when the Bluetooth adapter is initialized. | |
64 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); | |
65 | |
66 // ConnectionObserver: | |
67 virtual void OnConnectionStatusChanged( | |
68 const Connection& connection, | |
69 Connection::Status old_status, | |
70 Connection::Status new_status) override; | |
71 | |
72 // The remote device to connect to. | |
73 const RemoteDevice remote_device_; | |
74 | |
75 // The UUID of the service on the remote device. | |
76 const device::BluetoothUUID uuid_; | |
77 | |
78 // The time to wait between polling attempts. | |
79 const base::TimeDelta polling_interval_; | |
80 | |
81 // Records the time at which the finder began searching for connections. | |
82 base::TimeTicks start_time_; | |
83 | |
84 // The callback that should be called upon a successful connection. | |
85 ConnectionCallback connection_callback_; | |
86 | |
87 // The Bluetooth adapter over which the Bluetooth connection will be made. | |
88 scoped_refptr<device::BluetoothAdapter> adapter_; | |
89 | |
90 // The Bluetooth connection that will be opened. | |
91 scoped_ptr<Connection> connection_; | |
92 | |
93 // Whether there is currently a polling task scheduled. | |
94 bool has_delayed_poll_scheduled_; | |
95 | |
96 // Used to schedule everything else. | |
97 base::WeakPtrFactory<BluetoothConnectionFinder> weak_ptr_factory_; | |
98 | |
99 DISALLOW_COPY_AND_ASSIGN(BluetoothConnectionFinder); | |
100 }; | |
101 | |
102 // TODO(isherman): Make sure to wire up the controller to listen for screen lock | |
103 // state change events, and create or destroy the connection finder as | |
104 // appropriate. | |
105 | |
106 } // namespace proximity_auth | |
107 | |
108 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H | |
OLD | NEW |