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
;
53 class URLRequestJobFactory
;
57 class ShareableFileReference
;
61 class AppCacheService
;
62 class NavigationURLLoaderImplCore
;
63 class ResourceContext
;
64 class ResourceDispatcherHostDelegate
;
65 class ResourceMessageDelegate
;
66 class ResourceMessageFilter
;
67 class ResourceRequestInfoImpl
;
68 class SaveFileManager
;
69 class WebContentsImpl
;
70 struct CommonNavigationParams
;
71 struct DownloadSaveInfo
;
72 struct NavigationRequestInfo
;
75 class CONTENT_EXPORT ResourceDispatcherHostImpl
76 : public ResourceDispatcherHost
,
77 public ResourceLoaderDelegate
{
79 ResourceDispatcherHostImpl();
80 ~ResourceDispatcherHostImpl() override
;
82 // Returns the current ResourceDispatcherHostImpl. May return NULL if it
83 // hasn't been created yet.
84 static ResourceDispatcherHostImpl
* Get();
86 // ResourceDispatcherHost implementation:
87 void SetDelegate(ResourceDispatcherHostDelegate
* delegate
) override
;
88 void SetAllowCrossOriginAuthPrompt(bool value
) override
;
89 DownloadInterruptReason
BeginDownload(
90 scoped_ptr
<net::URLRequest
> request
,
91 const Referrer
& referrer
,
92 bool is_content_initiated
,
93 ResourceContext
* context
,
97 bool do_not_prompt_for_login
,
98 scoped_ptr
<DownloadSaveInfo
> save_info
,
100 const DownloadStartedCallback
& started_callback
) override
;
101 void ClearLoginDelegateForRequest(net::URLRequest
* request
) override
;
102 void BlockRequestsForRoute(int child_id
, int route_id
) override
;
103 void ResumeBlockedRequestsForRoute(int child_id
, int route_id
) override
;
105 // Puts the resource dispatcher host in an inactive state (unable to begin
106 // new requests). Cancels all pending requests.
109 // Notify the ResourceDispatcherHostImpl of a new resource context.
110 void AddResourceContext(ResourceContext
* context
);
112 // Notify the ResourceDispatcherHostImpl of a resource context destruction.
113 void RemoveResourceContext(ResourceContext
* context
);
115 // Force cancels any pending requests for the given |context|. This is
116 // necessary to ensure that before |context| goes away, all requests
118 void CancelRequestsForContext(ResourceContext
* context
);
120 // Returns true if the message was a resource message that was processed.
121 bool OnMessageReceived(const IPC::Message
& message
,
122 ResourceMessageFilter
* filter
);
124 // Initiates a save file from the browser process (as opposed to a resource
125 // request from the renderer or another child process).
126 void BeginSaveFile(const GURL
& url
,
127 const Referrer
& referrer
,
130 ResourceContext
* context
);
132 // Cancels the given request if it still exists.
133 void CancelRequest(int child_id
, int request_id
);
135 // Marks the request as "parked". This happens if a request is
136 // redirected cross-site and needs to be resumed by a new render view.
137 void MarkAsTransferredNavigation(const GlobalRequestID
& id
);
139 // Cancels a request previously marked as being transferred, for use when a
140 // navigation was cancelled.
141 void CancelTransferringNavigation(const GlobalRequestID
& id
);
143 // Resumes the request without transferring it to a new render view.
144 void ResumeDeferredNavigation(const GlobalRequestID
& id
);
146 // Returns the number of pending requests. This is designed for the unittests
147 int pending_requests() const {
148 return static_cast<int>(pending_loaders_
.size());
151 // Intended for unit-tests only. Overrides the outstanding requests bound.
152 void set_max_outstanding_requests_cost_per_process(int limit
) {
153 max_outstanding_requests_cost_per_process_
= limit
;
155 void set_max_num_in_flight_requests_per_process(int limit
) {
156 max_num_in_flight_requests_per_process_
= limit
;
158 void set_max_num_in_flight_requests(int limit
) {
159 max_num_in_flight_requests_
= limit
;
162 // The average private bytes increase of the browser for each new pending
163 // request. Experimentally obtained.
164 static const int kAvgBytesPerOutstandingRequest
= 4400;
166 SaveFileManager
* save_file_manager() const {
167 return save_file_manager_
.get();
170 // Called when a RenderViewHost is created.
171 void OnRenderViewHostCreated(int child_id
,
176 // Called when a RenderViewHost is deleted.
177 void OnRenderViewHostDeleted(int child_id
, int route_id
);
179 // Called when a RenderViewHost starts or stops loading.
180 void OnRenderViewHostSetIsLoading(int child_id
,
184 // Called when a RenderViewHost is hidden.
185 void OnRenderViewHostWasHidden(int child_id
, int route_id
);
187 // Called when a RenderViewHost is shown.
188 void OnRenderViewHostWasShown(int child_id
, int route_id
);
190 // Called when an AudioRenderHost starts or stops playing.
191 void OnAudioRenderHostStreamStateChanged(int child_id
,
195 // Force cancels any pending requests for the given process.
196 void CancelRequestsForProcess(int child_id
);
198 void OnUserGesture(WebContentsImpl
* contents
);
200 // Retrieves a net::URLRequest. Must be called from the IO thread.
201 net::URLRequest
* GetURLRequest(const GlobalRequestID
& request_id
);
203 void RemovePendingRequest(int child_id
, int request_id
);
205 // Cancels any blocked request for the specified route id.
206 void CancelBlockedRequestsForRoute(int child_id
, int route_id
);
208 // Maintains a collection of temp files created in support of
209 // the download_to_file capability. Used to grant access to the
210 // child process and to defer deletion of the file until it's
212 void RegisterDownloadedTempFile(
213 int child_id
, int request_id
,
214 const base::FilePath
& file_path
);
215 void UnregisterDownloadedTempFile(int child_id
, int request_id
);
217 // Needed for the sync IPC message dispatcher macros.
218 bool Send(IPC::Message
* message
);
220 // Indicates whether third-party sub-content can pop-up HTTP basic auth
222 bool allow_cross_origin_auth_prompt();
224 ResourceDispatcherHostDelegate
* delegate() {
228 // Must be called after the ResourceRequestInfo has been created
229 // and associated with the request.
230 // |id| should be |content::DownloadItem::kInvalidId| to request automatic
231 // assignment. This is marked virtual so it can be overriden in testing.
232 virtual scoped_ptr
<ResourceHandler
> CreateResourceHandlerForDownload(
233 net::URLRequest
* request
,
234 bool is_content_initiated
,
237 scoped_ptr
<DownloadSaveInfo
> save_info
,
238 const DownloadUrlParameters::OnStartedCallback
& started_cb
);
240 // Called to determine whether the response to |request| should be intercepted
241 // and handled as a stream. Streams are used to pass direct access to a
242 // resource response to another application (e.g. a web page) without being
243 // handled by the browser itself. If the request should be intercepted as a
244 // stream, a StreamResourceHandler is returned which provides access to the
245 // response. |plugin_path| is the path to the plugin which is handling the
246 // URL request. This may be empty if there is no plugin handling the request.
248 // This function must be called after the ResourceRequestInfo has been created
249 // and associated with the request. If |payload| is set to a non-empty value,
250 // the caller must send it to the old resource handler instead of cancelling
252 virtual scoped_ptr
<ResourceHandler
> MaybeInterceptAsStream(
253 const base::FilePath
& plugin_path
,
254 net::URLRequest
* request
,
255 ResourceResponse
* response
,
256 std::string
* payload
);
258 ResourceScheduler
* scheduler() { return scheduler_
.get(); }
260 // Called by a ResourceHandler when it's ready to start reading data and
261 // sending it to the renderer. Returns true if there are enough file
262 // descriptors available for the shared memory buffer. If false is returned,
263 // the request should cancel.
264 bool HasSufficientResourcesForRequest(net::URLRequest
* request
);
266 // Called by a ResourceHandler after it has finished its request and is done
267 // using its shared memory buffer. Frees up that file descriptor to be used
269 void FinishedWithResourcesForRequest(net::URLRequest
* request
);
271 // PlzNavigate: Begins a request for NavigationURLLoader. |loader| is the
272 // loader to attach to the leaf resource handler.
273 void BeginNavigationRequest(ResourceContext
* resource_context
,
274 int frame_tree_node_id
,
275 const NavigationRequestInfo
& info
,
276 NavigationURLLoaderImplCore
* loader
);
279 friend class ResourceDispatcherHostTest
;
281 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest
,
282 TestBlockedRequestsProcessDies
);
283 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest
,
284 CalculateApproximateMemoryCost
);
285 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest
,
286 DetachableResourceTimesOut
);
287 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest
,
288 TestProcessCancelDetachableTimesOut
);
290 struct OustandingRequestsStats
{
295 friend class ShutdownTask
;
296 friend class ResourceMessageDelegate
;
298 // Information about status of a ResourceLoader.
301 net::LoadStateWithParam load_state
;
302 uint64 upload_position
;
306 // Map from ProcessID+RouteID pair to the "most interesting" LoadState.
307 typedef std::map
<GlobalRoutingID
, LoadInfo
> LoadInfoMap
;
309 // ResourceLoaderDelegate implementation:
310 ResourceDispatcherHostLoginDelegate
* CreateLoginDelegate(
311 ResourceLoader
* loader
,
312 net::AuthChallengeInfo
* auth_info
) override
;
313 bool HandleExternalProtocol(ResourceLoader
* loader
, const GURL
& url
) override
;
314 void DidStartRequest(ResourceLoader
* loader
) override
;
315 void DidReceiveRedirect(ResourceLoader
* loader
, const GURL
& new_url
) override
;
316 void DidReceiveResponse(ResourceLoader
* loader
) override
;
317 void DidFinishLoading(ResourceLoader
* loader
) override
;
319 // An init helper that runs on the IO thread.
322 // A shutdown helper that runs on the IO thread.
325 // Helper function for regular and download requests.
326 void BeginRequestInternal(scoped_ptr
<net::URLRequest
> request
,
327 scoped_ptr
<ResourceHandler
> handler
);
329 void StartLoading(ResourceRequestInfoImpl
* info
,
330 const linked_ptr
<ResourceLoader
>& loader
);
332 // We keep track of how much memory each request needs and how many requests
333 // are issued by each renderer. These are known as OustandingRequestStats.
334 // Memory limits apply to all requests sent to us by the renderers. There is a
335 // limit for each renderer. File descriptor limits apply to requests that are
336 // receiving their body. These are known as in-flight requests. There is a
337 // global limit that applies for the browser process. Each render is allowed
338 // to use up to a fraction of that.
340 // Returns the OustandingRequestsStats for |info|'s renderer, or an empty
341 // struct if that renderer has no outstanding requests.
342 OustandingRequestsStats
GetOutstandingRequestsStats(
343 const ResourceRequestInfoImpl
& info
);
345 // Updates |outstanding_requests_stats_map_| with the specified |stats| for
346 // the renderer that made the request in |info|.
347 void UpdateOutstandingRequestsStats(const ResourceRequestInfoImpl
& info
,
348 const OustandingRequestsStats
& stats
);
350 // Called every time an outstanding request is created or deleted. |count|
351 // indicates whether the request is new or deleted. |count| must be 1 or -1.
352 OustandingRequestsStats
IncrementOutstandingRequestsMemory(
354 const ResourceRequestInfoImpl
& info
);
356 // Called when an in flight request allocates or releases a shared memory
357 // buffer. |count| indicates whether the request is issuing or finishing.
358 // |count| must be 1 or -1.
359 OustandingRequestsStats
IncrementOutstandingRequestsCount(
361 ResourceRequestInfoImpl
* info
);
363 // Estimate how much heap space |request| will consume to run.
364 static int CalculateApproximateMemoryCost(net::URLRequest
* request
);
366 // Force cancels any pending requests for the given route id. This method
367 // acts like CancelRequestsForProcess when route_id is -1.
368 void CancelRequestsForRoute(int child_id
, int route_id
);
370 // The list of all requests that we have pending. This list is not really
371 // optimized, and assumes that we have relatively few requests pending at once
372 // since some operations require brute-force searching of the list.
374 // It may be enhanced in the future to provide some kind of prioritization
375 // mechanism. We should also consider a hashtable or binary tree if it turns
376 // out we have a lot of things here.
377 typedef std::map
<GlobalRequestID
, linked_ptr
<ResourceLoader
> > LoaderMap
;
379 // Deletes the pending request identified by the iterator passed in.
380 // This function will invalidate the iterator passed in. Callers should
381 // not rely on this iterator being valid on return.
382 void RemovePendingLoader(const LoaderMap::iterator
& iter
);
384 // This function returns true if the LoadInfo of |a| is "more interesting"
385 // than the LoadInfo of |b|. The load that is currently sending the larger
386 // request body is considered more interesting. If neither request is
387 // sending a body (Neither request has a body, or any request that has a body
388 // is not currently sending the body), the request that is further along is
389 // considered more interesting.
391 // This takes advantage of the fact that the load states are an enumeration
392 // listed in the order in which they usually occur during the lifetime of a
393 // request, so states with larger numeric values are generally further along
394 // toward completion.
396 // For example, by this measure "tranferring data" is a more interesting state
397 // than "resolving host" because when transferring data something is being
398 // done that corresponds to changes that the user might observe, whereas
399 // waiting for a host name to resolve implies being stuck.
400 static bool LoadInfoIsMoreInteresting(const LoadInfo
& a
, const LoadInfo
& b
);
402 // Used to marshal calls to LoadStateChanged from the IO to UI threads. All
403 // are done as a single callback to avoid spamming the UI thread.
404 static void UpdateLoadInfoOnUIThread(scoped_ptr
<LoadInfoMap
> info_map
);
406 // Gets the most interesting LoadInfo for each GlobalRoutingID.
407 scoped_ptr
<LoadInfoMap
> GetLoadInfoForAllRoutes();
409 // Checks all pending requests and updates the load info if necessary.
410 void UpdateLoadInfo();
412 // Resumes or cancels (if |cancel_requests| is true) any blocked requests.
413 void ProcessBlockedRequestsForRoute(int child_id
,
415 bool cancel_requests
);
417 void OnRequestResource(int routing_id
,
419 const ResourceHostMsg_Request
& request_data
);
420 void OnSyncLoad(int request_id
,
421 const ResourceHostMsg_Request
& request_data
,
422 IPC::Message
* sync_result
);
424 // Update the ResourceRequestInfo and internal maps when a request is
425 // transferred from one process to another.
426 void UpdateRequestForTransfer(int child_id
,
429 const ResourceHostMsg_Request
& request_data
,
430 const linked_ptr
<ResourceLoader
>& loader
);
432 void BeginRequest(int request_id
,
433 const ResourceHostMsg_Request
& request_data
,
434 IPC::Message
* sync_result
, // only valid for sync
435 int route_id
); // only valid for async
437 // Creates a ResourceHandler to be used by BeginRequest() for normal resource
439 scoped_ptr
<ResourceHandler
> CreateResourceHandler(
440 net::URLRequest
* request
,
441 const ResourceHostMsg_Request
& request_data
,
442 IPC::Message
* sync_result
,
446 ResourceContext
* resource_context
);
448 // Wraps |handler| in the standard resource handlers for normal resource
449 // loading and navigation requests. This adds MimeTypeResourceHandler and
450 // ResourceThrottles.
451 scoped_ptr
<ResourceHandler
> AddStandardHandlers(
452 net::URLRequest
* request
,
453 ResourceType resource_type
,
454 ResourceContext
* resource_context
,
455 AppCacheService
* appcache_service
,
458 scoped_ptr
<ResourceHandler
> handler
);
460 void OnDataDownloadedACK(int request_id
);
461 void OnUploadProgressACK(int request_id
);
462 void OnCancelRequest(int request_id
);
463 void OnReleaseDownloadedFile(int request_id
);
465 // Creates ResourceRequestInfoImpl for a download or page save.
466 // |download| should be true if the request is a file download.
467 ResourceRequestInfoImpl
* CreateRequestInfo(
471 ResourceContext
* context
);
473 // Relationship of resource being authenticated with the top level page.
474 enum HttpAuthRelationType
{
475 HTTP_AUTH_RELATION_TOP
, // Top-level page itself
476 HTTP_AUTH_RELATION_SAME_DOMAIN
, // Sub-content from same domain
477 HTTP_AUTH_RELATION_BLOCKED_CROSS
, // Blocked Sub-content from cross domain
478 HTTP_AUTH_RELATION_ALLOWED_CROSS
, // Allowed Sub-content per command line
479 HTTP_AUTH_RELATION_LAST
482 HttpAuthRelationType
HttpAuthRelationTypeOf(const GURL
& request_url
,
483 const GURL
& first_party
);
485 // Returns whether the URLRequest identified by |transferred_request_id| is
486 // currently in the process of being transferred to a different renderer.
487 // This happens if a request is redirected cross-site and needs to be resumed
488 // by a new render view.
489 bool IsTransferredNavigation(
490 const GlobalRequestID
& transferred_request_id
) const;
492 ResourceLoader
* GetLoader(const GlobalRequestID
& id
) const;
493 ResourceLoader
* GetLoader(int child_id
, int request_id
) const;
495 // Registers |delegate| to receive resource IPC messages targeted to the
497 void RegisterResourceMessageDelegate(const GlobalRequestID
& id
,
498 ResourceMessageDelegate
* delegate
);
499 void UnregisterResourceMessageDelegate(const GlobalRequestID
& id
,
500 ResourceMessageDelegate
* delegate
);
502 int BuildLoadFlagsForRequest(const ResourceHostMsg_Request
& request_data
,
506 LoaderMap pending_loaders_
;
508 // Collection of temp files downloaded for child processes via
509 // the download_to_file mechanism. We avoid deleting them until
510 // the client no longer needs them.
511 typedef std::map
<int, scoped_refptr
<storage::ShareableFileReference
> >
512 DeletableFilesMap
; // key is request id
513 typedef std::map
<int, DeletableFilesMap
>
514 RegisteredTempFiles
; // key is child process id
515 RegisteredTempFiles registered_temp_files_
;
517 // A timer that periodically calls UpdateLoadInfo while pending_loaders_ is
518 // not empty and at least one RenderViewHost is loading.
519 scoped_ptr
<base::RepeatingTimer
<ResourceDispatcherHostImpl
> >
520 update_load_states_timer_
;
522 // We own the save file manager.
523 scoped_refptr
<SaveFileManager
> save_file_manager_
;
525 // Request ID for browser initiated requests. request_ids generated by
526 // child processes are counted up from 0, while browser created requests
527 // start at -2 and go down from there. (We need to start at -2 because -1 is
528 // used as a special value all over the resource_dispatcher_host for
529 // uninitialized variables.) This way, we no longer have the unlikely (but
530 // observed in the real world!) event where we have two requests with the same
534 // True if the resource dispatcher host has been shut down.
537 typedef std::vector
<linked_ptr
<ResourceLoader
> > BlockedLoadersList
;
538 typedef std::map
<GlobalRoutingID
, BlockedLoadersList
*> BlockedLoadersMap
;
539 BlockedLoadersMap blocked_loaders_map_
;
541 // Maps the child_ids to the approximate number of bytes
542 // being used to service its resource requests. No entry implies 0 cost.
543 typedef std::map
<int, OustandingRequestsStats
> OutstandingRequestsStatsMap
;
544 OutstandingRequestsStatsMap outstanding_requests_stats_map_
;
546 // |num_in_flight_requests_| is the total number of requests currently issued
547 // summed across all renderers.
548 int num_in_flight_requests_
;
550 // |max_num_in_flight_requests_| is the upper bound on how many requests
551 // can be in flight at once. It's based on the maximum number of file
552 // descriptors open per process. We need a global limit for the browser
554 int max_num_in_flight_requests_
;
556 // |max_num_in_flight_requests_| is the upper bound on how many requests
557 // can be issued at once. It's based on the maximum number of file
558 // descriptors open per process. We need a per-renderer limit so that no
559 // single renderer can hog the browser's limit.
560 int max_num_in_flight_requests_per_process_
;
562 // |max_outstanding_requests_cost_per_process_| is the upper bound on how
563 // many outstanding requests can be issued per child process host.
564 // The constraint is expressed in terms of bytes (where the cost of
565 // individual requests is given by CalculateApproximateMemoryCost).
566 // The total number of outstanding requests is roughly:
567 // (max_outstanding_requests_cost_per_process_ /
568 // kAvgBytesPerOutstandingRequest)
569 int max_outstanding_requests_cost_per_process_
;
571 // Time of the last user gesture. Stored so that we can add a load
572 // flag to requests occurring soon after a gesture to indicate they
573 // may be because of explicit user action.
574 base::TimeTicks last_user_gesture_time_
;
576 // Used during IPC message dispatching so that the handlers can get a pointer
577 // to the source of the message.
578 ResourceMessageFilter
* filter_
;
580 ResourceDispatcherHostDelegate
* delegate_
;
582 bool allow_cross_origin_auth_prompt_
;
584 // http://crbug.com/90971 - Assists in tracking down use-after-frees on
586 std::set
<const ResourceContext
*> active_resource_contexts_
;
588 typedef std::map
<GlobalRequestID
,
589 base::ObserverList
<ResourceMessageDelegate
>*> DelegateMap
;
590 DelegateMap delegate_map_
;
592 scoped_ptr
<ResourceScheduler
> scheduler_
;
594 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcherHostImpl
);
597 } // namespace content
599 #endif // CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_