snooping_protection_controller.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2021 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_SYSTEM_HUMAN_PRESENCE_SNOOPING_PROTECTION_CONTROLLER_H_
  5. #define ASH_SYSTEM_HUMAN_PRESENCE_SNOOPING_PROTECTION_CONTROLLER_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ash/public/cpp/session/session_controller.h"
  9. #include "ash/public/cpp/session/session_observer.h"
  10. #include "ash/system/human_presence/human_presence_orientation_controller.h"
  11. #include "base/observer_list.h"
  12. #include "base/observer_list_types.h"
  13. #include "base/scoped_observation.h"
  14. #include "base/time/time.h"
  15. #include "base/timer/timer.h"
  16. #include "chromeos/ash/components/dbus/hps/hps_service.pb.h"
  17. #include "chromeos/ash/components/dbus/human_presence/human_presence_dbus_client.h"
  18. #include "components/session_manager/session_manager_types.h"
  19. #include "third_party/abseil-cpp/absl/types/optional.h"
  20. class PrefChangeRegistrar;
  21. class PrefRegistrySimple;
  22. namespace ash {
  23. class SnoopingProtectionNotificationBlocker;
  24. // Pushes status changes to the snooping protection icon and notification
  25. // blocker based on DBus state, preferences and session type.
  26. class ASH_EXPORT SnoopingProtectionController
  27. : public SessionObserver,
  28. public HumanPresenceOrientationController::Observer,
  29. public HumanPresenceDBusClient::Observer {
  30. public:
  31. class Observer : public base::CheckedObserver {
  32. public:
  33. ~Observer() override = default;
  34. // Called when an observer should show or hide itself because the snooping
  35. // status has changed. Argument is true if a snooper has now been detected.
  36. virtual void OnSnoopingStatusChanged(bool snooper) = 0;
  37. // Used to coordinate observers that might outlive the controller.
  38. virtual void OnSnoopingProtectionControllerDestroyed() = 0;
  39. };
  40. SnoopingProtectionController();
  41. SnoopingProtectionController(const SnoopingProtectionController&) = delete;
  42. SnoopingProtectionController& operator=(const SnoopingProtectionController&) =
  43. delete;
  44. ~SnoopingProtectionController() override;
  45. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  46. // SessionObserver:
  47. void OnSessionStateChanged(session_manager::SessionState state) override;
  48. void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
  49. // HumanPresenceOrientationObserver:
  50. void OnOrientationChanged(bool suitable_for_human_presence) override;
  51. // HumanPresenceDBusClient::Observer:
  52. void OnHpsSenseChanged(const hps::HpsResultProto&) override;
  53. void OnHpsNotifyChanged(const hps::HpsResultProto&) override;
  54. void OnRestart() override;
  55. void OnShutdown() override;
  56. // Add/remove views that are listening for snooper presence.
  57. void AddObserver(Observer* observer);
  58. void RemoveObserver(Observer* observer);
  59. // The current snooper status.
  60. bool SnooperPresent() const;
  61. private:
  62. // Used to track whether a signal should actually trigger a visibility change.
  63. struct State {
  64. // Whether a snooper is present, as last reported by the service.
  65. bool present = false;
  66. // Whether there is an active user session ongoing.
  67. bool session_active = false;
  68. // Whether the user has enabled the feature via preferences.
  69. bool pref_enabled = false;
  70. // Whether the daemon is available for communication.
  71. bool service_available = false;
  72. // Whether the daemon has been successfully configured.
  73. bool service_configured = false;
  74. // Whether the device is in physical orientation where our models are
  75. // accurate.
  76. bool orientation_suitable = false;
  77. // Whether we are within the minimum time window for which to report a
  78. // positive result.
  79. bool within_pos_window = false;
  80. };
  81. // Updates snooper state as appropriate given the signal, session,
  82. // preference and device orientation state. If changed, notifies observers.
  83. void UpdateSnooperStatus(const State& new_state);
  84. // Requests the start or stop of the snooping signal, so that the daemon need
  85. // not be running snooping logic while the user has the feature disabled.
  86. // Also updates the new state of service availability.
  87. void ReconfigureService(State* new_state);
  88. // Configures the daemon, polls its initial state and opts into its signals.
  89. void StartServiceObservation(bool service_is_available);
  90. // Performs the state update from the daemon response.
  91. void UpdateServiceState(absl::optional<hps::HpsResultProto> result);
  92. // A callback to update visibility when the user enables or disables the
  93. // feature.
  94. void UpdatePrefState();
  95. // A callback that fires once a positive signal has been emitted for the
  96. // minimum allowed time. Sends out any delayed updates to observers.
  97. void OnMinWindowExpired();
  98. // Logs the amount of time a snooper was present / absent.
  99. // An initial call just caches the current time (and doesn't log anything).
  100. // Subsequent calls update the cache and log time elapsed if snooping status
  101. // has changed.
  102. // The cache can be cleared by resetting `last_presence_report_time_`.
  103. void LogPresenceWindow(bool is_present);
  104. // The state of all signals relevant to snooping status.
  105. State state_;
  106. // Used to enforce a minimum window of positive results.
  107. base::RetainingOneShotTimer pos_window_timer_;
  108. base::ScopedObservation<SessionController, SessionObserver>
  109. session_observation_{this};
  110. base::ScopedObservation<HumanPresenceOrientationController,
  111. HumanPresenceOrientationController::Observer>
  112. orientation_observation_{this};
  113. base::ScopedObservation<HumanPresenceDBusClient,
  114. HumanPresenceDBusClient::Observer>
  115. human_presence_dbus_observation_{this};
  116. // Used to notify ourselves of changes to the pref that enables / disables
  117. // this feature.
  118. std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
  119. // Clients listening for snooping status changes.
  120. base::ObserverList<Observer> observers_;
  121. // Controls popup hiding and our info notification.
  122. const std::unique_ptr<SnoopingProtectionNotificationBlocker>
  123. notification_blocker_;
  124. // The minimum amount of time between emitting an initial positive signal and
  125. // then a subsequent negative one.
  126. const base::TimeDelta pos_window_;
  127. // Last time the snooper became present or absent.
  128. base::TimeTicks last_presence_report_time_;
  129. // Must be last.
  130. base::WeakPtrFactory<SnoopingProtectionController> weak_ptr_factory_{this};
  131. };
  132. } // namespace ash
  133. #endif // ASH_SYSTEM_HUMAN_PRESENCE_SNOOPING_PROTECTION_CONTROLLER_H_