[email protected] | 62b4a96 | 2012-01-18 22:53:16 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 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 RenderProcessHosts, and dispatches them to URLRequests. It then |
| 7 | // fowards the messages from the URLRequests back to the correct process for |
| 8 | // handling. |
| 9 | // |
| 10 | // See http://dev.chromium.org/developers/design-documents/multi-process-resource-loading |
| 11 | |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 12 | #ifndef CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ |
| 13 | #define CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 14 | |
| 15 | #include <string> |
| 16 | |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
[email protected] | fb44196 | 2013-05-08 05:35:24 | [diff] [blame] | 18 | #include "base/sequenced_task_runner_helpers.h" |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 19 | #include "base/threading/non_thread_safe.h" |
[email protected] | 62b4a96 | 2012-01-18 22:53:16 | [diff] [blame] | 20 | #include "content/common/content_export.h" |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 21 | |
[email protected] | 2336ffe | 2011-11-24 01:23:34 | [diff] [blame] | 22 | class GURL; |
| 23 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 24 | namespace net { |
| 25 | class IOBuffer; |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 26 | class URLRequest; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 27 | class URLRequestStatus; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 28 | struct RedirectInfo; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 29 | } // namespace net |
| 30 | |
[email protected] | 42ac785 | 2012-06-01 06:38:48 | [diff] [blame] | 31 | namespace content { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 32 | class ResourceController; |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 33 | class ResourceMessageFilter; |
| 34 | class ResourceRequestInfoImpl; |
[email protected] | 42ac785 | 2012-06-01 06:38:48 | [diff] [blame] | 35 | struct ResourceResponse; |
| 36 | |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 37 | // The resource dispatcher host uses this interface to process network events |
| 38 | // for an URLRequest instance. A ResourceHandler's lifetime is bound to its |
| 39 | // associated URLRequest. |
[email protected] | 62b4a96 | 2012-01-18 22:53:16 | [diff] [blame] | 40 | class CONTENT_EXPORT ResourceHandler |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 41 | : public NON_EXPORTED_BASE(base::NonThreadSafe) { |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 42 | public: |
[email protected] | d651cbc | 2012-05-31 21:33:05 | [diff] [blame] | 43 | virtual ~ResourceHandler() {} |
| 44 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 45 | // Sets the controller for this handler. |
| 46 | virtual void SetController(ResourceController* controller); |
| 47 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 48 | // The request was redirected to a new URL. |*defer| has an initial value of |
| 49 | // false. Set |*defer| to true to defer the redirect. The redirect may be |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 50 | // followed later on via ResourceDispatcherHost::FollowDeferredRedirect. If |
| 51 | // the handler returns false, then the request is cancelled. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 52 | virtual bool OnRequestRedirected(const net::RedirectInfo& redirect_info, |
[email protected] | 42ac785 | 2012-06-01 06:38:48 | [diff] [blame] | 53 | ResourceResponse* response, |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 54 | bool* defer) = 0; |
| 55 | |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 56 | // Response headers and meta data are available. If the handler returns |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 57 | // false, then the request is cancelled. Set |*defer| to true to defer |
| 58 | // processing of the response. Call ResourceDispatcherHostImpl:: |
| 59 | // ResumeDeferredRequest to continue processing the response. |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 60 | virtual bool OnResponseStarted(ResourceResponse* response, bool* defer) = 0; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 61 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 62 | // Called before the net::URLRequest (whose url is |url|) is to be started. |
| 63 | // If the handler returns false, then the request is cancelled. Otherwise if |
| 64 | // the return value is true, the ResourceHandler can delay the request from |
| 65 | // starting by setting |*defer = true|. A deferred request will not have |
| 66 | // called net::URLRequest::Start(), and will not resume until someone calls |
| 67 | // ResourceDispatcherHost::StartDeferredRequest(). |
| 68 | virtual bool OnWillStart(const GURL& url, bool* defer) = 0; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 69 | |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 70 | // Called before the net::URLRequest (whose url is |url|} uses the network for |
| 71 | // the first time to load the resource. If the handler returns false, then the |
| 72 | // request is cancelled. Otherwise if the return value is true, the |
| 73 | // ResourceHandler can delay the request from starting by setting |*defer = |
| 74 | // true|. Call controller()->Resume() to continue if deferred. |
| 75 | virtual bool OnBeforeNetworkStart(const GURL& url, bool* defer) = 0; |
[email protected] | 5584eab | 2014-01-09 22:16:15 | [diff] [blame] | 76 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 77 | // Data will be read for the response. Upon success, this method places the |
| 78 | // size and address of the buffer where the data is to be written in its |
[email protected] | 698d13f | 2014-05-13 19:30:52 | [diff] [blame] | 79 | // out-params. This call will be followed by either OnReadCompleted (on |
| 80 | // successful read or EOF) or OnResponseCompleted (on error). If |
| 81 | // OnReadCompleted is called, the buffer may be recycled. Otherwise, it may |
| 82 | // not be recycled and may potentially outlive the handler. If |min_size| is |
| 83 | // not -1, it is the minimum size of the returned buffer. |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 84 | // |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 85 | // If the handler returns false, then the request is cancelled. Otherwise, |
| 86 | // once data is available, OnReadCompleted will be called. |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 87 | virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 88 | int* buf_size, |
| 89 | int min_size) = 0; |
| 90 | |
| 91 | // Data (*bytes_read bytes) was written into the buffer provided by |
[email protected] | 5e5e0c8 | 2012-01-25 09:12:29 | [diff] [blame] | 92 | // OnWillRead. A return value of false cancels the request, true continues |
[email protected] | 22b30544 | 2012-05-21 18:02:59 | [diff] [blame] | 93 | // reading data. Set |*defer| to true to defer reading more response data. |
[email protected] | 17b674e0 | 2014-05-10 04:30:02 | [diff] [blame] | 94 | // Call controller()->Resume() to continue reading response data. A zero |
| 95 | // |bytes_read| signals that no further data is available. |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 96 | virtual bool OnReadCompleted(int bytes_read, bool* defer) = 0; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 97 | |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 98 | // The response is complete. The final response status is given. Set |
| 99 | // |*defer| to true to defer destruction to a later time. Otherwise, the |
| 100 | // request will be destroyed upon return. |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 101 | virtual void OnResponseCompleted(const net::URLRequestStatus& status, |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 102 | const std::string& security_info, |
| 103 | bool* defer) = 0; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 104 | |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 105 | // This notification is synthesized by the RedirectToFileResourceHandler |
| 106 | // to indicate progress of 'download_to_file' requests. OnReadCompleted |
| 107 | // calls are consumed by the RedirectToFileResourceHandler and replaced |
| 108 | // with OnDataDownloaded calls. |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 109 | virtual void OnDataDownloaded(int bytes_downloaded) = 0; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 110 | |
| 111 | protected: |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 112 | ResourceHandler(net::URLRequest* request); |
| 113 | |
| 114 | ResourceController* controller() const { return controller_; } |
| 115 | net::URLRequest* request() const { return request_; } |
| 116 | |
| 117 | // Convenience functions. |
| 118 | ResourceRequestInfoImpl* GetRequestInfo() const; |
| 119 | int GetRequestID() const; |
| 120 | ResourceMessageFilter* GetFilter() const; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 121 | |
| 122 | private: |
| 123 | ResourceController* controller_; |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 124 | net::URLRequest* request_; |
[email protected] | 1d8a3d1f | 2011-02-19 07:11:52 | [diff] [blame] | 125 | }; |
| 126 | |
[email protected] | 42ac785 | 2012-06-01 06:38:48 | [diff] [blame] | 127 | } // namespace content |
| 128 | |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 129 | #endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ |