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 #ifndef CONTENT_COMMON_RESOURCE_DISPATCHER_H_
8 #define CONTENT_COMMON_RESOURCE_DISPATCHER_H_
13 #include "base/hash_tables.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/shared_memory.h"
17 #include "base/time.h"
18 #include "content/common/content_export.h"
19 #include "ipc/ipc_listener.h"
20 #include "ipc/ipc_sender.h"
21 #include "webkit/glue/resource_loader_bridge.h"
24 class ResourceDispatcherDelegate
;
25 struct ResourceResponseHead
;
27 // This class serves as a communication interface between the
28 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in
29 // the child process. It can be used from any child process.
30 class CONTENT_EXPORT ResourceDispatcher
: public IPC::Listener
{
32 explicit ResourceDispatcher(IPC::Sender
* sender
);
33 virtual ~ResourceDispatcher();
35 // IPC::Listener implementation.
36 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
38 // Creates a ResourceLoaderBridge for this type of dispatcher, this is so
39 // this can be tested regardless of the ResourceLoaderBridge::Create
41 webkit_glue::ResourceLoaderBridge
* CreateBridge(
42 const webkit_glue::ResourceLoaderBridge::RequestInfo
& request_info
);
44 // Adds a request from the pending_requests_ list, returning the new
46 int AddPendingRequest(webkit_glue::ResourceLoaderBridge::Peer
* callback
,
47 ResourceType::Type resource_type
,
48 const GURL
& request_url
);
50 // Removes a request from the pending_requests_ list, returning true if the
51 // request was found and removed.
52 bool RemovePendingRequest(int request_id
);
54 // Cancels a request in the pending_requests_ list.
55 void CancelPendingRequest(int routing_id
, int request_id
);
57 IPC::Sender
* message_sender() const {
58 return message_sender_
;
61 // Toggles the is_deferred attribute for the specified request.
62 void SetDefersLoading(int request_id
, bool value
);
64 // This does not take ownership of the delegate. It is expected that the
65 // delegate have a longer lifetime than the ResourceDispatcher.
66 void set_delegate(ResourceDispatcherDelegate
* delegate
) {
71 friend class ResourceDispatcherTest
;
73 typedef std::deque
<IPC::Message
*> MessageQueue
;
74 struct PendingRequestInfo
{
77 PendingRequestInfo(webkit_glue::ResourceLoaderBridge::Peer
* peer
,
78 ResourceType::Type resource_type
,
79 const GURL
& request_url
);
81 ~PendingRequestInfo();
83 webkit_glue::ResourceLoaderBridge::Peer
* peer
;
84 ResourceType::Type resource_type
;
85 MessageQueue deferred_message_queue
;
88 linked_ptr
<IPC::Message
> pending_redirect_message
;
89 base::TimeTicks request_start
;
90 base::TimeTicks response_start
;
91 base::TimeTicks completion_time
;
92 linked_ptr
<base::SharedMemory
> buffer
;
95 typedef base::hash_map
<int, PendingRequestInfo
> PendingRequestList
;
97 // Helper to lookup the info based on the request_id.
98 // May return NULL if the request as been canceled from the client side.
99 PendingRequestInfo
* GetPendingRequestInfo(int request_id
);
101 // Follows redirect, if any, for the given request.
102 void FollowPendingRedirect(int request_id
, PendingRequestInfo
& request_info
);
104 // Message response handlers, called by the message handler for this process.
105 void OnUploadProgress(
106 const IPC::Message
& message
,
110 void OnReceivedResponse(int request_id
, const ResourceResponseHead
&);
111 void OnReceivedCachedMetadata(int request_id
, const std::vector
<char>& data
);
112 void OnReceivedRedirect(
113 const IPC::Message
& message
,
116 const ResourceResponseHead
& response_head
);
117 void OnSetDataBuffer(
118 const IPC::Message
& message
,
120 base::SharedMemoryHandle shm_handle
,
123 const IPC::Message
& message
,
127 int encoded_data_length
);
128 void OnDownloadedData(
129 const IPC::Message
& message
,
132 void OnRequestComplete(
135 bool was_ignored_by_handler
,
136 const std::string
& security_info
,
137 const base::TimeTicks
& completion_time
);
139 // Dispatch the message to one of the message response handlers.
140 void DispatchMessage(const IPC::Message
& message
);
142 // Dispatch any deferred messages for the given request, provided it is not
143 // again in the deferred state.
144 void FlushDeferredMessages(int request_id
);
146 void ToResourceResponseInfo(
147 const PendingRequestInfo
& request_info
,
148 const ResourceResponseHead
& browser_info
,
149 webkit_glue::ResourceResponseInfo
* renderer_info
) const;
151 base::TimeTicks
ToRendererCompletionTime(
152 const PendingRequestInfo
& request_info
,
153 const base::TimeTicks
& browser_completion_time
) const;
155 // Returns true if the message passed in is a resource related message.
156 static bool IsResourceDispatcherMessage(const IPC::Message
& message
);
158 // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory
159 // handle in it that we should cleanup it up nicely. This method accepts any
160 // message and determine whether the message is
161 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle.
162 static void ReleaseResourcesInDataMessage(const IPC::Message
& message
);
164 // Iterate through a message queue and clean up the messages by calling
165 // ReleaseResourcesInDataMessage and removing them from the queue. Intended
166 // for use on deferred message queues that are no longer needed.
167 static void ReleaseResourcesInMessageQueue(MessageQueue
* queue
);
169 IPC::Sender
* message_sender_
;
171 // All pending requests issued to the host
172 PendingRequestList pending_requests_
;
174 base::WeakPtrFactory
<ResourceDispatcher
> weak_factory_
;
176 ResourceDispatcherDelegate
* delegate_
;
178 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher
);
181 } // namespace content
183 #endif // CONTENT_COMMON_RESOURCE_DISPATCHER_H_