[content shell] hook up testRunner.dumpEditingCallbacks
[chromium-blink-merge.git] / content / common / resource_dispatcher.cc
blob2ef4b98616d684112406c226127a8b206e3a8fc6
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/common/resource_dispatcher.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/compiler_specific.h"
12 #include "base/file_path.h"
13 #include "base/message_loop.h"
14 #include "base/metrics/histogram.h"
15 #include "base/shared_memory.h"
16 #include "base/string_util.h"
17 #include "content/common/inter_process_time_ticks_converter.h"
18 #include "content/common/request_extra_data.h"
19 #include "content/common/resource_messages.h"
20 #include "content/public/common/resource_dispatcher_delegate.h"
21 #include "content/public/common/resource_response.h"
22 #include "net/base/net_errors.h"
23 #include "net/base/net_util.h"
24 #include "net/http/http_response_headers.h"
25 #include "webkit/glue/resource_request_body.h"
26 #include "webkit/glue/resource_type.h"
28 using webkit_glue::ResourceLoaderBridge;
29 using webkit_glue::ResourceRequestBody;
30 using webkit_glue::ResourceResponseInfo;
32 namespace content {
34 // Each resource request is assigned an ID scoped to this process.
35 static int MakeRequestID() {
36 // NOTE: The resource_dispatcher_host also needs probably unique
37 // request_ids, so they count down from -2 (-1 is a special we're
38 // screwed value), while the renderer process counts up.
39 static int next_request_id = 0;
40 return next_request_id++;
43 // ResourceLoaderBridge implementation ----------------------------------------
45 class IPCResourceLoaderBridge : public ResourceLoaderBridge {
46 public:
47 IPCResourceLoaderBridge(ResourceDispatcher* dispatcher,
48 const ResourceLoaderBridge::RequestInfo& request_info);
49 virtual ~IPCResourceLoaderBridge();
51 // ResourceLoaderBridge
52 virtual void SetRequestBody(ResourceRequestBody* request_body);
53 virtual bool Start(Peer* peer);
54 virtual void Cancel();
55 virtual void SetDefersLoading(bool value);
56 virtual void SyncLoad(SyncLoadResponse* response);
58 private:
59 ResourceLoaderBridge::Peer* peer_;
61 // The resource dispatcher for this loader. The bridge doesn't own it, but
62 // it's guaranteed to outlive the bridge.
63 ResourceDispatcher* dispatcher_;
65 // The request to send, created on initialization for modification and
66 // appending data.
67 ResourceHostMsg_Request request_;
69 // ID for the request, valid once Start()ed, -1 if not valid yet.
70 int request_id_;
72 // The routing id used when sending IPC messages.
73 int routing_id_;
75 bool is_synchronous_request_;
78 IPCResourceLoaderBridge::IPCResourceLoaderBridge(
79 ResourceDispatcher* dispatcher,
80 const ResourceLoaderBridge::RequestInfo& request_info)
81 : peer_(NULL),
82 dispatcher_(dispatcher),
83 request_id_(-1),
84 routing_id_(request_info.routing_id),
85 is_synchronous_request_(false) {
86 DCHECK(dispatcher_) << "no resource dispatcher";
87 request_.method = request_info.method;
88 request_.url = request_info.url;
89 request_.first_party_for_cookies = request_info.first_party_for_cookies;
90 request_.referrer = request_info.referrer;
91 request_.referrer_policy = request_info.referrer_policy;
92 request_.headers = request_info.headers;
93 request_.load_flags = request_info.load_flags;
94 request_.origin_pid = request_info.requestor_pid;
95 request_.resource_type = request_info.request_type;
96 request_.request_context = request_info.request_context;
97 request_.appcache_host_id = request_info.appcache_host_id;
98 request_.download_to_file = request_info.download_to_file;
99 request_.has_user_gesture = request_info.has_user_gesture;
100 if (request_info.extra_data) {
101 RequestExtraData* extra_data =
102 static_cast<RequestExtraData*>(request_info.extra_data);
103 request_.is_main_frame = extra_data->is_main_frame();
104 request_.frame_id = extra_data->frame_id();
105 request_.parent_is_main_frame = extra_data->parent_is_main_frame();
106 request_.parent_frame_id = extra_data->parent_frame_id();
107 request_.allow_download = extra_data->allow_download();
108 request_.transition_type = extra_data->transition_type();
109 request_.transferred_request_child_id =
110 extra_data->transferred_request_child_id();
111 request_.transferred_request_request_id =
112 extra_data->transferred_request_request_id();
113 } else {
114 request_.is_main_frame = false;
115 request_.frame_id = -1;
116 request_.parent_is_main_frame = false;
117 request_.parent_frame_id = -1;
118 request_.allow_download = true;
119 request_.transition_type = PAGE_TRANSITION_LINK;
120 request_.transferred_request_child_id = -1;
121 request_.transferred_request_request_id = -1;
125 IPCResourceLoaderBridge::~IPCResourceLoaderBridge() {
126 // we remove our hook for the resource dispatcher only when going away, since
127 // it doesn't keep track of whether we've force terminated the request
128 if (request_id_ >= 0) {
129 // this operation may fail, as the dispatcher will have preemptively
130 // removed us when the renderer sends the ReceivedAllData message.
131 dispatcher_->RemovePendingRequest(request_id_);
133 if (request_.download_to_file) {
134 dispatcher_->message_sender()->Send(
135 new ResourceHostMsg_ReleaseDownloadedFile(request_id_));
140 void IPCResourceLoaderBridge::SetRequestBody(
141 ResourceRequestBody* request_body) {
142 DCHECK(request_id_ == -1) << "request already started";
143 request_.request_body = request_body;
146 // Writes a footer on the message and sends it
147 bool IPCResourceLoaderBridge::Start(Peer* peer) {
148 if (request_id_ != -1) {
149 NOTREACHED() << "Starting a request twice";
150 return false;
153 peer_ = peer;
155 // generate the request ID, and append it to the message
156 request_id_ = dispatcher_->AddPendingRequest(
157 peer_, request_.resource_type, request_.url);
159 return dispatcher_->message_sender()->Send(
160 new ResourceHostMsg_RequestResource(routing_id_, request_id_, request_));
163 void IPCResourceLoaderBridge::Cancel() {
164 if (request_id_ < 0) {
165 NOTREACHED() << "Trying to cancel an unstarted request";
166 return;
169 if (!is_synchronous_request_)
170 dispatcher_->CancelPendingRequest(routing_id_, request_id_);
172 // We can't remove the request ID from the resource dispatcher because more
173 // data might be pending. Sending the cancel message may cause more data
174 // to be flushed, and will then cause a complete message to be sent.
177 void IPCResourceLoaderBridge::SetDefersLoading(bool value) {
178 if (request_id_ < 0) {
179 NOTREACHED() << "Trying to (un)defer an unstarted request";
180 return;
183 dispatcher_->SetDefersLoading(request_id_, value);
186 void IPCResourceLoaderBridge::SyncLoad(SyncLoadResponse* response) {
187 if (request_id_ != -1) {
188 NOTREACHED() << "Starting a request twice";
189 response->error_code = net::ERR_FAILED;
190 return;
193 request_id_ = MakeRequestID();
194 is_synchronous_request_ = true;
196 SyncLoadResult result;
197 IPC::SyncMessage* msg = new ResourceHostMsg_SyncLoad(routing_id_, request_id_,
198 request_, &result);
199 // NOTE: This may pump events (see RenderThread::Send).
200 if (!dispatcher_->message_sender()->Send(msg)) {
201 response->error_code = net::ERR_FAILED;
202 return;
205 response->error_code = result.error_code;
206 response->url = result.final_url;
207 response->headers = result.headers;
208 response->mime_type = result.mime_type;
209 response->charset = result.charset;
210 response->request_time = result.request_time;
211 response->response_time = result.response_time;
212 response->encoded_data_length = result.encoded_data_length;
213 response->connection_id = result.connection_id;
214 response->connection_reused = result.connection_reused;
215 response->load_timing = result.load_timing;
216 response->devtools_info = result.devtools_info;
217 response->data.swap(result.data);
218 response->download_file_path = result.download_file_path;
221 // ResourceDispatcher ---------------------------------------------------------
223 ResourceDispatcher::ResourceDispatcher(IPC::Sender* sender)
224 : message_sender_(sender),
225 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
226 delegate_(NULL) {
229 ResourceDispatcher::~ResourceDispatcher() {
232 // ResourceDispatcher implementation ------------------------------------------
234 bool ResourceDispatcher::OnMessageReceived(const IPC::Message& message) {
235 if (!IsResourceDispatcherMessage(message)) {
236 return false;
239 int request_id;
241 PickleIterator iter(message);
242 if (!message.ReadInt(&iter, &request_id)) {
243 NOTREACHED() << "malformed resource message";
244 return true;
247 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id);
248 if (!request_info) {
249 // Release resources in the message if it is a data message.
250 ReleaseResourcesInDataMessage(message);
251 return true;
254 if (request_info->is_deferred) {
255 request_info->deferred_message_queue.push_back(new IPC::Message(message));
256 return true;
258 // Make sure any deferred messages are dispatched before we dispatch more.
259 if (!request_info->deferred_message_queue.empty()) {
260 FlushDeferredMessages(request_id);
261 // The request could have been deferred now. If yes then the current
262 // message has to be queued up. The request_info instance should remain
263 // valid here as there are pending messages for it.
264 DCHECK(pending_requests_.find(request_id) != pending_requests_.end());
265 if (request_info->is_deferred) {
266 request_info->deferred_message_queue.push_back(new IPC::Message(message));
267 return true;
271 DispatchMessage(message);
272 return true;
275 ResourceDispatcher::PendingRequestInfo*
276 ResourceDispatcher::GetPendingRequestInfo(int request_id) {
277 PendingRequestList::iterator it = pending_requests_.find(request_id);
278 if (it == pending_requests_.end()) {
279 // This might happen for kill()ed requests on the webkit end.
280 return NULL;
282 return &(it->second);
285 void ResourceDispatcher::OnUploadProgress(
286 const IPC::Message& message, int request_id, int64 position, int64 size) {
287 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id);
288 if (!request_info)
289 return;
291 request_info->peer->OnUploadProgress(position, size);
293 // Acknowledge receipt
294 message_sender()->Send(
295 new ResourceHostMsg_UploadProgress_ACK(message.routing_id(), request_id));
298 void ResourceDispatcher::OnReceivedResponse(
299 int request_id, const ResourceResponseHead& response_head) {
300 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id);
301 if (!request_info)
302 return;
303 request_info->response_start = base::TimeTicks::Now();
305 if (delegate_) {
306 ResourceLoaderBridge::Peer* new_peer =
307 delegate_->OnReceivedResponse(
308 request_info->peer, response_head.mime_type, request_info->url);
309 if (new_peer)
310 request_info->peer = new_peer;
313 ResourceResponseInfo renderer_response_info;
314 ToResourceResponseInfo(*request_info, response_head, &renderer_response_info);
315 request_info->peer->OnReceivedResponse(renderer_response_info);
318 void ResourceDispatcher::OnReceivedCachedMetadata(
319 int request_id, const std::vector<char>& data) {
320 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id);
321 if (!request_info)
322 return;
324 if (data.size())
325 request_info->peer->OnReceivedCachedMetadata(&data.front(), data.size());
328 void ResourceDispatcher::OnSetDataBuffer(const IPC::Message& message,
329 int request_id,
330 base::SharedMemoryHandle shm_handle,
331 int shm_size) {
332 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id);
333 if (!request_info)
334 return;
336 bool shm_valid = base::SharedMemory::IsHandleValid(shm_handle);
337 CHECK((shm_valid && shm_size > 0) || (!shm_valid && !shm_size));
339 request_info->buffer.reset(
340 new base::SharedMemory(shm_handle, true)); // read only
342 bool ok = request_info->buffer->Map(shm_size);
343 CHECK(ok);
345 request_info->buffer_size = shm_size;
348 void ResourceDispatcher::OnReceivedData(const IPC::Message& message,
349 int request_id,
350 int data_offset,
351 int data_length,
352 int encoded_data_length) {
353 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id);
354 if (request_info && data_length > 0) {
355 CHECK(base::SharedMemory::IsHandleValid(request_info->buffer->handle()));
356 CHECK_GE(request_info->buffer_size, data_offset + data_length);
358 // Ensure that the SHM buffer remains valid for the duration of this scope.
359 // It is possible for CancelPendingRequest() to be called before we exit
360 // this scope.
361 linked_ptr<base::SharedMemory> retain_buffer(request_info->buffer);
363 base::TimeTicks time_start = base::TimeTicks::Now();
365 const char* data_ptr = static_cast<char*>(request_info->buffer->memory());
366 CHECK(data_ptr);
367 CHECK(data_ptr + data_offset);
369 request_info->peer->OnReceivedData(
370 data_ptr + data_offset,
371 data_length,
372 encoded_data_length);
374 UMA_HISTOGRAM_TIMES("ResourceDispatcher.OnReceivedDataTime",
375 base::TimeTicks::Now() - time_start);
378 // Acknowledge the reception of this data.
379 message_sender()->Send(
380 new ResourceHostMsg_DataReceived_ACK(message.routing_id(), request_id));
383 void ResourceDispatcher::OnDownloadedData(const IPC::Message& message,
384 int request_id,
385 int data_len) {
386 // Acknowledge the reception of this message.
387 message_sender()->Send(
388 new ResourceHostMsg_DataDownloaded_ACK(message.routing_id(), request_id));
390 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id);
391 if (!request_info)
392 return;
394 request_info->peer->OnDownloadedData(data_len);
397 void ResourceDispatcher::OnReceivedRedirect(
398 const IPC::Message& message,
399 int request_id,
400 const GURL& new_url,
401 const ResourceResponseHead& response_head) {
402 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id);
403 if (!request_info)
404 return;
405 request_info->response_start = base::TimeTicks::Now();
407 int32 routing_id = message.routing_id();
408 bool has_new_first_party_for_cookies = false;
409 GURL new_first_party_for_cookies;
410 ResourceResponseInfo renderer_response_info;
411 ToResourceResponseInfo(*request_info, response_head, &renderer_response_info);
412 if (request_info->peer->OnReceivedRedirect(new_url, renderer_response_info,
413 &has_new_first_party_for_cookies,
414 &new_first_party_for_cookies)) {
415 // Double-check if the request is still around. The call above could
416 // potentially remove it.
417 request_info = GetPendingRequestInfo(request_id);
418 if (!request_info)
419 return;
420 request_info->pending_redirect_message.reset(
421 new ResourceHostMsg_FollowRedirect(routing_id, request_id,
422 has_new_first_party_for_cookies,
423 new_first_party_for_cookies));
424 if (!request_info->is_deferred) {
425 FollowPendingRedirect(request_id, *request_info);
427 } else {
428 CancelPendingRequest(routing_id, request_id);
432 void ResourceDispatcher::FollowPendingRedirect(
433 int request_id,
434 PendingRequestInfo& request_info) {
435 IPC::Message* msg = request_info.pending_redirect_message.release();
436 if (msg)
437 message_sender()->Send(msg);
440 void ResourceDispatcher::OnRequestComplete(
441 int request_id,
442 int error_code,
443 bool was_ignored_by_handler,
444 const std::string& security_info,
445 const base::TimeTicks& browser_completion_time) {
446 PendingRequestInfo* request_info = GetPendingRequestInfo(request_id);
447 if (!request_info)
448 return;
449 request_info->completion_time = base::TimeTicks::Now();
450 request_info->buffer.reset();
451 request_info->buffer_size = 0;
453 ResourceLoaderBridge::Peer* peer = request_info->peer;
455 if (delegate_) {
456 ResourceLoaderBridge::Peer* new_peer =
457 delegate_->OnRequestComplete(
458 request_info->peer, request_info->resource_type, error_code);
459 if (new_peer)
460 request_info->peer = new_peer;
463 base::TimeTicks renderer_completion_time = ToRendererCompletionTime(
464 *request_info, browser_completion_time);
465 // The request ID will be removed from our pending list in the destructor.
466 // Normally, dispatching this message causes the reference-counted request to
467 // die immediately.
468 peer->OnCompletedRequest(error_code, was_ignored_by_handler, security_info,
469 renderer_completion_time);
472 int ResourceDispatcher::AddPendingRequest(
473 ResourceLoaderBridge::Peer* callback,
474 ResourceType::Type resource_type,
475 const GURL& request_url) {
476 // Compute a unique request_id for this renderer process.
477 int id = MakeRequestID();
478 pending_requests_[id] =
479 PendingRequestInfo(callback, resource_type, request_url);
480 return id;
483 bool ResourceDispatcher::RemovePendingRequest(int request_id) {
484 PendingRequestList::iterator it = pending_requests_.find(request_id);
485 if (it == pending_requests_.end())
486 return false;
488 PendingRequestInfo& request_info = it->second;
489 ReleaseResourcesInMessageQueue(&request_info.deferred_message_queue);
490 pending_requests_.erase(it);
492 return true;
495 void ResourceDispatcher::CancelPendingRequest(int routing_id,
496 int request_id) {
497 PendingRequestList::iterator it = pending_requests_.find(request_id);
498 if (it == pending_requests_.end()) {
499 DVLOG(1) << "unknown request";
500 return;
503 PendingRequestInfo& request_info = it->second;
504 ReleaseResourcesInMessageQueue(&request_info.deferred_message_queue);
505 pending_requests_.erase(it);
507 message_sender()->Send(
508 new ResourceHostMsg_CancelRequest(routing_id, request_id));
511 void ResourceDispatcher::SetDefersLoading(int request_id, bool value) {
512 PendingRequestList::iterator it = pending_requests_.find(request_id);
513 if (it == pending_requests_.end()) {
514 DLOG(ERROR) << "unknown request";
515 return;
517 PendingRequestInfo& request_info = it->second;
518 if (value) {
519 request_info.is_deferred = value;
520 } else if (request_info.is_deferred) {
521 request_info.is_deferred = false;
523 FollowPendingRedirect(request_id, request_info);
525 MessageLoop::current()->PostTask(FROM_HERE,
526 base::Bind(&ResourceDispatcher::FlushDeferredMessages,
527 weak_factory_.GetWeakPtr(), request_id));
531 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo()
532 : peer(NULL),
533 resource_type(ResourceType::SUB_RESOURCE),
534 is_deferred(false),
535 buffer_size(0) {
538 ResourceDispatcher::PendingRequestInfo::PendingRequestInfo(
539 webkit_glue::ResourceLoaderBridge::Peer* peer,
540 ResourceType::Type resource_type,
541 const GURL& request_url)
542 : peer(peer),
543 resource_type(resource_type),
544 is_deferred(false),
545 url(request_url),
546 request_start(base::TimeTicks::Now()) {
549 ResourceDispatcher::PendingRequestInfo::~PendingRequestInfo() {}
551 void ResourceDispatcher::DispatchMessage(const IPC::Message& message) {
552 IPC_BEGIN_MESSAGE_MAP(ResourceDispatcher, message)
553 IPC_MESSAGE_HANDLER(ResourceMsg_UploadProgress, OnUploadProgress)
554 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedResponse, OnReceivedResponse)
555 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedCachedMetadata,
556 OnReceivedCachedMetadata)
557 IPC_MESSAGE_HANDLER(ResourceMsg_ReceivedRedirect, OnReceivedRedirect)
558 IPC_MESSAGE_HANDLER(ResourceMsg_SetDataBuffer, OnSetDataBuffer)
559 IPC_MESSAGE_HANDLER(ResourceMsg_DataReceived, OnReceivedData)
560 IPC_MESSAGE_HANDLER(ResourceMsg_DataDownloaded, OnDownloadedData)
561 IPC_MESSAGE_HANDLER(ResourceMsg_RequestComplete, OnRequestComplete)
562 IPC_END_MESSAGE_MAP()
565 void ResourceDispatcher::FlushDeferredMessages(int request_id) {
566 PendingRequestList::iterator it = pending_requests_.find(request_id);
567 if (it == pending_requests_.end()) // The request could have become invalid.
568 return;
569 PendingRequestInfo& request_info = it->second;
570 if (request_info.is_deferred)
571 return;
572 // Because message handlers could result in request_info being destroyed,
573 // we need to work with a stack reference to the deferred queue.
574 MessageQueue q;
575 q.swap(request_info.deferred_message_queue);
576 while (!q.empty()) {
577 IPC::Message* m = q.front();
578 q.pop_front();
579 DispatchMessage(*m);
580 delete m;
581 // If this request is deferred in the context of the above message, then
582 // we should honor the same and stop dispatching further messages.
583 // We need to find the request again in the list as it may have completed
584 // by now and the request_info instance above may be invalid.
585 PendingRequestList::iterator index = pending_requests_.find(request_id);
586 if (index != pending_requests_.end()) {
587 PendingRequestInfo& pending_request = index->second;
588 if (pending_request.is_deferred) {
589 pending_request.deferred_message_queue.swap(q);
590 return;
596 ResourceLoaderBridge* ResourceDispatcher::CreateBridge(
597 const ResourceLoaderBridge::RequestInfo& request_info) {
598 return new IPCResourceLoaderBridge(this, request_info);
601 void ResourceDispatcher::ToResourceResponseInfo(
602 const PendingRequestInfo& request_info,
603 const ResourceResponseHead& browser_info,
604 ResourceResponseInfo* renderer_info) const {
605 *renderer_info = browser_info;
606 if (request_info.request_start.is_null() ||
607 request_info.response_start.is_null() ||
608 browser_info.request_start.is_null() ||
609 browser_info.response_start.is_null() ||
610 browser_info.load_timing.base_ticks.is_null()) {
611 return;
613 InterProcessTimeTicksConverter converter(
614 LocalTimeTicks::FromTimeTicks(request_info.request_start),
615 LocalTimeTicks::FromTimeTicks(request_info.response_start),
616 RemoteTimeTicks::FromTimeTicks(browser_info.request_start),
617 RemoteTimeTicks::FromTimeTicks(browser_info.response_start));
619 LocalTimeTicks renderer_base_ticks = converter.ToLocalTimeTicks(
620 RemoteTimeTicks::FromTimeTicks(browser_info.load_timing.base_ticks));
621 renderer_info->load_timing.base_ticks = renderer_base_ticks.ToTimeTicks();
623 #define CONVERT(field) \
624 LocalTimeDelta renderer_##field = converter.ToLocalTimeDelta( \
625 RemoteTimeDelta::FromRawDelta(browser_info.load_timing.field)); \
626 renderer_info->load_timing.field = renderer_##field.ToInt32()
628 CONVERT(proxy_start);
629 CONVERT(dns_start);
630 CONVERT(dns_end);
631 CONVERT(connect_start);
632 CONVERT(connect_end);
633 CONVERT(ssl_start);
634 CONVERT(ssl_end);
635 CONVERT(send_start);
636 CONVERT(send_end);
637 CONVERT(receive_headers_start);
638 CONVERT(receive_headers_end);
640 #undef CONVERT
643 base::TimeTicks ResourceDispatcher::ToRendererCompletionTime(
644 const PendingRequestInfo& request_info,
645 const base::TimeTicks& browser_completion_time) const {
646 if (request_info.completion_time.is_null()) {
647 return browser_completion_time;
650 // TODO(simonjam): The optimal lower bound should be the most recent value of
651 // TimeTicks::Now() returned to WebKit. Is it worth trying to cache that?
652 // Until then, |response_start| is used as it is the most recent value
653 // returned for this request.
654 int64 result = std::max(browser_completion_time.ToInternalValue(),
655 request_info.response_start.ToInternalValue());
656 result = std::min(result, request_info.completion_time.ToInternalValue());
657 return base::TimeTicks::FromInternalValue(result);
660 // static
661 bool ResourceDispatcher::IsResourceDispatcherMessage(
662 const IPC::Message& message) {
663 switch (message.type()) {
664 case ResourceMsg_UploadProgress::ID:
665 case ResourceMsg_ReceivedResponse::ID:
666 case ResourceMsg_ReceivedCachedMetadata::ID:
667 case ResourceMsg_ReceivedRedirect::ID:
668 case ResourceMsg_SetDataBuffer::ID:
669 case ResourceMsg_DataReceived::ID:
670 case ResourceMsg_DataDownloaded::ID:
671 case ResourceMsg_RequestComplete::ID:
672 return true;
674 default:
675 break;
678 return false;
681 // static
682 void ResourceDispatcher::ReleaseResourcesInDataMessage(
683 const IPC::Message& message) {
684 PickleIterator iter(message);
685 int request_id;
686 if (!message.ReadInt(&iter, &request_id)) {
687 NOTREACHED() << "malformed resource message";
688 return;
691 // If the message contains a shared memory handle, we should close the handle
692 // or there will be a memory leak.
693 if (message.type() == ResourceMsg_SetDataBuffer::ID) {
694 base::SharedMemoryHandle shm_handle;
695 if (IPC::ParamTraits<base::SharedMemoryHandle>::Read(&message,
696 &iter,
697 &shm_handle)) {
698 if (base::SharedMemory::IsHandleValid(shm_handle))
699 base::SharedMemory::CloseHandle(shm_handle);
704 // static
705 void ResourceDispatcher::ReleaseResourcesInMessageQueue(MessageQueue* queue) {
706 while (!queue->empty()) {
707 IPC::Message* message = queue->front();
708 ReleaseResourcesInDataMessage(*message);
709 queue->pop_front();
710 delete message;
714 } // namespace content