Redirect HTML resource bytes directly to parser thread (Chrome side)
[chromium-blink-merge.git] / content / child / resource_dispatcher.h
blob93dac18873a92b7bcf5308e0e1a12a4e8f807596
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_
10 #include <deque>
11 #include <string>
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 blink {
27 class WebThreadedDataReceiver;
30 namespace webkit_glue {
31 class ResourceLoaderBridge;
34 namespace content {
35 class RequestPeer;
36 class ResourceDispatcherDelegate;
37 class ThreadedDataProvider;
38 struct ResourceResponseInfo;
39 struct RequestInfo;
40 struct ResourceResponseHead;
41 struct SiteIsolationResponseMetaData;
43 // This class serves as a communication interface between the
44 // ResourceDispatcherHost in the browser process and the ResourceLoaderBridge in
45 // the child process. It can be used from any child process.
46 class CONTENT_EXPORT ResourceDispatcher : public IPC::Listener {
47 public:
48 explicit ResourceDispatcher(IPC::Sender* sender);
49 virtual ~ResourceDispatcher();
51 // IPC::Listener implementation.
52 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
54 // Creates a ResourceLoaderBridge for this type of dispatcher, this is so
55 // this can be tested regardless of the ResourceLoaderBridge::Create
56 // implementation.
57 webkit_glue::ResourceLoaderBridge* CreateBridge(
58 const RequestInfo& request_info);
60 // Adds a request from the |pending_requests_| list, returning the new
61 // requests' ID.
62 int AddPendingRequest(RequestPeer* callback,
63 ResourceType::Type resource_type,
64 int origin_pid,
65 const GURL& frame_origin,
66 const GURL& request_url,
67 bool download_to_file);
69 // Removes a request from the |pending_requests_| list, returning true if the
70 // request was found and removed.
71 bool RemovePendingRequest(int request_id);
73 // Cancels a request in the |pending_requests_| list.
74 void CancelPendingRequest(int request_id);
76 // Toggles the is_deferred attribute for the specified request.
77 void SetDefersLoading(int request_id, bool value);
79 // Indicates the priority of the specified request changed.
80 void DidChangePriority(int request_id,
81 net::RequestPriority new_priority,
82 int intra_priority_value);
84 // The provided data receiver will receive incoming resource data rather
85 // than the resource bridge.
86 bool AttachThreadedDataReceiver(
87 int request_id, blink::WebThreadedDataReceiver* threaded_data_receiver);
89 IPC::Sender* message_sender() const { return message_sender_; }
91 // This does not take ownership of the delegate. It is expected that the
92 // delegate have a longer lifetime than the ResourceDispatcher.
93 void set_delegate(ResourceDispatcherDelegate* delegate) {
94 delegate_ = delegate;
97 // Remembers IO thread timestamp for next resource message.
98 void set_io_timestamp(base::TimeTicks io_timestamp) {
99 io_timestamp_ = io_timestamp;
102 private:
103 friend class ResourceDispatcherTest;
105 typedef std::deque<IPC::Message*> MessageQueue;
106 struct PendingRequestInfo {
107 PendingRequestInfo();
109 PendingRequestInfo(RequestPeer* peer,
110 ResourceType::Type resource_type,
111 int origin_pid,
112 const GURL& frame_origin,
113 const GURL& request_url,
114 bool download_to_file);
116 ~PendingRequestInfo();
118 RequestPeer* peer;
119 ThreadedDataProvider* threaded_data_provider;
120 ResourceType::Type resource_type;
121 // The PID of the original process which issued this request. This gets
122 // non-zero only for a request proxied by another renderer, particularly
123 // requests from plugins.
124 int origin_pid;
125 MessageQueue deferred_message_queue;
126 bool is_deferred;
127 // Original requested url.
128 GURL url;
129 // The security origin of the frame that initiates this request.
130 GURL frame_origin;
131 // The url of the latest response even in case of redirection.
132 GURL response_url;
133 bool download_to_file;
134 linked_ptr<IPC::Message> pending_redirect_message;
135 base::TimeTicks request_start;
136 base::TimeTicks response_start;
137 base::TimeTicks completion_time;
138 linked_ptr<base::SharedMemory> buffer;
139 linked_ptr<SiteIsolationResponseMetaData> site_isolation_metadata;
140 bool blocked_response;
141 int buffer_size;
143 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList;
145 // Helper to lookup the info based on the request_id.
146 // May return NULL if the request as been canceled from the client side.
147 PendingRequestInfo* GetPendingRequestInfo(int request_id);
149 // Follows redirect, if any, for the given request.
150 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info);
152 // Message response handlers, called by the message handler for this process.
153 void OnUploadProgress(int request_id, int64 position, int64 size);
154 void OnReceivedResponse(int request_id, const ResourceResponseHead&);
155 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data);
156 void OnReceivedRedirect(int request_id,
157 const GURL& new_url,
158 const GURL& new_first_party_for_cookies,
159 const ResourceResponseHead& response_head);
160 void OnSetDataBuffer(int request_id,
161 base::SharedMemoryHandle shm_handle,
162 int shm_size,
163 base::ProcessId renderer_pid);
164 void OnReceivedData(int request_id,
165 int data_offset,
166 int data_length,
167 int encoded_data_length);
168 void OnDownloadedData(int request_id, int data_len, int encoded_data_length);
169 void OnRequestComplete(
170 int request_id,
171 const ResourceMsg_RequestCompleteData& request_complete_data);
173 // Dispatch the message to one of the message response handlers.
174 void DispatchMessage(const IPC::Message& message);
176 // Dispatch any deferred messages for the given request, provided it is not
177 // again in the deferred state.
178 void FlushDeferredMessages(int request_id);
180 void ToResourceResponseInfo(const PendingRequestInfo& request_info,
181 const ResourceResponseHead& browser_info,
182 ResourceResponseInfo* renderer_info) const;
184 base::TimeTicks ToRendererCompletionTime(
185 const PendingRequestInfo& request_info,
186 const base::TimeTicks& browser_completion_time) const;
188 // Returns timestamp provided by IO thread. If no timestamp is supplied,
189 // then current time is returned. Saved timestamp is reset, so following
190 // invocations will return current time until set_io_timestamp is called.
191 base::TimeTicks ConsumeIOTimestamp();
193 // Returns true if the message passed in is a resource related message.
194 static bool IsResourceDispatcherMessage(const IPC::Message& message);
196 // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory
197 // handle in it that we should cleanup it up nicely. This method accepts any
198 // message and determine whether the message is
199 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle.
200 static void ReleaseResourcesInDataMessage(const IPC::Message& message);
202 // Iterate through a message queue and clean up the messages by calling
203 // ReleaseResourcesInDataMessage and removing them from the queue. Intended
204 // for use on deferred message queues that are no longer needed.
205 static void ReleaseResourcesInMessageQueue(MessageQueue* queue);
207 IPC::Sender* message_sender_;
209 // All pending requests issued to the host
210 PendingRequestList pending_requests_;
212 base::WeakPtrFactory<ResourceDispatcher> weak_factory_;
214 ResourceDispatcherDelegate* delegate_;
216 // IO thread timestamp for ongoing IPC message.
217 base::TimeTicks io_timestamp_;
219 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher);
222 } // namespace content
224 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_