Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / child / resource_dispatcher.h
blob769eae2589f2040106bb8dbd6fa17432ff220f3b
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/scoped_ptr.h"
16 #include "base/memory/shared_memory.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/single_thread_task_runner.h"
19 #include "base/time/time.h"
20 #include "content/common/content_export.h"
21 #include "content/public/common/resource_type.h"
22 #include "ipc/ipc_listener.h"
23 #include "ipc/ipc_sender.h"
24 #include "net/base/request_priority.h"
25 #include "url/gurl.h"
27 struct ResourceHostMsg_Request;
28 struct ResourceMsg_RequestCompleteData;
30 namespace blink {
31 class WebThreadedDataReceiver;
34 namespace net {
35 struct RedirectInfo;
38 namespace content {
39 class RequestPeer;
40 class ResourceDispatcherDelegate;
41 class ResourceRequestBody;
42 class ThreadedDataProvider;
43 struct ResourceResponseInfo;
44 struct RequestInfo;
45 struct ResourceResponseHead;
46 struct SiteIsolationResponseMetaData;
47 struct SyncLoadResponse;
49 // This class serves as a communication interface to the ResourceDispatcherHost
50 // in the browser process. It can be used from any child process.
51 // Virtual methods are for tests.
52 class CONTENT_EXPORT ResourceDispatcher : public IPC::Listener {
53 public:
54 ResourceDispatcher(
55 IPC::Sender* sender,
56 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner);
57 ~ResourceDispatcher() override;
59 // IPC::Listener implementation.
60 bool OnMessageReceived(const IPC::Message& message) override;
62 // Call this method to load the resource synchronously (i.e., in one shot).
63 // This is an alternative to the StartAsync method. Be warned that this method
64 // will block the calling thread until the resource is fully downloaded or an
65 // error occurs. It could block the calling thread for a long time, so only
66 // use this if you really need it! There is also no way for the caller to
67 // interrupt this method. Errors are reported via the status field of the
68 // response parameter.
69 void StartSync(const RequestInfo& request_info,
70 ResourceRequestBody* request_body,
71 SyncLoadResponse* response);
73 // Call this method to initiate the request. If this method succeeds, then
74 // the peer's methods will be called asynchronously to report various events.
75 // Returns the request id.
76 virtual int StartAsync(const RequestInfo& request_info,
77 ResourceRequestBody* request_body,
78 RequestPeer* peer);
80 // Removes a request from the |pending_requests_| list, returning true if the
81 // request was found and removed.
82 bool RemovePendingRequest(int request_id);
84 // Cancels a request in the |pending_requests_| list. The request will be
85 // removed from the dispatcher as well.
86 virtual void Cancel(int request_id);
88 // Toggles the is_deferred attribute for the specified request.
89 void SetDefersLoading(int request_id, bool value);
91 // Indicates the priority of the specified request changed.
92 void DidChangePriority(int request_id,
93 net::RequestPriority new_priority,
94 int intra_priority_value);
96 // The provided data receiver will receive incoming resource data rather
97 // than the resource bridge.
98 bool AttachThreadedDataReceiver(
99 int request_id, blink::WebThreadedDataReceiver* threaded_data_receiver);
101 // If we have a ThreadedDataProvider attached, an OnRequestComplete message
102 // will get bounced via the background thread and then passed to this function
103 // to resume processing.
104 void CompletedRequestAfterBackgroundThreadFlush(
105 int request_id,
106 const ResourceMsg_RequestCompleteData& request_complete_data,
107 const base::TimeTicks& renderer_completion_time);
109 void set_message_sender(IPC::Sender* sender) {
110 DCHECK(sender);
111 DCHECK(pending_requests_.empty());
112 message_sender_ = sender;
115 // This does not take ownership of the delegate. It is expected that the
116 // delegate have a longer lifetime than the ResourceDispatcher.
117 void set_delegate(ResourceDispatcherDelegate* delegate) {
118 delegate_ = delegate;
121 // Remembers IO thread timestamp for next resource message.
122 void set_io_timestamp(base::TimeTicks io_timestamp) {
123 io_timestamp_ = io_timestamp;
126 void SetMainThreadTaskRunner(
127 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) {
128 main_thread_task_runner_ = main_thread_task_runner;
131 private:
132 friend class ResourceDispatcherTest;
134 typedef std::deque<IPC::Message*> MessageQueue;
135 struct PendingRequestInfo {
136 PendingRequestInfo();
138 PendingRequestInfo(RequestPeer* peer,
139 ResourceType resource_type,
140 int origin_pid,
141 const GURL& frame_origin,
142 const GURL& request_url,
143 bool download_to_file);
145 ~PendingRequestInfo();
147 RequestPeer* peer;
148 ThreadedDataProvider* threaded_data_provider;
149 ResourceType resource_type;
150 // The PID of the original process which issued this request. This gets
151 // non-zero only for a request proxied by another renderer, particularly
152 // requests from plugins.
153 int origin_pid;
154 MessageQueue deferred_message_queue;
155 bool is_deferred;
156 // Original requested url.
157 GURL url;
158 // The security origin of the frame that initiates this request.
159 GURL frame_origin;
160 // The url of the latest response even in case of redirection.
161 GURL response_url;
162 bool download_to_file;
163 linked_ptr<IPC::Message> pending_redirect_message;
164 base::TimeTicks request_start;
165 base::TimeTicks response_start;
166 base::TimeTicks completion_time;
167 linked_ptr<base::SharedMemory> buffer;
168 linked_ptr<SiteIsolationResponseMetaData> site_isolation_metadata;
169 bool blocked_response;
170 int buffer_size;
172 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList;
174 // Helper to lookup the info based on the request_id.
175 // May return NULL if the request as been canceled from the client side.
176 PendingRequestInfo* GetPendingRequestInfo(int request_id);
178 // Follows redirect, if any, for the given request.
179 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info);
181 // Message response handlers, called by the message handler for this process.
182 void OnUploadProgress(int request_id, int64 position, int64 size);
183 void OnReceivedResponse(int request_id, const ResourceResponseHead&);
184 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data);
185 void OnReceivedRedirect(int request_id,
186 const net::RedirectInfo& redirect_info,
187 const ResourceResponseHead& response_head);
188 void OnSetDataBuffer(int request_id,
189 base::SharedMemoryHandle shm_handle,
190 int shm_size,
191 base::ProcessId renderer_pid);
192 void OnReceivedData(int request_id,
193 int data_offset,
194 int data_length,
195 int encoded_data_length);
196 void OnDownloadedData(int request_id, int data_len, int encoded_data_length);
197 void OnRequestComplete(
198 int request_id,
199 const ResourceMsg_RequestCompleteData& request_complete_data);
201 // Dispatch the message to one of the message response handlers.
202 void DispatchMessage(const IPC::Message& message);
204 // Dispatch any deferred messages for the given request, provided it is not
205 // again in the deferred state.
206 void FlushDeferredMessages(int request_id);
208 void ToResourceResponseInfo(const PendingRequestInfo& request_info,
209 const ResourceResponseHead& browser_info,
210 ResourceResponseInfo* renderer_info) const;
212 base::TimeTicks ToRendererCompletionTime(
213 const PendingRequestInfo& request_info,
214 const base::TimeTicks& browser_completion_time) const;
216 // Returns timestamp provided by IO thread. If no timestamp is supplied,
217 // then current time is returned. Saved timestamp is reset, so following
218 // invocations will return current time until set_io_timestamp is called.
219 base::TimeTicks ConsumeIOTimestamp();
221 // Returns true if the message passed in is a resource related message.
222 static bool IsResourceDispatcherMessage(const IPC::Message& message);
224 // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory
225 // handle in it that we should cleanup it up nicely. This method accepts any
226 // message and determine whether the message is
227 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle.
228 static void ReleaseResourcesInDataMessage(const IPC::Message& message);
230 // Iterate through a message queue and clean up the messages by calling
231 // ReleaseResourcesInDataMessage and removing them from the queue. Intended
232 // for use on deferred message queues that are no longer needed.
233 static void ReleaseResourcesInMessageQueue(MessageQueue* queue);
235 scoped_ptr<ResourceHostMsg_Request> CreateRequest(
236 const RequestInfo& request_info,
237 ResourceRequestBody* request_body,
238 GURL* frame_origin);
240 IPC::Sender* message_sender_;
242 // All pending requests issued to the host
243 PendingRequestList pending_requests_;
245 ResourceDispatcherDelegate* delegate_;
247 // IO thread timestamp for ongoing IPC message.
248 base::TimeTicks io_timestamp_;
250 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
252 base::WeakPtrFactory<ResourceDispatcher> weak_factory_;
254 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher);
257 } // namespace content
259 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_