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 RenderProcessHosts, and dispatches them to URLRequests. It then | 6 // from the RenderProcessHosts, and dispatches them to URLRequests. It then |
7 // fowards the messages from the URLRequests back to the correct process for | 7 // fowards the messages from the URLRequests back to the correct process for |
8 // handling. | 8 // 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_HANDLER_H_ | 12 #ifndef CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ |
13 #define CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ | 13 #define CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ |
14 | 14 |
| 15 #include <memory> |
15 #include <string> | 16 #include <string> |
16 | 17 |
17 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
18 #include "base/macros.h" | 19 #include "base/macros.h" |
19 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
20 #include "base/threading/non_thread_safe.h" | 21 #include "base/threading/non_thread_safe.h" |
| 22 #include "content/browser/loader/resource_controller.h" |
21 #include "content/common/content_export.h" | 23 #include "content/common/content_export.h" |
22 | 24 |
23 class GURL; | 25 class GURL; |
24 | 26 |
25 namespace net { | 27 namespace net { |
26 class IOBuffer; | 28 class IOBuffer; |
27 class URLRequest; | 29 class URLRequest; |
28 class URLRequestStatus; | 30 class URLRequestStatus; |
29 struct RedirectInfo; | 31 struct RedirectInfo; |
30 } // namespace net | 32 } // namespace net |
31 | 33 |
32 namespace content { | 34 namespace content { |
33 class ResourceController; | |
34 class ResourceMessageFilter; | 35 class ResourceMessageFilter; |
35 class ResourceRequestInfoImpl; | 36 class ResourceRequestInfoImpl; |
36 struct ResourceResponse; | 37 struct ResourceResponse; |
37 | 38 |
38 // The resource dispatcher host uses this interface to process network events | 39 // The resource dispatcher host uses this interface to process network events |
39 // for an URLRequest instance. A ResourceHandler's lifetime is bound to its | 40 // for an URLRequest instance. A ResourceHandler's lifetime is bound to its |
40 // associated URLRequest. | 41 // associated URLRequest. |
| 42 // |
| 43 // No ResourceHandler method other than OnWillRead will ever be called |
| 44 // synchronously when it calls into the ResourceController passed in to it, |
| 45 // either to resume or cancel the request. |
41 class CONTENT_EXPORT ResourceHandler | 46 class CONTENT_EXPORT ResourceHandler |
42 : public NON_EXPORTED_BASE(base::NonThreadSafe) { | 47 : public NON_EXPORTED_BASE(base::NonThreadSafe) { |
43 public: | 48 public: |
44 virtual ~ResourceHandler(); | 49 virtual ~ResourceHandler(); |
45 | 50 |
46 // Sets the controller for this handler. | 51 // Used to allow a ResourceHandler to cancel the request out of band, when it |
47 virtual void SetController(ResourceController* controller); | 52 // may not have a ResourceController. |
| 53 class CONTENT_EXPORT Delegate { |
| 54 protected: |
| 55 Delegate(); |
| 56 virtual ~Delegate(); |
48 | 57 |
49 // The request was redirected to a new URL. |*defer| has an initial value of | 58 private: |
50 // false. Set |*defer| to true to defer the redirect. The redirect may be | 59 friend class ResourceHandler; |
51 // followed later on via ResourceDispatcherHost::FollowDeferredRedirect. If | |
52 // the handler returns false, then the request is cancelled. | |
53 virtual bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | |
54 ResourceResponse* response, | |
55 bool* defer) = 0; | |
56 | 60 |
57 // Response headers and meta data are available. If the handler returns | 61 // Cancels the request when the class does not currently have ownership of |
58 // false, then the request is cancelled. Set |*defer| to true to defer | 62 // the ResourceController. |
59 // processing of the response. Call ResourceDispatcherHostImpl:: | 63 // |error_code| indicates the reason for the cancellation, and |
60 // ResumeDeferredRequest to continue processing the response. | 64 // |tell_renderer| whether the renderer needs to be informed of the |
61 virtual bool OnResponseStarted(ResourceResponse* response, bool* defer) = 0; | 65 // cancellation. |
| 66 virtual void OutOfBandCancel(int error_code, bool tell_renderer) = 0; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 69 }; |
| 70 |
| 71 // Sets the ResourceHandler::Delegate, which handles out-of-band cancellation. |
| 72 virtual void SetDelegate(Delegate* delegate); |
| 73 |
| 74 // The request was redirected to a new URL. The request will not continue |
| 75 // until one of |controller|'s resume or cancellation methods is invoked. |
| 76 // |response| may be destroyed as soon as the method returns, so if the |
| 77 // ResourceHandler wants to continue to use it, it must maintain a reference |
| 78 // to it. |
| 79 virtual void OnRequestRedirected( |
| 80 const net::RedirectInfo& redirect_info, |
| 81 ResourceResponse* response, |
| 82 std::unique_ptr<ResourceController> controller) = 0; |
| 83 |
| 84 // Response headers and metadata are available. The request will not continue |
| 85 // until one of |controller|'s resume or cancellation methods is invoked. |
| 86 // |response| may be destroyed as soon as the method returns, so if the |
| 87 // ResourceHandler wants to continue to use it, it must maintain a reference |
| 88 // to it. |
| 89 virtual void OnResponseStarted( |
| 90 ResourceResponse* response, |
| 91 std::unique_ptr<ResourceController> controller) = 0; |
62 | 92 |
63 // Called before the net::URLRequest (whose url is |url|) is to be started. | 93 // Called before the net::URLRequest (whose url is |url|) is to be started. |
64 // If the handler returns false, then the request is cancelled. Otherwise if | 94 // The request will not continue until one of |controller|'s resume or |
65 // the return value is true, the ResourceHandler can delay the request from | 95 // cancellation methods is invoked. |
66 // starting by setting |*defer = true|. A deferred request will not have | 96 virtual void OnWillStart(const GURL& url, |
67 // called net::URLRequest::Start(), and will not resume until someone calls | 97 std::unique_ptr<ResourceController> controller) = 0; |
68 // ResourceDispatcherHost::StartDeferredRequest(). | |
69 virtual bool OnWillStart(const GURL& url, bool* defer) = 0; | |
70 | 98 |
71 // Data will be read for the response. Upon success, this method places the | 99 // Data will be read for the response. Upon success, this method places the |
72 // size and address of the buffer where the data is to be written in its | 100 // size and address of the buffer where the data is to be written in its |
73 // out-params. This call will be followed by either OnReadCompleted (on | 101 // out-params. This call will be followed by either OnReadCompleted (on |
74 // successful read or EOF) or OnResponseCompleted (on error). If | 102 // successful read or EOF) or OnResponseCompleted (on error). If |
75 // OnReadCompleted is called, the buffer may be recycled. Otherwise, it may | 103 // OnReadCompleted is called, the buffer may be recycled. Otherwise, it may |
76 // not be recycled and may potentially outlive the handler. If |min_size| is | 104 // not be recycled and may potentially outlive the handler. If |min_size| is |
77 // not -1, it is the minimum size of the returned buffer. | 105 // not -1, it is the minimum size of the returned buffer. |
78 // | 106 // |
| 107 // Unlike other methods, may be called synchronously on Resume, for |
| 108 // performance reasons. |
| 109 // |
79 // If the handler returns false, then the request is cancelled. Otherwise, | 110 // If the handler returns false, then the request is cancelled. Otherwise, |
80 // once data is available, OnReadCompleted will be called. | 111 // once data is available, OnReadCompleted will be called. |
| 112 // TODO(mmenke): Make this method use a ResourceController, and allow it to |
| 113 // succeed asynchronously. |
81 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 114 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
82 int* buf_size, | 115 int* buf_size, |
83 int min_size) = 0; | 116 int min_size) = 0; |
84 | 117 |
85 // Data (*bytes_read bytes) was written into the buffer provided by | 118 // Data (*bytes_read bytes) was written into the buffer provided by |
86 // OnWillRead. A return value of false cancels the request, true continues | 119 // OnWillRead. The request will not continue until one of |controller|'s |
87 // reading data. Set |*defer| to true to defer reading more response data. | 120 // resume or cancellation methods is invoked. A zero |bytes_read| signals |
88 // Call controller()->Resume() to continue reading response data. A zero | 121 // that no further data will be received. |
89 // |bytes_read| signals that no further data is available. | 122 virtual void OnReadCompleted( |
90 virtual bool OnReadCompleted(int bytes_read, bool* defer) = 0; | 123 int bytes_read, |
| 124 std::unique_ptr<ResourceController> controller) = 0; |
91 | 125 |
92 // The response is complete. The final response status is given. Set | 126 // The response is complete. The final response status is given. The request |
93 // |*defer| to true to defer destruction to a later time. Otherwise, the | 127 // will not be deleted until controller's Resume() method is invoked. It is |
94 // request will be destroyed upon return. | 128 // illegal to use its cancellation methods. |
95 virtual void OnResponseCompleted(const net::URLRequestStatus& status, | 129 virtual void OnResponseCompleted( |
96 bool* defer) = 0; | 130 const net::URLRequestStatus& status, |
| 131 std::unique_ptr<ResourceController> controller) = 0; |
97 | 132 |
98 // This notification is synthesized by the RedirectToFileResourceHandler | 133 // This notification is synthesized by the RedirectToFileResourceHandler |
99 // to indicate progress of 'download_to_file' requests. OnReadCompleted | 134 // to indicate progress of 'download_to_file' requests. OnReadCompleted |
100 // calls are consumed by the RedirectToFileResourceHandler and replaced | 135 // calls are consumed by the RedirectToFileResourceHandler and replaced |
101 // with OnDataDownloaded calls. | 136 // with OnDataDownloaded calls. |
102 virtual void OnDataDownloaded(int bytes_downloaded) = 0; | 137 virtual void OnDataDownloaded(int bytes_downloaded) = 0; |
103 | 138 |
104 protected: | 139 protected: |
105 ResourceHandler(net::URLRequest* request); | 140 explicit ResourceHandler(net::URLRequest* request); |
106 | 141 |
107 ResourceController* controller() const { return controller_; } | 142 // Convenience methods for managing a ResourceHandler's controller in the |
| 143 // async completion case. These ensure that the controller is nullptr after |
| 144 // being invoked, which allows for DCHECKing on it and better crashes on |
| 145 // calling into deleted objects. |
| 146 |
| 147 // Passes ownership of |controller| to |this|. Nothing is done with the |
| 148 // |controller| automatically. |
| 149 void HoldController(std::unique_ptr<ResourceController> controller); |
| 150 |
| 151 // Returns ownership of the ResourceController previously passed in to |
| 152 // HoldController. |
| 153 std::unique_ptr<ResourceController> ReleaseController(); |
| 154 |
| 155 bool has_controller() const { return !!controller_; } |
| 156 |
| 157 // These call the corresponding methods on the ResourceController previously |
| 158 // passed to HoldController and then destroy it. |
| 159 void Resume(); |
| 160 void Cancel(); |
| 161 void CancelAndIgnore(); |
| 162 void CancelWithError(int error_code); |
| 163 |
| 164 // Cancels the request when the class does not currently have ownership of the |
| 165 // ResourceController. |
| 166 void OutOfBandCancel(int error_code, bool tell_renderer); |
| 167 |
108 net::URLRequest* request() const { return request_; } | 168 net::URLRequest* request() const { return request_; } |
109 | 169 |
110 // Convenience functions. | 170 // Convenience functions. |
111 ResourceRequestInfoImpl* GetRequestInfo() const; | 171 ResourceRequestInfoImpl* GetRequestInfo() const; |
112 int GetRequestID() const; | 172 int GetRequestID() const; |
113 ResourceMessageFilter* GetFilter() const; | 173 ResourceMessageFilter* GetFilter() const; |
114 | 174 |
| 175 Delegate* delegate() { return delegate_; } |
| 176 |
115 private: | 177 private: |
116 ResourceController* controller_; | |
117 net::URLRequest* request_; | 178 net::URLRequest* request_; |
| 179 Delegate* delegate_; |
| 180 std::unique_ptr<ResourceController> controller_; |
118 | 181 |
119 DISALLOW_COPY_AND_ASSIGN(ResourceHandler); | 182 DISALLOW_COPY_AND_ASSIGN(ResourceHandler); |
120 }; | 183 }; |
121 | 184 |
122 } // namespace content | 185 } // namespace content |
123 | 186 |
124 #endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ | 187 #endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ |
OLD | NEW |