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_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"
19 class AutocompleteResult
;
22 class DictionaryValue
;
24 class RefCountedMemory
;
27 #if defined(OS_CHROMEOS)
30 class PointerDeviceObserver
;
32 } // namespace chromeos
37 // The base class handler of Javascript messages of options pages.
38 class OptionsPageUIHandler
: public content::WebUIMessageHandler
{
40 // Key for identifying the Settings App localized_strings in loadTimeData.
41 static const char kSettingsAppKey
[];
43 OptionsPageUIHandler();
44 virtual ~OptionsPageUIHandler();
46 // Is this handler enabled?
47 virtual bool IsEnabled();
49 // Collects localized strings for options page.
50 virtual void GetLocalizedValues(base::DictionaryValue
* localized_strings
) = 0;
52 virtual void PageLoadStarted() {}
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
57 virtual void InitializeHandler() {}
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() {}
64 // Uninitializes the page. Called just before the object is destructed.
65 virtual void Uninitialize() {}
67 // WebUIMessageHandler implementation.
68 virtual void RegisterMessages() OVERRIDE
{}
71 struct OptionsStringResource
{
72 // The name of the resource in templateData.
74 // The .grd ID for the resource (IDS_*).
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.
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
,
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
,
92 content::NotificationRegistrar registrar_
;
95 DISALLOW_COPY_AND_ASSIGN(OptionsPageUIHandler
);
98 // An interface for common operations that a host of OptionsPageUIHandlers
100 class OptionsPageUIHandlerHost
{
102 virtual void InitializeHandlers() = 0;
105 virtual ~OptionsPageUIHandlerHost() {}
108 // The WebUI for chrome:settings-frame.
109 class OptionsUI
: public content::WebUIController
,
110 public content::WebContentsObserver
,
111 public OptionsPageUIHandlerHost
{
113 explicit OptionsUI(content::WebUI
* web_ui
);
114 virtual ~OptionsUI();
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
);
122 static base::RefCountedMemory
* GetFaviconResourceBytes(
123 ui::ScaleFactor scale_factor
);
125 // Overridden from content::WebContentsObserver:
126 virtual void DidStartProvisionalLoadForFrame(
128 int64 parent_frame_id
,
130 const GURL
& validated_url
,
132 bool is_iframe_srcdoc
,
133 content::RenderViewHost
* render_view_host
) OVERRIDE
;
135 // Overridden from OptionsPageUIHandlerHost:
136 virtual void InitializeHandlers() OVERRIDE
;
139 // Adds OptionsPageUiHandler to the handlers list if handler is enabled.
140 void AddOptionsPageUIHandler(base::DictionaryValue
* localized_strings
,
141 OptionsPageUIHandler
* handler
);
143 bool initialized_handlers_
;
145 std::vector
<OptionsPageUIHandler
*> handlers_
;
147 #if defined(OS_CHROMEOS)
148 scoped_ptr
<chromeos::system::PointerDeviceObserver
>
149 pointer_device_observer_
;
152 DISALLOW_COPY_AND_ASSIGN(OptionsUI
);
155 } // namespace options
157 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_OPTIONS_UI_H_