Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / proximity_auth / bluetooth_connection_finder.h
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   ~BluetoothConnectionFinder() override;
34
35   // ConnectionFinder:
36   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   void AdapterPresentChanged(device::BluetoothAdapter* adapter,
44                              bool present) override;
45   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   void OnConnectionStatusChanged(const Connection& connection,
68                                  Connection::Status old_status,
69                                  Connection::Status new_status) override;
70
71   // The remote device to connect to.
72   const RemoteDevice remote_device_;
73
74   // The UUID of the service on the remote device.
75   const device::BluetoothUUID uuid_;
76
77   // The time to wait between polling attempts.
78   const base::TimeDelta polling_interval_;
79
80   // Records the time at which the finder began searching for connections.
81   base::TimeTicks start_time_;
82
83   // The callback that should be called upon a successful connection.
84   ConnectionCallback connection_callback_;
85
86   // The Bluetooth adapter over which the Bluetooth connection will be made.
87   scoped_refptr<device::BluetoothAdapter> adapter_;
88
89   // The Bluetooth connection that will be opened.
90   scoped_ptr<Connection> connection_;
91
92   // Whether there is currently a polling task scheduled.
93   bool has_delayed_poll_scheduled_;
94
95   // Used to schedule everything else.
96   base::WeakPtrFactory<BluetoothConnectionFinder> weak_ptr_factory_;
97
98   DISALLOW_COPY_AND_ASSIGN(BluetoothConnectionFinder);
99 };
100
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.
104
105 }  // namespace proximity_auth
106
107 #endif  // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H