Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(332)

Side by Side Diff: content/child/web_url_loader_impl.cc

Issue 1710733002: Move multipart resource handling to core/fetch (2/2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@multipart-cleanup
Patch Set: rebase Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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
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" &&
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) { 702 !show_raw_listing) {
714 ftp_listing_delegate_.reset( 703 ftp_listing_delegate_.reset(
715 new FtpDirectoryListingResponseDelegate(client_, loader_, response)); 704 new FtpDirectoryListingResponseDelegate(client_, loader_, response));
716 } 705 }
717 } 706 }
718 707
719 void WebURLLoaderImpl::Context::OnDownloadedData(int len, 708 void WebURLLoaderImpl::Context::OnDownloadedData(int len,
720 int encoded_data_length) { 709 int encoded_data_length) {
721 if (client_) 710 if (client_)
722 client_->didDownloadData(loader_, len, encoded_data_length); 711 client_->didDownloadData(loader_, len, encoded_data_length);
723 } 712 }
724 713
725 void WebURLLoaderImpl::Context::OnReceivedData(scoped_ptr<ReceivedData> data) { 714 void WebURLLoaderImpl::Context::OnReceivedData(scoped_ptr<ReceivedData> data) {
726 const char* payload = data->payload(); 715 const char* payload = data->payload();
727 int data_length = data->length(); 716 int data_length = data->length();
728 int encoded_data_length = data->encoded_length(); 717 int encoded_data_length = data->encoded_length();
729 if (!client_) 718 if (!client_)
730 return; 719 return;
731 720
732 if (ftp_listing_delegate_) { 721 if (ftp_listing_delegate_) {
733 // The FTP listing delegate will make the appropriate calls to 722 // The FTP listing delegate will make the appropriate calls to
734 // client_->didReceiveData and client_->didReceiveResponse. 723 // client_->didReceiveData and client_->didReceiveResponse.
735 ftp_listing_delegate_->OnReceivedData(payload, data_length); 724 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 { 725 } else {
742 // We dispatch the data even when |useStreamOnResponse()| is set, in order 726 // We dispatch the data even when |useStreamOnResponse()| is set, in order
743 // to make Devtools work. 727 // to make Devtools work.
744 client_->didReceiveData(loader_, payload, data_length, encoded_data_length); 728 client_->didReceiveData(loader_, payload, data_length, encoded_data_length);
745 729
746 if (request_.useStreamOnResponse()) { 730 if (request_.useStreamOnResponse()) {
747 // We don't support ftp_listening_delegate_ and multipart_delegate_ for 731 // We don't support ftp_listening_delegate_ for now.
748 // now. 732 // TODO(yhirano): Support ftp listening.
749 // TODO(yhirano): Support ftp listening and multipart.
750 body_stream_writer_->AddData(std::move(data)); 733 body_stream_writer_->AddData(std::move(data));
751 } 734 }
752 } 735 }
753 } 736 }
754 737
755 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata( 738 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata(
756 const char* data, int len) { 739 const char* data, int len) {
757 if (client_) 740 if (client_)
758 client_->didReceiveCachedMetadata(loader_, data, len); 741 client_->didReceiveCachedMetadata(loader_, data, len);
759 } 742 }
760 743
761 void WebURLLoaderImpl::Context::OnCompletedRequest( 744 void WebURLLoaderImpl::Context::OnCompletedRequest(
762 int error_code, 745 int error_code,
763 bool was_ignored_by_handler, 746 bool was_ignored_by_handler,
764 bool stale_copy_in_cache, 747 bool stale_copy_in_cache,
765 const std::string& security_info, 748 const std::string& security_info,
766 const base::TimeTicks& completion_time, 749 const base::TimeTicks& completion_time,
767 int64_t total_transfer_size) { 750 int64_t total_transfer_size) {
768 if (ftp_listing_delegate_) { 751 if (ftp_listing_delegate_) {
769 ftp_listing_delegate_->OnCompletedRequest(); 752 ftp_listing_delegate_->OnCompletedRequest();
770 ftp_listing_delegate_.reset(NULL); 753 ftp_listing_delegate_.reset(NULL);
771 } else if (multipart_delegate_) {
772 multipart_delegate_->OnCompletedRequest();
773 multipart_delegate_.reset(NULL);
774 } 754 }
775 755
776 if (body_stream_writer_ && error_code != net::OK) 756 if (body_stream_writer_ && error_code != net::OK)
777 body_stream_writer_->Fail(); 757 body_stream_writer_->Fail();
778 body_stream_writer_.reset(); 758 body_stream_writer_.reset();
779 759
780 if (client_) { 760 if (client_) {
781 if (error_code != net::OK) { 761 if (error_code != net::OK) {
782 client_->didFail( 762 client_->didFail(
783 loader_, 763 loader_,
(...skipping 12 matching lines...) Expand all
796 DCHECK_LT(request_id_, 0); 776 DCHECK_LT(request_id_, 0);
797 } 777 }
798 778
799 void WebURLLoaderImpl::Context::CancelBodyStreaming() { 779 void WebURLLoaderImpl::Context::CancelBodyStreaming() {
800 scoped_refptr<Context> protect(this); 780 scoped_refptr<Context> protect(this);
801 781
802 // Notify renderer clients that the request is canceled. 782 // Notify renderer clients that the request is canceled.
803 if (ftp_listing_delegate_) { 783 if (ftp_listing_delegate_) {
804 ftp_listing_delegate_->OnCompletedRequest(); 784 ftp_listing_delegate_->OnCompletedRequest();
805 ftp_listing_delegate_.reset(NULL); 785 ftp_listing_delegate_.reset(NULL);
806 } else if (multipart_delegate_) {
807 multipart_delegate_->OnCompletedRequest();
808 multipart_delegate_.reset(NULL);
809 } 786 }
810 787
811 if (body_stream_writer_) { 788 if (body_stream_writer_) {
812 body_stream_writer_->Fail(); 789 body_stream_writer_->Fail();
813 body_stream_writer_.reset(); 790 body_stream_writer_.reset();
814 } 791 }
815 if (client_) { 792 if (client_) {
816 // TODO(yhirano): Set |stale_copy_in_cache| appropriately if possible. 793 // TODO(yhirano): Set |stale_copy_in_cache| appropriately if possible.
817 client_->didFail( 794 client_->didFail(
818 loader_, CreateWebURLError(request_.url(), false, net::ERR_ABORTED)); 795 loader_, CreateWebURLError(request_.url(), false, net::ERR_ABORTED));
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after
1207 size_t iterator = 0; 1184 size_t iterator = 0;
1208 while (response_headers->EnumerateHeader(&iterator, name, &value)) { 1185 while (response_headers->EnumerateHeader(&iterator, name, &value)) {
1209 response->addHTTPHeaderField(WebString::fromLatin1(name), 1186 response->addHTTPHeaderField(WebString::fromLatin1(name),
1210 WebString::fromLatin1(value)); 1187 WebString::fromLatin1(value));
1211 } 1188 }
1212 } 1189 }
1213 return true; 1190 return true;
1214 } 1191 }
1215 1192
1216 } // namespace content 1193 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698