| 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 classes collaborate to keep a list of open requests and to | 7 // consumer. These classes collaborate to keep a list of open requests and to |
| 8 // make sure that requests do not outlive either of the objects involved in the | 8 // make sure that requests do not outlive either of the objects involved in the |
| 9 // transaction. | 9 // transaction. |
| 10 // | 10 // |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 // typedef Callback1<int>::Type RequestCallbackType; | 41 // typedef Callback1<int>::Type RequestCallbackType; |
| 42 // | 42 // |
| 43 // Handle StartRequest(int some_input1, int some_input2, | 43 // Handle StartRequest(int some_input1, int some_input2, |
| 44 // CancelableRequestConsumerBase* consumer, | 44 // CancelableRequestConsumerBase* consumer, |
| 45 // RequestCallbackType* callback) { | 45 // RequestCallbackType* callback) { |
| 46 // scoped_refptr<CancelableRequest<RequestCallbackType> > request( | 46 // scoped_refptr<CancelableRequest<RequestCallbackType> > request( |
| 47 // new CancelableRequest<RequestCallbackType>(callback)); | 47 // new CancelableRequest<RequestCallbackType>(callback)); |
| 48 // AddRequest(request, consumer); | 48 // AddRequest(request, consumer); |
| 49 // | 49 // |
| 50 // // Send the parameters and the request to the backend thread. | 50 // // Send the parameters and the request to the backend thread. |
| 51 // backend_thread_->PostTask(FROM_HERE, | 51 // backend_thread_->PostTask( |
| 52 // NewRunnableMethod(backend_, &Backend::DoRequest, request, | 52 // FROM_HERE, |
| 53 // base::Bind(&Backend::DoRequest, backend_, request, |
| 53 // some_input1, some_input2)); | 54 // some_input1, some_input2)); |
| 54 // | 55 // |
| 55 // // The handle will have been set by AddRequest. | 56 // // The handle will have been set by AddRequest. |
| 56 // return request->handle(); | 57 // return request->handle(); |
| 57 // } | 58 // } |
| 58 // }; | 59 // }; |
| 59 // | 60 // |
| 60 // | 61 // |
| 61 // Example backend provider that does work and dispatches the callback back | 62 // Example backend provider that does work and dispatches the callback back |
| 62 // to the original thread. Note that we need to pass it as a scoped_refptr so | 63 // to the original thread. Note that we need to pass it as a scoped_refptr so |
| (...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 // long as the request so that it can be forwarded to the other thread. | 667 // long as the request so that it can be forwarded to the other thread. |
| 667 // For complex objects, this would typically be done by having a derived | 668 // For complex objects, this would typically be done by having a derived |
| 668 // request own the data itself. | 669 // request own the data itself. |
| 669 void ForwardResult(const TupleType& param) { | 670 void ForwardResult(const TupleType& param) { |
| 670 DCHECK(callback_.get()); | 671 DCHECK(callback_.get()); |
| 671 if (!canceled()) { | 672 if (!canceled()) { |
| 672 if (callback_thread_ == MessageLoop::current()) { | 673 if (callback_thread_ == MessageLoop::current()) { |
| 673 // We can do synchronous callbacks when we're on the same thread. | 674 // We can do synchronous callbacks when we're on the same thread. |
| 674 ExecuteCallback(param); | 675 ExecuteCallback(param); |
| 675 } else { | 676 } else { |
| 676 callback_thread_->PostTask(FROM_HERE, NewRunnableMethod(this, | 677 callback_thread_->PostTask( |
| 677 &CancelableRequest<CB>::ExecuteCallback, param)); | 678 FROM_HERE, |
| 679 base::Bind(&CancelableRequest<CB>::ExecuteCallback, this, param)); |
| 678 } | 680 } |
| 679 } | 681 } |
| 680 } | 682 } |
| 681 | 683 |
| 682 // Like |ForwardResult| but this never does a synchronous callback. | 684 // Like |ForwardResult| but this never does a synchronous callback. |
| 683 void ForwardResultAsync(const TupleType& param) { | 685 void ForwardResultAsync(const TupleType& param) { |
| 684 DCHECK(callback_.get()); | 686 DCHECK(callback_.get()); |
| 685 if (!canceled()) { | 687 if (!canceled()) { |
| 686 callback_thread_->PostTask(FROM_HERE, NewRunnableMethod(this, | 688 callback_thread_->PostTask( |
| 687 &CancelableRequest<CB>::ExecuteCallback, param)); | 689 FROM_HERE, |
| 690 base::Bind(&CancelableRequest<CB>::ExecuteCallback, this, param)); |
| 688 } | 691 } |
| 689 } | 692 } |
| 690 | 693 |
| 691 protected: | 694 protected: |
| 692 virtual ~CancelableRequest() {} | 695 virtual ~CancelableRequest() {} |
| 693 | 696 |
| 694 private: | 697 private: |
| 695 // Executes the callback and notifies the provider and the consumer that this | 698 // Executes the callback and notifies the provider and the consumer that this |
| 696 // request has been completed. This must be called on the callback_thread_. | 699 // request has been completed. This must be called on the callback_thread_. |
| 697 void ExecuteCallback(const TupleType& param) { | 700 void ExecuteCallback(const TupleType& param) { |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1009 } | 1012 } |
| 1010 | 1013 |
| 1011 // The value. | 1014 // The value. |
| 1012 Type value; | 1015 Type value; |
| 1013 | 1016 |
| 1014 protected: | 1017 protected: |
| 1015 virtual ~CancelableRequest1() {} | 1018 virtual ~CancelableRequest1() {} |
| 1016 }; | 1019 }; |
| 1017 | 1020 |
| 1018 #endif // CHROME_BROWSER_CANCELABLE_REQUEST_H_ | 1021 #endif // CHROME_BROWSER_CANCELABLE_REQUEST_H_ |
| OLD | NEW |