Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(323)

Side by Side Diff: chrome/browser/ui/webui/options2/options_ui2.cc

Issue 8895023: Options2: Pull the trigger. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: DIAF. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 #include "chrome/browser/ui/webui/options2/options_ui2.h"
6
7 #include <algorithm>
8 #include <vector>
9
10 #include "base/callback.h"
11 #include "base/command_line.h"
12 #include "base/memory/singleton.h"
13 #include "base/message_loop.h"
14 #include "base/string_piece.h"
15 #include "base/string_util.h"
16 #include "base/threading/thread.h"
17 #include "base/time.h"
18 #include "base/values.h"
19 #include "chrome/browser/browser_about_handler.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/webui/options2/advanced_options_handler.h"
23 #include "chrome/browser/ui/webui/options2/autofill_options_handler.h"
24 #include "chrome/browser/ui/webui/options2/browser_options_handler.h"
25 #include "chrome/browser/ui/webui/options2/clear_browser_data_handler.h"
26 #include "chrome/browser/ui/webui/options2/content_settings_handler.h"
27 #include "chrome/browser/ui/webui/options2/cookies_view_handler.h"
28 #include "chrome/browser/ui/webui/options2/core_options_handler.h"
29 #include "chrome/browser/ui/webui/options2/extension_settings_handler.h"
30 #include "chrome/browser/ui/webui/options2/font_settings_handler.h"
31 #include "chrome/browser/ui/webui/options2/handler_options_handler.h"
32 #include "chrome/browser/ui/webui/options2/import_data_handler.h"
33 #include "chrome/browser/ui/webui/options2/language_options_handler.h"
34 #include "chrome/browser/ui/webui/options2/manage_profile_handler.h"
35 #include "chrome/browser/ui/webui/options2/options_sync_setup_handler.h"
36 #include "chrome/browser/ui/webui/options2/pack_extension_handler.h"
37 #include "chrome/browser/ui/webui/options2/password_manager_handler.h"
38 #include "chrome/browser/ui/webui/options2/personal_options_handler.h"
39 #include "chrome/browser/ui/webui/options2/search_engine_manager_handler.h"
40 #include "chrome/browser/ui/webui/options2/stop_syncing_handler.h"
41 #include "chrome/browser/ui/webui/options2/web_intents_settings_handler.h"
42 #include "chrome/browser/ui/webui/theme_source.h"
43 #include "chrome/common/jstemplate_builder.h"
44 #include "chrome/common/time_format.h"
45 #include "chrome/common/url_constants.h"
46 #include "content/browser/renderer_host/render_view_host.h"
47 #include "content/browser/tab_contents/tab_contents.h"
48 #include "content/browser/tab_contents/tab_contents_delegate.h"
49 #include "content/public/browser/browser_thread.h"
50 #include "content/public/browser/notification_types.h"
51 #include "grit/chromium_strings.h"
52 #include "grit/generated_resources.h"
53 #include "grit/locale_settings.h"
54 #include "grit/options2_resources.h"
55 #include "grit/theme_resources.h"
56 #include "grit/theme_resources_standard.h"
57 #include "net/base/escape.h"
58 #include "ui/base/resource/resource_bundle.h"
59
60 #if defined(OS_CHROMEOS)
61 #include "chrome/browser/ui/webui/options2/chromeos/about_page_handler.h"
62 #include "chrome/browser/ui/webui/options2/chromeos/accounts_options_handler.h"
63 #include "chrome/browser/ui/webui/options2/chromeos/bluetooth_options_handler.h"
64 #include "chrome/browser/ui/webui/options2/chromeos/change_picture_options_handl er.h"
65 #include "chrome/browser/ui/webui/options2/chromeos/core_chromeos_options_handle r.h"
66 #include "chrome/browser/ui/webui/options2/chromeos/cros_language_options_handle r.h"
67 #include "chrome/browser/ui/webui/options2/chromeos/internet_options_handler.h"
68 #include "chrome/browser/ui/webui/options2/chromeos/language_chewing_handler.h"
69 #include "chrome/browser/ui/webui/options2/chromeos/language_customize_modifier_ keys_handler.h"
70 #include "chrome/browser/ui/webui/options2/chromeos/language_hangul_handler.h"
71 #include "chrome/browser/ui/webui/options2/chromeos/language_mozc_handler.h"
72 #include "chrome/browser/ui/webui/options2/chromeos/language_pinyin_handler.h"
73 #include "chrome/browser/ui/webui/options2/chromeos/proxy_handler.h"
74 #include "chrome/browser/ui/webui/options2/chromeos/stats_options_handler.h"
75 #include "chrome/browser/ui/webui/options2/chromeos/system_options_handler.h"
76 #include "chrome/browser/ui/webui/options2/chromeos/user_image_source.h"
77 #include "chrome/browser/ui/webui/options2/chromeos/virtual_keyboard_manager_han dler.h"
78 #endif
79
80 #if defined(USE_NSS)
81 #include "chrome/browser/ui/webui/options2/certificate_manager_handler.h"
82 #endif
83
84 static const char kLocalizedStringsFile[] = "strings.js";
85 static const char kOptionsBundleJsFile[] = "options_bundle.js";
86
87 ////////////////////////////////////////////////////////////////////////////////
88 //
89 // Options2UIHTMLSource
90 //
91 ////////////////////////////////////////////////////////////////////////////////
92
93 class Options2UIHTMLSource : public ChromeURLDataManager::DataSource {
94 public:
95 // The constructor takes over ownership of |localized_strings|.
96 explicit Options2UIHTMLSource(DictionaryValue* localized_strings);
97 virtual ~Options2UIHTMLSource();
98
99 // Called when the network layer has requested a resource underneath
100 // the path we registered.
101 virtual void StartDataRequest(const std::string& path,
102 bool is_incognito,
103 int request_id);
104 virtual std::string GetMimeType(const std::string&) const;
105
106 private:
107 // Localized strings collection.
108 scoped_ptr<DictionaryValue> localized_strings_;
109
110 DISALLOW_COPY_AND_ASSIGN(Options2UIHTMLSource);
111 };
112
113 Options2UIHTMLSource::Options2UIHTMLSource(DictionaryValue* localized_strings)
114 : DataSource(chrome::kChromeUISettingsFrameHost, MessageLoop::current()) {
115 DCHECK(localized_strings);
116 localized_strings_.reset(localized_strings);
117 }
118
119 Options2UIHTMLSource::~Options2UIHTMLSource() {}
120
121 void Options2UIHTMLSource::StartDataRequest(const std::string& path,
122 bool is_incognito,
123 int request_id) {
124 scoped_refptr<RefCountedMemory> response_bytes;
125 SetFontAndTextDirection(localized_strings_.get());
126
127 if (path == kLocalizedStringsFile) {
128 // Return dynamically-generated strings from memory.
129 std::string strings_js;
130 jstemplate_builder::AppendJsonJS(localized_strings_.get(), &strings_js);
131 response_bytes = base::RefCountedString::TakeString(&strings_js);
132 } else if (path == kOptionsBundleJsFile) {
133 // Return (and cache) the options javascript code.
134 response_bytes = ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
135 IDR_OPTIONS2_BUNDLE_JS);
136 } else {
137 // Return (and cache) the main options html page as the default.
138 response_bytes = ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
139 IDR_OPTIONS2_HTML);
140 }
141
142 SendResponse(request_id, response_bytes);
143 }
144
145 std::string Options2UIHTMLSource::GetMimeType(const std::string& path) const {
146 if (path == kLocalizedStringsFile || path == kOptionsBundleJsFile)
147 return "application/javascript";
148
149 return "text/html";
150 }
151
152 ////////////////////////////////////////////////////////////////////////////////
153 //
154 // OptionsPage2UIHandler
155 //
156 ////////////////////////////////////////////////////////////////////////////////
157
158 OptionsPage2UIHandler::OptionsPage2UIHandler() {
159 }
160
161 OptionsPage2UIHandler::~OptionsPage2UIHandler() {
162 }
163
164 bool OptionsPage2UIHandler::IsEnabled() {
165 return true;
166 }
167
168 // static
169 void OptionsPage2UIHandler::RegisterStrings(
170 DictionaryValue* localized_strings,
171 const OptionsStringResource* resources,
172 size_t length) {
173 for (size_t i = 0; i < length; ++i) {
174 localized_strings->SetString(
175 resources[i].name, l10n_util::GetStringUTF16(resources[i].id));
176 }
177 }
178
179 void OptionsPage2UIHandler::RegisterTitle(DictionaryValue* localized_strings,
180 const std::string& variable_name,
181 int title_id) {
182 localized_strings->SetString(variable_name,
183 l10n_util::GetStringUTF16(title_id));
184 localized_strings->SetString(variable_name + "TabTitle",
185 l10n_util::GetStringFUTF16(IDS_OPTIONS_TAB_TITLE,
186 l10n_util::GetStringUTF16(IDS_SETTINGS_TITLE),
187 l10n_util::GetStringUTF16(title_id)));
188 }
189
190 ////////////////////////////////////////////////////////////////////////////////
191 //
192 // Options2UI
193 //
194 ////////////////////////////////////////////////////////////////////////////////
195
196 Options2UI::Options2UI(TabContents* contents)
197 : ChromeWebUI(contents),
198 initialized_handlers_(false) {
199 DictionaryValue* localized_strings = new DictionaryValue();
200
201 CoreOptionsHandler* core_handler;
202 #if defined(OS_CHROMEOS)
203 core_handler = new chromeos::CoreChromeOSOptionsHandler();
204 #else
205 core_handler = new CoreOptionsHandler();
206 #endif
207 core_handler->set_handlers_host(this);
208 AddOptionsPageUIHandler(localized_strings, core_handler);
209
210 AddOptionsPageUIHandler(localized_strings, new AdvancedOptionsHandler());
211 AddOptionsPageUIHandler(localized_strings, new AutofillOptionsHandler());
212 AddOptionsPageUIHandler(localized_strings, new BrowserOptionsHandler());
213 AddOptionsPageUIHandler(localized_strings, new ClearBrowserDataHandler());
214 AddOptionsPageUIHandler(localized_strings, new ContentSettingsHandler());
215 AddOptionsPageUIHandler(localized_strings, new CookiesViewHandler());
216 AddOptionsPageUIHandler(localized_strings, new ExtensionSettingsHandler());
217 AddOptionsPageUIHandler(localized_strings, new FontSettingsHandler());
218 AddOptionsPageUIHandler(localized_strings, new WebIntentsSettingsHandler());
219 #if defined(OS_CHROMEOS)
220 AddOptionsPageUIHandler(localized_strings,
221 new chromeos::CrosLanguageOptionsHandler());
222 #else
223 AddOptionsPageUIHandler(localized_strings, new LanguageOptionsHandler());
224 #endif
225 AddOptionsPageUIHandler(localized_strings, new ManageProfileHandler());
226 AddOptionsPageUIHandler(localized_strings, new PackExtensionHandler());
227 AddOptionsPageUIHandler(localized_strings, new PasswordManagerHandler());
228 AddOptionsPageUIHandler(localized_strings, new PersonalOptionsHandler());
229 AddOptionsPageUIHandler(localized_strings, new SearchEngineManagerHandler());
230 AddOptionsPageUIHandler(localized_strings, new ImportDataHandler());
231 AddOptionsPageUIHandler(localized_strings, new StopSyncingHandler());
232 AddOptionsPageUIHandler(localized_strings, new OptionsSyncSetupHandler(
233 g_browser_process->profile_manager()));
234 #if defined(OS_CHROMEOS)
235 AddOptionsPageUIHandler(localized_strings,
236 new chromeos::AboutPageHandler());
237 AddOptionsPageUIHandler(localized_strings,
238 new chromeos::AccountsOptionsHandler());
239 AddOptionsPageUIHandler(localized_strings,
240 new chromeos::BluetoothOptionsHandler());
241 AddOptionsPageUIHandler(localized_strings, new InternetOptionsHandler());
242 AddOptionsPageUIHandler(localized_strings,
243 new chromeos::LanguageChewingHandler());
244 AddOptionsPageUIHandler(localized_strings,
245 new chromeos::LanguageCustomizeModifierKeysHandler());
246 AddOptionsPageUIHandler(localized_strings,
247 new chromeos::LanguageHangulHandler());
248 AddOptionsPageUIHandler(localized_strings,
249 new chromeos::LanguageMozcHandler());
250 AddOptionsPageUIHandler(localized_strings,
251 new chromeos::LanguagePinyinHandler());
252 AddOptionsPageUIHandler(localized_strings,
253 new chromeos::VirtualKeyboardManagerHandler());
254 AddOptionsPageUIHandler(localized_strings,
255 new chromeos::ProxyHandler());
256 AddOptionsPageUIHandler(localized_strings,
257 new chromeos::ChangePictureOptionsHandler());
258 AddOptionsPageUIHandler(localized_strings,
259 new chromeos::StatsOptionsHandler());
260 AddOptionsPageUIHandler(localized_strings, new SystemOptionsHandler());
261 #endif
262 #if defined(USE_NSS)
263 AddOptionsPageUIHandler(localized_strings, new CertificateManagerHandler());
264 #endif
265 AddOptionsPageUIHandler(localized_strings, new HandlerOptionsHandler());
266
267 // |localized_strings| ownership is taken over by this constructor.
268 Options2UIHTMLSource* html_source =
269 new Options2UIHTMLSource(localized_strings);
270
271 // Set up the chrome://settings-frame/ source.
272 Profile* profile = Profile::FromBrowserContext(contents->browser_context());
273 profile->GetChromeURLDataManager()->AddDataSource(html_source);
274
275 // Set up the chrome://theme/ source.
276 ThemeSource* theme = new ThemeSource(profile);
277 profile->GetChromeURLDataManager()->AddDataSource(theme);
278
279 #if defined(OS_CHROMEOS)
280 // Set up the chrome://userimage/ source.
281 chromeos::UserImageSource* user_image_source =
282 new chromeos::UserImageSource();
283 profile->GetChromeURLDataManager()->AddDataSource(user_image_source);
284 #endif
285 }
286
287 Options2UI::~Options2UI() {
288 // Uninitialize all registered handlers. The base class owns them and it will
289 // eventually delete them. Skip over the generic handler.
290 for (std::vector<WebUIMessageHandler*>::iterator iter = handlers_.begin() + 1;
291 iter != handlers_.end();
292 ++iter) {
293 static_cast<OptionsPage2UIHandler*>(*iter)->Uninitialize();
294 }
295 }
296
297 // Override.
298 void Options2UI::RenderViewCreated(RenderViewHost* render_view_host) {
299 SetCommandLineString(render_view_host);
300 ChromeWebUI::RenderViewCreated(render_view_host);
301 }
302
303 void Options2UI::RenderViewReused(RenderViewHost* render_view_host) {
304 SetCommandLineString(render_view_host);
305 ChromeWebUI::RenderViewReused(render_view_host);
306 }
307
308 void Options2UI::DidBecomeActiveForReusedRenderView() {
309 // When the renderer is re-used (e.g., for back/forward navigation within
310 // options), the handlers are torn down and rebuilt, so are no longer
311 // initialized, but the web page's DOM may remain intact, in which case onload
312 // won't fire to initilize the handlers. To make sure initialization always
313 // happens, call reinitializeCore (which is a no-op unless the DOM was already
314 // initialized).
315 CallJavascriptFunction("OptionsPage.reinitializeCore");
316
317 ChromeWebUI::DidBecomeActiveForReusedRenderView();
318 }
319
320 // static
321 RefCountedMemory* Options2UI::GetFaviconResourceBytes() {
322 return ResourceBundle::GetSharedInstance().
323 LoadDataResourceBytes(IDR_SETTINGS_FAVICON);
324 }
325
326 void Options2UI::InitializeHandlers() {
327 DCHECK(!GetProfile()->IsOffTheRecord() || Profile::IsGuestSession());
328
329 // The reinitialize call from DidBecomeActiveForReusedRenderView end up being
330 // delivered after a new web page DOM has been brought up in an existing
331 // renderer (due to IPC delays), causing this method to be called twice. If
332 // that happens, ignore the second call.
333 if (initialized_handlers_)
334 return;
335 initialized_handlers_ = true;
336
337 std::vector<WebUIMessageHandler*>::iterator iter;
338 // Skip over the generic handler.
339 for (iter = handlers_.begin() + 1; iter != handlers_.end(); ++iter) {
340 (static_cast<OptionsPage2UIHandler*>(*iter))->Initialize();
341 }
342 }
343
344 void Options2UI::AddOptionsPageUIHandler(DictionaryValue* localized_strings,
345 OptionsPage2UIHandler* handler_raw) {
346 scoped_ptr<OptionsPage2UIHandler> handler(handler_raw);
347 DCHECK(handler.get());
348 // Add only if handler's service is enabled.
349 if (handler->IsEnabled()) {
350 handler->GetLocalizedValues(localized_strings);
351 // Add handler to the list and also pass the ownership.
352 AddMessageHandler(handler.release()->Attach(this));
353 }
354 }
355
356 void Options2UI::SetCommandLineString(RenderViewHost* render_view_host) {
357 std::string command_line_string;
358
359 #if defined(OS_WIN)
360 command_line_string =
361 WideToASCII(CommandLine::ForCurrentProcess()->GetCommandLineString());
362 #else
363 command_line_string =
364 CommandLine::ForCurrentProcess()->GetCommandLineString();
365 #endif
366
367 render_view_host->SetWebUIProperty("commandLineString", command_line_string);
368 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/options2/options_ui2.h ('k') | chrome/browser/ui/webui/options2/options_ui_uitest.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698