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/url_data_manager_backend.h" | 5 #include "content/browser/webui/url_data_manager_backend.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
25 #include "base/strings/stringprintf.h" | 25 #include "base/strings/stringprintf.h" |
26 #include "base/threading/thread_task_runner_handle.h" | 26 #include "base/threading/thread_task_runner_handle.h" |
27 #include "base/threading/worker_pool.h" | 27 #include "base/threading/worker_pool.h" |
28 #include "base/trace_event/trace_event.h" | 28 #include "base/trace_event/trace_event.h" |
29 #include "content/browser/blob_storage/chrome_blob_storage_context.h" | 29 #include "content/browser/blob_storage/chrome_blob_storage_context.h" |
30 #include "content/browser/histogram_internals_request_job.h" | 30 #include "content/browser/histogram_internals_request_job.h" |
31 #include "content/browser/net/view_blob_internals_job_factory.h" | 31 #include "content/browser/net/view_blob_internals_job_factory.h" |
32 #include "content/browser/net/view_http_cache_job_factory.h" | 32 #include "content/browser/net/view_http_cache_job_factory.h" |
33 #include "content/browser/resource_context_impl.h" | 33 #include "content/browser/resource_context_impl.h" |
| 34 #include "content/browser/webui/i18n_source_stream.h" |
34 #include "content/browser/webui/shared_resources_data_source.h" | 35 #include "content/browser/webui/shared_resources_data_source.h" |
35 #include "content/browser/webui/url_data_source_impl.h" | 36 #include "content/browser/webui/url_data_source_impl.h" |
36 #include "content/public/browser/browser_context.h" | 37 #include "content/public/browser/browser_context.h" |
37 #include "content/public/browser/browser_thread.h" | 38 #include "content/public/browser/browser_thread.h" |
38 #include "content/public/browser/content_browser_client.h" | 39 #include "content/public/browser/content_browser_client.h" |
39 #include "content/public/browser/render_process_host.h" | 40 #include "content/public/browser/render_process_host.h" |
40 #include "content/public/browser/resource_request_info.h" | 41 #include "content/public/browser/resource_request_info.h" |
41 #include "content/public/common/url_constants.h" | 42 #include "content/public/common/url_constants.h" |
42 #include "net/base/io_buffer.h" | 43 #include "net/base/io_buffer.h" |
43 #include "net/base/net_errors.h" | 44 #include "net/base/net_errors.h" |
44 #include "net/filter/gzip_source_stream.h" | 45 #include "net/filter/gzip_source_stream.h" |
45 #include "net/filter/source_stream.h" | 46 #include "net/filter/source_stream.h" |
46 #include "net/http/http_response_headers.h" | 47 #include "net/http/http_response_headers.h" |
47 #include "net/http/http_status_code.h" | 48 #include "net/http/http_status_code.h" |
48 #include "net/log/net_log_util.h" | 49 #include "net/log/net_log_util.h" |
49 #include "net/url_request/url_request.h" | 50 #include "net/url_request/url_request.h" |
50 #include "net/url_request/url_request_context.h" | 51 #include "net/url_request/url_request_context.h" |
51 #include "net/url_request/url_request_error_job.h" | 52 #include "net/url_request/url_request_error_job.h" |
52 #include "net/url_request/url_request_job.h" | 53 #include "net/url_request/url_request_job.h" |
53 #include "net/url_request/url_request_job_factory.h" | 54 #include "net/url_request/url_request_job_factory.h" |
| 55 #include "ui/base/template_expressions.h" |
54 #include "url/url_util.h" | 56 #include "url/url_util.h" |
55 | 57 |
56 namespace content { | 58 namespace content { |
57 | 59 |
58 namespace { | 60 namespace { |
59 | 61 |
60 const char kChromeURLContentSecurityPolicyHeaderBase[] = | 62 const char kChromeURLContentSecurityPolicyHeaderBase[] = |
61 "Content-Security-Policy: "; | 63 "Content-Security-Policy: "; |
62 | 64 |
63 const char kChromeURLXFrameOptionsHeader[] = "X-Frame-Options: DENY"; | 65 const char kChromeURLXFrameOptionsHeader[] = "X-Frame-Options: DENY"; |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 } | 204 } |
203 | 205 |
204 void set_access_control_allow_origin(const std::string& value) { | 206 void set_access_control_allow_origin(const std::string& value) { |
205 access_control_allow_origin_ = value; | 207 access_control_allow_origin_ = value; |
206 } | 208 } |
207 | 209 |
208 void set_is_gzipped(bool is_gzipped) { | 210 void set_is_gzipped(bool is_gzipped) { |
209 is_gzipped_ = is_gzipped; | 211 is_gzipped_ = is_gzipped; |
210 } | 212 } |
211 | 213 |
| 214 void SetReplacements(const ui::TemplateReplacements* replacements) { |
| 215 replacements_ = replacements; |
| 216 } |
| 217 |
212 // Returns true when job was generated from an incognito profile. | 218 // Returns true when job was generated from an incognito profile. |
213 bool is_incognito() const { | 219 bool is_incognito() const { |
214 return is_incognito_; | 220 return is_incognito_; |
215 } | 221 } |
216 | 222 |
217 private: | 223 private: |
218 ~URLRequestChromeJob() override; | 224 ~URLRequestChromeJob() override; |
219 | 225 |
220 // Helper for Start(), to let us start asynchronously. | 226 // Helper for Start(), to let us start asynchronously. |
221 // (This pattern is shared by most net::URLRequestJob implementations.) | 227 // (This pattern is shared by most net::URLRequestJob implementations.) |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 // string. | 278 // string. |
273 std::string access_control_allow_origin_; | 279 std::string access_control_allow_origin_; |
274 | 280 |
275 // True when job is generated from an incognito profile. | 281 // True when job is generated from an incognito profile. |
276 const bool is_incognito_; | 282 const bool is_incognito_; |
277 | 283 |
278 // True when gzip encoding should be used. NOTE: this requires the original | 284 // True when gzip encoding should be used. NOTE: this requires the original |
279 // resources in resources.pak use compress="gzip". | 285 // resources in resources.pak use compress="gzip". |
280 bool is_gzipped_; | 286 bool is_gzipped_; |
281 | 287 |
| 288 // Replacement dictionary for i18n. |
| 289 const ui::TemplateReplacements* replacements_; |
| 290 |
282 // The backend is owned by net::URLRequestContext and always outlives us. | 291 // The backend is owned by net::URLRequestContext and always outlives us. |
283 URLDataManagerBackend* const backend_; | 292 URLDataManagerBackend* const backend_; |
284 | 293 |
285 base::WeakPtrFactory<URLRequestChromeJob> weak_factory_; | 294 base::WeakPtrFactory<URLRequestChromeJob> weak_factory_; |
286 | 295 |
287 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); | 296 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob); |
288 }; | 297 }; |
289 | 298 |
290 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request, | 299 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request, |
291 net::NetworkDelegate* network_delegate, | 300 net::NetworkDelegate* network_delegate, |
292 URLDataManagerBackend* backend, | 301 URLDataManagerBackend* backend, |
293 bool is_incognito) | 302 bool is_incognito) |
294 : net::URLRequestJob(request, network_delegate), | 303 : net::URLRequestJob(request, network_delegate), |
295 data_offset_(0), | 304 data_offset_(0), |
296 data_available_status_(net::OK), | 305 data_available_status_(net::OK), |
297 pending_buf_size_(0), | 306 pending_buf_size_(0), |
298 allow_caching_(true), | 307 allow_caching_(true), |
299 add_content_security_policy_(true), | 308 add_content_security_policy_(true), |
300 deny_xframe_options_(true), | 309 deny_xframe_options_(true), |
301 send_content_type_header_(false), | 310 send_content_type_header_(false), |
302 is_incognito_(is_incognito), | 311 is_incognito_(is_incognito), |
303 is_gzipped_(false), | 312 is_gzipped_(false), |
| 313 replacements_(nullptr), |
304 backend_(backend), | 314 backend_(backend), |
305 weak_factory_(this) { | 315 weak_factory_(this) { |
306 DCHECK(backend); | 316 DCHECK(backend); |
307 } | 317 } |
308 | 318 |
309 URLRequestChromeJob::~URLRequestChromeJob() { | 319 URLRequestChromeJob::~URLRequestChromeJob() { |
310 CHECK(!backend_->HasPendingJob(this)); | 320 CHECK(!backend_->HasPendingJob(this)); |
311 } | 321 } |
312 | 322 |
313 void URLRequestChromeJob::Start() { | 323 void URLRequestChromeJob::Start() { |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
388 info->headers->AddHeader("Access-Control-Allow-Origin: " + | 398 info->headers->AddHeader("Access-Control-Allow-Origin: " + |
389 access_control_allow_origin_); | 399 access_control_allow_origin_); |
390 info->headers->AddHeader("Vary: Origin"); | 400 info->headers->AddHeader("Vary: Origin"); |
391 } | 401 } |
392 | 402 |
393 if (is_gzipped_) | 403 if (is_gzipped_) |
394 info->headers->AddHeader("Content-Encoding: gzip"); | 404 info->headers->AddHeader("Content-Encoding: gzip"); |
395 } | 405 } |
396 | 406 |
397 std::unique_ptr<net::SourceStream> URLRequestChromeJob::SetUpSourceStream() { | 407 std::unique_ptr<net::SourceStream> URLRequestChromeJob::SetUpSourceStream() { |
398 std::unique_ptr<net::SourceStream> source = | 408 std::unique_ptr<net::SourceStream> source_stream = |
399 net::URLRequestJob::SetUpSourceStream(); | 409 net::URLRequestJob::SetUpSourceStream(); |
400 return is_gzipped_ ? net::GzipSourceStream::Create( | 410 |
401 std::move(source), net::SourceStream::TYPE_GZIP) | 411 if (is_gzipped_) { |
402 : std::move(source); | 412 source_stream = net::GzipSourceStream::Create(std::move(source_stream), |
| 413 net::SourceStream::TYPE_GZIP); |
| 414 } |
| 415 |
| 416 if (replacements_) { |
| 417 source_stream = content::I18nSourceStream::Create( |
| 418 std::move(source_stream), net::SourceStream::TYPE_NONE, replacements_); |
| 419 } |
| 420 |
| 421 return source_stream; |
403 } | 422 } |
404 | 423 |
405 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) { | 424 void URLRequestChromeJob::MimeTypeAvailable(const std::string& mime_type) { |
406 set_mime_type(mime_type); | 425 set_mime_type(mime_type); |
407 NotifyHeadersComplete(); | 426 NotifyHeadersComplete(); |
408 } | 427 } |
409 | 428 |
410 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { | 429 void URLRequestChromeJob::DataAvailable(base::RefCountedMemory* bytes) { |
411 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); | 430 TRACE_EVENT_ASYNC_END0("browser", "DataManager:Request", this); |
412 DCHECK(!data_); | 431 DCHECK(!data_); |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
692 job->set_content_security_policy_style_source( | 711 job->set_content_security_policy_style_source( |
693 source->source()->GetContentSecurityPolicyStyleSrc()); | 712 source->source()->GetContentSecurityPolicyStyleSrc()); |
694 job->set_content_security_policy_image_source( | 713 job->set_content_security_policy_image_source( |
695 source->source()->GetContentSecurityPolicyImgSrc()); | 714 source->source()->GetContentSecurityPolicyImgSrc()); |
696 job->set_deny_xframe_options( | 715 job->set_deny_xframe_options( |
697 source->source()->ShouldDenyXFrameOptions()); | 716 source->source()->ShouldDenyXFrameOptions()); |
698 job->set_send_content_type_header( | 717 job->set_send_content_type_header( |
699 source->source()->ShouldServeMimeTypeAsContentTypeHeader()); | 718 source->source()->ShouldServeMimeTypeAsContentTypeHeader()); |
700 job->set_is_gzipped(source->source()->IsGzipped(path)); | 719 job->set_is_gzipped(source->source()->IsGzipped(path)); |
701 | 720 |
| 721 // TODO(dschuyler): improve filtering of which resource to run template |
| 722 // replacements upon. |
| 723 if (source->source()->GetMimeType(path) == "text/html") |
| 724 job->SetReplacements(source->GetReplacements()); |
| 725 |
702 std::string origin = GetOriginHeaderValue(request); | 726 std::string origin = GetOriginHeaderValue(request); |
703 if (!origin.empty()) { | 727 if (!origin.empty()) { |
704 std::string header = | 728 std::string header = |
705 source->source()->GetAccessControlAllowOriginForOrigin(origin); | 729 source->source()->GetAccessControlAllowOriginForOrigin(origin); |
706 DCHECK(header.empty() || header == origin || header == "*" || | 730 DCHECK(header.empty() || header == origin || header == "*" || |
707 header == "null"); | 731 header == "null"); |
708 job->set_access_control_allow_origin(header); | 732 job->set_access_control_allow_origin(header); |
709 } | 733 } |
710 | 734 |
711 // Look up additional request info to pass down. | 735 // Look up additional request info to pass down. |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
856 | 880 |
857 } // namespace | 881 } // namespace |
858 | 882 |
859 net::URLRequestJobFactory::ProtocolHandler* CreateDevToolsProtocolHandler( | 883 net::URLRequestJobFactory::ProtocolHandler* CreateDevToolsProtocolHandler( |
860 ResourceContext* resource_context, | 884 ResourceContext* resource_context, |
861 bool is_incognito) { | 885 bool is_incognito) { |
862 return new DevToolsJobFactory(resource_context, is_incognito); | 886 return new DevToolsJobFactory(resource_context, is_incognito); |
863 } | 887 } |
864 | 888 |
865 } // namespace content | 889 } // namespace content |
OLD | NEW |