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