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 // 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.
10 // See http://dev.chromium.org/developers/design-documents/multi-process-resource-loading
12 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_
13 #define CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_
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/observer_list.h"
25 #include "base/time/time.h"
26 #include "base/timer/timer.h"
27 #include "content/browser/download/download_resource_handler.h"
28 #include "content/browser/loader/global_routing_id.h"
29 #include "content/browser/loader/resource_loader.h"
30 #include "content/browser/loader/resource_loader_delegate.h"
31 #include "content/browser/loader/resource_scheduler.h"
32 #include "content/common/content_export.h"
33 #include "content/common/resource_request_body.h"
34 #include "content/public/browser/child_process_data.h"
35 #include "content/public/browser/download_item.h"
36 #include "content/public/browser/download_url_parameters.h"
37 #include "content/public/browser/global_request_id.h"
38 #include "content/public/browser/notification_types.h"
39 #include "content/public/browser/resource_dispatcher_host.h"
40 #include "content/public/common/resource_type.h"
41 #include "ipc/ipc_message.h"
42 #include "net/cookies/canonical_cookie.h"
43 #include "net/url_request/url_request.h"
45 class ResourceHandler
;
46 struct ResourceHostMsg_Request
;
49 class URLRequestJobFactory
;
53 class ShareableFileReference
;
57 class ResourceContext
;
58 class ResourceDispatcherHostDelegate
;
59 class ResourceMessageDelegate
;
60 class ResourceMessageFilter
;
61 class ResourceRequestInfoImpl
;
62 class SaveFileManager
;
63 class WebContentsImpl
;
64 struct DownloadSaveInfo
;
65 struct NavigationRequestInfo
;
68 class CONTENT_EXPORT ResourceDispatcherHostImpl
69 : public ResourceDispatcherHost
,
70 public ResourceLoaderDelegate
{
72 ResourceDispatcherHostImpl();
73 virtual ~ResourceDispatcherHostImpl();
75 // Returns the current ResourceDispatcherHostImpl. May return NULL if it
76 // hasn't been created yet.
77 static ResourceDispatcherHostImpl
* Get();
79 // ResourceDispatcherHost implementation:
80 virtual void SetDelegate(ResourceDispatcherHostDelegate
* delegate
) OVERRIDE
;
81 virtual void SetAllowCrossOriginAuthPrompt(bool value
) OVERRIDE
;
82 virtual DownloadInterruptReason
BeginDownload(
83 scoped_ptr
<net::URLRequest
> request
,
84 const Referrer
& referrer
,
85 bool is_content_initiated
,
86 ResourceContext
* context
,
90 scoped_ptr
<DownloadSaveInfo
> save_info
,
92 const DownloadStartedCallback
& started_callback
) OVERRIDE
;
93 virtual void ClearLoginDelegateForRequest(net::URLRequest
* request
) OVERRIDE
;
94 virtual void BlockRequestsForRoute(int child_id
, int route_id
) OVERRIDE
;
95 virtual void ResumeBlockedRequestsForRoute(
96 int child_id
, int route_id
) OVERRIDE
;
98 // Puts the resource dispatcher host in an inactive state (unable to begin
99 // new requests). Cancels all pending requests.
102 // Notify the ResourceDispatcherHostImpl of a new resource context.
103 void AddResourceContext(ResourceContext
* context
);
105 // Notify the ResourceDispatcherHostImpl of a resource context destruction.
106 void RemoveResourceContext(ResourceContext
* context
);
108 // Resumes a request that deferred at response start.
109 void ResumeResponseDeferredAtStart(const GlobalRequestID
& id
);
111 // Force cancels any pending requests for the given |context|. This is
112 // necessary to ensure that before |context| goes away, all requests
114 void CancelRequestsForContext(ResourceContext
* context
);
116 // Returns true if the message was a resource message that was processed.
117 bool OnMessageReceived(const IPC::Message
& message
,
118 ResourceMessageFilter
* filter
);
120 // Initiates a save file from the browser process (as opposed to a resource
121 // request from the renderer or another child process).
122 void BeginSaveFile(const GURL
& url
,
123 const Referrer
& referrer
,
126 ResourceContext
* context
);
128 // Cancels the given request if it still exists.
129 void CancelRequest(int child_id
, int request_id
);
131 // Marks the request as "parked". This happens if a request is
132 // redirected cross-site and needs to be resumed by a new render view.
133 void MarkAsTransferredNavigation(const GlobalRequestID
& id
);
135 // Cancels a request previously marked as being transferred, for use when a
136 // navigation was cancelled.
137 void CancelTransferringNavigation(const GlobalRequestID
& id
);
139 // Resumes the request without transferring it to a new render view.
140 void ResumeDeferredNavigation(const GlobalRequestID
& id
);
142 // Returns the number of pending requests. This is designed for the unittests
143 int pending_requests() const {
144 return static_cast<int>(pending_loaders_
.size());
147 // Intended for unit-tests only. Overrides the outstanding requests bound.
148 void set_max_outstanding_requests_cost_per_process(int limit
) {
149 max_outstanding_requests_cost_per_process_
= limit
;
151 void set_max_num_in_flight_requests_per_process(int limit
) {
152 max_num_in_flight_requests_per_process_
= limit
;
154 void set_max_num_in_flight_requests(int limit
) {
155 max_num_in_flight_requests_
= limit
;
158 // The average private bytes increase of the browser for each new pending
159 // request. Experimentally obtained.
160 static const int kAvgBytesPerOutstandingRequest
= 4400;
162 SaveFileManager
* save_file_manager() const {
163 return save_file_manager_
.get();
166 // Called when a RenderViewHost is created.
167 void OnRenderViewHostCreated(int child_id
, int route_id
, bool is_visible
);
169 // Called when a RenderViewHost is deleted.
170 void OnRenderViewHostDeleted(int child_id
, int route_id
);
172 // Called when a RenderViewHost is hidden.
173 void OnRenderViewHostWasHidden(int child_id
, int route_id
);
175 // Called when a RenderViewHost is shown.
176 void OnRenderViewHostWasShown(int child_id
, int route_id
);
178 // Force cancels any pending requests for the given process.
179 void CancelRequestsForProcess(int child_id
);
181 void OnUserGesture(WebContentsImpl
* contents
);
183 // Retrieves a net::URLRequest. Must be called from the IO thread.
184 net::URLRequest
* GetURLRequest(const GlobalRequestID
& request_id
);
186 void RemovePendingRequest(int child_id
, int request_id
);
188 // Cancels any blocked request for the specified route id.
189 void CancelBlockedRequestsForRoute(int child_id
, int route_id
);
191 // Maintains a collection of temp files created in support of
192 // the download_to_file capability. Used to grant access to the
193 // child process and to defer deletion of the file until it's
195 void RegisterDownloadedTempFile(
196 int child_id
, int request_id
,
197 const base::FilePath
& file_path
);
198 void UnregisterDownloadedTempFile(int child_id
, int request_id
);
200 // Needed for the sync IPC message dispatcher macros.
201 bool Send(IPC::Message
* message
);
203 // Indicates whether third-party sub-content can pop-up HTTP basic auth
205 bool allow_cross_origin_auth_prompt();
207 ResourceDispatcherHostDelegate
* delegate() {
211 // Must be called after the ResourceRequestInfo has been created
212 // and associated with the request.
213 // |id| should be |content::DownloadItem::kInvalidId| to request automatic
215 scoped_ptr
<ResourceHandler
> CreateResourceHandlerForDownload(
216 net::URLRequest
* request
,
217 bool is_content_initiated
,
220 scoped_ptr
<DownloadSaveInfo
> save_info
,
221 const DownloadUrlParameters::OnStartedCallback
& started_cb
);
223 // Must be called after the ResourceRequestInfo has been created
224 // and associated with the request. If |payload| is set to a non-empty value,
225 // the value will be sent to the old resource handler instead of cancelling
226 // it, except on HTTP errors.
227 scoped_ptr
<ResourceHandler
> MaybeInterceptAsStream(
228 net::URLRequest
* request
,
229 ResourceResponse
* response
,
230 std::string
* payload
);
232 void ClearSSLClientAuthHandlerForRequest(net::URLRequest
* request
);
234 ResourceScheduler
* scheduler() { return scheduler_
.get(); }
236 // Called by a ResourceHandler when it's ready to start reading data and
237 // sending it to the renderer. Returns true if there are enough file
238 // descriptors available for the shared memory buffer. If false is returned,
239 // the request should cancel.
240 bool HasSufficientResourcesForRequest(const net::URLRequest
* request_
);
242 // Called by a ResourceHandler after it has finished its request and is done
243 // using its shared memory buffer. Frees up that file descriptor to be used
245 void FinishedWithResourcesForRequest(const net::URLRequest
* request_
);
247 // Called by NavigationRequest to start a navigation request in the node
248 // identified by |frame_node_id|.
249 void NavigationRequest(const NavigationRequestInfo
& info
,
250 scoped_refptr
<ResourceRequestBody
> request_body
,
251 int64 frame_node_id
);
254 friend class ResourceDispatcherHostTest
;
256 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest
,
257 TestBlockedRequestsProcessDies
);
258 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest
,
259 CalculateApproximateMemoryCost
);
260 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest
,
261 DetachableResourceTimesOut
);
262 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest
,
263 TestProcessCancelDetachableTimesOut
);
267 struct OustandingRequestsStats
{
272 friend class ShutdownTask
;
273 friend class ResourceMessageDelegate
;
275 // ResourceLoaderDelegate implementation:
276 virtual ResourceDispatcherHostLoginDelegate
* CreateLoginDelegate(
277 ResourceLoader
* loader
,
278 net::AuthChallengeInfo
* auth_info
) OVERRIDE
;
279 virtual bool HandleExternalProtocol(ResourceLoader
* loader
,
280 const GURL
& url
) OVERRIDE
;
281 virtual void DidStartRequest(ResourceLoader
* loader
) OVERRIDE
;
282 virtual void DidReceiveRedirect(ResourceLoader
* loader
,
283 const GURL
& new_url
) OVERRIDE
;
284 virtual void DidReceiveResponse(ResourceLoader
* loader
) OVERRIDE
;
285 virtual void DidFinishLoading(ResourceLoader
* loader
) OVERRIDE
;
287 // An init helper that runs on the IO thread.
290 // A shutdown helper that runs on the IO thread.
293 // Helper function for regular and download requests.
294 void BeginRequestInternal(scoped_ptr
<net::URLRequest
> request
,
295 scoped_ptr
<ResourceHandler
> handler
);
297 void StartLoading(ResourceRequestInfoImpl
* info
,
298 const linked_ptr
<ResourceLoader
>& loader
);
300 // We keep track of how much memory each request needs and how many requests
301 // are issued by each renderer. These are known as OustandingRequestStats.
302 // Memory limits apply to all requests sent to us by the renderers. There is a
303 // limit for each renderer. File descriptor limits apply to requests that are
304 // receiving their body. These are known as in-flight requests. There is a
305 // global limit that applies for the browser process. Each render is allowed
306 // to use up to a fraction of that.
308 // Returns the OustandingRequestsStats for |info|'s renderer, or an empty
309 // struct if that renderer has no outstanding requests.
310 OustandingRequestsStats
GetOutstandingRequestsStats(
311 const ResourceRequestInfoImpl
& info
);
313 // Updates |outstanding_requests_stats_map_| with the specified |stats| for
314 // the renderer that made the request in |info|.
315 void UpdateOutstandingRequestsStats(const ResourceRequestInfoImpl
& info
,
316 const OustandingRequestsStats
& stats
);
318 // Called every time an outstanding request is created or deleted. |count|
319 // indicates whether the request is new or deleted. |count| must be 1 or -1.
320 OustandingRequestsStats
IncrementOutstandingRequestsMemory(
322 const ResourceRequestInfoImpl
& info
);
324 // Called every time an in flight request is issued or finished. |count|
325 // indicates whether the request is issuing or finishing. |count| must be 1
327 OustandingRequestsStats
IncrementOutstandingRequestsCount(
329 const ResourceRequestInfoImpl
& info
);
331 // Estimate how much heap space |request| will consume to run.
332 static int CalculateApproximateMemoryCost(net::URLRequest
* request
);
334 // Force cancels any pending requests for the given route id. This method
335 // acts like CancelRequestsForProcess when route_id is -1.
336 void CancelRequestsForRoute(int child_id
, int route_id
);
338 // The list of all requests that we have pending. This list is not really
339 // optimized, and assumes that we have relatively few requests pending at once
340 // since some operations require brute-force searching of the list.
342 // It may be enhanced in the future to provide some kind of prioritization
343 // mechanism. We should also consider a hashtable or binary tree if it turns
344 // out we have a lot of things here.
345 typedef std::map
<GlobalRequestID
, linked_ptr
<ResourceLoader
> > LoaderMap
;
347 // Deletes the pending request identified by the iterator passed in.
348 // This function will invalidate the iterator passed in. Callers should
349 // not rely on this iterator being valid on return.
350 void RemovePendingLoader(const LoaderMap::iterator
& iter
);
352 // Checks all pending requests and updates the load states and upload
353 // progress if necessary.
354 void UpdateLoadStates();
356 // Resumes or cancels (if |cancel_requests| is true) any blocked requests.
357 void ProcessBlockedRequestsForRoute(int child_id
,
359 bool cancel_requests
);
361 void OnRequestResource(int routing_id
,
363 const ResourceHostMsg_Request
& request_data
);
364 void OnSyncLoad(int request_id
,
365 const ResourceHostMsg_Request
& request_data
,
366 IPC::Message
* sync_result
);
368 // Update the ResourceRequestInfo and internal maps when a request is
369 // transferred from one process to another.
370 void UpdateRequestForTransfer(int child_id
,
373 const ResourceHostMsg_Request
& request_data
,
374 const linked_ptr
<ResourceLoader
>& loader
);
376 void BeginRequest(int request_id
,
377 const ResourceHostMsg_Request
& request_data
,
378 IPC::Message
* sync_result
, // only valid for sync
379 int route_id
); // only valid for async
381 // Creates a ResourceHandler to be used by BeginRequest() for normal resource
383 scoped_ptr
<ResourceHandler
> CreateResourceHandler(
384 net::URLRequest
* request
,
385 const ResourceHostMsg_Request
& request_data
,
386 IPC::Message
* sync_result
,
390 ResourceContext
* resource_context
);
392 void OnDataDownloadedACK(int request_id
);
393 void OnUploadProgressACK(int request_id
);
394 void OnCancelRequest(int request_id
);
395 void OnReleaseDownloadedFile(int request_id
);
397 // Creates ResourceRequestInfoImpl for a download or page save.
398 // |download| should be true if the request is a file download.
399 ResourceRequestInfoImpl
* CreateRequestInfo(
403 ResourceContext
* context
);
405 // Relationship of resource being authenticated with the top level page.
406 enum HttpAuthRelationType
{
407 HTTP_AUTH_RELATION_TOP
, // Top-level page itself
408 HTTP_AUTH_RELATION_SAME_DOMAIN
, // Sub-content from same domain
409 HTTP_AUTH_RELATION_BLOCKED_CROSS
, // Blocked Sub-content from cross domain
410 HTTP_AUTH_RELATION_ALLOWED_CROSS
, // Allowed Sub-content per command line
411 HTTP_AUTH_RELATION_LAST
414 HttpAuthRelationType
HttpAuthRelationTypeOf(const GURL
& request_url
,
415 const GURL
& first_party
);
417 // Returns whether the URLRequest identified by |transferred_request_id| is
418 // currently in the process of being transferred to a different renderer.
419 // This happens if a request is redirected cross-site and needs to be resumed
420 // by a new render view.
421 bool IsTransferredNavigation(
422 const GlobalRequestID
& transferred_request_id
) const;
424 ResourceLoader
* GetLoader(const GlobalRequestID
& id
) const;
425 ResourceLoader
* GetLoader(int child_id
, int request_id
) const;
427 // Registers |delegate| to receive resource IPC messages targeted to the
429 void RegisterResourceMessageDelegate(const GlobalRequestID
& id
,
430 ResourceMessageDelegate
* delegate
);
431 void UnregisterResourceMessageDelegate(const GlobalRequestID
& id
,
432 ResourceMessageDelegate
* delegate
);
434 int BuildLoadFlagsForRequest(const ResourceHostMsg_Request
& request_data
,
438 LoaderMap pending_loaders_
;
440 // Collection of temp files downloaded for child processes via
441 // the download_to_file mechanism. We avoid deleting them until
442 // the client no longer needs them.
443 typedef std::map
<int, scoped_refptr
<storage::ShareableFileReference
> >
444 DeletableFilesMap
; // key is request id
445 typedef std::map
<int, DeletableFilesMap
>
446 RegisteredTempFiles
; // key is child process id
447 RegisteredTempFiles registered_temp_files_
;
449 // A timer that periodically calls UpdateLoadStates while pending_requests_
451 scoped_ptr
<base::RepeatingTimer
<ResourceDispatcherHostImpl
> >
452 update_load_states_timer_
;
454 // We own the save file manager.
455 scoped_refptr
<SaveFileManager
> save_file_manager_
;
457 // Request ID for browser initiated requests. request_ids generated by
458 // child processes are counted up from 0, while browser created requests
459 // start at -2 and go down from there. (We need to start at -2 because -1 is
460 // used as a special value all over the resource_dispatcher_host for
461 // uninitialized variables.) This way, we no longer have the unlikely (but
462 // observed in the real world!) event where we have two requests with the same
466 // True if the resource dispatcher host has been shut down.
469 typedef std::vector
<linked_ptr
<ResourceLoader
> > BlockedLoadersList
;
470 typedef std::map
<GlobalRoutingID
, BlockedLoadersList
*> BlockedLoadersMap
;
471 BlockedLoadersMap blocked_loaders_map_
;
473 // Maps the child_ids to the approximate number of bytes
474 // being used to service its resource requests. No entry implies 0 cost.
475 typedef std::map
<int, OustandingRequestsStats
> OutstandingRequestsStatsMap
;
476 OutstandingRequestsStatsMap outstanding_requests_stats_map_
;
478 // |num_in_flight_requests_| is the total number of requests currently issued
479 // summed across all renderers.
480 int num_in_flight_requests_
;
482 // |max_num_in_flight_requests_| is the upper bound on how many requests
483 // can be in flight at once. It's based on the maximum number of file
484 // descriptors open per process. We need a global limit for the browser
486 int max_num_in_flight_requests_
;
488 // |max_num_in_flight_requests_| is the upper bound on how many requests
489 // can be issued at once. It's based on the maximum number of file
490 // descriptors open per process. We need a per-renderer limit so that no
491 // single renderer can hog the browser's limit.
492 int max_num_in_flight_requests_per_process_
;
494 // |max_outstanding_requests_cost_per_process_| is the upper bound on how
495 // many outstanding requests can be issued per child process host.
496 // The constraint is expressed in terms of bytes (where the cost of
497 // individual requests is given by CalculateApproximateMemoryCost).
498 // The total number of outstanding requests is roughly:
499 // (max_outstanding_requests_cost_per_process_ /
500 // kAvgBytesPerOutstandingRequest)
501 int max_outstanding_requests_cost_per_process_
;
503 // Time of the last user gesture. Stored so that we can add a load
504 // flag to requests occurring soon after a gesture to indicate they
505 // may be because of explicit user action.
506 base::TimeTicks last_user_gesture_time_
;
508 // Used during IPC message dispatching so that the handlers can get a pointer
509 // to the source of the message.
510 ResourceMessageFilter
* filter_
;
512 ResourceDispatcherHostDelegate
* delegate_
;
514 bool allow_cross_origin_auth_prompt_
;
516 // http://crbug.com/90971 - Assists in tracking down use-after-frees on
518 std::set
<const ResourceContext
*> active_resource_contexts_
;
520 typedef std::map
<GlobalRequestID
,
521 ObserverList
<ResourceMessageDelegate
>*> DelegateMap
;
522 DelegateMap delegate_map_
;
524 scoped_ptr
<ResourceScheduler
> scheduler_
;
526 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcherHostImpl
);
529 } // namespace content
531 #endif // CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_