snooping_protection_notification_blocker.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_NOTIFICATION_BLOCKER_H_
  5. #define ASH_SYSTEM_HUMAN_PRESENCE_SNOOPING_PROTECTION_NOTIFICATION_BLOCKER_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/public/cpp/session/session_controller.h"
  8. #include "ash/public/cpp/session/session_observer.h"
  9. #include "ash/system/human_presence/snooping_protection_controller.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/scoped_observation.h"
  12. #include "ui/message_center/message_center.h"
  13. #include "ui/message_center/message_center_observer.h"
  14. #include "ui/message_center/notification_blocker.h"
  15. class PrefService;
  16. class PrefChangeRegistrar;
  17. namespace message_center {
  18. class MessageCenter;
  19. class Notification;
  20. } // namespace message_center
  21. namespace ash {
  22. // A notification blocker that suppresses popup notifications when the HPS
  23. // service detects a person looking over the user's shoulder.
  24. //
  25. // Also manages a popup notification informing the user of which popups have
  26. // been blocked.
  27. //
  28. // TODO(crbug.com/1241706): make this naming less opaque. Currently using "HPS
  29. // notify" because it was the feature name early in development, but paths /
  30. // identifiers will be renamed in one fell swoop.
  31. class ASH_EXPORT SnoopingProtectionNotificationBlocker
  32. : public SessionObserver,
  33. public message_center::NotificationBlocker,
  34. public message_center::NotificationObserver,
  35. public SnoopingProtectionController::Observer,
  36. public message_center::MessageCenterObserver {
  37. public:
  38. // The ID of the informational popup.
  39. static constexpr char kInfoNotificationId[] = "hps-notify-info";
  40. SnoopingProtectionNotificationBlocker(
  41. message_center::MessageCenter* message_center,
  42. SnoopingProtectionController* controller);
  43. SnoopingProtectionNotificationBlocker(
  44. const SnoopingProtectionNotificationBlocker&) = delete;
  45. SnoopingProtectionNotificationBlocker& operator=(
  46. const SnoopingProtectionNotificationBlocker&) = delete;
  47. ~SnoopingProtectionNotificationBlocker() override;
  48. // SessionObserver:
  49. void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
  50. // message_center::NotificationBlocker:
  51. bool ShouldShowNotificationAsPopup(
  52. const message_center::Notification& notification) const override;
  53. // SnoopingProtectionController::Observer:
  54. void OnSnoopingStatusChanged(bool snooper) override;
  55. void OnSnoopingProtectionControllerDestroyed() override;
  56. // message_center::MessageCenterObserver:
  57. void OnNotificationAdded(const std::string& notification_id) override;
  58. void OnNotificationRemoved(const std::string& notification_id,
  59. bool by_user) override;
  60. void OnNotificationUpdated(const std::string& notification_id) override;
  61. void OnBlockingStateChanged(
  62. message_center::NotificationBlocker* blocker) override;
  63. // message_center::NotificationObserver:
  64. void Close(bool by_user) override;
  65. void Click(const absl::optional<int>& button_index,
  66. const absl::optional<std::u16string>& reply) override;
  67. private:
  68. // Starts or stops blocking and showing the info notification based on the
  69. // the snooping state and the user's preferences.
  70. void OnBlockingActiveChanged();
  71. // Called when prefs::kSnoopingProtectionNotificationSuppressionEnabled
  72. // changes its value.
  73. void OnBlockingPrefChanged();
  74. // Returns true if we are currently blocking notifications that aren't
  75. // explicit exceptions.
  76. bool BlockingActive() const;
  77. // Updates the visibility and contents of the info notification if necessary.
  78. void UpdateInfoNotificationIfNecessary();
  79. // Creates a new info notification with a message dependent on the set of
  80. // currently-blocked notifications.
  81. std::unique_ptr<message_center::Notification> CreateInfoNotification() const;
  82. message_center::MessageCenter* const message_center_;
  83. SnoopingProtectionController* const controller_;
  84. bool info_popup_exists_ = false;
  85. // The set of popups we are currently blocking.
  86. std::set<std::string> blocked_popups_;
  87. base::ScopedObservation<SessionController, SessionObserver>
  88. session_observation_{this};
  89. base::ScopedObservation<SnoopingProtectionController,
  90. SnoopingProtectionController::Observer>
  91. controller_observation_{this};
  92. base::ScopedObservation<message_center::MessageCenter,
  93. message_center::MessageCenterObserver>
  94. message_center_observation_{this};
  95. std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
  96. // Must be last.
  97. base::WeakPtrFactory<SnoopingProtectionNotificationBlocker> weak_ptr_factory_{
  98. this};
  99. };
  100. } // namespace ash
  101. #endif // ASH_SYSTEM_HUMAN_PRESENCE_SNOOPING_PROTECTION_NOTIFICATION_BLOCKER_H_