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 |
11 | 11 |
12 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_ | 12 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_ |
13 #define CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_ | 13 #define CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_ |
14 | 14 |
15 #include <stdint.h> | 15 #include <stdint.h> |
16 | 16 |
17 #include <map> | 17 #include <map> |
18 #include <memory> | 18 #include <memory> |
19 #include <string> | 19 #include <string> |
20 #include <vector> | 20 #include <vector> |
21 | 21 |
22 #include "base/gtest_prod_util.h" | 22 #include "base/gtest_prod_util.h" |
23 #include "base/macros.h" | 23 #include "base/macros.h" |
24 #include "base/observer_list.h" | 24 #include "base/observer_list.h" |
25 #include "base/time/time.h" | 25 #include "base/time/time.h" |
26 #include "base/timer/timer.h" | 26 #include "base/timer/timer.h" |
27 #include "content/browser/download/download_resource_handler.h" | |
28 #include "content/browser/download/save_types.h" | 27 #include "content/browser/download/save_types.h" |
29 #include "content/browser/loader/global_routing_id.h" | 28 #include "content/browser/loader/global_routing_id.h" |
30 #include "content/browser/loader/resource_loader.h" | 29 #include "content/browser/loader/resource_loader.h" |
31 #include "content/browser/loader/resource_loader_delegate.h" | 30 #include "content/browser/loader/resource_loader_delegate.h" |
32 #include "content/browser/loader/resource_scheduler.h" | 31 #include "content/browser/loader/resource_scheduler.h" |
33 #include "content/common/content_export.h" | 32 #include "content/common/content_export.h" |
34 #include "content/common/url_loader.mojom.h" | 33 #include "content/common/url_loader.mojom.h" |
35 #include "content/public/browser/global_request_id.h" | 34 #include "content/public/browser/global_request_id.h" |
36 #include "content/public/browser/resource_dispatcher_host.h" | 35 #include "content/public/browser/resource_dispatcher_host.h" |
37 #include "content/public/common/request_context_type.h" | 36 #include "content/public/common/request_context_type.h" |
(...skipping 20 matching lines...) Expand all Loading... |
58 | 57 |
59 namespace content { | 58 namespace content { |
60 class AppCacheService; | 59 class AppCacheService; |
61 class AsyncRevalidationManager; | 60 class AsyncRevalidationManager; |
62 class CertStore; | 61 class CertStore; |
63 class LoaderDelegate; | 62 class LoaderDelegate; |
64 class NavigationURLLoaderImplCore; | 63 class NavigationURLLoaderImplCore; |
65 class RenderFrameHostImpl; | 64 class RenderFrameHostImpl; |
66 class ResourceContext; | 65 class ResourceContext; |
67 class ResourceDispatcherHostDelegate; | 66 class ResourceDispatcherHostDelegate; |
| 67 class ResourceHandler; |
68 class ResourceMessageDelegate; | 68 class ResourceMessageDelegate; |
69 class ResourceMessageFilter; | 69 class ResourceMessageFilter; |
70 class ResourceRequestInfoImpl; | 70 class ResourceRequestInfoImpl; |
71 class ServiceWorkerNavigationHandleCore; | 71 class ServiceWorkerNavigationHandleCore; |
72 struct CommonNavigationParams; | 72 struct CommonNavigationParams; |
73 struct DownloadSaveInfo; | |
74 struct NavigationRequestInfo; | 73 struct NavigationRequestInfo; |
75 struct Referrer; | 74 struct Referrer; |
76 struct ResourceRequest; | 75 struct ResourceRequest; |
77 | 76 |
78 namespace mojom { | 77 namespace mojom { |
79 class URLLoader; | 78 class URLLoader; |
80 } // namespace mojom | 79 } // namespace mojom |
81 | 80 |
| 81 using CreateDownloadHandlerIntercept = |
| 82 base::Callback<std::unique_ptr<ResourceHandler>(net::URLRequest*)>; |
| 83 |
82 class CONTENT_EXPORT ResourceDispatcherHostImpl | 84 class CONTENT_EXPORT ResourceDispatcherHostImpl |
83 : public ResourceDispatcherHost, | 85 : public ResourceDispatcherHost, |
84 public ResourceLoaderDelegate { | 86 public ResourceLoaderDelegate { |
85 public: | 87 public: |
| 88 // This constructor should be used if we want downloads to work correctly. |
| 89 // TODO(ananta) |
| 90 // Work on moving creation of download handlers out of |
| 91 // ResourceDispatcherHostImpl. |
| 92 ResourceDispatcherHostImpl( |
| 93 CreateDownloadHandlerIntercept download_handler_intercept); |
86 ResourceDispatcherHostImpl(); | 94 ResourceDispatcherHostImpl(); |
87 ~ResourceDispatcherHostImpl() override; | 95 ~ResourceDispatcherHostImpl() override; |
88 | 96 |
89 // Returns the current ResourceDispatcherHostImpl. May return NULL if it | 97 // Returns the current ResourceDispatcherHostImpl. May return NULL if it |
90 // hasn't been created yet. | 98 // hasn't been created yet. |
91 static ResourceDispatcherHostImpl* Get(); | 99 static ResourceDispatcherHostImpl* Get(); |
92 | 100 |
93 // The following static methods should all be called from the UI thread. | 101 // The following static methods should all be called from the UI thread. |
94 | 102 |
95 // Resumes requests for a given render frame routing id. This will only resume | 103 // Resumes requests for a given render frame routing id. This will only resume |
(...skipping 26 matching lines...) Expand all Loading... |
122 | 130 |
123 // Force cancels any pending requests for the given |context|. This is | 131 // Force cancels any pending requests for the given |context|. This is |
124 // necessary to ensure that before |context| goes away, all requests | 132 // necessary to ensure that before |context| goes away, all requests |
125 // for it are dead. | 133 // for it are dead. |
126 void CancelRequestsForContext(ResourceContext* context); | 134 void CancelRequestsForContext(ResourceContext* context); |
127 | 135 |
128 // Returns true if the message was a resource message that was processed. | 136 // Returns true if the message was a resource message that was processed. |
129 bool OnMessageReceived(const IPC::Message& message, | 137 bool OnMessageReceived(const IPC::Message& message, |
130 ResourceMessageFilter* filter); | 138 ResourceMessageFilter* filter); |
131 | 139 |
132 DownloadInterruptReason BeginDownload( | |
133 std::unique_ptr<net::URLRequest> request, | |
134 const Referrer& referrer, | |
135 bool is_content_initiated, | |
136 ResourceContext* context, | |
137 int render_process_id, | |
138 int render_view_route_id, | |
139 int render_frame_route_id, | |
140 bool do_not_prompt_for_login); | |
141 | |
142 // Initiates a save file from the browser process (as opposed to a resource | |
143 // request from the renderer or another child process). | |
144 void BeginSaveFile(const GURL& url, | |
145 const Referrer& referrer, | |
146 SaveItemId save_item_id, | |
147 SavePackageId save_package_id, | |
148 int child_id, | |
149 int render_view_route_id, | |
150 int render_frame_route_id, | |
151 ResourceContext* context); | |
152 | |
153 // Cancels the given request if it still exists. | 140 // Cancels the given request if it still exists. |
154 void CancelRequest(int child_id, int request_id); | 141 void CancelRequest(int child_id, int request_id); |
155 | 142 |
156 // Marks the request, with its current |response|, as "parked". This | 143 // Marks the request, with its current |response|, as "parked". This |
157 // happens if a request is redirected cross-site and needs to be | 144 // happens if a request is redirected cross-site and needs to be |
158 // resumed by a new process. | 145 // resumed by a new process. |
159 void MarkAsTransferredNavigation( | 146 void MarkAsTransferredNavigation( |
160 const GlobalRequestID& id, | 147 const GlobalRequestID& id, |
161 const scoped_refptr<ResourceResponse>& response); | 148 const scoped_refptr<ResourceResponse>& response); |
162 | 149 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 // dialog boxes. | 224 // dialog boxes. |
238 bool allow_cross_origin_auth_prompt(); | 225 bool allow_cross_origin_auth_prompt(); |
239 | 226 |
240 ResourceDispatcherHostDelegate* delegate() { | 227 ResourceDispatcherHostDelegate* delegate() { |
241 return delegate_; | 228 return delegate_; |
242 } | 229 } |
243 | 230 |
244 // Must be called after the ResourceRequestInfo has been created | 231 // Must be called after the ResourceRequestInfo has been created |
245 // and associated with the request. | 232 // and associated with the request. |
246 // This is marked virtual so it can be overriden in testing. | 233 // This is marked virtual so it can be overriden in testing. |
| 234 // TODO(ananta) |
| 235 // This method should be removed or moved outside this class. |
247 virtual std::unique_ptr<ResourceHandler> CreateResourceHandlerForDownload( | 236 virtual std::unique_ptr<ResourceHandler> CreateResourceHandlerForDownload( |
248 net::URLRequest* request, | 237 net::URLRequest* request, |
249 bool is_content_initiated, | 238 bool is_content_initiated, |
250 bool must_download); | 239 bool must_download); |
251 | 240 |
252 // Called to determine whether the response to |request| should be intercepted | 241 // Called to determine whether the response to |request| should be intercepted |
253 // and handled as a stream. Streams are used to pass direct access to a | 242 // and handled as a stream. Streams are used to pass direct access to a |
254 // resource response to another application (e.g. a web page) without being | 243 // resource response to another application (e.g. a web page) without being |
255 // handled by the browser itself. If the request should be intercepted as a | 244 // handled by the browser itself. If the request should be intercepted as a |
256 // stream, a StreamResourceHandler is returned which provides access to the | 245 // stream, a StreamResourceHandler is returned which provides access to the |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 | 291 |
303 // Called when loading a request with mojo. | 292 // Called when loading a request with mojo. |
304 void OnRequestResourceWithMojo( | 293 void OnRequestResourceWithMojo( |
305 int routing_id, | 294 int routing_id, |
306 int request_id, | 295 int request_id, |
307 const ResourceRequest& request, | 296 const ResourceRequest& request, |
308 mojo::InterfaceRequest<mojom::URLLoader> mojo_request, | 297 mojo::InterfaceRequest<mojom::URLLoader> mojo_request, |
309 mojom::URLLoaderClientPtr url_loader_client, | 298 mojom::URLLoaderClientPtr url_loader_client, |
310 ResourceMessageFilter* filter); | 299 ResourceMessageFilter* filter); |
311 | 300 |
| 301 // Helper function for initializing the |request| passed in. By initializing |
| 302 // we mean setting the |referrer| on the |request|, associating the |
| 303 // ResourceRequestInfoImpl structure with the |request|, etc. |
| 304 // This function should be called for invoking the BeginURLRequest() function |
| 305 // to initiate a URL request. |
| 306 void InitializeURLRequest(net::URLRequest* request, |
| 307 const Referrer& referrer, |
| 308 bool is_download, |
| 309 int render_process_host_id, |
| 310 int render_view_routing_id, |
| 311 int render_frame_routing_id, |
| 312 ResourceContext* context); |
| 313 |
| 314 // Helper function for initiating a URL request. The |is_download| and |
| 315 // |is_content_initiated and |do_not_prompt_for_login| parameters are |
| 316 // specific to download requests. |
| 317 // TODO(ananta) |
| 318 // Look into a better way of passing these parameters in. |
| 319 // Please note that the InitializeURLRequest() function needs to be called |
| 320 // called to initialize the request before calling this function. |
| 321 void BeginURLRequest(std::unique_ptr<net::URLRequest> request, |
| 322 std::unique_ptr<ResourceHandler> handler, |
| 323 bool is_download, |
| 324 bool is_content_initiated, |
| 325 bool do_not_prompt_for_login, |
| 326 ResourceContext* context); |
| 327 |
| 328 bool is_shutdown() const { return is_shutdown_; } |
| 329 |
312 private: | 330 private: |
313 friend class ResourceDispatcherHostTest; | 331 friend class ResourceDispatcherHostTest; |
314 | 332 |
315 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, | 333 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, |
316 TestBlockedRequestsProcessDies); | 334 TestBlockedRequestsProcessDies); |
317 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, | 335 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, |
318 CalculateApproximateMemoryCost); | 336 CalculateApproximateMemoryCost); |
319 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, | 337 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, |
320 DetachableResourceTimesOut); | 338 DetachableResourceTimesOut); |
321 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, | 339 FRIEND_TEST_ALL_PREFIXES(ResourceDispatcherHostTest, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 void DidFinishLoading(ResourceLoader* loader) override; | 389 void DidFinishLoading(ResourceLoader* loader) override; |
372 std::unique_ptr<net::ClientCertStore> CreateClientCertStore( | 390 std::unique_ptr<net::ClientCertStore> CreateClientCertStore( |
373 ResourceLoader* loader) override; | 391 ResourceLoader* loader) override; |
374 | 392 |
375 // An init helper that runs on the IO thread. | 393 // An init helper that runs on the IO thread. |
376 void OnInit(); | 394 void OnInit(); |
377 | 395 |
378 // A shutdown helper that runs on the IO thread. | 396 // A shutdown helper that runs on the IO thread. |
379 void OnShutdown(); | 397 void OnShutdown(); |
380 | 398 |
381 // Helper function for regular and download requests. | 399 // Helper function for URL requests. |
382 void BeginRequestInternal(std::unique_ptr<net::URLRequest> request, | 400 void BeginRequestInternal(std::unique_ptr<net::URLRequest> request, |
383 std::unique_ptr<ResourceHandler> handler); | 401 std::unique_ptr<ResourceHandler> handler); |
384 | 402 |
385 void StartLoading(ResourceRequestInfoImpl* info, | 403 void StartLoading(ResourceRequestInfoImpl* info, |
386 std::unique_ptr<ResourceLoader> loader); | 404 std::unique_ptr<ResourceLoader> loader); |
387 | 405 |
388 // We keep track of how much memory each request needs and how many requests | 406 // We keep track of how much memory each request needs and how many requests |
389 // are issued by each renderer. These are known as OustandingRequestStats. | 407 // are issued by each renderer. These are known as OustandingRequestStats. |
390 // Memory limits apply to all requests sent to us by the renderers. There is a | 408 // Memory limits apply to all requests sent to us by the renderers. There is a |
391 // limit for each renderer. File descriptor limits apply to requests that are | 409 // limit for each renderer. File descriptor limits apply to requests that are |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
608 // ResourceDispatcherHostImpl should service this request. A request might | 626 // ResourceDispatcherHostImpl should service this request. A request might |
609 // be disallowed if the renderer is not authorized to retrieve the request | 627 // be disallowed if the renderer is not authorized to retrieve the request |
610 // URL or if the renderer is attempting to upload an unauthorized file. | 628 // URL or if the renderer is attempting to upload an unauthorized file. |
611 bool ShouldServiceRequest(int process_type, | 629 bool ShouldServiceRequest(int process_type, |
612 int child_id, | 630 int child_id, |
613 const ResourceRequest& request_data, | 631 const ResourceRequest& request_data, |
614 const net::HttpRequestHeaders& headers, | 632 const net::HttpRequestHeaders& headers, |
615 ResourceMessageFilter* filter, | 633 ResourceMessageFilter* filter, |
616 ResourceContext* resource_context); | 634 ResourceContext* resource_context); |
617 | 635 |
| 636 // Notifies the ResourceDispatcherHostDelegate about a download having |
| 637 // started. The function returns the |handler| passed in, if the download |
| 638 // is not throttled. If the download is to be throttled (Decided by the |
| 639 // delegate) the function returns a ThrottlingResourceHandler to handle the |
| 640 // download. |
| 641 std::unique_ptr<ResourceHandler> HandleDownloadStarted( |
| 642 net::URLRequest* request, |
| 643 std::unique_ptr<ResourceHandler> handler, |
| 644 bool is_content_initiated, |
| 645 bool must_download); |
| 646 |
618 LoaderMap pending_loaders_; | 647 LoaderMap pending_loaders_; |
619 | 648 |
620 // Collection of temp files downloaded for child processes via | 649 // Collection of temp files downloaded for child processes via |
621 // the download_to_file mechanism. We avoid deleting them until | 650 // the download_to_file mechanism. We avoid deleting them until |
622 // the client no longer needs them. | 651 // the client no longer needs them. |
623 typedef std::map<int, scoped_refptr<storage::ShareableFileReference> > | 652 typedef std::map<int, scoped_refptr<storage::ShareableFileReference> > |
624 DeletableFilesMap; // key is request id | 653 DeletableFilesMap; // key is request id |
625 typedef std::map<int, DeletableFilesMap> | 654 typedef std::map<int, DeletableFilesMap> |
626 RegisteredTempFiles; // key is child process id | 655 RegisteredTempFiles; // key is child process id |
627 RegisteredTempFiles registered_temp_files_; | 656 RegisteredTempFiles registered_temp_files_; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
702 | 731 |
703 std::unique_ptr<ResourceScheduler> scheduler_; | 732 std::unique_ptr<ResourceScheduler> scheduler_; |
704 | 733 |
705 // Allows tests to use a mock CertStore. If set, the CertStore must | 734 // Allows tests to use a mock CertStore. If set, the CertStore must |
706 // outlive this ResourceDispatcherHostImpl. | 735 // outlive this ResourceDispatcherHostImpl. |
707 CertStore* cert_store_for_testing_; | 736 CertStore* cert_store_for_testing_; |
708 | 737 |
709 // Used to invoke an interceptor for the HTTP header. | 738 // Used to invoke an interceptor for the HTTP header. |
710 HeaderInterceptorMap http_header_interceptor_map_; | 739 HeaderInterceptorMap http_header_interceptor_map_; |
711 | 740 |
| 741 // Points to the registered download handler intercept. |
| 742 CreateDownloadHandlerIntercept create_download_handler_intercept_; |
| 743 |
712 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcherHostImpl); | 744 DISALLOW_COPY_AND_ASSIGN(ResourceDispatcherHostImpl); |
713 }; | 745 }; |
714 | 746 |
715 } // namespace content | 747 } // namespace content |
716 | 748 |
717 #endif // CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_ | 749 #endif // CONTENT_BROWSER_LOADER_RESOURCE_DISPATCHER_HOST_IMPL_H_ |
OLD | NEW |