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