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/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"
20 class AutocompleteResult
;
23 class DictionaryValue
;
25 class RefCountedMemory
;
28 #if defined(OS_CHROMEOS)
31 class PointerDeviceObserver
;
33 } // namespace chromeos
38 // The base class handler of Javascript messages of options pages.
39 class OptionsPageUIHandler
: public content::WebUIMessageHandler
{
41 // Key for identifying the Settings App localized_strings in loadTimeData.
42 static const char kSettingsAppKey
[];
44 OptionsPageUIHandler();
45 ~OptionsPageUIHandler() override
;
47 // Is this handler enabled?
48 virtual bool IsEnabled();
50 // Collects localized strings for options page.
51 virtual void GetLocalizedValues(base::DictionaryValue
* localized_strings
) = 0;
53 virtual void PageLoadStarted() {}
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
58 virtual void InitializeHandler() {}
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() {}
65 // Uninitializes the page. Called just before the object is destructed.
66 virtual void Uninitialize() {}
68 // WebUIMessageHandler implementation.
69 void RegisterMessages() override
{}
72 struct OptionsStringResource
{
73 // The name of the resource in templateData.
75 // The .grd ID for the resource (IDS_*).
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.
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
,
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
,
93 content::NotificationRegistrar registrar_
;
96 DISALLOW_COPY_AND_ASSIGN(OptionsPageUIHandler
);
99 // An interface for common operations that a host of OptionsPageUIHandlers
101 class OptionsPageUIHandlerHost
{
103 virtual void InitializeHandlers() = 0;
104 virtual void OnFinishedLoading() {}
107 virtual ~OptionsPageUIHandlerHost() {}
110 // The WebUI for chrome:settings-frame.
111 class OptionsUI
: public content::WebUIController
,
112 public content::WebContentsObserver
,
113 public OptionsPageUIHandlerHost
{
115 typedef base::CallbackList
<void()> OnFinishedLoadingCallbackList
;
117 explicit OptionsUI(content::WebUI
* web_ui
);
118 ~OptionsUI() override
;
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
);
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
);
131 static base::RefCountedMemory
* GetFaviconResourceBytes(
132 ui::ScaleFactor scale_factor
);
134 // Overridden from content::WebContentsObserver:
135 void DidStartProvisionalLoadForFrame(
136 content::RenderFrameHost
* render_frame_host
,
137 const GURL
& validated_url
,
139 bool is_iframe_srcdoc
) override
;
141 // Overridden from OptionsPageUIHandlerHost:
142 void InitializeHandlers() override
;
143 void OnFinishedLoading() override
;
146 // Adds OptionsPageUiHandler to the handlers list if handler is enabled.
147 void AddOptionsPageUIHandler(base::DictionaryValue
* localized_strings
,
148 OptionsPageUIHandler
* handler
);
150 bool initialized_handlers_
;
152 std::vector
<OptionsPageUIHandler
*> handlers_
;
153 OnFinishedLoadingCallbackList on_finished_loading_callbacks_
;
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_