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/offline_policy.h"
30 #include "content/browser/loader/resource_loader.h"
31 #include "content/browser/loader/resource_loader_delegate.h"
32 #include "content/browser/loader/resource_scheduler.h"
33 #include "content/common/content_export.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 "ipc/ipc_message.h"
41 #include "net/cookies/canonical_cookie.h"
42 #include "net/url_request/url_request.h"
43 #include "webkit/common/resource_type.h"
45 class ResourceHandler
;
46 struct ResourceHostMsg_Request
;
49 class URLRequestJobFactory
;
52 namespace webkit_blob
{
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
;
67 class CONTENT_EXPORT ResourceDispatcherHostImpl
68 : public ResourceDispatcherHost
,
69 public ResourceLoaderDelegate
{
71 ResourceDispatcherHostImpl();
72 virtual ~ResourceDispatcherHostImpl();
74 // Returns the current ResourceDispatcherHostImpl. May return NULL if it
75 // hasn't been created yet.
76 static ResourceDispatcherHostImpl
* Get();
78 // ResourceDispatcherHost implementation:
79 virtual void SetDelegate(ResourceDispatcherHostDelegate
* delegate
) OVERRIDE
;
80 virtual void SetAllowCrossOriginAuthPrompt(bool value
) OVERRIDE
;
81 virtual net::Error
BeginDownload(
82 scoped_ptr
<net::URLRequest
> request
,
83 const Referrer
& referrer
,
84 bool is_content_initiated
,
85 ResourceContext
* context
,
89 scoped_ptr
<DownloadSaveInfo
> save_info
,
91 const DownloadStartedCallback
& started_callback
) OVERRIDE
;
92 virtual void ClearLoginDelegateForRequest(net::URLRequest
* request
) OVERRIDE
;
93 virtual void BlockRequestsForRoute(int child_id
, int route_id
) OVERRIDE
;
94 virtual void ResumeBlockedRequestsForRoute(
95 int child_id
, int route_id
) OVERRIDE
;
97 // Puts the resource dispatcher host in an inactive state (unable to begin
98 // new requests). Cancels all pending requests.
101 // Notify the ResourceDispatcherHostImpl of a new resource context.
102 void AddResourceContext(ResourceContext
* context
);
104 // Notify the ResourceDispatcherHostImpl of a resource context destruction.
105 void RemoveResourceContext(ResourceContext
* context
);
107 // Force cancels any pending requests for the given |context|. This is
108 // necessary to ensure that before |context| goes away, all requests
110 void CancelRequestsForContext(ResourceContext
* context
);
112 // Returns true if the message was a resource message that was processed.
113 // If it was, message_was_ok will be false iff the message was corrupt.
114 bool OnMessageReceived(const IPC::Message
& message
,
115 ResourceMessageFilter
* filter
,
116 bool* message_was_ok
);
118 // Initiates a save file from the browser process (as opposed to a resource
119 // request from the renderer or another child process).
120 void BeginSaveFile(const GURL
& url
,
121 const Referrer
& referrer
,
124 ResourceContext
* context
);
126 // Cancels the given request if it still exists. We ignore cancels from the
127 // renderer in the event of a download.
128 void CancelRequest(int child_id
,
132 // Marks the request as "parked". This happens if a request is
133 // redirected cross-site and needs to be resumed by a new render view.
134 void MarkAsTransferredNavigation(const GlobalRequestID
& id
);
136 // Resumes the request without transferring it to a new render view.
137 void ResumeDeferredNavigation(const GlobalRequestID
& id
);
139 // Returns the number of pending requests. This is designed for the unittests
140 int pending_requests() const {
141 return static_cast<int>(pending_loaders_
.size());
144 // Intended for unit-tests only. Overrides the outstanding requests bound.
145 void set_max_outstanding_requests_cost_per_process(int limit
) {
146 max_outstanding_requests_cost_per_process_
= limit
;
148 void set_max_num_in_flight_requests_per_process(int limit
) {
149 max_num_in_flight_requests_per_process_
= limit
;
151 void set_max_num_in_flight_requests(int limit
) {
152 max_num_in_flight_requests_
= limit
;
155 // The average private bytes increase of the browser for each new pending
156 // request. Experimentally obtained.
157 static const int kAvgBytesPerOutstandingRequest
= 4400;
159 SaveFileManager
* save_file_manager() const {
160 return save_file_manager_
.get();
163 // Called when a RenderViewHost is created.
164 void OnRenderViewHostCreated(int child_id
, int route_id
);
166 // Called when a RenderViewHost is deleted.
167 void OnRenderViewHostDeleted(int child_id
, int route_id
);
169 // Force cancels any pending requests for the given process.
170 void CancelRequestsForProcess(int child_id
);
172 void OnUserGesture(WebContentsImpl
* contents
);
174 // Retrieves a net::URLRequest. Must be called from the IO thread.
175 net::URLRequest
* GetURLRequest(const GlobalRequestID
& request_id
);
177 void RemovePendingRequest(int child_id
, int request_id
);
179 // Cancels any blocked request for the specified route id.
180 void CancelBlockedRequestsForRoute(int child_id
, int route_id
);
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
186 void RegisterDownloadedTempFile(
187 int child_id
, int request_id
,
188 webkit_blob::ShareableFileReference
* reference
);
189 void UnregisterDownloadedTempFile(int child_id
, int request_id
);
191 // Needed for the sync IPC message dispatcher macros.
192 bool Send(IPC::Message
* message
);
194 // Indicates whether third-party sub-content can pop-up HTTP basic auth
196 bool allow_cross_origin_auth_prompt();
198 ResourceDispatcherHostDelegate
* delegate() {
202 // Must be called after the ResourceRequestInfo has been created
203 // and associated with the request.
204 // |id| should be |content::DownloadItem::kInvalidId| to request automatic
206 scoped_ptr
<ResourceHandler
> CreateResourceHandlerForDownload(
207 net::URLRequest
* request
,
208 bool is_content_initiated
,
211 scoped_ptr
<DownloadSaveInfo
> save_info
,
212 const DownloadUrlParameters::OnStartedCallback
& started_cb
);
214 // Must be called after the ResourceRequestInfo has been created
215 // and associated with the request.
216 scoped_ptr
<ResourceHandler
> MaybeInterceptAsStream(
217 net::URLRequest
* request
,
218 ResourceResponse
* response
);
220 void ClearSSLClientAuthHandlerForRequest(net::URLRequest
* request
);
222 ResourceScheduler
* scheduler() { return scheduler_
.get(); }
224 // Called by a ResourceHandler when it's ready to start reading data and
225 // sending it to the renderer. Returns true if there are enough file
226 // descriptors available for the shared memory buffer. If false is returned,
227 // the request should cancel.
228 bool HasSufficientResourcesForRequest(const net::URLRequest
* request_
);
230 // Called by a ResourceHandler after it has finished its request and is done
231 // using its shared memory buffer. Frees up that file descriptor to be used
233 void FinishedWithResourcesForRequest(const net::URLRequest
* request_
);
236 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest
,
237 TestBlockedRequestsProcessDies
);
238 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest
,
239 CalculateApproximateMemoryCost
);
240 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest
,
241 DetachableResourceTimesOut
);
242 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest
,
243 TestProcessCancelDetachableTimesOut
);
247 struct OustandingRequestsStats
{
252 friend class ShutdownTask
;
253 friend class ResourceMessageDelegate
;
255 // ResourceLoaderDelegate implementation:
256 virtual ResourceDispatcherHostLoginDelegate
* CreateLoginDelegate(
257 ResourceLoader
* loader
,
258 net::AuthChallengeInfo
* auth_info
) OVERRIDE
;
259 virtual bool HandleExternalProtocol(ResourceLoader
* loader
,
260 const GURL
& url
) OVERRIDE
;
261 virtual void DidStartRequest(ResourceLoader
* loader
) OVERRIDE
;
262 virtual void DidReceiveRedirect(ResourceLoader
* loader
,
263 const GURL
& new_url
) OVERRIDE
;
264 virtual void DidReceiveResponse(ResourceLoader
* loader
) OVERRIDE
;
265 virtual void DidFinishLoading(ResourceLoader
* loader
) OVERRIDE
;
267 // An init helper that runs on the IO thread.
270 // A shutdown helper that runs on the IO thread.
273 // Helper function for regular and download requests.
274 void BeginRequestInternal(scoped_ptr
<net::URLRequest
> request
,
275 scoped_ptr
<ResourceHandler
> handler
);
277 void StartLoading(ResourceRequestInfoImpl
* info
,
278 const linked_ptr
<ResourceLoader
>& loader
);
280 // We keep track of how much memory each request needs and how many requests
281 // are issued by each renderer. These are known as OustandingRequestStats.
282 // Memory limits apply to all requests sent to us by the renderers. There is a
283 // limit for each renderer. File descriptor limits apply to requests that are
284 // receiving their body. These are known as in-flight requests. There is a
285 // global limit that applies for the browser process. Each render is allowed
286 // to use up to a fraction of that.
288 // Returns the OustandingRequestsStats for |info|'s renderer, or an empty
289 // struct if that renderer has no outstanding requests.
290 OustandingRequestsStats
GetOutstandingRequestsStats(
291 const ResourceRequestInfoImpl
& info
);
293 // Updates |outstanding_requests_stats_map_| with the specified |stats| for
294 // the renderer that made the request in |info|.
295 void UpdateOutstandingRequestsStats(const ResourceRequestInfoImpl
& info
,
296 const OustandingRequestsStats
& stats
);
298 // Called every time an outstanding request is created or deleted. |count|
299 // indicates whether the request is new or deleted. |count| must be 1 or -1.
300 OustandingRequestsStats
IncrementOutstandingRequestsMemory(
302 const ResourceRequestInfoImpl
& info
);
304 // Called every time an in flight request is issued or finished. |count|
305 // indicates whether the request is issuing or finishing. |count| must be 1
307 OustandingRequestsStats
IncrementOutstandingRequestsCount(
309 const ResourceRequestInfoImpl
& info
);
311 // Estimate how much heap space |request| will consume to run.
312 static int CalculateApproximateMemoryCost(net::URLRequest
* request
);
314 // Force cancels any pending requests for the given route id. This method
315 // acts like CancelRequestsForProcess when route_id is -1.
316 void CancelRequestsForRoute(int child_id
, int route_id
);
318 // The list of all requests that we have pending. This list is not really
319 // optimized, and assumes that we have relatively few requests pending at once
320 // since some operations require brute-force searching of the list.
322 // It may be enhanced in the future to provide some kind of prioritization
323 // mechanism. We should also consider a hashtable or binary tree if it turns
324 // out we have a lot of things here.
325 typedef std::map
<GlobalRequestID
, linked_ptr
<ResourceLoader
> > LoaderMap
;
327 // Deletes the pending request identified by the iterator passed in.
328 // This function will invalidate the iterator passed in. Callers should
329 // not rely on this iterator being valid on return.
330 void RemovePendingLoader(const LoaderMap::iterator
& iter
);
332 // Checks all pending requests and updates the load states and upload
333 // progress if necessary.
334 void UpdateLoadStates();
336 // Resumes or cancels (if |cancel_requests| is true) any blocked requests.
337 void ProcessBlockedRequestsForRoute(int child_id
,
339 bool cancel_requests
);
341 void OnRequestResource(const IPC::Message
& msg
,
343 const ResourceHostMsg_Request
& request_data
);
344 void OnSyncLoad(int request_id
,
345 const ResourceHostMsg_Request
& request_data
,
346 IPC::Message
* sync_result
);
348 // Update the ResourceRequestInfo and internal maps when a request is
349 // transferred from one process to another.
350 void UpdateRequestForTransfer(int child_id
,
353 const ResourceHostMsg_Request
& request_data
,
354 const linked_ptr
<ResourceLoader
>& loader
);
356 void BeginRequest(int request_id
,
357 const ResourceHostMsg_Request
& request_data
,
358 IPC::Message
* sync_result
, // only valid for sync
359 int route_id
); // only valid for async
361 // Creates a ResourceHandler to be used by BeginRequest() for normal resource
363 scoped_ptr
<ResourceHandler
> CreateResourceHandler(
364 net::URLRequest
* request
,
365 const ResourceHostMsg_Request
& request_data
,
366 IPC::Message
* sync_result
,
370 ResourceContext
* resource_context
);
372 void OnDataDownloadedACK(int request_id
);
373 void OnUploadProgressACK(int request_id
);
374 void OnCancelRequest(int request_id
);
375 void OnReleaseDownloadedFile(int request_id
);
377 // Creates ResourceRequestInfoImpl for a download or page save.
378 // |download| should be true if the request is a file download.
379 ResourceRequestInfoImpl
* CreateRequestInfo(
383 ResourceContext
* context
);
385 // Relationship of resource being authenticated with the top level page.
386 enum HttpAuthRelationType
{
387 HTTP_AUTH_RELATION_TOP
, // Top-level page itself
388 HTTP_AUTH_RELATION_SAME_DOMAIN
, // Sub-content from same domain
389 HTTP_AUTH_RELATION_BLOCKED_CROSS
, // Blocked Sub-content from cross domain
390 HTTP_AUTH_RELATION_ALLOWED_CROSS
, // Allowed Sub-content per command line
391 HTTP_AUTH_RELATION_LAST
394 HttpAuthRelationType
HttpAuthRelationTypeOf(const GURL
& request_url
,
395 const GURL
& first_party
);
397 // Returns whether the URLRequest identified by |transferred_request_id| is
398 // currently in the process of being transferred to a different renderer.
399 // This happens if a request is redirected cross-site and needs to be resumed
400 // by a new render view.
401 bool IsTransferredNavigation(
402 const GlobalRequestID
& transferred_request_id
) const;
404 ResourceLoader
* GetLoader(const GlobalRequestID
& id
) const;
405 ResourceLoader
* GetLoader(int child_id
, int request_id
) const;
407 // Registers |delegate| to receive resource IPC messages targeted to the
409 void RegisterResourceMessageDelegate(const GlobalRequestID
& id
,
410 ResourceMessageDelegate
* delegate
);
411 void UnregisterResourceMessageDelegate(const GlobalRequestID
& id
,
412 ResourceMessageDelegate
* delegate
);
414 int BuildLoadFlagsForRequest(const ResourceHostMsg_Request
& request_data
,
418 LoaderMap pending_loaders_
;
420 // Collection of temp files downloaded for child processes via
421 // the download_to_file mechanism. We avoid deleting them until
422 // the client no longer needs them.
423 typedef std::map
<int, scoped_refptr
<webkit_blob::ShareableFileReference
> >
424 DeletableFilesMap
; // key is request id
425 typedef std::map
<int, DeletableFilesMap
>
426 RegisteredTempFiles
; // key is child process id
427 RegisteredTempFiles registered_temp_files_
;
429 // A timer that periodically calls UpdateLoadStates while pending_requests_
431 scoped_ptr
<base::RepeatingTimer
<ResourceDispatcherHostImpl
> >
432 update_load_states_timer_
;
434 // We own the save file manager.
435 scoped_refptr
<SaveFileManager
> save_file_manager_
;
437 // Request ID for browser initiated requests. request_ids generated by
438 // child processes are counted up from 0, while browser created requests
439 // start at -2 and go down from there. (We need to start at -2 because -1 is
440 // used as a special value all over the resource_dispatcher_host for
441 // uninitialized variables.) This way, we no longer have the unlikely (but
442 // observed in the real world!) event where we have two requests with the same
446 // True if the resource dispatcher host has been shut down.
449 typedef std::vector
<linked_ptr
<ResourceLoader
> > BlockedLoadersList
;
450 typedef std::map
<GlobalRoutingID
, BlockedLoadersList
*> BlockedLoadersMap
;
451 BlockedLoadersMap blocked_loaders_map_
;
453 // Maps the child_ids to the approximate number of bytes
454 // being used to service its resource requests. No entry implies 0 cost.
455 typedef std::map
<int, OustandingRequestsStats
> OutstandingRequestsStatsMap
;
456 OutstandingRequestsStatsMap outstanding_requests_stats_map_
;
458 // |num_in_flight_requests_| is the total number of requests currently issued
459 // summed across all renderers.
460 int num_in_flight_requests_
;
462 // |max_num_in_flight_requests_| is the upper bound on how many requests
463 // can be in flight at once. It's based on the maximum number of file
464 // descriptors open per process. We need a global limit for the browser
466 int max_num_in_flight_requests_
;
468 // |max_num_in_flight_requests_| is the upper bound on how many requests
469 // can be issued at once. It's based on the maximum number of file
470 // descriptors open per process. We need a per-renderer limit so that no
471 // single renderer can hog the browser's limit.
472 int max_num_in_flight_requests_per_process_
;
474 // |max_outstanding_requests_cost_per_process_| is the upper bound on how
475 // many outstanding requests can be issued per child process host.
476 // The constraint is expressed in terms of bytes (where the cost of
477 // individual requests is given by CalculateApproximateMemoryCost).
478 // The total number of outstanding requests is roughly:
479 // (max_outstanding_requests_cost_per_process_ /
480 // kAvgBytesPerOutstandingRequest)
481 int max_outstanding_requests_cost_per_process_
;
483 // Time of the last user gesture. Stored so that we can add a load
484 // flag to requests occurring soon after a gesture to indicate they
485 // may be because of explicit user action.
486 base::TimeTicks last_user_gesture_time_
;
488 // Used during IPC message dispatching so that the handlers can get a pointer
489 // to the source of the message.
490 ResourceMessageFilter
* filter_
;
492 ResourceDispatcherHostDelegate
* delegate_
;
494 bool allow_cross_origin_auth_prompt_
;
496 // http://crbug.com/90971 - Assists in tracking down use-after-frees on
498 std::set
<const ResourceContext
*> active_resource_contexts_
;
500 typedef std::map
<GlobalRequestID
,
501 ObserverList
<ResourceMessageDelegate
>*> DelegateMap
;
502 DelegateMap delegate_map_
;
504 scoped_ptr
<ResourceScheduler
> scheduler_
;
506 typedef std::map
<GlobalRoutingID
, OfflinePolicy
*> OfflineMap
;
508 OfflineMap offline_policy_map_
;
510 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcherHostImpl
);
513 } // namespace content
515 #endif // CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_