Remove DnsConfigServiceTest.GetSystemConfig test
[chromium-blink-merge.git] / content / child / resource_dispatcher.h
blobb09a0a4a04c4284fa1ea9666d952ab2fb138d71b
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 bool blocked_response;
172 int buffer_size;
174 typedef base::hash_map<int, PendingRequestInfo> PendingRequestList;
176 // Helper to lookup the info based on the request_id.
177 // May return NULL if the request as been canceled from the client side.
178 PendingRequestInfo* GetPendingRequestInfo(int request_id);
180 // Follows redirect, if any, for the given request.
181 void FollowPendingRedirect(int request_id, PendingRequestInfo& request_info);
183 // Message response handlers, called by the message handler for this process.
184 void OnUploadProgress(int request_id, int64 position, int64 size);
185 void OnReceivedResponse(int request_id, const ResourceResponseHead&);
186 void OnReceivedCachedMetadata(int request_id, const std::vector<char>& data);
187 void OnReceivedRedirect(int request_id,
188 const net::RedirectInfo& redirect_info,
189 const ResourceResponseHead& response_head);
190 void OnSetDataBuffer(int request_id,
191 base::SharedMemoryHandle shm_handle,
192 int shm_size,
193 base::ProcessId renderer_pid);
194 void OnReceivedData(int request_id,
195 int data_offset,
196 int data_length,
197 int encoded_data_length);
198 void OnDownloadedData(int request_id, int data_len, int encoded_data_length);
199 void OnRequestComplete(
200 int request_id,
201 const ResourceMsg_RequestCompleteData& request_complete_data);
203 // Dispatch the message to one of the message response handlers.
204 void DispatchMessage(const IPC::Message& message);
206 // Dispatch any deferred messages for the given request, provided it is not
207 // again in the deferred state.
208 void FlushDeferredMessages(int request_id);
210 void ToResourceResponseInfo(const PendingRequestInfo& request_info,
211 const ResourceResponseHead& browser_info,
212 ResourceResponseInfo* renderer_info) const;
214 base::TimeTicks ToRendererCompletionTime(
215 const PendingRequestInfo& request_info,
216 const base::TimeTicks& browser_completion_time) const;
218 // Returns timestamp provided by IO thread. If no timestamp is supplied,
219 // then current time is returned. Saved timestamp is reset, so following
220 // invocations will return current time until set_io_timestamp is called.
221 base::TimeTicks ConsumeIOTimestamp();
223 // Returns true if the message passed in is a resource related message.
224 static bool IsResourceDispatcherMessage(const IPC::Message& message);
226 // ViewHostMsg_Resource_DataReceived is not POD, it has a shared memory
227 // handle in it that we should cleanup it up nicely. This method accepts any
228 // message and determine whether the message is
229 // ViewHostMsg_Resource_DataReceived and clean up the shared memory handle.
230 static void ReleaseResourcesInDataMessage(const IPC::Message& message);
232 // Iterate through a message queue and clean up the messages by calling
233 // ReleaseResourcesInDataMessage and removing them from the queue. Intended
234 // for use on deferred message queues that are no longer needed.
235 static void ReleaseResourcesInMessageQueue(MessageQueue* queue);
237 scoped_ptr<ResourceHostMsg_Request> CreateRequest(
238 const RequestInfo& request_info,
239 ResourceRequestBody* request_body,
240 GURL* frame_origin);
242 IPC::Sender* message_sender_;
244 // All pending requests issued to the host
245 PendingRequestList pending_requests_;
247 ResourceDispatcherDelegate* delegate_;
249 // IO thread timestamp for ongoing IPC message.
250 base::TimeTicks io_timestamp_;
252 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_;
254 base::WeakPtrFactory<ResourceDispatcher> weak_factory_;
256 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcher);
259 } // namespace content
261 #endif // CONTENT_CHILD_RESOURCE_DISPATCHER_H_