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 "content/public/common/resource_type.h"
20 #include "ipc/ipc_listener.h"
21 #include "ipc/ipc_sender.h"
22 #include "net/base/request_priority.h"
25 struct ResourceMsg_RequestCompleteData
;
28 class WebThreadedDataReceiver
;
35 namespace webkit_glue
{
36 class ResourceLoaderBridge
;
41 class ResourceDispatcherDelegate
;
42 class ThreadedDataProvider
;
43 struct ResourceResponseInfo
;
45 struct ResourceResponseHead
;
46 struct SiteIsolationResponseMetaData
;
48 // This class serves as a communication interface between the
49 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in
50 // the child process. It can be used from any child process.
51 class CONTENT_EXPORT ResourceDispatcher
: public IPC::Listener
{
53 explicit ResourceDispatcher(IPC::Sender
* sender
);
54 virtual ~ResourceDispatcher();
56 // IPC::Listener implementation.
57 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
59 // Creates a ResourceLoaderBridge for this type of dispatcher, this is so
60 // this can be tested regardless of the ResourceLoaderBridge::Create
61 // implementation. Virtual for tests.
62 virtual webkit_glue::ResourceLoaderBridge
* CreateBridge(
63 const RequestInfo
& request_info
);
65 // Adds a request from the |pending_requests_| list, returning the new
67 int AddPendingRequest(RequestPeer
* callback
,
68 ResourceType resource_type
,
70 const GURL
& frame_origin
,
71 const GURL
& request_url
,
72 bool download_to_file
);
74 // Removes a request from the |pending_requests_| list, returning true if the
75 // request was found and removed.
76 bool RemovePendingRequest(int request_id
);
78 // Cancels a request in the |pending_requests_| list. The request will be
79 // removed from the dispatcher as well.
80 void CancelPendingRequest(int request_id
);
82 // Toggles the is_deferred attribute for the specified request.
83 void SetDefersLoading(int request_id
, bool value
);
85 // Indicates the priority of the specified request changed.
86 void DidChangePriority(int request_id
,
87 net::RequestPriority new_priority
,
88 int intra_priority_value
);
90 // The provided data receiver will receive incoming resource data rather
91 // than the resource bridge.
92 bool AttachThreadedDataReceiver(
93 int request_id
, blink::WebThreadedDataReceiver
* threaded_data_receiver
);
95 IPC::Sender
* message_sender() const { return message_sender_
; }
97 // This does not take ownership of the delegate. It is expected that the
98 // delegate have a longer lifetime than the ResourceDispatcher.
99 void set_delegate(ResourceDispatcherDelegate
* delegate
) {
100 delegate_
= delegate
;
103 // Remembers IO thread timestamp for next resource message.
104 void set_io_timestamp(base::TimeTicks io_timestamp
) {
105 io_timestamp_
= io_timestamp
;
109 friend class ResourceDispatcherTest
;
111 typedef std::deque
<IPC::Message
*> MessageQueue
;
112 struct PendingRequestInfo
{
113 PendingRequestInfo();
115 PendingRequestInfo(RequestPeer
* peer
,
116 ResourceType resource_type
,
118 const GURL
& frame_origin
,
119 const GURL
& request_url
,
120 bool download_to_file
);
122 ~PendingRequestInfo();
125 ThreadedDataProvider
* threaded_data_provider
;
126 ResourceType resource_type
;
127 // The PID of the original process which issued this request. This gets
128 // non-zero only for a request proxied by another renderer, particularly
129 // requests from plugins.
131 MessageQueue deferred_message_queue
;
133 // Original requested url.
135 // The security origin of the frame that initiates this request.
137 // The url of the latest response even in case of redirection.
139 bool download_to_file
;
140 linked_ptr
<IPC::Message
> pending_redirect_message
;
141 base::TimeTicks request_start
;
142 base::TimeTicks response_start
;
143 base::TimeTicks completion_time
;
144 linked_ptr
<base::SharedMemory
> buffer
;
145 linked_ptr
<SiteIsolationResponseMetaData
> site_isolation_metadata
;
146 bool blocked_response
;
149 typedef base::hash_map
<int, PendingRequestInfo
> PendingRequestList
;
151 // Helper to lookup the info based on the request_id.
152 // May return NULL if the request as been canceled from the client side.
153 PendingRequestInfo
* GetPendingRequestInfo(int request_id
);
155 // Follows redirect, if any, for the given request.
156 void FollowPendingRedirect(int request_id
, PendingRequestInfo
& request_info
);
158 // Message response handlers, called by the message handler for this process.
159 void OnUploadProgress(int request_id
, int64 position
, int64 size
);
160 void OnReceivedResponse(int request_id
, const ResourceResponseHead
&);
161 void OnReceivedCachedMetadata(int request_id
, const std::vector
<char>& data
);
162 void OnReceivedRedirect(int request_id
,
163 const net::RedirectInfo
& redirect_info
,
164 const ResourceResponseHead
& response_head
);
165 void OnSetDataBuffer(int request_id
,
166 base::SharedMemoryHandle shm_handle
,
168 base::ProcessId renderer_pid
);
169 void OnReceivedData(int request_id
,
172 int encoded_data_length
);
173 void OnDownloadedData(int request_id
, int data_len
, int encoded_data_length
);
174 void OnRequestComplete(
176 const ResourceMsg_RequestCompleteData
& request_complete_data
);
178 // Dispatch the message to one of the message response handlers.
179 void DispatchMessage(const IPC::Message
& message
);
181 // Dispatch any deferred messages for the given request, provided it is not
182 // again in the deferred state.
183 void FlushDeferredMessages(int request_id
);
185 void ToResourceResponseInfo(const PendingRequestInfo
& request_info
,
186 const ResourceResponseHead
& browser_info
,
187 ResourceResponseInfo
* renderer_info
) const;
189 base::TimeTicks
ToRendererCompletionTime(
190 const PendingRequestInfo
& request_info
,
191 const base::TimeTicks
& browser_completion_time
) const;
193 // Returns timestamp provided by IO thread. If no timestamp is supplied,
194 // then current time is returned. Saved timestamp is reset, so following
195 // invocations will return current time until set_io_timestamp is called.
196 base::TimeTicks
ConsumeIOTimestamp();
198 // Returns true if the message passed in is a resource related message.
199 static bool IsResourceDispatcherMessage(const IPC::Message
& message
);
201 // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory
202 // handle in it that we should cleanup it up nicely. This method accepts any
203 // message and determine whether the message is
204 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle.
205 static void ReleaseResourcesInDataMessage(const IPC::Message
& message
);
207 // Iterate through a message queue and clean up the messages by calling
208 // ReleaseResourcesInDataMessage and removing them from the queue. Intended
209 // for use on deferred message queues that are no longer needed.
210 static void ReleaseResourcesInMessageQueue(MessageQueue
* queue
);
212 IPC::Sender
* message_sender_
;
214 // All pending requests issued to the host
215 PendingRequestList pending_requests_
;
217 base::WeakPtrFactory
<ResourceDispatcher
> weak_factory_
;
219 ResourceDispatcherDelegate
* delegate_
;
221 // IO thread timestamp for ongoing IPC message.
222 base::TimeTicks io_timestamp_
;
224 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher
);
227 } // namespace content
229 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_