NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / core_options_handler.h
blob369be28370ced58ba1dc0149fe2914e8c143b26e
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 virtual ~CoreOptionsHandler();
27 // OptionsPageUIHandler implementation.
28 virtual void GetLocalizedValues(
29 base::DictionaryValue* localized_strings) OVERRIDE;
30 virtual void InitializeHandler() OVERRIDE;
31 virtual void InitializePage() OVERRIDE;
32 virtual void Uninitialize() OVERRIDE;
34 // WebUIMessageHandler implementation.
35 virtual void RegisterMessages() OVERRIDE;
37 void set_handlers_host(OptionsPageUIHandlerHost* handlers_host) {
38 handlers_host_ = handlers_host;
41 // Adds localized strings to |localized_strings|.
42 static void GetStaticLocalizedValues(
43 base::DictionaryValue* localized_strings);
45 protected:
46 // Fetches a pref value of given |pref_name|.
47 // Note that caller owns the returned Value.
48 virtual base::Value* FetchPref(const std::string& pref_name);
50 // Observes a pref of given |pref_name|.
51 virtual void ObservePref(const std::string& pref_name);
53 // Stops observing given preference identified by |pref_name|.
54 virtual void StopObservingPref(const std::string& pref_name);
56 // Sets a pref |value| to given |pref_name|.
57 virtual void SetPref(const std::string& pref_name,
58 const base::Value* value,
59 const std::string& metric);
61 // Clears pref value for given |pref_name|.
62 void ClearPref(const std::string& pref_name, const std::string& metric);
64 // Records a user metric action for the given value.
65 void ProcessUserMetric(const base::Value* value,
66 const std::string& metric);
68 // Virtual dispatch is needed as handling of some prefs may be
69 // finessed in subclasses. The PrefService pointer is included
70 // so that subclasses can know whether the observed pref is from the
71 // local state or not.
72 virtual void OnPreferenceChanged(PrefService* service,
73 const std::string& pref_name);
75 // Notifies registered JS callbacks on change in |pref_name| preference.
76 // |controlling_pref_name| controls if |pref_name| is managed by
77 // policy/extension; empty |controlling_pref_name| indicates no other pref is
78 // controlling |pref_name|.
79 void NotifyPrefChanged(const std::string& pref_name,
80 const std::string& controlling_pref_name);
82 // Calls JS callbacks to report a change in the value of the |name|
83 // preference. |value| is the new value for |name|. Called from
84 // Notify*Changed methods to fire off the notifications.
85 void DispatchPrefChangeNotification(const std::string& name,
86 scoped_ptr<base::Value> value);
88 // Creates dictionary value for the pref described by |pref_name|.
89 // If |controlling_pref| is not empty, it describes the pref that manages
90 // |pref| via policy or extension.
91 virtual base::Value* CreateValueForPref(
92 const std::string& pref_name,
93 const std::string& controlling_pref_name);
95 typedef std::multimap<std::string, std::string> PreferenceCallbackMap;
96 PreferenceCallbackMap pref_callback_map_;
98 private:
99 // Type of preference value received from the page. This doesn't map 1:1 to
100 // Value::Type, since a TYPE_STRING can require custom processing.
101 enum PrefType {
102 TYPE_BOOLEAN = 0,
103 TYPE_INTEGER,
104 TYPE_DOUBLE,
105 TYPE_STRING,
106 TYPE_URL,
107 TYPE_LIST,
110 // Finds the pref service that holds the given pref. If the pref is not found,
111 // it will return user prefs.
112 PrefService* FindServiceForPref(const std::string& pref_name);
114 // Callback for the "coreOptionsInitialize" message. This message will
115 // trigger the Initialize() method of all other handlers so that final
116 // setup can be performed before the page is shown.
117 void HandleInitialize(const base::ListValue* args);
119 // Callback for the "fetchPrefs" message. This message accepts the list of
120 // preference names passed as the |args| parameter (ListValue). It passes
121 // results dictionary of preference values by calling prefsFetched() JS method
122 // on the page.
123 void HandleFetchPrefs(const base::ListValue* args);
125 // Callback for the "observePrefs" message. This message initiates
126 // notification observing for given array of preference names.
127 void HandleObservePrefs(const base::ListValue* args);
129 // Callbacks for the "set<type>Pref" message. This message saves the new
130 // preference value. |args| is an array of parameters as follows:
131 // item 0 - name of the preference.
132 // item 1 - the value of the preference in string form.
133 // item 2 - name of the metric identifier (optional).
134 void HandleSetBooleanPref(const base::ListValue* args);
135 void HandleSetIntegerPref(const base::ListValue* args);
136 void HandleSetDoublePref(const base::ListValue* args);
137 void HandleSetStringPref(const base::ListValue* args);
138 void HandleSetURLPref(const base::ListValue* args);
139 void HandleSetListPref(const base::ListValue* args);
141 void HandleSetPref(const base::ListValue* args, PrefType type);
143 // Callback for the "clearPref" message. This message clears a preference
144 // value. |args| is an array of parameters as follows:
145 // item 0 - name of the preference.
146 // item 1 - name of the metric identifier (optional).
147 void HandleClearPref(const base::ListValue* args);
149 // Callback for the "coreOptionsUserMetricsAction" message. This records
150 // an action that should be tracked if metrics recording is enabled. |args|
151 // is an array that contains a single item, the name of the metric identifier.
152 void HandleUserMetricsAction(const base::ListValue* args);
154 // Callback for the "disableExtension" message. The extension ID string is the
155 // only argument in the |args| list.
156 void HandleDisableExtension(const base::ListValue* args);
158 void UpdateClearPluginLSOData();
159 void UpdatePepperFlashSettingsEnabled();
161 OptionsPageUIHandlerHost* handlers_host_;
162 // This registrar keeps track of user prefs.
163 PrefChangeRegistrar registrar_;
164 // This registrar keeps track of local state.
165 PrefChangeRegistrar local_state_registrar_;
167 PluginStatusPrefSetter plugin_status_pref_setter_;
169 // This maps pref names to filter functions. The callbacks should take the
170 // value that the user has attempted to set for the pref, and should return
171 // true if that value may be applied. If the return value is false, the
172 // change will be ignored.
173 typedef std::map<std::string, base::Callback<bool(const base::Value*)> >
174 PrefChangeFilterMap;
175 PrefChangeFilterMap pref_change_filters_;
177 DISALLOW_COPY_AND_ASSIGN(CoreOptionsHandler);
180 } // namespace options
182 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_CORE_OPTIONS_HANDLER_H_