| 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 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 return URLDataSource::GetContentSecurityPolicyObjectSrc(); | 67 return URLDataSource::GetContentSecurityPolicyObjectSrc(); |
| 68 } | 68 } |
| 69 std::string GetContentSecurityPolicyChildSrc() const override { | 69 std::string GetContentSecurityPolicyChildSrc() const override { |
| 70 if (parent_->frame_src_set_) | 70 if (parent_->frame_src_set_) |
| 71 return parent_->frame_src_; | 71 return parent_->frame_src_; |
| 72 return URLDataSource::GetContentSecurityPolicyChildSrc(); | 72 return URLDataSource::GetContentSecurityPolicyChildSrc(); |
| 73 } | 73 } |
| 74 bool ShouldDenyXFrameOptions() const override { | 74 bool ShouldDenyXFrameOptions() const override { |
| 75 return parent_->deny_xframe_options_; | 75 return parent_->deny_xframe_options_; |
| 76 } | 76 } |
| 77 bool IsGzipped(const std::string& path) const override { |
| 78 if (!parent_->json_path_.empty() && path == parent_->json_path_) |
| 79 return false; |
| 80 return parent_->use_gzip_for_all_paths_; |
| 81 } |
| 77 | 82 |
| 78 private: | 83 private: |
| 79 WebUIDataSourceImpl* parent_; | 84 WebUIDataSourceImpl* parent_; |
| 80 }; | 85 }; |
| 81 | 86 |
| 82 WebUIDataSourceImpl::WebUIDataSourceImpl(const std::string& source_name) | 87 WebUIDataSourceImpl::WebUIDataSourceImpl(const std::string& source_name) |
| 83 : URLDataSourceImpl(source_name, new InternalDataSource(this)), | 88 : URLDataSourceImpl(source_name, new InternalDataSource(this)), |
| 84 source_name_(source_name), | 89 source_name_(source_name), |
| 85 default_resource_(-1), | 90 default_resource_(-1), |
| 86 add_csp_(true), | 91 add_csp_(true), |
| 87 object_src_set_(false), | 92 object_src_set_(false), |
| 88 frame_src_set_(false), | 93 frame_src_set_(false), |
| 89 deny_xframe_options_(true), | 94 deny_xframe_options_(true), |
| 90 add_load_time_data_defaults_(true), | 95 add_load_time_data_defaults_(true), |
| 91 replace_existing_source_(true) {} | 96 replace_existing_source_(true), |
| 97 use_gzip_for_all_paths_(false) {} |
| 92 | 98 |
| 93 WebUIDataSourceImpl::~WebUIDataSourceImpl() { | 99 WebUIDataSourceImpl::~WebUIDataSourceImpl() { |
| 94 } | 100 } |
| 95 | 101 |
| 96 void WebUIDataSourceImpl::AddString(const std::string& name, | 102 void WebUIDataSourceImpl::AddString(const std::string& name, |
| 97 const base::string16& value) { | 103 const base::string16& value) { |
| 98 // TODO(dschuyler): Share only one copy of these strings. | 104 // TODO(dschuyler): Share only one copy of these strings. |
| 99 localized_strings_.SetString(name, value); | 105 localized_strings_.SetString(name, value); |
| 100 replacements_[name] = base::UTF16ToUTF8(value); | 106 replacements_[name] = base::UTF16ToUTF8(value); |
| 101 } | 107 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 void WebUIDataSourceImpl::OverrideContentSecurityPolicyChildSrc( | 178 void WebUIDataSourceImpl::OverrideContentSecurityPolicyChildSrc( |
| 173 const std::string& data) { | 179 const std::string& data) { |
| 174 frame_src_set_ = true; | 180 frame_src_set_ = true; |
| 175 frame_src_ = data; | 181 frame_src_ = data; |
| 176 } | 182 } |
| 177 | 183 |
| 178 void WebUIDataSourceImpl::DisableDenyXFrameOptions() { | 184 void WebUIDataSourceImpl::DisableDenyXFrameOptions() { |
| 179 deny_xframe_options_ = false; | 185 deny_xframe_options_ = false; |
| 180 } | 186 } |
| 181 | 187 |
| 188 void WebUIDataSourceImpl::DisableI18nAndUseGzipForAllPaths() { |
| 189 use_gzip_for_all_paths_ = true; |
| 190 } |
| 191 |
| 182 std::string WebUIDataSourceImpl::GetSource() const { | 192 std::string WebUIDataSourceImpl::GetSource() const { |
| 183 return source_name_; | 193 return source_name_; |
| 184 } | 194 } |
| 185 | 195 |
| 186 std::string WebUIDataSourceImpl::GetMimeType(const std::string& path) const { | 196 std::string WebUIDataSourceImpl::GetMimeType(const std::string& path) const { |
| 187 // Remove the query string for to determine the mime type. | 197 // Remove the query string for to determine the mime type. |
| 188 std::string file_path = path.substr(0, path.find_first_of('?')); | 198 std::string file_path = path.substr(0, path.find_first_of('?')); |
| 189 | 199 |
| 190 if (base::EndsWith(file_path, ".css", base::CompareCase::INSENSITIVE_ASCII)) | 200 if (base::EndsWith(file_path, ".css", base::CompareCase::INSENSITIVE_ASCII)) |
| 191 return "text/css"; | 201 return "text/css"; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 // Remove the query string for named resource lookups. | 243 // Remove the query string for named resource lookups. |
| 234 std::string file_path = path.substr(0, path.find_first_of('?')); | 244 std::string file_path = path.substr(0, path.find_first_of('?')); |
| 235 result = path_to_idr_map_.find(file_path); | 245 result = path_to_idr_map_.find(file_path); |
| 236 if (result != path_to_idr_map_.end()) | 246 if (result != path_to_idr_map_.end()) |
| 237 resource_id = result->second; | 247 resource_id = result->second; |
| 238 DCHECK_NE(resource_id, -1); | 248 DCHECK_NE(resource_id, -1); |
| 239 scoped_refptr<base::RefCountedMemory> response( | 249 scoped_refptr<base::RefCountedMemory> response( |
| 240 GetContentClient()->GetDataResourceBytes(resource_id)); | 250 GetContentClient()->GetDataResourceBytes(resource_id)); |
| 241 | 251 |
| 242 // TODO(dschuyler): improve filtering of which resource to run template | 252 // TODO(dschuyler): improve filtering of which resource to run template |
| 243 // expansion upon. | 253 // expansion upon. TODO(dbeam): make a streaming filter that works on gzipped |
| 244 if (GetMimeType(path) == "text/html") { | 254 // content. |
| 255 if (GetMimeType(path) == "text/html" && !source()->IsGzipped(path)) { |
| 245 std::string replaced = ui::ReplaceTemplateExpressions( | 256 std::string replaced = ui::ReplaceTemplateExpressions( |
| 246 base::StringPiece(response->front_as<char>(), response->size()), | 257 base::StringPiece(response->front_as<char>(), response->size()), |
| 247 replacements_); | 258 replacements_); |
| 248 response = base::RefCountedString::TakeString(&replaced); | 259 response = base::RefCountedString::TakeString(&replaced); |
| 249 } | 260 } |
| 250 | 261 |
| 251 callback.Run(response.get()); | 262 callback.Run(response.get()); |
| 252 } | 263 } |
| 253 | 264 |
| 254 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( | 265 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( |
| 255 const URLDataSource::GotDataCallback& callback) { | 266 const URLDataSource::GotDataCallback& callback) { |
| 256 std::string template_data; | 267 std::string template_data; |
| 257 webui::AppendJsonJS(&localized_strings_, &template_data); | 268 webui::AppendJsonJS(&localized_strings_, &template_data); |
| 258 callback.Run(base::RefCountedString::TakeString(&template_data)); | 269 callback.Run(base::RefCountedString::TakeString(&template_data)); |
| 259 } | 270 } |
| 260 | 271 |
| 261 } // namespace content | 272 } // namespace content |
| OLD | NEW |