Chromium Code Reviews| 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 |
| 37 ProximityMonitorImpl::ProximityMonitorImpl( | 32 ProximityMonitorImpl::ProximityMonitorImpl( |
| 38 cryptauth::Connection* connection, | 33 cryptauth::Connection* connection, |
| 39 std::unique_ptr<base::TickClock> clock) | 34 std::unique_ptr<base::TickClock> clock, |
| 35 ProximityAuthPrefManager* pref_manager) | |
| 40 : connection_(connection), | 36 : connection_(connection), |
| 41 remote_device_is_in_proximity_(false), | 37 remote_device_is_in_proximity_(false), |
| 42 is_active_(false), | 38 is_active_(false), |
| 43 clock_(std::move(clock)), | 39 clock_(std::move(clock)), |
| 40 pref_manager_(pref_manager), | |
| 44 polling_weak_ptr_factory_(this), | 41 polling_weak_ptr_factory_(this), |
| 45 weak_ptr_factory_(this) { | 42 weak_ptr_factory_(this) { |
| 46 if (device::BluetoothAdapterFactory::IsBluetoothSupported()) { | 43 if (device::BluetoothAdapterFactory::IsBluetoothSupported()) { |
| 47 device::BluetoothAdapterFactory::GetAdapter( | 44 device::BluetoothAdapterFactory::GetAdapter( |
| 48 base::Bind(&ProximityMonitorImpl::OnAdapterInitialized, | 45 base::Bind(&ProximityMonitorImpl::OnAdapterInitialized, |
| 49 weak_ptr_factory_.GetWeakPtr())); | 46 weak_ptr_factory_.GetWeakPtr())); |
| 50 } else { | 47 } else { |
| 51 PA_LOG(ERROR) << "[Proximity] Proximity monitoring unavailable: " | 48 PA_LOG(ERROR) << "[Proximity] Proximity monitoring unavailable: " |
| 52 << "Bluetooth is unsupported on this platform."; | 49 << "Bluetooth is unsupported on this platform."; |
| 53 } | 50 } |
| 54 } | 51 } |
| 55 | 52 |
| 56 ProximityMonitorImpl::~ProximityMonitorImpl() { | 53 ProximityMonitorImpl::~ProximityMonitorImpl() { |
| 57 } | 54 } |
| 58 | 55 |
| 59 void ProximityMonitorImpl::Start() { | 56 void ProximityMonitorImpl::Start() { |
| 60 is_active_ = true; | 57 is_active_ = true; |
| 58 GetRssiThresholdFromPrefs(); | |
| 61 UpdatePollingState(); | 59 UpdatePollingState(); |
| 62 } | 60 } |
| 63 | 61 |
| 64 void ProximityMonitorImpl::Stop() { | 62 void ProximityMonitorImpl::Stop() { |
| 65 is_active_ = false; | 63 is_active_ = false; |
| 66 ClearProximityState(); | 64 ClearProximityState(); |
| 67 UpdatePollingState(); | 65 UpdatePollingState(); |
| 68 } | 66 } |
| 69 | 67 |
| 70 bool ProximityMonitorImpl::IsUnlockAllowed() const { | 68 bool ProximityMonitorImpl::IsUnlockAllowed() const { |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 185 } else { | 183 } else { |
| 186 *rssi_rolling_average_ = | 184 *rssi_rolling_average_ = |
| 187 weight * connection_info.rssi + (1 - weight) * (*rssi_rolling_average_); | 185 weight * connection_info.rssi + (1 - weight) * (*rssi_rolling_average_); |
| 188 } | 186 } |
| 189 | 187 |
| 190 CheckForProximityStateChange(); | 188 CheckForProximityStateChange(); |
| 191 } | 189 } |
| 192 | 190 |
| 193 void ProximityMonitorImpl::CheckForProximityStateChange() { | 191 void ProximityMonitorImpl::CheckForProximityStateChange() { |
| 194 bool is_now_in_proximity = | 192 bool is_now_in_proximity = |
| 195 rssi_rolling_average_ && *rssi_rolling_average_ > kRssiThreshold; | 193 rssi_rolling_average_ && *rssi_rolling_average_ > rssi_threshold_; |
| 196 | 194 |
| 197 if (rssi_rolling_average_) | 195 if (rssi_rolling_average_) |
| 198 PA_LOG(INFO) << " Rolling RSSI: " << *rssi_rolling_average_; | 196 PA_LOG(INFO) << " Rolling RSSI: " << *rssi_rolling_average_; |
| 199 | 197 |
| 200 if (remote_device_is_in_proximity_ != is_now_in_proximity) { | 198 if (remote_device_is_in_proximity_ != is_now_in_proximity) { |
| 201 PA_LOG(INFO) << "[Proximity] Updated proximity state: " | 199 PA_LOG(INFO) << "[Proximity] Updated proximity state: " |
| 202 << (is_now_in_proximity ? "proximate" : "distant"); | 200 << (is_now_in_proximity ? "proximate" : "distant"); |
| 203 remote_device_is_in_proximity_ = is_now_in_proximity; | 201 remote_device_is_in_proximity_ = is_now_in_proximity; |
| 204 for (auto& observer : observers_) | 202 for (auto& observer : observers_) |
| 205 observer.OnProximityStateChanged(); | 203 observer.OnProximityStateChanged(); |
| 206 } | 204 } |
| 207 } | 205 } |
| 208 | 206 |
| 207 void ProximityMonitorImpl::GetRssiThresholdFromPrefs() { | |
| 208 ProximityAuthPrefManager::ProximityThreshold threshold = | |
| 209 pref_manager_->GetProximityThreshold(); | |
| 210 switch (threshold) { | |
| 211 case ProximityAuthPrefManager::ProximityThreshold::kVeryClose: | |
| 212 rssi_threshold_ = -45; | |
| 213 return; | |
| 214 case ProximityAuthPrefManager::ProximityThreshold::kClose: | |
| 215 rssi_threshold_ = -60; | |
| 216 return; | |
| 217 case ProximityAuthPrefManager::ProximityThreshold::kFar: | |
| 218 rssi_threshold_ = -70; | |
| 219 return; | |
| 220 case ProximityAuthPrefManager::ProximityThreshold::kVeryFar: | |
| 221 rssi_threshold_ = -85; | |
| 222 return; | |
| 223 default: | |
|
battre
2017/07/12 07:18:47
I would suggest to drop the default block. The com
sacomoto
2017/07/12 15:52:53
Done.
| |
| 224 PA_LOG(WARNING) << "[Proximity] Unmapped proximityThreshold: " | |
| 225 << threshold; | |
| 226 rssi_threshold_ = -70; | |
| 227 return; | |
| 228 } | |
| 229 } | |
| 230 | |
| 209 } // namespace proximity_auth | 231 } // namespace proximity_auth |
| OLD | NEW |