| 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 } | 21 } |
| 22 | 22 |
| 23 namespace device { | 23 namespace device { |
| 24 class BluetoothAdapter; | 24 class BluetoothAdapter; |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace proximity_auth { | 27 namespace proximity_auth { |
| 28 | 28 |
| 29 class ProximityMonitorObserver; | 29 class ProximityMonitorObserver; |
| 30 | 30 |
| 31 // The concrete implemenation of the proximity monitor interface. | 31 // The concrete implemenation of the proximity monitor interface. |
| 32 class ProximityMonitorImpl : public ProximityMonitor { | 32 class ProximityMonitorImpl : public ProximityMonitor { |
| 33 public: | 33 public: |
| 34 // The |observer| is not owned, and must outlive |this| instance. | 34 // The |connection| is not owned, and must outlive |this| instance. |
| 35 ProximityMonitorImpl(const cryptauth::RemoteDevice& remote_device, | 35 ProximityMonitorImpl(cryptauth::Connection* connection, |
| 36 std::unique_ptr<base::TickClock> clock); | 36 std::unique_ptr<base::TickClock> clock); |
| 37 ~ProximityMonitorImpl() override; | 37 ~ProximityMonitorImpl() override; |
| 38 | 38 |
| 39 // ProximityMonitor: | 39 // ProximityMonitor: |
| 40 void Start() override; | 40 void Start() override; |
| 41 void Stop() override; | 41 void Stop() override; |
| 42 Strategy GetStrategy() const override; | |
| 43 bool IsUnlockAllowed() const override; | 42 bool IsUnlockAllowed() const override; |
| 44 bool IsInRssiRange() const override; | |
| 45 void RecordProximityMetricsOnAuthSuccess() override; | 43 void RecordProximityMetricsOnAuthSuccess() override; |
| 46 void AddObserver(ProximityMonitorObserver* observer) override; | 44 void AddObserver(ProximityMonitorObserver* observer) override; |
| 47 void RemoveObserver(ProximityMonitorObserver* observer) override; | 45 void RemoveObserver(ProximityMonitorObserver* observer) override; |
| 48 | 46 |
| 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: | 47 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. | 48 // Callback for asynchronous initialization of the Bluetooth adpater. |
| 70 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); | 49 void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter); |
| 71 | 50 |
| 72 // Ensures that the app is periodically polling for the proximity status | 51 // 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 | 52 // between the remote and the local device iff it should be, based on the |
| 74 // current app state. | 53 // current app state. |
| 75 void UpdatePollingState(); | 54 void UpdatePollingState(); |
| 76 | 55 |
| 77 // Performs a scheduled |UpdatePollingState()| operation. This method is | 56 // Performs a scheduled |UpdatePollingState()| operation. This method is |
| 78 // used to distinguish periodically scheduled calls to |UpdatePollingState()| | 57 // used to distinguish periodically scheduled calls to |UpdatePollingState()| |
| 79 // from event-driven calls, which should be handled differently. | 58 // from event-driven calls, which should be handled differently. |
| 80 void PerformScheduledUpdatePollingState(); | 59 void PerformScheduledUpdatePollingState(); |
| 81 | 60 |
| 82 // Returns |true| iff the app should be periodically polling for the proximity | 61 // Returns |true| iff the app should be periodically polling for the proximity |
| 83 // status between the remote and the local device. | 62 // status between the remote and the local device. |
| 84 bool ShouldPoll() const; | 63 bool ShouldPoll() const; |
| 85 | 64 |
| 86 // Polls the connection information. | 65 // Polls the connection information. |
| 87 void Poll(); | 66 void Poll(); |
| 88 | 67 |
| 89 // Callback to received the polled-for connection info. | 68 // Callback to received the polled-for connection info. |
| 90 void OnConnectionInfo( | 69 void OnConnectionInfo( |
| 91 const device::BluetoothDevice::ConnectionInfo& connection_info); | 70 const device::BluetoothDevice::ConnectionInfo& connection_info); |
| 92 | 71 |
| 93 // Resets the proximity state to |false|, and clears all member variables | 72 // Resets the proximity state to |false|, and clears all member variables |
| 94 // tracking the proximity state. | 73 // tracking the proximity state. |
| 95 void ClearProximityState(); | 74 void ClearProximityState(); |
| 96 | 75 |
| 97 // Updates the proximity state with a new |connection_info| sample of the | 76 // 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. | 77 // current RSSI. |
| 99 void AddSample( | 78 void AddSample( |
| 100 const device::BluetoothDevice::ConnectionInfo& connection_info); | 79 const device::BluetoothDevice::ConnectionInfo& connection_info); |
| 101 | 80 |
| 102 // Checks whether the proximity state has changed based on the current | 81 // Checks whether the proximity state has changed based on the current |
| 103 // samples. Notifies |observers_| on a change. | 82 // samples. Notifies |observers_| on a change. |
| 104 void CheckForProximityStateChange(); | 83 void CheckForProximityStateChange(); |
| 105 | 84 |
| 106 // The remote device being monitored. | 85 // The current connection being monitored. Not owned and must outlive this |
| 107 const cryptauth::RemoteDevice remote_device_; | 86 // instance. |
| 87 cryptauth::Connection* connection_; |
| 108 | 88 |
| 109 // The observers attached to the ProximityMonitor. | 89 // The observers attached to the ProximityMonitor. |
| 110 base::ObserverList<ProximityMonitorObserver> observers_; | 90 base::ObserverList<ProximityMonitorObserver> observers_; |
| 111 | 91 |
| 112 // The Bluetooth adapter that will be polled for connection info. | 92 // The Bluetooth adapter that will be polled for connection info. |
| 113 scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_; | 93 scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_; |
| 114 | 94 |
| 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 | 95 // Whether the remote device is currently in close proximity to the local |
| 119 // device. | 96 // device. |
| 120 bool remote_device_is_in_proximity_; | 97 bool remote_device_is_in_proximity_; |
| 121 | 98 |
| 122 // Whether the proximity monitor is active, i.e. should possibly be scanning | 99 // Whether the proximity monitor is active, i.e. should possibly be scanning |
| 123 // for proximity to the remote device. | 100 // for proximity to the remote device. |
| 124 bool is_active_; | 101 bool is_active_; |
| 125 | 102 |
| 126 // The exponentailly weighted rolling average of the RSSI, used to smooth the | 103 // 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 | 104 // 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 | 105 // an RSSI reading, or the most recent connection info included an invalid |
| 129 // measurement. | 106 // measurement. |
| 130 std::unique_ptr<double> rssi_rolling_average_; | 107 std::unique_ptr<double> rssi_rolling_average_; |
| 131 | 108 |
| 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 | |
| 138 // 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 | |
| 140 // recently observed an RSSI reading, or the most recent connection info | |
| 141 // included an invalid measurement. | |
| 142 std::unique_ptr<base::TimeTicks> last_zero_rssi_timestamp_; | |
| 143 | |
| 144 // Used to access non-decreasing time measurements. | 109 // Used to access non-decreasing time measurements. |
| 145 std::unique_ptr<base::TickClock> clock_; | 110 std::unique_ptr<base::TickClock> clock_; |
| 146 | 111 |
| 147 // Used to vend weak pointers for polling. Using a separate factory for these | 112 // 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 | 113 // weak pointers allows the weak pointers to be invalidated when polling |
| 149 // stops, which effectively cancels the scheduled tasks. | 114 // stops, which effectively cancels the scheduled tasks. |
| 150 base::WeakPtrFactory<ProximityMonitorImpl> polling_weak_ptr_factory_; | 115 base::WeakPtrFactory<ProximityMonitorImpl> polling_weak_ptr_factory_; |
| 151 | 116 |
| 152 // Used to vend all other weak pointers. | 117 // Used to vend all other weak pointers. |
| 153 base::WeakPtrFactory<ProximityMonitorImpl> weak_ptr_factory_; | 118 base::WeakPtrFactory<ProximityMonitorImpl> weak_ptr_factory_; |
| 154 | 119 |
| 155 DISALLOW_COPY_AND_ASSIGN(ProximityMonitorImpl); | 120 DISALLOW_COPY_AND_ASSIGN(ProximityMonitorImpl); |
| 156 }; | 121 }; |
| 157 | 122 |
| 158 } // namespace proximity_auth | 123 } // namespace proximity_auth |
| 159 | 124 |
| 160 #endif // COMPONENTS_PROXIMITY_AUTH_PROXIMITY_MONITOR_IMPL_H | 125 #endif // COMPONENTS_PROXIMITY_AUTH_PROXIMITY_MONITOR_IMPL_H |
| OLD | NEW |