Infobar material design refresh: bg color
[chromium-blink-merge.git] / content / child / resource_dispatcher.h
blob2cf3874eba9217fa34eaaa3445aa06f69454bb56
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 class SharedMemoryReceivedDataFactory;
47 struct SiteIsolationResponseMetaData;
48 struct SyncLoadResponse;
50 // This class serves as a communication interface to the ResourceDispatcherHost
51 // in the browser process. It can be used from any child process.
52 // Virtual methods are for tests.
53 class CONTENT_EXPORT ResourceDispatcher : public IPC::Listener {
54 public:
55 ResourceDispatcher(
56 IPC::Sender* sender,
57 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner);
58 ~ResourceDispatcher() override;
60 // IPC::Listener implementation.
61 bool OnMessageReceived(const IPC::Message& message) override;
63 // Call this method to load the resource synchronously (i.e., in one shot).
64 // This is an alternative to the StartAsync method. Be warned that this method
65 // will block the calling thread until the resource is fully downloaded or an
66 // error occurs. It could block the calling thread for a long time, so only
67 // use this if you really need it! There is also no way for the caller to
68 // interrupt this method. Errors are reported via the status field of the
69 // response parameter.
70 void StartSync(const RequestInfo& request_info,
71 ResourceRequestBody* request_body,
72 SyncLoadResponse* response);
74 // Call this method to initiate the request. If this method succeeds, then
75 // the peer's methods will be called asynchronously to report various events.
76 // Returns the request id.
77 virtual int StartAsync(const RequestInfo& request_info,
78 ResourceRequestBody* request_body,
79 RequestPeer* peer);
81 // Removes a request from the |pending_requests_| list, returning true if the
82 // request was found and removed.
83 bool RemovePendingRequest(int request_id);
85 // Cancels a request in the |pending_requests_| list. The request will be
86 // removed from the dispatcher as well.
87 virtual void Cancel(int request_id);
89 // Toggles the is_deferred attribute for the specified request.
90 void SetDefersLoading(int request_id, bool value);
92 // Indicates the priority of the specified request changed.
93 void DidChangePriority(int request_id,
94 net::RequestPriority new_priority,
95 int intra_priority_value);
97 // The provided data receiver will receive incoming resource data rather
98 // than the resource bridge.
99 bool AttachThreadedDataReceiver(
100 int request_id, blink::WebThreadedDataReceiver* threaded_data_receiver);
102 // If we have a ThreadedDataProvider attached, an OnRequestComplete message
103 // will get bounced via the background thread and then passed to this function
104 // to resume processing.
105 void CompletedRequestAfterBackgroundThreadFlush(
106 int request_id,
107 const ResourceMsg_RequestCompleteData& request_complete_data,
108 const base::TimeTicks& renderer_completion_time);
110 void set_message_sender(IPC::Sender* sender) {
111 DCHECK(sender);
112 DCHECK(pending_requests_.empty());
113 message_sender_ = sender;
116 // This does not take ownership of the delegate. It is expected that the
117 // delegate have a longer lifetime than the ResourceDispatcher.
118 void set_delegate(ResourceDispatcherDelegate* delegate) {
119 delegate_ = delegate;
122 // Remembers IO thread timestamp for next resource message.
123 void set_io_timestamp(base::TimeTicks io_timestamp) {
124 io_timestamp_ = io_timestamp;
127 void SetMainThreadTaskRunner(
128 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) {
129 main_thread_task_runner_ = main_thread_task_runner;
132 private:
133 friend class ResourceDispatcherTest;
135 typedef std::deque<IPC::Message*> MessageQueue;
136 struct PendingRequestInfo {
137 PendingRequestInfo();
139 PendingRequestInfo(RequestPeer* peer,
140 ResourceType resource_type,
141 int origin_pid,
142 const GURL& frame_origin,
143 const GURL& request_url,
144 bool download_to_file);
146 ~PendingRequestInfo();
148 RequestPeer* peer;
149 ThreadedDataProvider* threaded_data_provider;
150 ResourceType resource_type;
151 // The PID of the original process which issued this request. This gets
152 // non-zero only for a request proxied by another renderer, particularly
153 // requests from plugins.
154 int origin_pid;
155 MessageQueue deferred_message_queue;
156 bool is_deferred;
157 // Original requested url.
158 GURL url;
159 // The security origin of the frame that initiates this request.
160 GURL frame_origin;
161 // The url of the latest response even in case of redirection.
162 GURL response_url;
163 bool download_to_file;
164 linked_ptr<IPC::Message> pending_redirect_message;
165 base::TimeTicks request_start;
166 base::TimeTicks response_start;
167 base::TimeTicks completion_time;
168 linked_ptr<base::SharedMemory> buffer;
169 scoped_refptr<SharedMemoryReceivedDataFactory> received_data_factory;
170 linked_ptr<SiteIsolationResponseMetaData> site_isolation_metadata;
171 int buffer_size;
173 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList;
175 // Helper to lookup the info based on the request_id.
176 // May return NULL if the request as been canceled from the client side.
177 PendingRequestInfo* GetPendingRequestInfo(int request_id);
179 // Follows redirect, if any, for the given request.
180 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info);
182 // Message response handlers, called by the message handler for this process.
183 void OnUploadProgress(int request_id, int64 position, int64 size);
184 void OnReceivedResponse(int request_id, const ResourceResponseHead&);
185 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data);
186 void OnReceivedRedirect(int request_id,
187 const net::RedirectInfo& redirect_info,
188 const ResourceResponseHead& response_head);
189 void OnSetDataBuffer(int request_id,
190 base::SharedMemoryHandle shm_handle,
191 int shm_size,
192 base::ProcessId renderer_pid);
193 void OnReceivedData(int request_id,
194 int data_offset,
195 int data_length,
196 int encoded_data_length);
197 void OnDownloadedData(int request_id, int data_len, int encoded_data_length);
198 void OnRequestComplete(
199 int request_id,
200 const ResourceMsg_RequestCompleteData& request_complete_data);
202 // Dispatch the message to one of the message response handlers.
203 void DispatchMessage(const IPC::Message& message);
205 // Dispatch any deferred messages for the given request, provided it is not
206 // again in the deferred state.
207 void FlushDeferredMessages(int request_id);
209 void ToResourceResponseInfo(const PendingRequestInfo& request_info,
210 const ResourceResponseHead& browser_info,
211 ResourceResponseInfo* renderer_info) const;
213 base::TimeTicks ToRendererCompletionTime(
214 const PendingRequestInfo& request_info,
215 const base::TimeTicks& browser_completion_time) const;
217 // Returns timestamp provided by IO thread. If no timestamp is supplied,
218 // then current time is returned. Saved timestamp is reset, so following
219 // invocations will return current time until set_io_timestamp is called.
220 base::TimeTicks ConsumeIOTimestamp();
222 // Returns true if the message passed in is a resource related message.
223 static bool IsResourceDispatcherMessage(const IPC::Message& message);
225 // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory
226 // handle in it that we should cleanup it up nicely. This method accepts any
227 // message and determine whether the message is
228 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle.
229 static void ReleaseResourcesInDataMessage(const IPC::Message& message);
231 // Iterate through a message queue and clean up the messages by calling
232 // ReleaseResourcesInDataMessage and removing them from the queue. Intended
233 // for use on deferred message queues that are no longer needed.
234 static void ReleaseResourcesInMessageQueue(MessageQueue* queue);
236 scoped_ptr<ResourceHostMsg_Request> CreateRequest(
237 const RequestInfo& request_info,
238 ResourceRequestBody* request_body,
239 GURL* frame_origin);
241 IPC::Sender* message_sender_;
243 // All pending requests issued to the host
244 PendingRequestList pending_requests_;
246 ResourceDispatcherDelegate* delegate_;
248 // IO thread timestamp for ongoing IPC message.
249 base::TimeTicks io_timestamp_;
251 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
253 base::WeakPtrFactory<ResourceDispatcher> weak_factory_;
255 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher);
258 } // namespace content
260 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_