OLD | NEW |
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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 } | 217 } |
218 | 218 |
219 void WebUIDataSourceImpl::DisableDenyXFrameOptions() { | 219 void WebUIDataSourceImpl::DisableDenyXFrameOptions() { |
220 deny_xframe_options_ = false; | 220 deny_xframe_options_ = false; |
221 } | 221 } |
222 | 222 |
223 void WebUIDataSourceImpl::DisableI18nAndUseGzipForAllPaths() { | 223 void WebUIDataSourceImpl::DisableI18nAndUseGzipForAllPaths() { |
224 use_gzip_for_all_paths_ = true; | 224 use_gzip_for_all_paths_ = true; |
225 } | 225 } |
226 | 226 |
| 227 const ui::TemplateReplacements* WebUIDataSourceImpl::GetReplacements() const { |
| 228 return &replacements_; |
| 229 } |
| 230 |
| 231 void WebUIDataSourceImpl::EnsureLoadTimeDataDefaultsAdded() { |
| 232 if (!add_load_time_data_defaults_) |
| 233 return; |
| 234 |
| 235 add_load_time_data_defaults_ = false; |
| 236 std::string locale = GetContentClient()->browser()->GetApplicationLocale(); |
| 237 base::DictionaryValue defaults; |
| 238 webui::SetLoadTimeDataDefaults(locale, &defaults); |
| 239 AddLocalizedStrings(defaults); |
| 240 } |
| 241 |
227 std::string WebUIDataSourceImpl::GetSource() const { | 242 std::string WebUIDataSourceImpl::GetSource() const { |
228 return source_name_; | 243 return source_name_; |
229 } | 244 } |
230 | 245 |
231 std::string WebUIDataSourceImpl::GetMimeType(const std::string& path) const { | 246 std::string WebUIDataSourceImpl::GetMimeType(const std::string& path) const { |
232 // Remove the query string for to determine the mime type. | 247 // Remove the query string for to determine the mime type. |
233 std::string file_path = path.substr(0, path.find_first_of('?')); | 248 std::string file_path = path.substr(0, path.find_first_of('?')); |
234 | 249 |
235 if (base::EndsWith(file_path, ".css", base::CompareCase::INSENSITIVE_ASCII)) | 250 if (base::EndsWith(file_path, ".css", base::CompareCase::INSENSITIVE_ASCII)) |
236 return "text/css"; | 251 return "text/css"; |
(...skipping 15 matching lines...) Expand all Loading... |
252 | 267 |
253 void WebUIDataSourceImpl::StartDataRequest( | 268 void WebUIDataSourceImpl::StartDataRequest( |
254 const std::string& path, | 269 const std::string& path, |
255 const ResourceRequestInfo::WebContentsGetter& wc_getter, | 270 const ResourceRequestInfo::WebContentsGetter& wc_getter, |
256 const URLDataSource::GotDataCallback& callback) { | 271 const URLDataSource::GotDataCallback& callback) { |
257 if (!filter_callback_.is_null() && | 272 if (!filter_callback_.is_null() && |
258 filter_callback_.Run(path, callback)) { | 273 filter_callback_.Run(path, callback)) { |
259 return; | 274 return; |
260 } | 275 } |
261 | 276 |
262 if (add_load_time_data_defaults_) { | 277 EnsureLoadTimeDataDefaultsAdded(); |
263 std::string locale = GetContentClient()->browser()->GetApplicationLocale(); | |
264 base::DictionaryValue defaults; | |
265 webui::SetLoadTimeDataDefaults(locale, &defaults); | |
266 AddLocalizedStrings(defaults); | |
267 add_load_time_data_defaults_ = false; | |
268 } | |
269 | 278 |
270 if (!json_path_.empty() && path == json_path_) { | 279 if (!json_path_.empty() && path == json_path_) { |
271 SendLocalizedStringsAsJSON(callback); | 280 SendLocalizedStringsAsJSON(callback); |
272 return; | 281 return; |
273 } | 282 } |
274 | 283 |
275 int resource_id = default_resource_; | 284 int resource_id = default_resource_; |
276 std::map<std::string, int>::iterator result; | 285 std::map<std::string, int>::iterator result; |
277 // Remove the query string for named resource lookups. | 286 // Remove the query string for named resource lookups. |
278 std::string file_path = path.substr(0, path.find_first_of('?')); | 287 std::string file_path = path.substr(0, path.find_first_of('?')); |
279 result = path_to_idr_map_.find(file_path); | 288 result = path_to_idr_map_.find(file_path); |
280 if (result != path_to_idr_map_.end()) | 289 if (result != path_to_idr_map_.end()) |
281 resource_id = result->second; | 290 resource_id = result->second; |
282 DCHECK_NE(resource_id, -1); | 291 DCHECK_NE(resource_id, -1); |
283 scoped_refptr<base::RefCountedMemory> response( | 292 scoped_refptr<base::RefCountedMemory> response( |
284 GetContentClient()->GetDataResourceBytes(resource_id)); | 293 GetContentClient()->GetDataResourceBytes(resource_id)); |
285 | |
286 // TODO(dschuyler): improve filtering of which resource to run template | |
287 // expansion upon. TODO(dbeam): make a streaming filter that works on gzipped | |
288 // content. | |
289 if (response.get() && GetMimeType(path) == "text/html" && | |
290 !source()->IsGzipped(path)) { | |
291 std::string replaced = ui::ReplaceTemplateExpressions( | |
292 base::StringPiece(response->front_as<char>(), response->size()), | |
293 replacements_); | |
294 response = base::RefCountedString::TakeString(&replaced); | |
295 } | |
296 | |
297 callback.Run(response.get()); | 294 callback.Run(response.get()); |
298 } | 295 } |
299 | 296 |
300 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( | 297 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( |
301 const URLDataSource::GotDataCallback& callback) { | 298 const URLDataSource::GotDataCallback& callback) { |
302 std::string template_data; | 299 std::string template_data; |
303 webui::AppendJsonJS(&localized_strings_, &template_data); | 300 webui::AppendJsonJS(&localized_strings_, &template_data); |
304 callback.Run(base::RefCountedString::TakeString(&template_data)); | 301 callback.Run(base::RefCountedString::TakeString(&template_data)); |
305 } | 302 } |
306 | 303 |
307 } // namespace content | 304 } // namespace content |
OLD | NEW |