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 "webkit/child/resource_loader_bridge.h"
23 struct ResourceMsg_RequestCompleteData
;
26 class ResourceDispatcherDelegate
;
27 struct ResourceResponseHead
;
28 struct SiteIsolationResponseMetaData
;
30 // This class serves as a communication interface between the
31 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in
32 // the child process. It can be used from any child process.
33 class CONTENT_EXPORT ResourceDispatcher
: public IPC::Listener
{
35 explicit ResourceDispatcher(IPC::Sender
* sender
);
36 virtual ~ResourceDispatcher();
38 // IPC::Listener implementation.
39 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
41 // Creates a ResourceLoaderBridge for this type of dispatcher, this is so
42 // this can be tested regardless of the ResourceLoaderBridge::Create
44 webkit_glue::ResourceLoaderBridge
* CreateBridge(
45 const webkit_glue::ResourceLoaderBridge::RequestInfo
& request_info
);
47 // Adds a request from the pending_requests_ list, returning the new
49 int AddPendingRequest(webkit_glue::ResourceLoaderBridge::Peer
* callback
,
50 ResourceType::Type resource_type
,
52 const GURL
& frame_origin
,
53 const GURL
& request_url
);
55 // Removes a request from the pending_requests_ list, returning true if the
56 // request was found and removed.
57 bool RemovePendingRequest(int request_id
);
59 // Cancels a request in the pending_requests_ list.
60 void CancelPendingRequest(int request_id
);
62 IPC::Sender
* message_sender() const {
63 return message_sender_
;
66 // Toggles the is_deferred attribute for the specified request.
67 void SetDefersLoading(int request_id
, bool value
);
69 // Indicates the priority of the specified request changed.
70 void DidChangePriority(int routing_id
, int request_id
,
71 net::RequestPriority new_priority
);
73 // This does not take ownership of the delegate. It is expected that the
74 // delegate have a longer lifetime than the ResourceDispatcher.
75 void set_delegate(ResourceDispatcherDelegate
* delegate
) {
79 // Remembers IO thread timestamp for next resource message.
80 void set_io_timestamp(base::TimeTicks io_timestamp
) {
81 io_timestamp_
= io_timestamp
;
85 friend class ResourceDispatcherTest
;
87 typedef std::deque
<IPC::Message
*> MessageQueue
;
88 struct PendingRequestInfo
{
91 PendingRequestInfo(webkit_glue::ResourceLoaderBridge::Peer
* peer
,
92 ResourceType::Type resource_type
,
94 const GURL
& frame_origin
,
95 const GURL
& request_url
);
97 ~PendingRequestInfo();
99 webkit_glue::ResourceLoaderBridge::Peer
* peer
;
100 ResourceType::Type resource_type
;
101 // The PID of the original process which issued this request. This gets
102 // non-zero only for a request proxied by another renderer, particularly
103 // requests from plugins.
105 MessageQueue deferred_message_queue
;
107 // Original requested url.
109 // The security origin of the frame that initiates this request.
111 // The url of the latest response even in case of redirection.
113 linked_ptr
<IPC::Message
> pending_redirect_message
;
114 base::TimeTicks request_start
;
115 base::TimeTicks response_start
;
116 base::TimeTicks completion_time
;
117 linked_ptr
<base::SharedMemory
> buffer
;
118 linked_ptr
<SiteIsolationResponseMetaData
> site_isolation_metadata
;
119 bool blocked_response
;
122 typedef base::hash_map
<int, PendingRequestInfo
> PendingRequestList
;
124 // Helper to lookup the info based on the request_id.
125 // May return NULL if the request as been canceled from the client side.
126 PendingRequestInfo
* GetPendingRequestInfo(int request_id
);
128 // Follows redirect, if any, for the given request.
129 void FollowPendingRedirect(int request_id
, PendingRequestInfo
& request_info
);
131 // Message response handlers, called by the message handler for this process.
132 void OnUploadProgress(
136 void OnReceivedResponse(int request_id
, const ResourceResponseHead
&);
137 void OnReceivedCachedMetadata(int request_id
, const std::vector
<char>& data
);
138 void OnReceivedRedirect(
141 const ResourceResponseHead
& response_head
);
142 void OnSetDataBuffer(
144 base::SharedMemoryHandle shm_handle
,
146 base::ProcessId renderer_pid
);
151 int encoded_data_length
);
152 void OnDownloadedData(
155 int encoded_data_length
);
156 void OnRequestComplete(
158 const ResourceMsg_RequestCompleteData
&request_complete_data
);
160 // Dispatch the message to one of the message response handlers.
161 void DispatchMessage(const IPC::Message
& message
);
163 // Dispatch any deferred messages for the given request, provided it is not
164 // again in the deferred state.
165 void FlushDeferredMessages(int request_id
);
167 void ToResourceResponseInfo(
168 const PendingRequestInfo
& request_info
,
169 const ResourceResponseHead
& browser_info
,
170 webkit_glue::ResourceResponseInfo
* renderer_info
) const;
172 base::TimeTicks
ToRendererCompletionTime(
173 const PendingRequestInfo
& request_info
,
174 const base::TimeTicks
& browser_completion_time
) const;
176 // Returns timestamp provided by IO thread. If no timestamp is supplied,
177 // then current time is returned. Saved timestamp is reset, so following
178 // invocations will return current time until set_io_timestamp is called.
179 base::TimeTicks
ConsumeIOTimestamp();
181 // Returns true if the message passed in is a resource related message.
182 static bool IsResourceDispatcherMessage(const IPC::Message
& message
);
184 // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory
185 // handle in it that we should cleanup it up nicely. This method accepts any
186 // message and determine whether the message is
187 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle.
188 static void ReleaseResourcesInDataMessage(const IPC::Message
& message
);
190 // Iterate through a message queue and clean up the messages by calling
191 // ReleaseResourcesInDataMessage and removing them from the queue. Intended
192 // for use on deferred message queues that are no longer needed.
193 static void ReleaseResourcesInMessageQueue(MessageQueue
* queue
);
195 IPC::Sender
* message_sender_
;
197 // All pending requests issued to the host
198 PendingRequestList pending_requests_
;
200 base::WeakPtrFactory
<ResourceDispatcher
> weak_factory_
;
202 ResourceDispatcherDelegate
* delegate_
;
204 // IO thread timestamp for ongoing IPC message.
205 base::TimeTicks io_timestamp_
;
207 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher
);
210 } // namespace content
212 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_