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 "chrome/common/pref_names.h" | |
| 16 #include "components/prefs/pref_service.h" | |
| 15 #include "components/proximity_auth/logging/logging.h" | 17 #include "components/proximity_auth/logging/logging.h" |
| 16 #include "components/proximity_auth/metrics.h" | 18 #include "components/proximity_auth/metrics.h" |
| 17 #include "components/proximity_auth/proximity_monitor_observer.h" | 19 #include "components/proximity_auth/proximity_monitor_observer.h" |
| 18 #include "device/bluetooth/bluetooth_adapter.h" | 20 #include "device/bluetooth/bluetooth_adapter.h" |
| 19 #include "device/bluetooth/bluetooth_adapter_factory.h" | 21 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 20 | 22 |
| 21 using device::BluetoothDevice; | 23 using device::BluetoothDevice; |
| 22 | 24 |
| 23 namespace proximity_auth { | 25 namespace proximity_auth { |
| 24 | 26 |
| 25 // The time to wait, in milliseconds, between proximity polling iterations. | 27 // The time to wait, in milliseconds, between proximity polling iterations. |
| 26 const int kPollingTimeoutMs = 250; | 28 const int kPollingTimeoutMs = 250; |
| 27 | 29 |
| 28 // The RSSI threshold below which we consider the remote device to not be in | 30 // The RSSI threshold below which we consider the remote device to not be in |
| 29 // proximity. | 31 // proximity. |
| 30 // Note: Because this threshold is unconfigurable right now, it is set quite | 32 // Note: Because this threshold is unconfigurable right now, it is set quite |
| 31 // conservatively in order to avoid blocking users. | 33 // conservatively in order to avoid blocking users. |
| 32 const int kRssiThreshold = -70; | 34 const int kRssiThreshold = -70; |
| 33 | 35 |
| 34 // The weight of the most recent RSSI sample. | 36 // The weight of the most recent RSSI sample. |
| 35 const double kRssiSampleWeight = 0.3; | 37 const double kRssiSampleWeight = 0.3; |
| 36 | 38 |
| 39 // These are arbitrary labels displayed in the settings page for the user | |
| 40 // to select. The correspondence to actual RSSI values is done in the | |
| 41 // GetRssiThresholdFromPrefs(). | |
| 42 enum ProximityThreshold { kVeryClose = 0, kClose = 1, kFar = 2, kVeryFar = 3 }; | |
| 43 | |
| 37 ProximityMonitorImpl::ProximityMonitorImpl( | 44 ProximityMonitorImpl::ProximityMonitorImpl( |
| 38 cryptauth::Connection* connection, | 45 cryptauth::Connection* connection, |
| 39 std::unique_ptr<base::TickClock> clock) | 46 std::unique_ptr<base::TickClock> clock, |
| 47 PrefService* pref_service) | |
| 40 : connection_(connection), | 48 : connection_(connection), |
| 41 remote_device_is_in_proximity_(false), | 49 remote_device_is_in_proximity_(false), |
| 42 is_active_(false), | 50 is_active_(false), |
| 43 clock_(std::move(clock)), | 51 clock_(std::move(clock)), |
| 52 pref_service_(pref_service), | |
| 44 polling_weak_ptr_factory_(this), | 53 polling_weak_ptr_factory_(this), |
| 45 weak_ptr_factory_(this) { | 54 weak_ptr_factory_(this) { |
| 46 if (device::BluetoothAdapterFactory::IsBluetoothSupported()) { | 55 if (device::BluetoothAdapterFactory::IsBluetoothSupported()) { |
| 47 device::BluetoothAdapterFactory::GetAdapter( | 56 device::BluetoothAdapterFactory::GetAdapter( |
| 48 base::Bind(&ProximityMonitorImpl::OnAdapterInitialized, | 57 base::Bind(&ProximityMonitorImpl::OnAdapterInitialized, |
| 49 weak_ptr_factory_.GetWeakPtr())); | 58 weak_ptr_factory_.GetWeakPtr())); |
| 50 } else { | 59 } else { |
| 51 PA_LOG(ERROR) << "[Proximity] Proximity monitoring unavailable: " | 60 PA_LOG(ERROR) << "[Proximity] Proximity monitoring unavailable: " |
| 52 << "Bluetooth is unsupported on this platform."; | 61 << "Bluetooth is unsupported on this platform."; |
| 53 } | 62 } |
| 54 } | 63 } |
| 55 | 64 |
| 56 ProximityMonitorImpl::~ProximityMonitorImpl() { | 65 ProximityMonitorImpl::~ProximityMonitorImpl() { |
| 57 } | 66 } |
| 58 | 67 |
| 59 void ProximityMonitorImpl::Start() { | 68 void ProximityMonitorImpl::Start() { |
| 60 is_active_ = true; | 69 is_active_ = true; |
| 70 GetRssiThresholdFromPrefs(); | |
| 61 UpdatePollingState(); | 71 UpdatePollingState(); |
| 62 } | 72 } |
| 63 | 73 |
| 64 void ProximityMonitorImpl::Stop() { | 74 void ProximityMonitorImpl::Stop() { |
| 65 is_active_ = false; | 75 is_active_ = false; |
| 66 ClearProximityState(); | 76 ClearProximityState(); |
| 67 UpdatePollingState(); | 77 UpdatePollingState(); |
| 68 } | 78 } |
| 69 | 79 |
| 70 bool ProximityMonitorImpl::IsUnlockAllowed() const { | 80 bool ProximityMonitorImpl::IsUnlockAllowed() const { |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 | 209 |
| 200 if (remote_device_is_in_proximity_ != is_now_in_proximity) { | 210 if (remote_device_is_in_proximity_ != is_now_in_proximity) { |
| 201 PA_LOG(INFO) << "[Proximity] Updated proximity state: " | 211 PA_LOG(INFO) << "[Proximity] Updated proximity state: " |
| 202 << (is_now_in_proximity ? "proximate" : "distant"); | 212 << (is_now_in_proximity ? "proximate" : "distant"); |
| 203 remote_device_is_in_proximity_ = is_now_in_proximity; | 213 remote_device_is_in_proximity_ = is_now_in_proximity; |
| 204 for (auto& observer : observers_) | 214 for (auto& observer : observers_) |
| 205 observer.OnProximityStateChanged(); | 215 observer.OnProximityStateChanged(); |
| 206 } | 216 } |
| 207 } | 217 } |
| 208 | 218 |
| 219 void ProximityMonitorImpl::GetRssiThresholdFromPrefs() { | |
| 220 int pref_value = | |
| 221 pref_service_->GetInteger(prefs::kEasyUnlockProximityThreshold); | |
| 222 ProximityThreshold threshold = static_cast<ProximityThreshold>(pref_value); | |
| 223 switch (threshold) { | |
|
Tim Song
2017/07/07 21:48:24
Add a default case as well.
sacomoto
2017/07/10 16:54:07
Done.
| |
| 224 case ProximityThreshold::kVeryClose: | |
| 225 rssi_threshold_ = -55; | |
|
Tim Song
2017/07/07 21:48:24
I would spread out these values out a bit more. Pe
sacomoto
2017/07/10 16:54:07
I spread it a bit more, but I think -30 is really
| |
| 226 return; | |
| 227 case ProximityThreshold::kClose: | |
| 228 rssi_threshold_ = -65; | |
| 229 return; | |
| 230 case ProximityThreshold::kFar: | |
| 231 rssi_threshold_ = -75; | |
| 232 return; | |
| 233 case ProximityThreshold::kVeryFar: | |
| 234 rssi_threshold_ = -85; | |
| 235 return; | |
| 236 } | |
| 237 } | |
| 238 | |
| 209 } // namespace proximity_auth | 239 } // namespace proximity_auth |
| OLD | NEW |