Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / content / browser / loader / resource_dispatcher_host_impl.h
blob87181a3854c6c3a4cf0af0def319239e3e8a6c22
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.
9 //
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_
15 #include <map>
16 #include <set>
17 #include <string>
18 #include <vector>
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;
48 namespace net {
49 class URLRequestJobFactory;
52 namespace storage {
53 class ShareableFileReference;
56 namespace content {
57 class AppCacheService;
58 class NavigationURLLoaderImplCore;
59 class ResourceContext;
60 class ResourceDispatcherHostDelegate;
61 class ResourceMessageDelegate;
62 class ResourceMessageFilter;
63 class ResourceRequestInfoImpl;
64 class SaveFileManager;
65 class WebContentsImpl;
66 struct CommonNavigationParams;
67 struct DownloadSaveInfo;
68 struct NavigationRequestInfo;
69 struct Referrer;
71 class CONTENT_EXPORT ResourceDispatcherHostImpl
72 : public ResourceDispatcherHost,
73 public ResourceLoaderDelegate {
74 public:
75 ResourceDispatcherHostImpl();
76 ~ResourceDispatcherHostImpl() override;
78 // Returns the current ResourceDispatcherHostImpl. May return NULL if it
79 // hasn't been created yet.
80 static ResourceDispatcherHostImpl* Get();
82 // ResourceDispatcherHost implementation:
83 void SetDelegate(ResourceDispatcherHostDelegate* delegate) override;
84 void SetAllowCrossOriginAuthPrompt(bool value) override;
85 DownloadInterruptReason BeginDownload(
86 scoped_ptr<net::URLRequest> request,
87 const Referrer& referrer,
88 bool is_content_initiated,
89 ResourceContext* context,
90 int child_id,
91 int route_id,
92 bool prefer_cache,
93 bool do_not_prompt_for_login,
94 scoped_ptr<DownloadSaveInfo> save_info,
95 uint32 download_id,
96 const DownloadStartedCallback& started_callback) override;
97 void ClearLoginDelegateForRequest(net::URLRequest* request) override;
98 void BlockRequestsForRoute(int child_id, int route_id) override;
99 void ResumeBlockedRequestsForRoute(int child_id, int route_id) override;
101 // Puts the resource dispatcher host in an inactive state (unable to begin
102 // new requests). Cancels all pending requests.
103 void Shutdown();
105 // Notify the ResourceDispatcherHostImpl of a new resource context.
106 void AddResourceContext(ResourceContext* context);
108 // Notify the ResourceDispatcherHostImpl of a resource context destruction.
109 void RemoveResourceContext(ResourceContext* context);
111 // Resumes a request that deferred at response start.
112 void ResumeResponseDeferredAtStart(const GlobalRequestID& id);
114 // Force cancels any pending requests for the given |context|. This is
115 // necessary to ensure that before |context| goes away, all requests
116 // for it are dead.
117 void CancelRequestsForContext(ResourceContext* context);
119 // Returns true if the message was a resource message that was processed.
120 bool OnMessageReceived(const IPC::Message& message,
121 ResourceMessageFilter* filter);
123 // Initiates a save file from the browser process (as opposed to a resource
124 // request from the renderer or another child process).
125 void BeginSaveFile(const GURL& url,
126 const Referrer& referrer,
127 int child_id,
128 int route_id,
129 ResourceContext* context);
131 // Cancels the given request if it still exists.
132 void CancelRequest(int child_id, int request_id);
134 // Marks the request as "parked". This happens if a request is
135 // redirected cross-site and needs to be resumed by a new render view.
136 void MarkAsTransferredNavigation(const GlobalRequestID& id);
138 // Cancels a request previously marked as being transferred, for use when a
139 // navigation was cancelled.
140 void CancelTransferringNavigation(const GlobalRequestID& id);
142 // Resumes the request without transferring it to a new render view.
143 void ResumeDeferredNavigation(const GlobalRequestID& id);
145 // Returns the number of pending requests. This is designed for the unittests
146 int pending_requests() const {
147 return static_cast<int>(pending_loaders_.size());
150 // Intended for unit-tests only. Overrides the outstanding requests bound.
151 void set_max_outstanding_requests_cost_per_process(int limit) {
152 max_outstanding_requests_cost_per_process_ = limit;
154 void set_max_num_in_flight_requests_per_process(int limit) {
155 max_num_in_flight_requests_per_process_ = limit;
157 void set_max_num_in_flight_requests(int limit) {
158 max_num_in_flight_requests_ = limit;
161 // The average private bytes increase of the browser for each new pending
162 // request. Experimentally obtained.
163 static const int kAvgBytesPerOutstandingRequest = 4400;
165 SaveFileManager* save_file_manager() const {
166 return save_file_manager_.get();
169 // Called when a RenderViewHost is created.
170 void OnRenderViewHostCreated(int child_id,
171 int route_id,
172 bool is_visible,
173 bool is_audible);
175 // Called when a RenderViewHost is deleted.
176 void OnRenderViewHostDeleted(int child_id, int route_id);
178 // Called when a RenderViewHost starts or stops loading.
179 void OnRenderViewHostSetIsLoading(int child_id,
180 int route_id,
181 bool is_loading);
183 // Called when a RenderViewHost is hidden.
184 void OnRenderViewHostWasHidden(int child_id, int route_id);
186 // Called when a RenderViewHost is shown.
187 void OnRenderViewHostWasShown(int child_id, int route_id);
189 // Called when an AudioRenderHost starts or stops playing.
190 void OnAudioRenderHostStreamStateChanged(int child_id,
191 int route_id,
192 bool is_playing);
194 // Force cancels any pending requests for the given process.
195 void CancelRequestsForProcess(int child_id);
197 void OnUserGesture(WebContentsImpl* contents);
199 // Retrieves a net::URLRequest. Must be called from the IO thread.
200 net::URLRequest* GetURLRequest(const GlobalRequestID& request_id);
202 void RemovePendingRequest(int child_id, int request_id);
204 // Cancels any blocked request for the specified route id.
205 void CancelBlockedRequestsForRoute(int child_id, int route_id);
207 // Maintains a collection of temp files created in support of
208 // the download_to_file capability. Used to grant access to the
209 // child process and to defer deletion of the file until it's
210 // no longer needed.
211 void RegisterDownloadedTempFile(
212 int child_id, int request_id,
213 const base::FilePath& file_path);
214 void UnregisterDownloadedTempFile(int child_id, int request_id);
216 // Needed for the sync IPC message dispatcher macros.
217 bool Send(IPC::Message* message);
219 // Indicates whether third-party sub-content can pop-up HTTP basic auth
220 // dialog boxes.
221 bool allow_cross_origin_auth_prompt();
223 ResourceDispatcherHostDelegate* delegate() {
224 return delegate_;
227 // Must be called after the ResourceRequestInfo has been created
228 // and associated with the request.
229 // |id| should be |content::DownloadItem::kInvalidId| to request automatic
230 // assignment. This is marked virtual so it can be overriden in testing.
231 virtual scoped_ptr<ResourceHandler> CreateResourceHandlerForDownload(
232 net::URLRequest* request,
233 bool is_content_initiated,
234 bool must_download,
235 uint32 id,
236 scoped_ptr<DownloadSaveInfo> save_info,
237 const DownloadUrlParameters::OnStartedCallback& started_cb);
239 // Must be called after the ResourceRequestInfo has been created
240 // and associated with the request. If |payload| is set to a non-empty value,
241 // the value will be sent to the old resource handler instead of canceling
242 // it, except on HTTP errors. This is marked virtual so it can be overriden in
243 // testing.
244 virtual scoped_ptr<ResourceHandler> MaybeInterceptAsStream(
245 net::URLRequest* request,
246 ResourceResponse* response,
247 std::string* payload);
249 ResourceScheduler* scheduler() { return scheduler_.get(); }
251 // Called by a ResourceHandler when it's ready to start reading data and
252 // sending it to the renderer. Returns true if there are enough file
253 // descriptors available for the shared memory buffer. If false is returned,
254 // the request should cancel.
255 bool HasSufficientResourcesForRequest(net::URLRequest* request);
257 // Called by a ResourceHandler after it has finished its request and is done
258 // using its shared memory buffer. Frees up that file descriptor to be used
259 // elsewhere.
260 void FinishedWithResourcesForRequest(net::URLRequest* request);
262 // PlzNavigate: Begins a request for NavigationURLLoader. |loader| is the
263 // loader to attach to the leaf resource handler.
264 void BeginNavigationRequest(ResourceContext* resource_context,
265 int frame_tree_node_id,
266 const NavigationRequestInfo& info,
267 NavigationURLLoaderImplCore* loader);
269 private:
270 friend class ResourceDispatcherHostTest;
272 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest,
273 TestBlockedRequestsProcessDies);
274 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest,
275 CalculateApproximateMemoryCost);
276 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest,
277 DetachableResourceTimesOut);
278 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest,
279 TestProcessCancelDetachableTimesOut);
281 struct OustandingRequestsStats {
282 int memory_cost;
283 int num_requests;
286 friend class ShutdownTask;
287 friend class ResourceMessageDelegate;
289 // Information about status of a ResourceLoader.
290 struct LoadInfo {
291 GURL url;
292 net::LoadStateWithParam load_state;
293 uint64 upload_position;
294 uint64 upload_size;
297 // Map from ProcessID+RouteID pair to the "most interesting" LoadState.
298 typedef std::map<GlobalRoutingID, LoadInfo> LoadInfoMap;
300 // ResourceLoaderDelegate implementation:
301 ResourceDispatcherHostLoginDelegate* CreateLoginDelegate(
302 ResourceLoader* loader,
303 net::AuthChallengeInfo* auth_info) override;
304 bool HandleExternalProtocol(ResourceLoader* loader, const GURL& url) override;
305 void DidStartRequest(ResourceLoader* loader) override;
306 void DidReceiveRedirect(ResourceLoader* loader, const GURL& new_url) override;
307 void DidReceiveResponse(ResourceLoader* loader) override;
308 void DidFinishLoading(ResourceLoader* loader) override;
310 // An init helper that runs on the IO thread.
311 void OnInit();
313 // A shutdown helper that runs on the IO thread.
314 void OnShutdown();
316 // Helper function for regular and download requests.
317 void BeginRequestInternal(scoped_ptr<net::URLRequest> request,
318 scoped_ptr<ResourceHandler> handler);
320 void StartLoading(ResourceRequestInfoImpl* info,
321 const linked_ptr<ResourceLoader>& loader);
323 // We keep track of how much memory each request needs and how many requests
324 // are issued by each renderer. These are known as OustandingRequestStats.
325 // Memory limits apply to all requests sent to us by the renderers. There is a
326 // limit for each renderer. File descriptor limits apply to requests that are
327 // receiving their body. These are known as in-flight requests. There is a
328 // global limit that applies for the browser process. Each render is allowed
329 // to use up to a fraction of that.
331 // Returns the OustandingRequestsStats for |info|'s renderer, or an empty
332 // struct if that renderer has no outstanding requests.
333 OustandingRequestsStats GetOutstandingRequestsStats(
334 const ResourceRequestInfoImpl& info);
336 // Updates |outstanding_requests_stats_map_| with the specified |stats| for
337 // the renderer that made the request in |info|.
338 void UpdateOutstandingRequestsStats(const ResourceRequestInfoImpl& info,
339 const OustandingRequestsStats& stats);
341 // Called every time an outstanding request is created or deleted. |count|
342 // indicates whether the request is new or deleted. |count| must be 1 or -1.
343 OustandingRequestsStats IncrementOutstandingRequestsMemory(
344 int count,
345 const ResourceRequestInfoImpl& info);
347 // Called when an in flight request allocates or releases a shared memory
348 // buffer. |count| indicates whether the request is issuing or finishing.
349 // |count| must be 1 or -1.
350 OustandingRequestsStats IncrementOutstandingRequestsCount(
351 int count,
352 ResourceRequestInfoImpl* info);
354 // Estimate how much heap space |request| will consume to run.
355 static int CalculateApproximateMemoryCost(net::URLRequest* request);
357 // Force cancels any pending requests for the given route id. This method
358 // acts like CancelRequestsForProcess when route_id is -1.
359 void CancelRequestsForRoute(int child_id, int route_id);
361 // The list of all requests that we have pending. This list is not really
362 // optimized, and assumes that we have relatively few requests pending at once
363 // since some operations require brute-force searching of the list.
365 // It may be enhanced in the future to provide some kind of prioritization
366 // mechanism. We should also consider a hashtable or binary tree if it turns
367 // out we have a lot of things here.
368 typedef std::map<GlobalRequestID, linked_ptr<ResourceLoader> > LoaderMap;
370 // Deletes the pending request identified by the iterator passed in.
371 // This function will invalidate the iterator passed in. Callers should
372 // not rely on this iterator being valid on return.
373 void RemovePendingLoader(const LoaderMap::iterator& iter);
375 // This function returns true if the LoadInfo of |a| is "more interesting"
376 // than the LoadInfo of |b|. The load that is currently sending the larger
377 // request body is considered more interesting. If neither request is
378 // sending a body (Neither request has a body, or any request that has a body
379 // is not currently sending the body), the request that is further along is
380 // considered more interesting.
382 // This takes advantage of the fact that the load states are an enumeration
383 // listed in the order in which they usually occur during the lifetime of a
384 // request, so states with larger numeric values are generally further along
385 // toward completion.
387 // For example, by this measure "tranferring data" is a more interesting state
388 // than "resolving host" because when transferring data something is being
389 // done that corresponds to changes that the user might observe, whereas
390 // waiting for a host name to resolve implies being stuck.
391 static bool LoadInfoIsMoreInteresting(const LoadInfo& a, const LoadInfo& b);
393 // Used to marshal calls to LoadStateChanged from the IO to UI threads. All
394 // are done as a single callback to avoid spamming the UI thread.
395 static void UpdateLoadInfoOnUIThread(scoped_ptr<LoadInfoMap> info_map);
397 // Gets the most interesting LoadInfo for each GlobalRoutingID.
398 scoped_ptr<LoadInfoMap> GetLoadInfoForAllRoutes();
400 // Checks all pending requests and updates the load info if necessary.
401 void UpdateLoadInfo();
403 // Resumes or cancels (if |cancel_requests| is true) any blocked requests.
404 void ProcessBlockedRequestsForRoute(int child_id,
405 int route_id,
406 bool cancel_requests);
408 void OnRequestResource(int routing_id,
409 int request_id,
410 const ResourceHostMsg_Request& request_data);
411 void OnSyncLoad(int request_id,
412 const ResourceHostMsg_Request& request_data,
413 IPC::Message* sync_result);
415 // Update the ResourceRequestInfo and internal maps when a request is
416 // transferred from one process to another.
417 void UpdateRequestForTransfer(int child_id,
418 int route_id,
419 int request_id,
420 const ResourceHostMsg_Request& request_data,
421 const linked_ptr<ResourceLoader>& loader);
423 void BeginRequest(int request_id,
424 const ResourceHostMsg_Request& request_data,
425 IPC::Message* sync_result, // only valid for sync
426 int route_id); // only valid for async
428 // Creates a ResourceHandler to be used by BeginRequest() for normal resource
429 // loading.
430 scoped_ptr<ResourceHandler> CreateResourceHandler(
431 net::URLRequest* request,
432 const ResourceHostMsg_Request& request_data,
433 IPC::Message* sync_result,
434 int route_id,
435 int process_type,
436 int child_id,
437 ResourceContext* resource_context);
439 // Wraps |handler| in the standard resource handlers for normal resource
440 // loading and navigation requests. This adds BufferedResourceHandler and
441 // ResourceThrottles.
442 scoped_ptr<ResourceHandler> AddStandardHandlers(
443 net::URLRequest* request,
444 ResourceType resource_type,
445 ResourceContext* resource_context,
446 AppCacheService* appcache_service,
447 int child_id,
448 int route_id,
449 scoped_ptr<ResourceHandler> handler);
451 void OnDataDownloadedACK(int request_id);
452 void OnUploadProgressACK(int request_id);
453 void OnCancelRequest(int request_id);
454 void OnReleaseDownloadedFile(int request_id);
456 // Creates ResourceRequestInfoImpl for a download or page save.
457 // |download| should be true if the request is a file download.
458 ResourceRequestInfoImpl* CreateRequestInfo(
459 int child_id,
460 int route_id,
461 bool download,
462 ResourceContext* context);
464 // Relationship of resource being authenticated with the top level page.
465 enum HttpAuthRelationType {
466 HTTP_AUTH_RELATION_TOP, // Top-level page itself
467 HTTP_AUTH_RELATION_SAME_DOMAIN, // Sub-content from same domain
468 HTTP_AUTH_RELATION_BLOCKED_CROSS, // Blocked Sub-content from cross domain
469 HTTP_AUTH_RELATION_ALLOWED_CROSS, // Allowed Sub-content per command line
470 HTTP_AUTH_RELATION_LAST
473 HttpAuthRelationType HttpAuthRelationTypeOf(const GURL& request_url,
474 const GURL& first_party);
476 // Returns whether the URLRequest identified by |transferred_request_id| is
477 // currently in the process of being transferred to a different renderer.
478 // This happens if a request is redirected cross-site and needs to be resumed
479 // by a new render view.
480 bool IsTransferredNavigation(
481 const GlobalRequestID& transferred_request_id) const;
483 ResourceLoader* GetLoader(const GlobalRequestID& id) const;
484 ResourceLoader* GetLoader(int child_id, int request_id) const;
486 // Registers |delegate| to receive resource IPC messages targeted to the
487 // specified |id|.
488 void RegisterResourceMessageDelegate(const GlobalRequestID& id,
489 ResourceMessageDelegate* delegate);
490 void UnregisterResourceMessageDelegate(const GlobalRequestID& id,
491 ResourceMessageDelegate* delegate);
493 int BuildLoadFlagsForRequest(const ResourceHostMsg_Request& request_data,
494 int child_id,
495 bool is_sync_load);
497 LoaderMap pending_loaders_;
499 // Collection of temp files downloaded for child processes via
500 // the download_to_file mechanism. We avoid deleting them until
501 // the client no longer needs them.
502 typedef std::map<int, scoped_refptr<storage::ShareableFileReference> >
503 DeletableFilesMap; // key is request id
504 typedef std::map<int, DeletableFilesMap>
505 RegisteredTempFiles; // key is child process id
506 RegisteredTempFiles registered_temp_files_;
508 // A timer that periodically calls UpdateLoadStates while pending_requests_
509 // is not empty.
510 scoped_ptr<base::RepeatingTimer<ResourceDispatcherHostImpl> >
511 update_load_states_timer_;
513 // We own the save file manager.
514 scoped_refptr<SaveFileManager> save_file_manager_;
516 // Request ID for browser initiated requests. request_ids generated by
517 // child processes are counted up from 0, while browser created requests
518 // start at -2 and go down from there. (We need to start at -2 because -1 is
519 // used as a special value all over the resource_dispatcher_host for
520 // uninitialized variables.) This way, we no longer have the unlikely (but
521 // observed in the real world!) event where we have two requests with the same
522 // request_id_.
523 int request_id_;
525 // True if the resource dispatcher host has been shut down.
526 bool is_shutdown_;
528 typedef std::vector<linked_ptr<ResourceLoader> > BlockedLoadersList;
529 typedef std::map<GlobalRoutingID, BlockedLoadersList*> BlockedLoadersMap;
530 BlockedLoadersMap blocked_loaders_map_;
532 // Maps the child_ids to the approximate number of bytes
533 // being used to service its resource requests. No entry implies 0 cost.
534 typedef std::map<int, OustandingRequestsStats> OutstandingRequestsStatsMap;
535 OutstandingRequestsStatsMap outstanding_requests_stats_map_;
537 // |num_in_flight_requests_| is the total number of requests currently issued
538 // summed across all renderers.
539 int num_in_flight_requests_;
541 // |max_num_in_flight_requests_| is the upper bound on how many requests
542 // can be in flight at once. It's based on the maximum number of file
543 // descriptors open per process. We need a global limit for the browser
544 // process.
545 int max_num_in_flight_requests_;
547 // |max_num_in_flight_requests_| is the upper bound on how many requests
548 // can be issued at once. It's based on the maximum number of file
549 // descriptors open per process. We need a per-renderer limit so that no
550 // single renderer can hog the browser's limit.
551 int max_num_in_flight_requests_per_process_;
553 // |max_outstanding_requests_cost_per_process_| is the upper bound on how
554 // many outstanding requests can be issued per child process host.
555 // The constraint is expressed in terms of bytes (where the cost of
556 // individual requests is given by CalculateApproximateMemoryCost).
557 // The total number of outstanding requests is roughly:
558 // (max_outstanding_requests_cost_per_process_ /
559 // kAvgBytesPerOutstandingRequest)
560 int max_outstanding_requests_cost_per_process_;
562 // Time of the last user gesture. Stored so that we can add a load
563 // flag to requests occurring soon after a gesture to indicate they
564 // may be because of explicit user action.
565 base::TimeTicks last_user_gesture_time_;
567 // Used during IPC message dispatching so that the handlers can get a pointer
568 // to the source of the message.
569 ResourceMessageFilter* filter_;
571 ResourceDispatcherHostDelegate* delegate_;
573 bool allow_cross_origin_auth_prompt_;
575 // http://crbug.com/90971 - Assists in tracking down use-after-frees on
576 // shutdown.
577 std::set<const ResourceContext*> active_resource_contexts_;
579 typedef std::map<GlobalRequestID,
580 ObserverList<ResourceMessageDelegate>*> DelegateMap;
581 DelegateMap delegate_map_;
583 scoped_ptr<ResourceScheduler> scheduler_;
585 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcherHostImpl);
588 } // namespace content
590 #endif // CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_