1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85

ash / policy / policy_recommendation_restorer.h [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_POLICY_POLICY_RECOMMENDATION_RESTORER_H_
#define ASH_POLICY_POLICY_RECOMMENDATION_RESTORER_H_

#include <memory>
#include <set>
#include <string>

#include "ash/public/cpp/session/session_observer.h"
#include "base/timer/timer.h"
#include "ui/base/user_activity/user_activity_observer.h"

class PrefChangeRegistrar;
class PrefService;

namespace ash {

// Manages observing a set of prefs on signin screen. If any of the prefs has a
// recommended value on observing, or changed to have recommended value or
// active user session started, its user settings is cleared so that the
// recommendation can take effect. On signin screen prefs, user settings are
// cleared when the user becomes idle for one minute.
//
// The above efforts are made to ensure that the observed prefs are *policy*
// overridden and can be restored properly. For example, a demo device on a
// store shelf. One customer walks up to device and enables some a11y features,
// leaving the device in a "funny" state (high contrast, screen magnifier,
// spoken feedback enabled). After some time, another customer won't feel the
// device looks "broken".
class PolicyRecommendationRestorer : public SessionObserver,
                                     public ui::UserActivityObserver {
 public:
  PolicyRecommendationRestorer();

  PolicyRecommendationRestorer(const PolicyRecommendationRestorer&) = delete;
  PolicyRecommendationRestorer& operator=(const PolicyRecommendationRestorer&) =
      delete;

  ~PolicyRecommendationRestorer() override;

  // Caller calls to start observing recommended value for |pref_name|. It
  // should be called when signin pref service is connected but before
  // observing/loading user settings for |pref_name|.
  void ObservePref(const std::string& pref_name);

  // SessionObserver:
  void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;

  // ui::UserActivityObserver:
  void OnUserActivity(const ui::Event* event) override;

  void DisableForTesting();

  base::OneShotTimer* restore_timer_for_test() { return &restore_timer_; }

 private:
  // If a recommended value and a user setting exist for |pref_name|, clears the
  // user setting so that the recommended value can take effect. If
  // |allow_delay| is |true| and user prefs not started yet, a timer is started
  // that will clear the setting when the user becomes idle for one minute.
  // Otherwise, the setting is cleared immediately.
  void Restore(bool allow_delay, const std::string& pref_name);

  void RestoreAll();

  void StartTimer();
  void StopTimer();

  std::set<std::string> pref_names_;

  std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;

  bool active_user_pref_connected_ = false;

  base::OneShotTimer restore_timer_;

  bool disabled_for_testing_ = false;
};

}  // namespace ash

#endif  // ASH_POLICY_POLICY_RECOMMENDATION_RESTORER_H_