OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This is the browser side of the resource dispatcher, it receives requests | 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 | 6 // from the child process (i.e. [Renderer, Plugin, Worker]ProcessHost), and |
7 // dispatches them to URLRequests. It then forwards the messages from the | 7 // dispatches them to URLRequests. It then forwards the messages from the |
8 // URLRequests back to the correct process for handling. | 8 // URLRequests back to the correct process for handling. |
9 // | 9 // |
10 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc
e-loading | 10 // See http://dev.chromium.org/developers/design-documents/multi-process-resourc
e-loading |
(...skipping 21 matching lines...) Expand all Loading... |
32 #include "content/browser/loader/resource_loader_delegate.h" | 32 #include "content/browser/loader/resource_loader_delegate.h" |
33 #include "content/browser/loader/resource_scheduler.h" | 33 #include "content/browser/loader/resource_scheduler.h" |
34 #include "content/common/content_export.h" | 34 #include "content/common/content_export.h" |
35 #include "content/common/resource_request_body.h" | 35 #include "content/common/resource_request_body.h" |
36 #include "content/public/browser/child_process_data.h" | 36 #include "content/public/browser/child_process_data.h" |
37 #include "content/public/browser/download_item.h" | 37 #include "content/public/browser/download_item.h" |
38 #include "content/public/browser/download_url_parameters.h" | 38 #include "content/public/browser/download_url_parameters.h" |
39 #include "content/public/browser/global_request_id.h" | 39 #include "content/public/browser/global_request_id.h" |
40 #include "content/public/browser/notification_types.h" | 40 #include "content/public/browser/notification_types.h" |
41 #include "content/public/browser/resource_dispatcher_host.h" | 41 #include "content/public/browser/resource_dispatcher_host.h" |
| 42 #include "content/public/browser/web_contents_observer.h" |
42 #include "content/public/common/resource_type.h" | 43 #include "content/public/common/resource_type.h" |
43 #include "ipc/ipc_message.h" | 44 #include "ipc/ipc_message.h" |
44 #include "net/base/request_priority.h" | 45 #include "net/base/request_priority.h" |
45 #include "net/cookies/canonical_cookie.h" | 46 #include "net/cookies/canonical_cookie.h" |
46 #include "net/url_request/url_request.h" | 47 #include "net/url_request/url_request.h" |
47 | 48 |
48 class ResourceHandler; | 49 class ResourceHandler; |
49 struct ResourceHostMsg_Request; | 50 struct ResourceHostMsg_Request; |
50 | 51 |
51 namespace base { | 52 namespace base { |
52 class FilePath; | 53 class FilePath; |
53 } | 54 } |
54 | 55 |
55 namespace net { | 56 namespace net { |
56 class URLRequestJobFactory; | 57 class URLRequestJobFactory; |
57 } | 58 } |
58 | 59 |
59 namespace storage { | 60 namespace storage { |
60 class ShareableFileReference; | 61 class ShareableFileReference; |
61 } | 62 } |
62 | 63 |
63 namespace content { | 64 namespace content { |
64 class AppCacheService; | 65 class AppCacheService; |
65 class AsyncRevalidationManager; | 66 class AsyncRevalidationManager; |
| 67 class FrameTree; |
66 class NavigationURLLoaderImplCore; | 68 class NavigationURLLoaderImplCore; |
| 69 class RenderFrameHostImpl; |
67 class ResourceContext; | 70 class ResourceContext; |
68 class ResourceDispatcherHostDelegate; | 71 class ResourceDispatcherHostDelegate; |
69 class ResourceMessageDelegate; | 72 class ResourceMessageDelegate; |
70 class ResourceMessageFilter; | 73 class ResourceMessageFilter; |
71 class ResourceRequestInfoImpl; | 74 class ResourceRequestInfoImpl; |
72 class SaveFileManager; | 75 class SaveFileManager; |
73 class ServiceWorkerNavigationHandleCore; | 76 class ServiceWorkerNavigationHandleCore; |
74 class WebContentsImpl; | 77 class WebContentsImpl; |
75 struct CommonNavigationParams; | 78 struct CommonNavigationParams; |
76 struct DownloadSaveInfo; | 79 struct DownloadSaveInfo; |
77 struct NavigationRequestInfo; | 80 struct NavigationRequestInfo; |
78 struct Referrer; | 81 struct Referrer; |
79 | 82 |
| 83 // This class is responsible for notifying the IO thread (specifically, the |
| 84 // ResourceDispatcherHostImpl) of frame events. It has an interace for callers |
| 85 // to use and also sends notifications on WebContentsObserver events. All |
| 86 // methods (static or class) will be called from the UI thread and post to the |
| 87 // IO thread. |
| 88 // TODO(csharrison): Add methods tracking visibility and audio changes, to |
| 89 // propogate to the ResourceScheduler. |
| 90 class LoaderIOThreadNotifier : public WebContentsObserver { |
| 91 public: |
| 92 explicit LoaderIOThreadNotifier(WebContents* web_contents); |
| 93 ~LoaderIOThreadNotifier() override; |
| 94 |
| 95 // content::WebContentsObserver: |
| 96 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; |
| 97 }; |
| 98 |
80 class CONTENT_EXPORT ResourceDispatcherHostImpl | 99 class CONTENT_EXPORT ResourceDispatcherHostImpl |
81 : public ResourceDispatcherHost, | 100 : public ResourceDispatcherHost, |
82 public ResourceLoaderDelegate { | 101 public ResourceLoaderDelegate { |
83 public: | 102 public: |
84 ResourceDispatcherHostImpl(); | 103 ResourceDispatcherHostImpl(); |
85 ~ResourceDispatcherHostImpl() override; | 104 ~ResourceDispatcherHostImpl() override; |
86 | 105 |
87 // Returns the current ResourceDispatcherHostImpl. May return NULL if it | 106 // Returns the current ResourceDispatcherHostImpl. May return NULL if it |
88 // hasn't been created yet. | 107 // hasn't been created yet. |
89 static ResourceDispatcherHostImpl* Get(); | 108 static ResourceDispatcherHostImpl* Get(); |
90 | 109 |
| 110 // The following static methods should all be called from the UI thread. |
| 111 |
| 112 // Resumes requests for a given render frame routing id. This will only resume |
| 113 // requests for a single frame. |
| 114 static void ResumeBlockedRequestsForRouteFromUI( |
| 115 const GlobalFrameRoutingId& global_routing_id); |
| 116 |
| 117 // Blocks (and does not start) all requests for the frame and its subframes. |
| 118 static void BlockRequestsForFrameFromUI(RenderFrameHost* root_frame_host); |
| 119 |
| 120 // Resumes any blocked requests for the specified frame and its subframes. |
| 121 static void ResumeBlockedRequestsForFrameFromUI( |
| 122 RenderFrameHost* root_frame_host); |
| 123 |
| 124 // Cancels any blocked request for the frame and its subframes. |
| 125 static void CancelBlockedRequestsForFrameFromUI( |
| 126 RenderFrameHostImpl* root_frame_host); |
| 127 |
91 // ResourceDispatcherHost implementation: | 128 // ResourceDispatcherHost implementation: |
92 void SetDelegate(ResourceDispatcherHostDelegate* delegate) override; | 129 void SetDelegate(ResourceDispatcherHostDelegate* delegate) override; |
93 void SetAllowCrossOriginAuthPrompt(bool value) override; | 130 void SetAllowCrossOriginAuthPrompt(bool value) override; |
94 DownloadInterruptReason BeginDownload( | 131 DownloadInterruptReason BeginDownload( |
95 scoped_ptr<net::URLRequest> request, | 132 scoped_ptr<net::URLRequest> request, |
96 const Referrer& referrer, | 133 const Referrer& referrer, |
97 bool is_content_initiated, | 134 bool is_content_initiated, |
98 ResourceContext* context, | 135 ResourceContext* context, |
99 int child_id, | 136 int child_id, |
100 int render_view_route_id, | 137 int render_view_route_id, |
101 int render_frame_route_id, | 138 int render_frame_route_id, |
102 bool prefer_cache, | 139 bool prefer_cache, |
103 bool do_not_prompt_for_login, | 140 bool do_not_prompt_for_login, |
104 scoped_ptr<DownloadSaveInfo> save_info, | 141 scoped_ptr<DownloadSaveInfo> save_info, |
105 uint32_t download_id, | 142 uint32_t download_id, |
106 const DownloadStartedCallback& started_callback) override; | 143 const DownloadStartedCallback& started_callback) override; |
107 void ClearLoginDelegateForRequest(net::URLRequest* request) override; | 144 void ClearLoginDelegateForRequest(net::URLRequest* request) override; |
108 void BlockRequestsForRoute(int child_id, int route_id) override; | |
109 void ResumeBlockedRequestsForRoute(int child_id, int route_id) override; | |
110 | 145 |
111 // Puts the resource dispatcher host in an inactive state (unable to begin | 146 // Puts the resource dispatcher host in an inactive state (unable to begin |
112 // new requests). Cancels all pending requests. | 147 // new requests). Cancels all pending requests. |
113 void Shutdown(); | 148 void Shutdown(); |
114 | 149 |
115 // Notify the ResourceDispatcherHostImpl of a new resource context. | 150 // Notify the ResourceDispatcherHostImpl of a new resource context. |
116 void AddResourceContext(ResourceContext* context); | 151 void AddResourceContext(ResourceContext* context); |
117 | 152 |
118 // Notify the ResourceDispatcherHostImpl of a resource context destruction. | 153 // Notify the ResourceDispatcherHostImpl of a resource context destruction. |
119 void RemoveResourceContext(ResourceContext* context); | 154 void RemoveResourceContext(ResourceContext* context); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 // Force cancels any pending requests for the given process. | 226 // Force cancels any pending requests for the given process. |
192 void CancelRequestsForProcess(int child_id); | 227 void CancelRequestsForProcess(int child_id); |
193 | 228 |
194 void OnUserGesture(WebContentsImpl* contents); | 229 void OnUserGesture(WebContentsImpl* contents); |
195 | 230 |
196 // Retrieves a net::URLRequest. Must be called from the IO thread. | 231 // Retrieves a net::URLRequest. Must be called from the IO thread. |
197 net::URLRequest* GetURLRequest(const GlobalRequestID& request_id); | 232 net::URLRequest* GetURLRequest(const GlobalRequestID& request_id); |
198 | 233 |
199 void RemovePendingRequest(int child_id, int request_id); | 234 void RemovePendingRequest(int child_id, int request_id); |
200 | 235 |
| 236 // Causes all new requests for the route identified by |routing_id| to be |
| 237 // blocked (not being started) until ResumeBlockedRequestsForRoute is called. |
| 238 void BlockRequestsForRoute(const GlobalFrameRoutingId& global_routing_id); |
| 239 |
| 240 // Resumes any blocked request for the specified route id. |
| 241 void ResumeBlockedRequestsForRoute( |
| 242 const GlobalFrameRoutingId& global_routing_id); |
| 243 |
201 // Cancels any blocked request for the specified route id. | 244 // Cancels any blocked request for the specified route id. |
202 void CancelBlockedRequestsForRoute(int child_id, int route_id); | 245 void CancelBlockedRequestsForRoute( |
| 246 const GlobalFrameRoutingId& global_routing_id); |
203 | 247 |
204 // Maintains a collection of temp files created in support of | 248 // Maintains a collection of temp files created in support of |
205 // the download_to_file capability. Used to grant access to the | 249 // the download_to_file capability. Used to grant access to the |
206 // child process and to defer deletion of the file until it's | 250 // child process and to defer deletion of the file until it's |
207 // no longer needed. | 251 // no longer needed. |
208 void RegisterDownloadedTempFile( | 252 void RegisterDownloadedTempFile( |
209 int child_id, int request_id, | 253 int child_id, int request_id, |
210 const base::FilePath& file_path); | 254 const base::FilePath& file_path); |
211 void UnregisterDownloadedTempFile(int child_id, int request_id); | 255 void UnregisterDownloadedTempFile(int child_id, int request_id); |
212 | 256 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 ResourceContext* resource_context, | 314 ResourceContext* resource_context, |
271 const NavigationRequestInfo& info, | 315 const NavigationRequestInfo& info, |
272 NavigationURLLoaderImplCore* loader, | 316 NavigationURLLoaderImplCore* loader, |
273 ServiceWorkerNavigationHandleCore* service_worker_handle_core); | 317 ServiceWorkerNavigationHandleCore* service_worker_handle_core); |
274 | 318 |
275 // Turns on stale-while-revalidate support, regardless of command-line flags | 319 // Turns on stale-while-revalidate support, regardless of command-line flags |
276 // or experiment status. For unit tests only. | 320 // or experiment status. For unit tests only. |
277 void EnableStaleWhileRevalidateForTesting(); | 321 void EnableStaleWhileRevalidateForTesting(); |
278 | 322 |
279 private: | 323 private: |
| 324 friend class LoaderIOThreadNotifier; |
280 friend class ResourceDispatcherHostTest; | 325 friend class ResourceDispatcherHostTest; |
281 | 326 |
282 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, | 327 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, |
283 TestBlockedRequestsProcessDies); | 328 TestBlockedRequestsProcessDies); |
284 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, | 329 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, |
285 CalculateApproximateMemoryCost); | 330 CalculateApproximateMemoryCost); |
286 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, | 331 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, |
287 DetachableResourceTimesOut); | 332 DetachableResourceTimesOut); |
288 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, | 333 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, |
289 TestProcessCancelDetachableTimesOut); | 334 TestProcessCancelDetachableTimesOut); |
(...skipping 26 matching lines...) Expand all Loading... |
316 void DidReceiveRedirect(ResourceLoader* loader, const GURL& new_url) override; | 361 void DidReceiveRedirect(ResourceLoader* loader, const GURL& new_url) override; |
317 void DidReceiveResponse(ResourceLoader* loader) override; | 362 void DidReceiveResponse(ResourceLoader* loader) override; |
318 void DidFinishLoading(ResourceLoader* loader) override; | 363 void DidFinishLoading(ResourceLoader* loader) override; |
319 | 364 |
320 // An init helper that runs on the IO thread. | 365 // An init helper that runs on the IO thread. |
321 void OnInit(); | 366 void OnInit(); |
322 | 367 |
323 // A shutdown helper that runs on the IO thread. | 368 // A shutdown helper that runs on the IO thread. |
324 void OnShutdown(); | 369 void OnShutdown(); |
325 | 370 |
| 371 void OnRenderFrameDeleted(const GlobalFrameRoutingId& global_routing_id); |
| 372 |
326 // Helper function for regular and download requests. | 373 // Helper function for regular and download requests. |
327 void BeginRequestInternal(scoped_ptr<net::URLRequest> request, | 374 void BeginRequestInternal(scoped_ptr<net::URLRequest> request, |
328 scoped_ptr<ResourceHandler> handler); | 375 scoped_ptr<ResourceHandler> handler); |
329 | 376 |
330 void StartLoading(ResourceRequestInfoImpl* info, | 377 void StartLoading(ResourceRequestInfoImpl* info, |
331 scoped_ptr<ResourceLoader> loader); | 378 scoped_ptr<ResourceLoader> loader); |
332 | 379 |
333 // We keep track of how much memory each request needs and how many requests | 380 // We keep track of how much memory each request needs and how many requests |
334 // are issued by each renderer. These are known as OustandingRequestStats. | 381 // are issued by each renderer. These are known as OustandingRequestStats. |
335 // Memory limits apply to all requests sent to us by the renderers. There is a | 382 // Memory limits apply to all requests sent to us by the renderers. There is a |
(...skipping 22 matching lines...) Expand all Loading... |
358 // buffer. |count| indicates whether the request is issuing or finishing. | 405 // buffer. |count| indicates whether the request is issuing or finishing. |
359 // |count| must be 1 or -1. | 406 // |count| must be 1 or -1. |
360 OustandingRequestsStats IncrementOutstandingRequestsCount( | 407 OustandingRequestsStats IncrementOutstandingRequestsCount( |
361 int count, | 408 int count, |
362 ResourceRequestInfoImpl* info); | 409 ResourceRequestInfoImpl* info); |
363 | 410 |
364 // Estimate how much heap space |request| will consume to run. | 411 // Estimate how much heap space |request| will consume to run. |
365 static int CalculateApproximateMemoryCost(net::URLRequest* request); | 412 static int CalculateApproximateMemoryCost(net::URLRequest* request); |
366 | 413 |
367 // Force cancels any pending requests for the given route id. This method | 414 // Force cancels any pending requests for the given route id. This method |
368 // acts like CancelRequestsForProcess when route_id is -1. | 415 // acts like CancelRequestsForProcess when the |route_id| member of |
369 void CancelRequestsForRoute(int child_id, int route_id); | 416 // |routing_id| is MSG_ROUTING_NONE. |
| 417 void CancelRequestsForRoute(const GlobalFrameRoutingId& global_routing_id); |
370 | 418 |
371 // The list of all requests that we have pending. This list is not really | 419 // The list of all requests that we have pending. This list is not really |
372 // optimized, and assumes that we have relatively few requests pending at once | 420 // optimized, and assumes that we have relatively few requests pending at once |
373 // since some operations require brute-force searching of the list. | 421 // since some operations require brute-force searching of the list. |
374 // | 422 // |
375 // It may be enhanced in the future to provide some kind of prioritization | 423 // It may be enhanced in the future to provide some kind of prioritization |
376 // mechanism. We should also consider a hashtable or binary tree if it turns | 424 // mechanism. We should also consider a hashtable or binary tree if it turns |
377 // out we have a lot of things here. | 425 // out we have a lot of things here. |
378 using LoaderMap = std::map<GlobalRequestID, scoped_ptr<ResourceLoader>>; | 426 using LoaderMap = std::map<GlobalRequestID, scoped_ptr<ResourceLoader>>; |
379 | 427 |
(...skipping 24 matching lines...) Expand all Loading... |
404 // are done as a single callback to avoid spamming the UI thread. | 452 // are done as a single callback to avoid spamming the UI thread. |
405 static void UpdateLoadInfoOnUIThread(scoped_ptr<LoadInfoMap> info_map); | 453 static void UpdateLoadInfoOnUIThread(scoped_ptr<LoadInfoMap> info_map); |
406 | 454 |
407 // Gets the most interesting LoadInfo for each GlobalRoutingID. | 455 // Gets the most interesting LoadInfo for each GlobalRoutingID. |
408 scoped_ptr<LoadInfoMap> GetLoadInfoForAllRoutes(); | 456 scoped_ptr<LoadInfoMap> GetLoadInfoForAllRoutes(); |
409 | 457 |
410 // Checks all pending requests and updates the load info if necessary. | 458 // Checks all pending requests and updates the load info if necessary. |
411 void UpdateLoadInfo(); | 459 void UpdateLoadInfo(); |
412 | 460 |
413 // Resumes or cancels (if |cancel_requests| is true) any blocked requests. | 461 // Resumes or cancels (if |cancel_requests| is true) any blocked requests. |
414 void ProcessBlockedRequestsForRoute(int child_id, | 462 void ProcessBlockedRequestsForRoute( |
415 int route_id, | 463 const GlobalFrameRoutingId& global_routing_id, |
416 bool cancel_requests); | 464 bool cancel_requests); |
417 | 465 |
418 void OnRequestResource(int routing_id, | 466 void OnRequestResource(int routing_id, |
419 int request_id, | 467 int request_id, |
420 const ResourceHostMsg_Request& request_data); | 468 const ResourceHostMsg_Request& request_data); |
421 void OnSyncLoad(int request_id, | 469 void OnSyncLoad(int request_id, |
422 const ResourceHostMsg_Request& request_data, | 470 const ResourceHostMsg_Request& request_data, |
423 IPC::Message* sync_result); | 471 IPC::Message* sync_result); |
424 | 472 |
425 bool IsRequestIDInUse(const GlobalRequestID& id) const; | 473 bool IsRequestIDInUse(const GlobalRequestID& id) const; |
426 | 474 |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 // uninitialized variables.) This way, we no longer have the unlikely (but | 582 // uninitialized variables.) This way, we no longer have the unlikely (but |
535 // observed in the real world!) event where we have two requests with the same | 583 // observed in the real world!) event where we have two requests with the same |
536 // request_id_. | 584 // request_id_. |
537 int request_id_; | 585 int request_id_; |
538 | 586 |
539 // True if the resource dispatcher host has been shut down. | 587 // True if the resource dispatcher host has been shut down. |
540 bool is_shutdown_; | 588 bool is_shutdown_; |
541 | 589 |
542 using BlockedLoadersList = std::vector<scoped_ptr<ResourceLoader>>; | 590 using BlockedLoadersList = std::vector<scoped_ptr<ResourceLoader>>; |
543 using BlockedLoadersMap = | 591 using BlockedLoadersMap = |
544 std::map<GlobalRoutingID, scoped_ptr<BlockedLoadersList>>; | 592 std::map<GlobalFrameRoutingId, scoped_ptr<BlockedLoadersList>>; |
545 BlockedLoadersMap blocked_loaders_map_; | 593 BlockedLoadersMap blocked_loaders_map_; |
546 | 594 |
547 // Maps the child_ids to the approximate number of bytes | 595 // Maps the child_ids to the approximate number of bytes |
548 // being used to service its resource requests. No entry implies 0 cost. | 596 // being used to service its resource requests. No entry implies 0 cost. |
549 typedef std::map<int, OustandingRequestsStats> OutstandingRequestsStatsMap; | 597 typedef std::map<int, OustandingRequestsStats> OutstandingRequestsStatsMap; |
550 OutstandingRequestsStatsMap outstanding_requests_stats_map_; | 598 OutstandingRequestsStatsMap outstanding_requests_stats_map_; |
551 | 599 |
552 // |num_in_flight_requests_| is the total number of requests currently issued | 600 // |num_in_flight_requests_| is the total number of requests currently issued |
553 // summed across all renderers. | 601 // summed across all renderers. |
554 int num_in_flight_requests_; | 602 int num_in_flight_requests_; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 DelegateMap delegate_map_; | 648 DelegateMap delegate_map_; |
601 | 649 |
602 scoped_ptr<ResourceScheduler> scheduler_; | 650 scoped_ptr<ResourceScheduler> scheduler_; |
603 | 651 |
604 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcherHostImpl); | 652 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcherHostImpl); |
605 }; | 653 }; |
606 | 654 |
607 } // namespace content | 655 } // namespace content |
608 | 656 |
609 #endif // CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_ | 657 #endif // CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_ |
OLD | NEW |