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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 } | 81 } |
82 std::string GetContentSecurityPolicyChildSrc() const override { | 82 std::string GetContentSecurityPolicyChildSrc() const override { |
83 if (parent_->frame_src_set_) | 83 if (parent_->frame_src_set_) |
84 return parent_->frame_src_; | 84 return parent_->frame_src_; |
85 return URLDataSource::GetContentSecurityPolicyChildSrc(); | 85 return URLDataSource::GetContentSecurityPolicyChildSrc(); |
86 } | 86 } |
87 bool ShouldDenyXFrameOptions() const override { | 87 bool ShouldDenyXFrameOptions() const override { |
88 return parent_->deny_xframe_options_; | 88 return parent_->deny_xframe_options_; |
89 } | 89 } |
90 bool IsGzipped(const std::string& path) const override { | 90 bool IsGzipped(const std::string& path) const override { |
91 return parent_->use_gzip_for_all_paths_ && | 91 return parent_->use_gzip_ && |
92 parent_->excluded_paths_.find(path) == parent_->excluded_paths_.end(); | 92 parent_->excluded_paths_.find(path) == parent_->excluded_paths_.end(); |
93 } | 93 } |
94 | 94 |
95 private: | 95 private: |
96 WebUIDataSourceImpl* parent_; | 96 WebUIDataSourceImpl* parent_; |
97 }; | 97 }; |
98 | 98 |
99 WebUIDataSourceImpl::WebUIDataSourceImpl(const std::string& source_name) | 99 WebUIDataSourceImpl::WebUIDataSourceImpl(const std::string& source_name) |
100 : URLDataSourceImpl(source_name, new InternalDataSource(this)), | 100 : URLDataSourceImpl(source_name, new InternalDataSource(this)), |
101 source_name_(source_name), | 101 source_name_(source_name), |
102 default_resource_(-1), | 102 default_resource_(-1), |
103 add_csp_(true), | 103 add_csp_(true), |
104 script_src_set_(false), | 104 script_src_set_(false), |
105 object_src_set_(false), | 105 object_src_set_(false), |
106 frame_src_set_(false), | 106 frame_src_set_(false), |
107 deny_xframe_options_(true), | 107 deny_xframe_options_(true), |
108 add_load_time_data_defaults_(true), | 108 add_load_time_data_defaults_(true), |
109 replace_existing_source_(true), | 109 replace_existing_source_(true), |
110 use_gzip_for_all_paths_(false) {} | 110 use_gzip_(false) {} |
111 | 111 |
112 WebUIDataSourceImpl::~WebUIDataSourceImpl() { | 112 WebUIDataSourceImpl::~WebUIDataSourceImpl() { |
113 } | 113 } |
114 | 114 |
115 void WebUIDataSourceImpl::AddString(const std::string& name, | 115 void WebUIDataSourceImpl::AddString(const std::string& name, |
116 const base::string16& value) { | 116 const base::string16& value) { |
117 // TODO(dschuyler): Share only one copy of these strings. | 117 // TODO(dschuyler): Share only one copy of these strings. |
118 localized_strings_.SetString(name, value); | 118 localized_strings_.SetString(name, value); |
119 replacements_[name] = base::UTF16ToUTF8(value); | 119 replacements_[name] = base::UTF16ToUTF8(value); |
120 } | 120 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 158 |
159 void WebUIDataSourceImpl::AddInteger(const std::string& name, int32_t value) { | 159 void WebUIDataSourceImpl::AddInteger(const std::string& name, int32_t value) { |
160 localized_strings_.SetInteger(name, value); | 160 localized_strings_.SetInteger(name, value); |
161 } | 161 } |
162 | 162 |
163 void WebUIDataSourceImpl::SetJsonPath(const std::string& path) { | 163 void WebUIDataSourceImpl::SetJsonPath(const std::string& path) { |
164 DCHECK(json_path_.empty()); | 164 DCHECK(json_path_.empty()); |
165 DCHECK(!path.empty()); | 165 DCHECK(!path.empty()); |
166 | 166 |
167 json_path_ = path; | 167 json_path_ = path; |
168 ExcludePathFromGzip(json_path_); | 168 excluded_paths_.insert(json_path_); |
169 } | 169 } |
170 | 170 |
171 void WebUIDataSourceImpl::AddResourcePath(const std::string &path, | 171 void WebUIDataSourceImpl::AddResourcePath(const std::string &path, |
172 int resource_id) { | 172 int resource_id) { |
173 path_to_idr_map_[path] = resource_id; | 173 path_to_idr_map_[path] = resource_id; |
174 } | 174 } |
175 | 175 |
176 void WebUIDataSourceImpl::SetDefaultResource(int resource_id) { | 176 void WebUIDataSourceImpl::SetDefaultResource(int resource_id) { |
177 default_resource_ = resource_id; | 177 default_resource_ = resource_id; |
178 } | 178 } |
179 | 179 |
180 void WebUIDataSourceImpl::SetRequestFilter( | 180 void WebUIDataSourceImpl::SetRequestFilter( |
181 const WebUIDataSource::HandleRequestCallback& callback) { | 181 const WebUIDataSource::HandleRequestCallback& callback) { |
182 filter_callback_ = callback; | 182 filter_callback_ = callback; |
183 } | 183 } |
184 | 184 |
185 void WebUIDataSourceImpl::DisableReplaceExistingSource() { | 185 void WebUIDataSourceImpl::DisableReplaceExistingSource() { |
186 replace_existing_source_ = false; | 186 replace_existing_source_ = false; |
187 } | 187 } |
188 | 188 |
189 void WebUIDataSourceImpl::ExcludePathFromGzip(const std::string& path) { | |
190 excluded_paths_.insert(path); | |
191 } | |
192 | |
193 bool WebUIDataSourceImpl::IsWebUIDataSourceImpl() const { | 189 bool WebUIDataSourceImpl::IsWebUIDataSourceImpl() const { |
194 return true; | 190 return true; |
195 } | 191 } |
196 | 192 |
197 void WebUIDataSourceImpl::DisableContentSecurityPolicy() { | 193 void WebUIDataSourceImpl::DisableContentSecurityPolicy() { |
198 add_csp_ = false; | 194 add_csp_ = false; |
199 } | 195 } |
200 | 196 |
201 void WebUIDataSourceImpl::OverrideContentSecurityPolicyScriptSrc( | 197 void WebUIDataSourceImpl::OverrideContentSecurityPolicyScriptSrc( |
202 const std::string& data) { | 198 const std::string& data) { |
(...skipping 10 matching lines...) Expand all Loading... |
213 void WebUIDataSourceImpl::OverrideContentSecurityPolicyChildSrc( | 209 void WebUIDataSourceImpl::OverrideContentSecurityPolicyChildSrc( |
214 const std::string& data) { | 210 const std::string& data) { |
215 frame_src_set_ = true; | 211 frame_src_set_ = true; |
216 frame_src_ = data; | 212 frame_src_ = data; |
217 } | 213 } |
218 | 214 |
219 void WebUIDataSourceImpl::DisableDenyXFrameOptions() { | 215 void WebUIDataSourceImpl::DisableDenyXFrameOptions() { |
220 deny_xframe_options_ = false; | 216 deny_xframe_options_ = false; |
221 } | 217 } |
222 | 218 |
223 void WebUIDataSourceImpl::DisableI18nAndUseGzipForAllPaths() { | 219 void WebUIDataSourceImpl::UseGzip( |
224 use_gzip_for_all_paths_ = true; | 220 const std::unordered_set<std::string>& excluded_paths) { |
| 221 use_gzip_ = true; |
| 222 for (const auto& path : excluded_paths) |
| 223 excluded_paths_.insert(path); |
225 } | 224 } |
226 | 225 |
227 const ui::TemplateReplacements* WebUIDataSourceImpl::GetReplacements() const { | 226 const ui::TemplateReplacements* WebUIDataSourceImpl::GetReplacements() const { |
228 return &replacements_; | 227 return &replacements_; |
229 } | 228 } |
230 | 229 |
231 void WebUIDataSourceImpl::EnsureLoadTimeDataDefaultsAdded() { | 230 void WebUIDataSourceImpl::EnsureLoadTimeDataDefaultsAdded() { |
232 if (!add_load_time_data_defaults_) | 231 if (!add_load_time_data_defaults_) |
233 return; | 232 return; |
234 | 233 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 } | 294 } |
296 | 295 |
297 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( | 296 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( |
298 const URLDataSource::GotDataCallback& callback) { | 297 const URLDataSource::GotDataCallback& callback) { |
299 std::string template_data; | 298 std::string template_data; |
300 webui::AppendJsonJS(&localized_strings_, &template_data); | 299 webui::AppendJsonJS(&localized_strings_, &template_data); |
301 callback.Run(base::RefCountedString::TakeString(&template_data)); | 300 callback.Run(base::RefCountedString::TakeString(&template_data)); |
302 } | 301 } |
303 | 302 |
304 } // namespace content | 303 } // namespace content |
OLD | NEW |