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

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, 9 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 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 644
650 if (info.mime_type == "text/vnd.chromium.ftp-dir") { 645 if (info.mime_type == "text/vnd.chromium.ftp-dir") {
651 if (show_raw_listing) { 646 if (show_raw_listing) {
652 // Set the MIME type to plain text to prevent any active content. 647 // Set the MIME type to plain text to prevent any active content.
653 response.setMIMEType("text/plain"); 648 response.setMIMEType("text/plain");
654 } else { 649 } else {
655 // We're going to produce a parsed listing in HTML. 650 // We're going to produce a parsed listing in HTML.
656 response.setMIMEType("text/html"); 651 response.setMIMEType("text/html");
657 } 652 }
658 } 653 }
654 if (info.headers.get() && info.mime_type == "multipart/x-mixed-replace") {
655 std::string content_type;
656 info.headers->EnumerateHeader(NULL, "content-type", &content_type);
657
658 std::string mime_type;
659 std::string charset;
660 bool had_charset = false;
661 std::string boundary;
662 net::HttpUtil::ParseContentType(content_type, &mime_type, &charset,
663 &had_charset, &boundary);
664 base::TrimString(boundary, " \"", &boundary);
665 response.setMultipartBoundary(boundary.data(), boundary.size());
666 }
659 667
660 if (request_.useStreamOnResponse()) { 668 if (request_.useStreamOnResponse()) {
661 SharedMemoryDataConsumerHandle::BackpressureMode mode = 669 SharedMemoryDataConsumerHandle::BackpressureMode mode =
662 SharedMemoryDataConsumerHandle::kDoNotApplyBackpressure; 670 SharedMemoryDataConsumerHandle::kDoNotApplyBackpressure;
663 if (info.headers && 671 if (info.headers &&
664 info.headers->HasHeaderValue("Cache-Control", "no-store")) { 672 info.headers->HasHeaderValue("Cache-Control", "no-store")) {
665 mode = SharedMemoryDataConsumerHandle::kApplyBackpressure; 673 mode = SharedMemoryDataConsumerHandle::kApplyBackpressure;
666 } 674 }
667 675
668 auto read_handle = make_scoped_ptr(new SharedMemoryDataConsumerHandle( 676 auto read_handle = make_scoped_ptr(new SharedMemoryDataConsumerHandle(
(...skipping 13 matching lines...) Expand all
682 } else { 690 } else {
683 client_->didReceiveResponse(loader_, response); 691 client_->didReceiveResponse(loader_, response);
684 } 692 }
685 693
686 // We may have been cancelled after didReceiveResponse, which would leave us 694 // We may have been cancelled after didReceiveResponse, which would leave us
687 // without a client and therefore without much need to do further handling. 695 // without a client and therefore without much need to do further handling.
688 if (!client_) 696 if (!client_)
689 return; 697 return;
690 698
691 DCHECK(!ftp_listing_delegate_.get()); 699 DCHECK(!ftp_listing_delegate_.get());
692 DCHECK(!multipart_delegate_.get()); 700 if (info.mime_type == "text/vnd.chromium.ftp-dir" && !show_raw_listing) {
693 if (info.headers.get() && info.mime_type == "multipart/x-mixed-replace") {
694 std::string content_type;
695 info.headers->EnumerateHeader(NULL, "content-type", &content_type);
696
697 std::string mime_type;
698 std::string charset;
699 bool had_charset = false;
700 std::string boundary;
701 net::HttpUtil::ParseContentType(content_type, &mime_type, &charset,
702 &had_charset, &boundary);
703 base::TrimString(boundary, " \"", &boundary);
704
705 // If there's no boundary, just handle the request normally. In the gecko
706 // code, nsMultiMixedConv::OnStartRequest throws an exception.
707 if (!boundary.empty()) {
708 multipart_delegate_.reset(
709 new MultipartResponseDelegate(client_, loader_, response, boundary));
710 }
711 } else if (info.mime_type == "text/vnd.chromium.ftp-dir" &&
712 !show_raw_listing) {
713 ftp_listing_delegate_.reset( 701 ftp_listing_delegate_.reset(
714 new FtpDirectoryListingResponseDelegate(client_, loader_, response)); 702 new FtpDirectoryListingResponseDelegate(client_, loader_, response));
715 } 703 }
716 } 704 }
717 705
718 void WebURLLoaderImpl::Context::OnDownloadedData(int len, 706 void WebURLLoaderImpl::Context::OnDownloadedData(int len,
719 int encoded_data_length) { 707 int encoded_data_length) {
720 if (client_) 708 if (client_)
721 client_->didDownloadData(loader_, len, encoded_data_length); 709 client_->didDownloadData(loader_, len, encoded_data_length);
722 } 710 }
723 711
724 void WebURLLoaderImpl::Context::OnReceivedData(scoped_ptr<ReceivedData> data) { 712 void WebURLLoaderImpl::Context::OnReceivedData(scoped_ptr<ReceivedData> data) {
725 const char* payload = data->payload(); 713 const char* payload = data->payload();
726 int data_length = data->length(); 714 int data_length = data->length();
727 int encoded_data_length = data->encoded_length(); 715 int encoded_data_length = data->encoded_length();
728 if (!client_) 716 if (!client_)
729 return; 717 return;
730 718
731 if (ftp_listing_delegate_) { 719 if (ftp_listing_delegate_) {
732 // The FTP listing delegate will make the appropriate calls to 720 // The FTP listing delegate will make the appropriate calls to
733 // client_->didReceiveData and client_->didReceiveResponse. 721 // client_->didReceiveData and client_->didReceiveResponse.
734 ftp_listing_delegate_->OnReceivedData(payload, data_length); 722 ftp_listing_delegate_->OnReceivedData(payload, data_length);
735 } else if (multipart_delegate_) {
736 // The multipart delegate will make the appropriate calls to
737 // client_->didReceiveData and client_->didReceiveResponse.
738 multipart_delegate_->OnReceivedData(payload, data_length,
739 encoded_data_length);
740 } else { 723 } else {
741 // We dispatch the data even when |useStreamOnResponse()| is set, in order 724 // We dispatch the data even when |useStreamOnResponse()| is set, in order
742 // to make Devtools work. 725 // to make Devtools work.
743 client_->didReceiveData(loader_, payload, data_length, encoded_data_length); 726 client_->didReceiveData(loader_, payload, data_length, encoded_data_length);
744 727
745 if (request_.useStreamOnResponse()) { 728 if (request_.useStreamOnResponse()) {
746 // We don't support ftp_listening_delegate_ and multipart_delegate_ for 729 // We don't support ftp_listening_delegate_ for now.
747 // now. 730 // TODO(yhirano): Support ftp listening.
748 // TODO(yhirano): Support ftp listening and multipart.
749 body_stream_writer_->AddData(std::move(data)); 731 body_stream_writer_->AddData(std::move(data));
750 } 732 }
751 } 733 }
752 } 734 }
753 735
754 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata( 736 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata(
755 const char* data, int len) { 737 const char* data, int len) {
756 if (client_) 738 if (client_)
757 client_->didReceiveCachedMetadata(loader_, data, len); 739 client_->didReceiveCachedMetadata(loader_, data, len);
758 } 740 }
759 741
760 void WebURLLoaderImpl::Context::OnCompletedRequest( 742 void WebURLLoaderImpl::Context::OnCompletedRequest(
761 int error_code, 743 int error_code,
762 bool was_ignored_by_handler, 744 bool was_ignored_by_handler,
763 bool stale_copy_in_cache, 745 bool stale_copy_in_cache,
764 const std::string& security_info, 746 const std::string& security_info,
765 const base::TimeTicks& completion_time, 747 const base::TimeTicks& completion_time,
766 int64_t total_transfer_size) { 748 int64_t total_transfer_size) {
767 if (ftp_listing_delegate_) { 749 if (ftp_listing_delegate_) {
768 ftp_listing_delegate_->OnCompletedRequest(); 750 ftp_listing_delegate_->OnCompletedRequest();
769 ftp_listing_delegate_.reset(NULL); 751 ftp_listing_delegate_.reset(NULL);
770 } else if (multipart_delegate_) {
771 multipart_delegate_->OnCompletedRequest();
772 multipart_delegate_.reset(NULL);
773 } 752 }
774 753
775 if (body_stream_writer_ && error_code != net::OK) 754 if (body_stream_writer_ && error_code != net::OK)
776 body_stream_writer_->Fail(); 755 body_stream_writer_->Fail();
777 body_stream_writer_.reset(); 756 body_stream_writer_.reset();
778 757
779 if (client_) { 758 if (client_) {
780 if (error_code != net::OK) { 759 if (error_code != net::OK) {
781 client_->didFail( 760 client_->didFail(
782 loader_, 761 loader_,
(...skipping 12 matching lines...) Expand all
795 DCHECK_LT(request_id_, 0); 774 DCHECK_LT(request_id_, 0);
796 } 775 }
797 776
798 void WebURLLoaderImpl::Context::CancelBodyStreaming() { 777 void WebURLLoaderImpl::Context::CancelBodyStreaming() {
799 scoped_refptr<Context> protect(this); 778 scoped_refptr<Context> protect(this);
800 779
801 // Notify renderer clients that the request is canceled. 780 // Notify renderer clients that the request is canceled.
802 if (ftp_listing_delegate_) { 781 if (ftp_listing_delegate_) {
803 ftp_listing_delegate_->OnCompletedRequest(); 782 ftp_listing_delegate_->OnCompletedRequest();
804 ftp_listing_delegate_.reset(NULL); 783 ftp_listing_delegate_.reset(NULL);
805 } else if (multipart_delegate_) {
806 multipart_delegate_->OnCompletedRequest();
807 multipart_delegate_.reset(NULL);
808 } 784 }
809 785
810 if (body_stream_writer_) { 786 if (body_stream_writer_) {
811 body_stream_writer_->Fail(); 787 body_stream_writer_->Fail();
812 body_stream_writer_.reset(); 788 body_stream_writer_.reset();
813 } 789 }
814 if (client_) { 790 if (client_) {
815 // TODO(yhirano): Set |stale_copy_in_cache| appropriately if possible. 791 // TODO(yhirano): Set |stale_copy_in_cache| appropriately if possible.
816 client_->didFail( 792 client_->didFail(
817 loader_, CreateWebURLError(request_.url(), false, net::ERR_ABORTED)); 793 loader_, CreateWebURLError(request_.url(), false, net::ERR_ABORTED));
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
1212 response->clearHTTPHeaderField(webStringName); 1188 response->clearHTTPHeaderField(webStringName);
1213 while (response_headers->EnumerateHeader(&iterator, name, &value)) { 1189 while (response_headers->EnumerateHeader(&iterator, name, &value)) {
1214 response->addHTTPHeaderField(webStringName, 1190 response->addHTTPHeaderField(webStringName,
1215 WebString::fromLatin1(value)); 1191 WebString::fromLatin1(value));
1216 } 1192 }
1217 } 1193 }
1218 return true; 1194 return true;
1219 } 1195 }
1220 1196
1221 } // namespace content 1197 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698