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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS_OPTIONS_UI_H_
6 #define CHROME_BROWSER_UI_WEBUI_OPTIONS_OPTIONS_UI_H_
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_contents_observer.h"
17 #include "content/public/browser/web_ui_controller.h"
18 #include "content/public/browser/web_ui_message_handler.h"
19 #include "ui/base/layout.h"
21 class AutocompleteResult
;
24 class DictionaryValue
;
26 class RefCountedMemory
;
29 #if defined(OS_CHROMEOS)
32 class PointerDeviceObserver
;
34 } // namespace chromeos
39 // The base class handler of Javascript messages of options pages.
40 class OptionsPageUIHandler
: public content::WebUIMessageHandler
,
41 public content::NotificationObserver
{
43 // Key for identifying the Settings App localized_strings in loadTimeData.
44 static const char kSettingsAppKey
[];
46 OptionsPageUIHandler();
47 virtual ~OptionsPageUIHandler();
49 // Is this handler enabled?
50 virtual bool IsEnabled();
52 // Collects localized strings for options page.
53 virtual void GetLocalizedValues(base::DictionaryValue
* localized_strings
) = 0;
55 virtual void PageLoadStarted() {}
57 // Will be called only once in the life time of the handler. Generally used to
58 // add observers, initializes preferences, or start asynchronous calls from
60 virtual void InitializeHandler() {}
62 // Initialize the page. Called once the DOM is available for manipulation.
63 // This will be called when a RenderView is re-used (when navigated to with
64 // back/forward or session restored in some cases) or when created.
65 virtual void InitializePage() {}
67 // Uninitializes the page. Called just before the object is destructed.
68 virtual void Uninitialize() {}
70 // WebUIMessageHandler implementation.
71 virtual void RegisterMessages() OVERRIDE
{}
73 // content::NotificationObserver implementation.
74 virtual void Observe(int type
,
75 const content::NotificationSource
& source
,
76 const content::NotificationDetails
& details
) OVERRIDE
{}
79 struct OptionsStringResource
{
80 // The name of the resource in templateData.
82 // The .grd ID for the resource (IDS_*).
84 // The .grd ID of the string to replace $1 in |id|'s string. If zero or
85 // omitted (default initialized), no substitution is attempted.
89 // A helper to simplify string registration in WebUI for strings which do not
90 // change at runtime and optionally contain a single substitution.
91 static void RegisterStrings(base::DictionaryValue
* localized_strings
,
92 const OptionsStringResource
* resources
,
95 // Registers string resources for a page's header and tab title.
96 static void RegisterTitle(base::DictionaryValue
* localized_strings
,
97 const std::string
& variable_name
,
100 content::NotificationRegistrar registrar_
;
103 DISALLOW_COPY_AND_ASSIGN(OptionsPageUIHandler
);
106 // An interface for common operations that a host of OptionsPageUIHandlers
108 class OptionsPageUIHandlerHost
{
110 virtual void InitializeHandlers() = 0;
113 virtual ~OptionsPageUIHandlerHost() {}
116 // The WebUI for chrome:settings-frame.
117 class OptionsUI
: public content::WebUIController
,
118 public content::WebContentsObserver
,
119 public OptionsPageUIHandlerHost
{
121 explicit OptionsUI(content::WebUI
* web_ui
);
122 virtual ~OptionsUI();
124 // Takes the suggestions from |result| and adds them to |suggestions| so that
125 // they can be passed to a JavaScript function.
126 static void ProcessAutocompleteSuggestions(
127 const AutocompleteResult
& result
,
128 base::ListValue
* const suggestions
);
130 static base::RefCountedMemory
* GetFaviconResourceBytes(
131 ui::ScaleFactor scale_factor
);
133 // Overridden from content::WebContentsObserver:
134 virtual void DidStartProvisionalLoadForFrame(
136 int64 parent_frame_id
,
138 const GURL
& validated_url
,
140 bool is_iframe_srcdoc
,
141 content::RenderViewHost
* render_view_host
) OVERRIDE
;
143 // Overridden from OptionsPageUIHandlerHost:
144 virtual void InitializeHandlers() OVERRIDE
;
147 // Adds OptionsPageUiHandler to the handlers list if handler is enabled.
148 void AddOptionsPageUIHandler(base::DictionaryValue
* localized_strings
,
149 OptionsPageUIHandler
* handler
);
151 bool initialized_handlers_
;
153 std::vector
<OptionsPageUIHandler
*> handlers_
;
155 #if defined(OS_CHROMEOS)
156 scoped_ptr
<chromeos::system::PointerDeviceObserver
>
157 pointer_device_observer_
;
160 DISALLOW_COPY_AND_ASSIGN(OptionsUI
);
163 } // namespace options
165 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_OPTIONS_UI_H_