Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: content/browser/webui/web_ui_data_source_impl.cc

Issue 2544683002: [MD settings] i18n source stream filtering (Closed)
Patch Set: added unit test Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "content/browser/webui/web_ui_data_source_impl.h" 5 #include "content/browser/webui/web_ui_data_source_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <string> 10 #include <string>
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 } 205 }
206 206
207 void WebUIDataSourceImpl::DisableDenyXFrameOptions() { 207 void WebUIDataSourceImpl::DisableDenyXFrameOptions() {
208 deny_xframe_options_ = false; 208 deny_xframe_options_ = false;
209 } 209 }
210 210
211 void WebUIDataSourceImpl::DisableI18nAndUseGzipForAllPaths() { 211 void WebUIDataSourceImpl::DisableI18nAndUseGzipForAllPaths() {
212 use_gzip_for_all_paths_ = true; 212 use_gzip_for_all_paths_ = true;
213 } 213 }
214 214
215 const ui::TemplateReplacements* WebUIDataSourceImpl::GetReplacements() const {
216 return &replacements_;
217 }
218
219 void WebUIDataSourceImpl::EnsureLoadTimeDataDefaultsAdded() {
220 if (!add_load_time_data_defaults_)
221 return;
222
223 add_load_time_data_defaults_ = false;
224 std::string locale = GetContentClient()->browser()->GetApplicationLocale();
225 base::DictionaryValue defaults;
226 webui::SetLoadTimeDataDefaults(locale, &defaults);
227 AddLocalizedStrings(defaults);
228 }
229
215 std::string WebUIDataSourceImpl::GetSource() const { 230 std::string WebUIDataSourceImpl::GetSource() const {
216 return source_name_; 231 return source_name_;
217 } 232 }
218 233
219 std::string WebUIDataSourceImpl::GetMimeType(const std::string& path) const { 234 std::string WebUIDataSourceImpl::GetMimeType(const std::string& path) const {
220 // Remove the query string for to determine the mime type. 235 // Remove the query string for to determine the mime type.
221 std::string file_path = path.substr(0, path.find_first_of('?')); 236 std::string file_path = path.substr(0, path.find_first_of('?'));
222 237
223 if (base::EndsWith(file_path, ".css", base::CompareCase::INSENSITIVE_ASCII)) 238 if (base::EndsWith(file_path, ".css", base::CompareCase::INSENSITIVE_ASCII))
224 return "text/css"; 239 return "text/css";
(...skipping 15 matching lines...) Expand all
240 255
241 void WebUIDataSourceImpl::StartDataRequest( 256 void WebUIDataSourceImpl::StartDataRequest(
242 const std::string& path, 257 const std::string& path,
243 const ResourceRequestInfo::WebContentsGetter& wc_getter, 258 const ResourceRequestInfo::WebContentsGetter& wc_getter,
244 const URLDataSource::GotDataCallback& callback) { 259 const URLDataSource::GotDataCallback& callback) {
245 if (!filter_callback_.is_null() && 260 if (!filter_callback_.is_null() &&
246 filter_callback_.Run(path, callback)) { 261 filter_callback_.Run(path, callback)) {
247 return; 262 return;
248 } 263 }
249 264
250 if (add_load_time_data_defaults_) { 265 EnsureLoadTimeDataDefaultsAdded();
251 std::string locale = GetContentClient()->browser()->GetApplicationLocale();
252 base::DictionaryValue defaults;
253 webui::SetLoadTimeDataDefaults(locale, &defaults);
254 AddLocalizedStrings(defaults);
255 add_load_time_data_defaults_ = false;
256 }
257 266
258 if (!json_path_.empty() && path == json_path_) { 267 if (!json_path_.empty() && path == json_path_) {
259 SendLocalizedStringsAsJSON(callback); 268 SendLocalizedStringsAsJSON(callback);
260 return; 269 return;
261 } 270 }
262 271
263 int resource_id = default_resource_; 272 int resource_id = default_resource_;
264 std::map<std::string, int>::iterator result; 273 std::map<std::string, int>::iterator result;
265 // Remove the query string for named resource lookups. 274 // Remove the query string for named resource lookups.
266 std::string file_path = path.substr(0, path.find_first_of('?')); 275 std::string file_path = path.substr(0, path.find_first_of('?'));
267 result = path_to_idr_map_.find(file_path); 276 result = path_to_idr_map_.find(file_path);
268 if (result != path_to_idr_map_.end()) 277 if (result != path_to_idr_map_.end())
269 resource_id = result->second; 278 resource_id = result->second;
270 DCHECK_NE(resource_id, -1); 279 DCHECK_NE(resource_id, -1);
271 scoped_refptr<base::RefCountedMemory> response( 280 scoped_refptr<base::RefCountedMemory> response(
272 GetContentClient()->GetDataResourceBytes(resource_id)); 281 GetContentClient()->GetDataResourceBytes(resource_id));
273
274 // TODO(dschuyler): improve filtering of which resource to run template
275 // expansion upon. TODO(dbeam): make a streaming filter that works on gzipped
276 // content.
277 if (response.get() && GetMimeType(path) == "text/html" &&
278 !source()->IsGzipped(path)) {
279 std::string replaced = ui::ReplaceTemplateExpressions(
280 base::StringPiece(response->front_as<char>(), response->size()),
281 replacements_);
282 response = base::RefCountedString::TakeString(&replaced);
283 }
284
285 callback.Run(response.get()); 282 callback.Run(response.get());
286 } 283 }
287 284
288 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( 285 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON(
289 const URLDataSource::GotDataCallback& callback) { 286 const URLDataSource::GotDataCallback& callback) {
290 std::string template_data; 287 std::string template_data;
291 webui::AppendJsonJS(&localized_strings_, &template_data); 288 webui::AppendJsonJS(&localized_strings_, &template_data);
292 callback.Run(base::RefCountedString::TakeString(&template_data)); 289 callback.Run(base::RefCountedString::TakeString(&template_data));
293 } 290 }
294 291
295 } // namespace content 292 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698