| 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 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc
e-loading | 5 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc
e-loading |
| 6 | 6 |
| 7 #include "content/child/resource_dispatcher.h" | 7 #include "content/child/resource_dispatcher.h" |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "base/metrics/histogram.h" | 21 #include "base/metrics/histogram.h" |
| 22 #include "base/rand_util.h" | 22 #include "base/rand_util.h" |
| 23 #include "base/strings/string_util.h" | 23 #include "base/strings/string_util.h" |
| 24 #include "build/build_config.h" | 24 #include "build/build_config.h" |
| 25 #include "content/child/request_extra_data.h" | 25 #include "content/child/request_extra_data.h" |
| 26 #include "content/child/request_info.h" | 26 #include "content/child/request_info.h" |
| 27 #include "content/child/resource_scheduling_filter.h" | 27 #include "content/child/resource_scheduling_filter.h" |
| 28 #include "content/child/shared_memory_received_data_factory.h" | 28 #include "content/child/shared_memory_received_data_factory.h" |
| 29 #include "content/child/site_isolation_stats_gatherer.h" | 29 #include "content/child/site_isolation_stats_gatherer.h" |
| 30 #include "content/child/sync_load_response.h" | 30 #include "content/child/sync_load_response.h" |
| 31 #include "content/child/url_response_body_consumer.h" |
| 31 #include "content/common/inter_process_time_ticks_converter.h" | 32 #include "content/common/inter_process_time_ticks_converter.h" |
| 32 #include "content/common/navigation_params.h" | 33 #include "content/common/navigation_params.h" |
| 33 #include "content/common/resource_messages.h" | 34 #include "content/common/resource_messages.h" |
| 34 #include "content/common/resource_request.h" | 35 #include "content/common/resource_request.h" |
| 35 #include "content/common/resource_request_completion_status.h" | 36 #include "content/common/resource_request_completion_status.h" |
| 36 #include "content/public/child/fixed_received_data.h" | 37 #include "content/public/child/fixed_received_data.h" |
| 37 #include "content/public/child/request_peer.h" | 38 #include "content/public/child/request_peer.h" |
| 38 #include "content/public/child/resource_dispatcher_delegate.h" | 39 #include "content/public/child/resource_dispatcher_delegate.h" |
| 39 #include "content/public/common/content_features.h" | 40 #include "content/public/common/content_features.h" |
| 40 #include "content/public/common/resource_response.h" | 41 #include "content/public/common/resource_response.h" |
| 41 #include "content/public/common/resource_type.h" | 42 #include "content/public/common/resource_type.h" |
| 43 #include "mojo/public/cpp/bindings/binding.h" |
| 42 #include "net/base/net_errors.h" | 44 #include "net/base/net_errors.h" |
| 43 #include "net/base/request_priority.h" | 45 #include "net/base/request_priority.h" |
| 44 #include "net/http/http_response_headers.h" | 46 #include "net/http/http_response_headers.h" |
| 45 | 47 |
| 46 namespace content { | 48 namespace content { |
| 47 | 49 |
| 48 namespace { | 50 namespace { |
| 49 | 51 |
| 50 // Converts |time| from a remote to local TimeTicks, overwriting the original | 52 // Converts |time| from a remote to local TimeTicks, overwriting the original |
| 51 // value. | 53 // value. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 66 | 68 |
| 67 // Each resource request is assigned an ID scoped to this process. | 69 // Each resource request is assigned an ID scoped to this process. |
| 68 int MakeRequestID() { | 70 int MakeRequestID() { |
| 69 // NOTE: The resource_dispatcher_host also needs probably unique | 71 // NOTE: The resource_dispatcher_host also needs probably unique |
| 70 // request_ids, so they count down from -2 (-1 is a special we're | 72 // request_ids, so they count down from -2 (-1 is a special we're |
| 71 // screwed value), while the renderer process counts up. | 73 // screwed value), while the renderer process counts up. |
| 72 static int next_request_id = 0; | 74 static int next_request_id = 0; |
| 73 return next_request_id++; | 75 return next_request_id++; |
| 74 } | 76 } |
| 75 | 77 |
| 78 class URLLoaderClientImpl final : public mojom::URLLoaderClient { |
| 79 public: |
| 80 URLLoaderClientImpl(int request_id, ResourceDispatcher* resource_dispatcher) |
| 81 : binding_(this), |
| 82 request_id_(request_id), |
| 83 resource_dispatcher_(resource_dispatcher) {} |
| 84 ~URLLoaderClientImpl() override { |
| 85 if (body_consumer_) |
| 86 body_consumer_->Cancel(); |
| 87 } |
| 88 |
| 89 void OnReceiveResponse(const ResourceResponseHead& response_head) override { |
| 90 resource_dispatcher_->OnMessageReceived( |
| 91 ResourceMsg_ReceivedResponse(request_id_, response_head)); |
| 92 } |
| 93 |
| 94 void OnStartLoadingResponseBody( |
| 95 mojo::ScopedDataPipeConsumerHandle body) override { |
| 96 DCHECK(!body_consumer_); |
| 97 body_consumer_ = new URLResponseBodyConsumer( |
| 98 request_id_, resource_dispatcher_, std::move(body)); |
| 99 } |
| 100 |
| 101 void OnComplete(const ResourceRequestCompletionStatus& status) override { |
| 102 body_consumer_->OnComplete(status); |
| 103 } |
| 104 |
| 105 mojom::URLLoaderClientPtr CreateInterfacePtrAndBind() { |
| 106 return binding_.CreateInterfacePtrAndBind(); |
| 107 } |
| 108 |
| 109 private: |
| 110 mojo::Binding<mojom::URLLoaderClient> binding_; |
| 111 scoped_refptr<URLResponseBodyConsumer> body_consumer_; |
| 112 const int request_id_; |
| 113 ResourceDispatcher* const resource_dispatcher_; |
| 114 }; |
| 115 |
| 76 } // namespace | 116 } // namespace |
| 77 | 117 |
| 78 ResourceDispatcher::ResourceDispatcher( | 118 ResourceDispatcher::ResourceDispatcher( |
| 79 IPC::Sender* sender, | 119 IPC::Sender* sender, |
| 80 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) | 120 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) |
| 81 : message_sender_(sender), | 121 : message_sender_(sender), |
| 82 delegate_(NULL), | 122 delegate_(NULL), |
| 83 io_timestamp_(base::TimeTicks()), | 123 io_timestamp_(base::TimeTicks()), |
| 84 main_thread_task_runner_(main_thread_task_runner), | 124 main_thread_task_runner_(main_thread_task_runner), |
| 85 weak_factory_(this) { | 125 weak_factory_(this) { |
| (...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 537 if (index != pending_requests_.end()) { | 577 if (index != pending_requests_.end()) { |
| 538 PendingRequestInfo* pending_request = index->second.get(); | 578 PendingRequestInfo* pending_request = index->second.get(); |
| 539 if (pending_request->is_deferred) { | 579 if (pending_request->is_deferred) { |
| 540 pending_request->deferred_message_queue.swap(q); | 580 pending_request->deferred_message_queue.swap(q); |
| 541 return; | 581 return; |
| 542 } | 582 } |
| 543 } | 583 } |
| 544 } | 584 } |
| 545 } | 585 } |
| 546 | 586 |
| 547 void ResourceDispatcher::StartSync(const RequestInfo& request_info, | 587 void ResourceDispatcher::StartSync( |
| 548 ResourceRequestBodyImpl* request_body, | 588 const RequestInfo& request_info, |
| 549 SyncLoadResponse* response) { | 589 ResourceRequestBodyImpl* request_body, |
| 590 SyncLoadResponse* response, |
| 591 blink::WebURLRequest::LoadingIPCType ipc_type, |
| 592 mojom::URLLoaderFactory* url_loader_factory) { |
| 550 std::unique_ptr<ResourceRequest> request = | 593 std::unique_ptr<ResourceRequest> request = |
| 551 CreateRequest(request_info, request_body, NULL); | 594 CreateRequest(request_info, request_body, NULL); |
| 595 // TODO(yhirano): Use url_loader_factory otherwise. |
| 596 DCHECK_EQ(blink::WebURLRequest::LoadingIPCType::ChromeIPC, ipc_type); |
| 552 | 597 |
| 553 SyncLoadResult result; | 598 SyncLoadResult result; |
| 554 IPC::SyncMessage* msg = new ResourceHostMsg_SyncLoad( | 599 IPC::SyncMessage* msg = new ResourceHostMsg_SyncLoad( |
| 555 request_info.routing_id, MakeRequestID(), *request, &result); | 600 request_info.routing_id, MakeRequestID(), *request, &result); |
| 556 | 601 |
| 557 // NOTE: This may pump events (see RenderThread::Send). | 602 // NOTE: This may pump events (see RenderThread::Send). |
| 558 if (!message_sender_->Send(msg)) { | 603 if (!message_sender_->Send(msg)) { |
| 559 response->error_code = net::ERR_FAILED; | 604 response->error_code = net::ERR_FAILED; |
| 560 return; | 605 return; |
| 561 } | 606 } |
| 562 | 607 |
| 563 response->error_code = result.error_code; | 608 response->error_code = result.error_code; |
| 564 response->url = result.final_url; | 609 response->url = result.final_url; |
| 565 response->headers = result.headers; | 610 response->headers = result.headers; |
| 566 response->mime_type = result.mime_type; | 611 response->mime_type = result.mime_type; |
| 567 response->charset = result.charset; | 612 response->charset = result.charset; |
| 568 response->request_time = result.request_time; | 613 response->request_time = result.request_time; |
| 569 response->response_time = result.response_time; | 614 response->response_time = result.response_time; |
| 570 response->encoded_data_length = result.encoded_data_length; | 615 response->encoded_data_length = result.encoded_data_length; |
| 571 response->load_timing = result.load_timing; | 616 response->load_timing = result.load_timing; |
| 572 response->devtools_info = result.devtools_info; | 617 response->devtools_info = result.devtools_info; |
| 573 response->data.swap(result.data); | 618 response->data.swap(result.data); |
| 574 response->download_file_path = result.download_file_path; | 619 response->download_file_path = result.download_file_path; |
| 575 response->socket_address = result.socket_address; | 620 response->socket_address = result.socket_address; |
| 576 } | 621 } |
| 577 | 622 |
| 578 int ResourceDispatcher::StartAsync(const RequestInfo& request_info, | 623 int ResourceDispatcher::StartAsync( |
| 579 ResourceRequestBodyImpl* request_body, | 624 const RequestInfo& request_info, |
| 580 std::unique_ptr<RequestPeer> peer) { | 625 ResourceRequestBodyImpl* request_body, |
| 626 std::unique_ptr<RequestPeer> peer, |
| 627 blink::WebURLRequest::LoadingIPCType ipc_type, |
| 628 mojom::URLLoaderFactory* url_loader_factory) { |
| 581 GURL frame_origin; | 629 GURL frame_origin; |
| 582 std::unique_ptr<ResourceRequest> request = | 630 std::unique_ptr<ResourceRequest> request = |
| 583 CreateRequest(request_info, request_body, &frame_origin); | 631 CreateRequest(request_info, request_body, &frame_origin); |
| 584 | 632 |
| 585 // Compute a unique request_id for this renderer process. | 633 // Compute a unique request_id for this renderer process. |
| 586 int request_id = MakeRequestID(); | 634 int request_id = MakeRequestID(); |
| 587 pending_requests_[request_id] = base::WrapUnique(new PendingRequestInfo( | 635 pending_requests_[request_id] = base::WrapUnique(new PendingRequestInfo( |
| 588 std::move(peer), request->resource_type, request->origin_pid, | 636 std::move(peer), request->resource_type, request->origin_pid, |
| 589 frame_origin, request->url, request_info.download_to_file)); | 637 frame_origin, request->url, request_info.download_to_file)); |
| 590 | 638 |
| 591 if (resource_scheduling_filter_.get() && | 639 if (resource_scheduling_filter_.get() && |
| 592 request_info.loading_web_task_runner) { | 640 request_info.loading_web_task_runner) { |
| 593 resource_scheduling_filter_->SetRequestIdTaskRunner( | 641 resource_scheduling_filter_->SetRequestIdTaskRunner( |
| 594 request_id, | 642 request_id, |
| 595 base::WrapUnique(request_info.loading_web_task_runner->clone())); | 643 base::WrapUnique(request_info.loading_web_task_runner->clone())); |
| 596 } | 644 } |
| 597 | 645 |
| 598 message_sender_->Send(new ResourceHostMsg_RequestResource( | 646 if (ipc_type == blink::WebURLRequest::LoadingIPCType::Mojo) { |
| 599 request_info.routing_id, request_id, *request)); | 647 std::unique_ptr<URLLoaderClientImpl> client( |
| 648 new URLLoaderClientImpl(request_id, this)); |
| 649 mojom::URLLoaderPtr url_loader; |
| 650 url_loader_factory->CreateLoaderAndStart( |
| 651 GetProxy(&url_loader), request_id, *request, |
| 652 client->CreateInterfacePtrAndBind()); |
| 653 pending_requests_[request_id]->url_loader = std::move(url_loader); |
| 654 pending_requests_[request_id]->url_loader_client = std::move(client); |
| 655 } else { |
| 656 message_sender_->Send(new ResourceHostMsg_RequestResource( |
| 657 request_info.routing_id, request_id, *request)); |
| 658 } |
| 600 | 659 |
| 601 return request_id; | 660 return request_id; |
| 602 } | 661 } |
| 603 | 662 |
| 604 void ResourceDispatcher::ToResourceResponseInfo( | 663 void ResourceDispatcher::ToResourceResponseInfo( |
| 605 const PendingRequestInfo& request_info, | 664 const PendingRequestInfo& request_info, |
| 606 const ResourceResponseHead& browser_info, | 665 const ResourceResponseHead& browser_info, |
| 607 ResourceResponseInfo* renderer_info) const { | 666 ResourceResponseInfo* renderer_info) const { |
| 608 *renderer_info = browser_info; | 667 *renderer_info = browser_info; |
| 609 if (request_info.request_start.is_null() || | 668 if (request_info.request_start.is_null() || |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 810 *frame_origin = extra_data->frame_origin(); | 869 *frame_origin = extra_data->frame_origin(); |
| 811 return request; | 870 return request; |
| 812 } | 871 } |
| 813 | 872 |
| 814 void ResourceDispatcher::SetResourceSchedulingFilter( | 873 void ResourceDispatcher::SetResourceSchedulingFilter( |
| 815 scoped_refptr<ResourceSchedulingFilter> resource_scheduling_filter) { | 874 scoped_refptr<ResourceSchedulingFilter> resource_scheduling_filter) { |
| 816 resource_scheduling_filter_ = resource_scheduling_filter; | 875 resource_scheduling_filter_ = resource_scheduling_filter; |
| 817 } | 876 } |
| 818 | 877 |
| 819 } // namespace content | 878 } // namespace content |
| OLD | NEW |