OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/cancelable_request.h" | 5 #include "chrome/browser/cancelable_request.h" |
6 | 6 |
7 CancelableRequestProvider::CancelableRequestProvider() : next_handle_(1) { | 7 CancelableRequestProvider::CancelableRequestProvider() : next_handle_(1) { |
8 } | 8 } |
9 | 9 |
10 CancelableRequestProvider::~CancelableRequestProvider() { | 10 CancelableRequestProvider::~CancelableRequestProvider() { |
11 // There may be requests whose result callback has not been run yet. We need | 11 // There may be requests whose result callback has not been run yet. We need |
12 // to cancel them otherwise they may try and call us back after we've been | 12 // to cancel them otherwise they may try and call us back after we've been |
13 // deleted, or do other bad things. This can occur on shutdown (or profile | 13 // deleted, or do other bad things. This can occur on shutdown (or profile |
14 // destruction) when a request is scheduled, completed (but not dispatched), | 14 // destruction) when a request is scheduled, completed (but not dispatched), |
15 // then the Profile is deleted. | 15 // then the Profile is deleted. |
16 AutoLock lock(pending_request_lock_); | 16 base::AutoLock lock(pending_request_lock_); |
17 while (!pending_requests_.empty()) | 17 while (!pending_requests_.empty()) |
18 CancelRequestLocked(pending_requests_.begin()); | 18 CancelRequestLocked(pending_requests_.begin()); |
19 } | 19 } |
20 | 20 |
21 CancelableRequestProvider::Handle CancelableRequestProvider::AddRequest( | 21 CancelableRequestProvider::Handle CancelableRequestProvider::AddRequest( |
22 CancelableRequestBase* request, | 22 CancelableRequestBase* request, |
23 CancelableRequestConsumerBase* consumer) { | 23 CancelableRequestConsumerBase* consumer) { |
24 Handle handle; | 24 Handle handle; |
25 { | 25 { |
26 AutoLock lock(pending_request_lock_); | 26 base::AutoLock lock(pending_request_lock_); |
27 | 27 |
28 handle = next_handle_; | 28 handle = next_handle_; |
29 pending_requests_[next_handle_] = request; | 29 pending_requests_[next_handle_] = request; |
30 ++next_handle_; | 30 ++next_handle_; |
31 } | 31 } |
32 | 32 |
33 consumer->OnRequestAdded(this, handle); | 33 consumer->OnRequestAdded(this, handle); |
34 | 34 |
35 request->Init(this, handle, consumer); | 35 request->Init(this, handle, consumer); |
36 return handle; | 36 return handle; |
37 } | 37 } |
38 | 38 |
39 void CancelableRequestProvider::CancelRequest(Handle handle) { | 39 void CancelableRequestProvider::CancelRequest(Handle handle) { |
40 AutoLock lock(pending_request_lock_); | 40 base::AutoLock lock(pending_request_lock_); |
41 CancelRequestLocked(pending_requests_.find(handle)); | 41 CancelRequestLocked(pending_requests_.find(handle)); |
42 } | 42 } |
43 | 43 |
44 void CancelableRequestProvider::CancelRequestLocked( | 44 void CancelableRequestProvider::CancelRequestLocked( |
45 const CancelableRequestMap::iterator& item) { | 45 const CancelableRequestMap::iterator& item) { |
46 pending_request_lock_.AssertAcquired(); | 46 pending_request_lock_.AssertAcquired(); |
47 if (item == pending_requests_.end()) { | 47 if (item == pending_requests_.end()) { |
48 NOTREACHED() << "Trying to cancel an unknown request"; | 48 NOTREACHED() << "Trying to cancel an unknown request"; |
49 return; | 49 return; |
50 } | 50 } |
51 | 51 |
52 item->second->consumer()->OnRequestRemoved(this, item->first); | 52 item->second->consumer()->OnRequestRemoved(this, item->first); |
53 item->second->set_canceled(); | 53 item->second->set_canceled(); |
54 pending_requests_.erase(item); | 54 pending_requests_.erase(item); |
55 } | 55 } |
56 | 56 |
57 void CancelableRequestProvider::RequestCompleted(Handle handle) { | 57 void CancelableRequestProvider::RequestCompleted(Handle handle) { |
58 CancelableRequestConsumerBase* consumer = NULL; | 58 CancelableRequestConsumerBase* consumer = NULL; |
59 { | 59 { |
60 AutoLock lock(pending_request_lock_); | 60 base::AutoLock lock(pending_request_lock_); |
61 | 61 |
62 CancelableRequestMap::iterator i = pending_requests_.find(handle); | 62 CancelableRequestMap::iterator i = pending_requests_.find(handle); |
63 if (i == pending_requests_.end()) { | 63 if (i == pending_requests_.end()) { |
64 NOTREACHED() << "Trying to cancel an unknown request"; | 64 NOTREACHED() << "Trying to cancel an unknown request"; |
65 return; | 65 return; |
66 } | 66 } |
67 consumer = i->second->consumer(); | 67 consumer = i->second->consumer(); |
68 | 68 |
69 // This message should only get sent if the class is not cancelled, or | 69 // This message should only get sent if the class is not cancelled, or |
70 // else the consumer might be gone). | 70 // else the consumer might be gone). |
(...skipping 26 matching lines...) Expand all Loading... |
97 } | 97 } |
98 | 98 |
99 void CancelableRequestBase::Init(CancelableRequestProvider* provider, | 99 void CancelableRequestBase::Init(CancelableRequestProvider* provider, |
100 CancelableRequestProvider::Handle handle, | 100 CancelableRequestProvider::Handle handle, |
101 CancelableRequestConsumerBase* consumer) { | 101 CancelableRequestConsumerBase* consumer) { |
102 DCHECK(handle_ == 0 && provider_ == NULL && consumer_ == NULL); | 102 DCHECK(handle_ == 0 && provider_ == NULL && consumer_ == NULL); |
103 provider_ = provider; | 103 provider_ = provider; |
104 consumer_ = consumer; | 104 consumer_ = consumer; |
105 handle_ = handle; | 105 handle_ = handle; |
106 } | 106 } |
OLD | NEW |