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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 void WebUIDataSourceImpl::FinalizeReplacements() { | |
Dan Beam
2016/12/03 02:25:28
nit: EnsureLoadTimeDataDefaultsAdded() ?
dschuyler
2016/12/03 02:44:57
Done.
| |
216 // After this function is called, make no further changes to |replacements_|. | |
217 if (!add_load_time_data_defaults_) | |
218 return; | |
219 | |
220 add_load_time_data_defaults_ = false; | |
221 std::string locale = GetContentClient()->browser()->GetApplicationLocale(); | |
222 base::DictionaryValue defaults; | |
223 webui::SetLoadTimeDataDefaults(locale, &defaults); | |
224 AddLocalizedStrings(defaults); | |
225 } | |
226 | |
227 const ui::TemplateReplacements* WebUIDataSourceImpl::GetReplacements() const { | |
228 return &replacements_; | |
229 } | |
230 | |
215 std::string WebUIDataSourceImpl::GetSource() const { | 231 std::string WebUIDataSourceImpl::GetSource() const { |
216 return source_name_; | 232 return source_name_; |
217 } | 233 } |
218 | 234 |
219 std::string WebUIDataSourceImpl::GetMimeType(const std::string& path) const { | 235 std::string WebUIDataSourceImpl::GetMimeType(const std::string& path) const { |
220 // Remove the query string for to determine the mime type. | 236 // Remove the query string for to determine the mime type. |
221 std::string file_path = path.substr(0, path.find_first_of('?')); | 237 std::string file_path = path.substr(0, path.find_first_of('?')); |
222 | 238 |
223 if (base::EndsWith(file_path, ".css", base::CompareCase::INSENSITIVE_ASCII)) | 239 if (base::EndsWith(file_path, ".css", base::CompareCase::INSENSITIVE_ASCII)) |
224 return "text/css"; | 240 return "text/css"; |
(...skipping 15 matching lines...) Expand all Loading... | |
240 | 256 |
241 void WebUIDataSourceImpl::StartDataRequest( | 257 void WebUIDataSourceImpl::StartDataRequest( |
242 const std::string& path, | 258 const std::string& path, |
243 const ResourceRequestInfo::WebContentsGetter& wc_getter, | 259 const ResourceRequestInfo::WebContentsGetter& wc_getter, |
244 const URLDataSource::GotDataCallback& callback) { | 260 const URLDataSource::GotDataCallback& callback) { |
245 if (!filter_callback_.is_null() && | 261 if (!filter_callback_.is_null() && |
246 filter_callback_.Run(path, callback)) { | 262 filter_callback_.Run(path, callback)) { |
247 return; | 263 return; |
248 } | 264 } |
249 | 265 |
250 if (add_load_time_data_defaults_) { | 266 // The replacements are initiallized in the main thread and then used in the |
251 std::string locale = GetContentClient()->browser()->GetApplicationLocale(); | 267 // IO thread. The map is safe to read from multiple threads as long as no |
252 base::DictionaryValue defaults; | 268 // futher changes are made to it. |
253 webui::SetLoadTimeDataDefaults(locale, &defaults); | 269 FinalizeReplacements(); |
254 AddLocalizedStrings(defaults); | |
255 add_load_time_data_defaults_ = false; | |
256 } | |
257 | 270 |
258 if (!json_path_.empty() && path == json_path_) { | 271 if (!json_path_.empty() && path == json_path_) { |
259 SendLocalizedStringsAsJSON(callback); | 272 SendLocalizedStringsAsJSON(callback); |
260 return; | 273 return; |
261 } | 274 } |
262 | 275 |
263 int resource_id = default_resource_; | 276 int resource_id = default_resource_; |
264 std::map<std::string, int>::iterator result; | 277 std::map<std::string, int>::iterator result; |
265 // Remove the query string for named resource lookups. | 278 // Remove the query string for named resource lookups. |
266 std::string file_path = path.substr(0, path.find_first_of('?')); | 279 std::string file_path = path.substr(0, path.find_first_of('?')); |
267 result = path_to_idr_map_.find(file_path); | 280 result = path_to_idr_map_.find(file_path); |
268 if (result != path_to_idr_map_.end()) | 281 if (result != path_to_idr_map_.end()) |
269 resource_id = result->second; | 282 resource_id = result->second; |
270 DCHECK_NE(resource_id, -1); | 283 DCHECK_NE(resource_id, -1); |
271 scoped_refptr<base::RefCountedMemory> response( | 284 scoped_refptr<base::RefCountedMemory> response( |
272 GetContentClient()->GetDataResourceBytes(resource_id)); | 285 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()); | 286 callback.Run(response.get()); |
286 } | 287 } |
287 | 288 |
288 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( | 289 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( |
289 const URLDataSource::GotDataCallback& callback) { | 290 const URLDataSource::GotDataCallback& callback) { |
290 std::string template_data; | 291 std::string template_data; |
291 webui::AppendJsonJS(&localized_strings_, &template_data); | 292 webui::AppendJsonJS(&localized_strings_, &template_data); |
292 callback.Run(base::RefCountedString::TakeString(&template_data)); | 293 callback.Run(base::RefCountedString::TakeString(&template_data)); |
293 } | 294 } |
294 | 295 |
295 } // namespace content | 296 } // namespace content |
OLD | NEW |