| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // CancelableRequestProviders and Consumers work together to make requests that | 5 // CancelableRequestProviders and Consumers work together to make requests that |
| 6 // execute on a background thread in the provider and return data to the | 6 // execute on a background thread in the provider and return data to the |
| 7 // consumer. These class collaborate to keep a list of open requests and to | 7 // consumer. These class collaborate to keep a list of open requests and to |
| 8 // make sure that requests to not outlive either of the objects involved in the | 8 // make sure that requests to not outlive either of the objects involved in the |
| 9 // transaction. | 9 // transaction. |
| 10 // | 10 // |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 } | 230 } |
| 231 | 231 |
| 232 // Returns the number of pending requests. | 232 // Returns the number of pending requests. |
| 233 size_t PendingRequestCount() const { | 233 size_t PendingRequestCount() const { |
| 234 return pending_requests_.size(); | 234 return pending_requests_.size(); |
| 235 } | 235 } |
| 236 | 236 |
| 237 // Cancels all requests outstanding. | 237 // Cancels all requests outstanding. |
| 238 void CancelAllRequests() { | 238 void CancelAllRequests() { |
| 239 PendingRequestList copied_requests(pending_requests_); | 239 PendingRequestList copied_requests(pending_requests_); |
| 240 for (PendingRequestList::iterator i = copied_requests.begin(); | 240 for (typename PendingRequestList::iterator i = copied_requests.begin(); |
| 241 i != copied_requests.end(); ++i) | 241 i != copied_requests.end(); ++i) |
| 242 i->first.provider->CancelRequest(i->first.handle); | 242 i->first.provider->CancelRequest(i->first.handle); |
| 243 copied_requests.clear(); | 243 copied_requests.clear(); |
| 244 | 244 |
| 245 // That should have cleared all the pending items. | 245 // That should have cleared all the pending items. |
| 246 DCHECK(pending_requests_.empty()); | 246 DCHECK(pending_requests_.empty()); |
| 247 } | 247 } |
| 248 | 248 |
| 249 // Gets the client data for all pending requests. | 249 // Gets the client data for all pending requests. |
| 250 void GetAllClientData(std::vector<T>* data) { | 250 void GetAllClientData(std::vector<T>* data) { |
| 251 DCHECK(data); | 251 DCHECK(data); |
| 252 for (PendingRequestList::iterator i = pending_requests_.begin(); | 252 for (typename PendingRequestList::iterator i = pending_requests_.begin(); |
| 253 i != pending_requests_.end(); ++i) | 253 i != pending_requests_.end(); ++i) |
| 254 data->push_back(i->second); | 254 data->push_back(i->second); |
| 255 } | 255 } |
| 256 | 256 |
| 257 protected: | 257 protected: |
| 258 struct PendingRequest { | 258 struct PendingRequest { |
| 259 PendingRequest(CancelableRequestProvider* p, | 259 PendingRequest(CancelableRequestProvider* p, |
| 260 CancelableRequestProvider::Handle h) | 260 CancelableRequestProvider::Handle h) |
| 261 : provider(p), handle(h) { | 261 : provider(p), handle(h) { |
| 262 } | 262 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 275 | 275 |
| 276 virtual void OnRequestAdded(CancelableRequestProvider* provider, | 276 virtual void OnRequestAdded(CancelableRequestProvider* provider, |
| 277 CancelableRequestProvider::Handle handle) { | 277 CancelableRequestProvider::Handle handle) { |
| 278 DCHECK(pending_requests_.find(PendingRequest(provider, handle)) == | 278 DCHECK(pending_requests_.find(PendingRequest(provider, handle)) == |
| 279 pending_requests_.end()); | 279 pending_requests_.end()); |
| 280 pending_requests_[PendingRequest(provider, handle)] = initial_t; | 280 pending_requests_[PendingRequest(provider, handle)] = initial_t; |
| 281 } | 281 } |
| 282 | 282 |
| 283 virtual void OnRequestRemoved(CancelableRequestProvider* provider, | 283 virtual void OnRequestRemoved(CancelableRequestProvider* provider, |
| 284 CancelableRequestProvider::Handle handle) { | 284 CancelableRequestProvider::Handle handle) { |
| 285 PendingRequestList::iterator i = | 285 typename PendingRequestList::iterator i = |
| 286 pending_requests_.find(PendingRequest(provider, handle)); | 286 pending_requests_.find(PendingRequest(provider, handle)); |
| 287 if (i == pending_requests_.end()) { | 287 if (i == pending_requests_.end()) { |
| 288 NOTREACHED() << "Got a complete notification for a nonexistant request"; | 288 NOTREACHED() << "Got a complete notification for a nonexistant request"; |
| 289 return; | 289 return; |
| 290 } | 290 } |
| 291 | 291 |
| 292 pending_requests_.erase(i); | 292 pending_requests_.erase(i); |
| 293 } | 293 } |
| 294 | 294 |
| 295 // Lists all outstanding requests. | 295 // Lists all outstanding requests. |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 510 // typedef CancelableRequest1<FooCallback, std::vector<Foo>> FooRequest; | 510 // typedef CancelableRequest1<FooCallback, std::vector<Foo>> FooRequest; |
| 511 // 3. The provider method should then fillin the contents of the vector, | 511 // 3. The provider method should then fillin the contents of the vector, |
| 512 // forwarding the result like so: | 512 // forwarding the result like so: |
| 513 // request->ForwardResult(FooRequest::TupleType(request->handle(), | 513 // request->ForwardResult(FooRequest::TupleType(request->handle(), |
| 514 // &request->value)); | 514 // &request->value)); |
| 515 // | 515 // |
| 516 // Tip: for passing more than one value, use a Tuple for the value. | 516 // Tip: for passing more than one value, use a Tuple for the value. |
| 517 template<typename CB, typename Type> | 517 template<typename CB, typename Type> |
| 518 class CancelableRequest1 : public CancelableRequest<CB> { | 518 class CancelableRequest1 : public CancelableRequest<CB> { |
| 519 public: | 519 public: |
| 520 explicit CancelableRequest1(CallbackType* callback) | 520 explicit CancelableRequest1( |
| 521 : CancelableRequest(callback) { | 521 typename CancelableRequest<CB>::CallbackType* callback) |
| 522 : CancelableRequest<CB>(callback) { |
| 522 } | 523 } |
| 523 | 524 |
| 524 virtual ~CancelableRequest1() { | 525 virtual ~CancelableRequest1() { |
| 525 } | 526 } |
| 526 | 527 |
| 527 // The value. | 528 // The value. |
| 528 Type value; | 529 Type value; |
| 529 }; | 530 }; |
| 530 | 531 |
| 531 #endif // CHROME_BROWSER_CANCELABLE_REQUEST_H__ | 532 #endif // CHROME_BROWSER_CANCELABLE_REQUEST_H__ |
| 532 | 533 |
| OLD | NEW |