proximity_monitor_impl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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
  3. // found in the LICENSE file.
  4. #ifndef ASH_COMPONENTS_PROXIMITY_AUTH_PROXIMITY_MONITOR_IMPL_H_
  5. #define ASH_COMPONENTS_PROXIMITY_AUTH_PROXIMITY_MONITOR_IMPL_H_
  6. #include <memory>
  7. #include "ash/components/multidevice/remote_device_ref.h"
  8. #include "ash/components/proximity_auth/proximity_monitor.h"
  9. #include "ash/services/secure_channel/public/mojom/secure_channel.mojom.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "device/bluetooth/bluetooth_device.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace ash {
  14. namespace secure_channel {
  15. class ClientChannel;
  16. }
  17. } // namespace ash
  18. namespace device {
  19. class BluetoothAdapter;
  20. } // namespace device
  21. namespace proximity_auth {
  22. // The concrete implemenation of the proximity monitor interface.
  23. class ProximityMonitorImpl : public ProximityMonitor {
  24. public:
  25. // The |connection| is not owned, and must outlive |this| instance.
  26. ProximityMonitorImpl(ash::multidevice::RemoteDeviceRef remote_device,
  27. ash::secure_channel::ClientChannel* channel);
  28. ProximityMonitorImpl(const ProximityMonitorImpl&) = delete;
  29. ProximityMonitorImpl& operator=(const ProximityMonitorImpl&) = delete;
  30. ~ProximityMonitorImpl() override;
  31. // ProximityMonitor:
  32. void Start() override;
  33. void Stop() override;
  34. bool IsUnlockAllowed() const override;
  35. void RecordProximityMetricsOnAuthSuccess() override;
  36. private:
  37. // Callback for asynchronous initialization of the Bluetooth adpater.
  38. void OnAdapterInitialized(scoped_refptr<device::BluetoothAdapter> adapter);
  39. // Ensures that the app is periodically polling for the proximity status
  40. // between the remote and the local device iff it should be, based on the
  41. // current app state.
  42. void UpdatePollingState();
  43. // Performs a scheduled |UpdatePollingState()| operation. This method is
  44. // used to distinguish periodically scheduled calls to |UpdatePollingState()|
  45. // from event-driven calls, which should be handled differently.
  46. void PerformScheduledUpdatePollingState();
  47. // Returns |true| iff the app should be periodically polling for the proximity
  48. // status between the remote and the local device.
  49. bool ShouldPoll() const;
  50. // Polls the connection information.
  51. void Poll();
  52. void OnGetConnectionMetadata(
  53. ash::secure_channel::mojom::ConnectionMetadataPtr connection_metadata);
  54. void OnGetRssi(const absl::optional<int32_t>& rssi);
  55. // Resets the proximity state to |false|, and clears all member variables
  56. // tracking the proximity state.
  57. void ClearProximityState();
  58. // Updates the proximity state with a new sample of the current RSSI.
  59. void AddSample(int32_t rssi);
  60. // Checks whether the proximity state has changed based on the current
  61. // samples. Notifies observers on a change.
  62. void CheckForProximityStateChange();
  63. // Used to get the name of the remote device that ProximitMonitor is
  64. // communicating with, for metrics purposes.
  65. ash::multidevice::RemoteDeviceRef remote_device_;
  66. // Used to communicate with the remote device to gauge its proximity via RSSI
  67. // measurement.
  68. ash::secure_channel::ClientChannel* channel_;
  69. // The Bluetooth adapter that will be polled for connection info.
  70. scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
  71. // Whether the remote device is currently in close proximity to the local
  72. // device.
  73. bool remote_device_is_in_proximity_;
  74. // Whether the proximity monitor is active, i.e. should possibly be scanning
  75. // for proximity to the remote device.
  76. bool is_active_;
  77. // The exponentailly weighted rolling average of the RSSI, used to smooth the
  78. // RSSI readings. Null if the monitor is inactive, has not recently observed
  79. // an RSSI reading, or the most recent connection info included an invalid
  80. // measurement.
  81. std::unique_ptr<double> rssi_rolling_average_;
  82. // Used to vend weak pointers for polling. Using a separate factory for these
  83. // weak pointers allows the weak pointers to be invalidated when polling
  84. // stops, which effectively cancels the scheduled tasks.
  85. base::WeakPtrFactory<ProximityMonitorImpl> polling_weak_ptr_factory_{this};
  86. // Used to vend all other weak pointers.
  87. base::WeakPtrFactory<ProximityMonitorImpl> weak_ptr_factory_{this};
  88. };
  89. } // namespace proximity_auth
  90. #endif // ASH_COMPONENTS_PROXIMITY_AUTH_PROXIMITY_MONITOR_IMPL_H_