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