summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjoth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-23 10:27:36 +0000
committerjoth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-23 10:27:36 +0000
commit8e7e7838f96d9e60a9f51ab448ceb58727ef0cfb (patch)
treee430daa7aac940c0d221912e32d8a5df9129851b
parent795d701b3266d3156f33ca6ce7836ace6d128b59 (diff)
downloadchromium_src-8e7e7838f96d9e60a9f51ab448ceb58727ef0cfb.zip
chromium_src-8e7e7838f96d9e60a9f51ab448ceb58727ef0cfb.tar.gz
chromium_src-8e7e7838f96d9e60a9f51ab448ceb58727ef0cfb.tar.bz2
Improve example code for cancelable request -- pass consumer base class pointer in StartRequest as it is more general
Make a small optimization: passing an iterator in CancelRequestLocked rather than Handle can avoid a call to map::find() BUG=none TEST=none Review URL: http://codereview.chromium.org/650143 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@39708 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/cancelable_request.cc17
-rw-r--r--chrome/browser/cancelable_request.h10
2 files changed, 14 insertions, 13 deletions
diff --git a/chrome/browser/cancelable_request.cc b/chrome/browser/cancelable_request.cc
index 1d20eca..85ab51a 100644
--- a/chrome/browser/cancelable_request.cc
+++ b/chrome/browser/cancelable_request.cc
@@ -15,7 +15,7 @@ CancelableRequestProvider::~CancelableRequestProvider() {
// then the Profile is deleted.
AutoLock lock(pending_request_lock_);
while (!pending_requests_.empty())
- CancelRequestLocked(pending_requests_.begin()->first);
+ CancelRequestLocked(pending_requests_.begin());
}
CancelableRequestProvider::Handle CancelableRequestProvider::AddRequest(
@@ -38,19 +38,20 @@ CancelableRequestProvider::Handle CancelableRequestProvider::AddRequest(
void CancelableRequestProvider::CancelRequest(Handle handle) {
AutoLock lock(pending_request_lock_);
- CancelRequestLocked(handle);
+ CancelRequestLocked(pending_requests_.find(handle));
}
-void CancelableRequestProvider::CancelRequestLocked(Handle handle) {
- CancelableRequestMap::iterator i = pending_requests_.find(handle);
- if (i == pending_requests_.end()) {
+void CancelableRequestProvider::CancelRequestLocked(
+ const CancelableRequestMap::iterator& item) {
+ pending_request_lock_.AssertAcquired();
+ if (item == pending_requests_.end()) {
NOTREACHED() << "Trying to cancel an unknown request";
return;
}
- i->second->consumer()->OnRequestRemoved(this, handle);
- i->second->set_canceled();
- pending_requests_.erase(i);
+ item->second->consumer()->OnRequestRemoved(this, item->first);
+ item->second->set_canceled();
+ pending_requests_.erase(item);
}
void CancelableRequestProvider::RequestCompleted(Handle handle) {
diff --git a/chrome/browser/cancelable_request.h b/chrome/browser/cancelable_request.h
index 2dc4b9b..28bc8ed 100644
--- a/chrome/browser/cancelable_request.h
+++ b/chrome/browser/cancelable_request.h
@@ -41,7 +41,7 @@
// typedef Callback1<int>::Type RequestCallbackType;
//
// Handle StartRequest(int some_input1, int some_input2,
-// CancelableRequestConsumer* consumer,
+// CancelableRequestConsumerBase* consumer,
// RequestCallbackType* callback) {
// scoped_refptr<CancelableRequest<RequestCallbackType> > request(
// new CancelableRequest<RequestCallbackType>(callback));
@@ -137,14 +137,14 @@ class CancelableRequestProvider {
void RequestCompleted(Handle handle);
private:
+ typedef std::map<Handle, scoped_refptr<CancelableRequestBase> >
+ CancelableRequestMap;
+
// Only call this when you already have acquired pending_request_lock_.
- void CancelRequestLocked(Handle handle);
+ void CancelRequestLocked(const CancelableRequestMap::iterator& item);
friend class CancelableRequestBase;
- typedef std::map<Handle, scoped_refptr<CancelableRequestBase> >
- CancelableRequestMap;
-
Lock pending_request_lock_;
// Lists all outstanding requests. Protected by the |lock_|.