| 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/labs_ui.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "app/l10n_util.h" | |
| 10 #include "app/resource_bundle.h" | |
| 11 #include "base/singleton.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/browser_list.h" | |
| 14 #include "chrome/browser/browser_process.h" | |
| 15 #include "chrome/browser/chrome_thread.h" | |
| 16 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" | |
| 17 #include "chrome/browser/labs.h" | |
| 18 #include "chrome/browser/prefs/pref_service.h" | |
| 19 #include "chrome/common/jstemplate_builder.h" | |
| 20 #include "chrome/common/pref_names.h" | |
| 21 #include "chrome/common/url_constants.h" | |
| 22 #include "grit/browser_resources.h" | |
| 23 #include "grit/chromium_strings.h" | |
| 24 #include "grit/generated_resources.h" | |
| 25 #include "grit/theme_resources.h" | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 /////////////////////////////////////////////////////////////////////////////// | |
| 30 // | |
| 31 // LabsUIHTMLSource | |
| 32 // | |
| 33 /////////////////////////////////////////////////////////////////////////////// | |
| 34 | |
| 35 class LabsUIHTMLSource : public ChromeURLDataManager::DataSource { | |
| 36 public: | |
| 37 LabsUIHTMLSource() | |
| 38 : DataSource(chrome::kChromeUILabsHost, MessageLoop::current()) {} | |
| 39 | |
| 40 // Called when the network layer has requested a resource underneath | |
| 41 // the path we registered. | |
| 42 virtual void StartDataRequest(const std::string& path, | |
| 43 bool is_off_the_record, | |
| 44 int request_id); | |
| 45 virtual std::string GetMimeType(const std::string&) const { | |
| 46 return "text/html"; | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 ~LabsUIHTMLSource() {} | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(LabsUIHTMLSource); | |
| 53 }; | |
| 54 | |
| 55 void LabsUIHTMLSource::StartDataRequest(const std::string& path, | |
| 56 bool is_off_the_record, | |
| 57 int request_id) { | |
| 58 // Strings used in the JsTemplate file. | |
| 59 DictionaryValue localized_strings; | |
| 60 localized_strings.SetString("labsTitle", | |
| 61 l10n_util::GetStringUTF16(IDS_LABS_TITLE)); | |
| 62 localized_strings.SetString("labsLongTitle", | |
| 63 l10n_util::GetStringUTF16(IDS_LABS_LONG_TITLE)); | |
| 64 localized_strings.SetString("labsTableTitle", | |
| 65 l10n_util::GetStringUTF16(IDS_LABS_TABLE_TITLE)); | |
| 66 localized_strings.SetString("labsNoExperimentsAvailable", | |
| 67 l10n_util::GetStringUTF16(IDS_LABS_NO_EXPERIMENTS_AVAILABLE)); | |
| 68 localized_strings.SetString("labsBlurb", l10n_util::GetStringFUTF16( | |
| 69 IDS_LABS_BLURB, | |
| 70 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); | |
| 71 localized_strings.SetString("labsRestartNotice", l10n_util::GetStringFUTF16( | |
| 72 IDS_LABS_RESTART_NOTICE, | |
| 73 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME))); | |
| 74 localized_strings.SetString("labsRestartButton", | |
| 75 l10n_util::GetStringUTF16(IDS_LABS_RESTART_BUTTON)); | |
| 76 localized_strings.SetString("disable", | |
| 77 l10n_util::GetStringUTF16(IDS_LABS_DISABLE)); | |
| 78 localized_strings.SetString("enable", | |
| 79 l10n_util::GetStringUTF16(IDS_LABS_ENABLE)); | |
| 80 | |
| 81 ChromeURLDataManager::DataSource::SetFontAndTextDirection(&localized_strings); | |
| 82 | |
| 83 static const base::StringPiece labs_html( | |
| 84 ResourceBundle::GetSharedInstance().GetRawDataResource(IDR_LABS_HTML)); | |
| 85 std::string full_html(labs_html.data(), labs_html.size()); | |
| 86 jstemplate_builder::AppendJsonHtml(&localized_strings, &full_html); | |
| 87 jstemplate_builder::AppendI18nTemplateSourceHtml(&full_html); | |
| 88 jstemplate_builder::AppendI18nTemplateProcessHtml(&full_html); | |
| 89 jstemplate_builder::AppendJsTemplateSourceHtml(&full_html); | |
| 90 | |
| 91 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | |
| 92 html_bytes->data.resize(full_html.size()); | |
| 93 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | |
| 94 | |
| 95 SendResponse(request_id, html_bytes); | |
| 96 } | |
| 97 | |
| 98 //////////////////////////////////////////////////////////////////////////////// | |
| 99 // | |
| 100 // LabsDOMHandler | |
| 101 // | |
| 102 //////////////////////////////////////////////////////////////////////////////// | |
| 103 | |
| 104 // The handler for Javascript messages for the chrome://labs/ page. | |
| 105 class LabsDOMHandler : public DOMMessageHandler { | |
| 106 public: | |
| 107 LabsDOMHandler() {} | |
| 108 virtual ~LabsDOMHandler() {} | |
| 109 | |
| 110 // DOMMessageHandler implementation. | |
| 111 virtual void RegisterMessages(); | |
| 112 | |
| 113 // Callback for the "requestLabsExperiments" message. | |
| 114 void HandleRequestLabsExperiments(const ListValue* args); | |
| 115 | |
| 116 // Callback for the "enableLabsExperiment" message. | |
| 117 void HandleEnableLabsExperimentMessage(const ListValue* args); | |
| 118 | |
| 119 // Callback for the "restartBrowser" message. Restores all tabs on restart. | |
| 120 void HandleRestartBrowser(const ListValue* args); | |
| 121 | |
| 122 private: | |
| 123 DISALLOW_COPY_AND_ASSIGN(LabsDOMHandler); | |
| 124 }; | |
| 125 | |
| 126 void LabsDOMHandler::RegisterMessages() { | |
| 127 dom_ui_->RegisterMessageCallback("requestLabsExperiments", | |
| 128 NewCallback(this, &LabsDOMHandler::HandleRequestLabsExperiments)); | |
| 129 dom_ui_->RegisterMessageCallback("enableLabsExperiment", | |
| 130 NewCallback(this, &LabsDOMHandler::HandleEnableLabsExperimentMessage)); | |
| 131 dom_ui_->RegisterMessageCallback("restartBrowser", | |
| 132 NewCallback(this, &LabsDOMHandler::HandleRestartBrowser)); | |
| 133 } | |
| 134 | |
| 135 void LabsDOMHandler::HandleRequestLabsExperiments(const ListValue* args) { | |
| 136 DictionaryValue results; | |
| 137 results.Set("labsExperiments", | |
| 138 about_labs::GetLabsExperimentsData(dom_ui_->GetProfile())); | |
| 139 results.SetBoolean("needsRestart", | |
| 140 about_labs::IsRestartNeededToCommitChanges()); | |
| 141 dom_ui_->CallJavascriptFunction(L"returnLabsExperiments", results); | |
| 142 } | |
| 143 | |
| 144 void LabsDOMHandler::HandleEnableLabsExperimentMessage(const ListValue* args) { | |
| 145 DCHECK_EQ(2u, args->GetSize()); | |
| 146 if (args->GetSize() != 2) | |
| 147 return; | |
| 148 | |
| 149 std::string experiment_internal_name; | |
| 150 std::string enable_str; | |
| 151 if (!args->GetString(0, &experiment_internal_name) || | |
| 152 !args->GetString(1, &enable_str)) | |
| 153 return; | |
| 154 | |
| 155 about_labs::SetExperimentEnabled( | |
| 156 dom_ui_->GetProfile(), experiment_internal_name, enable_str == "true"); | |
| 157 } | |
| 158 | |
| 159 void LabsDOMHandler::HandleRestartBrowser(const ListValue* args) { | |
| 160 // Set the flag to restore state after the restart. | |
| 161 PrefService* pref_service = g_browser_process->local_state(); | |
| 162 pref_service->SetBoolean(prefs::kRestartLastSessionOnShutdown, true); | |
| 163 BrowserList::CloseAllBrowsersAndExit(); | |
| 164 } | |
| 165 | |
| 166 } // namespace | |
| 167 | |
| 168 /////////////////////////////////////////////////////////////////////////////// | |
| 169 // | |
| 170 // LabsUI | |
| 171 // | |
| 172 /////////////////////////////////////////////////////////////////////////////// | |
| 173 | |
| 174 LabsUI::LabsUI(TabContents* contents) : DOMUI(contents) { | |
| 175 AddMessageHandler((new LabsDOMHandler())->Attach(this)); | |
| 176 | |
| 177 LabsUIHTMLSource* html_source = new LabsUIHTMLSource(); | |
| 178 | |
| 179 // Set up the chrome://labs/ source. | |
| 180 ChromeThread::PostTask( | |
| 181 ChromeThread::IO, FROM_HERE, | |
| 182 NewRunnableMethod(Singleton<ChromeURLDataManager>::get(), | |
| 183 &ChromeURLDataManager::AddDataSource, | |
| 184 make_scoped_refptr(html_source))); | |
| 185 } | |
| 186 | |
| 187 // static | |
| 188 RefCountedMemory* LabsUI::GetFaviconResourceBytes() { | |
| 189 return ResourceBundle::GetSharedInstance(). | |
| 190 LoadDataResourceBytes(IDR_LABS); | |
| 191 } | |
| 192 | |
| 193 // static | |
| 194 void LabsUI::RegisterUserPrefs(PrefService* prefs) { | |
| 195 prefs->RegisterListPref(prefs::kEnabledLabsExperiments); | |
| 196 } | |
| OLD | NEW |