| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/child/web_url_loader_impl.h" | 5 #include "content/child/web_url_loader_impl.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
| 21 #include "components/mime_util/mime_util.h" | 21 #include "components/mime_util/mime_util.h" |
| 22 #include "components/scheduler/child/web_task_runner_impl.h" | 22 #include "components/scheduler/child/web_task_runner_impl.h" |
| 23 #include "content/child/child_thread_impl.h" | 23 #include "content/child/child_thread_impl.h" |
| 24 #include "content/child/ftp_directory_listing_response_delegate.h" | 24 #include "content/child/ftp_directory_listing_response_delegate.h" |
| 25 #include "content/child/multipart_response_delegate.h" | |
| 26 #include "content/child/request_extra_data.h" | 25 #include "content/child/request_extra_data.h" |
| 27 #include "content/child/request_info.h" | 26 #include "content/child/request_info.h" |
| 28 #include "content/child/resource_dispatcher.h" | 27 #include "content/child/resource_dispatcher.h" |
| 29 #include "content/child/shared_memory_data_consumer_handle.h" | 28 #include "content/child/shared_memory_data_consumer_handle.h" |
| 30 #include "content/child/sync_load_response.h" | 29 #include "content/child/sync_load_response.h" |
| 31 #include "content/child/web_url_request_util.h" | 30 #include "content/child/web_url_request_util.h" |
| 32 #include "content/child/weburlresponse_extradata_impl.h" | 31 #include "content/child/weburlresponse_extradata_impl.h" |
| 33 #include "content/common/resource_messages.h" | 32 #include "content/common/resource_messages.h" |
| 34 #include "content/common/resource_request_body.h" | 33 #include "content/common/resource_request_body.h" |
| 35 #include "content/common/service_worker/service_worker_types.h" | 34 #include "content/common/service_worker/service_worker_types.h" |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 bool CanHandleDataURLRequestLocally() const; | 350 bool CanHandleDataURLRequestLocally() const; |
| 352 void HandleDataURL(); | 351 void HandleDataURL(); |
| 353 | 352 |
| 354 WebURLLoaderImpl* loader_; | 353 WebURLLoaderImpl* loader_; |
| 355 WebURLRequest request_; | 354 WebURLRequest request_; |
| 356 WebURLLoaderClient* client_; | 355 WebURLLoaderClient* client_; |
| 357 ResourceDispatcher* resource_dispatcher_; | 356 ResourceDispatcher* resource_dispatcher_; |
| 358 scoped_ptr<blink::WebTaskRunner> web_task_runner_; | 357 scoped_ptr<blink::WebTaskRunner> web_task_runner_; |
| 359 WebReferrerPolicy referrer_policy_; | 358 WebReferrerPolicy referrer_policy_; |
| 360 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_; | 359 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_; |
| 361 scoped_ptr<MultipartResponseDelegate> multipart_delegate_; | |
| 362 scoped_ptr<StreamOverrideParameters> stream_override_; | 360 scoped_ptr<StreamOverrideParameters> stream_override_; |
| 363 scoped_ptr<SharedMemoryDataConsumerHandle::Writer> body_stream_writer_; | 361 scoped_ptr<SharedMemoryDataConsumerHandle::Writer> body_stream_writer_; |
| 364 enum DeferState {NOT_DEFERRING, SHOULD_DEFER, DEFERRED_DATA}; | 362 enum DeferState {NOT_DEFERRING, SHOULD_DEFER, DEFERRED_DATA}; |
| 365 DeferState defers_loading_; | 363 DeferState defers_loading_; |
| 366 int request_id_; | 364 int request_id_; |
| 367 }; | 365 }; |
| 368 | 366 |
| 369 // A thin wrapper class for Context to ensure its lifetime while it is | 367 // A thin wrapper class for Context to ensure its lifetime while it is |
| 370 // handling IPC messages coming from ResourceDispatcher. Owns one ref to | 368 // handling IPC messages coming from ResourceDispatcher. Owns one ref to |
| 371 // Context and held by ResourceDispatcher. | 369 // Context and held by ResourceDispatcher. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 void WebURLLoaderImpl::Context::Cancel() { | 408 void WebURLLoaderImpl::Context::Cancel() { |
| 411 if (resource_dispatcher_ && // NULL in unittest. | 409 if (resource_dispatcher_ && // NULL in unittest. |
| 412 request_id_ != -1) { | 410 request_id_ != -1) { |
| 413 resource_dispatcher_->Cancel(request_id_); | 411 resource_dispatcher_->Cancel(request_id_); |
| 414 request_id_ = -1; | 412 request_id_ = -1; |
| 415 } | 413 } |
| 416 | 414 |
| 417 if (body_stream_writer_) | 415 if (body_stream_writer_) |
| 418 body_stream_writer_->Fail(); | 416 body_stream_writer_->Fail(); |
| 419 | 417 |
| 420 // Ensure that we do not notify the multipart delegate anymore as it has | 418 // Ensure that we do not notify the delegate anymore as it has |
| 421 // its own pointer to the client. | 419 // its own pointer to the client. |
| 422 if (multipart_delegate_) | |
| 423 multipart_delegate_->Cancel(); | |
| 424 // Ditto for the ftp delegate. | |
| 425 if (ftp_listing_delegate_) | 420 if (ftp_listing_delegate_) |
| 426 ftp_listing_delegate_->Cancel(); | 421 ftp_listing_delegate_->Cancel(); |
| 427 | 422 |
| 428 // Do not make any further calls to the client. | 423 // Do not make any further calls to the client. |
| 429 client_ = NULL; | 424 client_ = NULL; |
| 430 loader_ = NULL; | 425 loader_ = NULL; |
| 431 } | 426 } |
| 432 | 427 |
| 433 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) { | 428 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) { |
| 434 if (request_id_ != -1) | 429 if (request_id_ != -1) |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 | 645 |
| 651 if (info.mime_type == "text/vnd.chromium.ftp-dir") { | 646 if (info.mime_type == "text/vnd.chromium.ftp-dir") { |
| 652 if (show_raw_listing) { | 647 if (show_raw_listing) { |
| 653 // Set the MIME type to plain text to prevent any active content. | 648 // Set the MIME type to plain text to prevent any active content. |
| 654 response.setMIMEType("text/plain"); | 649 response.setMIMEType("text/plain"); |
| 655 } else { | 650 } else { |
| 656 // We're going to produce a parsed listing in HTML. | 651 // We're going to produce a parsed listing in HTML. |
| 657 response.setMIMEType("text/html"); | 652 response.setMIMEType("text/html"); |
| 658 } | 653 } |
| 659 } | 654 } |
| 655 if (info.headers.get() && info.mime_type == "multipart/x-mixed-replace") { |
| 656 std::string content_type; |
| 657 info.headers->EnumerateHeader(NULL, "content-type", &content_type); |
| 658 |
| 659 std::string mime_type; |
| 660 std::string charset; |
| 661 bool had_charset = false; |
| 662 std::string boundary; |
| 663 net::HttpUtil::ParseContentType(content_type, &mime_type, &charset, |
| 664 &had_charset, &boundary); |
| 665 base::TrimString(boundary, " \"", &boundary); |
| 666 response.setMultipartBoundary(boundary.data(), boundary.size()); |
| 667 } |
| 660 | 668 |
| 661 if (request_.useStreamOnResponse()) { | 669 if (request_.useStreamOnResponse()) { |
| 662 SharedMemoryDataConsumerHandle::BackpressureMode mode = | 670 SharedMemoryDataConsumerHandle::BackpressureMode mode = |
| 663 SharedMemoryDataConsumerHandle::kDoNotApplyBackpressure; | 671 SharedMemoryDataConsumerHandle::kDoNotApplyBackpressure; |
| 664 if (info.headers && | 672 if (info.headers && |
| 665 info.headers->HasHeaderValue("Cache-Control", "no-store")) { | 673 info.headers->HasHeaderValue("Cache-Control", "no-store")) { |
| 666 mode = SharedMemoryDataConsumerHandle::kApplyBackpressure; | 674 mode = SharedMemoryDataConsumerHandle::kApplyBackpressure; |
| 667 } | 675 } |
| 668 | 676 |
| 669 auto read_handle = make_scoped_ptr(new SharedMemoryDataConsumerHandle( | 677 auto read_handle = make_scoped_ptr(new SharedMemoryDataConsumerHandle( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 683 } else { | 691 } else { |
| 684 client_->didReceiveResponse(loader_, response); | 692 client_->didReceiveResponse(loader_, response); |
| 685 } | 693 } |
| 686 | 694 |
| 687 // We may have been cancelled after didReceiveResponse, which would leave us | 695 // We may have been cancelled after didReceiveResponse, which would leave us |
| 688 // without a client and therefore without much need to do further handling. | 696 // without a client and therefore without much need to do further handling. |
| 689 if (!client_) | 697 if (!client_) |
| 690 return; | 698 return; |
| 691 | 699 |
| 692 DCHECK(!ftp_listing_delegate_.get()); | 700 DCHECK(!ftp_listing_delegate_.get()); |
| 693 DCHECK(!multipart_delegate_.get()); | 701 if (info.mime_type == "text/vnd.chromium.ftp-dir" && !show_raw_listing) { |
| 694 if (info.headers.get() && info.mime_type == "multipart/x-mixed-replace") { | |
| 695 std::string content_type; | |
| 696 info.headers->EnumerateHeader(NULL, "content-type", &content_type); | |
| 697 | |
| 698 std::string mime_type; | |
| 699 std::string charset; | |
| 700 bool had_charset = false; | |
| 701 std::string boundary; | |
| 702 net::HttpUtil::ParseContentType(content_type, &mime_type, &charset, | |
| 703 &had_charset, &boundary); | |
| 704 base::TrimString(boundary, " \"", &boundary); | |
| 705 | |
| 706 // If there's no boundary, just handle the request normally. In the gecko | |
| 707 // code, nsMultiMixedConv::OnStartRequest throws an exception. | |
| 708 if (!boundary.empty()) { | |
| 709 multipart_delegate_.reset( | |
| 710 new MultipartResponseDelegate(client_, loader_, response, boundary)); | |
| 711 } | |
| 712 } else if (info.mime_type == "text/vnd.chromium.ftp-dir" && | |
| 713 !show_raw_listing) { | |
| 714 ftp_listing_delegate_.reset( | 702 ftp_listing_delegate_.reset( |
| 715 new FtpDirectoryListingResponseDelegate(client_, loader_, response)); | 703 new FtpDirectoryListingResponseDelegate(client_, loader_, response)); |
| 716 } | 704 } |
| 717 } | 705 } |
| 718 | 706 |
| 719 void WebURLLoaderImpl::Context::OnDownloadedData(int len, | 707 void WebURLLoaderImpl::Context::OnDownloadedData(int len, |
| 720 int encoded_data_length) { | 708 int encoded_data_length) { |
| 721 if (client_) | 709 if (client_) |
| 722 client_->didDownloadData(loader_, len, encoded_data_length); | 710 client_->didDownloadData(loader_, len, encoded_data_length); |
| 723 } | 711 } |
| 724 | 712 |
| 725 void WebURLLoaderImpl::Context::OnReceivedData(scoped_ptr<ReceivedData> data) { | 713 void WebURLLoaderImpl::Context::OnReceivedData(scoped_ptr<ReceivedData> data) { |
| 726 const char* payload = data->payload(); | 714 const char* payload = data->payload(); |
| 727 int data_length = data->length(); | 715 int data_length = data->length(); |
| 728 int encoded_data_length = data->encoded_length(); | 716 int encoded_data_length = data->encoded_length(); |
| 729 if (!client_) | 717 if (!client_) |
| 730 return; | 718 return; |
| 731 | 719 |
| 732 if (ftp_listing_delegate_) { | 720 if (ftp_listing_delegate_) { |
| 733 // The FTP listing delegate will make the appropriate calls to | 721 // The FTP listing delegate will make the appropriate calls to |
| 734 // client_->didReceiveData and client_->didReceiveResponse. | 722 // client_->didReceiveData and client_->didReceiveResponse. |
| 735 ftp_listing_delegate_->OnReceivedData(payload, data_length); | 723 ftp_listing_delegate_->OnReceivedData(payload, data_length); |
| 736 } else if (multipart_delegate_) { | |
| 737 // The multipart delegate will make the appropriate calls to | |
| 738 // client_->didReceiveData and client_->didReceiveResponse. | |
| 739 multipart_delegate_->OnReceivedData(payload, data_length, | |
| 740 encoded_data_length); | |
| 741 } else { | 724 } else { |
| 742 // We dispatch the data even when |useStreamOnResponse()| is set, in order | 725 // We dispatch the data even when |useStreamOnResponse()| is set, in order |
| 743 // to make Devtools work. | 726 // to make Devtools work. |
| 744 client_->didReceiveData(loader_, payload, data_length, encoded_data_length); | 727 client_->didReceiveData(loader_, payload, data_length, encoded_data_length); |
| 745 | 728 |
| 746 if (request_.useStreamOnResponse()) { | 729 if (request_.useStreamOnResponse()) { |
| 747 // We don't support ftp_listening_delegate_ and multipart_delegate_ for | 730 // We don't support ftp_listening_delegate_ for now. |
| 748 // now. | 731 // TODO(yhirano): Support ftp listening. |
| 749 // TODO(yhirano): Support ftp listening and multipart. | |
| 750 body_stream_writer_->AddData(std::move(data)); | 732 body_stream_writer_->AddData(std::move(data)); |
| 751 } | 733 } |
| 752 } | 734 } |
| 753 } | 735 } |
| 754 | 736 |
| 755 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata( | 737 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata( |
| 756 const char* data, int len) { | 738 const char* data, int len) { |
| 757 if (client_) | 739 if (client_) |
| 758 client_->didReceiveCachedMetadata(loader_, data, len); | 740 client_->didReceiveCachedMetadata(loader_, data, len); |
| 759 } | 741 } |
| 760 | 742 |
| 761 void WebURLLoaderImpl::Context::OnCompletedRequest( | 743 void WebURLLoaderImpl::Context::OnCompletedRequest( |
| 762 int error_code, | 744 int error_code, |
| 763 bool was_ignored_by_handler, | 745 bool was_ignored_by_handler, |
| 764 bool stale_copy_in_cache, | 746 bool stale_copy_in_cache, |
| 765 const std::string& security_info, | 747 const std::string& security_info, |
| 766 const base::TimeTicks& completion_time, | 748 const base::TimeTicks& completion_time, |
| 767 int64_t total_transfer_size) { | 749 int64_t total_transfer_size) { |
| 768 if (ftp_listing_delegate_) { | 750 if (ftp_listing_delegate_) { |
| 769 ftp_listing_delegate_->OnCompletedRequest(); | 751 ftp_listing_delegate_->OnCompletedRequest(); |
| 770 ftp_listing_delegate_.reset(NULL); | 752 ftp_listing_delegate_.reset(NULL); |
| 771 } else if (multipart_delegate_) { | |
| 772 multipart_delegate_->OnCompletedRequest(); | |
| 773 multipart_delegate_.reset(NULL); | |
| 774 } | 753 } |
| 775 | 754 |
| 776 if (body_stream_writer_ && error_code != net::OK) | 755 if (body_stream_writer_ && error_code != net::OK) |
| 777 body_stream_writer_->Fail(); | 756 body_stream_writer_->Fail(); |
| 778 body_stream_writer_.reset(); | 757 body_stream_writer_.reset(); |
| 779 | 758 |
| 780 if (client_) { | 759 if (client_) { |
| 781 if (error_code != net::OK) { | 760 if (error_code != net::OK) { |
| 782 client_->didFail( | 761 client_->didFail( |
| 783 loader_, | 762 loader_, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 796 DCHECK_LT(request_id_, 0); | 775 DCHECK_LT(request_id_, 0); |
| 797 } | 776 } |
| 798 | 777 |
| 799 void WebURLLoaderImpl::Context::CancelBodyStreaming() { | 778 void WebURLLoaderImpl::Context::CancelBodyStreaming() { |
| 800 scoped_refptr<Context> protect(this); | 779 scoped_refptr<Context> protect(this); |
| 801 | 780 |
| 802 // Notify renderer clients that the request is canceled. | 781 // Notify renderer clients that the request is canceled. |
| 803 if (ftp_listing_delegate_) { | 782 if (ftp_listing_delegate_) { |
| 804 ftp_listing_delegate_->OnCompletedRequest(); | 783 ftp_listing_delegate_->OnCompletedRequest(); |
| 805 ftp_listing_delegate_.reset(NULL); | 784 ftp_listing_delegate_.reset(NULL); |
| 806 } else if (multipart_delegate_) { | |
| 807 multipart_delegate_->OnCompletedRequest(); | |
| 808 multipart_delegate_.reset(NULL); | |
| 809 } | 785 } |
| 810 | 786 |
| 811 if (body_stream_writer_) { | 787 if (body_stream_writer_) { |
| 812 body_stream_writer_->Fail(); | 788 body_stream_writer_->Fail(); |
| 813 body_stream_writer_.reset(); | 789 body_stream_writer_.reset(); |
| 814 } | 790 } |
| 815 if (client_) { | 791 if (client_) { |
| 816 // TODO(yhirano): Set |stale_copy_in_cache| appropriately if possible. | 792 // TODO(yhirano): Set |stale_copy_in_cache| appropriately if possible. |
| 817 client_->didFail( | 793 client_->didFail( |
| 818 loader_, CreateWebURLError(request_.url(), false, net::ERR_ABORTED)); | 794 loader_, CreateWebURLError(request_.url(), false, net::ERR_ABORTED)); |
| (...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1213 response->clearHTTPHeaderField(webStringName); | 1189 response->clearHTTPHeaderField(webStringName); |
| 1214 while (response_headers->EnumerateHeader(&iterator, name, &value)) { | 1190 while (response_headers->EnumerateHeader(&iterator, name, &value)) { |
| 1215 response->addHTTPHeaderField(webStringName, | 1191 response->addHTTPHeaderField(webStringName, |
| 1216 WebString::fromLatin1(value)); | 1192 WebString::fromLatin1(value)); |
| 1217 } | 1193 } |
| 1218 } | 1194 } |
| 1219 return true; | 1195 return true; |
| 1220 } | 1196 } |
| 1221 | 1197 |
| 1222 } // namespace content | 1198 } // namespace content |
| OLD | NEW |