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/site_isolation_policy.h" | 19 #include "content/child/site_isolation_policy.h" |
| 20 #include "content/child/threadeddataprovider.h" |
20 #include "content/common/inter_process_time_ticks_converter.h" | 21 #include "content/common/inter_process_time_ticks_converter.h" |
21 #include "content/common/resource_messages.h" | 22 #include "content/common/resource_messages.h" |
22 #include "content/common/service_worker/service_worker_types.h" | 23 #include "content/common/service_worker/service_worker_types.h" |
23 #include "content/public/child/resource_dispatcher_delegate.h" | 24 #include "content/public/child/resource_dispatcher_delegate.h" |
24 #include "content/public/common/resource_response.h" | 25 #include "content/public/common/resource_response.h" |
25 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
26 #include "net/base/net_util.h" | 27 #include "net/base/net_util.h" |
27 #include "net/base/request_priority.h" | 28 #include "net/base/request_priority.h" |
28 #include "net/http/http_response_headers.h" | 29 #include "net/http/http_response_headers.h" |
29 #include "webkit/common/resource_type.h" | 30 #include "webkit/common/resource_type.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 IPCResourceLoaderBridge(ResourceDispatcher* dispatcher, | 72 IPCResourceLoaderBridge(ResourceDispatcher* dispatcher, |
72 const ResourceLoaderBridge::RequestInfo& request_info); | 73 const ResourceLoaderBridge::RequestInfo& request_info); |
73 virtual ~IPCResourceLoaderBridge(); | 74 virtual ~IPCResourceLoaderBridge(); |
74 | 75 |
75 // ResourceLoaderBridge | 76 // ResourceLoaderBridge |
76 virtual void SetRequestBody(ResourceRequestBody* request_body) OVERRIDE; | 77 virtual void SetRequestBody(ResourceRequestBody* request_body) OVERRIDE; |
77 virtual bool Start(Peer* peer) OVERRIDE; | 78 virtual bool Start(Peer* peer) OVERRIDE; |
78 virtual void Cancel() OVERRIDE; | 79 virtual void Cancel() OVERRIDE; |
79 virtual void SetDefersLoading(bool value) OVERRIDE; | 80 virtual void SetDefersLoading(bool value) OVERRIDE; |
80 virtual void DidChangePriority(net::RequestPriority new_priority) OVERRIDE; | 81 virtual void DidChangePriority(net::RequestPriority new_priority) OVERRIDE; |
| 82 virtual bool AttachThreadedDataReceiver( |
| 83 blink::WebThreadedDataReceiver* threadedDataReceiver) OVERRIDE; |
81 virtual void SyncLoad(SyncLoadResponse* response) OVERRIDE; | 84 virtual void SyncLoad(SyncLoadResponse* response) OVERRIDE; |
82 | 85 |
83 private: | 86 private: |
84 ResourceLoaderBridge::Peer* peer_; | 87 ResourceLoaderBridge::Peer* peer_; |
85 | 88 |
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 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 void IPCResourceLoaderBridge::DidChangePriority( | 230 void IPCResourceLoaderBridge::DidChangePriority( |
228 net::RequestPriority new_priority) { | 231 net::RequestPriority new_priority) { |
229 if (request_id_ < 0) { | 232 if (request_id_ < 0) { |
230 NOTREACHED() << "Trying to change priority of an unstarted request"; | 233 NOTREACHED() << "Trying to change priority of an unstarted request"; |
231 return; | 234 return; |
232 } | 235 } |
233 | 236 |
234 dispatcher_->DidChangePriority(routing_id_, request_id_, new_priority); | 237 dispatcher_->DidChangePriority(routing_id_, request_id_, new_priority); |
235 } | 238 } |
236 | 239 |
| 240 bool IPCResourceLoaderBridge::AttachThreadedDataReceiver( |
| 241 blink::WebThreadedDataReceiver* threadedDataReceiver) { |
| 242 if (request_id_ < 0) { |
| 243 NOTREACHED() << "Trying to attach threaded receiver on unstarted request"; |
| 244 return false; |
| 245 } |
| 246 |
| 247 return dispatcher_->AttachThreadedDataReceiver(request_id_, |
| 248 threadedDataReceiver); |
| 249 } |
| 250 |
237 void IPCResourceLoaderBridge::SyncLoad(SyncLoadResponse* response) { | 251 void IPCResourceLoaderBridge::SyncLoad(SyncLoadResponse* response) { |
238 if (request_id_ != -1) { | 252 if (request_id_ != -1) { |
239 NOTREACHED() << "Starting a request twice"; | 253 NOTREACHED() << "Starting a request twice"; |
240 response->error_code = net::ERR_FAILED; | 254 response->error_code = net::ERR_FAILED; |
241 return; | 255 return; |
242 } | 256 } |
243 | 257 |
244 request_id_ = MakeRequestID(); | 258 request_id_ = MakeRequestID(); |
245 is_synchronous_request_ = true; | 259 is_synchronous_request_ = true; |
246 | 260 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 request_info->buffer_size = shm_size; | 426 request_info->buffer_size = shm_size; |
413 } | 427 } |
414 | 428 |
415 void ResourceDispatcher::OnReceivedData(int request_id, | 429 void ResourceDispatcher::OnReceivedData(int request_id, |
416 int data_offset, | 430 int data_offset, |
417 int data_length, | 431 int data_length, |
418 int encoded_data_length) { | 432 int encoded_data_length) { |
419 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedData"); | 433 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedData"); |
420 DCHECK_GT(data_length, 0); | 434 DCHECK_GT(data_length, 0); |
421 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 435 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
| 436 bool send_ack = true; |
422 if (request_info && data_length > 0) { | 437 if (request_info && data_length > 0) { |
423 CHECK(base::SharedMemory::IsHandleValid(request_info->buffer->handle())); | 438 CHECK(base::SharedMemory::IsHandleValid(request_info->buffer->handle())); |
424 CHECK_GE(request_info->buffer_size, data_offset + data_length); | 439 CHECK_GE(request_info->buffer_size, data_offset + data_length); |
425 | 440 |
426 // Ensure that the SHM buffer remains valid for the duration of this scope. | 441 // Ensure that the SHM buffer remains valid for the duration of this scope. |
427 // It is possible for CancelPendingRequest() to be called before we exit | 442 // It is possible for CancelPendingRequest() to be called before we exit |
428 // this scope. | 443 // this scope. |
429 linked_ptr<base::SharedMemory> retain_buffer(request_info->buffer); | 444 linked_ptr<base::SharedMemory> retain_buffer(request_info->buffer); |
430 | 445 |
431 base::TimeTicks time_start = base::TimeTicks::Now(); | 446 base::TimeTicks time_start = base::TimeTicks::Now(); |
432 | 447 |
433 const char* data_ptr = static_cast<char*>(request_info->buffer->memory()); | 448 const char* data_start = static_cast<char*>(request_info->buffer->memory()); |
434 CHECK(data_ptr); | 449 CHECK(data_start); |
435 CHECK(data_ptr + data_offset); | 450 CHECK(data_start + data_offset); |
| 451 const char* data_ptr = data_start + data_offset; |
436 | 452 |
437 // Check whether this response data is compliant with our cross-site | 453 // Check whether this response data is compliant with our cross-site |
438 // document blocking policy. We only do this for the first packet. | 454 // document blocking policy. We only do this for the first packet. |
439 std::string alternative_data; | 455 std::string alternative_data; |
440 if (request_info->site_isolation_metadata.get()) { | 456 if (request_info->site_isolation_metadata.get()) { |
441 request_info->blocked_response = | 457 request_info->blocked_response = |
442 SiteIsolationPolicy::ShouldBlockResponse( | 458 SiteIsolationPolicy::ShouldBlockResponse( |
443 request_info->site_isolation_metadata, data_ptr + data_offset, | 459 request_info->site_isolation_metadata, data_ptr, data_length, |
444 data_length, &alternative_data); | 460 &alternative_data); |
445 request_info->site_isolation_metadata.reset(); | 461 request_info->site_isolation_metadata.reset(); |
| 462 |
| 463 // When the response is blocked we may have any alternative data to |
| 464 // send to the renderer. When |alternative_data| is zero-sized, we do not |
| 465 // call peer's callback. |
| 466 if (request_info->blocked_response && alternative_data.size() > 0) { |
| 467 data_ptr = alternative_data.data(); |
| 468 data_length = alternative_data.size(); |
| 469 encoded_data_length = alternative_data.size(); |
| 470 } |
446 } | 471 } |
447 | 472 |
448 // When the response is not blocked. | 473 if (!request_info->blocked_response || alternative_data.size() > 0) { |
449 if (!request_info->blocked_response) { | 474 if (request_info->threaded_data_provider) { |
450 request_info->peer->OnReceivedData( | 475 request_info->threaded_data_provider->OnReceivedDataOnForegroundThread( |
451 data_ptr + data_offset, data_length, encoded_data_length); | 476 data_ptr, data_length, encoded_data_length); |
452 } else if (alternative_data.size() > 0) { | 477 send_ack = false; |
453 // When the response is blocked, and when we have any alternative data to | 478 } else { |
454 // send to the renderer. When |alternative_data| is zero-sized, we do not | 479 request_info->peer->OnReceivedData( |
455 // call peer's callback. | 480 data_ptr, data_length, encoded_data_length); |
456 request_info->peer->OnReceivedData(alternative_data.data(), | 481 } |
457 alternative_data.size(), | |
458 alternative_data.size()); | |
459 } | 482 } |
460 | 483 |
461 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime", | 484 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime", |
462 base::TimeTicks::Now() - time_start); | 485 base::TimeTicks::Now() - time_start); |
463 } | 486 } |
464 | 487 |
465 // Acknowledge the reception of this data. | 488 // Acknowledge the reception of this data. |
466 message_sender()->Send(new ResourceHostMsg_DataReceived_ACK(request_id)); | 489 if (send_ack) |
| 490 message_sender()->Send(new ResourceHostMsg_DataReceived_ACK(request_id)); |
467 } | 491 } |
468 | 492 |
469 void ResourceDispatcher::OnDownloadedData(int request_id, | 493 void ResourceDispatcher::OnDownloadedData(int request_id, |
470 int data_len, | 494 int data_len, |
471 int encoded_data_length) { | 495 int encoded_data_length) { |
472 // Acknowledge the reception of this message. | 496 // Acknowledge the reception of this message. |
473 message_sender()->Send( | 497 message_sender()->Send( |
474 new ResourceHostMsg_DataDownloaded_ACK(request_id)); | 498 new ResourceHostMsg_DataDownloaded_ACK(request_id)); |
475 | 499 |
476 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); | 500 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 } | 644 } |
621 } | 645 } |
622 | 646 |
623 void ResourceDispatcher::DidChangePriority( | 647 void ResourceDispatcher::DidChangePriority( |
624 int routing_id, int request_id, net::RequestPriority new_priority) { | 648 int routing_id, int request_id, net::RequestPriority new_priority) { |
625 DCHECK(ContainsKey(pending_requests_, request_id)); | 649 DCHECK(ContainsKey(pending_requests_, request_id)); |
626 message_sender()->Send(new ResourceHostMsg_DidChangePriority( | 650 message_sender()->Send(new ResourceHostMsg_DidChangePriority( |
627 request_id, new_priority)); | 651 request_id, new_priority)); |
628 } | 652 } |
629 | 653 |
| 654 bool ResourceDispatcher::AttachThreadedDataReceiver( |
| 655 int request_id, blink::WebThreadedDataReceiver* threaded_data_receiver) { |
| 656 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id); |
| 657 DCHECK(request_info); |
| 658 DCHECK(!request_info->threaded_data_provider); |
| 659 request_info->threaded_data_provider = new ThreadedDataProvider( |
| 660 request_id, threaded_data_receiver, request_info->buffer, |
| 661 request_info->buffer_size); |
| 662 |
| 663 return true; |
| 664 } |
| 665 |
630 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo() | 666 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo() |
631 : peer(NULL), | 667 : peer(NULL), |
| 668 threaded_data_provider(NULL), |
632 resource_type(ResourceType::SUB_RESOURCE), | 669 resource_type(ResourceType::SUB_RESOURCE), |
633 is_deferred(false), | 670 is_deferred(false), |
634 blocked_response(false), | 671 blocked_response(false), |
635 buffer_size(0) { | 672 buffer_size(0) { |
636 } | 673 } |
637 | 674 |
638 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo( | 675 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo( |
639 webkit_glue::ResourceLoaderBridge::Peer* peer, | 676 webkit_glue::ResourceLoaderBridge::Peer* peer, |
640 ResourceType::Type resource_type, | 677 ResourceType::Type resource_type, |
641 int origin_pid, | 678 int origin_pid, |
642 const GURL& frame_origin, | 679 const GURL& frame_origin, |
643 const GURL& request_url) | 680 const GURL& request_url) |
644 : peer(peer), | 681 : peer(peer), |
| 682 threaded_data_provider(NULL), |
645 resource_type(resource_type), | 683 resource_type(resource_type), |
646 origin_pid(origin_pid), | 684 origin_pid(origin_pid), |
647 is_deferred(false), | 685 is_deferred(false), |
648 url(request_url), | 686 url(request_url), |
649 frame_origin(frame_origin), | 687 frame_origin(frame_origin), |
650 response_url(request_url), | 688 response_url(request_url), |
651 request_start(base::TimeTicks::Now()), | 689 request_start(base::TimeTicks::Now()), |
652 blocked_response(false) { | 690 blocked_response(false) { |
653 } | 691 } |
654 | 692 |
655 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() {} | 693 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() { |
| 694 if (threaded_data_provider) |
| 695 threaded_data_provider->Stop(); |
| 696 } |
656 | 697 |
657 void ResourceDispatcher::DispatchMessage(const IPC::Message& message) { | 698 void ResourceDispatcher::DispatchMessage(const IPC::Message& message) { |
658 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher, message) | 699 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher, message) |
659 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress, OnUploadProgress) | 700 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress, OnUploadProgress) |
660 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse, OnReceivedResponse) | 701 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse, OnReceivedResponse) |
661 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata, | 702 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata, |
662 OnReceivedCachedMetadata) | 703 OnReceivedCachedMetadata) |
663 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedRedirect, OnReceivedRedirect) | 704 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedRedirect, OnReceivedRedirect) |
664 IPC_MESSAGE_HANDLER(ResourceMsg_SetDataBuffer, OnSetDataBuffer) | 705 IPC_MESSAGE_HANDLER(ResourceMsg_SetDataBuffer, OnSetDataBuffer) |
665 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... |
810 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue* queue) { | 851 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue* queue) { |
811 while (!queue->empty()) { | 852 while (!queue->empty()) { |
812 IPC::Message* message = queue->front(); | 853 IPC::Message* message = queue->front(); |
813 ReleaseResourcesInDataMessage(*message); | 854 ReleaseResourcesInDataMessage(*message); |
814 queue->pop_front(); | 855 queue->pop_front(); |
815 delete message; | 856 delete message; |
816 } | 857 } |
817 } | 858 } |
818 | 859 |
819 } // namespace content | 860 } // namespace content |
OLD | NEW |