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_UI_WEBUI_OPTIONS_CORE_OPTIONS_HANDLER_H_ 6 #define CHROME_BROWSER_UI_WEBUI_OPTIONS_CORE_OPTIONS_HANDLER_H_ 7 8 #include <map> 9 #include <string> 10 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" 17 18 namespace options { 19 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(); 26 27 // OptionsPageUIHandler implementation. 28 virtual void GetLocalizedValues(DictionaryValue* localized_strings) OVERRIDE; 29 virtual void InitializeHandler() OVERRIDE; 30 virtual void InitializePage() OVERRIDE; 31 virtual void Uninitialize() OVERRIDE; 32 33 // WebUIMessageHandler implementation. 34 virtual void RegisterMessages() OVERRIDE; 35 36 void set_handlers_host(OptionsPageUIHandlerHost* handlers_host) { 37 handlers_host_ = handlers_host; 38 } 39 40 // Adds localized strings to |localized_strings|. 41 static void GetStaticLocalizedValues( 42 base::DictionaryValue* localized_strings); 43 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); 48 49 // Observes a pref of given |pref_name|. 50 virtual void ObservePref(const std::string& pref_name); 51 52 // Stops observing given preference identified by |pref_name|. 53 virtual void StopObservingPref(const std::string& pref_name); 54 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); 59 60 // Clears pref value for given |pref_name|. 61 void ClearPref(const std::string& pref_name, const std::string& metric); 62 63 // Records a user metric action for the given value. 64 void ProcessUserMetric(const base::Value* value, 65 const std::string& metric); 66 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); 73 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); 80 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); 86 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 base::Value* CreateValueForPref(const std::string& pref_name, 91 const std::string& controlling_pref_name); 92 93 typedef std::multimap<std::string, std::string> PreferenceCallbackMap; 94 PreferenceCallbackMap pref_callback_map_; 95 96 private: 97 // Type of preference value received from the page. This doesn't map 1:1 to 98 // Value::Type, since a TYPE_STRING can require custom processing. 99 enum PrefType { 100 TYPE_BOOLEAN = 0, 101 TYPE_INTEGER, 102 TYPE_DOUBLE, 103 TYPE_STRING, 104 TYPE_URL, 105 TYPE_LIST, 106 }; 107 108 // Finds the pref service that holds the given pref. If the pref is not found, 109 // it will return user prefs. 110 PrefService* FindServiceForPref(const std::string& pref_name); 111 112 // Callback for the "coreOptionsInitialize" message. This message will 113 // trigger the Initialize() method of all other handlers so that final 114 // setup can be performed before the page is shown. 115 void HandleInitialize(const ListValue* args); 116 117 // Callback for the "fetchPrefs" message. This message accepts the list of 118 // preference names passed as the |args| parameter (ListValue). It passes 119 // results dictionary of preference values by calling prefsFetched() JS method 120 // on the page. 121 void HandleFetchPrefs(const ListValue* args); 122 123 // Callback for the "observePrefs" message. This message initiates 124 // notification observing for given array of preference names. 125 void HandleObservePrefs(const ListValue* args); 126 127 // Callbacks for the "set<type>Pref" message. This message saves the new 128 // preference value. |args| is an array of parameters as follows: 129 // item 0 - name of the preference. 130 // item 1 - the value of the preference in string form. 131 // item 2 - name of the metric identifier (optional). 132 void HandleSetBooleanPref(const ListValue* args); 133 void HandleSetIntegerPref(const ListValue* args); 134 void HandleSetDoublePref(const ListValue* args); 135 void HandleSetStringPref(const ListValue* args); 136 void HandleSetURLPref(const ListValue* args); 137 void HandleSetListPref(const ListValue* args); 138 139 void HandleSetPref(const ListValue* args, PrefType type); 140 141 // Callback for the "clearPref" message. This message clears a preference 142 // value. |args| is an array of parameters as follows: 143 // item 0 - name of the preference. 144 // item 1 - name of the metric identifier (optional). 145 void HandleClearPref(const ListValue* args); 146 147 // Callback for the "coreOptionsUserMetricsAction" message. This records 148 // an action that should be tracked if metrics recording is enabled. |args| 149 // is an array that contains a single item, the name of the metric identifier. 150 void HandleUserMetricsAction(const ListValue* args); 151 152 // Callback for the "disableExtension" message. The extension ID string is the 153 // only argument in the |args| list. 154 void HandleDisableExtension(const ListValue* args); 155 156 void UpdateClearPluginLSOData(); 157 void UpdatePepperFlashSettingsEnabled(); 158 159 OptionsPageUIHandlerHost* handlers_host_; 160 // This registrar keeps track of user prefs. 161 PrefChangeRegistrar registrar_; 162 // This registrar keeps track of local state. 163 PrefChangeRegistrar local_state_registrar_; 164 165 PluginStatusPrefSetter plugin_status_pref_setter_; 166 167 // This maps pref names to filter functions. The callbacks should take the 168 // value that the user has attempted to set for the pref, and should return 169 // true if that value may be applied. If the return value is false, the 170 // change will be ignored. 171 typedef std::map<std::string, base::Callback<bool(const base::Value*)> > 172 PrefChangeFilterMap; 173 PrefChangeFilterMap pref_change_filters_; 174 175 DISALLOW_COPY_AND_ASSIGN(CoreOptionsHandler); 176 }; 177 178 } // namespace options 179 180 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_CORE_OPTIONS_HANDLER_H_ 181