diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-07 21:33:58 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-07 21:33:58 +0000 |
commit | 2509af1d899d53267046c4f0518bd80173c5da33 (patch) | |
tree | 9cb44b1485d5928adc8487675664a3c258795224 /chrome/browser/cancelable_request.h | |
parent | 3317e2755360c9c36258d45d48c502ffe79e45ba (diff) | |
download | chromium_src-2509af1d899d53267046c4f0518bd80173c5da33.zip chromium_src-2509af1d899d53267046c4f0518bd80173c5da33.tar.gz chromium_src-2509af1d899d53267046c4f0518bd80173c5da33.tar.bz2 |
Adds the following to CancelableRequestConsumer:
GetClientDataForCurrentRequest: The particular use case I need this
for is that I'm using the same CancelableRequestConsumer for multiple
sources so that I don't know whic CancelableRequestProvider to pass in
to GetClientData. None-the-less it seems a lot easier to use this when
always get the provider and handle.
GetFirstHandleForClientData: For this one I know I want to cancel a
request for a given client data but don't know the associated handle.
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/3311012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@58759 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cancelable_request.h')
-rw-r--r-- | chrome/browser/cancelable_request.h | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/chrome/browser/cancelable_request.h b/chrome/browser/cancelable_request.h index 1365a26..d793398 100644 --- a/chrome/browser/cancelable_request.h +++ b/chrome/browser/cancelable_request.h @@ -176,6 +176,7 @@ class CancelableRequestProvider { // Base class used to notify of new requests. class CancelableRequestConsumerBase { protected: + friend class CancelableRequestBase; friend class CancelableRequestProvider; virtual ~CancelableRequestConsumerBase() { @@ -191,6 +192,14 @@ class CancelableRequestConsumerBase { // given request, and by the provider when a request is canceled. virtual void OnRequestRemoved(CancelableRequestProvider* provider, CancelableRequestProvider::Handle handle) = 0; + + // Sent to provider before executing a callback. + virtual void WillExecute(CancelableRequestProvider* provider, + CancelableRequestProvider::Handle handle) = 0; + + // Sent after executing a callback. + virtual void DidExecute(CancelableRequestProvider* provider, + CancelableRequestProvider::Handle handle) = 0; }; // Template for clients to use. It allows them to associate random "client @@ -232,6 +241,13 @@ class CancelableRequestConsumerTSimple : public CancelableRequestConsumerBase { return pending_requests_[request]; } + // Returns the data associated with the current request being processed. This + // is only valid during the time a callback is being processed. + T GetClientDataForCurrentRequest() { + DCHECK(current_request_.is_valid()); + return GetClientData(current_request_.provider, current_request_.handle); + } + // Returns true if there are any pending requests. bool HasPendingRequests() const { return !pending_requests_.empty(); @@ -254,6 +270,22 @@ class CancelableRequestConsumerTSimple : public CancelableRequestConsumerBase { DCHECK(pending_requests_.empty()); } + // Returns the handle for the first request that has the specified client data + // (in |handle|). Returns true if there is a request for the specified client + // data, false otherwise. + bool GetFirstHandleForClientData(T client_data, + CancelableRequestProvider::Handle* handle) { + for (typename PendingRequestList::const_iterator i = + pending_requests_.begin(); i != pending_requests_.end(); ++i) { + if (i->second == client_data) { + *handle = i->first.handle; + return true; + } + } + *handle = 0; + return false; + } + // Gets the client data for all pending requests. void GetAllClientData(std::vector<T>* data) { DCHECK(data); @@ -269,6 +301,8 @@ class CancelableRequestConsumerTSimple : public CancelableRequestConsumerBase { : provider(p), handle(h) { } + PendingRequest() : provider(NULL), handle(0) {} + // Comparison operator for stl. bool operator<(const PendingRequest& other) const { if (provider != other.provider) @@ -276,6 +310,8 @@ class CancelableRequestConsumerTSimple : public CancelableRequestConsumerBase { return handle < other.handle; } + bool is_valid() const { return provider != NULL; } + CancelableRequestProvider* provider; CancelableRequestProvider::Handle handle; }; @@ -304,8 +340,22 @@ class CancelableRequestConsumerTSimple : public CancelableRequestConsumerBase { pending_requests_.erase(i); } + virtual void WillExecute(CancelableRequestProvider* provider, + CancelableRequestProvider::Handle handle) { + current_request_ = PendingRequest(provider, handle); + } + + virtual void DidExecute(CancelableRequestProvider* provider, + CancelableRequestProvider::Handle handle) { + current_request_ = PendingRequest(); + } + // Lists all outstanding requests. PendingRequestList pending_requests_; + + // This is valid while processing a request and is used to identify the + // provider/handle of request. + PendingRequest current_request_; }; // See CancelableRequestConsumerTSimple. The default value for T @@ -414,6 +464,12 @@ class CancelableRequestBase // consumer. void NotifyCompleted() const { provider_->RequestCompleted(handle()); + consumer_->DidExecute(provider_, handle_); + } + + // Cover method for CancelableRequestConsumerBase::WillExecute. + void WillExecute() { + consumer_->WillExecute(provider_, handle_); } // The message loop that this request was created on. The callback will @@ -521,6 +577,8 @@ class CancelableRequest : public CancelableRequestBase { // request has been completed. This must be called on the callback_thread_. void ExecuteCallback(const TupleType& param) { if (!canceled_.IsSet()) { + WillExecute(); + // Execute the callback. callback_->RunWithParams(param); |