File: bluetooth_connection_finder.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (128 lines) | stat: -rw-r--r-- 4,832 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H
#define COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H

#include <memory>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "components/cryptauth/connection.h"
#include "components/cryptauth/connection_finder.h"
#include "components/cryptauth/connection_observer.h"
#include "components/cryptauth/remote_device.h"
#include "components/proximity_auth/bluetooth_util.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_uuid.h"

namespace proximity_auth {

// This cryptauth::ConnectionFinder implementation tries to find a Bluetooth
// connection to
// the remote device by polling at a fixed interval.
class BluetoothConnectionFinder : public cryptauth::ConnectionFinder,
                                  public cryptauth::ConnectionObserver,
                                  public device::BluetoothAdapter::Observer {
 public:
  BluetoothConnectionFinder(const cryptauth::RemoteDevice& remote_device,
                            const device::BluetoothUUID& uuid,
                            const base::TimeDelta& polling_interval);
  ~BluetoothConnectionFinder() override;

  // cryptauth::ConnectionFinder:
  void Find(const cryptauth::ConnectionFinder::ConnectionCallback&
                connection_callback) override;

 protected:
  // Exposed for mocking out the connection in tests.
  virtual std::unique_ptr<cryptauth::Connection> CreateConnection();

  // Calls bluetooth_util::SeekDeviceByAddress. Exposed for testing, as this
  // utility function is platform dependent.
  virtual void SeekDeviceByAddress(
      const std::string& bluetooth_address,
      const base::Closure& callback,
      const bluetooth_util::ErrorCallback& error_callback);

  // BluetoothAdapter::Observer:
  void AdapterPresentChanged(device::BluetoothAdapter* adapter,
                             bool present) override;
  void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
                             bool powered) override;

 private:
  // Returns true iff the Bluetooth adapter is ready to make connections.
  bool IsReadyToPoll();

  // Attempts to connect to the |remote_device_| if the system is ready for
  // another iteration of polling.
  void PollIfReady();

  // Posts a delayed task to call |PollIfReady|. |OnDelayedPoll()| will be
  // called when the task fires.
  void PostDelayedPoll();
  void OnDelayedPoll();

  // Callbacks for bluetooth_util::SeekDeviceByAddress().
  void OnSeekedDeviceByAddress();
  void OnSeekedDeviceByAddressError(const std::string& error_message);

  // Unregisters |this| instance as an observer from all objects that it might
  // have registered with.
  void UnregisterAsObserver();

  // Callback to be called when the Bluetooth adapter is initialized.
  void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter);

  // ConnectionObserver:
  void OnConnectionStatusChanged(
      cryptauth::Connection* connection,
      cryptauth::Connection::Status old_status,
      cryptauth::Connection::Status new_status) override;

  // Used to invoke |connection_callback_| asynchronously, decoupling the
  // callback invocation from the ConnectionObserver callstack.
  void InvokeCallbackAsync();

  // The remote device to connect to.
  const cryptauth::RemoteDevice remote_device_;

  // The UUID of the service on the remote device.
  const device::BluetoothUUID uuid_;

  // The time to wait between polling attempts.
  const base::TimeDelta polling_interval_;

  // Records the time at which the finder began searching for connections.
  base::TimeTicks start_time_;

  // The callback that should be called upon a successful connection.
  cryptauth::ConnectionFinder::ConnectionCallback connection_callback_;

  // The Bluetooth adapter over which the Bluetooth connection will be made.
  scoped_refptr<device::BluetoothAdapter> adapter_;

  // The Bluetooth connection that will be opened.
  std::unique_ptr<cryptauth::Connection> connection_;

  // Whether there is currently a polling task scheduled.
  bool has_delayed_poll_scheduled_;

  // Used to schedule everything else.
  base::WeakPtrFactory<BluetoothConnectionFinder> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(BluetoothConnectionFinder);
};

// TODO(isherman): Make sure to wire up the controller to listen for screen lock
// state change events, and create or destroy the connection finder as
// appropriate.

}  // namespace proximity_auth

#endif  // COMPONENTS_PROXIMITY_AUTH_BLUETOOTH_CONNECTION_FINDER_H