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_OPTIONS_UI_H_
      6 #define CHROME_BROWSER_UI_WEBUI_OPTIONS_OPTIONS_UI_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/callback_list.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "content/public/browser/notification_registrar.h"
     15 #include "content/public/browser/web_contents_observer.h"
     16 #include "content/public/browser/web_ui_controller.h"
     17 #include "content/public/browser/web_ui_message_handler.h"
     18 #include "ui/base/layout.h"
     19 
     20 class AutocompleteResult;
     21 
     22 namespace base {
     23 class DictionaryValue;
     24 class ListValue;
     25 class RefCountedMemory;
     26 }
     27 
     28 #if defined(OS_CHROMEOS)
     29 namespace chromeos {
     30 namespace system {
     31 class PointerDeviceObserver;
     32 }  // namespace system
     33 }  // namespace chromeos
     34 #endif
     35 
     36 namespace options {
     37 
     38 // The base class handler of Javascript messages of options pages.
     39 class OptionsPageUIHandler : public content::WebUIMessageHandler {
     40  public:
     41   // Key for identifying the Settings App localized_strings in loadTimeData.
     42   static const char kSettingsAppKey[];
     43 
     44   OptionsPageUIHandler();
     45   virtual ~OptionsPageUIHandler();
     46 
     47   // Is this handler enabled?
     48   virtual bool IsEnabled();
     49 
     50   // Collects localized strings for options page.
     51   virtual void GetLocalizedValues(base::DictionaryValue* localized_strings) = 0;
     52 
     53   virtual void PageLoadStarted() {}
     54 
     55   // Will be called only once in the life time of the handler. Generally used to
     56   // add observers, initializes preferences, or start asynchronous calls from
     57   // various services.
     58   virtual void InitializeHandler() {}
     59 
     60   // Initialize the page. Called once the DOM is available for manipulation.
     61   // This will be called when a RenderView is re-used (when navigated to with
     62   // back/forward or session restored in some cases) or when created.
     63   virtual void InitializePage() {}
     64 
     65   // Uninitializes the page.  Called just before the object is destructed.
     66   virtual void Uninitialize() {}
     67 
     68   // WebUIMessageHandler implementation.
     69   virtual void RegisterMessages() OVERRIDE {}
     70 
     71  protected:
     72   struct OptionsStringResource {
     73     // The name of the resource in templateData.
     74     const char* name;
     75     // The .grd ID for the resource (IDS_*).
     76     int id;
     77     // The .grd ID of the string to replace $1 in |id|'s string. If zero or
     78     // omitted (default initialized), no substitution is attempted.
     79     int substitution_id;
     80   };
     81 
     82   // A helper to simplify string registration in WebUI for strings which do not
     83   // change at runtime and optionally contain a single substitution.
     84   static void RegisterStrings(base::DictionaryValue* localized_strings,
     85                               const OptionsStringResource* resources,
     86                               size_t length);
     87 
     88   // Registers string resources for a page's header and tab title.
     89   static void RegisterTitle(base::DictionaryValue* localized_strings,
     90                             const std::string& variable_name,
     91                             int title_id);
     92 
     93   content::NotificationRegistrar registrar_;
     94 
     95  private:
     96   DISALLOW_COPY_AND_ASSIGN(OptionsPageUIHandler);
     97 };
     98 
     99 // An interface for common operations that a host of OptionsPageUIHandlers
    100 // should provide.
    101 class OptionsPageUIHandlerHost {
    102  public:
    103   virtual void InitializeHandlers() = 0;
    104   virtual void OnFinishedLoading() {}
    105 
    106  protected:
    107   virtual ~OptionsPageUIHandlerHost() {}
    108 };
    109 
    110 // The WebUI for chrome:settings-frame.
    111 class OptionsUI : public content::WebUIController,
    112                   public content::WebContentsObserver,
    113                   public OptionsPageUIHandlerHost {
    114  public:
    115   typedef base::CallbackList<void()> OnFinishedLoadingCallbackList;
    116 
    117   explicit OptionsUI(content::WebUI* web_ui);
    118   virtual ~OptionsUI();
    119 
    120   // Registers a callback to be called once the settings frame has finished
    121   // loading on the HTML/JS side.
    122   scoped_ptr<OnFinishedLoadingCallbackList::Subscription>
    123       RegisterOnFinishedLoadingCallback(const base::Closure& callback);
    124 
    125   // Takes the suggestions from |result| and adds them to |suggestions| so that
    126   // they can be passed to a JavaScript function.
    127   static void ProcessAutocompleteSuggestions(
    128       const AutocompleteResult& result,
    129       base::ListValue* const suggestions);
    130 
    131   static base::RefCountedMemory* GetFaviconResourceBytes(
    132       ui::ScaleFactor scale_factor);
    133 
    134   // Overridden from content::WebContentsObserver:
    135   virtual void DidStartProvisionalLoadForFrame(
    136       content::RenderFrameHost* render_frame_host,
    137       const GURL& validated_url,
    138       bool is_error_page,
    139       bool is_iframe_srcdoc) OVERRIDE;
    140 
    141   // Overridden from OptionsPageUIHandlerHost:
    142   virtual void InitializeHandlers() OVERRIDE;
    143   virtual void OnFinishedLoading() OVERRIDE;
    144 
    145  private:
    146   // Adds OptionsPageUiHandler to the handlers list if handler is enabled.
    147   void AddOptionsPageUIHandler(base::DictionaryValue* localized_strings,
    148                                OptionsPageUIHandler* handler);
    149 
    150   bool initialized_handlers_;
    151 
    152   std::vector<OptionsPageUIHandler*> handlers_;
    153   OnFinishedLoadingCallbackList on_finished_loading_callbacks_;
    154 
    155 #if defined(OS_CHROMEOS)
    156   scoped_ptr<chromeos::system::PointerDeviceObserver>
    157       pointer_device_observer_;
    158 #endif
    159 
    160   DISALLOW_COPY_AND_ASSIGN(OptionsUI);
    161 };
    162 
    163 }  // namespace options
    164 
    165 #endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_OPTIONS_UI_H_
    166