| 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 "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/debug/alias.h" | 12 #include "base/debug/alias.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/memory/shared_memory.h" | 14 #include "base/memory/shared_memory.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "content/child/request_extra_data.h" | 18 #include "content/child/request_extra_data.h" |
| 19 #include "content/child/request_info.h" | 19 #include "content/child/request_info.h" |
| 20 #include "content/child/site_isolation_policy.h" | 20 #include "content/child/site_isolation_policy.h" |
| 21 #include "content/child/sync_load_response.h" | 21 #include "content/child/sync_load_response.h" |
| 22 #include "content/child/threaded_data_provider.h" |
| 22 #include "content/common/inter_process_time_ticks_converter.h" | 23 #include "content/common/inter_process_time_ticks_converter.h" |
| 23 #include "content/common/resource_messages.h" | 24 #include "content/common/resource_messages.h" |
| 24 #include "content/public/child/request_peer.h" | 25 #include "content/public/child/request_peer.h" |
| 25 #include "content/public/child/resource_dispatcher_delegate.h" | 26 #include "content/public/child/resource_dispatcher_delegate.h" |
| 26 #include "content/public/common/resource_response.h" | 27 #include "content/public/common/resource_response.h" |
| 27 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 28 #include "net/base/net_util.h" | 29 #include "net/base/net_util.h" |
| 29 #include "net/base/request_priority.h" | 30 #include "net/base/request_priority.h" |
| 30 #include "net/http/http_response_headers.h" | 31 #include "net/http/http_response_headers.h" |
| 31 #include "webkit/child/resource_loader_bridge.h" | 32 #include "webkit/child/resource_loader_bridge.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 const RequestInfo& request_info); | 74 const RequestInfo& request_info); |
| 74 virtual ~IPCResourceLoaderBridge(); | 75 virtual ~IPCResourceLoaderBridge(); |
| 75 | 76 |
| 76 // ResourceLoaderBridge | 77 // ResourceLoaderBridge |
| 77 virtual void SetRequestBody(ResourceRequestBody* request_body) OVERRIDE; | 78 virtual void SetRequestBody(ResourceRequestBody* request_body) OVERRIDE; |
| 78 virtual bool Start(RequestPeer* peer) OVERRIDE; | 79 virtual bool Start(RequestPeer* peer) OVERRIDE; |
| 79 virtual void Cancel() OVERRIDE; | 80 virtual void Cancel() OVERRIDE; |
| 80 virtual void SetDefersLoading(bool value) OVERRIDE; | 81 virtual void SetDefersLoading(bool value) OVERRIDE; |
| 81 virtual void DidChangePriority(net::RequestPriority new_priority, | 82 virtual void DidChangePriority(net::RequestPriority new_priority, |
| 82 int intra_priority_value) OVERRIDE; | 83 int intra_priority_value) OVERRIDE; |
| 84 virtual bool AttachThreadedDataReceiver( |
| 85 blink::WebThreadedDataReceiver* threaded_data_receiver) OVERRIDE; |
| 83 virtual void SyncLoad(SyncLoadResponse* response) OVERRIDE; | 86 virtual void SyncLoad(SyncLoadResponse* response) OVERRIDE; |
| 84 | 87 |
| 85 private: | 88 private: |
| 86 // The resource dispatcher for this loader. The bridge doesn't own it, but | 89 // The resource dispatcher for this loader. The bridge doesn't own it, but |
| 87 // it's guaranteed to outlive the bridge. | 90 // it's guaranteed to outlive the bridge. |
| 88 ResourceDispatcher* dispatcher_; | 91 ResourceDispatcher* dispatcher_; |
| 89 | 92 |
| 90 // The request to send, created on initialization for modification and | 93 // The request to send, created on initialization for modification and |
| 91 // appending data. | 94 // appending data. |
| 92 ResourceHostMsg_Request request_; | 95 ResourceHostMsg_Request request_; |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 int intra_priority_value) { | 216 int intra_priority_value) { |
| 214 if (request_id_ < 0) { | 217 if (request_id_ < 0) { |
| 215 NOTREACHED() << "Trying to change priority of an unstarted request"; | 218 NOTREACHED() << "Trying to change priority of an unstarted request"; |
| 216 return; | 219 return; |
| 217 } | 220 } |
| 218 | 221 |
| 219 dispatcher_->DidChangePriority( | 222 dispatcher_->DidChangePriority( |
| 220 request_id_, new_priority, intra_priority_value); | 223 request_id_, new_priority, intra_priority_value); |
| 221 } | 224 } |
| 222 | 225 |
| 226 bool IPCResourceLoaderBridge::AttachThreadedDataReceiver( |
| 227 blink::WebThreadedDataReceiver* threaded_data_receiver) { |
| 228 if (request_id_ < 0) { |
| 229 NOTREACHED() << "Trying to attach threaded receiver on unstarted request"; |
| 230 return false; |
| 231 } |
| 232 |
| 233 return dispatcher_->AttachThreadedDataReceiver(request_id_, |
| 234 threaded_data_receiver); |
| 235 } |
| 236 |
| 223 void IPCResourceLoaderBridge::SyncLoad(SyncLoadResponse* response) { | 237 void IPCResourceLoaderBridge::SyncLoad(SyncLoadResponse* response) { |
| 224 if (request_id_ != -1) { | 238 if (request_id_ != -1) { |
| 225 NOTREACHED() << "Starting a request twice"; | 239 NOTREACHED() << "Starting a request twice"; |
| 226 response->error_code = net::ERR_FAILED; | 240 response->error_code = net::ERR_FAILED; |
| 227 return; | 241 return; |
| 228 } | 242 } |
| 229 | 243 |
| 230 request_id_ = MakeRequestID(); | 244 request_id_ = MakeRequestID(); |
| 231 is_synchronous_request_ = true; | 245 is_synchronous_request_ = true; |
| 232 | 246 |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 request_info->buffer_size = shm_size; | 412 request_info->buffer_size = shm_size; |
| 399 } | 413 } |
| 400 | 414 |
| 401 void ResourceDispatcher::OnReceivedData(int request_id, | 415 void ResourceDispatcher::OnReceivedData(int request_id, |
| 402 int data_offset, | 416 int data_offset, |
| 403 int data_length, | 417 int data_length, |
| 404 int encoded_data_length) { | 418 int encoded_data_length) { |
| 405 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedData"); | 419 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedData"); |
| 406 DCHECK_GT(data_length, 0); | 420 DCHECK_GT(data_length, 0); |
| 407 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 421 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
| 422 bool send_ack = true; |
| 408 if (request_info && data_length > 0) { | 423 if (request_info && data_length > 0) { |
| 409 CHECK(base::SharedMemory::IsHandleValid(request_info->buffer->handle())); | 424 CHECK(base::SharedMemory::IsHandleValid(request_info->buffer->handle())); |
| 410 CHECK_GE(request_info->buffer_size, data_offset + data_length); | 425 CHECK_GE(request_info->buffer_size, data_offset + data_length); |
| 411 | 426 |
| 412 // Ensure that the SHM buffer remains valid for the duration of this scope. | 427 // Ensure that the SHM buffer remains valid for the duration of this scope. |
| 413 // It is possible for CancelPendingRequest() to be called before we exit | 428 // It is possible for CancelPendingRequest() to be called before we exit |
| 414 // this scope. | 429 // this scope. |
| 415 linked_ptr<base::SharedMemory> retain_buffer(request_info->buffer); | 430 linked_ptr<base::SharedMemory> retain_buffer(request_info->buffer); |
| 416 | 431 |
| 417 base::TimeTicks time_start = base::TimeTicks::Now(); | 432 base::TimeTicks time_start = base::TimeTicks::Now(); |
| 418 | 433 |
| 419 const char* data_ptr = static_cast<char*>(request_info->buffer->memory()); | 434 const char* data_start = static_cast<char*>(request_info->buffer->memory()); |
| 420 CHECK(data_ptr); | 435 CHECK(data_start); |
| 421 CHECK(data_ptr + data_offset); | 436 CHECK(data_start + data_offset); |
| 437 const char* data_ptr = data_start + data_offset; |
| 422 | 438 |
| 423 // Check whether this response data is compliant with our cross-site | 439 // Check whether this response data is compliant with our cross-site |
| 424 // document blocking policy. We only do this for the first packet. | 440 // document blocking policy. We only do this for the first packet. |
| 425 std::string alternative_data; | 441 std::string alternative_data; |
| 426 if (request_info->site_isolation_metadata.get()) { | 442 if (request_info->site_isolation_metadata.get()) { |
| 427 request_info->blocked_response = | 443 request_info->blocked_response = |
| 428 SiteIsolationPolicy::ShouldBlockResponse( | 444 SiteIsolationPolicy::ShouldBlockResponse( |
| 429 request_info->site_isolation_metadata, data_ptr + data_offset, | 445 request_info->site_isolation_metadata, data_ptr, data_length, |
| 430 data_length, &alternative_data); | 446 &alternative_data); |
| 431 request_info->site_isolation_metadata.reset(); | 447 request_info->site_isolation_metadata.reset(); |
| 448 |
| 449 // When the response is blocked we may have any alternative data to |
| 450 // send to the renderer. When |alternative_data| is zero-sized, we do not |
| 451 // call peer's callback. |
| 452 if (request_info->blocked_response && !alternative_data.empty()) { |
| 453 data_ptr = alternative_data.data(); |
| 454 data_length = alternative_data.size(); |
| 455 encoded_data_length = alternative_data.size(); |
| 456 } |
| 432 } | 457 } |
| 433 | 458 |
| 434 // When the response is not blocked. | 459 if (!request_info->blocked_response || !alternative_data.empty()) { |
| 435 if (!request_info->blocked_response) { | 460 if (request_info->threaded_data_provider) { |
| 436 request_info->peer->OnReceivedData( | 461 request_info->threaded_data_provider->OnReceivedDataOnForegroundThread( |
| 437 data_ptr + data_offset, data_length, encoded_data_length); | 462 data_ptr, data_length, encoded_data_length); |
| 438 } else if (alternative_data.size() > 0) { | 463 // A threaded data provider will take care of its own ACKing, as the |
| 439 // When the response is blocked, and when we have any alternative data to | 464 // data may be processed later on another thread. |
| 440 // send to the renderer. When |alternative_data| is zero-sized, we do not | 465 send_ack = false; |
| 441 // call peer's callback. | 466 } else { |
| 442 request_info->peer->OnReceivedData(alternative_data.data(), | 467 request_info->peer->OnReceivedData( |
| 443 alternative_data.size(), | 468 data_ptr, data_length, encoded_data_length); |
| 444 alternative_data.size()); | 469 } |
| 445 } | 470 } |
| 446 | 471 |
| 447 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime", | 472 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime", |
| 448 base::TimeTicks::Now() - time_start); | 473 base::TimeTicks::Now() - time_start); |
| 449 } | 474 } |
| 450 | 475 |
| 451 // Acknowledge the reception of this data. | 476 // Acknowledge the reception of this data. |
| 452 message_sender_->Send(new ResourceHostMsg_DataReceived_ACK(request_id)); | 477 if (send_ack) |
| 478 message_sender_->Send(new ResourceHostMsg_DataReceived_ACK(request_id)); |
| 453 } | 479 } |
| 454 | 480 |
| 455 void ResourceDispatcher::OnDownloadedData(int request_id, | 481 void ResourceDispatcher::OnDownloadedData(int request_id, |
| 456 int data_len, | 482 int data_len, |
| 457 int encoded_data_length) { | 483 int encoded_data_length) { |
| 458 // Acknowledge the reception of this message. | 484 // Acknowledge the reception of this message. |
| 459 message_sender_->Send(new ResourceHostMsg_DataDownloaded_ACK(request_id)); | 485 message_sender_->Send(new ResourceHostMsg_DataDownloaded_ACK(request_id)); |
| 460 | 486 |
| 461 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 487 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
| 462 if (!request_info) | 488 if (!request_info) |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 616 } | 642 } |
| 617 | 643 |
| 618 void ResourceDispatcher::DidChangePriority(int request_id, | 644 void ResourceDispatcher::DidChangePriority(int request_id, |
| 619 net::RequestPriority new_priority, | 645 net::RequestPriority new_priority, |
| 620 int intra_priority_value) { | 646 int intra_priority_value) { |
| 621 DCHECK(ContainsKey(pending_requests_, request_id)); | 647 DCHECK(ContainsKey(pending_requests_, request_id)); |
| 622 message_sender_->Send(new ResourceHostMsg_DidChangePriority( | 648 message_sender_->Send(new ResourceHostMsg_DidChangePriority( |
| 623 request_id, new_priority, intra_priority_value)); | 649 request_id, new_priority, intra_priority_value)); |
| 624 } | 650 } |
| 625 | 651 |
| 652 bool ResourceDispatcher::AttachThreadedDataReceiver( |
| 653 int request_id, blink::WebThreadedDataReceiver* threaded_data_receiver) { |
| 654 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
| 655 DCHECK(request_info); |
| 656 DCHECK(!request_info->threaded_data_provider); |
| 657 request_info->threaded_data_provider = new ThreadedDataProvider( |
| 658 request_id, threaded_data_receiver, request_info->buffer, |
| 659 request_info->buffer_size); |
| 660 |
| 661 return true; |
| 662 } |
| 663 |
| 626 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo() | 664 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo() |
| 627 : peer(NULL), | 665 : peer(NULL), |
| 666 threaded_data_provider(NULL), |
| 628 resource_type(ResourceType::SUB_RESOURCE), | 667 resource_type(ResourceType::SUB_RESOURCE), |
| 629 is_deferred(false), | 668 is_deferred(false), |
| 630 download_to_file(false), | 669 download_to_file(false), |
| 631 blocked_response(false), | 670 blocked_response(false), |
| 632 buffer_size(0) { | 671 buffer_size(0) { |
| 633 } | 672 } |
| 634 | 673 |
| 635 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo( | 674 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo( |
| 636 RequestPeer* peer, | 675 RequestPeer* peer, |
| 637 ResourceType::Type resource_type, | 676 ResourceType::Type resource_type, |
| 638 int origin_pid, | 677 int origin_pid, |
| 639 const GURL& frame_origin, | 678 const GURL& frame_origin, |
| 640 const GURL& request_url, | 679 const GURL& request_url, |
| 641 bool download_to_file) | 680 bool download_to_file) |
| 642 : peer(peer), | 681 : peer(peer), |
| 682 threaded_data_provider(NULL), |
| 643 resource_type(resource_type), | 683 resource_type(resource_type), |
| 644 origin_pid(origin_pid), | 684 origin_pid(origin_pid), |
| 645 is_deferred(false), | 685 is_deferred(false), |
| 646 url(request_url), | 686 url(request_url), |
| 647 frame_origin(frame_origin), | 687 frame_origin(frame_origin), |
| 648 response_url(request_url), | 688 response_url(request_url), |
| 649 download_to_file(download_to_file), | 689 download_to_file(download_to_file), |
| 650 request_start(base::TimeTicks::Now()), | 690 request_start(base::TimeTicks::Now()), |
| 651 blocked_response(false) {} | 691 blocked_response(false) {} |
| 652 | 692 |
| 653 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() {} | 693 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() { |
| 694 if (threaded_data_provider) |
| 695 threaded_data_provider->Stop(); |
| 696 } |
| 654 | 697 |
| 655 void ResourceDispatcher::DispatchMessage(const IPC::Message& message) { | 698 void ResourceDispatcher::DispatchMessage(const IPC::Message& message) { |
| 656 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher, message) | 699 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher, message) |
| 657 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress, OnUploadProgress) | 700 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress, OnUploadProgress) |
| 658 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse, OnReceivedResponse) | 701 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse, OnReceivedResponse) |
| 659 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata, | 702 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata, |
| 660 OnReceivedCachedMetadata) | 703 OnReceivedCachedMetadata) |
| 661 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedRedirect, OnReceivedRedirect) | 704 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedRedirect, OnReceivedRedirect) |
| 662 IPC_MESSAGE_HANDLER(ResourceMsg_SetDataBuffer, OnSetDataBuffer) | 705 IPC_MESSAGE_HANDLER(ResourceMsg_SetDataBuffer, OnSetDataBuffer) |
| 663 IPC_MESSAGE_HANDLER(ResourceMsg_DataReceived, OnReceivedData) | 706 IPC_MESSAGE_HANDLER(ResourceMsg_DataReceived, OnReceivedData) |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 808 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue* queue) { | 851 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue* queue) { |
| 809 while (!queue->empty()) { | 852 while (!queue->empty()) { |
| 810 IPC::Message* message = queue->front(); | 853 IPC::Message* message = queue->front(); |
| 811 ReleaseResourcesInDataMessage(*message); | 854 ReleaseResourcesInDataMessage(*message); |
| 812 queue->pop_front(); | 855 queue->pop_front(); |
| 813 delete message; | 856 delete message; |
| 814 } | 857 } |
| 815 } | 858 } |
| 816 | 859 |
| 817 } // namespace content | 860 } // namespace content |
| OLD | NEW |