Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(954)

Unified Diff: content/browser/loader/resource_handler.h

Issue 2526983002: Refactor ResourceHandler API. (Closed)
Patch Set: Response to comments Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/loader/resource_handler.h
diff --git a/content/browser/loader/resource_handler.h b/content/browser/loader/resource_handler.h
index a6cc6822c691fc0ae706d25ae566b7bc65fb4655..b4cdfc28e6bf751b1834bdc52e01e217d8da0102 100644
--- a/content/browser/loader/resource_handler.h
+++ b/content/browser/loader/resource_handler.h
@@ -12,12 +12,14 @@
#ifndef CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_
#define CONTENT_BROWSER_LOADER_RESOURCE_HANDLER_H_
+#include <memory>
#include <string>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/threading/non_thread_safe.h"
+#include "content/browser/loader/resource_controller.h"
#include "content/common/content_export.h"
class GURL;
@@ -30,7 +32,6 @@ struct RedirectInfo;
} // namespace net
namespace content {
-class ResourceController;
class ResourceMessageFilter;
class ResourceRequestInfoImpl;
struct ResourceResponse;
@@ -38,35 +39,63 @@ struct ResourceResponse;
// The resource dispatcher host uses this interface to process network events
// for an URLRequest instance. A ResourceHandler's lifetime is bound to its
// associated URLRequest.
+//
+// No ResourceHandler method will ever be called synchronously when it calls
+// into the ResourceController passed in to it, either to resume or cancel the
+// request.
class CONTENT_EXPORT ResourceHandler
: public NON_EXPORTED_BASE(base::NonThreadSafe) {
public:
virtual ~ResourceHandler();
- // Sets the controller for this handler.
- virtual void SetController(ResourceController* controller);
-
- // The request was redirected to a new URL. |*defer| has an initial value of
- // false. Set |*defer| to true to defer the redirect. The redirect may be
- // followed later on via ResourceDispatcherHost::FollowDeferredRedirect. If
- // the handler returns false, then the request is cancelled.
- virtual bool OnRequestRedirected(const net::RedirectInfo& redirect_info,
- ResourceResponse* response,
- bool* defer) = 0;
-
- // Response headers and meta data are available. If the handler returns
- // false, then the request is cancelled. Set |*defer| to true to defer
- // processing of the response. Call ResourceDispatcherHostImpl::
- // ResumeDeferredRequest to continue processing the response.
- virtual bool OnResponseStarted(ResourceResponse* response, bool* defer) = 0;
+ // Used to allow a ResourceHandler to cancel the request out of band, when it
+ // may not have a ResourceController.
+ class CONTENT_EXPORT Delegate {
+ protected:
+ Delegate();
+ virtual ~Delegate();
+
+ private:
+ friend class ResourceHandler;
+
+ // Cancels the request when the class does not currently have ownership of
+ // the ResourceController.
+ // |error_code| indicates the reason for the cancellation, and
+ // |tell_renderer| whether the renderer needs to be informed of the
+ // cancellation.
+ virtual void OutOfBandCancel(int error_code, bool tell_renderer) = 0;
+
+ DISALLOW_COPY_AND_ASSIGN(Delegate);
+ };
+
+ // 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
+ // cancellation.
+ virtual void SetDelegate(Delegate* delegate);
+
+ // The request was redirected to a new URL. The request will not continue
+ // until one of |controller|'s resume or cancellation methods is invoked.
+ // |response| may be destroyed as soon as the method returns, so if the
+ // ResourceHandler wants to continue to use it, it must maintain a reference
+ // to it.
+ virtual void OnRequestRedirected(
+ const net::RedirectInfo& redirect_info,
+ ResourceResponse* response,
+ std::unique_ptr<ResourceController> controller) = 0;
+
+ // Response headers and metadata are available. The request will not continue
+ // until one of |controller|'s resume or cancellation methods is invoked.
+ // |response| may be destroyed as soon as the method returns, so if the
+ // ResourceHandler wants to continue to use it, it must maintain a reference
+ // to it.
+ virtual void OnResponseStarted(
+ ResourceResponse* response,
+ std::unique_ptr<ResourceController> controller) = 0;
// Called before the net::URLRequest (whose url is |url|) is to be started.
- // If the handler returns false, then the request is cancelled. Otherwise if
- // the return value is true, the ResourceHandler can delay the request from
- // starting by setting |*defer = true|. A deferred request will not have
- // called net::URLRequest::Start(), and will not resume until someone calls
- // ResourceDispatcherHost::StartDeferredRequest().
- virtual bool OnWillStart(const GURL& url, bool* defer) = 0;
+ // The request will not continue until one of |controller|'s resume or
+ // cancellation methods is invoked.
+ virtual void OnWillStart(const GURL& url,
+ std::unique_ptr<ResourceController> controller) = 0;
// Data will be read for the response. Upon success, this method places the
// size and address of the buffer where the data is to be written in its
@@ -78,22 +107,26 @@ class CONTENT_EXPORT ResourceHandler
//
// If the handler returns false, then the request is cancelled. Otherwise,
// once data is available, OnReadCompleted will be called.
+ // TODO(mmenke): Make this method use a ResourceController, and allow it to
+ // succeed asynchronously.
virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
int* buf_size,
int min_size) = 0;
// Data (*bytes_read bytes) was written into the buffer provided by
- // OnWillRead. A return value of false cancels the request, true continues
- // reading data. Set |*defer| to true to defer reading more response data.
- // Call controller()->Resume() to continue reading response data. A zero
- // |bytes_read| signals that no further data is available.
- virtual bool OnReadCompleted(int bytes_read, bool* defer) = 0;
-
- // The response is complete. The final response status is given. Set
- // |*defer| to true to defer destruction to a later time. Otherwise, the
- // request will be destroyed upon return.
- virtual void OnResponseCompleted(const net::URLRequestStatus& status,
- bool* defer) = 0;
+ // OnWillRead. The request will not continue until one of |controller|'s
+ // resume or cancellation methods is invoked. A zero |bytes_read| signals
+ // that no further data will be received.
+ virtual void OnReadCompleted(
+ int bytes_read,
+ std::unique_ptr<ResourceController> controller) = 0;
+
+ // The response is complete. The final response status is given. The request
+ // will not be deleted until controller's Resume() method is invoked. It is
+ // illegal to use its cancellation methods.
+ virtual void OnResponseCompleted(
+ const net::URLRequestStatus& status,
+ std::unique_ptr<ResourceController> controller) = 0;
// This notification is synthesized by the RedirectToFileResourceHandler
// to indicate progress of 'download_to_file' requests. OnReadCompleted
@@ -102,9 +135,34 @@ class CONTENT_EXPORT ResourceHandler
virtual void OnDataDownloaded(int bytes_downloaded) = 0;
protected:
- ResourceHandler(net::URLRequest* request);
+ explicit ResourceHandler(net::URLRequest* request);
+
+ // Convenience methods for managing a ResourceHandler's controller in the
+ // async completion case. These ensure that the controller is nullptr after
+ // being invoked, which allows for DCHECKing on it and better crashes on
+ // calling into deleted objects.
+
+ // Passes ownership of |controller| to |this|. Nothing is done with the
+ // |controller| automatically.
+ void HoldController(std::unique_ptr<ResourceController> controller);
+
+ // Returns ownership of the ResourceController previously passed in to
+ // HoldController.
+ std::unique_ptr<ResourceController> ReleaseController();
+
+ bool has_controller() const { return !!controller_; }
+
+ // These call the corresponding methods on the ResourceController previously
+ // passed to HoldController and then destroy it.
+ void Resume();
+ void Cancel();
+ void CancelAndIgnore();
+ void CancelWithError(int error_code);
+
+ // Cancels the request when the class does not currently have ownership of the
+ // ResourceController.
+ void OutOfBandCancel(int error_code, bool tell_renderer);
- ResourceController* controller() const { return controller_; }
net::URLRequest* request() const { return request_; }
// Convenience functions.
@@ -112,9 +170,12 @@ class CONTENT_EXPORT ResourceHandler
int GetRequestID() const;
ResourceMessageFilter* GetFilter() const;
+ Delegate* delegate() { return delegate_; }
+
private:
- ResourceController* controller_;
net::URLRequest* request_;
+ Delegate* delegate_;
+ std::unique_ptr<ResourceController> controller_;
DISALLOW_COPY_AND_ASSIGN(ResourceHandler);
};

Powered by Google App Engine
This is Rietveld 408576698