blob: 0d86576fb1ce6a05558e99f0301ecec5a91d1580 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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/options/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"
Ben Murdoch9ab55632013-07-18 11:57:30 +010014#include "base/message_loop/message_loop.h"
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010015#include "base/strings/string_piece.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010016#include "base/strings/string_util.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000017#include "base/threading/thread.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010018#include "base/time/time.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019#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"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000025#include "chrome/browser/ui/webui/options/autofill_options_handler.h"
26#include "chrome/browser/ui/webui/options/browser_options_handler.h"
27#include "chrome/browser/ui/webui/options/clear_browser_data_handler.h"
28#include "chrome/browser/ui/webui/options/content_settings_handler.h"
29#include "chrome/browser/ui/webui/options/cookies_view_handler.h"
30#include "chrome/browser/ui/webui/options/core_options_handler.h"
31#include "chrome/browser/ui/webui/options/font_settings_handler.h"
32#include "chrome/browser/ui/webui/options/handler_options_handler.h"
33#include "chrome/browser/ui/webui/options/home_page_overlay_handler.h"
34#include "chrome/browser/ui/webui/options/import_data_handler.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000035#include "chrome/browser/ui/webui/options/language_dictionary_overlay_handler.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000036#include "chrome/browser/ui/webui/options/language_options_handler.h"
37#include "chrome/browser/ui/webui/options/manage_profile_handler.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010038#include "chrome/browser/ui/webui/options/managed_user_create_confirm_handler.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010039#include "chrome/browser/ui/webui/options/managed_user_learn_more_handler.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000040#include "chrome/browser/ui/webui/options/media_devices_selection_handler.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000041#include "chrome/browser/ui/webui/options/media_galleries_handler.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000042#include "chrome/browser/ui/webui/options/password_manager_handler.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010043#include "chrome/browser/ui/webui/options/reset_profile_settings_handler.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000044#include "chrome/browser/ui/webui/options/search_engine_manager_handler.h"
45#include "chrome/browser/ui/webui/options/startup_pages_handler.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010046#include "chrome/browser/ui/webui/sync_setup_handler.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000047#include "chrome/browser/ui/webui/theme_source.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000048#include "chrome/common/url_constants.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000049#include "content/public/browser/notification_types.h"
50#include "content/public/browser/render_view_host.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000051#include "content/public/browser/url_data_source.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000052#include "content/public/browser/web_contents.h"
53#include "content/public/browser/web_contents_delegate.h"
54#include "content/public/browser/web_ui.h"
55#include "grit/chromium_strings.h"
56#include "grit/generated_resources.h"
57#include "grit/locale_settings.h"
58#include "grit/options_resources.h"
59#include "grit/theme_resources.h"
60#include "net/base/escape.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010061#include "ui/base/l10n/l10n_util.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000062#include "ui/base/resource/resource_bundle.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000063#include "ui/webui/jstemplate_builder.h"
64#include "ui/webui/web_ui_util.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000065
66#if defined(OS_CHROMEOS)
67#include "chrome/browser/chromeos/system/pointer_device_observer.h"
68#include "chrome/browser/ui/webui/options/chromeos/accounts_options_handler.h"
69#include "chrome/browser/ui/webui/options/chromeos/bluetooth_options_handler.h"
70#include "chrome/browser/ui/webui/options/chromeos/change_picture_options_handler.h"
71#include "chrome/browser/ui/webui/options/chromeos/core_chromeos_options_handler.h"
72#include "chrome/browser/ui/webui/options/chromeos/cros_language_options_handler.h"
73#include "chrome/browser/ui/webui/options/chromeos/display_options_handler.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000074#include "chrome/browser/ui/webui/options/chromeos/display_overscan_handler.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000075#include "chrome/browser/ui/webui/options/chromeos/internet_options_handler.h"
76#include "chrome/browser/ui/webui/options/chromeos/keyboard_handler.h"
77#include "chrome/browser/ui/webui/options/chromeos/language_chewing_handler.h"
78#include "chrome/browser/ui/webui/options/chromeos/language_hangul_handler.h"
79#include "chrome/browser/ui/webui/options/chromeos/language_mozc_handler.h"
80#include "chrome/browser/ui/webui/options/chromeos/language_pinyin_handler.h"
81#include "chrome/browser/ui/webui/options/chromeos/pointer_handler.h"
82#include "chrome/browser/ui/webui/options/chromeos/proxy_handler.h"
83#include "chrome/browser/ui/webui/options/chromeos/stats_options_handler.h"
84#include "chrome/browser/ui/webui/options/chromeos/user_image_source.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000085#endif
86
87#if defined(USE_NSS)
88#include "chrome/browser/ui/webui/options/certificate_manager_handler.h"
89#endif
90
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010091#if defined(ENABLE_GOOGLE_NOW)
92#include "chrome/browser/ui/webui/options/geolocation_options_handler.h"
93#endif
94
Torne (Richard Coles)58218062012-11-14 11:43:16 +000095using content::RenderViewHost;
96
97namespace {
98
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000099const char kLocalizedStringsFile[] = "strings.js";
100const char kOptionsBundleJsFile[] = "options_bundle.js";
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000101
102} // namespace
103
104namespace options {
105
106////////////////////////////////////////////////////////////////////////////////
107//
108// OptionsUIHTMLSource
109//
110////////////////////////////////////////////////////////////////////////////////
111
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000112class OptionsUIHTMLSource : public content::URLDataSource {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000113 public:
114 // The constructor takes over ownership of |localized_strings|.
115 explicit OptionsUIHTMLSource(DictionaryValue* localized_strings);
116
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000117 // content::URLDataSource implementation.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100118 virtual std::string GetSource() const OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000119 virtual void StartDataRequest(
120 const std::string& path,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100121 int render_process_id,
122 int render_view_id,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000123 const content::URLDataSource::GotDataCallback& callback) OVERRIDE;
124 virtual std::string GetMimeType(const std::string&) const OVERRIDE;
125 virtual bool ShouldDenyXFrameOptions() const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000126
127 private:
128 virtual ~OptionsUIHTMLSource();
129
130 // Localized strings collection.
131 scoped_ptr<DictionaryValue> localized_strings_;
132
133 DISALLOW_COPY_AND_ASSIGN(OptionsUIHTMLSource);
134};
135
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000136OptionsUIHTMLSource::OptionsUIHTMLSource(DictionaryValue* localized_strings) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000137 DCHECK(localized_strings);
138 localized_strings_.reset(localized_strings);
139}
140
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100141std::string OptionsUIHTMLSource::GetSource() const {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000142 return chrome::kChromeUISettingsFrameHost;
143}
144
145void OptionsUIHTMLSource::StartDataRequest(
146 const std::string& path,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100147 int render_process_id,
148 int render_view_id,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000149 const content::URLDataSource::GotDataCallback& callback) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000150 scoped_refptr<base::RefCountedMemory> response_bytes;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000151 webui::SetFontAndTextDirection(localized_strings_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000152
153 if (path == kLocalizedStringsFile) {
154 // Return dynamically-generated strings from memory.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000155 webui::UseVersion2 version;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000156 std::string strings_js;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000157 webui::AppendJsonJS(localized_strings_.get(), &strings_js);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000158 response_bytes = base::RefCountedString::TakeString(&strings_js);
159 } else if (path == kOptionsBundleJsFile) {
160 // Return (and cache) the options javascript code.
161 response_bytes = ui::ResourceBundle::GetSharedInstance().
162 LoadDataResourceBytes(IDR_OPTIONS_BUNDLE_JS);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000163 } else {
164 // Return (and cache) the main options html page as the default.
165 response_bytes = ui::ResourceBundle::GetSharedInstance().
166 LoadDataResourceBytes(IDR_OPTIONS_HTML);
167 }
168
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100169 callback.Run(response_bytes.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000170}
171
172std::string OptionsUIHTMLSource::GetMimeType(const std::string& path) const {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000173 if (path == kLocalizedStringsFile || path == kOptionsBundleJsFile)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000174 return "application/javascript";
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000175
176 return "text/html";
177}
178
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000179bool OptionsUIHTMLSource::ShouldDenyXFrameOptions() const {
180 return false;
181}
182
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000183OptionsUIHTMLSource::~OptionsUIHTMLSource() {}
184
185////////////////////////////////////////////////////////////////////////////////
186//
187// OptionsPageUIHandler
188//
189////////////////////////////////////////////////////////////////////////////////
190
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000191const char OptionsPageUIHandler::kSettingsAppKey[] = "settingsApp";
192
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000193OptionsPageUIHandler::OptionsPageUIHandler() {
194}
195
196OptionsPageUIHandler::~OptionsPageUIHandler() {
197}
198
199bool OptionsPageUIHandler::IsEnabled() {
200 return true;
201}
202
203// static
204void OptionsPageUIHandler::RegisterStrings(
205 DictionaryValue* localized_strings,
206 const OptionsStringResource* resources,
207 size_t length) {
208 for (size_t i = 0; i < length; ++i) {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000209 string16 value;
210 if (resources[i].substitution_id == 0) {
211 value = l10n_util::GetStringUTF16(resources[i].id);
212 } else {
213 value = l10n_util::GetStringFUTF16(
214 resources[i].id,
215 l10n_util::GetStringUTF16(resources[i].substitution_id));
216 }
217 localized_strings->SetString(resources[i].name, value);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000218 }
219}
220
221void OptionsPageUIHandler::RegisterTitle(DictionaryValue* localized_strings,
222 const std::string& variable_name,
223 int title_id) {
224 localized_strings->SetString(variable_name,
225 l10n_util::GetStringUTF16(title_id));
226 localized_strings->SetString(variable_name + "TabTitle",
227 l10n_util::GetStringFUTF16(IDS_OPTIONS_TAB_TITLE,
228 l10n_util::GetStringUTF16(IDS_SETTINGS_TITLE),
229 l10n_util::GetStringUTF16(title_id)));
230}
231
232////////////////////////////////////////////////////////////////////////////////
233//
234// OptionsUI
235//
236////////////////////////////////////////////////////////////////////////////////
237
238OptionsUI::OptionsUI(content::WebUI* web_ui)
239 : WebUIController(web_ui),
240 initialized_handlers_(false) {
241 DictionaryValue* localized_strings = new DictionaryValue();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000242 localized_strings->Set(OptionsPageUIHandler::kSettingsAppKey,
243 new DictionaryValue());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000244
245 CoreOptionsHandler* core_handler;
246#if defined(OS_CHROMEOS)
247 core_handler = new chromeos::options::CoreChromeOSOptionsHandler();
248#else
249 core_handler = new CoreOptionsHandler();
250#endif
251 core_handler->set_handlers_host(this);
252 AddOptionsPageUIHandler(localized_strings, core_handler);
253
254 AddOptionsPageUIHandler(localized_strings, new AutofillOptionsHandler());
255
256 BrowserOptionsHandler* browser_options_handler = new BrowserOptionsHandler();
257 AddOptionsPageUIHandler(localized_strings, browser_options_handler);
258
259 AddOptionsPageUIHandler(localized_strings, new ClearBrowserDataHandler());
260 AddOptionsPageUIHandler(localized_strings, new ContentSettingsHandler());
261 AddOptionsPageUIHandler(localized_strings, new CookiesViewHandler());
262 AddOptionsPageUIHandler(localized_strings, new FontSettingsHandler());
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100263#if defined(ENABLE_GOOGLE_NOW)
264 AddOptionsPageUIHandler(localized_strings, new GeolocationOptionsHandler());
265#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000266 AddOptionsPageUIHandler(localized_strings, new HomePageOverlayHandler());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000267 AddOptionsPageUIHandler(localized_strings,
268 new MediaDevicesSelectionHandler());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000269 AddOptionsPageUIHandler(localized_strings, new MediaGalleriesHandler());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000270#if defined(OS_CHROMEOS)
271 AddOptionsPageUIHandler(localized_strings,
272 new chromeos::options::CrosLanguageOptionsHandler());
273#else
274 AddOptionsPageUIHandler(localized_strings, new LanguageOptionsHandler());
275#endif
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000276 AddOptionsPageUIHandler(localized_strings,
277 new LanguageDictionaryOverlayHandler());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000278 AddOptionsPageUIHandler(localized_strings, new ManageProfileHandler());
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100279 AddOptionsPageUIHandler(localized_strings,
280 new ManagedUserCreateConfirmHandler());
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100281 AddOptionsPageUIHandler(localized_strings, new ManagedUserLearnMoreHandler());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000282 AddOptionsPageUIHandler(localized_strings, new PasswordManagerHandler());
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100283 AddOptionsPageUIHandler(localized_strings, new ResetProfileSettingsHandler());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000284 AddOptionsPageUIHandler(localized_strings, new SearchEngineManagerHandler());
285 AddOptionsPageUIHandler(localized_strings, new ImportDataHandler());
286 AddOptionsPageUIHandler(localized_strings, new StartupPagesHandler());
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100287 AddOptionsPageUIHandler(localized_strings, new SyncSetupHandler(
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000288 g_browser_process->profile_manager()));
289#if defined(OS_CHROMEOS)
290 AddOptionsPageUIHandler(localized_strings,
291 new chromeos::options::AccountsOptionsHandler());
292 AddOptionsPageUIHandler(localized_strings,
293 new chromeos::options::BluetoothOptionsHandler());
294 AddOptionsPageUIHandler(localized_strings,
295 new chromeos::options::DisplayOptionsHandler());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000296 AddOptionsPageUIHandler(localized_strings,
297 new chromeos::options::DisplayOverscanHandler());
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100298 AddOptionsPageUIHandler(localized_strings,
299 new chromeos::options::InternetOptionsHandler());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000300 AddOptionsPageUIHandler(localized_strings,
301 new chromeos::options::LanguageChewingHandler());
302 AddOptionsPageUIHandler(localized_strings,
303 new chromeos::options::KeyboardHandler());
304 AddOptionsPageUIHandler(localized_strings,
305 new chromeos::options::LanguageHangulHandler());
306 AddOptionsPageUIHandler(localized_strings,
307 new chromeos::options::LanguageMozcHandler());
308 AddOptionsPageUIHandler(localized_strings,
309 new chromeos::options::LanguagePinyinHandler());
310
311 chromeos::options::PointerHandler* pointer_handler =
312 new chromeos::options::PointerHandler();
313 AddOptionsPageUIHandler(localized_strings, pointer_handler);
314
315 AddOptionsPageUIHandler(localized_strings,
316 new chromeos::options::ProxyHandler());
317 AddOptionsPageUIHandler(
318 localized_strings,
319 new chromeos::options::ChangePictureOptionsHandler());
320 AddOptionsPageUIHandler(localized_strings,
321 new chromeos::options::StatsOptionsHandler());
322#endif
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000323#if defined(USE_NSS)
324 AddOptionsPageUIHandler(localized_strings, new CertificateManagerHandler());
325#endif
326 AddOptionsPageUIHandler(localized_strings, new HandlerOptionsHandler());
327
328 // |localized_strings| ownership is taken over by this constructor.
329 OptionsUIHTMLSource* html_source =
330 new OptionsUIHTMLSource(localized_strings);
331
332 // Set up the chrome://settings-frame/ source.
333 Profile* profile = Profile::FromWebUI(web_ui);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000334 content::URLDataSource::Add(profile, html_source);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000335
336 // Set up the chrome://theme/ source.
337 ThemeSource* theme = new ThemeSource(profile);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000338 content::URLDataSource::Add(profile, theme);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000339
340#if defined(OS_CHROMEOS)
341 // Set up the chrome://userimage/ source.
342 chromeos::options::UserImageSource* user_image_source =
343 new chromeos::options::UserImageSource();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000344 content::URLDataSource::Add(profile, user_image_source);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000345
346 pointer_device_observer_.reset(
347 new chromeos::system::PointerDeviceObserver());
348 pointer_device_observer_->AddObserver(browser_options_handler);
349 pointer_device_observer_->AddObserver(pointer_handler);
350#endif
351}
352
353OptionsUI::~OptionsUI() {
354 // Uninitialize all registered handlers. Deleted by WebUIImpl.
355 for (size_t i = 0; i < handlers_.size(); ++i)
356 handlers_[i]->Uninitialize();
357}
358
359// static
360void OptionsUI::ProcessAutocompleteSuggestions(
361 const AutocompleteResult& result,
362 base::ListValue* const suggestions) {
363 for (size_t i = 0; i < result.size(); ++i) {
364 const AutocompleteMatch& match = result.match_at(i);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100365 AutocompleteMatchType::Type type = match.type;
366 if (type != AutocompleteMatchType::HISTORY_URL &&
367 type != AutocompleteMatchType::HISTORY_TITLE &&
368 type != AutocompleteMatchType::HISTORY_BODY &&
369 type != AutocompleteMatchType::HISTORY_KEYWORD &&
370 type != AutocompleteMatchType::NAVSUGGEST)
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000371 continue;
372 base::DictionaryValue* entry = new base::DictionaryValue();
373 entry->SetString("title", match.description);
374 entry->SetString("displayURL", match.contents);
375 entry->SetString("url", match.destination_url.spec());
376 suggestions->Append(entry);
377 }
378}
379
380// static
381base::RefCountedMemory* OptionsUI::GetFaviconResourceBytes(
382 ui::ScaleFactor scale_factor) {
383 return ui::ResourceBundle::GetSharedInstance().
384 LoadDataResourceBytesForScale(IDR_SETTINGS_FAVICON, scale_factor);
385}
386
387void OptionsUI::InitializeHandlers() {
388 Profile* profile = Profile::FromWebUI(web_ui());
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000389 DCHECK(!profile->IsOffTheRecord() || profile->IsGuestSession());
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000390
391 // A new web page DOM has been brought up in an existing renderer, causing
392 // this method to be called twice. If that happens, ignore the second call.
393 if (!initialized_handlers_) {
394 for (size_t i = 0; i < handlers_.size(); ++i)
395 handlers_[i]->InitializeHandler();
396 initialized_handlers_ = true;
397
398#if defined(OS_CHROMEOS)
399 pointer_device_observer_->Init();
400#endif
401 }
402
403#if defined(OS_CHROMEOS)
404 pointer_device_observer_->CheckDevices();
405#endif
406
407 // Always initialize the page as when handlers are left over we still need to
408 // do various things like show/hide sections and send data to the Javascript.
409 for (size_t i = 0; i < handlers_.size(); ++i)
410 handlers_[i]->InitializePage();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000411
412 web_ui()->CallJavascriptFunction(
413 "BrowserOptions.notifyInitializationComplete");
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000414}
415
416void OptionsUI::RenderViewCreated(content::RenderViewHost* render_view_host) {
417 content::WebUIController::RenderViewCreated(render_view_host);
418 for (size_t i = 0; i < handlers_.size(); ++i)
419 handlers_[i]->PageLoadStarted();
420}
421
422void OptionsUI::RenderViewReused(content::RenderViewHost* render_view_host) {
423 content::WebUIController::RenderViewReused(render_view_host);
424 for (size_t i = 0; i < handlers_.size(); ++i)
425 handlers_[i]->PageLoadStarted();
426}
427
428void OptionsUI::AddOptionsPageUIHandler(DictionaryValue* localized_strings,
429 OptionsPageUIHandler* handler_raw) {
430 scoped_ptr<OptionsPageUIHandler> handler(handler_raw);
431 DCHECK(handler.get());
432 // Add only if handler's service is enabled.
433 if (handler->IsEnabled()) {
434 // Add handler to the list and also pass the ownership.
435 web_ui()->AddMessageHandler(handler.release());
436 handler_raw->GetLocalizedValues(localized_strings);
437 handlers_.push_back(handler_raw);
438 }
439}
440
441} // namespace options