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

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: 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 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 bool CanHandleDataURLRequestLocally() const; 348 bool CanHandleDataURLRequestLocally() const;
350 void HandleDataURL(); 349 void HandleDataURL();
351 350
352 WebURLLoaderImpl* loader_; 351 WebURLLoaderImpl* loader_;
353 WebURLRequest request_; 352 WebURLRequest request_;
354 WebURLLoaderClient* client_; 353 WebURLLoaderClient* client_;
355 ResourceDispatcher* resource_dispatcher_; 354 ResourceDispatcher* resource_dispatcher_;
356 scoped_ptr<blink::WebTaskRunner> web_task_runner_; 355 scoped_ptr<blink::WebTaskRunner> web_task_runner_;
357 WebReferrerPolicy referrer_policy_; 356 WebReferrerPolicy referrer_policy_;
358 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_; 357 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_;
359 scoped_ptr<MultipartResponseDelegate> multipart_delegate_;
360 scoped_ptr<StreamOverrideParameters> stream_override_; 358 scoped_ptr<StreamOverrideParameters> stream_override_;
361 scoped_ptr<SharedMemoryDataConsumerHandle::Writer> body_stream_writer_; 359 scoped_ptr<SharedMemoryDataConsumerHandle::Writer> body_stream_writer_;
362 enum DeferState {NOT_DEFERRING, SHOULD_DEFER, DEFERRED_DATA}; 360 enum DeferState {NOT_DEFERRING, SHOULD_DEFER, DEFERRED_DATA};
363 DeferState defers_loading_; 361 DeferState defers_loading_;
364 int request_id_; 362 int request_id_;
365 }; 363 };
366 364
367 // A thin wrapper class for Context to ensure its lifetime while it is 365 // A thin wrapper class for Context to ensure its lifetime while it is
368 // handling IPC messages coming from ResourceDispatcher. Owns one ref to 366 // handling IPC messages coming from ResourceDispatcher. Owns one ref to
369 // Context and held by ResourceDispatcher. 367 // Context and held by ResourceDispatcher.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 void WebURLLoaderImpl::Context::Cancel() { 406 void WebURLLoaderImpl::Context::Cancel() {
409 if (resource_dispatcher_ && // NULL in unittest. 407 if (resource_dispatcher_ && // NULL in unittest.
410 request_id_ != -1) { 408 request_id_ != -1) {
411 resource_dispatcher_->Cancel(request_id_); 409 resource_dispatcher_->Cancel(request_id_);
412 request_id_ = -1; 410 request_id_ = -1;
413 } 411 }
414 412
415 if (body_stream_writer_) 413 if (body_stream_writer_)
416 body_stream_writer_->Fail(); 414 body_stream_writer_->Fail();
417 415
418 // Ensure that we do not notify the multipart delegate anymore as it has 416 // Ensure that we do not notify the delegate anymore as it has
419 // its own pointer to the client. 417 // its own pointer to the client.
420 if (multipart_delegate_)
421 multipart_delegate_->Cancel();
422 // Ditto for the ftp delegate.
423 if (ftp_listing_delegate_) 418 if (ftp_listing_delegate_)
424 ftp_listing_delegate_->Cancel(); 419 ftp_listing_delegate_->Cancel();
425 420
426 // Do not make any further calls to the client. 421 // Do not make any further calls to the client.
427 client_ = NULL; 422 client_ = NULL;
428 loader_ = NULL; 423 loader_ = NULL;
429 } 424 }
430 425
431 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) { 426 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) {
432 if (request_id_ != -1) 427 if (request_id_ != -1)
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 633
639 if (info.mime_type == "text/vnd.chromium.ftp-dir") { 634 if (info.mime_type == "text/vnd.chromium.ftp-dir") {
640 if (show_raw_listing) { 635 if (show_raw_listing) {
641 // Set the MIME type to plain text to prevent any active content. 636 // Set the MIME type to plain text to prevent any active content.
642 response.setMIMEType("text/plain"); 637 response.setMIMEType("text/plain");
643 } else { 638 } else {
644 // We're going to produce a parsed listing in HTML. 639 // We're going to produce a parsed listing in HTML.
645 response.setMIMEType("text/html"); 640 response.setMIMEType("text/html");
646 } 641 }
647 } 642 }
643 if (info.headers.get() && info.mime_type == "multipart/x-mixed-replace") {
644 std::string content_type;
645 info.headers->EnumerateHeader(NULL, "content-type", &content_type);
646
647 std::string mime_type;
648 std::string charset;
649 bool had_charset = false;
650 std::string boundary;
651 net::HttpUtil::ParseContentType(content_type, &mime_type, &charset,
652 &had_charset, &boundary);
653 base::TrimString(boundary, " \"", &boundary);
654 response.setMultipartBoundary(boundary.data(), boundary.size());
655 }
648 656
649 if (request_.useStreamOnResponse()) { 657 if (request_.useStreamOnResponse()) {
650 SharedMemoryDataConsumerHandle::BackpressureMode mode = 658 SharedMemoryDataConsumerHandle::BackpressureMode mode =
651 SharedMemoryDataConsumerHandle::kDoNotApplyBackpressure; 659 SharedMemoryDataConsumerHandle::kDoNotApplyBackpressure;
652 if (info.headers && 660 if (info.headers &&
653 info.headers->HasHeaderValue("Cache-Control", "no-store")) { 661 info.headers->HasHeaderValue("Cache-Control", "no-store")) {
654 mode = SharedMemoryDataConsumerHandle::kApplyBackpressure; 662 mode = SharedMemoryDataConsumerHandle::kApplyBackpressure;
655 } 663 }
656 664
657 auto read_handle = make_scoped_ptr(new SharedMemoryDataConsumerHandle( 665 auto read_handle = make_scoped_ptr(new SharedMemoryDataConsumerHandle(
(...skipping 13 matching lines...) Expand all
671 } else { 679 } else {
672 client_->didReceiveResponse(loader_, response); 680 client_->didReceiveResponse(loader_, response);
673 } 681 }
674 682
675 // We may have been cancelled after didReceiveResponse, which would leave us 683 // We may have been cancelled after didReceiveResponse, which would leave us
676 // without a client and therefore without much need to do further handling. 684 // without a client and therefore without much need to do further handling.
677 if (!client_) 685 if (!client_)
678 return; 686 return;
679 687
680 DCHECK(!ftp_listing_delegate_.get()); 688 DCHECK(!ftp_listing_delegate_.get());
681 DCHECK(!multipart_delegate_.get()); 689 if (info.mime_type == "text/vnd.chromium.ftp-dir" && !show_raw_listing) {
682 if (info.headers.get() && info.mime_type == "multipart/x-mixed-replace") {
683 std::string content_type;
684 info.headers->EnumerateHeader(NULL, "content-type", &content_type);
685
686 std::string mime_type;
687 std::string charset;
688 bool had_charset = false;
689 std::string boundary;
690 net::HttpUtil::ParseContentType(content_type, &mime_type, &charset,
691 &had_charset, &boundary);
692 base::TrimString(boundary, " \"", &boundary);
693
694 // If there's no boundary, just handle the request normally. In the gecko
695 // code, nsMultiMixedConv::OnStartRequest throws an exception.
696 if (!boundary.empty()) {
697 multipart_delegate_.reset(
698 new MultipartResponseDelegate(client_, loader_, response, boundary));
699 }
700 } else if (info.mime_type == "text/vnd.chromium.ftp-dir" &&
701 !show_raw_listing) {
702 ftp_listing_delegate_.reset( 690 ftp_listing_delegate_.reset(
703 new FtpDirectoryListingResponseDelegate(client_, loader_, response)); 691 new FtpDirectoryListingResponseDelegate(client_, loader_, response));
704 } 692 }
705 } 693 }
706 694
707 void WebURLLoaderImpl::Context::OnDownloadedData(int len, 695 void WebURLLoaderImpl::Context::OnDownloadedData(int len,
708 int encoded_data_length) { 696 int encoded_data_length) {
709 if (client_) 697 if (client_)
710 client_->didDownloadData(loader_, len, encoded_data_length); 698 client_->didDownloadData(loader_, len, encoded_data_length);
711 } 699 }
712 700
713 void WebURLLoaderImpl::Context::OnReceivedData(scoped_ptr<ReceivedData> data) { 701 void WebURLLoaderImpl::Context::OnReceivedData(scoped_ptr<ReceivedData> data) {
714 const char* payload = data->payload(); 702 const char* payload = data->payload();
715 int data_length = data->length(); 703 int data_length = data->length();
716 int encoded_data_length = data->encoded_length(); 704 int encoded_data_length = data->encoded_length();
717 if (!client_) 705 if (!client_)
718 return; 706 return;
719 707
720 if (ftp_listing_delegate_) { 708 if (ftp_listing_delegate_) {
721 // The FTP listing delegate will make the appropriate calls to 709 // The FTP listing delegate will make the appropriate calls to
722 // client_->didReceiveData and client_->didReceiveResponse. 710 // client_->didReceiveData and client_->didReceiveResponse.
723 ftp_listing_delegate_->OnReceivedData(payload, data_length); 711 ftp_listing_delegate_->OnReceivedData(payload, data_length);
724 } else if (multipart_delegate_) {
725 // The multipart delegate will make the appropriate calls to
726 // client_->didReceiveData and client_->didReceiveResponse.
727 multipart_delegate_->OnReceivedData(payload, data_length,
728 encoded_data_length);
729 } else { 712 } else {
730 // We dispatch the data even when |useStreamOnResponse()| is set, in order 713 // We dispatch the data even when |useStreamOnResponse()| is set, in order
731 // to make Devtools work. 714 // to make Devtools work.
732 client_->didReceiveData(loader_, payload, data_length, encoded_data_length); 715 client_->didReceiveData(loader_, payload, data_length, encoded_data_length);
733 716
734 if (request_.useStreamOnResponse()) { 717 if (request_.useStreamOnResponse()) {
735 // We don't support ftp_listening_delegate_ and multipart_delegate_ for 718 // We don't support ftp_listening_delegate_ for now.
736 // now. 719 // TODO(yhirano): Support ftp listening.
737 // TODO(yhirano): Support ftp listening and multipart.
738 body_stream_writer_->AddData(std::move(data)); 720 body_stream_writer_->AddData(std::move(data));
739 } 721 }
740 } 722 }
741 } 723 }
742 724
743 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata( 725 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata(
744 const char* data, int len) { 726 const char* data, int len) {
745 if (client_) 727 if (client_)
746 client_->didReceiveCachedMetadata(loader_, data, len); 728 client_->didReceiveCachedMetadata(loader_, data, len);
747 } 729 }
748 730
749 void WebURLLoaderImpl::Context::OnCompletedRequest( 731 void WebURLLoaderImpl::Context::OnCompletedRequest(
750 int error_code, 732 int error_code,
751 bool was_ignored_by_handler, 733 bool was_ignored_by_handler,
752 bool stale_copy_in_cache, 734 bool stale_copy_in_cache,
753 const std::string& security_info, 735 const std::string& security_info,
754 const base::TimeTicks& completion_time, 736 const base::TimeTicks& completion_time,
755 int64_t total_transfer_size) { 737 int64_t total_transfer_size) {
756 if (ftp_listing_delegate_) { 738 if (ftp_listing_delegate_) {
757 ftp_listing_delegate_->OnCompletedRequest(); 739 ftp_listing_delegate_->OnCompletedRequest();
758 ftp_listing_delegate_.reset(NULL); 740 ftp_listing_delegate_.reset(NULL);
759 } else if (multipart_delegate_) {
760 multipart_delegate_->OnCompletedRequest();
761 multipart_delegate_.reset(NULL);
762 } 741 }
763 742
764 if (body_stream_writer_ && error_code != net::OK) 743 if (body_stream_writer_ && error_code != net::OK)
765 body_stream_writer_->Fail(); 744 body_stream_writer_->Fail();
766 body_stream_writer_.reset(); 745 body_stream_writer_.reset();
767 746
768 if (client_) { 747 if (client_) {
769 if (error_code != net::OK) { 748 if (error_code != net::OK) {
770 client_->didFail( 749 client_->didFail(
771 loader_, 750 loader_,
(...skipping 12 matching lines...) Expand all
784 DCHECK_LT(request_id_, 0); 763 DCHECK_LT(request_id_, 0);
785 } 764 }
786 765
787 void WebURLLoaderImpl::Context::CancelBodyStreaming() { 766 void WebURLLoaderImpl::Context::CancelBodyStreaming() {
788 scoped_refptr<Context> protect(this); 767 scoped_refptr<Context> protect(this);
789 768
790 // Notify renderer clients that the request is canceled. 769 // Notify renderer clients that the request is canceled.
791 if (ftp_listing_delegate_) { 770 if (ftp_listing_delegate_) {
792 ftp_listing_delegate_->OnCompletedRequest(); 771 ftp_listing_delegate_->OnCompletedRequest();
793 ftp_listing_delegate_.reset(NULL); 772 ftp_listing_delegate_.reset(NULL);
794 } else if (multipart_delegate_) {
795 multipart_delegate_->OnCompletedRequest();
796 multipart_delegate_.reset(NULL);
797 } 773 }
798 774
799 if (body_stream_writer_) { 775 if (body_stream_writer_) {
800 body_stream_writer_->Fail(); 776 body_stream_writer_->Fail();
801 body_stream_writer_.reset(); 777 body_stream_writer_.reset();
802 } 778 }
803 if (client_) { 779 if (client_) {
804 // TODO(yhirano): Set |stale_copy_in_cache| appropriately if possible. 780 // TODO(yhirano): Set |stale_copy_in_cache| appropriately if possible.
805 client_->didFail( 781 client_->didFail(
806 loader_, CreateWebURLError(request_.url(), false, net::ERR_ABORTED)); 782 loader_, CreateWebURLError(request_.url(), false, net::ERR_ABORTED));
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
1196 response->clearHTTPHeaderField(webStringName); 1172 response->clearHTTPHeaderField(webStringName);
1197 while (response_headers->EnumerateHeader(&iterator, name, &value)) { 1173 while (response_headers->EnumerateHeader(&iterator, name, &value)) {
1198 response->addHTTPHeaderField(webStringName, 1174 response->addHTTPHeaderField(webStringName,
1199 WebString::fromLatin1(value)); 1175 WebString::fromLatin1(value));
1200 } 1176 }
1201 } 1177 }
1202 return true; 1178 return true;
1203 } 1179 }
1204 1180
1205 } // namespace content 1181 } // namespace content
OLDNEW
« no previous file with comments | « content/child/multipart_response_delegate_unittest.cc ('k') | content/child/web_url_loader_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698