Roll libvpx 861f35:1fff3e
[chromium-blink-merge.git] / components / proximity_auth / bluetooth_connection_finder.h
blob40b28dd4bbef276d7b1d4196e5e625fb1d94225e
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.
5 #ifndef COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H
6 #define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H
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"
20 namespace proximity_auth {
22 class BluetoothConnection;
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 ~BluetoothConnectionFinder() override;
35 // ConnectionFinder:
36 void Find(const ConnectionCallback& connection_callback) override;
38 protected:
39 // Exposed for mocking out the connection in tests.
40 virtual scoped_ptr<Connection> CreateConnection();
42 // BluetoothAdapter::Observer:
43 void AdapterPresentChanged(device::BluetoothAdapter* adapter,
44 bool present) override;
45 void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
46 bool powered) override;
48 private:
49 // Returns true iff the Bluetooth adapter is ready to make connections.
50 bool IsReadyToPoll();
52 // Attempts to connect to the |remote_device_| if the system is ready for
53 // another iteration of polling.
54 void PollIfReady();
56 // Wrapper around |PollIfReady()| that can be posted as a delayed task.
57 void DelayedPollIfReady();
59 // Unregisters |this| instance as an observer from all objects that it might
60 // have registered with.
61 void UnregisterAsObserver();
63 // Callback to be called when the Bluetooth adapter is initialized.
64 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter);
66 // ConnectionObserver:
67 void OnConnectionStatusChanged(const Connection& connection,
68 Connection::Status old_status,
69 Connection::Status new_status) override;
71 // The remote device to connect to.
72 const RemoteDevice remote_device_;
74 // The UUID of the service on the remote device.
75 const device::BluetoothUUID uuid_;
77 // The time to wait between polling attempts.
78 const base::TimeDelta polling_interval_;
80 // Records the time at which the finder began searching for connections.
81 base::TimeTicks start_time_;
83 // The callback that should be called upon a successful connection.
84 ConnectionCallback connection_callback_;
86 // The Bluetooth adapter over which the Bluetooth connection will be made.
87 scoped_refptr<device::BluetoothAdapter> adapter_;
89 // The Bluetooth connection that will be opened.
90 scoped_ptr<Connection> connection_;
92 // Whether there is currently a polling task scheduled.
93 bool has_delayed_poll_scheduled_;
95 // Used to schedule everything else.
96 base::WeakPtrFactory<BluetoothConnectionFinder> weak_ptr_factory_;
98 DISALLOW_COPY_AND_ASSIGN(BluetoothConnectionFinder);
101 // TODO(isherman): Make sure to wire up the controller to listen for screen lock
102 // state change events, and create or destroy the connection finder as
103 // appropriate.
105 } // namespace proximity_auth
107 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H