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