| OLD | NEW |
| 1 // Copyright (c) 2011 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 // 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 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 T GetClientData(CancelableRequestProvider* p, | 231 T GetClientData(CancelableRequestProvider* p, |
| 232 CancelableRequestProvider::Handle h); | 232 CancelableRequestProvider::Handle h); |
| 233 | 233 |
| 234 // Returns the data associated with the current request being processed. This | 234 // Returns the data associated with the current request being processed. This |
| 235 // is only valid during the time a callback is being processed. | 235 // is only valid during the time a callback is being processed. |
| 236 T GetClientDataForCurrentRequest(); | 236 T GetClientDataForCurrentRequest(); |
| 237 | 237 |
| 238 // Returns true if there are any pending requests. | 238 // Returns true if there are any pending requests. |
| 239 bool HasPendingRequests() const; | 239 bool HasPendingRequests() const; |
| 240 | 240 |
| 241 // Returns true if there are any pending requests. |
| 242 bool HasPendingRequestsForClientData(T client_data) const; |
| 243 |
| 241 // Returns the number of pending requests. | 244 // Returns the number of pending requests. |
| 242 size_t PendingRequestCount() const; | 245 size_t PendingRequestCount() const; |
| 243 | 246 |
| 244 // Cancels all requests outstanding. | 247 // Cancels all requests outstanding. |
| 245 void CancelAllRequests(); | 248 void CancelAllRequests(); |
| 246 | 249 |
| 250 // Cancels all requests outstanding matching the client data |
| 251 void CancelAllRequestsForClientData(T client_data); |
| 252 |
| 247 // Returns the handle for the first request that has the specified client data | 253 // Returns the handle for the first request that has the specified client data |
| 248 // (in |handle|). Returns true if there is a request for the specified client | 254 // (in |handle|). Returns true if there is a request for the specified client |
| 249 // data, false otherwise. | 255 // data, false otherwise. |
| 250 bool GetFirstHandleForClientData(T client_data, | 256 bool GetFirstHandleForClientData(T client_data, |
| 251 CancelableRequestProvider::Handle* handle); | 257 CancelableRequestProvider::Handle* handle); |
| 252 | 258 |
| 253 // Gets the client data for all pending requests. | 259 // Gets the client data for all pending requests. |
| 254 void GetAllClientData(std::vector<T>* data); | 260 void GetAllClientData(std::vector<T>* data); |
| 255 | 261 |
| 256 protected: | 262 protected: |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 DCHECK(current_request_.is_valid()); | 337 DCHECK(current_request_.is_valid()); |
| 332 return GetClientData(current_request_.provider, current_request_.handle); | 338 return GetClientData(current_request_.provider, current_request_.handle); |
| 333 } | 339 } |
| 334 | 340 |
| 335 template<class T> | 341 template<class T> |
| 336 bool CancelableRequestConsumerTSimple<T>::HasPendingRequests() const { | 342 bool CancelableRequestConsumerTSimple<T>::HasPendingRequests() const { |
| 337 return !pending_requests_.empty(); | 343 return !pending_requests_.empty(); |
| 338 } | 344 } |
| 339 | 345 |
| 340 template<class T> | 346 template<class T> |
| 347 bool CancelableRequestConsumerTSimple<T>::HasPendingRequestsForClientData( |
| 348 T client_data) const { |
| 349 for (typename PendingRequestList::const_iterator i = |
| 350 pending_requests_.begin(); i != pending_requests_.end(); ++i) { |
| 351 if (i->second == client_data) { |
| 352 return true; |
| 353 } |
| 354 } |
| 355 return false; |
| 356 } |
| 357 |
| 358 template<class T> |
| 341 size_t CancelableRequestConsumerTSimple<T>::PendingRequestCount() const { | 359 size_t CancelableRequestConsumerTSimple<T>::PendingRequestCount() const { |
| 342 return pending_requests_.size(); | 360 return pending_requests_.size(); |
| 343 } | 361 } |
| 344 | 362 |
| 345 template<class T> | 363 template<class T> |
| 346 void CancelableRequestConsumerTSimple<T>::CancelAllRequests() { | 364 void CancelableRequestConsumerTSimple<T>::CancelAllRequests() { |
| 347 // TODO(atwilson): This code is not thread safe as it is called from the | 365 // TODO(atwilson): This code is not thread safe as it is called from the |
| 348 // consumer thread (via the destructor) and accesses pending_requests_ | 366 // consumer thread (via the destructor) and accesses pending_requests_ |
| 349 // without acquiring the provider lock (http://crbug.com/85970). | 367 // without acquiring the provider lock (http://crbug.com/85970). |
| 350 PendingRequestList copied_requests(pending_requests_); | 368 PendingRequestList copied_requests(pending_requests_); |
| 351 for (typename PendingRequestList::iterator i = copied_requests.begin(); | 369 for (typename PendingRequestList::iterator i = copied_requests.begin(); |
| 352 i != copied_requests.end(); ++i) | 370 i != copied_requests.end(); ++i) |
| 353 i->first.provider->CancelRequest(i->first.handle); | 371 i->first.provider->CancelRequest(i->first.handle); |
| 354 copied_requests.clear(); | 372 copied_requests.clear(); |
| 355 | 373 |
| 356 // That should have cleared all the pending items. | 374 // That should have cleared all the pending items. |
| 357 DCHECK(pending_requests_.empty()); | 375 DCHECK(pending_requests_.empty()); |
| 358 } | 376 } |
| 359 | 377 |
| 360 template<class T> | 378 template<class T> |
| 379 void CancelableRequestConsumerTSimple<T>::CancelAllRequestsForClientData( |
| 380 T client_data) { |
| 381 PendingRequestList copied_requests(pending_requests_); |
| 382 for (typename PendingRequestList::const_iterator i = copied_requests.begin(); |
| 383 i != copied_requests.end(); ++i) |
| 384 if (i->second == client_data) |
| 385 i->first.provider->CancelRequest(i->first.handle); |
| 386 copied_requests.clear(); |
| 387 } |
| 388 |
| 389 template<class T> |
| 361 bool CancelableRequestConsumerTSimple<T>::GetFirstHandleForClientData( | 390 bool CancelableRequestConsumerTSimple<T>::GetFirstHandleForClientData( |
| 362 T client_data, | 391 T client_data, |
| 363 CancelableRequestProvider::Handle* handle) { | 392 CancelableRequestProvider::Handle* handle) { |
| 364 for (typename PendingRequestList::const_iterator i = | 393 for (typename PendingRequestList::const_iterator i = |
| 365 pending_requests_.begin(); i != pending_requests_.end(); ++i) { | 394 pending_requests_.begin(); i != pending_requests_.end(); ++i) { |
| 366 if (i->second == client_data) { | 395 if (i->second == client_data) { |
| 367 *handle = i->first.handle; | 396 *handle = i->first.handle; |
| 368 return true; | 397 return true; |
| 369 } | 398 } |
| 370 } | 399 } |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 707 } | 736 } |
| 708 | 737 |
| 709 // The value. | 738 // The value. |
| 710 Type value; | 739 Type value; |
| 711 | 740 |
| 712 protected: | 741 protected: |
| 713 virtual ~CancelableRequest1() {} | 742 virtual ~CancelableRequest1() {} |
| 714 }; | 743 }; |
| 715 | 744 |
| 716 #endif // CONTENT_BROWSER_CANCELABLE_REQUEST_H_ | 745 #endif // CONTENT_BROWSER_CANCELABLE_REQUEST_H_ |
| OLD | NEW |