Standardize usage of virtual/override/final in chrome/browser/ui/
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / core_options_handler.h
blobe5e5bc2593588ab92707a0b4f7c2e84deca0ca26
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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS_CORE_OPTIONS_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_OPTIONS_CORE_OPTIONS_HANDLER_H_
8 #include <map>
9 #include <string>
11 #include "base/callback.h"
12 #include "base/prefs/pref_change_registrar.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/values.h"
15 #include "chrome/browser/plugins/plugin_status_pref_setter.h"
16 #include "chrome/browser/ui/webui/options/options_ui.h"
18 namespace options {
20 // Core options UI handler.
21 // Handles resource and JS calls common to all options sub-pages.
22 class CoreOptionsHandler : public OptionsPageUIHandler {
23 public:
24 CoreOptionsHandler();
25 ~CoreOptionsHandler() override;
27 // OptionsPageUIHandler implementation.
28 void GetLocalizedValues(base::DictionaryValue* localized_strings) override;
29 void InitializeHandler() override;
30 void InitializePage() override;
31 void Uninitialize() override;
33 // WebUIMessageHandler implementation.
34 void RegisterMessages() override;
36 void set_handlers_host(OptionsPageUIHandlerHost* handlers_host) {
37 handlers_host_ = handlers_host;
40 // Adds localized strings to |localized_strings|.
41 static void GetStaticLocalizedValues(
42 base::DictionaryValue* localized_strings);
44 protected:
45 // Fetches a pref value of given |pref_name|.
46 // Note that caller owns the returned Value.
47 virtual base::Value* FetchPref(const std::string& pref_name);
49 // Observes a pref of given |pref_name|.
50 virtual void ObservePref(const std::string& pref_name);
52 // Stops observing given preference identified by |pref_name|.
53 virtual void StopObservingPref(const std::string& pref_name);
55 // Sets a pref |value| to given |pref_name|.
56 virtual void SetPref(const std::string& pref_name,
57 const base::Value* value,
58 const std::string& metric);
60 // Clears pref value for given |pref_name|.
61 void ClearPref(const std::string& pref_name, const std::string& metric);
63 // Records a user metric action for the given value.
64 void ProcessUserMetric(const base::Value* value,
65 const std::string& metric);
67 // Virtual dispatch is needed as handling of some prefs may be
68 // finessed in subclasses. The PrefService pointer is included
69 // so that subclasses can know whether the observed pref is from the
70 // local state or not.
71 virtual void OnPreferenceChanged(PrefService* service,
72 const std::string& pref_name);
74 // Notifies registered JS callbacks on change in |pref_name| preference.
75 // |controlling_pref_name| controls if |pref_name| is managed by
76 // policy/extension; empty |controlling_pref_name| indicates no other pref is
77 // controlling |pref_name|.
78 void NotifyPrefChanged(const std::string& pref_name,
79 const std::string& controlling_pref_name);
81 // Calls JS callbacks to report a change in the value of the |name|
82 // preference. |value| is the new value for |name|. Called from
83 // Notify*Changed methods to fire off the notifications.
84 void DispatchPrefChangeNotification(const std::string& name,
85 scoped_ptr<base::Value> value);
87 // Creates dictionary value for the pref described by |pref_name|.
88 // If |controlling_pref| is not empty, it describes the pref that manages
89 // |pref| via policy or extension.
90 virtual base::Value* CreateValueForPref(
91 const std::string& pref_name,
92 const std::string& controlling_pref_name);
94 typedef std::multimap<std::string, std::string> PreferenceCallbackMap;
95 PreferenceCallbackMap pref_callback_map_;
97 private:
98 // Type of preference value received from the page. This doesn't map 1:1 to
99 // Value::Type, since a TYPE_STRING can require custom processing.
100 enum PrefType {
101 TYPE_BOOLEAN = 0,
102 TYPE_INTEGER,
103 TYPE_DOUBLE,
104 TYPE_STRING,
105 TYPE_URL,
106 TYPE_LIST,
109 // Finds the pref service that holds the given pref. If the pref is not found,
110 // it will return user prefs.
111 PrefService* FindServiceForPref(const std::string& pref_name);
113 // Callback for the "coreOptionsInitialize" message. This message will
114 // trigger the Initialize() method of all other handlers so that final
115 // setup can be performed before the page is shown.
116 void HandleInitialize(const base::ListValue* args);
118 // Callback for the "onFinishedLoadingOptions" message. This message is sent
119 // when the load() handler for the options frame, along with all asynchronous
120 // calls it has spawned, have finished running.
121 void OnFinishedLoading(const base::ListValue* args);
123 // Callback for the "fetchPrefs" message. This message accepts the list of
124 // preference names passed as the |args| parameter (ListValue). It passes
125 // results dictionary of preference values by calling prefsFetched() JS method
126 // on the page.
127 void HandleFetchPrefs(const base::ListValue* args);
129 // Callback for the "observePrefs" message. This message initiates
130 // notification observing for given array of preference names.
131 void HandleObservePrefs(const base::ListValue* args);
133 // Callbacks for the "set<type>Pref" message. This message saves the new
134 // preference value. |args| is an array of parameters as follows:
135 // item 0 - name of the preference.
136 // item 1 - the value of the preference in string form.
137 // item 2 - name of the metric identifier (optional).
138 void HandleSetBooleanPref(const base::ListValue* args);
139 void HandleSetIntegerPref(const base::ListValue* args);
140 void HandleSetDoublePref(const base::ListValue* args);
141 void HandleSetStringPref(const base::ListValue* args);
142 void HandleSetURLPref(const base::ListValue* args);
143 void HandleSetListPref(const base::ListValue* args);
145 void HandleSetPref(const base::ListValue* args, PrefType type);
147 // Callback for the "clearPref" message. This message clears a preference
148 // value. |args| is an array of parameters as follows:
149 // item 0 - name of the preference.
150 // item 1 - name of the metric identifier (optional).
151 void HandleClearPref(const base::ListValue* args);
153 // Callback for the "coreOptionsUserMetricsAction" message. This records
154 // an action that should be tracked if metrics recording is enabled. |args|
155 // is an array that contains a single item, the name of the metric identifier.
156 void HandleUserMetricsAction(const base::ListValue* args);
158 // Callback for the "disableExtension" message. The extension ID string is the
159 // only argument in the |args| list.
160 void HandleDisableExtension(const base::ListValue* args);
162 void UpdateClearPluginLSOData();
163 void UpdatePepperFlashSettingsEnabled();
165 // Checks that the current profile is not supervised. Used as a pref filter.
166 bool IsUserUnsupervised(const base::Value* to_value);
168 OptionsPageUIHandlerHost* handlers_host_;
169 // This registrar keeps track of user prefs.
170 PrefChangeRegistrar registrar_;
171 // This registrar keeps track of local state.
172 PrefChangeRegistrar local_state_registrar_;
174 PluginStatusPrefSetter plugin_status_pref_setter_;
176 // This maps pref names to filter functions. The callbacks should take the
177 // value that the user has attempted to set for the pref, and should return
178 // true if that value may be applied. If the return value is false, the
179 // change will be ignored.
180 typedef std::map<std::string, base::Callback<bool(const base::Value*)> >
181 PrefChangeFilterMap;
182 PrefChangeFilterMap pref_change_filters_;
184 DISALLOW_COPY_AND_ASSIGN(CoreOptionsHandler);
187 } // namespace options
189 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_CORE_OPTIONS_HANDLER_H_