Home | History | Annotate | Download | only in options
      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(
     29       base::DictionaryValue* localized_strings) OVERRIDE;
     30   virtual void InitializeHandler() OVERRIDE;
     31   virtual void InitializePage() OVERRIDE;
     32   virtual void Uninitialize() OVERRIDE;
     33 
     34   // WebUIMessageHandler implementation.
     35   virtual void RegisterMessages() OVERRIDE;
     36 
     37   void set_handlers_host(OptionsPageUIHandlerHost* handlers_host) {
     38     handlers_host_ = handlers_host;
     39   }
     40 
     41   // Adds localized strings to |localized_strings|.
     42   static void GetStaticLocalizedValues(
     43       base::DictionaryValue* localized_strings);
     44 
     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);
     49 
     50   // Observes a pref of given |pref_name|.
     51   virtual void ObservePref(const std::string& pref_name);
     52 
     53   // Stops observing given preference identified by |pref_name|.
     54   virtual void StopObservingPref(const std::string& pref_name);
     55 
     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);
     60 
     61   // Clears pref value for given |pref_name|.
     62   void ClearPref(const std::string& pref_name, const std::string& metric);
     63 
     64   // Records a user metric action for the given value.
     65   void ProcessUserMetric(const base::Value* value,
     66                          const std::string& metric);
     67 
     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);
     74 
     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);
     81 
     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);
     87 
     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);
     94 
     95   typedef std::multimap<std::string, std::string> PreferenceCallbackMap;
     96   PreferenceCallbackMap pref_callback_map_;
     97 
     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,
    108   };
    109 
    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);
    113 
    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);
    118 
    119   // Callback for the "onFinishedLoadingOptions" message. This message is sent
    120   // when the load() handler for the options frame, along with all asynchronous
    121   // calls it has spawned, have finished running.
    122   void OnFinishedLoading(const base::ListValue* args);
    123 
    124   // Callback for the "fetchPrefs" message. This message accepts the list of
    125   // preference names passed as the |args| parameter (ListValue). It passes
    126   // results dictionary of preference values by calling prefsFetched() JS method
    127   // on the page.
    128   void HandleFetchPrefs(const base::ListValue* args);
    129 
    130   // Callback for the "observePrefs" message. This message initiates
    131   // notification observing for given array of preference names.
    132   void HandleObservePrefs(const base::ListValue* args);
    133 
    134   // Callbacks for the "set<type>Pref" message. This message saves the new
    135   // preference value. |args| is an array of parameters as follows:
    136   //  item 0 - name of the preference.
    137   //  item 1 - the value of the preference in string form.
    138   //  item 2 - name of the metric identifier (optional).
    139   void HandleSetBooleanPref(const base::ListValue* args);
    140   void HandleSetIntegerPref(const base::ListValue* args);
    141   void HandleSetDoublePref(const base::ListValue* args);
    142   void HandleSetStringPref(const base::ListValue* args);
    143   void HandleSetURLPref(const base::ListValue* args);
    144   void HandleSetListPref(const base::ListValue* args);
    145 
    146   void HandleSetPref(const base::ListValue* args, PrefType type);
    147 
    148   // Callback for the "clearPref" message.  This message clears a preference
    149   // value. |args| is an array of parameters as follows:
    150   //  item 0 - name of the preference.
    151   //  item 1 - name of the metric identifier (optional).
    152   void HandleClearPref(const base::ListValue* args);
    153 
    154   // Callback for the "coreOptionsUserMetricsAction" message.  This records
    155   // an action that should be tracked if metrics recording is enabled. |args|
    156   // is an array that contains a single item, the name of the metric identifier.
    157   void HandleUserMetricsAction(const base::ListValue* args);
    158 
    159   // Callback for the "disableExtension" message. The extension ID string is the
    160   // only argument in the |args| list.
    161   void HandleDisableExtension(const base::ListValue* args);
    162 
    163   void UpdateClearPluginLSOData();
    164   void UpdatePepperFlashSettingsEnabled();
    165 
    166   OptionsPageUIHandlerHost* handlers_host_;
    167   // This registrar keeps track of user prefs.
    168   PrefChangeRegistrar registrar_;
    169   // This registrar keeps track of local state.
    170   PrefChangeRegistrar local_state_registrar_;
    171 
    172   PluginStatusPrefSetter plugin_status_pref_setter_;
    173 
    174   // This maps pref names to filter functions. The callbacks should take the
    175   // value that the user has attempted to set for the pref, and should return
    176   // true if that value may be applied. If the return value is false, the
    177   // change will be ignored.
    178   typedef std::map<std::string, base::Callback<bool(const base::Value*)> >
    179       PrefChangeFilterMap;
    180   PrefChangeFilterMap pref_change_filters_;
    181 
    182   DISALLOW_COPY_AND_ASSIGN(CoreOptionsHandler);
    183 };
    184 
    185 }  // namespace options
    186 
    187 #endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_CORE_OPTIONS_HANDLER_H_
    188