| 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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // to add a new request. The request's Init() will be called (which is why | 132 // to add a new request. The request's Init() will be called (which is why |
| 133 // the consumer is required. The handle to the new request is returned. | 133 // the consumer is required. The handle to the new request is returned. |
| 134 Handle AddRequest(CancelableRequestBase* request, | 134 Handle AddRequest(CancelableRequestBase* request, |
| 135 CancelableRequestConsumerBase* consumer); | 135 CancelableRequestConsumerBase* consumer); |
| 136 | 136 |
| 137 // Called by the CancelableRequest when the request has executed. It will | 137 // Called by the CancelableRequest when the request has executed. It will |
| 138 // be removed from the list of pending requests (as opposed to canceling, | 138 // be removed from the list of pending requests (as opposed to canceling, |
| 139 // which will also set some state on the request). | 139 // which will also set some state on the request). |
| 140 void RequestCompleted(Handle handle); | 140 void RequestCompleted(Handle handle); |
| 141 | 141 |
| 142 // Identifies whether or not the specified request has already been completed |
| 143 // or canceled, according to whether it exists in |completed_requests_|. |
| 144 bool IsRequestCompleted(Handle handle); |
| 145 |
| 142 private: | 146 private: |
| 143 typedef std::map<Handle, scoped_refptr<CancelableRequestBase> > | 147 typedef std::map<Handle, scoped_refptr<CancelableRequestBase> > |
| 144 CancelableRequestMap; | 148 CancelableRequestMap; |
| 145 | 149 |
| 146 // Only call this when you already have acquired pending_request_lock_. | 150 // Only call this when you already have acquired pending_request_lock_. |
| 147 void CancelRequestLocked(const CancelableRequestMap::iterator& item); | 151 void CancelRequestLocked(const CancelableRequestMap::iterator& item); |
| 148 | 152 |
| 149 friend class CancelableRequestBase; | 153 friend class CancelableRequestBase; |
| 150 | 154 |
| 151 base::Lock pending_request_lock_; | 155 base::Lock pending_request_lock_; |
| 152 | 156 |
| 153 // Lists all outstanding requests. Protected by the |lock_|. | 157 // Lists all outstanding requests. Protected by the |lock_|. |
| 154 CancelableRequestMap pending_requests_; | 158 CancelableRequestMap pending_requests_; |
| 155 | 159 |
| 160 // Lists all completed or canceled requests. Protected by the |lock_|. |
| 161 CancelableRequestMap completed_requests_; |
| 162 |
| 156 // The next handle value we will return. Protected by the |lock_|. | 163 // The next handle value we will return. Protected by the |lock_|. |
| 157 int next_handle_; | 164 int next_handle_; |
| 158 | 165 |
| 159 DISALLOW_COPY_AND_ASSIGN(CancelableRequestProvider); | 166 DISALLOW_COPY_AND_ASSIGN(CancelableRequestProvider); |
| 160 }; | 167 }; |
| 161 | 168 |
| 162 // CancelableRequestConsumer -------------------------------------------------- | 169 // CancelableRequestConsumer -------------------------------------------------- |
| 163 // | 170 // |
| 164 // Classes wishing to make requests on a provider should have an instance of | 171 // Classes wishing to make requests on a provider should have an instance of |
| 165 // this class. Callers will need to pass a pointer to this consumer object | 172 // this class. Callers will need to pass a pointer to this consumer object |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 | 541 |
| 535 // Initializes the object with the particulars from the provider. It may only | 542 // Initializes the object with the particulars from the provider. It may only |
| 536 // be called once (it is called by the provider, which is a friend). | 543 // be called once (it is called by the provider, which is a friend). |
| 537 void Init(CancelableRequestProvider* provider, | 544 void Init(CancelableRequestProvider* provider, |
| 538 CancelableRequestProvider::Handle handle, | 545 CancelableRequestProvider::Handle handle, |
| 539 CancelableRequestConsumerBase* consumer); | 546 CancelableRequestConsumerBase* consumer); |
| 540 | 547 |
| 541 // Tells the provider that the request is complete, which then tells the | 548 // Tells the provider that the request is complete, which then tells the |
| 542 // consumer. | 549 // consumer. |
| 543 void NotifyCompleted() const { | 550 void NotifyCompleted() const { |
| 544 provider_->RequestCompleted(handle()); | 551 if (!provider_->IsRequestCompleted(handle())) { |
| 545 consumer_->DidExecute(provider_, handle_); | 552 provider_->RequestCompleted(handle()); |
| 553 consumer_->DidExecute(provider_, handle_); |
| 554 } |
| 546 } | 555 } |
| 547 | 556 |
| 548 // Cover method for CancelableRequestConsumerBase::WillExecute. | 557 // Cover method for CancelableRequestConsumerBase::WillExecute. |
| 549 void WillExecute() { | 558 void WillExecute() { |
| 550 consumer_->WillExecute(provider_, handle_); | 559 consumer_->WillExecute(provider_, handle_); |
| 551 } | 560 } |
| 552 | 561 |
| 553 // The message loop that this request was created on. The callback will | 562 // The message loop that this request was created on. The callback will |
| 554 // happen on the same thread. | 563 // happen on the same thread. |
| 555 MessageLoop* callback_thread_; | 564 MessageLoop* callback_thread_; |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 695 } | 704 } |
| 696 | 705 |
| 697 // The value. | 706 // The value. |
| 698 Type value; | 707 Type value; |
| 699 | 708 |
| 700 protected: | 709 protected: |
| 701 virtual ~CancelableRequest1() {} | 710 virtual ~CancelableRequest1() {} |
| 702 }; | 711 }; |
| 703 | 712 |
| 704 #endif // CONTENT_BROWSER_CANCELABLE_REQUEST_H_ | 713 #endif // CONTENT_BROWSER_CANCELABLE_REQUEST_H_ |
| OLD | NEW |