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