OLD | NEW |
| (Empty) |
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. | |
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/ref_counted_memory.h" | |
13 #include "base/memory/singleton.h" | |
14 #include "base/message_loop.h" | |
15 #include "base/string_piece.h" | |
16 #include "base/string_util.h" | |
17 #include "base/threading/thread.h" | |
18 #include "base/time.h" | |
19 #include "base/values.h" | |
20 #include "chrome/browser/autocomplete/autocomplete_match.h" | |
21 #include "chrome/browser/autocomplete/autocomplete_result.h" | |
22 #include "chrome/browser/browser_about_handler.h" | |
23 #include "chrome/browser/browser_process.h" | |
24 #include "chrome/browser/profiles/profile.h" | |
25 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
26 #include "chrome/browser/ui/webui/options2/autofill_options_handler.h" | |
27 #include "chrome/browser/ui/webui/options2/browser_options_handler.h" | |
28 #include "chrome/browser/ui/webui/options2/clear_browser_data_handler.h" | |
29 #include "chrome/browser/ui/webui/options2/content_settings_handler.h" | |
30 #include "chrome/browser/ui/webui/options2/cookies_view_handler.h" | |
31 #include "chrome/browser/ui/webui/options2/core_options_handler.h" | |
32 #include "chrome/browser/ui/webui/options2/font_settings_handler.h" | |
33 #include "chrome/browser/ui/webui/options2/handler_options_handler.h" | |
34 #include "chrome/browser/ui/webui/options2/home_page_overlay_handler.h" | |
35 #include "chrome/browser/ui/webui/options2/import_data_handler.h" | |
36 #include "chrome/browser/ui/webui/options2/language_options_handler.h" | |
37 #include "chrome/browser/ui/webui/options2/manage_profile_handler.h" | |
38 #include "chrome/browser/ui/webui/options2/media_galleries_handler.h" | |
39 #include "chrome/browser/ui/webui/options2/options_sync_setup_handler.h" | |
40 #include "chrome/browser/ui/webui/options2/password_manager_handler.h" | |
41 #include "chrome/browser/ui/webui/options2/search_engine_manager_handler.h" | |
42 #include "chrome/browser/ui/webui/options2/startup_pages_handler.h" | |
43 #include "chrome/browser/ui/webui/options2/web_intents_settings_handler.h" | |
44 #include "chrome/browser/ui/webui/theme_source.h" | |
45 #include "chrome/common/jstemplate_builder.h" | |
46 #include "chrome/common/time_format.h" | |
47 #include "chrome/common/url_constants.h" | |
48 #include "content/public/browser/browser_thread.h" | |
49 #include "content/public/browser/notification_types.h" | |
50 #include "content/public/browser/render_view_host.h" | |
51 #include "content/public/browser/web_contents.h" | |
52 #include "content/public/browser/web_contents_delegate.h" | |
53 #include "content/public/browser/web_ui.h" | |
54 #include "grit/chromium_strings.h" | |
55 #include "grit/generated_resources.h" | |
56 #include "grit/locale_settings.h" | |
57 #include "grit/options_resources.h" | |
58 #include "grit/theme_resources.h" | |
59 #include "net/base/escape.h" | |
60 #include "ui/base/layout.h" | |
61 #include "ui/base/resource/resource_bundle.h" | |
62 | |
63 #if defined(OS_CHROMEOS) | |
64 #include "chrome/browser/chromeos/system/pointer_device_observer.h" | |
65 #include "chrome/browser/ui/webui/options2/chromeos/accounts_options_handler.h" | |
66 #include "chrome/browser/ui/webui/options2/chromeos/bluetooth_options_handler.h" | |
67 #include "chrome/browser/ui/webui/options2/chromeos/change_picture_options_handl
er.h" | |
68 #include "chrome/browser/ui/webui/options2/chromeos/core_chromeos_options_handle
r.h" | |
69 #include "chrome/browser/ui/webui/options2/chromeos/cros_language_options_handle
r.h" | |
70 #include "chrome/browser/ui/webui/options2/chromeos/display_options_handler.h" | |
71 #include "chrome/browser/ui/webui/options2/chromeos/internet_options_handler.h" | |
72 #include "chrome/browser/ui/webui/options2/chromeos/keyboard_handler.h" | |
73 #include "chrome/browser/ui/webui/options2/chromeos/language_chewing_handler.h" | |
74 #include "chrome/browser/ui/webui/options2/chromeos/language_hangul_handler.h" | |
75 #include "chrome/browser/ui/webui/options2/chromeos/language_mozc_handler.h" | |
76 #include "chrome/browser/ui/webui/options2/chromeos/language_pinyin_handler.h" | |
77 #include "chrome/browser/ui/webui/options2/chromeos/pointer_handler.h" | |
78 #include "chrome/browser/ui/webui/options2/chromeos/proxy_handler.h" | |
79 #include "chrome/browser/ui/webui/options2/chromeos/stats_options_handler.h" | |
80 #include "chrome/browser/ui/webui/options2/chromeos/user_image_source.h" | |
81 #include "chrome/browser/ui/webui/options2/chromeos/wallpaper_thumbnail_source.h
" | |
82 #endif | |
83 | |
84 #if defined(OS_CHROMEOS) && defined(USE_ASH) | |
85 #include "chrome/browser/ui/webui/options2/chromeos/set_wallpaper_options_handle
r.h" | |
86 #endif | |
87 | |
88 #if defined(USE_NSS) | |
89 #include "chrome/browser/ui/webui/options2/certificate_manager_handler.h" | |
90 #endif | |
91 | |
92 using content::RenderViewHost; | |
93 | |
94 namespace { | |
95 | |
96 const char kLocalizedStringsFile[] = "strings.js"; | |
97 const char kOptionsBundleJsFile[] = "options_bundle.js"; | |
98 | |
99 } // namespace | |
100 | |
101 namespace options { | |
102 | |
103 //////////////////////////////////////////////////////////////////////////////// | |
104 // | |
105 // OptionsUIHTMLSource | |
106 // | |
107 //////////////////////////////////////////////////////////////////////////////// | |
108 | |
109 class OptionsUIHTMLSource : public ChromeURLDataManager::DataSource { | |
110 public: | |
111 // The constructor takes over ownership of |localized_strings|. | |
112 explicit OptionsUIHTMLSource(DictionaryValue* localized_strings); | |
113 | |
114 // Called when the network layer has requested a resource underneath | |
115 // the path we registered. | |
116 virtual void StartDataRequest(const std::string& path, | |
117 bool is_incognito, | |
118 int request_id); | |
119 virtual std::string GetMimeType(const std::string&) const; | |
120 | |
121 private: | |
122 virtual ~OptionsUIHTMLSource(); | |
123 | |
124 // Localized strings collection. | |
125 scoped_ptr<DictionaryValue> localized_strings_; | |
126 | |
127 DISALLOW_COPY_AND_ASSIGN(OptionsUIHTMLSource); | |
128 }; | |
129 | |
130 OptionsUIHTMLSource::OptionsUIHTMLSource(DictionaryValue* localized_strings) | |
131 : DataSource(chrome::kChromeUISettingsFrameHost, MessageLoop::current()) { | |
132 DCHECK(localized_strings); | |
133 localized_strings_.reset(localized_strings); | |
134 } | |
135 | |
136 void OptionsUIHTMLSource::StartDataRequest(const std::string& path, | |
137 bool is_incognito, | |
138 int request_id) { | |
139 scoped_refptr<base::RefCountedMemory> response_bytes; | |
140 SetFontAndTextDirection(localized_strings_.get()); | |
141 | |
142 if (path == kLocalizedStringsFile) { | |
143 // Return dynamically-generated strings from memory. | |
144 jstemplate_builder::UseVersion2 version; | |
145 std::string strings_js; | |
146 jstemplate_builder::AppendJsonJS(localized_strings_.get(), &strings_js); | |
147 response_bytes = base::RefCountedString::TakeString(&strings_js); | |
148 } else if (path == kOptionsBundleJsFile) { | |
149 // Return (and cache) the options javascript code. | |
150 response_bytes = ui::ResourceBundle::GetSharedInstance(). | |
151 LoadDataResourceBytes(IDR_OPTIONS_BUNDLE_JS, ui::SCALE_FACTOR_NONE); | |
152 } else { | |
153 // Return (and cache) the main options html page as the default. | |
154 response_bytes = ui::ResourceBundle::GetSharedInstance(). | |
155 LoadDataResourceBytes(IDR_OPTIONS_HTML, ui::SCALE_FACTOR_NONE); | |
156 } | |
157 | |
158 SendResponse(request_id, response_bytes); | |
159 } | |
160 | |
161 std::string OptionsUIHTMLSource::GetMimeType(const std::string& path) const { | |
162 if (path == kLocalizedStringsFile || path == kOptionsBundleJsFile) | |
163 return "application/javascript"; | |
164 | |
165 return "text/html"; | |
166 } | |
167 | |
168 OptionsUIHTMLSource::~OptionsUIHTMLSource() {} | |
169 | |
170 //////////////////////////////////////////////////////////////////////////////// | |
171 // | |
172 // OptionsPageUIHandler | |
173 // | |
174 //////////////////////////////////////////////////////////////////////////////// | |
175 | |
176 OptionsPageUIHandler::OptionsPageUIHandler() { | |
177 } | |
178 | |
179 OptionsPageUIHandler::~OptionsPageUIHandler() { | |
180 } | |
181 | |
182 bool OptionsPageUIHandler::IsEnabled() { | |
183 return true; | |
184 } | |
185 | |
186 // static | |
187 void OptionsPageUIHandler::RegisterStrings( | |
188 DictionaryValue* localized_strings, | |
189 const OptionsStringResource* resources, | |
190 size_t length) { | |
191 for (size_t i = 0; i < length; ++i) { | |
192 localized_strings->SetString( | |
193 resources[i].name, l10n_util::GetStringUTF16(resources[i].id)); | |
194 } | |
195 } | |
196 | |
197 void OptionsPageUIHandler::RegisterTitle(DictionaryValue* localized_strings, | |
198 const std::string& variable_name, | |
199 int title_id) { | |
200 localized_strings->SetString(variable_name, | |
201 l10n_util::GetStringUTF16(title_id)); | |
202 localized_strings->SetString(variable_name + "TabTitle", | |
203 l10n_util::GetStringFUTF16(IDS_OPTIONS_TAB_TITLE, | |
204 l10n_util::GetStringUTF16(IDS_SETTINGS_TITLE), | |
205 l10n_util::GetStringUTF16(title_id))); | |
206 } | |
207 | |
208 //////////////////////////////////////////////////////////////////////////////// | |
209 // | |
210 // OptionsUI | |
211 // | |
212 //////////////////////////////////////////////////////////////////////////////// | |
213 | |
214 OptionsUI::OptionsUI(content::WebUI* web_ui) | |
215 : WebUIController(web_ui), | |
216 initialized_handlers_(false) { | |
217 DictionaryValue* localized_strings = new DictionaryValue(); | |
218 | |
219 CoreOptionsHandler* core_handler; | |
220 #if defined(OS_CHROMEOS) | |
221 core_handler = new chromeos::options::CoreChromeOSOptionsHandler(); | |
222 #else | |
223 core_handler = new CoreOptionsHandler(); | |
224 #endif | |
225 core_handler->set_handlers_host(this); | |
226 AddOptionsPageUIHandler(localized_strings, core_handler); | |
227 | |
228 AddOptionsPageUIHandler(localized_strings, new AutofillOptionsHandler()); | |
229 | |
230 BrowserOptionsHandler* browser_options_handler = new BrowserOptionsHandler(); | |
231 AddOptionsPageUIHandler(localized_strings, browser_options_handler); | |
232 | |
233 AddOptionsPageUIHandler(localized_strings, new ClearBrowserDataHandler()); | |
234 AddOptionsPageUIHandler(localized_strings, new ContentSettingsHandler()); | |
235 AddOptionsPageUIHandler(localized_strings, new CookiesViewHandler()); | |
236 AddOptionsPageUIHandler(localized_strings, new FontSettingsHandler()); | |
237 AddOptionsPageUIHandler(localized_strings, new HomePageOverlayHandler()); | |
238 AddOptionsPageUIHandler(localized_strings, new MediaGalleriesHandler()); | |
239 AddOptionsPageUIHandler(localized_strings, new WebIntentsSettingsHandler()); | |
240 #if defined(OS_CHROMEOS) | |
241 AddOptionsPageUIHandler(localized_strings, | |
242 new chromeos::options::CrosLanguageOptionsHandler()); | |
243 #else | |
244 AddOptionsPageUIHandler(localized_strings, new LanguageOptionsHandler()); | |
245 #endif | |
246 AddOptionsPageUIHandler(localized_strings, new ManageProfileHandler()); | |
247 AddOptionsPageUIHandler(localized_strings, new PasswordManagerHandler()); | |
248 AddOptionsPageUIHandler(localized_strings, new SearchEngineManagerHandler()); | |
249 AddOptionsPageUIHandler(localized_strings, new ImportDataHandler()); | |
250 AddOptionsPageUIHandler(localized_strings, new StartupPagesHandler()); | |
251 AddOptionsPageUIHandler(localized_strings, new OptionsSyncSetupHandler( | |
252 g_browser_process->profile_manager())); | |
253 #if defined(OS_CHROMEOS) | |
254 AddOptionsPageUIHandler(localized_strings, | |
255 new chromeos::options::AccountsOptionsHandler()); | |
256 AddOptionsPageUIHandler(localized_strings, | |
257 new chromeos::options::BluetoothOptionsHandler()); | |
258 AddOptionsPageUIHandler(localized_strings, | |
259 new chromeos::options::DisplayOptionsHandler()); | |
260 AddOptionsPageUIHandler(localized_strings, new InternetOptionsHandler()); | |
261 AddOptionsPageUIHandler(localized_strings, | |
262 new chromeos::options::LanguageChewingHandler()); | |
263 AddOptionsPageUIHandler(localized_strings, | |
264 new chromeos::options::KeyboardHandler()); | |
265 AddOptionsPageUIHandler(localized_strings, | |
266 new chromeos::options::LanguageHangulHandler()); | |
267 AddOptionsPageUIHandler(localized_strings, | |
268 new chromeos::options::LanguageMozcHandler()); | |
269 AddOptionsPageUIHandler(localized_strings, | |
270 new chromeos::options::LanguagePinyinHandler()); | |
271 | |
272 chromeos::options::PointerHandler* pointer_handler = | |
273 new chromeos::options::PointerHandler(); | |
274 AddOptionsPageUIHandler(localized_strings, pointer_handler); | |
275 | |
276 AddOptionsPageUIHandler(localized_strings, | |
277 new chromeos::options::ProxyHandler()); | |
278 AddOptionsPageUIHandler( | |
279 localized_strings, | |
280 new chromeos::options::ChangePictureOptionsHandler()); | |
281 AddOptionsPageUIHandler(localized_strings, | |
282 new chromeos::options::StatsOptionsHandler()); | |
283 #endif | |
284 #if defined(OS_CHROMEOS) && defined(USE_ASH) | |
285 AddOptionsPageUIHandler( | |
286 localized_strings, | |
287 new chromeos::options::SetWallpaperOptionsHandler()); | |
288 #endif | |
289 #if defined(USE_NSS) | |
290 AddOptionsPageUIHandler(localized_strings, new CertificateManagerHandler()); | |
291 #endif | |
292 AddOptionsPageUIHandler(localized_strings, new HandlerOptionsHandler()); | |
293 | |
294 // |localized_strings| ownership is taken over by this constructor. | |
295 OptionsUIHTMLSource* html_source = | |
296 new OptionsUIHTMLSource(localized_strings); | |
297 | |
298 // Set up the chrome://settings-frame/ source. | |
299 Profile* profile = Profile::FromWebUI(web_ui); | |
300 ChromeURLDataManager::AddDataSource(profile, html_source); | |
301 | |
302 // Set up the chrome://theme/ source. | |
303 ThemeSource* theme = new ThemeSource(profile); | |
304 ChromeURLDataManager::AddDataSource(profile, theme); | |
305 | |
306 #if defined(OS_CHROMEOS) | |
307 // Set up the chrome://userimage/ source. | |
308 chromeos::options::UserImageSource* user_image_source = | |
309 new chromeos::options::UserImageSource(); | |
310 ChromeURLDataManager::AddDataSource(profile, user_image_source); | |
311 | |
312 // Set up the chrome://wallpaper-thumb/ source. | |
313 chromeos::options::WallpaperThumbnailSource* wallpaper_thumbnail_source = | |
314 new chromeos::options::WallpaperThumbnailSource(); | |
315 ChromeURLDataManager::AddDataSource(profile, wallpaper_thumbnail_source); | |
316 | |
317 pointer_device_observer_.reset( | |
318 new chromeos::system::PointerDeviceObserver()); | |
319 pointer_device_observer_->AddObserver(browser_options_handler); | |
320 pointer_device_observer_->AddObserver(pointer_handler); | |
321 #endif | |
322 } | |
323 | |
324 OptionsUI::~OptionsUI() { | |
325 // Uninitialize all registered handlers. Deleted by WebUIImpl. | |
326 for (size_t i = 0; i < handlers_.size(); ++i) | |
327 handlers_[i]->Uninitialize(); | |
328 } | |
329 | |
330 // static | |
331 void OptionsUI::ProcessAutocompleteSuggestions( | |
332 const AutocompleteResult& result, | |
333 base::ListValue* const suggestions) { | |
334 for (size_t i = 0; i < result.size(); ++i) { | |
335 const AutocompleteMatch& match = result.match_at(i); | |
336 AutocompleteMatch::Type type = match.type; | |
337 if (type != AutocompleteMatch::HISTORY_URL && | |
338 type != AutocompleteMatch::HISTORY_TITLE && | |
339 type != AutocompleteMatch::HISTORY_BODY && | |
340 type != AutocompleteMatch::HISTORY_KEYWORD && | |
341 type != AutocompleteMatch::NAVSUGGEST) | |
342 continue; | |
343 base::DictionaryValue* entry = new base::DictionaryValue(); | |
344 entry->SetString("title", match.description); | |
345 entry->SetString("displayURL", match.contents); | |
346 entry->SetString("url", match.destination_url.spec()); | |
347 suggestions->Append(entry); | |
348 } | |
349 } | |
350 | |
351 // static | |
352 base::RefCountedMemory* OptionsUI::GetFaviconResourceBytes() { | |
353 return ui::ResourceBundle::GetSharedInstance(). | |
354 LoadDataResourceBytes(IDR_SETTINGS_FAVICON, ui::SCALE_FACTOR_100P); | |
355 } | |
356 | |
357 void OptionsUI::InitializeHandlers() { | |
358 Profile* profile = Profile::FromWebUI(web_ui()); | |
359 DCHECK(!profile->IsOffTheRecord() || Profile::IsGuestSession()); | |
360 | |
361 // A new web page DOM has been brought up in an existing renderer, causing | |
362 // this method to be called twice. If that happens, ignore the second call. | |
363 if (!initialized_handlers_) { | |
364 for (size_t i = 0; i < handlers_.size(); ++i) | |
365 handlers_[i]->InitializeHandler(); | |
366 initialized_handlers_ = true; | |
367 | |
368 #if defined(OS_CHROMEOS) | |
369 pointer_device_observer_->Init(); | |
370 #endif | |
371 } | |
372 | |
373 #if defined(OS_CHROMEOS) | |
374 pointer_device_observer_->CheckDevices(); | |
375 #endif | |
376 | |
377 // Always initialize the page as when handlers are left over we still need to | |
378 // do various things like show/hide sections and send data to the Javascript. | |
379 for (size_t i = 0; i < handlers_.size(); ++i) | |
380 handlers_[i]->InitializePage(); | |
381 } | |
382 | |
383 void OptionsUI::AddOptionsPageUIHandler(DictionaryValue* localized_strings, | |
384 OptionsPageUIHandler* handler_raw) { | |
385 scoped_ptr<OptionsPageUIHandler> handler(handler_raw); | |
386 DCHECK(handler.get()); | |
387 // Add only if handler's service is enabled. | |
388 if (handler->IsEnabled()) { | |
389 // Add handler to the list and also pass the ownership. | |
390 web_ui()->AddMessageHandler(handler.release()); | |
391 handler_raw->GetLocalizedValues(localized_strings); | |
392 handlers_.push_back(handler_raw); | |
393 } | |
394 } | |
395 | |
396 } // namespace options | |
OLD | NEW |