OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/dom_ui/options_ui.h" |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" |
| 9 #include "base/callback.h" |
| 10 #include "base/i18n/time_formatting.h" |
| 11 #include "base/message_loop.h" |
| 12 #include "base/singleton.h" |
| 13 #include "base/string_piece.h" |
| 14 #include "base/string_util.h" |
| 15 #include "base/thread.h" |
| 16 #include "base/time.h" |
| 17 #include "base/values.h" |
| 18 #include "chrome/browser/chrome_thread.h" |
| 19 #include "chrome/browser/dom_ui/core_options_handler.h" |
| 20 #include "chrome/browser/metrics/user_metrics.h" |
| 21 #include "chrome/browser/pref_service.h" |
| 22 #include "chrome/browser/profile.h" |
| 23 #include "chrome/browser/tab_contents/tab_contents.h" |
| 24 #include "chrome/browser/tab_contents/tab_contents_delegate.h" |
| 25 #include "chrome/common/jstemplate_builder.h" |
| 26 #include "chrome/common/notification_service.h" |
| 27 #include "chrome/common/notification_type.h" |
| 28 #include "chrome/common/time_format.h" |
| 29 #include "chrome/common/url_constants.h" |
| 30 #include "net/base/escape.h" |
| 31 |
| 32 #include "grit/browser_resources.h" |
| 33 #include "grit/chromium_strings.h" |
| 34 #include "grit/generated_resources.h" |
| 35 #include "grit/locale_settings.h" |
| 36 #include "grit/theme_resources.h" |
| 37 |
| 38 #if defined(OS_CHROMEOS) |
| 39 #include "chrome/browser/chromeos/dom_ui/system_options_handler.h" |
| 40 #endif |
| 41 |
| 42 //////////////////////////////////////////////////////////////////////////////// |
| 43 // |
| 44 // OptionsUIHTMLSource |
| 45 // |
| 46 //////////////////////////////////////////////////////////////////////////////// |
| 47 |
| 48 OptionsUIHTMLSource::OptionsUIHTMLSource(DictionaryValue* localized_strings) |
| 49 : DataSource(chrome::kChromeUIOptionsHost, MessageLoop::current()) { |
| 50 DCHECK(localized_strings); |
| 51 localized_strings_.reset(localized_strings); |
| 52 } |
| 53 |
| 54 void OptionsUIHTMLSource::StartDataRequest(const std::string& path, |
| 55 bool is_off_the_record, |
| 56 int request_id) { |
| 57 SetFontAndTextDirection(localized_strings_.get()); |
| 58 |
| 59 static const base::StringPiece options_html( |
| 60 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 61 IDR_OPTIONS_HTML)); |
| 62 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( |
| 63 options_html, localized_strings_.get()); |
| 64 |
| 65 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 66 html_bytes->data.resize(full_html.size()); |
| 67 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
| 68 |
| 69 SendResponse(request_id, html_bytes); |
| 70 } |
| 71 |
| 72 //////////////////////////////////////////////////////////////////////////////// |
| 73 // |
| 74 // OptionsUIHandler |
| 75 // |
| 76 //////////////////////////////////////////////////////////////////////////////// |
| 77 |
| 78 OptionsPageUIHandler::OptionsPageUIHandler() { |
| 79 } |
| 80 |
| 81 OptionsPageUIHandler::~OptionsPageUIHandler() { |
| 82 } |
| 83 |
| 84 void OptionsPageUIHandler::UserMetricsRecordAction( |
| 85 const UserMetricsAction& action, PrefService* prefs) { |
| 86 UserMetrics::RecordAction(action, dom_ui_->GetProfile()); |
| 87 if (prefs) |
| 88 prefs->ScheduleSavePersistentPrefs(); |
| 89 } |
| 90 |
| 91 //////////////////////////////////////////////////////////////////////////////// |
| 92 // |
| 93 // OptionsUIContents |
| 94 // |
| 95 //////////////////////////////////////////////////////////////////////////////// |
| 96 |
| 97 OptionsUI::OptionsUI(TabContents* contents) : DOMUI(contents) { |
| 98 DictionaryValue* localized_strings = new DictionaryValue(); |
| 99 |
| 100 // TODO(zelidrag): Add all other page handlers here as we implement them. |
| 101 AddOptionsPageUIHandler(localized_strings, new CoreOptionsHandler()); |
| 102 #if defined(OS_CHROMEOS) |
| 103 AddOptionsPageUIHandler(localized_strings, new SystemOptionsHandler()); |
| 104 #endif |
| 105 |
| 106 // |localized_strings| ownership is taken over by this constructor. |
| 107 OptionsUIHTMLSource* html_source = |
| 108 new OptionsUIHTMLSource(localized_strings); |
| 109 |
| 110 // Set up the chrome://options/ source. |
| 111 ChromeThread::PostTask( |
| 112 ChromeThread::IO, FROM_HERE, |
| 113 NewRunnableMethod( |
| 114 Singleton<ChromeURLDataManager>::get(), |
| 115 &ChromeURLDataManager::AddDataSource, |
| 116 make_scoped_refptr(html_source))); |
| 117 } |
| 118 |
| 119 void OptionsUI::AddOptionsPageUIHandler(DictionaryValue* localized_strings, |
| 120 OptionsPageUIHandler* handler) { |
| 121 DCHECK(handler); |
| 122 handler->GetLocalizedValues(localized_strings); |
| 123 AddMessageHandler(handler->Attach(this)); |
| 124 } |
OLD | NEW |