privacy_screen_controller.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2020 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_DISPLAY_PRIVACY_SCREEN_CONTROLLER_H_
  5. #define ASH_DISPLAY_PRIVACY_SCREEN_CONTROLLER_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ash/public/cpp/privacy_screen_dlp_helper.h"
  9. #include "ash/public/cpp/session/session_observer.h"
  10. #include "base/observer_list.h"
  11. #include "ui/display/manager/display_configurator.h"
  12. class PrefRegistrySimple;
  13. class PrefService;
  14. class PrefChangeRegistrar;
  15. namespace ash {
  16. // Controls the privacy screen feature.
  17. class ASH_EXPORT PrivacyScreenController
  18. : public PrivacyScreenDlpHelper,
  19. public SessionObserver,
  20. display::DisplayConfigurator::Observer {
  21. public:
  22. class Observer : public base::CheckedObserver {
  23. public:
  24. // Called when the privacy screen setting is changed.
  25. virtual void OnPrivacyScreenSettingChanged(bool enabled, bool notify_ui) {}
  26. protected:
  27. ~Observer() override = default;
  28. };
  29. // The UI surface from which the privacy screen is toggled on/off. Keep in
  30. // sync with PrivacyScreenToggleUISurface in
  31. // tools/metrics/histograms/enums.xml.
  32. enum ToggleUISurface {
  33. kToggleUISurfaceKeyboardShortcut,
  34. kToggleUISurfaceFeaturePod,
  35. kToggleUISurfaceToastButton,
  36. // Must be last.
  37. kToggleUISurfaceCount,
  38. };
  39. PrivacyScreenController();
  40. ~PrivacyScreenController() override;
  41. PrivacyScreenController(const PrivacyScreenController&) = delete;
  42. PrivacyScreenController& operator=(const PrivacyScreenController&) = delete;
  43. static void RegisterProfilePrefs(PrefRegistrySimple* registry);
  44. // Whether or not the privacy screen feature is enforced by policy.
  45. bool IsManaged() const;
  46. // Get the PrivacyScreen settings stored in the current active user prefs.
  47. bool GetEnabled() const;
  48. // Set the desired PrivacyScreen settings in the current active user prefs.
  49. void SetEnabled(bool enabled, ToggleUISurface ui_surface);
  50. void AddObserver(Observer* observer);
  51. void RemoveObserver(Observer* observer);
  52. // PrivacyScreenDlpHelper:
  53. bool IsSupported() const override;
  54. void SetEnforced(bool enforced) override;
  55. // SessionObserver:
  56. void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
  57. void OnSigninScreenPrefServiceInitialized(PrefService* pref_service) override;
  58. // DisplayConfigurator::Observer:
  59. void OnDisplayModeChanged(
  60. const std::vector<display::DisplaySnapshot*>& displays) override;
  61. private:
  62. // Calculates PrivacyScreen's logical status.
  63. bool CalculateCurrentStatus() const;
  64. // Called when the user pref or DLP enforcement for the state of PrivacyScreen
  65. // is changed.
  66. void OnStateChanged(bool notify_observers);
  67. // Called when GPU/DRM is done setting the privacy screen panel to
  68. // |requested_config|.
  69. void OnSetPrivacyScreenComplete(bool from_user_pref_init,
  70. bool requested_config,
  71. bool success);
  72. // Called when a change to |active_user_pref_service_| is detected (i.e. when
  73. // OnActiveUserPrefServiceChanged() is called.
  74. void InitFromUserPrefs();
  75. // Retrieves the current user's PrivacyScreen preference.
  76. bool GetStateFromActiveUserPreference() const;
  77. // Whether or not to alert observers about PrivacyScreen state change.
  78. bool ShouldNotifyObservers(bool from_user_pref_init) const;
  79. // The pref service of the currently active user. Can be null in
  80. // ash_unittests.
  81. PrefService* active_user_pref_service_ = nullptr;
  82. // Set to true when entering the login screen. This should happen once per
  83. // Chrome restart.
  84. bool applying_login_screen_prefs_ = false;
  85. // Indicates if the PrivacyScreen is currently on or off.
  86. bool current_status_ = false;
  87. // Indicates whether PrivacyScreen is enforced by Data Leak Protection
  88. // feature.
  89. bool dlp_enforced_ = false;
  90. base::ObserverList<Observer> observers_;
  91. // The registrar used to watch privacy screen prefs changes in the above
  92. // |active_user_pref_service_| from outside ash.
  93. // NOTE: Prefs are how Chrome communicates changes to the
  94. // PrivacyScreenController settings controlled by this class from the WebUI
  95. // settings.
  96. std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
  97. // This must be the last variable.
  98. base::WeakPtrFactory<PrivacyScreenController> weak_ptr_factory_{this};
  99. };
  100. } // namespace ash
  101. #endif // ASH_DISPLAY_PRIVACY_SCREEN_CONTROLLER_H_