1 // Copyright (c) 2012 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 CHROME_BROWSER_CHROMEOS_PREFERENCES_H_
6 #define CHROME_BROWSER_CHROMEOS_PREFERENCES_H_
7 
8 #include <string>
9 
10 #include "ash/public/mojom/cros_display_config.mojom.h"
11 #include "base/macros.h"
12 #include "chrome/browser/chromeos/language_preferences.h"
13 #include "components/prefs/pref_change_registrar.h"
14 #include "components/prefs/pref_member.h"
15 #include "components/sync_preferences/pref_service_syncable_observer.h"
16 #include "components/user_manager/user_manager.h"
17 #include "mojo/public/cpp/bindings/remote.h"
18 #include "ui/base/ime/chromeos/input_method_manager.h"
19 
20 class PrefRegistrySimple;
21 class TracingManager;
22 
23 namespace sync_preferences {
24 class PrefServiceSyncable;
25 }
26 
27 namespace user_prefs {
28 class PrefRegistrySyncable;
29 }
30 
31 namespace chromeos {
32 
33 class User;
34 
35 namespace input_method {
36 class InputMethodManager;
37 class InputMethodSyncer;
38 }
39 
40 // The Preferences class handles Chrome OS preferences. When the class
41 // is first initialized, it will initialize the OS settings to what's stored in
42 // the preferences. These include touchpad settings, etc.
43 // When the preferences change, we change the settings to reflect the new value.
44 class Preferences : public sync_preferences::PrefServiceSyncableObserver,
45                     public user_manager::UserManager::UserSessionStateObserver {
46  public:
47   Preferences();
48   explicit Preferences(
49       input_method::InputMethodManager* input_method_manager);  // for testing
50   ~Preferences() override;
51 
52   // These method will register the prefs associated with Chrome OS settings.
53   static void RegisterPrefs(PrefRegistrySimple* registry);
54   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
55 
56   // This method will initialize Chrome OS settings to values in user prefs.
57   // |user| is the user owning this preferences.
58   void Init(Profile* profile, const user_manager::User* user);
59 
60   void InitUserPrefsForTesting(
61       sync_preferences::PrefServiceSyncable* prefs,
62       const user_manager::User* user,
63       scoped_refptr<input_method::InputMethodManager::State> ime_state);
64   void SetInputMethodListForTesting();
65 
66  private:
67   enum ApplyReason {
68     REASON_INITIALIZATION,
69     REASON_ACTIVE_USER_CHANGED,
70     REASON_PREF_CHANGED
71   };
72 
73   // Initializes all member prefs.
74   void InitUserPrefs(sync_preferences::PrefServiceSyncable* prefs);
75 
76   // Callback method for preference changes.
77   void OnPreferenceChanged(const std::string& pref_name);
78 
79   // Add a sample to the appropriate UMA histogram for a boolean preference.
80   void ReportBooleanPrefApplication(ApplyReason reason,
81                                     const std::string& changed_histogram_name,
82                                     const std::string& started_histogram_name,
83                                     bool sample);
84 
85   // Add a sample to the appropriate UMA histogram for a sensitivity preference.
86   void ReportSensitivityPrefApplication(
87       ApplyReason reason,
88       const std::string& changed_histogram_name,
89       const std::string& started_histogram_name,
90       int sensitivity_int);
91 
92   // This will set the OS settings when the preference changed or the user
93   // owning these preferences became active. Also this method is called on
94   // initialization. The reason of the call is stored as the |reason| parameter.
95   // |pref_name| is the name of the changed preference if the |reason| is
96   // |REASON_PREF_CHANGED|, otherwise it is empty.
97   void ApplyPreferences(ApplyReason reason,
98                         const std::string& pref_name);
99 
100   // A variant of SetLanguageConfigStringList. You can pass comma-separated
101   // values. Examples of |value|: "", "Control+space,Hiragana"
102   void SetLanguageConfigStringListAsCSV(const char* section,
103                                         const char* name,
104                                         const std::string& value);
105 
106   // Restores the user's preferred input method / keyboard layout on signing in.
107   void SetInputMethodList();
108 
109   // Updates the initial key repeat delay and key repeat interval following
110   // current prefs values. We set the delay and interval at once since an
111   // underlying XKB API requires it.
112   void UpdateAutoRepeatRate();
113 
114   // Force natural scroll to on if --enable-natural-scroll-default is specified
115   // on the cmd line.
116   void ForceNaturalScrollDefault();
117 
118   // sync_preferences::PrefServiceSyncableObserver implementation.
119   void OnIsSyncingChanged() override;
120 
121   // Overriden form user_manager::UserManager::UserSessionStateObserver.
122   void ActiveUserChanged(user_manager::User* active_user) override;
123 
124   sync_preferences::PrefServiceSyncable* prefs_;
125 
126   input_method::InputMethodManager* input_method_manager_;
127   std::unique_ptr<TracingManager> tracing_manager_;
128 
129   BooleanPrefMember performance_tracing_enabled_;
130   BooleanPrefMember tap_to_click_enabled_;
131   BooleanPrefMember three_finger_click_enabled_;
132   BooleanPrefMember unified_desktop_enabled_by_default_;
133   BooleanPrefMember natural_scroll_;
134   BooleanPrefMember vert_edge_scroll_enabled_;
135   IntegerPrefMember speed_factor_;
136   IntegerPrefMember mouse_sensitivity_;
137   IntegerPrefMember mouse_scroll_sensitivity_;
138   IntegerPrefMember touchpad_sensitivity_;
139   IntegerPrefMember touchpad_scroll_sensitivity_;
140   BooleanPrefMember primary_mouse_button_right_;
141   BooleanPrefMember mouse_reverse_scroll_;
142   BooleanPrefMember mouse_acceleration_;
143   BooleanPrefMember mouse_scroll_acceleration_;
144   BooleanPrefMember touchpad_acceleration_;
145   BooleanPrefMember touchpad_scroll_acceleration_;
146   FilePathPrefMember download_default_directory_;
147 
148   StringListPrefMember allowed_languages_;
149   StringPrefMember preferred_languages_;
150 
151   // Input method preferences.
152   StringPrefMember preload_engines_;
153   StringPrefMember current_input_method_;
154   StringPrefMember previous_input_method_;
155 
156   StringListPrefMember allowed_input_methods_;
157   StringPrefMember enabled_imes_;
158   BooleanPrefMember ime_menu_activated_;
159 
160   BooleanPrefMember xkb_auto_repeat_enabled_;
161   IntegerPrefMember xkb_auto_repeat_delay_pref_;
162   IntegerPrefMember xkb_auto_repeat_interval_pref_;
163 
164   PrefChangeRegistrar pref_change_registrar_;
165 
166   // User owning these preferences.
167   const user_manager::User* user_;
168 
169   // Whether user is a primary user.
170   bool user_is_primary_;
171 
172   // Input Methods state for this user.
173   scoped_refptr<input_method::InputMethodManager::State> ime_state_;
174 
175   std::unique_ptr<input_method::InputMethodSyncer> input_method_syncer_;
176 
177   mojo::Remote<ash::mojom::CrosDisplayConfigController> cros_display_config_;
178 
179   DISALLOW_COPY_AND_ASSIGN(Preferences);
180 };
181 
182 }  // namespace chromeos
183 
184 #endif  // CHROME_BROWSER_CHROMEOS_PREFERENCES_H_
185