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