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