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