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 #include "components/proximity_auth/proximity_monitor_impl.h" | 5 #include "components/proximity_auth/proximity_monitor_impl.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "base/time/tick_clock.h" | 13 #include "base/time/tick_clock.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "components/proximity_auth/logging/logging.h" | 15 #include "components/proximity_auth/logging/logging.h" |
16 #include "components/proximity_auth/metrics.h" | 16 #include "components/proximity_auth/metrics.h" |
| 17 #include "components/proximity_auth/proximity_auth_pref_manager.h" |
17 #include "components/proximity_auth/proximity_monitor_observer.h" | 18 #include "components/proximity_auth/proximity_monitor_observer.h" |
18 #include "device/bluetooth/bluetooth_adapter.h" | 19 #include "device/bluetooth/bluetooth_adapter.h" |
19 #include "device/bluetooth/bluetooth_adapter_factory.h" | 20 #include "device/bluetooth/bluetooth_adapter_factory.h" |
20 | 21 |
21 using device::BluetoothDevice; | 22 using device::BluetoothDevice; |
22 | 23 |
23 namespace proximity_auth { | 24 namespace proximity_auth { |
24 | 25 |
25 // The time to wait, in milliseconds, between proximity polling iterations. | 26 // The time to wait, in milliseconds, between proximity polling iterations. |
26 const int kPollingTimeoutMs = 250; | 27 const int kPollingTimeoutMs = 250; |
27 | 28 |
28 // The RSSI threshold below which we consider the remote device to not be in | |
29 // proximity. | |
30 // Note: Because this threshold is unconfigurable right now, it is set quite | |
31 // conservatively in order to avoid blocking users. | |
32 const int kRssiThreshold = -70; | |
33 | |
34 // The weight of the most recent RSSI sample. | 29 // The weight of the most recent RSSI sample. |
35 const double kRssiSampleWeight = 0.3; | 30 const double kRssiSampleWeight = 0.3; |
36 | 31 |
| 32 // The default RSSI threshold. |
| 33 const int kDefaultRssiThreshold = -70; |
| 34 |
37 ProximityMonitorImpl::ProximityMonitorImpl( | 35 ProximityMonitorImpl::ProximityMonitorImpl( |
38 cryptauth::Connection* connection, | 36 cryptauth::Connection* connection, |
39 std::unique_ptr<base::TickClock> clock) | 37 std::unique_ptr<base::TickClock> clock, |
| 38 ProximityAuthPrefManager* pref_manager) |
40 : connection_(connection), | 39 : connection_(connection), |
41 remote_device_is_in_proximity_(false), | 40 remote_device_is_in_proximity_(false), |
42 is_active_(false), | 41 is_active_(false), |
| 42 rssi_threshold_(kDefaultRssiThreshold), |
43 clock_(std::move(clock)), | 43 clock_(std::move(clock)), |
| 44 pref_manager_(pref_manager), |
44 polling_weak_ptr_factory_(this), | 45 polling_weak_ptr_factory_(this), |
45 weak_ptr_factory_(this) { | 46 weak_ptr_factory_(this) { |
46 if (device::BluetoothAdapterFactory::IsBluetoothSupported()) { | 47 if (device::BluetoothAdapterFactory::IsBluetoothSupported()) { |
47 device::BluetoothAdapterFactory::GetAdapter( | 48 device::BluetoothAdapterFactory::GetAdapter( |
48 base::Bind(&ProximityMonitorImpl::OnAdapterInitialized, | 49 base::Bind(&ProximityMonitorImpl::OnAdapterInitialized, |
49 weak_ptr_factory_.GetWeakPtr())); | 50 weak_ptr_factory_.GetWeakPtr())); |
50 } else { | 51 } else { |
51 PA_LOG(ERROR) << "[Proximity] Proximity monitoring unavailable: " | 52 PA_LOG(ERROR) << "[Proximity] Proximity monitoring unavailable: " |
52 << "Bluetooth is unsupported on this platform."; | 53 << "Bluetooth is unsupported on this platform."; |
53 } | 54 } |
54 } | 55 } |
55 | 56 |
56 ProximityMonitorImpl::~ProximityMonitorImpl() { | 57 ProximityMonitorImpl::~ProximityMonitorImpl() { |
57 } | 58 } |
58 | 59 |
59 void ProximityMonitorImpl::Start() { | 60 void ProximityMonitorImpl::Start() { |
60 is_active_ = true; | 61 is_active_ = true; |
| 62 GetRssiThresholdFromPrefs(); |
61 UpdatePollingState(); | 63 UpdatePollingState(); |
62 } | 64 } |
63 | 65 |
64 void ProximityMonitorImpl::Stop() { | 66 void ProximityMonitorImpl::Stop() { |
65 is_active_ = false; | 67 is_active_ = false; |
66 ClearProximityState(); | 68 ClearProximityState(); |
67 UpdatePollingState(); | 69 UpdatePollingState(); |
68 } | 70 } |
69 | 71 |
70 bool ProximityMonitorImpl::IsUnlockAllowed() const { | 72 bool ProximityMonitorImpl::IsUnlockAllowed() const { |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 } else { | 187 } else { |
186 *rssi_rolling_average_ = | 188 *rssi_rolling_average_ = |
187 weight * connection_info.rssi + (1 - weight) * (*rssi_rolling_average_); | 189 weight * connection_info.rssi + (1 - weight) * (*rssi_rolling_average_); |
188 } | 190 } |
189 | 191 |
190 CheckForProximityStateChange(); | 192 CheckForProximityStateChange(); |
191 } | 193 } |
192 | 194 |
193 void ProximityMonitorImpl::CheckForProximityStateChange() { | 195 void ProximityMonitorImpl::CheckForProximityStateChange() { |
194 bool is_now_in_proximity = | 196 bool is_now_in_proximity = |
195 rssi_rolling_average_ && *rssi_rolling_average_ > kRssiThreshold; | 197 rssi_rolling_average_ && *rssi_rolling_average_ > rssi_threshold_; |
196 | 198 |
197 if (rssi_rolling_average_) | 199 if (rssi_rolling_average_) |
198 PA_LOG(INFO) << " Rolling RSSI: " << *rssi_rolling_average_; | 200 PA_LOG(INFO) << " Rolling RSSI: " << *rssi_rolling_average_; |
199 | 201 |
200 if (remote_device_is_in_proximity_ != is_now_in_proximity) { | 202 if (remote_device_is_in_proximity_ != is_now_in_proximity) { |
201 PA_LOG(INFO) << "[Proximity] Updated proximity state: " | 203 PA_LOG(INFO) << "[Proximity] Updated proximity state: " |
202 << (is_now_in_proximity ? "proximate" : "distant"); | 204 << (is_now_in_proximity ? "proximate" : "distant"); |
203 remote_device_is_in_proximity_ = is_now_in_proximity; | 205 remote_device_is_in_proximity_ = is_now_in_proximity; |
204 for (auto& observer : observers_) | 206 for (auto& observer : observers_) |
205 observer.OnProximityStateChanged(); | 207 observer.OnProximityStateChanged(); |
206 } | 208 } |
207 } | 209 } |
208 | 210 |
| 211 void ProximityMonitorImpl::GetRssiThresholdFromPrefs() { |
| 212 ProximityAuthPrefManager::ProximityThreshold threshold = |
| 213 pref_manager_->GetProximityThreshold(); |
| 214 switch (threshold) { |
| 215 case ProximityAuthPrefManager::ProximityThreshold::kVeryClose: |
| 216 rssi_threshold_ = -45; |
| 217 return; |
| 218 case ProximityAuthPrefManager::ProximityThreshold::kClose: |
| 219 rssi_threshold_ = -60; |
| 220 return; |
| 221 case ProximityAuthPrefManager::ProximityThreshold::kFar: |
| 222 rssi_threshold_ = -70; |
| 223 return; |
| 224 case ProximityAuthPrefManager::ProximityThreshold::kVeryFar: |
| 225 rssi_threshold_ = -85; |
| 226 return; |
| 227 } |
| 228 NOTREACHED(); |
| 229 } |
| 230 |
209 } // namespace proximity_auth | 231 } // namespace proximity_auth |
OLD | NEW |