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/debug/dump_without_crashing.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/shared_memory.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/metrics/histogram.h"
18 #include "base/strings/string_util.h"
19 #include "content/child/request_extra_data.h"
20 #include "content/child/request_info.h"
21 #include "content/child/shared_memory_received_data_factory.h"
22 #include "content/child/site_isolation_stats_gatherer.h"
23 #include "content/child/sync_load_response.h"
24 #include "content/child/threaded_data_provider.h"
25 #include "content/common/inter_process_time_ticks_converter.h"
26 #include "content/common/resource_messages.h"
27 #include "content/public/child/fixed_received_data.h"
28 #include "content/public/child/request_peer.h"
29 #include "content/public/child/resource_dispatcher_delegate.h"
30 #include "content/public/common/resource_response.h"
31 #include "content/public/common/resource_type.h"
32 #include "net/base/net_errors.h"
33 #include "net/base/net_util.h"
34 #include "net/base/request_priority.h"
35 #include "net/http/http_response_headers.h"
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 ResourceDispatcher::ResourceDispatcher(
71 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_task_runner
)
72 : message_sender_(sender
),
74 io_timestamp_(base::TimeTicks()),
75 main_thread_task_runner_(main_thread_task_runner
),
79 ResourceDispatcher::~ResourceDispatcher() {
82 bool ResourceDispatcher::OnMessageReceived(const IPC::Message
& message
) {
83 if (!IsResourceDispatcherMessage(message
)) {
89 base::PickleIterator
iter(message
);
90 if (!iter
.ReadInt(&request_id
)) {
91 NOTREACHED() << "malformed resource message";
95 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
97 // Release resources in the message if it is a data message.
98 ReleaseResourcesInDataMessage(message
);
102 if (request_info
->is_deferred
) {
103 request_info
->deferred_message_queue
.push_back(new IPC::Message(message
));
106 // Make sure any deferred messages are dispatched before we dispatch more.
107 if (!request_info
->deferred_message_queue
.empty()) {
108 FlushDeferredMessages(request_id
);
109 // The request could have been deferred now. If yes then the current
110 // message has to be queued up. The request_info instance should remain
111 // valid here as there are pending messages for it.
112 DCHECK(pending_requests_
.find(request_id
) != pending_requests_
.end());
113 if (request_info
->is_deferred
) {
114 request_info
->deferred_message_queue
.push_back(new IPC::Message(message
));
119 DispatchMessage(message
);
123 ResourceDispatcher::PendingRequestInfo
*
124 ResourceDispatcher::GetPendingRequestInfo(int request_id
) {
125 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
126 if (it
== pending_requests_
.end()) {
127 // This might happen for kill()ed requests on the webkit end.
130 return &(it
->second
);
133 void ResourceDispatcher::OnUploadProgress(int request_id
, int64 position
,
135 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
139 request_info
->peer
->OnUploadProgress(position
, size
);
141 // Acknowledge receipt
142 message_sender_
->Send(new ResourceHostMsg_UploadProgress_ACK(request_id
));
145 void ResourceDispatcher::OnReceivedResponse(
146 int request_id
, const ResourceResponseHead
& response_head
) {
147 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedResponse");
148 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
151 request_info
->response_start
= ConsumeIOTimestamp();
154 RequestPeer
* new_peer
=
155 delegate_
->OnReceivedResponse(
156 request_info
->peer
, response_head
.mime_type
, request_info
->url
);
158 request_info
->peer
= new_peer
;
161 ResourceResponseInfo renderer_response_info
;
162 ToResourceResponseInfo(*request_info
, response_head
, &renderer_response_info
);
163 request_info
->site_isolation_metadata
=
164 SiteIsolationStatsGatherer::OnReceivedResponse(
165 request_info
->frame_origin
, request_info
->response_url
,
166 request_info
->resource_type
, request_info
->origin_pid
,
167 renderer_response_info
);
168 request_info
->peer
->OnReceivedResponse(renderer_response_info
);
171 void ResourceDispatcher::OnReceivedCachedMetadata(
172 int request_id
, const std::vector
<char>& data
) {
173 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
178 request_info
->peer
->OnReceivedCachedMetadata(&data
.front(), data
.size());
181 void ResourceDispatcher::OnSetDataBuffer(int request_id
,
182 base::SharedMemoryHandle shm_handle
,
184 base::ProcessId renderer_pid
) {
185 TRACE_EVENT0("loader", "ResourceDispatcher::OnSetDataBuffer");
186 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
190 bool shm_valid
= base::SharedMemory::IsHandleValid(shm_handle
);
191 CHECK((shm_valid
&& shm_size
> 0) || (!shm_valid
&& !shm_size
));
193 request_info
->buffer
.reset(
194 new base::SharedMemory(shm_handle
, true)); // read only
195 request_info
->received_data_factory
=
196 make_scoped_refptr(new SharedMemoryReceivedDataFactory(
197 message_sender_
, request_id
, request_info
->buffer
));
199 bool ok
= request_info
->buffer
->Map(shm_size
);
201 // Added to help debug crbug/160401.
202 base::ProcessId renderer_pid_copy
= renderer_pid
;
203 base::debug::Alias(&renderer_pid_copy
);
205 base::SharedMemoryHandle shm_handle_copy
= shm_handle
;
206 base::debug::Alias(&shm_handle_copy
);
212 request_info
->buffer_size
= shm_size
;
215 void ResourceDispatcher::OnReceivedData(int request_id
,
218 int encoded_data_length
) {
219 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedData");
220 DCHECK_GT(data_length
, 0);
221 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
222 bool send_ack
= true;
223 if (request_info
&& data_length
> 0) {
224 CHECK(base::SharedMemory::IsHandleValid(request_info
->buffer
->handle()));
225 CHECK_GE(request_info
->buffer_size
, data_offset
+ data_length
);
227 // Ensure that the SHM buffer remains valid for the duration of this scope.
228 // It is possible for Cancel() to be called before we exit this scope.
229 // SharedMemoryReceivedDataFactory stores the SHM buffer inside it.
230 scoped_refptr
<SharedMemoryReceivedDataFactory
> factory(
231 request_info
->received_data_factory
);
233 base::TimeTicks time_start
= base::TimeTicks::Now();
235 const char* data_start
= static_cast<char*>(request_info
->buffer
->memory());
237 CHECK(data_start
+ data_offset
);
238 const char* data_ptr
= data_start
+ data_offset
;
240 // Check whether this response data is compliant with our cross-site
241 // document blocking policy. We only do this for the first chunk of data.
242 if (request_info
->site_isolation_metadata
.get()) {
243 SiteIsolationStatsGatherer::OnReceivedFirstChunk(
244 request_info
->site_isolation_metadata
, data_ptr
, data_length
);
245 request_info
->site_isolation_metadata
.reset();
248 if (request_info
->threaded_data_provider
) {
249 // A threaded data provider will take care of its own ACKing, as the data
250 // may be processed later on another thread.
252 request_info
->threaded_data_provider
->OnReceivedDataOnForegroundThread(
253 data_ptr
, data_length
, encoded_data_length
);
255 scoped_ptr
<RequestPeer::ReceivedData
> data
=
256 factory
->Create(data_offset
, data_length
, encoded_data_length
);
257 // |data| takes care of ACKing.
259 request_info
->peer
->OnReceivedData(data
.Pass());
262 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime",
263 base::TimeTicks::Now() - time_start
);
266 // Acknowledge the reception of this data.
268 message_sender_
->Send(new ResourceHostMsg_DataReceived_ACK(request_id
));
271 void ResourceDispatcher::OnDownloadedData(int request_id
,
273 int encoded_data_length
) {
274 // Acknowledge the reception of this message.
275 message_sender_
->Send(new ResourceHostMsg_DataDownloaded_ACK(request_id
));
277 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
281 request_info
->peer
->OnDownloadedData(data_len
, encoded_data_length
);
284 void ResourceDispatcher::OnReceivedRedirect(
286 const net::RedirectInfo
& redirect_info
,
287 const ResourceResponseHead
& response_head
) {
288 TRACE_EVENT0("loader", "ResourceDispatcher::OnReceivedRedirect");
289 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
292 request_info
->response_start
= ConsumeIOTimestamp();
294 ResourceResponseInfo renderer_response_info
;
295 ToResourceResponseInfo(*request_info
, response_head
, &renderer_response_info
);
296 if (request_info
->peer
->OnReceivedRedirect(redirect_info
,
297 renderer_response_info
)) {
298 // Double-check if the request is still around. The call above could
299 // potentially remove it.
300 request_info
= GetPendingRequestInfo(request_id
);
303 // We update the response_url here so that we can send it to
304 // SiteIsolationStatsGatherer later when OnReceivedResponse is called.
305 request_info
->response_url
= redirect_info
.new_url
;
306 request_info
->pending_redirect_message
.reset(
307 new ResourceHostMsg_FollowRedirect(request_id
));
308 if (!request_info
->is_deferred
) {
309 FollowPendingRedirect(request_id
, *request_info
);
316 void ResourceDispatcher::FollowPendingRedirect(
318 PendingRequestInfo
& request_info
) {
319 IPC::Message
* msg
= request_info
.pending_redirect_message
.release();
321 message_sender_
->Send(msg
);
324 void ResourceDispatcher::OnRequestComplete(
326 const ResourceMsg_RequestCompleteData
& request_complete_data
) {
327 TRACE_EVENT0("loader", "ResourceDispatcher::OnRequestComplete");
329 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
332 request_info
->completion_time
= ConsumeIOTimestamp();
333 request_info
->buffer
.reset();
334 if (request_info
->received_data_factory
)
335 request_info
->received_data_factory
->Stop();
336 request_info
->received_data_factory
= nullptr;
337 request_info
->buffer_size
= 0;
339 RequestPeer
* peer
= request_info
->peer
;
342 RequestPeer
* new_peer
=
343 delegate_
->OnRequestComplete(
344 request_info
->peer
, request_info
->resource_type
,
345 request_complete_data
.error_code
);
347 request_info
->peer
= new_peer
;
350 base::TimeTicks renderer_completion_time
= ToRendererCompletionTime(
351 *request_info
, request_complete_data
.completion_time
);
353 // If we have a threaded data provider, this message needs to bounce off the
354 // background thread before it's returned to this thread and handled,
355 // to make sure it's processed after all incoming data.
356 if (request_info
->threaded_data_provider
) {
357 request_info
->threaded_data_provider
->OnRequestCompleteForegroundThread(
358 weak_factory_
.GetWeakPtr(), request_complete_data
,
359 renderer_completion_time
);
363 // The request ID will be removed from our pending list in the destructor.
364 // Normally, dispatching this message causes the reference-counted request to
366 peer
->OnCompletedRequest(request_complete_data
.error_code
,
367 request_complete_data
.was_ignored_by_handler
,
368 request_complete_data
.exists_in_cache
,
369 request_complete_data
.security_info
,
370 renderer_completion_time
,
371 request_complete_data
.encoded_data_length
);
374 void ResourceDispatcher::CompletedRequestAfterBackgroundThreadFlush(
376 const ResourceMsg_RequestCompleteData
& request_complete_data
,
377 const base::TimeTicks
& renderer_completion_time
) {
378 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
382 RequestPeer
* peer
= request_info
->peer
;
383 peer
->OnCompletedRequest(request_complete_data
.error_code
,
384 request_complete_data
.was_ignored_by_handler
,
385 request_complete_data
.exists_in_cache
,
386 request_complete_data
.security_info
,
387 renderer_completion_time
,
388 request_complete_data
.encoded_data_length
);
391 bool ResourceDispatcher::RemovePendingRequest(int request_id
) {
392 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
393 if (it
== pending_requests_
.end())
396 PendingRequestInfo
& request_info
= it
->second
;
398 bool release_downloaded_file
= request_info
.download_to_file
;
400 ReleaseResourcesInMessageQueue(&request_info
.deferred_message_queue
);
401 pending_requests_
.erase(it
);
403 if (release_downloaded_file
) {
404 message_sender_
->Send(
405 new ResourceHostMsg_ReleaseDownloadedFile(request_id
));
411 void ResourceDispatcher::Cancel(int request_id
) {
412 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
413 if (it
== pending_requests_
.end()) {
414 DVLOG(1) << "unknown request";
418 // Cancel the request, and clean it up so the bridge will receive no more
420 message_sender_
->Send(new ResourceHostMsg_CancelRequest(request_id
));
421 RemovePendingRequest(request_id
);
424 void ResourceDispatcher::SetDefersLoading(int request_id
, bool value
) {
425 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
426 if (it
== pending_requests_
.end()) {
427 DLOG(ERROR
) << "unknown request";
430 PendingRequestInfo
& request_info
= it
->second
;
432 request_info
.is_deferred
= value
;
433 } else if (request_info
.is_deferred
) {
434 request_info
.is_deferred
= false;
436 FollowPendingRedirect(request_id
, request_info
);
438 main_thread_task_runner_
->PostTask(
439 FROM_HERE
, base::Bind(&ResourceDispatcher::FlushDeferredMessages
,
440 weak_factory_
.GetWeakPtr(), request_id
));
444 void ResourceDispatcher::DidChangePriority(int request_id
,
445 net::RequestPriority new_priority
,
446 int intra_priority_value
) {
447 DCHECK(ContainsKey(pending_requests_
, request_id
));
448 message_sender_
->Send(new ResourceHostMsg_DidChangePriority(
449 request_id
, new_priority
, intra_priority_value
));
452 bool ResourceDispatcher::AttachThreadedDataReceiver(
453 int request_id
, blink::WebThreadedDataReceiver
* threaded_data_receiver
) {
454 PendingRequestInfo
* request_info
= GetPendingRequestInfo(request_id
);
455 DCHECK(request_info
);
457 if (request_info
->buffer
!= NULL
) {
458 DCHECK(!request_info
->threaded_data_provider
);
459 request_info
->threaded_data_provider
= new ThreadedDataProvider(
460 request_id
, threaded_data_receiver
, request_info
->buffer
,
461 request_info
->buffer_size
, main_thread_task_runner_
);
468 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo()
470 threaded_data_provider(NULL
),
471 resource_type(RESOURCE_TYPE_SUB_RESOURCE
),
473 download_to_file(false),
477 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo(
479 ResourceType resource_type
,
481 const GURL
& frame_origin
,
482 const GURL
& request_url
,
483 bool download_to_file
)
485 threaded_data_provider(NULL
),
486 resource_type(resource_type
),
487 origin_pid(origin_pid
),
490 frame_origin(frame_origin
),
491 response_url(request_url
),
492 download_to_file(download_to_file
),
493 request_start(base::TimeTicks::Now()) {
496 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() {
497 if (threaded_data_provider
)
498 threaded_data_provider
->Stop();
501 void ResourceDispatcher::DispatchMessage(const IPC::Message
& message
) {
502 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher
, message
)
503 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress
, OnUploadProgress
)
504 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse
, OnReceivedResponse
)
505 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata
,
506 OnReceivedCachedMetadata
)
507 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedRedirect
, OnReceivedRedirect
)
508 IPC_MESSAGE_HANDLER(ResourceMsg_SetDataBuffer
, OnSetDataBuffer
)
509 IPC_MESSAGE_HANDLER(ResourceMsg_DataReceived
, OnReceivedData
)
510 IPC_MESSAGE_HANDLER(ResourceMsg_DataDownloaded
, OnDownloadedData
)
511 IPC_MESSAGE_HANDLER(ResourceMsg_RequestComplete
, OnRequestComplete
)
512 IPC_END_MESSAGE_MAP()
515 void ResourceDispatcher::FlushDeferredMessages(int request_id
) {
516 PendingRequestList::iterator it
= pending_requests_
.find(request_id
);
517 if (it
== pending_requests_
.end()) // The request could have become invalid.
519 PendingRequestInfo
& request_info
= it
->second
;
520 if (request_info
.is_deferred
)
522 // Because message handlers could result in request_info being destroyed,
523 // we need to work with a stack reference to the deferred queue.
525 q
.swap(request_info
.deferred_message_queue
);
527 IPC::Message
* m
= q
.front();
531 // If this request is deferred in the context of the above message, then
532 // we should honor the same and stop dispatching further messages.
533 // We need to find the request again in the list as it may have completed
534 // by now and the request_info instance above may be invalid.
535 PendingRequestList::iterator index
= pending_requests_
.find(request_id
);
536 if (index
!= pending_requests_
.end()) {
537 PendingRequestInfo
& pending_request
= index
->second
;
538 if (pending_request
.is_deferred
) {
539 pending_request
.deferred_message_queue
.swap(q
);
546 void ResourceDispatcher::StartSync(const RequestInfo
& request_info
,
547 ResourceRequestBody
* request_body
,
548 SyncLoadResponse
* response
) {
549 scoped_ptr
<ResourceHostMsg_Request
> request
=
550 CreateRequest(request_info
, request_body
, NULL
);
552 SyncLoadResult result
;
553 IPC::SyncMessage
* msg
= new ResourceHostMsg_SyncLoad(
554 request_info
.routing_id
, MakeRequestID(), *request
, &result
);
556 // NOTE: This may pump events (see RenderThread::Send).
557 if (!message_sender_
->Send(msg
)) {
558 response
->error_code
= net::ERR_FAILED
;
562 response
->error_code
= result
.error_code
;
563 response
->url
= result
.final_url
;
564 response
->headers
= result
.headers
;
565 response
->mime_type
= result
.mime_type
;
566 response
->charset
= result
.charset
;
567 response
->request_time
= result
.request_time
;
568 response
->response_time
= result
.response_time
;
569 response
->encoded_data_length
= result
.encoded_data_length
;
570 response
->load_timing
= result
.load_timing
;
571 response
->devtools_info
= result
.devtools_info
;
572 response
->data
.swap(result
.data
);
573 response
->download_file_path
= result
.download_file_path
;
576 int ResourceDispatcher::StartAsync(const RequestInfo
& request_info
,
577 ResourceRequestBody
* request_body
,
580 scoped_ptr
<ResourceHostMsg_Request
> request
=
581 CreateRequest(request_info
, request_body
, &frame_origin
);
583 // Compute a unique request_id for this renderer process.
584 int request_id
= MakeRequestID();
585 pending_requests_
[request_id
] =
586 PendingRequestInfo(peer
,
587 request
->resource_type
,
591 request_info
.download_to_file
);
593 message_sender_
->Send(new ResourceHostMsg_RequestResource(
594 request_info
.routing_id
, request_id
, *request
));
599 void ResourceDispatcher::ToResourceResponseInfo(
600 const PendingRequestInfo
& request_info
,
601 const ResourceResponseHead
& browser_info
,
602 ResourceResponseInfo
* renderer_info
) const {
603 *renderer_info
= browser_info
;
604 if (request_info
.request_start
.is_null() ||
605 request_info
.response_start
.is_null() ||
606 browser_info
.request_start
.is_null() ||
607 browser_info
.response_start
.is_null() ||
608 browser_info
.load_timing
.request_start
.is_null()) {
611 InterProcessTimeTicksConverter
converter(
612 LocalTimeTicks::FromTimeTicks(request_info
.request_start
),
613 LocalTimeTicks::FromTimeTicks(request_info
.response_start
),
614 RemoteTimeTicks::FromTimeTicks(browser_info
.request_start
),
615 RemoteTimeTicks::FromTimeTicks(browser_info
.response_start
));
617 net::LoadTimingInfo
* load_timing
= &renderer_info
->load_timing
;
618 RemoteToLocalTimeTicks(converter
, &load_timing
->request_start
);
619 RemoteToLocalTimeTicks(converter
, &load_timing
->proxy_resolve_start
);
620 RemoteToLocalTimeTicks(converter
, &load_timing
->proxy_resolve_end
);
621 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.dns_start
);
622 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.dns_end
);
623 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.connect_start
);
624 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.connect_end
);
625 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.ssl_start
);
626 RemoteToLocalTimeTicks(converter
, &load_timing
->connect_timing
.ssl_end
);
627 RemoteToLocalTimeTicks(converter
, &load_timing
->send_start
);
628 RemoteToLocalTimeTicks(converter
, &load_timing
->send_end
);
629 RemoteToLocalTimeTicks(converter
, &load_timing
->receive_headers_end
);
630 RemoteToLocalTimeTicks(converter
, &renderer_info
->service_worker_start_time
);
631 RemoteToLocalTimeTicks(converter
, &renderer_info
->service_worker_ready_time
);
633 // Collect UMA on the inter-process skew.
634 bool is_skew_additive
= false;
635 if (converter
.IsSkewAdditiveForMetrics()) {
636 is_skew_additive
= true;
637 base::TimeDelta skew
= converter
.GetSkewForMetrics();
638 if (skew
>= base::TimeDelta()) {
640 "InterProcessTimeTicks.BrowserAhead_BrowserToRenderer", skew
);
643 "InterProcessTimeTicks.BrowserBehind_BrowserToRenderer", -skew
);
646 UMA_HISTOGRAM_BOOLEAN(
647 "InterProcessTimeTicks.IsSkewAdditive_BrowserToRenderer",
651 base::TimeTicks
ResourceDispatcher::ToRendererCompletionTime(
652 const PendingRequestInfo
& request_info
,
653 const base::TimeTicks
& browser_completion_time
) const {
654 if (request_info
.completion_time
.is_null()) {
655 return browser_completion_time
;
658 // TODO(simonjam): The optimal lower bound should be the most recent value of
659 // TimeTicks::Now() returned to WebKit. Is it worth trying to cache that?
660 // Until then, |response_start| is used as it is the most recent value
661 // returned for this request.
662 int64 result
= std::max(browser_completion_time
.ToInternalValue(),
663 request_info
.response_start
.ToInternalValue());
664 result
= std::min(result
, request_info
.completion_time
.ToInternalValue());
665 return base::TimeTicks::FromInternalValue(result
);
668 base::TimeTicks
ResourceDispatcher::ConsumeIOTimestamp() {
669 if (io_timestamp_
== base::TimeTicks())
670 return base::TimeTicks::Now();
671 base::TimeTicks result
= io_timestamp_
;
672 io_timestamp_
= base::TimeTicks();
677 bool ResourceDispatcher::IsResourceDispatcherMessage(
678 const IPC::Message
& message
) {
679 switch (message
.type()) {
680 case ResourceMsg_UploadProgress::ID
:
681 case ResourceMsg_ReceivedResponse::ID
:
682 case ResourceMsg_ReceivedCachedMetadata::ID
:
683 case ResourceMsg_ReceivedRedirect::ID
:
684 case ResourceMsg_SetDataBuffer::ID
:
685 case ResourceMsg_DataReceived::ID
:
686 case ResourceMsg_DataDownloaded::ID
:
687 case ResourceMsg_RequestComplete::ID
:
698 void ResourceDispatcher::ReleaseResourcesInDataMessage(
699 const IPC::Message
& message
) {
700 base::PickleIterator
iter(message
);
702 if (!iter
.ReadInt(&request_id
)) {
703 NOTREACHED() << "malformed resource message";
707 // If the message contains a shared memory handle, we should close the handle
708 // or there will be a memory leak.
709 if (message
.type() == ResourceMsg_SetDataBuffer::ID
) {
710 base::SharedMemoryHandle shm_handle
;
711 if (IPC::ParamTraits
<base::SharedMemoryHandle
>::Read(&message
,
714 if (base::SharedMemory::IsHandleValid(shm_handle
))
715 base::SharedMemory::CloseHandle(shm_handle
);
721 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue
* queue
) {
722 while (!queue
->empty()) {
723 IPC::Message
* message
= queue
->front();
724 ReleaseResourcesInDataMessage(*message
);
730 scoped_ptr
<ResourceHostMsg_Request
> ResourceDispatcher::CreateRequest(
731 const RequestInfo
& request_info
,
732 ResourceRequestBody
* request_body
,
733 GURL
* frame_origin
) {
734 scoped_ptr
<ResourceHostMsg_Request
> request(new ResourceHostMsg_Request
);
735 request
->method
= request_info
.method
;
736 request
->url
= request_info
.url
;
737 request
->first_party_for_cookies
= request_info
.first_party_for_cookies
;
738 request
->referrer
= request_info
.referrer
.url
;
739 request
->referrer_policy
= request_info
.referrer
.policy
;
740 request
->headers
= request_info
.headers
;
741 request
->load_flags
= request_info
.load_flags
;
742 request
->origin_pid
= request_info
.requestor_pid
;
743 request
->resource_type
= request_info
.request_type
;
744 request
->priority
= request_info
.priority
;
745 request
->request_context
= request_info
.request_context
;
746 request
->appcache_host_id
= request_info
.appcache_host_id
;
747 request
->download_to_file
= request_info
.download_to_file
;
748 request
->has_user_gesture
= request_info
.has_user_gesture
;
749 request
->skip_service_worker
= request_info
.skip_service_worker
;
750 request
->should_reset_appcache
= request_info
.should_reset_appcache
;
751 request
->fetch_request_mode
= request_info
.fetch_request_mode
;
752 request
->fetch_credentials_mode
= request_info
.fetch_credentials_mode
;
753 request
->fetch_redirect_mode
= request_info
.fetch_redirect_mode
;
754 request
->fetch_request_context_type
= request_info
.fetch_request_context_type
;
755 request
->fetch_frame_type
= request_info
.fetch_frame_type
;
756 request
->enable_load_timing
= request_info
.enable_load_timing
;
757 request
->enable_upload_progress
= request_info
.enable_upload_progress
;
758 request
->do_not_prompt_for_login
= request_info
.do_not_prompt_for_login
;
759 request
->report_raw_headers
= request_info
.report_raw_headers
;
761 if ((request_info
.referrer
.policy
== blink::WebReferrerPolicyDefault
||
762 request_info
.referrer
.policy
==
763 blink::WebReferrerPolicyNoReferrerWhenDowngrade
) &&
764 request_info
.referrer
.url
.SchemeIsCryptographic() &&
765 !request_info
.url
.SchemeIsCryptographic()) {
766 LOG(FATAL
) << "Trying to send secure referrer for insecure request "
767 << "without an appropriate referrer policy.\n"
768 << "URL = " << request_info
.url
<< "\n"
769 << "Referrer = " << request_info
.referrer
.url
;
772 const RequestExtraData kEmptyData
;
773 const RequestExtraData
* extra_data
;
774 if (request_info
.extra_data
)
775 extra_data
= static_cast<RequestExtraData
*>(request_info
.extra_data
);
777 extra_data
= &kEmptyData
;
778 request
->visiblity_state
= extra_data
->visibility_state();
779 request
->render_frame_id
= extra_data
->render_frame_id();
780 request
->is_main_frame
= extra_data
->is_main_frame();
781 request
->parent_is_main_frame
= extra_data
->parent_is_main_frame();
782 request
->parent_render_frame_id
= extra_data
->parent_render_frame_id();
783 request
->allow_download
= extra_data
->allow_download();
784 request
->transition_type
= extra_data
->transition_type();
785 request
->should_replace_current_entry
=
786 extra_data
->should_replace_current_entry();
787 request
->transferred_request_child_id
=
788 extra_data
->transferred_request_child_id();
789 request
->transferred_request_request_id
=
790 extra_data
->transferred_request_request_id();
791 request
->service_worker_provider_id
=
792 extra_data
->service_worker_provider_id();
793 request
->request_body
= request_body
;
795 *frame_origin
= extra_data
->frame_origin();
796 return request
.Pass();
799 } // namespace content