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/common/inter_process_time_ticks_converter.h"
23 #include "content/common/resource_messages.h"
24 #include "content/public/child/request_peer.h"
25 #include "content/public/child/resource_dispatcher_delegate.h"
26 #include "content/public/common/resource_response.h"
27 #include "net/base/net_errors.h"
28 #include "net/base/net_util.h"
29 #include "net/base/request_priority.h"
30 #include "net/http/http_response_headers.h"
31 #include "webkit/child/resource_loader_bridge.h"
32 #include "webkit/common/resource_type.h"
34 using webkit_glue::ResourceLoaderBridge
;
35 using webkit_glue::ResourceResponseInfo
;
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();
53 static void CrashOnMapFailure() {
55 DWORD last_err
= GetLastError();
56 base::debug::Alias(&last_err
);
61 // Each resource request is assigned an ID scoped to this process.
62 static int MakeRequestID() {
63 // NOTE: The resource_dispatcher_host also needs probably unique
64 // request_ids, so they count down from -2 (-1 is a special we're
65 // screwed value), while the renderer process counts up.
66 static int next_request_id
= 0;
67 return next_request_id
++;
70 // ResourceLoaderBridge implementation ----------------------------------------
72 class IPCResourceLoaderBridge
: public ResourceLoaderBridge
{
74 IPCResourceLoaderBridge(ResourceDispatcher
* dispatcher
,
75 const RequestInfo
& request_info
);
76 virtual ~IPCResourceLoaderBridge();
78 // ResourceLoaderBridge
79 virtual void SetRequestBody(ResourceRequestBody
* request_body
) OVERRIDE
;
80 virtual bool Start(RequestPeer
* peer
) OVERRIDE
;
81 virtual void Cancel() OVERRIDE
;
82 virtual void SetDefersLoading(bool value
) OVERRIDE
;
83 virtual void DidChangePriority(net::RequestPriority new_priority
,
84 int intra_priority_value
) OVERRIDE
;
85 virtual void SyncLoad(SyncLoadResponse
* response
) OVERRIDE
;
90 // The resource dispatcher for this loader. The bridge doesn't own it, but
91 // it's guaranteed to outlive the bridge.
92 ResourceDispatcher
* dispatcher_
;
94 // The request to send, created on initialization for modification and
96 ResourceHostMsg_Request request_
;
98 // ID for the request, valid once Start()ed, -1 if not valid yet.
101 // The routing id used when sending IPC messages.
104 // The security origin of the frame that initiates this request.
107 bool is_synchronous_request_
;
110 IPCResourceLoaderBridge::IPCResourceLoaderBridge(
111 ResourceDispatcher
* dispatcher
,
112 const RequestInfo
& request_info
)
114 dispatcher_(dispatcher
),
116 routing_id_(request_info
.routing_id
),
117 is_synchronous_request_(false) {
118 DCHECK(dispatcher_
) << "no resource dispatcher";
119 request_
.method
= request_info
.method
;
120 request_
.url
= request_info
.url
;
121 request_
.first_party_for_cookies
= request_info
.first_party_for_cookies
;
122 request_
.referrer
= request_info
.referrer
;
123 request_
.referrer_policy
= request_info
.referrer_policy
;
124 request_
.headers
= request_info
.headers
;
125 request_
.load_flags
= request_info
.load_flags
;
126 request_
.origin_pid
= request_info
.requestor_pid
;
127 request_
.resource_type
= request_info
.request_type
;
128 request_
.priority
= request_info
.priority
;
129 request_
.request_context
= request_info
.request_context
;
130 request_
.appcache_host_id
= request_info
.appcache_host_id
;
131 request_
.download_to_file
= request_info
.download_to_file
;
132 request_
.has_user_gesture
= request_info
.has_user_gesture
;
134 const RequestExtraData kEmptyData
;
135 const RequestExtraData
* extra_data
;
136 if (request_info
.extra_data
)
137 extra_data
= static_cast<RequestExtraData
*>(request_info
.extra_data
);
139 extra_data
= &kEmptyData
;
140 request_
.visiblity_state
= extra_data
->visibility_state();
141 request_
.render_frame_id
= extra_data
->render_frame_id();
142 request_
.is_main_frame
= extra_data
->is_main_frame();
143 request_
.parent_is_main_frame
= extra_data
->parent_is_main_frame();
144 request_
.parent_render_frame_id
= extra_data
->parent_render_frame_id();
145 request_
.allow_download
= extra_data
->allow_download();
146 request_
.transition_type
= extra_data
->transition_type();
147 request_
.should_replace_current_entry
=
148 extra_data
->should_replace_current_entry();
149 request_
.transferred_request_child_id
=
150 extra_data
->transferred_request_child_id();
151 request_
.transferred_request_request_id
=
152 extra_data
->transferred_request_request_id();
153 request_
.service_worker_provider_id
=
154 extra_data
->service_worker_provider_id();
155 frame_origin_
= extra_data
->frame_origin();
158 IPCResourceLoaderBridge::~IPCResourceLoaderBridge() {
159 // we remove our hook for the resource dispatcher only when going away, since
160 // it doesn't keep track of whether we've force terminated the request
161 if (request_id_
>= 0) {
162 // this operation may fail, as the dispatcher will have preemptively
163 // removed us when the renderer sends the ReceivedAllData message.
164 dispatcher_
->RemovePendingRequest(request_id_
);
166 if (request_
.download_to_file
) {
167 dispatcher_
->message_sender()->Send(
168 new ResourceHostMsg_ReleaseDownloadedFile(request_id_
));
173 void IPCResourceLoaderBridge::SetRequestBody(
174 ResourceRequestBody
* request_body
) {
175 DCHECK(request_id_
== -1) << "request already started";
176 request_
.request_body
= request_body
;
179 // Writes a footer on the message and sends it
180 bool IPCResourceLoaderBridge::Start(RequestPeer
* peer
) {
181 if (request_id_
!= -1) {
182 NOTREACHED() << "Starting a request twice";
188 // generate the request ID, and append it to the message
189 request_id_
= dispatcher_
->AddPendingRequest(peer_
,
190 request_
.resource_type
,
195 return dispatcher_
->message_sender()->Send(
196 new ResourceHostMsg_RequestResource(routing_id_
, request_id_
, request_
));
199 void IPCResourceLoaderBridge::Cancel() {
200 if (request_id_
< 0) {
201 NOTREACHED() << "Trying to cancel an unstarted request";
205 if (!is_synchronous_request_
)
206 dispatcher_
->CancelPendingRequest(request_id_
);
208 // We can't remove the request ID from the resource dispatcher because more
209 // data might be pending. Sending the cancel message may cause more data
210 // to be flushed, and will then cause a complete message to be sent.
213 void IPCResourceLoaderBridge::SetDefersLoading(bool value
) {
214 if (request_id_
< 0) {
215 NOTREACHED() << "Trying to (un)defer an unstarted request";
219 dispatcher_
->SetDefersLoading(request_id_
, value
);
222 void IPCResourceLoaderBridge::DidChangePriority(
223 net::RequestPriority new_priority
, int intra_priority_value
) {
224 if (request_id_
< 0) {
225 NOTREACHED() << "Trying to change priority of an unstarted request";
229 dispatcher_
->DidChangePriority(routing_id_
, request_id_
, new_priority
,
230 intra_priority_value
);
233 void IPCResourceLoaderBridge::SyncLoad(SyncLoadResponse
* response
) {
234 if (request_id_
!= -1) {
235 NOTREACHED() << "Starting a request twice";
236 response
->error_code
= net::ERR_FAILED
;
240 request_id_
= MakeRequestID();
241 is_synchronous_request_
= true;
243 SyncLoadResult result
;
244 IPC::SyncMessage
* msg
= new ResourceHostMsg_SyncLoad(routing_id_
, request_id_
,
246 // NOTE: This may pump events (see RenderThread::Send).
247 if (!dispatcher_
->message_sender()->Send(msg
)) {
248 response
->error_code
= net::ERR_FAILED
;
252 response
->error_code
= result
.error_code
;
253 response
->url
= result
.final_url
;
254 response
->headers
= result
.headers
;
255 response
->mime_type
= result
.mime_type
;
256 response
->charset
= result
.charset
;
257 response
->request_time
= result
.request_time
;
258 response
->response_time
= result
.response_time
;
259 response
->encoded_data_length
= result
.encoded_data_length
;
260 response
->load_timing
= result
.load_timing
;
261 response
->devtools_info
= result
.devtools_info
;
262 response
->data
.swap(result
.data
);
263 response
->download_file_path
= result
.download_file_path
;
266 // ResourceDispatcher ---------------------------------------------------------
268 ResourceDispatcher::ResourceDispatcher(IPC::Sender
* sender
)
269 : message_sender_(sender
),
272 io_timestamp_(base::TimeTicks()) {
275 ResourceDispatcher::~ResourceDispatcher() {
278 // ResourceDispatcher implementation ------------------------------------------
280 bool ResourceDispatcher::OnMessageReceived(const IPC::Message
& message
) {
281 if (!IsResourceDispatcherMessage(message
)) {
287 PickleIterator
iter(message
);
288 if (!message
.ReadInt(&iter
, &request_id
)) {
289 NOTREACHED() << "malformed resource message";
293 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
295 // Release resources in the message if it is a data message.
296 ReleaseResourcesInDataMessage(message
);
300 // If the request has been canceled, only dispatch
301 // ResourceMsg_RequestComplete (otherwise resource leaks) and drop other
303 if (request_info
->is_canceled
) {
304 if (message
.type() == ResourceMsg_RequestComplete::ID
) {
305 DispatchMessage(message
);
307 ReleaseResourcesInDataMessage(message
);
312 if (request_info
->is_deferred
) {
313 request_info
->deferred_message_queue
.push_back(new IPC::Message(message
));
316 // Make sure any deferred messages are dispatched before we dispatch more.
317 if (!request_info
->deferred_message_queue
.empty()) {
318 FlushDeferredMessages(request_id
);
319 // The request could have been deferred now. If yes then the current
320 // message has to be queued up. The request_info instance should remain
321 // valid here as there are pending messages for it.
322 DCHECK(pending_requests_
.find(request_id
) != pending_requests_
.end());
323 if (request_info
->is_deferred
) {
324 request_info
->deferred_message_queue
.push_back(new IPC::Message(message
));
329 DispatchMessage(message
);
333 ResourceDispatcher::PendingRequestInfo
*
334 ResourceDispatcher::GetPendingRequestInfo(int request_id
) {
335 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
336 if (it
== pending_requests_
.end()) {
337 // This might happen for kill()ed requests on the webkit end.
340 return &(it
->second
);
343 void ResourceDispatcher::OnUploadProgress(int request_id
, int64 position
,
345 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
349 request_info
->peer
->OnUploadProgress(position
, size
);
351 // Acknowledge receipt
352 message_sender()->Send(new ResourceHostMsg_UploadProgress_ACK(request_id
));
355 void ResourceDispatcher::OnReceivedResponse(
356 int request_id
, const ResourceResponseHead
& response_head
) {
357 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedResponse");
358 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
361 request_info
->response_start
= ConsumeIOTimestamp();
364 RequestPeer
* new_peer
=
365 delegate_
->OnReceivedResponse(
366 request_info
->peer
, response_head
.mime_type
, request_info
->url
);
368 request_info
->peer
= new_peer
;
371 ResourceResponseInfo renderer_response_info
;
372 ToResourceResponseInfo(*request_info
, response_head
, &renderer_response_info
);
373 request_info
->site_isolation_metadata
=
374 SiteIsolationPolicy::OnReceivedResponse(request_info
->frame_origin
,
375 request_info
->response_url
,
376 request_info
->resource_type
,
377 request_info
->origin_pid
,
378 renderer_response_info
);
379 request_info
->peer
->OnReceivedResponse(renderer_response_info
);
382 void ResourceDispatcher::OnReceivedCachedMetadata(
383 int request_id
, const std::vector
<char>& data
) {
384 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
389 request_info
->peer
->OnReceivedCachedMetadata(&data
.front(), data
.size());
392 void ResourceDispatcher::OnSetDataBuffer(int request_id
,
393 base::SharedMemoryHandle shm_handle
,
395 base::ProcessId renderer_pid
) {
396 TRACE_EVENT0("loader", "ResourceDispatcher::OnSetDataBuffer");
397 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
401 bool shm_valid
= base::SharedMemory::IsHandleValid(shm_handle
);
402 CHECK((shm_valid
&& shm_size
> 0) || (!shm_valid
&& !shm_size
));
404 request_info
->buffer
.reset(
405 new base::SharedMemory(shm_handle
, true)); // read only
407 bool ok
= request_info
->buffer
->Map(shm_size
);
409 // Added to help debug crbug/160401.
410 base::ProcessId renderer_pid_copy
= renderer_pid
;
411 base::debug::Alias(&renderer_pid_copy
);
413 base::SharedMemoryHandle shm_handle_copy
= shm_handle
;
414 base::debug::Alias(&shm_handle_copy
);
420 request_info
->buffer_size
= shm_size
;
423 void ResourceDispatcher::OnReceivedData(int request_id
,
426 int encoded_data_length
) {
427 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedData");
428 DCHECK_GT(data_length
, 0);
429 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
430 if (request_info
&& data_length
> 0) {
431 CHECK(base::SharedMemory::IsHandleValid(request_info
->buffer
->handle()));
432 CHECK_GE(request_info
->buffer_size
, data_offset
+ data_length
);
434 // Ensure that the SHM buffer remains valid for the duration of this scope.
435 // It is possible for CancelPendingRequest() to be called before we exit
437 linked_ptr
<base::SharedMemory
> retain_buffer(request_info
->buffer
);
439 base::TimeTicks time_start
= base::TimeTicks::Now();
441 const char* data_ptr
= static_cast<char*>(request_info
->buffer
->memory());
443 CHECK(data_ptr
+ data_offset
);
445 // Check whether this response data is compliant with our cross-site
446 // document blocking policy. We only do this for the first packet.
447 std::string alternative_data
;
448 if (request_info
->site_isolation_metadata
.get()) {
449 request_info
->blocked_response
=
450 SiteIsolationPolicy::ShouldBlockResponse(
451 request_info
->site_isolation_metadata
, data_ptr
+ data_offset
,
452 data_length
, &alternative_data
);
453 request_info
->site_isolation_metadata
.reset();
456 // When the response is not blocked.
457 if (!request_info
->blocked_response
) {
458 request_info
->peer
->OnReceivedData(
459 data_ptr
+ data_offset
, data_length
, encoded_data_length
);
460 } else if (alternative_data
.size() > 0) {
461 // When the response is blocked, and when we have any alternative data to
462 // send to the renderer. When |alternative_data| is zero-sized, we do not
463 // call peer's callback.
464 request_info
->peer
->OnReceivedData(alternative_data
.data(),
465 alternative_data
.size(),
466 alternative_data
.size());
469 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime",
470 base::TimeTicks::Now() - time_start
);
473 // Acknowledge the reception of this data.
474 message_sender()->Send(new ResourceHostMsg_DataReceived_ACK(request_id
));
477 void ResourceDispatcher::OnDownloadedData(int request_id
,
479 int encoded_data_length
) {
480 // Acknowledge the reception of this message.
481 message_sender()->Send(
482 new ResourceHostMsg_DataDownloaded_ACK(request_id
));
484 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
488 request_info
->peer
->OnDownloadedData(data_len
, encoded_data_length
);
491 void ResourceDispatcher::OnReceivedRedirect(
494 const ResourceResponseHead
& response_head
) {
495 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedRedirect");
496 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
499 request_info
->response_start
= ConsumeIOTimestamp();
501 bool has_new_first_party_for_cookies
= false;
502 GURL new_first_party_for_cookies
;
503 ResourceResponseInfo renderer_response_info
;
504 ToResourceResponseInfo(*request_info
, response_head
, &renderer_response_info
);
505 if (request_info
->peer
->OnReceivedRedirect(new_url
, renderer_response_info
,
506 &has_new_first_party_for_cookies
,
507 &new_first_party_for_cookies
)) {
508 // Double-check if the request is still around. The call above could
509 // potentially remove it.
510 request_info
= GetPendingRequestInfo(request_id
);
513 // We update the response_url here so that we can send it to
514 // SiteIsolationPolicy later when OnReceivedResponse is called.
515 request_info
->response_url
= new_url
;
516 request_info
->pending_redirect_message
.reset(
517 new ResourceHostMsg_FollowRedirect(request_id
,
518 has_new_first_party_for_cookies
,
519 new_first_party_for_cookies
));
520 if (!request_info
->is_deferred
) {
521 FollowPendingRedirect(request_id
, *request_info
);
524 CancelPendingRequest(request_id
);
528 void ResourceDispatcher::FollowPendingRedirect(
530 PendingRequestInfo
& request_info
) {
531 IPC::Message
* msg
= request_info
.pending_redirect_message
.release();
533 message_sender()->Send(msg
);
536 void ResourceDispatcher::OnRequestComplete(
538 const ResourceMsg_RequestCompleteData
& request_complete_data
) {
539 TRACE_EVENT0("loader", "ResourceDispatcher::OnRequestComplete");
541 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
544 request_info
->completion_time
= ConsumeIOTimestamp();
545 request_info
->buffer
.reset();
546 request_info
->buffer_size
= 0;
548 RequestPeer
* peer
= request_info
->peer
;
551 RequestPeer
* new_peer
=
552 delegate_
->OnRequestComplete(
553 request_info
->peer
, request_info
->resource_type
,
554 request_complete_data
.error_code
);
556 request_info
->peer
= new_peer
;
559 base::TimeTicks renderer_completion_time
= ToRendererCompletionTime(
560 *request_info
, request_complete_data
.completion_time
);
561 // The request ID will be removed from our pending list in the destructor.
562 // Normally, dispatching this message causes the reference-counted request to
564 peer
->OnCompletedRequest(request_complete_data
.error_code
,
565 request_complete_data
.was_ignored_by_handler
,
566 request_complete_data
.exists_in_cache
,
567 request_complete_data
.security_info
,
568 renderer_completion_time
,
569 request_complete_data
.encoded_data_length
);
572 int ResourceDispatcher::AddPendingRequest(RequestPeer
* callback
,
573 ResourceType::Type resource_type
,
575 const GURL
& frame_origin
,
576 const GURL
& request_url
) {
577 // Compute a unique request_id for this renderer process.
578 int id
= MakeRequestID();
579 pending_requests_
[id
] = PendingRequestInfo(
580 callback
, resource_type
, origin_pid
, frame_origin
, request_url
);
584 bool ResourceDispatcher::RemovePendingRequest(int request_id
) {
585 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
586 if (it
== pending_requests_
.end())
589 PendingRequestInfo
& request_info
= it
->second
;
590 ReleaseResourcesInMessageQueue(&request_info
.deferred_message_queue
);
591 pending_requests_
.erase(it
);
596 void ResourceDispatcher::CancelPendingRequest(int request_id
) {
597 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
598 if (it
== pending_requests_
.end()) {
599 DVLOG(1) << "unknown request";
603 PendingRequestInfo
& request_info
= it
->second
;
604 request_info
.is_canceled
= true;
606 // Removes pending requests. If ResourceMsg_RequestComplete was queued,
608 MessageQueue
& queue
= request_info
.deferred_message_queue
;
609 while (!queue
.empty()) {
610 IPC::Message
* message
= queue
.front();
611 if (message
->type() == ResourceMsg_RequestComplete::ID
) {
612 DispatchMessage(*message
);
614 ReleaseResourcesInDataMessage(*message
);
620 // |request_id| will be removed from |pending_requests_| when
621 // OnRequestComplete returns with ERR_ABORTED.
622 message_sender()->Send(new ResourceHostMsg_CancelRequest(request_id
));
625 void ResourceDispatcher::SetDefersLoading(int request_id
, bool value
) {
626 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
627 if (it
== pending_requests_
.end()) {
628 DLOG(ERROR
) << "unknown request";
631 PendingRequestInfo
& request_info
= it
->second
;
633 request_info
.is_deferred
= value
;
634 } else if (request_info
.is_deferred
) {
635 request_info
.is_deferred
= false;
637 FollowPendingRedirect(request_id
, request_info
);
639 base::MessageLoop::current()->PostTask(
641 base::Bind(&ResourceDispatcher::FlushDeferredMessages
,
642 weak_factory_
.GetWeakPtr(),
647 void ResourceDispatcher::DidChangePriority(
648 int routing_id
, int request_id
, net::RequestPriority new_priority
,
649 int intra_priority_value
) {
650 DCHECK(ContainsKey(pending_requests_
, request_id
));
651 message_sender()->Send(new ResourceHostMsg_DidChangePriority(
652 request_id
, new_priority
, intra_priority_value
));
655 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo()
657 resource_type(ResourceType::SUB_RESOURCE
),
660 blocked_response(false),
664 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo(
666 ResourceType::Type resource_type
,
668 const GURL
& frame_origin
,
669 const GURL
& request_url
)
671 resource_type(resource_type
),
672 origin_pid(origin_pid
),
676 frame_origin(frame_origin
),
677 response_url(request_url
),
678 request_start(base::TimeTicks::Now()),
679 blocked_response(false) {}
681 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() {}
683 void ResourceDispatcher::DispatchMessage(const IPC::Message
& message
) {
684 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher
, message
)
685 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress
, OnUploadProgress
)
686 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse
, OnReceivedResponse
)
687 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata
,
688 OnReceivedCachedMetadata
)
689 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedRedirect
, OnReceivedRedirect
)
690 IPC_MESSAGE_HANDLER(ResourceMsg_SetDataBuffer
, OnSetDataBuffer
)
691 IPC_MESSAGE_HANDLER(ResourceMsg_DataReceived
, OnReceivedData
)
692 IPC_MESSAGE_HANDLER(ResourceMsg_DataDownloaded
, OnDownloadedData
)
693 IPC_MESSAGE_HANDLER(ResourceMsg_RequestComplete
, OnRequestComplete
)
694 IPC_END_MESSAGE_MAP()
697 void ResourceDispatcher::FlushDeferredMessages(int request_id
) {
698 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
699 if (it
== pending_requests_
.end()) // The request could have become invalid.
701 PendingRequestInfo
& request_info
= it
->second
;
702 if (request_info
.is_deferred
)
704 // Because message handlers could result in request_info being destroyed,
705 // we need to work with a stack reference to the deferred queue.
707 q
.swap(request_info
.deferred_message_queue
);
709 IPC::Message
* m
= q
.front();
713 // If this request is deferred in the context of the above message, then
714 // we should honor the same and stop dispatching further messages.
715 // We need to find the request again in the list as it may have completed
716 // by now and the request_info instance above may be invalid.
717 PendingRequestList::iterator index
= pending_requests_
.find(request_id
);
718 if (index
!= pending_requests_
.end()) {
719 PendingRequestInfo
& pending_request
= index
->second
;
720 if (pending_request
.is_deferred
) {
721 pending_request
.deferred_message_queue
.swap(q
);
728 ResourceLoaderBridge
* ResourceDispatcher::CreateBridge(
729 const RequestInfo
& request_info
) {
730 return new IPCResourceLoaderBridge(this, request_info
);
733 void ResourceDispatcher::ToResourceResponseInfo(
734 const PendingRequestInfo
& request_info
,
735 const ResourceResponseHead
& browser_info
,
736 ResourceResponseInfo
* renderer_info
) const {
737 *renderer_info
= browser_info
;
738 if (request_info
.request_start
.is_null() ||
739 request_info
.response_start
.is_null() ||
740 browser_info
.request_start
.is_null() ||
741 browser_info
.response_start
.is_null() ||
742 browser_info
.load_timing
.request_start
.is_null()) {
745 InterProcessTimeTicksConverter
converter(
746 LocalTimeTicks::FromTimeTicks(request_info
.request_start
),
747 LocalTimeTicks::FromTimeTicks(request_info
.response_start
),
748 RemoteTimeTicks::FromTimeTicks(browser_info
.request_start
),
749 RemoteTimeTicks::FromTimeTicks(browser_info
.response_start
));
751 net::LoadTimingInfo
* load_timing
= &renderer_info
->load_timing
;
752 RemoteToLocalTimeTicks(converter
, &load_timing
->request_start
);
753 RemoteToLocalTimeTicks(converter
, &load_timing
->proxy_resolve_start
);
754 RemoteToLocalTimeTicks(converter
, &load_timing
->proxy_resolve_end
);
755 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.dns_start
);
756 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.dns_end
);
757 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.connect_start
);
758 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.connect_end
);
759 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.ssl_start
);
760 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.ssl_end
);
761 RemoteToLocalTimeTicks(converter
, &load_timing
->send_start
);
762 RemoteToLocalTimeTicks(converter
, &load_timing
->send_end
);
763 RemoteToLocalTimeTicks(converter
, &load_timing
->receive_headers_end
);
766 base::TimeTicks
ResourceDispatcher::ToRendererCompletionTime(
767 const PendingRequestInfo
& request_info
,
768 const base::TimeTicks
& browser_completion_time
) const {
769 if (request_info
.completion_time
.is_null()) {
770 return browser_completion_time
;
773 // TODO(simonjam): The optimal lower bound should be the most recent value of
774 // TimeTicks::Now() returned to WebKit. Is it worth trying to cache that?
775 // Until then, |response_start| is used as it is the most recent value
776 // returned for this request.
777 int64 result
= std::max(browser_completion_time
.ToInternalValue(),
778 request_info
.response_start
.ToInternalValue());
779 result
= std::min(result
, request_info
.completion_time
.ToInternalValue());
780 return base::TimeTicks::FromInternalValue(result
);
783 base::TimeTicks
ResourceDispatcher::ConsumeIOTimestamp() {
784 if (io_timestamp_
== base::TimeTicks())
785 return base::TimeTicks::Now();
786 base::TimeTicks result
= io_timestamp_
;
787 io_timestamp_
= base::TimeTicks();
792 bool ResourceDispatcher::IsResourceDispatcherMessage(
793 const IPC::Message
& message
) {
794 switch (message
.type()) {
795 case ResourceMsg_UploadProgress::ID
:
796 case ResourceMsg_ReceivedResponse::ID
:
797 case ResourceMsg_ReceivedCachedMetadata::ID
:
798 case ResourceMsg_ReceivedRedirect::ID
:
799 case ResourceMsg_SetDataBuffer::ID
:
800 case ResourceMsg_DataReceived::ID
:
801 case ResourceMsg_DataDownloaded::ID
:
802 case ResourceMsg_RequestComplete::ID
:
813 void ResourceDispatcher::ReleaseResourcesInDataMessage(
814 const IPC::Message
& message
) {
815 PickleIterator
iter(message
);
817 if (!message
.ReadInt(&iter
, &request_id
)) {
818 NOTREACHED() << "malformed resource message";
822 // If the message contains a shared memory handle, we should close the handle
823 // or there will be a memory leak.
824 if (message
.type() == ResourceMsg_SetDataBuffer::ID
) {
825 base::SharedMemoryHandle shm_handle
;
826 if (IPC::ParamTraits
<base::SharedMemoryHandle
>::Read(&message
,
829 if (base::SharedMemory::IsHandleValid(shm_handle
))
830 base::SharedMemory::CloseHandle(shm_handle
);
836 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue
* queue
) {
837 while (!queue
->empty()) {
838 IPC::Message
* message
= queue
->front();
839 ReleaseResourcesInDataMessage(*message
);
845 } // namespace content