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 will ever be called synchronously when it calls | |
44 // into the ResourceController passed in to it, either to resume or cancel the | |
45 // 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-bounds | |
Charlie Harrison
2017/01/27 22:48:04
out of band?
mmenke
2017/01/27 23:02:15
Done. Though worth noting that if it is cancelled
| |
72 // cancellation. | |
73 virtual void SetDelegate(Delegate* delegate); | |
74 | |
75 // The request was redirected to a new URL. The request will not continue | |
76 // until one of |controller|'s resume or cancellation methods is invoked. | |
77 // |response| may be destroyed as soon as the method returns, so if the | |
78 // ResourceHandler wants to continue to use it, it must maintain a reference | |
79 // to it. | |
80 virtual void OnRequestRedirected( | |
81 const net::RedirectInfo& redirect_info, | |
82 ResourceResponse* response, | |
83 std::unique_ptr<ResourceController> controller) = 0; | |
84 | |
85 // Response headers and metadata are available. The request will not continue | |
86 // until one of |controller|'s resume or cancellation methods is invoked. | |
87 // |response| may be destroyed as soon as the method returns, so if the | |
88 // ResourceHandler wants to continue to use it, it must maintain a reference | |
89 // to it. | |
90 virtual void OnResponseStarted( | |
91 ResourceResponse* response, | |
92 std::unique_ptr<ResourceController> controller) = 0; | |
62 | 93 |
63 // Called before the net::URLRequest (whose url is |url|) is to be started. | 94 // 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 | 95 // 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 | 96 // cancellation methods is invoked. |
66 // starting by setting |*defer = true|. A deferred request will not have | 97 virtual void OnWillStart(const GURL& url, |
67 // called net::URLRequest::Start(), and will not resume until someone calls | 98 std::unique_ptr<ResourceController> controller) = 0; |
68 // ResourceDispatcherHost::StartDeferredRequest(). | |
69 virtual bool OnWillStart(const GURL& url, bool* defer) = 0; | |
70 | 99 |
71 // Data will be read for the response. Upon success, this method places the | 100 // 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 | 101 // 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 | 102 // out-params. This call will be followed by either OnReadCompleted (on |
74 // successful read or EOF) or OnResponseCompleted (on error). If | 103 // successful read or EOF) or OnResponseCompleted (on error). If |
75 // OnReadCompleted is called, the buffer may be recycled. Otherwise, it may | 104 // 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 | 105 // 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. | 106 // not -1, it is the minimum size of the returned buffer. |
78 // | 107 // |
79 // If the handler returns false, then the request is cancelled. Otherwise, | 108 // If the handler returns false, then the request is cancelled. Otherwise, |
80 // once data is available, OnReadCompleted will be called. | 109 // once data is available, OnReadCompleted will be called. |
110 // TODO(mmenke): Make this method use a ResourceController, and allow it to | |
111 // succeed asynchronously. | |
81 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 112 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
82 int* buf_size, | 113 int* buf_size, |
83 int min_size) = 0; | 114 int min_size) = 0; |
84 | 115 |
85 // Data (*bytes_read bytes) was written into the buffer provided by | 116 // Data (*bytes_read bytes) was written into the buffer provided by |
86 // OnWillRead. A return value of false cancels the request, true continues | 117 // OnWillRead. The request will not continue until one of |controller|'s |
87 // reading data. Set |*defer| to true to defer reading more response data. | 118 // resume or cancellation methods is invoked. A zero |bytes_read| signals |
88 // Call controller()->Resume() to continue reading response data. A zero | 119 // that no further data will be received. |
89 // |bytes_read| signals that no further data is available. | 120 virtual void OnReadCompleted( |
90 virtual bool OnReadCompleted(int bytes_read, bool* defer) = 0; | 121 int bytes_read, |
122 std::unique_ptr<ResourceController> controller) = 0; | |
91 | 123 |
92 // The response is complete. The final response status is given. Set | 124 // 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 | 125 // will not be deleted until controller's Resume() method is invoked. It is |
94 // request will be destroyed upon return. | 126 // illegal to use its cancellation methods. |
95 virtual void OnResponseCompleted(const net::URLRequestStatus& status, | 127 virtual void OnResponseCompleted( |
96 bool* defer) = 0; | 128 const net::URLRequestStatus& status, |
129 std::unique_ptr<ResourceController> controller) = 0; | |
97 | 130 |
98 // This notification is synthesized by the RedirectToFileResourceHandler | 131 // This notification is synthesized by the RedirectToFileResourceHandler |
99 // to indicate progress of 'download_to_file' requests. OnReadCompleted | 132 // to indicate progress of 'download_to_file' requests. OnReadCompleted |
100 // calls are consumed by the RedirectToFileResourceHandler and replaced | 133 // calls are consumed by the RedirectToFileResourceHandler and replaced |
101 // with OnDataDownloaded calls. | 134 // with OnDataDownloaded calls. |
102 virtual void OnDataDownloaded(int bytes_downloaded) = 0; | 135 virtual void OnDataDownloaded(int bytes_downloaded) = 0; |
103 | 136 |
104 protected: | 137 protected: |
105 ResourceHandler(net::URLRequest* request); | 138 explicit ResourceHandler(net::URLRequest* request); |
106 | 139 |
107 ResourceController* controller() const { return controller_; } | 140 // Convenience methods for managing a ResourceHandler's controller in the |
141 // async completion case. These ensure that the controller is nullptr after | |
142 // being invoked, which allows for DCHECKing on it and better crashes on | |
143 // calling into deleted objects. | |
144 | |
145 // Passes ownership of |controller| to |this|. Nothing is done with the | |
146 // |controller| automatically. | |
147 void HoldController(std::unique_ptr<ResourceController> controller); | |
148 | |
149 // Returns ownership of the ResourceController previously passed in to | |
150 // HoldController. | |
151 std::unique_ptr<ResourceController> ReleaseController(); | |
152 | |
153 bool has_controller() const { return !!controller_; } | |
154 | |
155 // These call the corresponding methods on the ResourceController previously | |
156 // passed to HoldController and then destroy it. | |
157 void Resume(); | |
158 void Cancel(); | |
159 void CancelAndIgnore(); | |
160 void CancelWithError(int error_code); | |
161 | |
162 // Cancels the request when the class does not currently have ownership of the | |
163 // ResourceController. | |
164 void OutOfBandCancel(int error_code, bool tell_renderer); | |
165 | |
108 net::URLRequest* request() const { return request_; } | 166 net::URLRequest* request() const { return request_; } |
109 | 167 |
110 // Convenience functions. | 168 // Convenience functions. |
111 ResourceRequestInfoImpl* GetRequestInfo() const; | 169 ResourceRequestInfoImpl* GetRequestInfo() const; |
112 int GetRequestID() const; | 170 int GetRequestID() const; |
113 ResourceMessageFilter* GetFilter() const; | 171 ResourceMessageFilter* GetFilter() const; |
114 | 172 |
173 Delegate* delegate() { return delegate_; } | |
174 | |
115 private: | 175 private: |
116 ResourceController* controller_; | |
117 net::URLRequest* request_; | 176 net::URLRequest* request_; |
177 Delegate* delegate_; | |
178 std::unique_ptr<ResourceController> controller_; | |
118 | 179 |
119 DISALLOW_COPY_AND_ASSIGN(ResourceHandler); | 180 DISALLOW_COPY_AND_ASSIGN(ResourceHandler); |
120 }; | 181 }; |
121 | 182 |
122 } // namespace content | 183 } // namespace content |
123 | 184 |
124 #endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ | 185 #endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ |
OLD | NEW |