OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // This is the browser side of the resource dispatcher, it receives requests | |
6 // from the child process (i.e. [Renderer, Plugin, Worker]ProcessHost), and | |
7 // dispatches them to URLRequests. It then forwards the messages from the | |
8 // URLRequests back to the correct process for handling. | |
9 // | |
10 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc
e-loading | |
11 | |
12 #ifndef CONTENT_BROWSER_RENDERER_HOST_RESOURCE_DISPATCHER_HOST_IMPL_H_ | |
13 #define CONTENT_BROWSER_RENDERER_HOST_RESOURCE_DISPATCHER_HOST_IMPL_H_ | |
14 | |
15 #include <map> | |
16 #include <set> | |
17 #include <string> | |
18 #include <vector> | |
19 | |
20 #include "base/basictypes.h" | |
21 #include "base/gtest_prod_util.h" | |
22 #include "base/memory/linked_ptr.h" | |
23 #include "base/memory/scoped_ptr.h" | |
24 #include "base/time.h" | |
25 #include "base/timer.h" | |
26 #include "content/browser/download/download_resource_handler.h" | |
27 #include "content/browser/renderer_host/resource_loader.h" | |
28 #include "content/browser/renderer_host/resource_loader_delegate.h" | |
29 #include "content/common/content_export.h" | |
30 #include "content/public/browser/child_process_data.h" | |
31 #include "content/public/browser/notification_types.h" | |
32 #include "content/public/browser/resource_dispatcher_host.h" | |
33 #include "ipc/ipc_message.h" | |
34 #include "net/cookies/canonical_cookie.h" | |
35 #include "net/url_request/url_request.h" | |
36 #include "webkit/glue/resource_type.h" | |
37 | |
38 class ResourceHandler; | |
39 struct ResourceHostMsg_Request; | |
40 struct ViewMsg_SwapOut_Params; | |
41 | |
42 namespace net { | |
43 class URLRequestJobFactory; | |
44 } | |
45 | |
46 namespace webkit_blob { | |
47 class ShareableFileReference; | |
48 } | |
49 | |
50 namespace content { | |
51 class ResourceContext; | |
52 class ResourceDispatcherHostDelegate; | |
53 class ResourceMessageFilter; | |
54 class ResourceRequestInfoImpl; | |
55 class SaveFileManager; | |
56 class WebContentsImpl; | |
57 struct DownloadSaveInfo; | |
58 struct GlobalRequestID; | |
59 struct Referrer; | |
60 | |
61 class CONTENT_EXPORT ResourceDispatcherHostImpl | |
62 : public ResourceDispatcherHost, | |
63 public ResourceLoaderDelegate { | |
64 public: | |
65 ResourceDispatcherHostImpl(); | |
66 virtual ~ResourceDispatcherHostImpl(); | |
67 | |
68 // Returns the current ResourceDispatcherHostImpl. May return NULL if it | |
69 // hasn't been created yet. | |
70 static ResourceDispatcherHostImpl* Get(); | |
71 | |
72 // ResourceDispatcherHost implementation: | |
73 virtual void SetDelegate(ResourceDispatcherHostDelegate* delegate) OVERRIDE; | |
74 virtual void SetAllowCrossOriginAuthPrompt(bool value) OVERRIDE; | |
75 virtual net::Error BeginDownload( | |
76 scoped_ptr<net::URLRequest> request, | |
77 bool is_content_initiated, | |
78 ResourceContext* context, | |
79 int child_id, | |
80 int route_id, | |
81 bool prefer_cache, | |
82 scoped_ptr<DownloadSaveInfo> save_info, | |
83 const DownloadStartedCallback& started_callback) OVERRIDE; | |
84 virtual void ClearLoginDelegateForRequest(net::URLRequest* request) OVERRIDE; | |
85 virtual void BlockRequestsForRoute(int child_id, int route_id) OVERRIDE; | |
86 virtual void ResumeBlockedRequestsForRoute( | |
87 int child_id, int route_id) OVERRIDE; | |
88 | |
89 // Puts the resource dispatcher host in an inactive state (unable to begin | |
90 // new requests). Cancels all pending requests. | |
91 void Shutdown(); | |
92 | |
93 // Notify the ResourceDispatcherHostImpl of a new resource context. | |
94 void AddResourceContext(ResourceContext* context); | |
95 | |
96 // Notify the ResourceDispatcherHostImpl of a resource context destruction. | |
97 void RemoveResourceContext(ResourceContext* context); | |
98 | |
99 // Force cancels any pending requests for the given |context|. This is | |
100 // necessary to ensure that before |context| goes away, all requests | |
101 // for it are dead. | |
102 void CancelRequestsForContext(ResourceContext* context); | |
103 | |
104 // Returns true if the message was a resource message that was processed. | |
105 // If it was, message_was_ok will be false iff the message was corrupt. | |
106 bool OnMessageReceived(const IPC::Message& message, | |
107 ResourceMessageFilter* filter, | |
108 bool* message_was_ok); | |
109 | |
110 // Initiates a save file from the browser process (as opposed to a resource | |
111 // request from the renderer or another child process). | |
112 void BeginSaveFile(const GURL& url, | |
113 const Referrer& referrer, | |
114 int child_id, | |
115 int route_id, | |
116 ResourceContext* context); | |
117 | |
118 // Cancels the given request if it still exists. We ignore cancels from the | |
119 // renderer in the event of a download. | |
120 void CancelRequest(int child_id, | |
121 int request_id, | |
122 bool from_renderer); | |
123 | |
124 // Marks the request as "parked". This happens if a request is | |
125 // redirected cross-site and needs to be resumed by a new render view. | |
126 void MarkAsTransferredNavigation(const GlobalRequestID& id); | |
127 | |
128 // Returns the number of pending requests. This is designed for the unittests | |
129 int pending_requests() const { | |
130 return static_cast<int>(pending_loaders_.size()); | |
131 } | |
132 | |
133 // Intended for unit-tests only. Returns the memory cost of all the | |
134 // outstanding requests (pending and blocked) for |child_id|. | |
135 int GetOutstandingRequestsMemoryCost(int child_id) const; | |
136 | |
137 // Intended for unit-tests only. Overrides the outstanding requests bound. | |
138 void set_max_outstanding_requests_cost_per_process(int limit) { | |
139 max_outstanding_requests_cost_per_process_ = limit; | |
140 } | |
141 | |
142 // The average private bytes increase of the browser for each new pending | |
143 // request. Experimentally obtained. | |
144 static const int kAvgBytesPerOutstandingRequest = 4400; | |
145 | |
146 SaveFileManager* save_file_manager() const { | |
147 return save_file_manager_; | |
148 } | |
149 | |
150 // Called when the unload handler for a cross-site request has finished. | |
151 void OnSwapOutACK(const ViewMsg_SwapOut_Params& params); | |
152 | |
153 // Called when we want to simulate the renderer process sending | |
154 // ViewHostMsg_SwapOut_ACK in cases where the renderer has died or is | |
155 // unresponsive. | |
156 void OnSimulateSwapOutACK(const ViewMsg_SwapOut_Params& params); | |
157 | |
158 // Called when the renderer loads a resource from its internal cache. | |
159 void OnDidLoadResourceFromMemoryCache(const GURL& url, | |
160 const std::string& security_info, | |
161 const std::string& http_method, | |
162 const std::string& mime_type, | |
163 ResourceType::Type resource_type); | |
164 | |
165 // Force cancels any pending requests for the given process. | |
166 void CancelRequestsForProcess(int child_id); | |
167 | |
168 // Force cancels any pending requests for the given route id. This method | |
169 // acts like CancelRequestsForProcess when route_id is -1. | |
170 void CancelRequestsForRoute(int child_id, int route_id); | |
171 | |
172 void OnUserGesture(WebContentsImpl* contents); | |
173 | |
174 // Retrieves a net::URLRequest. Must be called from the IO thread. | |
175 net::URLRequest* GetURLRequest(const GlobalRequestID& request_id); | |
176 | |
177 void RemovePendingRequest(int child_id, int request_id); | |
178 | |
179 // Cancels any blocked request for the specified route id. | |
180 void CancelBlockedRequestsForRoute(int child_id, int route_id); | |
181 | |
182 // Maintains a collection of temp files created in support of | |
183 // the download_to_file capability. Used to grant access to the | |
184 // child process and to defer deletion of the file until it's | |
185 // no longer needed. | |
186 void RegisterDownloadedTempFile( | |
187 int child_id, int request_id, | |
188 webkit_blob::ShareableFileReference* reference); | |
189 void UnregisterDownloadedTempFile(int child_id, int request_id); | |
190 | |
191 // Needed for the sync IPC message dispatcher macros. | |
192 bool Send(IPC::Message* message); | |
193 | |
194 // Indicates whether third-party sub-content can pop-up HTTP basic auth | |
195 // dialog boxes. | |
196 bool allow_cross_origin_auth_prompt(); | |
197 | |
198 ResourceDispatcherHostDelegate* delegate() { | |
199 return delegate_; | |
200 } | |
201 | |
202 // Must be called after the ResourceRequestInfo has been created | |
203 // and associated with the request. | |
204 scoped_ptr<ResourceHandler> CreateResourceHandlerForDownload( | |
205 net::URLRequest* request, | |
206 bool is_content_initiated, | |
207 scoped_ptr<DownloadSaveInfo> save_info, | |
208 const DownloadResourceHandler::OnStartedCallback& started_cb); | |
209 | |
210 void ClearSSLClientAuthHandlerForRequest(net::URLRequest* request); | |
211 | |
212 private: | |
213 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, | |
214 TestBlockedRequestsProcessDies); | |
215 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, | |
216 IncrementOutstandingRequestsMemoryCost); | |
217 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, | |
218 CalculateApproximateMemoryCost); | |
219 | |
220 class ShutdownTask; | |
221 | |
222 friend class ShutdownTask; | |
223 | |
224 // ResourceLoaderDelegate implementation: | |
225 virtual ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( | |
226 ResourceLoader* loader, | |
227 net::AuthChallengeInfo* auth_info) OVERRIDE; | |
228 virtual bool AcceptAuthRequest( | |
229 ResourceLoader* loader, | |
230 net::AuthChallengeInfo* auth_info) OVERRIDE; | |
231 virtual bool AcceptSSLClientCertificateRequest( | |
232 ResourceLoader* loader, | |
233 net::SSLCertRequestInfo* cert_info) OVERRIDE; | |
234 virtual bool HandleExternalProtocol(ResourceLoader* loader, | |
235 const GURL& url) OVERRIDE; | |
236 virtual void DidStartRequest(ResourceLoader* loader) OVERRIDE; | |
237 virtual void DidReceiveRedirect(ResourceLoader* loader, | |
238 const GURL& new_url) OVERRIDE; | |
239 virtual void DidReceiveResponse(ResourceLoader* loader) OVERRIDE; | |
240 virtual void DidFinishLoading(ResourceLoader* loader) OVERRIDE; | |
241 | |
242 // Extracts the render view/process host's identifiers from the given request | |
243 // and places them in the given out params (both required). If there are no | |
244 // such IDs associated with the request (such as non-page-related requests), | |
245 // this function will return false and both out params will be -1. | |
246 static bool RenderViewForRequest(const net::URLRequest* request, | |
247 int* render_process_host_id, | |
248 int* render_view_host_id); | |
249 | |
250 // A shutdown helper that runs on the IO thread. | |
251 void OnShutdown(); | |
252 | |
253 // The real implementation of the OnSwapOutACK logic. OnSwapOutACK and | |
254 // OnSimulateSwapOutACK just call this method, supplying the |timed_out| | |
255 // parameter, which indicates whether the call is due to a timeout while | |
256 // waiting for SwapOut acknowledgement from the renderer process. | |
257 void HandleSwapOutACK(const ViewMsg_SwapOut_Params& params, bool timed_out); | |
258 | |
259 // Helper function for regular and download requests. | |
260 void BeginRequestInternal(scoped_ptr<net::URLRequest> request, | |
261 scoped_ptr<ResourceHandler> handler); | |
262 | |
263 void StartLoading(ResourceRequestInfoImpl* info, | |
264 const linked_ptr<ResourceLoader>& loader); | |
265 | |
266 // Updates the "cost" of outstanding requests for |child_id|. | |
267 // The "cost" approximates how many bytes are consumed by all the in-memory | |
268 // data structures supporting this request (net::URLRequest object, | |
269 // HttpNetworkTransaction, etc...). | |
270 // The value of |cost| is added to the running total, and the resulting | |
271 // sum is returned. | |
272 int IncrementOutstandingRequestsMemoryCost(int cost, | |
273 int child_id); | |
274 | |
275 // Estimate how much heap space |request| will consume to run. | |
276 static int CalculateApproximateMemoryCost(net::URLRequest* request); | |
277 | |
278 // The list of all requests that we have pending. This list is not really | |
279 // optimized, and assumes that we have relatively few requests pending at once | |
280 // since some operations require brute-force searching of the list. | |
281 // | |
282 // It may be enhanced in the future to provide some kind of prioritization | |
283 // mechanism. We should also consider a hashtable or binary tree if it turns | |
284 // out we have a lot of things here. | |
285 typedef std::map<GlobalRequestID, linked_ptr<ResourceLoader> > LoaderMap; | |
286 | |
287 // Deletes the pending request identified by the iterator passed in. | |
288 // This function will invalidate the iterator passed in. Callers should | |
289 // not rely on this iterator being valid on return. | |
290 void RemovePendingLoader(const LoaderMap::iterator& iter); | |
291 | |
292 // Checks all pending requests and updates the load states and upload | |
293 // progress if necessary. | |
294 void UpdateLoadStates(); | |
295 | |
296 // Resumes or cancels (if |cancel_requests| is true) any blocked requests. | |
297 void ProcessBlockedRequestsForRoute(int child_id, | |
298 int route_id, | |
299 bool cancel_requests); | |
300 | |
301 void OnRequestResource(const IPC::Message& msg, | |
302 int request_id, | |
303 const ResourceHostMsg_Request& request_data); | |
304 void OnSyncLoad(int request_id, | |
305 const ResourceHostMsg_Request& request_data, | |
306 IPC::Message* sync_result); | |
307 void BeginRequest(int request_id, | |
308 const ResourceHostMsg_Request& request_data, | |
309 IPC::Message* sync_result, // only valid for sync | |
310 int route_id); // only valid for async | |
311 void OnDataReceivedACK(int request_id); | |
312 void OnDataDownloadedACK(int request_id); | |
313 void OnUploadProgressACK(int request_id); | |
314 void OnCancelRequest(int request_id); | |
315 void OnFollowRedirect(int request_id, | |
316 bool has_new_first_party_for_cookies, | |
317 const GURL& new_first_party_for_cookies); | |
318 void OnReleaseDownloadedFile(int request_id); | |
319 | |
320 // Creates ResourceRequestInfoImpl for a download or page save. | |
321 // |download| should be true if the request is a file download. | |
322 ResourceRequestInfoImpl* CreateRequestInfo( | |
323 int child_id, | |
324 int route_id, | |
325 bool download, | |
326 ResourceContext* context); | |
327 | |
328 // Relationship of resource being authenticated with the top level page. | |
329 enum HttpAuthResourceType { | |
330 HTTP_AUTH_RESOURCE_TOP, // Top-level page itself | |
331 HTTP_AUTH_RESOURCE_SAME_DOMAIN, // Sub-content from same domain | |
332 HTTP_AUTH_RESOURCE_BLOCKED_CROSS, // Blocked Sub-content from cross domain | |
333 HTTP_AUTH_RESOURCE_ALLOWED_CROSS, // Allowed Sub-content per command line | |
334 HTTP_AUTH_RESOURCE_LAST | |
335 }; | |
336 | |
337 HttpAuthResourceType HttpAuthResourceTypeOf(net::URLRequest* request); | |
338 | |
339 // Returns whether the URLRequest identified by |transferred_request_id| is | |
340 // currently in the process of being transferred to a different renderer. | |
341 // This happens if a request is redirected cross-site and needs to be resumed | |
342 // by a new render view. | |
343 bool IsTransferredNavigation( | |
344 const GlobalRequestID& transferred_request_id) const; | |
345 | |
346 ResourceLoader* GetLoader(const GlobalRequestID& id) const; | |
347 ResourceLoader* GetLoader(int child_id, int request_id) const; | |
348 | |
349 LoaderMap pending_loaders_; | |
350 | |
351 // Collection of temp files downloaded for child processes via | |
352 // the download_to_file mechanism. We avoid deleting them until | |
353 // the client no longer needs them. | |
354 typedef std::map<int, scoped_refptr<webkit_blob::ShareableFileReference> > | |
355 DeletableFilesMap; // key is request id | |
356 typedef std::map<int, DeletableFilesMap> | |
357 RegisteredTempFiles; // key is child process id | |
358 RegisteredTempFiles registered_temp_files_; | |
359 | |
360 // A timer that periodically calls UpdateLoadStates while pending_requests_ | |
361 // is not empty. | |
362 scoped_ptr<base::RepeatingTimer<ResourceDispatcherHostImpl> > | |
363 update_load_states_timer_; | |
364 | |
365 // We own the save file manager. | |
366 scoped_refptr<SaveFileManager> save_file_manager_; | |
367 | |
368 // Request ID for browser initiated requests. request_ids generated by | |
369 // child processes are counted up from 0, while browser created requests | |
370 // start at -2 and go down from there. (We need to start at -2 because -1 is | |
371 // used as a special value all over the resource_dispatcher_host for | |
372 // uninitialized variables.) This way, we no longer have the unlikely (but | |
373 // observed in the real world!) event where we have two requests with the same | |
374 // request_id_. | |
375 int request_id_; | |
376 | |
377 // True if the resource dispatcher host has been shut down. | |
378 bool is_shutdown_; | |
379 | |
380 typedef std::vector<linked_ptr<ResourceLoader> > BlockedLoadersList; | |
381 typedef std::pair<int, int> ProcessRouteIDs; | |
382 typedef std::map<ProcessRouteIDs, BlockedLoadersList*> BlockedLoadersMap; | |
383 BlockedLoadersMap blocked_loaders_map_; | |
384 | |
385 // Maps the child_ids to the approximate number of bytes | |
386 // being used to service its resource requests. No entry implies 0 cost. | |
387 typedef std::map<int, int> OutstandingRequestsMemoryCostMap; | |
388 OutstandingRequestsMemoryCostMap outstanding_requests_memory_cost_map_; | |
389 | |
390 // |max_outstanding_requests_cost_per_process_| is the upper bound on how | |
391 // many outstanding requests can be issued per child process host. | |
392 // The constraint is expressed in terms of bytes (where the cost of | |
393 // individual requests is given by CalculateApproximateMemoryCost). | |
394 // The total number of outstanding requests is roughly: | |
395 // (max_outstanding_requests_cost_per_process_ / | |
396 // kAvgBytesPerOutstandingRequest) | |
397 int max_outstanding_requests_cost_per_process_; | |
398 | |
399 // Time of the last user gesture. Stored so that we can add a load | |
400 // flag to requests occurring soon after a gesture to indicate they | |
401 // may be because of explicit user action. | |
402 base::TimeTicks last_user_gesture_time_; | |
403 | |
404 // Used during IPC message dispatching so that the handlers can get a pointer | |
405 // to the source of the message. | |
406 ResourceMessageFilter* filter_; | |
407 | |
408 ResourceDispatcherHostDelegate* delegate_; | |
409 | |
410 bool allow_cross_origin_auth_prompt_; | |
411 | |
412 // http://crbug.com/90971 - Assists in tracking down use-after-frees on | |
413 // shutdown. | |
414 std::set<const ResourceContext*> active_resource_contexts_; | |
415 | |
416 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcherHostImpl); | |
417 }; | |
418 | |
419 } // namespace content | |
420 | |
421 #endif // CONTENT_BROWSER_RENDERER_HOST_RESOURCE_DISPATCHER_HOST_IMPL_H_ | |
OLD | NEW |