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_CHILD_RESOURCE_DISPATCHER_H_
8 #define CONTENT_CHILD_RESOURCE_DISPATCHER_H_
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/linked_ptr.h"
15 #include "base/memory/shared_memory.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/time/time.h"
18 #include "content/common/content_export.h"
19 #include "ipc/ipc_listener.h"
20 #include "ipc/ipc_sender.h"
21 #include "net/base/request_priority.h"
22 #include "webkit/common/resource_type.h"
24 struct ResourceMsg_RequestCompleteData
;
26 namespace webkit_glue
{
27 class ResourceLoaderBridge
;
28 struct ResourceResponseInfo
;
33 class ResourceDispatcherDelegate
;
35 struct ResourceResponseHead
;
36 struct SiteIsolationResponseMetaData
;
38 // This class serves as a communication interface between the
39 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in
40 // the child process. It can be used from any child process.
41 class CONTENT_EXPORT ResourceDispatcher
: public IPC::Listener
{
43 explicit ResourceDispatcher(IPC::Sender
* sender
);
44 virtual ~ResourceDispatcher();
46 // IPC::Listener implementation.
47 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
49 // Creates a ResourceLoaderBridge for this type of dispatcher, this is so
50 // this can be tested regardless of the ResourceLoaderBridge::Create
52 webkit_glue::ResourceLoaderBridge
* CreateBridge(
53 const RequestInfo
& request_info
);
55 // Adds a request from the pending_requests_ list, returning the new
57 int AddPendingRequest(RequestPeer
* callback
,
58 ResourceType::Type resource_type
,
60 const GURL
& frame_origin
,
61 const GURL
& request_url
);
63 // Removes a request from the pending_requests_ list, returning true if the
64 // request was found and removed.
65 bool RemovePendingRequest(int request_id
);
67 // Cancels a request in the pending_requests_ list.
68 void CancelPendingRequest(int request_id
);
70 IPC::Sender
* message_sender() const {
71 return message_sender_
;
74 // Toggles the is_deferred attribute for the specified request.
75 void SetDefersLoading(int request_id
, bool value
);
77 // Indicates the priority of the specified request changed.
78 void DidChangePriority(int routing_id
, int request_id
,
79 net::RequestPriority new_priority
,
80 int intra_priority_value
);
82 // This does not take ownership of the delegate. It is expected that the
83 // delegate have a longer lifetime than the ResourceDispatcher.
84 void set_delegate(ResourceDispatcherDelegate
* delegate
) {
88 // Remembers IO thread timestamp for next resource message.
89 void set_io_timestamp(base::TimeTicks io_timestamp
) {
90 io_timestamp_
= io_timestamp
;
94 friend class ResourceDispatcherTest
;
96 typedef std::deque
<IPC::Message
*> MessageQueue
;
97 struct PendingRequestInfo
{
100 PendingRequestInfo(RequestPeer
* peer
,
101 ResourceType::Type resource_type
,
103 const GURL
& frame_origin
,
104 const GURL
& request_url
);
106 ~PendingRequestInfo();
109 ResourceType::Type resource_type
;
110 // The PID of the original process which issued this request. This gets
111 // non-zero only for a request proxied by another renderer, particularly
112 // requests from plugins.
114 MessageQueue deferred_message_queue
;
116 // Original requested url.
118 // The security origin of the frame that initiates this request.
120 // The url of the latest response even in case of redirection.
122 linked_ptr
<IPC::Message
> pending_redirect_message
;
123 base::TimeTicks request_start
;
124 base::TimeTicks response_start
;
125 base::TimeTicks completion_time
;
126 linked_ptr
<base::SharedMemory
> buffer
;
127 linked_ptr
<SiteIsolationResponseMetaData
> site_isolation_metadata
;
128 bool blocked_response
;
131 typedef base::hash_map
<int, PendingRequestInfo
> PendingRequestList
;
133 // Helper to lookup the info based on the request_id.
134 // May return NULL if the request as been canceled from the client side.
135 PendingRequestInfo
* GetPendingRequestInfo(int request_id
);
137 // Follows redirect, if any, for the given request.
138 void FollowPendingRedirect(int request_id
, PendingRequestInfo
& request_info
);
140 // Message response handlers, called by the message handler for this process.
141 void OnUploadProgress(
145 void OnReceivedResponse(int request_id
, const ResourceResponseHead
&);
146 void OnReceivedCachedMetadata(int request_id
, const std::vector
<char>& data
);
147 void OnReceivedRedirect(
150 const ResourceResponseHead
& response_head
);
151 void OnSetDataBuffer(
153 base::SharedMemoryHandle shm_handle
,
155 base::ProcessId renderer_pid
);
160 int encoded_data_length
);
161 void OnDownloadedData(
164 int encoded_data_length
);
165 void OnRequestComplete(
167 const ResourceMsg_RequestCompleteData
&request_complete_data
);
169 // Dispatch the message to one of the message response handlers.
170 void DispatchMessage(const IPC::Message
& message
);
172 // Dispatch any deferred messages for the given request, provided it is not
173 // again in the deferred state.
174 void FlushDeferredMessages(int request_id
);
176 void ToResourceResponseInfo(
177 const PendingRequestInfo
& request_info
,
178 const ResourceResponseHead
& browser_info
,
179 webkit_glue::ResourceResponseInfo
* renderer_info
) const;
181 base::TimeTicks
ToRendererCompletionTime(
182 const PendingRequestInfo
& request_info
,
183 const base::TimeTicks
& browser_completion_time
) const;
185 // Returns timestamp provided by IO thread. If no timestamp is supplied,
186 // then current time is returned. Saved timestamp is reset, so following
187 // invocations will return current time until set_io_timestamp is called.
188 base::TimeTicks
ConsumeIOTimestamp();
190 // Returns true if the message passed in is a resource related message.
191 static bool IsResourceDispatcherMessage(const IPC::Message
& message
);
193 // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory
194 // handle in it that we should cleanup it up nicely. This method accepts any
195 // message and determine whether the message is
196 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle.
197 static void ReleaseResourcesInDataMessage(const IPC::Message
& message
);
199 // Iterate through a message queue and clean up the messages by calling
200 // ReleaseResourcesInDataMessage and removing them from the queue. Intended
201 // for use on deferred message queues that are no longer needed.
202 static void ReleaseResourcesInMessageQueue(MessageQueue
* queue
);
204 IPC::Sender
* message_sender_
;
206 // All pending requests issued to the host
207 PendingRequestList pending_requests_
;
209 base::WeakPtrFactory
<ResourceDispatcher
> weak_factory_
;
211 ResourceDispatcherDelegate
* delegate_
;
213 // IO thread timestamp for ongoing IPC message.
214 base::TimeTicks io_timestamp_
;
216 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher
);
219 } // namespace content
221 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_