Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / proximity_auth / bluetooth_connection_finder.h
blobac195d8c06c5ad68f504e43b6ff109232aa0c9a1
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/bluetooth_util.h"
15 #include "components/proximity_auth/connection_finder.h"
16 #include "components/proximity_auth/connection_observer.h"
17 #include "components/proximity_auth/remote_device.h"
18 #include "device/bluetooth/bluetooth_adapter.h"
19 #include "device/bluetooth/bluetooth_uuid.h"
21 namespace proximity_auth {
23 class BluetoothConnection;
25 // This ConnectionFinder implementation tries to find a Bluetooth connection to
26 // the remote device by polling at a fixed interval.
27 class BluetoothConnectionFinder : public ConnectionFinder,
28 public ConnectionObserver,
29 public device::BluetoothAdapter::Observer {
30 public:
31 BluetoothConnectionFinder(const RemoteDevice& remote_device,
32 const device::BluetoothUUID& uuid,
33 const base::TimeDelta& polling_interval);
34 ~BluetoothConnectionFinder() override;
36 // ConnectionFinder:
37 void Find(const ConnectionCallback& connection_callback) override;
39 protected:
40 // Exposed for mocking out the connection in tests.
41 virtual scoped_ptr<Connection> CreateConnection();
43 // Calls bluetooth_util::SeekDeviceByAddress. Exposed for testing, as this
44 // utility function is platform dependent.
45 virtual void SeekDeviceByAddress(
46 const std::string& bluetooth_address,
47 const base::Closure& callback,
48 const bluetooth_util::ErrorCallback& error_callback);
50 // BluetoothAdapter::Observer:
51 void AdapterPresentChanged(device::BluetoothAdapter* adapter,
52 bool present) override;
53 void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
54 bool powered) override;
56 private:
57 // Returns true iff the Bluetooth adapter is ready to make connections.
58 bool IsReadyToPoll();
60 // Attempts to connect to the |remote_device_| if the system is ready for
61 // another iteration of polling.
62 void PollIfReady();
64 // Posts a delayed task to call |PollIfReady|. |OnDelayedPoll()| will be
65 // called when the task fires.
66 void PostDelayedPoll();
67 void OnDelayedPoll();
69 // Callbacks for bluetooth_util::SeekDeviceByAddress().
70 void OnSeekedDeviceByAddress();
71 void OnSeekedDeviceByAddressError(const std::string& error_message);
73 // Unregisters |this| instance as an observer from all objects that it might
74 // have registered with.
75 void UnregisterAsObserver();
77 // Callback to be called when the Bluetooth adapter is initialized.
78 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter);
80 // ConnectionObserver:
81 void OnConnectionStatusChanged(Connection* connection,
82 Connection::Status old_status,
83 Connection::Status new_status) override;
85 // Used to invoke |connection_callback_| asynchronously, decoupling the
86 // callback invocation from the ConnectionObserver callstack.
87 void InvokeCallbackAsync();
89 // The remote device to connect to.
90 const RemoteDevice remote_device_;
92 // The UUID of the service on the remote device.
93 const device::BluetoothUUID uuid_;
95 // The time to wait between polling attempts.
96 const base::TimeDelta polling_interval_;
98 // Records the time at which the finder began searching for connections.
99 base::TimeTicks start_time_;
101 // The callback that should be called upon a successful connection.
102 ConnectionCallback connection_callback_;
104 // The Bluetooth adapter over which the Bluetooth connection will be made.
105 scoped_refptr<device::BluetoothAdapter> adapter_;
107 // The Bluetooth connection that will be opened.
108 scoped_ptr<Connection> connection_;
110 // Whether there is currently a polling task scheduled.
111 bool has_delayed_poll_scheduled_;
113 // Used to schedule everything else.
114 base::WeakPtrFactory<BluetoothConnectionFinder> weak_ptr_factory_;
116 DISALLOW_COPY_AND_ASSIGN(BluetoothConnectionFinder);
119 // TODO(isherman): Make sure to wire up the controller to listen for screen lock
120 // state change events, and create or destroy the connection finder as
121 // appropriate.
123 } // namespace proximity_auth
125 #endif // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H