policy_recommendation_restorer.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2018 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_POLICY_POLICY_RECOMMENDATION_RESTORER_H_
  5. #define ASH_POLICY_POLICY_RECOMMENDATION_RESTORER_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include "ash/public/cpp/session/session_observer.h"
  10. #include "base/timer/timer.h"
  11. #include "ui/base/user_activity/user_activity_observer.h"
  12. class PrefChangeRegistrar;
  13. class PrefService;
  14. namespace ash {
  15. // Manages observing a set of prefs on signin screen. If any of the prefs has a
  16. // recommended value on observing, or changed to have recommended value or
  17. // active user session started, its user settings is cleared so that the
  18. // recommendation can take effect. On signin screen prefs, user settings are
  19. // cleared when the user becomes idle for one minute.
  20. //
  21. // The above efforts are made to ensure that the observed prefs are *policy*
  22. // overridden and can be restored properly. For example, a demo device on a
  23. // store shelf. One customer walks up to device and enables some a11y features,
  24. // leaving the device in a "funny" state (high contrast, screen magnifier,
  25. // spoken feedback enabled). After some time, another customer won't feel the
  26. // device looks "broken".
  27. class PolicyRecommendationRestorer : public SessionObserver,
  28. public ui::UserActivityObserver {
  29. public:
  30. PolicyRecommendationRestorer();
  31. PolicyRecommendationRestorer(const PolicyRecommendationRestorer&) = delete;
  32. PolicyRecommendationRestorer& operator=(const PolicyRecommendationRestorer&) =
  33. delete;
  34. ~PolicyRecommendationRestorer() override;
  35. // Caller calls to start observing recommended value for |pref_name|. It
  36. // should be called when signin pref service is connected but before
  37. // observing/loading user settings for |pref_name|.
  38. void ObservePref(const std::string& pref_name);
  39. // SessionObserver:
  40. void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
  41. // ui::UserActivityObserver:
  42. void OnUserActivity(const ui::Event* event) override;
  43. void DisableForTesting();
  44. base::OneShotTimer* restore_timer_for_test() { return &restore_timer_; }
  45. private:
  46. // If a recommended value and a user setting exist for |pref_name|, clears the
  47. // user setting so that the recommended value can take effect. If
  48. // |allow_delay| is |true| and user prefs not started yet, a timer is started
  49. // that will clear the setting when the user becomes idle for one minute.
  50. // Otherwise, the setting is cleared immediately.
  51. void Restore(bool allow_delay, const std::string& pref_name);
  52. void RestoreAll();
  53. void StartTimer();
  54. void StopTimer();
  55. std::set<std::string> pref_names_;
  56. std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
  57. bool active_user_pref_connected_ = false;
  58. base::OneShotTimer restore_timer_;
  59. bool disabled_for_testing_ = false;
  60. };
  61. } // namespace ash
  62. #endif // ASH_POLICY_POLICY_RECOMMENDATION_RESTORER_H_