OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_PROXIMITY_AUTH_PROXIMITY_MONITOR_IMPL_H | 5 #ifndef COMPONENTS_PROXIMITY_AUTH_PROXIMITY_MONITOR_IMPL_H |
6 #define COMPONENTS_PROXIMITY_AUTH_PROXIMITY_MONITOR_IMPL_H | 6 #define COMPONENTS_PROXIMITY_AUTH_PROXIMITY_MONITOR_IMPL_H |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
14 #include "components/cryptauth/connection.h" | |
14 #include "components/cryptauth/remote_device.h" | 15 #include "components/cryptauth/remote_device.h" |
15 #include "components/proximity_auth/proximity_monitor.h" | 16 #include "components/proximity_auth/proximity_monitor.h" |
16 #include "device/bluetooth/bluetooth_device.h" | 17 #include "device/bluetooth/bluetooth_device.h" |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 class TickClock; | 20 class TickClock; |
20 class TimeTicks; | 21 class TimeTicks; |
21 } | 22 } |
22 | 23 |
23 namespace device { | 24 namespace device { |
24 class BluetoothAdapter; | 25 class BluetoothAdapter; |
25 } | 26 } |
26 | 27 |
27 namespace proximity_auth { | 28 namespace proximity_auth { |
28 | 29 |
29 class ProximityMonitorObserver; | 30 class ProximityMonitorObserver; |
30 | 31 |
31 // The concrete implemenation of the proximity monitor interface. | 32 // The concrete implemenation of the proximity monitor interface. |
32 class ProximityMonitorImpl : public ProximityMonitor { | 33 class ProximityMonitorImpl : public ProximityMonitor { |
33 public: | 34 public: |
34 // The |observer| is not owned, and must outlive |this| instance. | 35 // The |connection| is not owned, and must outlive |this| instance. |
35 ProximityMonitorImpl(const cryptauth::RemoteDevice& remote_device, | 36 ProximityMonitorImpl(cryptauth::Connection* connection, |
36 std::unique_ptr<base::TickClock> clock); | 37 std::unique_ptr<base::TickClock> clock); |
37 ~ProximityMonitorImpl() override; | 38 ~ProximityMonitorImpl() override; |
38 | 39 |
39 // ProximityMonitor: | 40 // ProximityMonitor: |
40 void Start() override; | 41 void Start() override; |
41 void Stop() override; | 42 void Stop() override; |
42 Strategy GetStrategy() const override; | |
43 bool IsUnlockAllowed() const override; | 43 bool IsUnlockAllowed() const override; |
44 bool IsInRssiRange() const override; | |
45 void RecordProximityMetricsOnAuthSuccess() override; | 44 void RecordProximityMetricsOnAuthSuccess() override; |
46 void AddObserver(ProximityMonitorObserver* observer) override; | 45 void AddObserver(ProximityMonitorObserver* observer) override; |
47 void RemoveObserver(ProximityMonitorObserver* observer) override; | 46 void RemoveObserver(ProximityMonitorObserver* observer) override; |
48 | 47 |
49 protected: | |
50 // Sets the proximity detection strategy. Exposed for testing. | |
51 // TODO(isherman): Stop exposing this for testing once prefs are properly | |
52 // hooked up. | |
53 virtual void SetStrategy(Strategy strategy); | |
54 | |
55 private: | 48 private: |
56 struct TransmitPowerReading { | |
57 TransmitPowerReading(int transmit_power, int max_transmit_power); | |
58 | |
59 // Returns true if |this| transmit power reading indicates proximity. | |
60 bool IsInProximity() const; | |
61 | |
62 // The current transmit power. | |
63 int transmit_power; | |
64 | |
65 // The maximum possible transmit power. | |
66 int max_transmit_power; | |
67 }; | |
68 | |
69 // Callback for asynchronous initialization of the Bluetooth adpater. | 49 // Callback for asynchronous initialization of the Bluetooth adpater. |
70 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); | 50 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); |
71 | 51 |
72 // Ensures that the app is periodically polling for the proximity status | 52 // Ensures that the app is periodically polling for the proximity status |
73 // between the remote and the local device iff it should be, based on the | 53 // between the remote and the local device iff it should be, based on the |
74 // current app state. | 54 // current app state. |
75 void UpdatePollingState(); | 55 void UpdatePollingState(); |
76 | 56 |
77 // Performs a scheduled |UpdatePollingState()| operation. This method is | 57 // Performs a scheduled |UpdatePollingState()| operation. This method is |
78 // used to distinguish periodically scheduled calls to |UpdatePollingState()| | 58 // used to distinguish periodically scheduled calls to |UpdatePollingState()| |
79 // from event-driven calls, which should be handled differently. | 59 // from event-driven calls, which should be handled differently. |
80 void PerformScheduledUpdatePollingState(); | 60 void PerformScheduledUpdatePollingState(); |
81 | 61 |
82 // Returns |true| iff the app should be periodically polling for the proximity | 62 // Returns |true| iff the app should be periodically polling for the proximity |
83 // status between the remote and the local device. | 63 // status between the remote and the local device. |
84 bool ShouldPoll() const; | 64 bool ShouldPoll() const; |
85 | 65 |
86 // Polls the connection information. | 66 // Polls the connection information. |
87 void Poll(); | 67 void Poll(); |
88 | 68 |
89 // Callback to received the polled-for connection info. | 69 // Callback to received the polled-for connection info. |
90 void OnConnectionInfo( | 70 void OnConnectionInfo( |
91 const device::BluetoothDevice::ConnectionInfo& connection_info); | 71 const device::BluetoothDevice::ConnectionInfo& connection_info); |
92 | 72 |
93 // Resets the proximity state to |false|, and clears all member variables | 73 // Resets the proximity state to |false|, and clears all member variables |
94 // tracking the proximity state. | 74 // tracking the proximity state. |
95 void ClearProximityState(); | 75 void ClearProximityState(); |
96 | 76 |
97 // Updates the proximity state with a new |connection_info| sample of the | 77 // Updates the proximity state with a new |connection_info| sample of the |
98 // current RSSI and Tx power, and the device's maximum Tx power. | 78 // current RSSI. |
99 void AddSample( | 79 void AddSample( |
100 const device::BluetoothDevice::ConnectionInfo& connection_info); | 80 const device::BluetoothDevice::ConnectionInfo& connection_info); |
101 | 81 |
102 // Checks whether the proximity state has changed based on the current | 82 // Checks whether the proximity state has changed based on the current |
103 // samples. Notifies |observers_| on a change. | 83 // samples. Notifies |observers_| on a change. |
104 void CheckForProximityStateChange(); | 84 void CheckForProximityStateChange(); |
105 | 85 |
106 // The remote device being monitored. | 86 // The current connection being monitored. Not owned and must outlive this |
107 const cryptauth::RemoteDevice remote_device_; | 87 // instance. |
88 cryptauth::Connection* connection_; | |
108 | 89 |
109 // The observers attached to the ProximityMonitor. | 90 // The observers attached to the ProximityMonitor. |
110 base::ObserverList<ProximityMonitorObserver> observers_; | 91 base::ObserverList<ProximityMonitorObserver> observers_; |
111 | 92 |
112 // The Bluetooth adapter that will be polled for connection info. | 93 // The Bluetooth adapter that will be polled for connection info. |
113 scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_; | 94 scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_; |
114 | 95 |
115 // The strategy used to determine whether the remote device is in proximity. | |
116 Strategy strategy_; | |
117 | |
118 // Whether the remote device is currently in close proximity to the local | 96 // Whether the remote device is currently in close proximity to the local |
119 // device. | 97 // device. |
120 bool remote_device_is_in_proximity_; | 98 bool remote_device_is_in_proximity_; |
121 | 99 |
122 // Whether the proximity monitor is active, i.e. should possibly be scanning | 100 // Whether the proximity monitor is active, i.e. should possibly be scanning |
123 // for proximity to the remote device. | 101 // for proximity to the remote device. |
124 bool is_active_; | 102 bool is_active_; |
125 | 103 |
126 // The exponentailly weighted rolling average of the RSSI, used to smooth the | 104 // The exponentailly weighted rolling average of the RSSI, used to smooth the |
127 // RSSI readings. Null if the monitor is inactive, has not recently observed | 105 // RSSI readings. Null if the monitor is inactive, has not recently observed |
128 // an RSSI reading, or the most recent connection info included an invalid | 106 // an RSSI reading, or the most recent connection info included an invalid |
129 // measurement. | 107 // measurement. |
130 std::unique_ptr<double> rssi_rolling_average_; | 108 std::unique_ptr<double> rssi_rolling_average_; |
131 | 109 |
132 // The last TX power reading. Null if the monitor is inactive, has not | |
133 // recently observed a TX power reading, or the most recent connection info | |
134 // included an invalid measurement. | |
135 std::unique_ptr<TransmitPowerReading> last_transmit_power_reading_; | |
136 | |
137 // The timestamp of the last zero RSSI reading. An RSSI value of 0 is special | 110 // The timestamp of the last zero RSSI reading. An RSSI value of 0 is special |
138 // because both devices adjust their transmit powers such that the RSSI is in | 111 // because both devices adjust their transmit powers such that the RSSI is in |
139 // this golden range, if possible. Null if the monitor is inactive, has not | 112 // this golden range, if possible. Null if the monitor is inactive, has not |
140 // recently observed an RSSI reading, or the most recent connection info | 113 // recently observed an RSSI reading, or the most recent connection info |
141 // included an invalid measurement. | 114 // included an invalid measurement. |
142 std::unique_ptr<base::TimeTicks> last_zero_rssi_timestamp_; | 115 std::unique_ptr<base::TimeTicks> last_zero_rssi_timestamp_; |
sacomoto
2017/04/27 20:10:03
This is not relevant anymore, right? For BLE we wo
Tim Song
2017/04/28 00:27:26
Done.
| |
143 | 116 |
144 // Used to access non-decreasing time measurements. | 117 // Used to access non-decreasing time measurements. |
145 std::unique_ptr<base::TickClock> clock_; | 118 std::unique_ptr<base::TickClock> clock_; |
146 | 119 |
147 // Used to vend weak pointers for polling. Using a separate factory for these | 120 // Used to vend weak pointers for polling. Using a separate factory for these |
148 // weak pointers allows the weak pointers to be invalidated when polling | 121 // weak pointers allows the weak pointers to be invalidated when polling |
149 // stops, which effectively cancels the scheduled tasks. | 122 // stops, which effectively cancels the scheduled tasks. |
150 base::WeakPtrFactory<ProximityMonitorImpl> polling_weak_ptr_factory_; | 123 base::WeakPtrFactory<ProximityMonitorImpl> polling_weak_ptr_factory_; |
151 | 124 |
152 // Used to vend all other weak pointers. | 125 // Used to vend all other weak pointers. |
153 base::WeakPtrFactory<ProximityMonitorImpl> weak_ptr_factory_; | 126 base::WeakPtrFactory<ProximityMonitorImpl> weak_ptr_factory_; |
154 | 127 |
155 DISALLOW_COPY_AND_ASSIGN(ProximityMonitorImpl); | 128 DISALLOW_COPY_AND_ASSIGN(ProximityMonitorImpl); |
156 }; | 129 }; |
157 | 130 |
158 } // namespace proximity_auth | 131 } // namespace proximity_auth |
159 | 132 |
160 #endif // COMPONENTS_PROXIMITY_AUTH_PROXIMITY_MONITOR_IMPL_H | 133 #endif // COMPONENTS_PROXIMITY_AUTH_PROXIMITY_MONITOR_IMPL_H |
OLD | NEW |