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