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