Chromium Code Reviews| 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" |
| 21 #include "content/common/content_export.h" | 22 #include "content/common/content_export.h" |
| 23 #include "content/public/browser/resource_controller.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. |
| 41 class CONTENT_EXPORT ResourceHandler | 42 class CONTENT_EXPORT ResourceHandler |
| 42 : public NON_EXPORTED_BASE(base::NonThreadSafe) { | 43 : public NON_EXPORTED_BASE(base::NonThreadSafe) { |
| 43 public: | 44 public: |
| 44 virtual ~ResourceHandler(); | 45 virtual ~ResourceHandler(); |
| 45 | 46 |
| 46 // Sets the controller for this handler. | 47 // The request was redirected to a new URL. The request will not continue |
| 47 virtual void SetController(ResourceController* controller); | 48 // until one of |controller|'s resume or cancellation methods is invoked, at |
| 49 // which point the request will be resumed synchronously. | |
|
Randy Smith (Not in Mondays)
2016/12/16 21:37:27
nit: Implies Cancel() will resume the request, whi
| |
| 50 virtual void OnRequestRedirected( | |
| 51 const net::RedirectInfo& redirect_info, | |
| 52 ResourceResponse* response, | |
| 53 std::unique_ptr<ResourceController> controller) = 0; | |
| 48 | 54 |
| 49 // The request was redirected to a new URL. |*defer| has an initial value of | 55 // Response headers and metadata are available. The request will not continue |
| 50 // false. Set |*defer| to true to defer the redirect. The redirect may be | 56 // until one of |controller|'s resume or cancellation methods is invoked, at |
| 51 // followed later on via ResourceDispatcherHost::FollowDeferredRedirect. If | 57 // which point the request will be resumed synchronously. |
| 52 // the handler returns false, then the request is cancelled. | 58 virtual void OnResponseStarted( |
| 53 virtual bool OnRequestRedirected(const net::RedirectInfo& redirect_info, | 59 ResourceResponse* response, |
| 54 ResourceResponse* response, | 60 std::unique_ptr<ResourceController> controller) = 0; |
| 55 bool* defer) = 0; | |
| 56 | |
| 57 // Response headers and meta data are available. If the handler returns | |
| 58 // false, then the request is cancelled. Set |*defer| to true to defer | |
| 59 // processing of the response. Call ResourceDispatcherHostImpl:: | |
| 60 // ResumeDeferredRequest to continue processing the response. | |
| 61 virtual bool OnResponseStarted(ResourceResponse* response, bool* defer) = 0; | |
| 62 | 61 |
| 63 // Called before the net::URLRequest (whose url is |url|) is to be started. | 62 // 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 | 63 // 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 | 64 // cancellation methods is invoked, at which point the request will be started |
| 66 // starting by setting |*defer = true|. A deferred request will not have | 65 // synchronously. |
| 67 // called net::URLRequest::Start(), and will not resume until someone calls | 66 virtual void OnWillStart(const GURL& url, |
| 68 // ResourceDispatcherHost::StartDeferredRequest(). | 67 std::unique_ptr<ResourceController> controller) = 0; |
| 69 virtual bool OnWillStart(const GURL& url, bool* defer) = 0; | |
| 70 | 68 |
| 71 // Data will be read for the response. Upon success, this method places the | 69 // 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 | 70 // 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 | 71 // out-params. This call will be followed by either OnReadCompleted (on |
| 74 // successful read or EOF) or OnResponseCompleted (on error). If | 72 // successful read or EOF) or OnResponseCompleted (on error). If |
| 75 // OnReadCompleted is called, the buffer may be recycled. Otherwise, it may | 73 // 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 | 74 // 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. | 75 // not -1, it is the minimum size of the returned buffer. |
| 78 // | 76 // |
| 79 // If the handler returns false, then the request is cancelled. Otherwise, | 77 // If the handler returns false, then the request is cancelled. Otherwise, |
| 80 // once data is available, OnReadCompleted will be called. | 78 // once data is available, OnReadCompleted will be called. |
| 79 // TODO(mmenke): Make this method use a ResourceController, and allow it to | |
| 80 // succeed asynchronously. | |
| 81 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 81 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 82 int* buf_size, | 82 int* buf_size, |
| 83 int min_size) = 0; | 83 int min_size) = 0; |
| 84 | 84 |
| 85 // Data (*bytes_read bytes) was written into the buffer provided by | 85 // Data (*bytes_read bytes) was written into the buffer provided by |
| 86 // OnWillRead. A return value of false cancels the request, true continues | 86 // OnWillRead. The request will not continue until one of |controller|'s |
| 87 // reading data. Set |*defer| to true to defer reading more response data. | 87 // resume or cancellation methods is invoked, at which point the request will |
| 88 // Call controller()->Resume() to continue reading response data. A zero | 88 // be resumed synchronously. A zero |bytes_read| signals that no further data |
| 89 // |bytes_read| signals that no further data is available. | 89 // is available. |
| 90 virtual bool OnReadCompleted(int bytes_read, bool* defer) = 0; | 90 virtual void OnReadCompleted( |
| 91 int bytes_read, | |
| 92 std::unique_ptr<ResourceController> controller) = 0; | |
| 91 | 93 |
| 92 // The response is complete. The final response status is given. Set | 94 // 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 | 95 // will not be deleted until controller's Resume() method is invoked. It is |
| 94 // request will be destroyed upon return. | 96 // illegal to use its cancellation methods. |
| 95 virtual void OnResponseCompleted(const net::URLRequestStatus& status, | 97 virtual void OnResponseCompleted( |
| 96 bool* defer) = 0; | 98 const net::URLRequestStatus& status, |
| 99 std::unique_ptr<ResourceController> controller) = 0; | |
| 97 | 100 |
| 98 // This notification is synthesized by the RedirectToFileResourceHandler | 101 // This notification is synthesized by the RedirectToFileResourceHandler |
| 99 // to indicate progress of 'download_to_file' requests. OnReadCompleted | 102 // to indicate progress of 'download_to_file' requests. OnReadCompleted |
| 100 // calls are consumed by the RedirectToFileResourceHandler and replaced | 103 // calls are consumed by the RedirectToFileResourceHandler and replaced |
| 101 // with OnDataDownloaded calls. | 104 // with OnDataDownloaded calls. |
| 102 virtual void OnDataDownloaded(int bytes_downloaded) = 0; | 105 virtual void OnDataDownloaded(int bytes_downloaded) = 0; |
| 103 | 106 |
| 104 protected: | 107 protected: |
| 105 ResourceHandler(net::URLRequest* request); | 108 explicit ResourceHandler(net::URLRequest* request); |
| 106 | 109 |
| 107 ResourceController* controller() const { return controller_; } | 110 // Utility methods for managing a ResourceHandler's controller in the async |
| 111 // completion case. These ensure that the controller is nullptr after being | |
| 112 // invoked, which allows for DCHECKing on it and better crashes on calling | |
| 113 // into deleting objects. | |
| 114 | |
| 115 void set_controller(std::unique_ptr<ResourceController> controller) { | |
| 116 controller_ = std::move(controller); | |
| 117 } | |
| 118 | |
| 119 bool has_controller() const { return !!controller_; } | |
| 120 | |
| 121 std::unique_ptr<ResourceController> TakeController(); | |
| 122 | |
| 123 // These call the corresponding methods on the previously set | |
| 124 // ResourceController, and then destroy the controller. | |
| 125 void Resume(); | |
| 126 void Cancel(); | |
| 127 void CancelAndIgnore(); | |
| 128 void CancelWithError(int error_code); | |
| 129 | |
| 108 net::URLRequest* request() const { return request_; } | 130 net::URLRequest* request() const { return request_; } |
| 109 | 131 |
| 110 // Convenience functions. | 132 // Convenience functions. |
| 111 ResourceRequestInfoImpl* GetRequestInfo() const; | 133 ResourceRequestInfoImpl* GetRequestInfo() const; |
| 112 int GetRequestID() const; | 134 int GetRequestID() const; |
| 113 ResourceMessageFilter* GetFilter() const; | 135 ResourceMessageFilter* GetFilter() const; |
| 114 | 136 |
| 137 // Cancels the request when the class does not currently have ownership of the | |
| 138 // ResourceController. | |
| 139 void OutOfBandCancel(); | |
| 140 | |
| 115 private: | 141 private: |
| 116 ResourceController* controller_; | |
| 117 net::URLRequest* request_; | 142 net::URLRequest* request_; |
| 143 std::unique_ptr<ResourceController> controller_; | |
| 118 | 144 |
| 119 DISALLOW_COPY_AND_ASSIGN(ResourceHandler); | 145 DISALLOW_COPY_AND_ASSIGN(ResourceHandler); |
| 120 }; | 146 }; |
| 121 | 147 |
| 122 } // namespace content | 148 } // namespace content |
| 123 | 149 |
| 124 #endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ | 150 #endif // CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_ |
| OLD | NEW |