| 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, |
| 90 mojo::ScopedDataPipeConsumerHandle body) override { |
| 91 body_consumer_ = new URLResponseBodyConsumer( |
| 92 request_id_, resource_dispatcher_, std::move(body)); |
| 93 |
| 94 resource_dispatcher_->OnMessageReceived( |
| 95 ResourceMsg_ReceivedResponse(request_id_, response_head)); |
| 96 } |
| 97 void OnComplete(const ResourceRequestCompletionStatus& status) override { |
| 98 body_consumer_->OnComplete(status); |
| 99 } |
| 100 |
| 101 mojom::URLLoaderClientPtr CreateInterfacePtrAndBind() { |
| 102 return binding_.CreateInterfacePtrAndBind(); |
| 103 } |
| 104 |
| 105 private: |
| 106 mojo::Binding<mojom::URLLoaderClient> binding_; |
| 107 scoped_refptr<URLResponseBodyConsumer> body_consumer_; |
| 108 const int request_id_; |
| 109 ResourceDispatcher* const resource_dispatcher_; |
| 110 }; |
| 111 |
| 76 } // namespace | 112 } // namespace |
| 77 | 113 |
| 78 ResourceDispatcher::ResourceDispatcher( | 114 ResourceDispatcher::ResourceDispatcher( |
| 79 IPC::Sender* sender, | 115 IPC::Sender* sender, |
| 80 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) | 116 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) |
| 81 : message_sender_(sender), | 117 : message_sender_(sender), |
| 82 delegate_(NULL), | 118 delegate_(NULL), |
| 83 io_timestamp_(base::TimeTicks()), | 119 io_timestamp_(base::TimeTicks()), |
| 84 main_thread_task_runner_(main_thread_task_runner), | 120 main_thread_task_runner_(main_thread_task_runner), |
| 85 weak_factory_(this) { | 121 weak_factory_(this) { |
| (...skipping 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 if (pending_request->is_deferred) { | 572 if (pending_request->is_deferred) { |
| 537 pending_request->deferred_message_queue.swap(q); | 573 pending_request->deferred_message_queue.swap(q); |
| 538 return; | 574 return; |
| 539 } | 575 } |
| 540 } | 576 } |
| 541 } | 577 } |
| 542 } | 578 } |
| 543 | 579 |
| 544 void ResourceDispatcher::StartSync(const RequestInfo& request_info, | 580 void ResourceDispatcher::StartSync(const RequestInfo& request_info, |
| 545 ResourceRequestBody* request_body, | 581 ResourceRequestBody* request_body, |
| 546 SyncLoadResponse* response) { | 582 SyncLoadResponse* response, |
| 583 mojom::URLLoaderPtr url_loader) { |
| 547 std::unique_ptr<ResourceRequest> request = | 584 std::unique_ptr<ResourceRequest> request = |
| 548 CreateRequest(request_info, request_body, NULL); | 585 CreateRequest(request_info, request_body, NULL); |
| 549 | 586 |
| 550 SyncLoadResult result; | 587 SyncLoadResult result; |
| 551 IPC::SyncMessage* msg = new ResourceHostMsg_SyncLoad( | 588 IPC::SyncMessage* msg = new ResourceHostMsg_SyncLoad( |
| 552 request_info.routing_id, MakeRequestID(), *request, &result); | 589 request_info.routing_id, MakeRequestID(), *request, &result); |
| 553 | 590 |
| 554 // NOTE: This may pump events (see RenderThread::Send). | 591 // NOTE: This may pump events (see RenderThread::Send). |
| 555 if (!message_sender_->Send(msg)) { | 592 if (!message_sender_->Send(msg)) { |
| 556 response->error_code = net::ERR_FAILED; | 593 response->error_code = net::ERR_FAILED; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 567 response->encoded_data_length = result.encoded_data_length; | 604 response->encoded_data_length = result.encoded_data_length; |
| 568 response->load_timing = result.load_timing; | 605 response->load_timing = result.load_timing; |
| 569 response->devtools_info = result.devtools_info; | 606 response->devtools_info = result.devtools_info; |
| 570 response->data.swap(result.data); | 607 response->data.swap(result.data); |
| 571 response->download_file_path = result.download_file_path; | 608 response->download_file_path = result.download_file_path; |
| 572 response->socket_address = result.socket_address; | 609 response->socket_address = result.socket_address; |
| 573 } | 610 } |
| 574 | 611 |
| 575 int ResourceDispatcher::StartAsync(const RequestInfo& request_info, | 612 int ResourceDispatcher::StartAsync(const RequestInfo& request_info, |
| 576 ResourceRequestBody* request_body, | 613 ResourceRequestBody* request_body, |
| 577 std::unique_ptr<RequestPeer> peer) { | 614 std::unique_ptr<RequestPeer> peer, |
| 615 mojom::URLLoaderPtr url_loader) { |
| 578 GURL frame_origin; | 616 GURL frame_origin; |
| 579 std::unique_ptr<ResourceRequest> request = | 617 std::unique_ptr<ResourceRequest> request = |
| 580 CreateRequest(request_info, request_body, &frame_origin); | 618 CreateRequest(request_info, request_body, &frame_origin); |
| 581 | 619 |
| 582 // Compute a unique request_id for this renderer process. | 620 // Compute a unique request_id for this renderer process. |
| 583 int request_id = MakeRequestID(); | 621 int request_id = MakeRequestID(); |
| 584 pending_requests_[request_id] = base::WrapUnique(new PendingRequestInfo( | 622 pending_requests_[request_id] = base::WrapUnique(new PendingRequestInfo( |
| 585 std::move(peer), request->resource_type, request->origin_pid, | 623 std::move(peer), request->resource_type, request->origin_pid, |
| 586 frame_origin, request->url, request_info.download_to_file)); | 624 frame_origin, request->url, request_info.download_to_file)); |
| 587 | 625 |
| 588 if (resource_scheduling_filter_.get() && | 626 if (resource_scheduling_filter_.get() && |
| 589 request_info.loading_web_task_runner) { | 627 request_info.loading_web_task_runner) { |
| 590 resource_scheduling_filter_->SetRequestIdTaskRunner( | 628 resource_scheduling_filter_->SetRequestIdTaskRunner( |
| 591 request_id, | 629 request_id, |
| 592 base::WrapUnique(request_info.loading_web_task_runner->clone())); | 630 base::WrapUnique(request_info.loading_web_task_runner->clone())); |
| 593 } | 631 } |
| 594 | 632 |
| 595 message_sender_->Send(new ResourceHostMsg_RequestResource( | 633 if (url_loader) { |
| 596 request_info.routing_id, request_id, *request)); | 634 std::unique_ptr<URLLoaderClientImpl> client( |
| 635 new URLLoaderClientImpl(request_id, this)); |
| 636 url_loader->Load(request_id, request_info.routing_id, *request, |
| 637 client->CreateInterfacePtrAndBind()); |
| 638 pending_requests_[request_id]->url_loader = std::move(url_loader); |
| 639 pending_requests_[request_id]->url_loader_client = std::move(client); |
| 640 } else { |
| 641 message_sender_->Send(new ResourceHostMsg_RequestResource( |
| 642 request_info.routing_id, request_id, *request)); |
| 643 } |
| 597 | 644 |
| 598 return request_id; | 645 return request_id; |
| 599 } | 646 } |
| 600 | 647 |
| 601 void ResourceDispatcher::ToResourceResponseInfo( | 648 void ResourceDispatcher::ToResourceResponseInfo( |
| 602 const PendingRequestInfo& request_info, | 649 const PendingRequestInfo& request_info, |
| 603 const ResourceResponseHead& browser_info, | 650 const ResourceResponseHead& browser_info, |
| 604 ResourceResponseInfo* renderer_info) const { | 651 ResourceResponseInfo* renderer_info) const { |
| 605 *renderer_info = browser_info; | 652 *renderer_info = browser_info; |
| 606 if (request_info.request_start.is_null() || | 653 if (request_info.request_start.is_null() || |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 805 *frame_origin = extra_data->frame_origin(); | 852 *frame_origin = extra_data->frame_origin(); |
| 806 return request; | 853 return request; |
| 807 } | 854 } |
| 808 | 855 |
| 809 void ResourceDispatcher::SetResourceSchedulingFilter( | 856 void ResourceDispatcher::SetResourceSchedulingFilter( |
| 810 scoped_refptr<ResourceSchedulingFilter> resource_scheduling_filter) { | 857 scoped_refptr<ResourceSchedulingFilter> resource_scheduling_filter) { |
| 811 resource_scheduling_filter_ = resource_scheduling_filter; | 858 resource_scheduling_filter_ = resource_scheduling_filter; |
| 812 } | 859 } |
| 813 | 860 |
| 814 } // namespace content | 861 } // namespace content |
| OLD | NEW |