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