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