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,
62 bool download_to_file);
64 // Removes a request from the |pending_requests_| list, returning true if the
65 // request was found and removed.
66 bool RemovePendingRequest(int request_id);
68 // Cancels a request in the |pending_requests_| list.
69 void CancelPendingRequest(int request_id);
71 // Toggles the is_deferred attribute for the specified request.
72 void SetDefersLoading(int request_id, bool value);
74 // Indicates the priority of the specified request changed.
75 void DidChangePriority(int routing_id,
77 net::RequestPriority new_priority,
78 int intra_priority_value);
80 IPC::Sender* message_sender() const { return message_sender_; }
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,
105 bool download_to_file);
107 ~PendingRequestInfo();
110 ResourceType::Type resource_type;
111 // The PID of the original process which issued this request. This gets
112 // non-zero only for a request proxied by another renderer, particularly
113 // requests from plugins.
115 MessageQueue deferred_message_queue;
118 // Original requested url.
120 // The security origin of the frame that initiates this request.
122 // The url of the latest response even in case of redirection.
124 bool download_to_file;
125 linked_ptr<IPC::Message> pending_redirect_message;
126 base::TimeTicks request_start;
127 base::TimeTicks response_start;
128 base::TimeTicks completion_time;
129 linked_ptr<base::SharedMemory> buffer;
130 linked_ptr<SiteIsolationResponseMetaData> site_isolation_metadata;
131 bool blocked_response;
134 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList;
136 // Helper to lookup the info based on the request_id.
137 // May return NULL if the request as been canceled from the client side.
138 PendingRequestInfo* GetPendingRequestInfo(int request_id);
140 // Follows redirect, if any, for the given request.
141 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info);
143 // Message response handlers, called by the message handler for this process.
144 void OnUploadProgress(int request_id, int64 position, int64 size);
145 void OnReceivedResponse(int request_id, const ResourceResponseHead&);
146 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data);
147 void OnReceivedRedirect(int request_id,
149 const ResourceResponseHead& response_head);
150 void OnSetDataBuffer(int request_id,
151 base::SharedMemoryHandle shm_handle,
153 base::ProcessId renderer_pid);
154 void OnReceivedData(int request_id,
157 int encoded_data_length);
158 void OnDownloadedData(int request_id, int data_len, int encoded_data_length);
159 void OnRequestComplete(
161 const ResourceMsg_RequestCompleteData& request_complete_data);
163 // Dispatch the message to one of the message response handlers.
164 void DispatchMessage(const IPC::Message& message);
166 // Dispatch any deferred messages for the given request, provided it is not
167 // again in the deferred state.
168 void FlushDeferredMessages(int request_id);
170 void ToResourceResponseInfo(
171 const PendingRequestInfo& request_info,
172 const ResourceResponseHead& browser_info,
173 webkit_glue::ResourceResponseInfo* renderer_info) const;
175 base::TimeTicks ToRendererCompletionTime(
176 const PendingRequestInfo& request_info,
177 const base::TimeTicks& browser_completion_time) const;
179 // Returns timestamp provided by IO thread. If no timestamp is supplied,
180 // then current time is returned. Saved timestamp is reset, so following
181 // invocations will return current time until set_io_timestamp is called.
182 base::TimeTicks ConsumeIOTimestamp();
184 // Returns true if the message passed in is a resource related message.
185 static bool IsResourceDispatcherMessage(const IPC::Message& message);
187 // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory
188 // handle in it that we should cleanup it up nicely. This method accepts any
189 // message and determine whether the message is
190 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle.
191 static void ReleaseResourcesInDataMessage(const IPC::Message& message);
193 // Iterate through a message queue and clean up the messages by calling
194 // ReleaseResourcesInDataMessage and removing them from the queue. Intended
195 // for use on deferred message queues that are no longer needed.
196 static void ReleaseResourcesInMessageQueue(MessageQueue* queue);
198 IPC::Sender* message_sender_;
200 // All pending requests issued to the host
201 PendingRequestList pending_requests_;
203 base::WeakPtrFactory<ResourceDispatcher> weak_factory_;
205 ResourceDispatcherDelegate* delegate_;
207 // IO thread timestamp for ongoing IPC message.
208 base::TimeTicks io_timestamp_;
210 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher);
213 } // namespace content
215 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_