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
3 // found in the LICENSE file.
5 // See http://dev.chromium.org/developers/design-documents/multi-process-resource-loading
7 #include "content/child/resource_dispatcher.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/compiler_specific.h"
12 #include "base/debug/alias.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/shared_memory.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/metrics/histogram.h"
17 #include "base/strings/string_util.h"
18 #include "content/child/request_extra_data.h"
19 #include "content/child/request_info.h"
20 #include "content/child/site_isolation_policy.h"
21 #include "content/child/sync_load_response.h"
22 #include "content/child/threaded_data_provider.h"
23 #include "content/common/inter_process_time_ticks_converter.h"
24 #include "content/common/resource_messages.h"
25 #include "content/public/child/request_peer.h"
26 #include "content/public/child/resource_dispatcher_delegate.h"
27 #include "content/public/common/resource_response.h"
28 #include "net/base/net_errors.h"
29 #include "net/base/net_util.h"
30 #include "net/base/request_priority.h"
31 #include "net/http/http_response_headers.h"
32 #include "webkit/child/resource_loader_bridge.h"
33 #include "webkit/common/resource_type.h"
35 using webkit_glue::ResourceLoaderBridge
;
41 // Converts |time| from a remote to local TimeTicks, overwriting the original
43 void RemoteToLocalTimeTicks(
44 const InterProcessTimeTicksConverter
& converter
,
45 base::TimeTicks
* time
) {
46 RemoteTimeTicks remote_time
= RemoteTimeTicks::FromTimeTicks(*time
);
47 *time
= converter
.ToLocalTimeTicks(remote_time
).ToTimeTicks();
50 void CrashOnMapFailure() {
52 DWORD last_err
= GetLastError();
53 base::debug::Alias(&last_err
);
58 // Each resource request is assigned an ID scoped to this process.
60 // NOTE: The resource_dispatcher_host also needs probably unique
61 // request_ids, so they count down from -2 (-1 is a special we're
62 // screwed value), while the renderer process counts up.
63 static int next_request_id
= 0;
64 return next_request_id
++;
69 // ResourceLoaderBridge implementation ----------------------------------------
71 class IPCResourceLoaderBridge
: public ResourceLoaderBridge
{
73 IPCResourceLoaderBridge(ResourceDispatcher
* dispatcher
,
74 const RequestInfo
& request_info
);
75 virtual ~IPCResourceLoaderBridge();
77 // ResourceLoaderBridge
78 virtual void SetRequestBody(ResourceRequestBody
* request_body
) OVERRIDE
;
79 virtual bool Start(RequestPeer
* peer
) OVERRIDE
;
80 virtual void Cancel() OVERRIDE
;
81 virtual void SetDefersLoading(bool value
) OVERRIDE
;
82 virtual void DidChangePriority(net::RequestPriority new_priority
,
83 int intra_priority_value
) OVERRIDE
;
84 virtual bool AttachThreadedDataReceiver(
85 blink::WebThreadedDataReceiver
* threaded_data_receiver
) OVERRIDE
;
86 virtual void SyncLoad(SyncLoadResponse
* response
) OVERRIDE
;
89 // The resource dispatcher for this loader. The bridge doesn't own it, but
90 // it's guaranteed to outlive the bridge.
91 ResourceDispatcher
* dispatcher_
;
93 // The request to send, created on initialization for modification and
95 ResourceHostMsg_Request request_
;
97 // ID for the request, valid once Start()ed, -1 if not valid yet.
100 // The routing id used when sending IPC messages.
103 // The security origin of the frame that initiates this request.
106 bool is_synchronous_request_
;
109 IPCResourceLoaderBridge::IPCResourceLoaderBridge(
110 ResourceDispatcher
* dispatcher
,
111 const RequestInfo
& request_info
)
112 : dispatcher_(dispatcher
),
114 routing_id_(request_info
.routing_id
),
115 is_synchronous_request_(false) {
116 DCHECK(dispatcher_
) << "no resource dispatcher";
117 request_
.method
= request_info
.method
;
118 request_
.url
= request_info
.url
;
119 request_
.first_party_for_cookies
= request_info
.first_party_for_cookies
;
120 request_
.referrer
= request_info
.referrer
;
121 request_
.referrer_policy
= request_info
.referrer_policy
;
122 request_
.headers
= request_info
.headers
;
123 request_
.load_flags
= request_info
.load_flags
;
124 request_
.origin_pid
= request_info
.requestor_pid
;
125 request_
.resource_type
= request_info
.request_type
;
126 request_
.priority
= request_info
.priority
;
127 request_
.request_context
= request_info
.request_context
;
128 request_
.appcache_host_id
= request_info
.appcache_host_id
;
129 request_
.download_to_file
= request_info
.download_to_file
;
130 request_
.has_user_gesture
= request_info
.has_user_gesture
;
132 const RequestExtraData kEmptyData
;
133 const RequestExtraData
* extra_data
;
134 if (request_info
.extra_data
)
135 extra_data
= static_cast<RequestExtraData
*>(request_info
.extra_data
);
137 extra_data
= &kEmptyData
;
138 request_
.visiblity_state
= extra_data
->visibility_state();
139 request_
.render_frame_id
= extra_data
->render_frame_id();
140 request_
.is_main_frame
= extra_data
->is_main_frame();
141 request_
.parent_is_main_frame
= extra_data
->parent_is_main_frame();
142 request_
.parent_render_frame_id
= extra_data
->parent_render_frame_id();
143 request_
.allow_download
= extra_data
->allow_download();
144 request_
.transition_type
= extra_data
->transition_type();
145 request_
.should_replace_current_entry
=
146 extra_data
->should_replace_current_entry();
147 request_
.transferred_request_child_id
=
148 extra_data
->transferred_request_child_id();
149 request_
.transferred_request_request_id
=
150 extra_data
->transferred_request_request_id();
151 request_
.service_worker_provider_id
=
152 extra_data
->service_worker_provider_id();
153 frame_origin_
= extra_data
->frame_origin();
156 IPCResourceLoaderBridge::~IPCResourceLoaderBridge() {
157 // we remove our hook for the resource dispatcher only when going away, since
158 // it doesn't keep track of whether we've force terminated the request
159 if (request_id_
>= 0) {
160 // this operation may fail, as the dispatcher will have preemptively
161 // removed us when the renderer sends the ReceivedAllData message.
162 dispatcher_
->RemovePendingRequest(request_id_
);
166 void IPCResourceLoaderBridge::SetRequestBody(
167 ResourceRequestBody
* request_body
) {
168 DCHECK(request_id_
== -1) << "request already started";
169 request_
.request_body
= request_body
;
172 // Writes a footer on the message and sends it
173 bool IPCResourceLoaderBridge::Start(RequestPeer
* peer
) {
174 if (request_id_
!= -1) {
175 NOTREACHED() << "Starting a request twice";
179 // generate the request ID, and append it to the message
180 request_id_
= dispatcher_
->AddPendingRequest(peer
,
181 request_
.resource_type
,
185 request_
.download_to_file
);
187 return dispatcher_
->message_sender()->Send(
188 new ResourceHostMsg_RequestResource(routing_id_
, request_id_
, request_
));
191 void IPCResourceLoaderBridge::Cancel() {
192 if (request_id_
< 0) {
193 NOTREACHED() << "Trying to cancel an unstarted request";
197 if (!is_synchronous_request_
)
198 dispatcher_
->CancelPendingRequest(request_id_
);
200 // We can't remove the request ID from the resource dispatcher because more
201 // data might be pending. Sending the cancel message may cause more data
202 // to be flushed, and will then cause a complete message to be sent.
205 void IPCResourceLoaderBridge::SetDefersLoading(bool value
) {
206 if (request_id_
< 0) {
207 NOTREACHED() << "Trying to (un)defer an unstarted request";
211 dispatcher_
->SetDefersLoading(request_id_
, value
);
214 void IPCResourceLoaderBridge::DidChangePriority(
215 net::RequestPriority new_priority
,
216 int intra_priority_value
) {
217 if (request_id_
< 0) {
218 NOTREACHED() << "Trying to change priority of an unstarted request";
222 dispatcher_
->DidChangePriority(
223 request_id_
, new_priority
, intra_priority_value
);
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";
233 return dispatcher_
->AttachThreadedDataReceiver(request_id_
,
234 threaded_data_receiver
);
237 void IPCResourceLoaderBridge::SyncLoad(SyncLoadResponse
* response
) {
238 if (request_id_
!= -1) {
239 NOTREACHED() << "Starting a request twice";
240 response
->error_code
= net::ERR_FAILED
;
244 request_id_
= MakeRequestID();
245 is_synchronous_request_
= true;
247 SyncLoadResult result
;
248 IPC::SyncMessage
* msg
= new ResourceHostMsg_SyncLoad(routing_id_
, request_id_
,
250 // NOTE: This may pump events (see RenderThread::Send).
251 if (!dispatcher_
->message_sender()->Send(msg
)) {
252 response
->error_code
= net::ERR_FAILED
;
256 response
->error_code
= result
.error_code
;
257 response
->url
= result
.final_url
;
258 response
->headers
= result
.headers
;
259 response
->mime_type
= result
.mime_type
;
260 response
->charset
= result
.charset
;
261 response
->request_time
= result
.request_time
;
262 response
->response_time
= result
.response_time
;
263 response
->encoded_data_length
= result
.encoded_data_length
;
264 response
->load_timing
= result
.load_timing
;
265 response
->devtools_info
= result
.devtools_info
;
266 response
->data
.swap(result
.data
);
267 response
->download_file_path
= result
.download_file_path
;
270 // ResourceDispatcher ---------------------------------------------------------
272 ResourceDispatcher::ResourceDispatcher(IPC::Sender
* sender
)
273 : message_sender_(sender
),
276 io_timestamp_(base::TimeTicks()) {
279 ResourceDispatcher::~ResourceDispatcher() {
282 // ResourceDispatcher implementation ------------------------------------------
284 bool ResourceDispatcher::OnMessageReceived(const IPC::Message
& message
) {
285 if (!IsResourceDispatcherMessage(message
)) {
291 PickleIterator
iter(message
);
292 if (!message
.ReadInt(&iter
, &request_id
)) {
293 NOTREACHED() << "malformed resource message";
297 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
299 // Release resources in the message if it is a data message.
300 ReleaseResourcesInDataMessage(message
);
304 if (request_info
->is_deferred
) {
305 request_info
->deferred_message_queue
.push_back(new IPC::Message(message
));
308 // Make sure any deferred messages are dispatched before we dispatch more.
309 if (!request_info
->deferred_message_queue
.empty()) {
310 FlushDeferredMessages(request_id
);
311 // The request could have been deferred now. If yes then the current
312 // message has to be queued up. The request_info instance should remain
313 // valid here as there are pending messages for it.
314 DCHECK(pending_requests_
.find(request_id
) != pending_requests_
.end());
315 if (request_info
->is_deferred
) {
316 request_info
->deferred_message_queue
.push_back(new IPC::Message(message
));
321 DispatchMessage(message
);
325 ResourceDispatcher::PendingRequestInfo
*
326 ResourceDispatcher::GetPendingRequestInfo(int request_id
) {
327 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
328 if (it
== pending_requests_
.end()) {
329 // This might happen for kill()ed requests on the webkit end.
332 return &(it
->second
);
335 void ResourceDispatcher::OnUploadProgress(int request_id
, int64 position
,
337 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
341 request_info
->peer
->OnUploadProgress(position
, size
);
343 // Acknowledge receipt
344 message_sender_
->Send(new ResourceHostMsg_UploadProgress_ACK(request_id
));
347 void ResourceDispatcher::OnReceivedResponse(
348 int request_id
, const ResourceResponseHead
& response_head
) {
349 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedResponse");
350 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
353 request_info
->response_start
= ConsumeIOTimestamp();
356 RequestPeer
* new_peer
=
357 delegate_
->OnReceivedResponse(
358 request_info
->peer
, response_head
.mime_type
, request_info
->url
);
360 request_info
->peer
= new_peer
;
363 ResourceResponseInfo renderer_response_info
;
364 ToResourceResponseInfo(*request_info
, response_head
, &renderer_response_info
);
365 request_info
->site_isolation_metadata
=
366 SiteIsolationPolicy::OnReceivedResponse(request_info
->frame_origin
,
367 request_info
->response_url
,
368 request_info
->resource_type
,
369 request_info
->origin_pid
,
370 renderer_response_info
);
371 request_info
->peer
->OnReceivedResponse(renderer_response_info
);
374 void ResourceDispatcher::OnReceivedCachedMetadata(
375 int request_id
, const std::vector
<char>& data
) {
376 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
381 request_info
->peer
->OnReceivedCachedMetadata(&data
.front(), data
.size());
384 void ResourceDispatcher::OnSetDataBuffer(int request_id
,
385 base::SharedMemoryHandle shm_handle
,
387 base::ProcessId renderer_pid
) {
388 TRACE_EVENT0("loader", "ResourceDispatcher::OnSetDataBuffer");
389 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
393 bool shm_valid
= base::SharedMemory::IsHandleValid(shm_handle
);
394 CHECK((shm_valid
&& shm_size
> 0) || (!shm_valid
&& !shm_size
));
396 request_info
->buffer
.reset(
397 new base::SharedMemory(shm_handle
, true)); // read only
399 bool ok
= request_info
->buffer
->Map(shm_size
);
401 // Added to help debug crbug/160401.
402 base::ProcessId renderer_pid_copy
= renderer_pid
;
403 base::debug::Alias(&renderer_pid_copy
);
405 base::SharedMemoryHandle shm_handle_copy
= shm_handle
;
406 base::debug::Alias(&shm_handle_copy
);
412 request_info
->buffer_size
= shm_size
;
415 void ResourceDispatcher::OnReceivedData(int request_id
,
418 int encoded_data_length
) {
419 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedData");
420 DCHECK_GT(data_length
, 0);
421 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
422 bool send_ack
= true;
423 if (request_info
&& data_length
> 0) {
424 CHECK(base::SharedMemory::IsHandleValid(request_info
->buffer
->handle()));
425 CHECK_GE(request_info
->buffer_size
, data_offset
+ data_length
);
427 // Ensure that the SHM buffer remains valid for the duration of this scope.
428 // It is possible for CancelPendingRequest() to be called before we exit
430 linked_ptr
<base::SharedMemory
> retain_buffer(request_info
->buffer
);
432 base::TimeTicks time_start
= base::TimeTicks::Now();
434 const char* data_start
= static_cast<char*>(request_info
->buffer
->memory());
436 CHECK(data_start
+ data_offset
);
437 const char* data_ptr
= data_start
+ data_offset
;
439 // Check whether this response data is compliant with our cross-site
440 // document blocking policy. We only do this for the first packet.
441 std::string alternative_data
;
442 if (request_info
->site_isolation_metadata
.get()) {
443 request_info
->blocked_response
=
444 SiteIsolationPolicy::ShouldBlockResponse(
445 request_info
->site_isolation_metadata
, data_ptr
, data_length
,
447 request_info
->site_isolation_metadata
.reset();
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();
459 if (!request_info
->blocked_response
|| !alternative_data
.empty()) {
460 if (request_info
->threaded_data_provider
) {
461 request_info
->threaded_data_provider
->OnReceivedDataOnForegroundThread(
462 data_ptr
, data_length
, encoded_data_length
);
463 // A threaded data provider will take care of its own ACKing, as the
464 // data may be processed later on another thread.
467 request_info
->peer
->OnReceivedData(
468 data_ptr
, data_length
, encoded_data_length
);
472 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime",
473 base::TimeTicks::Now() - time_start
);
476 // Acknowledge the reception of this data.
478 message_sender_
->Send(new ResourceHostMsg_DataReceived_ACK(request_id
));
481 void ResourceDispatcher::OnDownloadedData(int request_id
,
483 int encoded_data_length
) {
484 // Acknowledge the reception of this message.
485 message_sender_
->Send(new ResourceHostMsg_DataDownloaded_ACK(request_id
));
487 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
491 request_info
->peer
->OnDownloadedData(data_len
, encoded_data_length
);
494 void ResourceDispatcher::OnReceivedRedirect(
497 const GURL
& new_first_party_for_cookies
,
498 const ResourceResponseHead
& response_head
) {
499 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedRedirect");
500 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
503 request_info
->response_start
= ConsumeIOTimestamp();
505 ResourceResponseInfo renderer_response_info
;
506 ToResourceResponseInfo(*request_info
, response_head
, &renderer_response_info
);
507 if (request_info
->peer
->OnReceivedRedirect(
508 new_url
, new_first_party_for_cookies
, renderer_response_info
)) {
509 // Double-check if the request is still around. The call above could
510 // potentially remove it.
511 request_info
= GetPendingRequestInfo(request_id
);
514 // We update the response_url here so that we can send it to
515 // SiteIsolationPolicy later when OnReceivedResponse is called.
516 request_info
->response_url
= new_url
;
517 request_info
->pending_redirect_message
.reset(
518 new ResourceHostMsg_FollowRedirect(request_id
));
519 if (!request_info
->is_deferred
) {
520 FollowPendingRedirect(request_id
, *request_info
);
523 CancelPendingRequest(request_id
);
527 void ResourceDispatcher::FollowPendingRedirect(
529 PendingRequestInfo
& request_info
) {
530 IPC::Message
* msg
= request_info
.pending_redirect_message
.release();
532 message_sender_
->Send(msg
);
535 void ResourceDispatcher::OnRequestComplete(
537 const ResourceMsg_RequestCompleteData
& request_complete_data
) {
538 TRACE_EVENT0("loader", "ResourceDispatcher::OnRequestComplete");
540 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
543 request_info
->completion_time
= ConsumeIOTimestamp();
544 request_info
->buffer
.reset();
545 request_info
->buffer_size
= 0;
547 RequestPeer
* peer
= request_info
->peer
;
550 RequestPeer
* new_peer
=
551 delegate_
->OnRequestComplete(
552 request_info
->peer
, request_info
->resource_type
,
553 request_complete_data
.error_code
);
555 request_info
->peer
= new_peer
;
558 base::TimeTicks renderer_completion_time
= ToRendererCompletionTime(
559 *request_info
, request_complete_data
.completion_time
);
560 // The request ID will be removed from our pending list in the destructor.
561 // Normally, dispatching this message causes the reference-counted request to
563 peer
->OnCompletedRequest(request_complete_data
.error_code
,
564 request_complete_data
.was_ignored_by_handler
,
565 request_complete_data
.exists_in_cache
,
566 request_complete_data
.security_info
,
567 renderer_completion_time
,
568 request_complete_data
.encoded_data_length
);
571 int ResourceDispatcher::AddPendingRequest(RequestPeer
* callback
,
572 ResourceType::Type resource_type
,
574 const GURL
& frame_origin
,
575 const GURL
& request_url
,
576 bool download_to_file
) {
577 // Compute a unique request_id for this renderer process.
578 int id
= MakeRequestID();
579 pending_requests_
[id
] = PendingRequestInfo(callback
,
588 bool ResourceDispatcher::RemovePendingRequest(int request_id
) {
589 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
590 if (it
== pending_requests_
.end())
593 PendingRequestInfo
& request_info
= it
->second
;
595 bool release_downloaded_file
= request_info
.download_to_file
;
597 ReleaseResourcesInMessageQueue(&request_info
.deferred_message_queue
);
598 pending_requests_
.erase(it
);
600 if (release_downloaded_file
) {
601 message_sender_
->Send(
602 new ResourceHostMsg_ReleaseDownloadedFile(request_id
));
608 void ResourceDispatcher::CancelPendingRequest(int request_id
) {
609 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
610 if (it
== pending_requests_
.end()) {
611 DVLOG(1) << "unknown request";
615 PendingRequestInfo
& request_info
= it
->second
;
616 ReleaseResourcesInMessageQueue(&request_info
.deferred_message_queue
);
617 pending_requests_
.erase(it
);
619 message_sender_
->Send(new ResourceHostMsg_CancelRequest(request_id
));
622 void ResourceDispatcher::SetDefersLoading(int request_id
, bool value
) {
623 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
624 if (it
== pending_requests_
.end()) {
625 DLOG(ERROR
) << "unknown request";
628 PendingRequestInfo
& request_info
= it
->second
;
630 request_info
.is_deferred
= value
;
631 } else if (request_info
.is_deferred
) {
632 request_info
.is_deferred
= false;
634 FollowPendingRedirect(request_id
, request_info
);
636 base::MessageLoop::current()->PostTask(
638 base::Bind(&ResourceDispatcher::FlushDeferredMessages
,
639 weak_factory_
.GetWeakPtr(),
644 void ResourceDispatcher::DidChangePriority(int request_id
,
645 net::RequestPriority new_priority
,
646 int intra_priority_value
) {
647 DCHECK(ContainsKey(pending_requests_
, request_id
));
648 message_sender_
->Send(new ResourceHostMsg_DidChangePriority(
649 request_id
, new_priority
, intra_priority_value
));
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
);
664 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo()
666 threaded_data_provider(NULL
),
667 resource_type(ResourceType::SUB_RESOURCE
),
669 download_to_file(false),
670 blocked_response(false),
674 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo(
676 ResourceType::Type resource_type
,
678 const GURL
& frame_origin
,
679 const GURL
& request_url
,
680 bool download_to_file
)
682 threaded_data_provider(NULL
),
683 resource_type(resource_type
),
684 origin_pid(origin_pid
),
687 frame_origin(frame_origin
),
688 response_url(request_url
),
689 download_to_file(download_to_file
),
690 request_start(base::TimeTicks::Now()),
691 blocked_response(false) {}
693 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() {
694 if (threaded_data_provider
)
695 threaded_data_provider
->Stop();
698 void ResourceDispatcher::DispatchMessage(const IPC::Message
& message
) {
699 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher
, message
)
700 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress
, OnUploadProgress
)
701 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse
, OnReceivedResponse
)
702 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata
,
703 OnReceivedCachedMetadata
)
704 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedRedirect
, OnReceivedRedirect
)
705 IPC_MESSAGE_HANDLER(ResourceMsg_SetDataBuffer
, OnSetDataBuffer
)
706 IPC_MESSAGE_HANDLER(ResourceMsg_DataReceived
, OnReceivedData
)
707 IPC_MESSAGE_HANDLER(ResourceMsg_DataDownloaded
, OnDownloadedData
)
708 IPC_MESSAGE_HANDLER(ResourceMsg_RequestComplete
, OnRequestComplete
)
709 IPC_END_MESSAGE_MAP()
712 void ResourceDispatcher::FlushDeferredMessages(int request_id
) {
713 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
714 if (it
== pending_requests_
.end()) // The request could have become invalid.
716 PendingRequestInfo
& request_info
= it
->second
;
717 if (request_info
.is_deferred
)
719 // Because message handlers could result in request_info being destroyed,
720 // we need to work with a stack reference to the deferred queue.
722 q
.swap(request_info
.deferred_message_queue
);
724 IPC::Message
* m
= q
.front();
728 // If this request is deferred in the context of the above message, then
729 // we should honor the same and stop dispatching further messages.
730 // We need to find the request again in the list as it may have completed
731 // by now and the request_info instance above may be invalid.
732 PendingRequestList::iterator index
= pending_requests_
.find(request_id
);
733 if (index
!= pending_requests_
.end()) {
734 PendingRequestInfo
& pending_request
= index
->second
;
735 if (pending_request
.is_deferred
) {
736 pending_request
.deferred_message_queue
.swap(q
);
743 ResourceLoaderBridge
* ResourceDispatcher::CreateBridge(
744 const RequestInfo
& request_info
) {
745 return new IPCResourceLoaderBridge(this, request_info
);
748 void ResourceDispatcher::ToResourceResponseInfo(
749 const PendingRequestInfo
& request_info
,
750 const ResourceResponseHead
& browser_info
,
751 ResourceResponseInfo
* renderer_info
) const {
752 *renderer_info
= browser_info
;
753 if (request_info
.request_start
.is_null() ||
754 request_info
.response_start
.is_null() ||
755 browser_info
.request_start
.is_null() ||
756 browser_info
.response_start
.is_null() ||
757 browser_info
.load_timing
.request_start
.is_null()) {
760 InterProcessTimeTicksConverter
converter(
761 LocalTimeTicks::FromTimeTicks(request_info
.request_start
),
762 LocalTimeTicks::FromTimeTicks(request_info
.response_start
),
763 RemoteTimeTicks::FromTimeTicks(browser_info
.request_start
),
764 RemoteTimeTicks::FromTimeTicks(browser_info
.response_start
));
766 net::LoadTimingInfo
* load_timing
= &renderer_info
->load_timing
;
767 RemoteToLocalTimeTicks(converter
, &load_timing
->request_start
);
768 RemoteToLocalTimeTicks(converter
, &load_timing
->proxy_resolve_start
);
769 RemoteToLocalTimeTicks(converter
, &load_timing
->proxy_resolve_end
);
770 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.dns_start
);
771 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.dns_end
);
772 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.connect_start
);
773 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.connect_end
);
774 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.ssl_start
);
775 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.ssl_end
);
776 RemoteToLocalTimeTicks(converter
, &load_timing
->send_start
);
777 RemoteToLocalTimeTicks(converter
, &load_timing
->send_end
);
778 RemoteToLocalTimeTicks(converter
, &load_timing
->receive_headers_end
);
781 base::TimeTicks
ResourceDispatcher::ToRendererCompletionTime(
782 const PendingRequestInfo
& request_info
,
783 const base::TimeTicks
& browser_completion_time
) const {
784 if (request_info
.completion_time
.is_null()) {
785 return browser_completion_time
;
788 // TODO(simonjam): The optimal lower bound should be the most recent value of
789 // TimeTicks::Now() returned to WebKit. Is it worth trying to cache that?
790 // Until then, |response_start| is used as it is the most recent value
791 // returned for this request.
792 int64 result
= std::max(browser_completion_time
.ToInternalValue(),
793 request_info
.response_start
.ToInternalValue());
794 result
= std::min(result
, request_info
.completion_time
.ToInternalValue());
795 return base::TimeTicks::FromInternalValue(result
);
798 base::TimeTicks
ResourceDispatcher::ConsumeIOTimestamp() {
799 if (io_timestamp_
== base::TimeTicks())
800 return base::TimeTicks::Now();
801 base::TimeTicks result
= io_timestamp_
;
802 io_timestamp_
= base::TimeTicks();
807 bool ResourceDispatcher::IsResourceDispatcherMessage(
808 const IPC::Message
& message
) {
809 switch (message
.type()) {
810 case ResourceMsg_UploadProgress::ID
:
811 case ResourceMsg_ReceivedResponse::ID
:
812 case ResourceMsg_ReceivedCachedMetadata::ID
:
813 case ResourceMsg_ReceivedRedirect::ID
:
814 case ResourceMsg_SetDataBuffer::ID
:
815 case ResourceMsg_DataReceived::ID
:
816 case ResourceMsg_DataDownloaded::ID
:
817 case ResourceMsg_RequestComplete::ID
:
828 void ResourceDispatcher::ReleaseResourcesInDataMessage(
829 const IPC::Message
& message
) {
830 PickleIterator
iter(message
);
832 if (!message
.ReadInt(&iter
, &request_id
)) {
833 NOTREACHED() << "malformed resource message";
837 // If the message contains a shared memory handle, we should close the handle
838 // or there will be a memory leak.
839 if (message
.type() == ResourceMsg_SetDataBuffer::ID
) {
840 base::SharedMemoryHandle shm_handle
;
841 if (IPC::ParamTraits
<base::SharedMemoryHandle
>::Read(&message
,
844 if (base::SharedMemory::IsHandleValid(shm_handle
))
845 base::SharedMemory::CloseHandle(shm_handle
);
851 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue
* queue
) {
852 while (!queue
->empty()) {
853 IPC::Message
* message
= queue
->front();
854 ReleaseResourcesInDataMessage(*message
);
860 } // namespace content