| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 // }; | 84 // }; |
| 85 | 85 |
| 86 #ifndef CONTENT_BROWSER_CANCELABLE_REQUEST_H_ | 86 #ifndef CONTENT_BROWSER_CANCELABLE_REQUEST_H_ |
| 87 #define CONTENT_BROWSER_CANCELABLE_REQUEST_H_ | 87 #define CONTENT_BROWSER_CANCELABLE_REQUEST_H_ |
| 88 #pragma once | 88 #pragma once |
| 89 | 89 |
| 90 #include <map> | 90 #include <map> |
| 91 #include <vector> | 91 #include <vector> |
| 92 | 92 |
| 93 #include "base/basictypes.h" | 93 #include "base/basictypes.h" |
| 94 #include "base/bind.h" |
| 94 #include "base/callback.h" | 95 #include "base/callback.h" |
| 95 #include "base/logging.h" | 96 #include "base/logging.h" |
| 96 #include "base/memory/ref_counted.h" | 97 #include "base/memory/ref_counted.h" |
| 97 #include "base/memory/scoped_ptr.h" | 98 #include "base/memory/scoped_ptr.h" |
| 98 #include "base/message_loop.h" | 99 #include "base/message_loop.h" |
| 99 #include "base/synchronization/cancellation_flag.h" | 100 #include "base/synchronization/cancellation_flag.h" |
| 100 #include "base/synchronization/lock.h" | 101 #include "base/synchronization/lock.h" |
| 101 #include "base/task.h" | 102 #include "base/task.h" |
| 102 #include "build/build_config.h" | 103 #include "build/build_config.h" |
| 103 #include "content/common/content_export.h" | 104 #include "content/common/content_export.h" |
| (...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 558 protected: | 559 protected: |
| 559 friend class base::RefCountedThreadSafe<CancelableRequestBase>; | 560 friend class base::RefCountedThreadSafe<CancelableRequestBase>; |
| 560 virtual ~CancelableRequestBase(); | 561 virtual ~CancelableRequestBase(); |
| 561 | 562 |
| 562 // Initializes the object with the particulars from the provider. It may only | 563 // Initializes the object with the particulars from the provider. It may only |
| 563 // be called once (it is called by the provider, which is a friend). | 564 // be called once (it is called by the provider, which is a friend). |
| 564 void Init(CancelableRequestProvider* provider, | 565 void Init(CancelableRequestProvider* provider, |
| 565 CancelableRequestProvider::Handle handle, | 566 CancelableRequestProvider::Handle handle, |
| 566 CancelableRequestConsumerBase* consumer); | 567 CancelableRequestConsumerBase* consumer); |
| 567 | 568 |
| 569 // Attempts to execute callback on |callback_thread_| if the request has not |
| 570 // been canceled yet. |
| 571 void DoForward(const base::Closure& forwarded_call, bool force_async); |
| 572 |
| 568 // Tells the provider that the request is complete, which then tells the | 573 // Tells the provider that the request is complete, which then tells the |
| 569 // consumer. | 574 // consumer. |
| 570 void NotifyCompleted() const { | 575 void NotifyCompleted() const { |
| 571 provider_->RequestCompleted(handle()); | 576 provider_->RequestCompleted(handle()); |
| 572 consumer_->DidExecute(provider_, handle_); | 577 consumer_->DidExecute(provider_, handle_); |
| 573 } | 578 } |
| 574 | 579 |
| 575 // Cover method for CancelableRequestConsumerBase::WillExecute. | 580 // Cover method for CancelableRequestConsumerBase::WillExecute. |
| 576 void WillExecute() { | 581 void WillExecute() { |
| 577 consumer_->WillExecute(provider_, handle_); | 582 consumer_->WillExecute(provider_, handle_); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 592 // The handle to this request inside the provider. This will be initialized | 597 // The handle to this request inside the provider. This will be initialized |
| 593 // to 0 when the request is created, and the provider will set it once the | 598 // to 0 when the request is created, and the provider will set it once the |
| 594 // request has been dispatched. | 599 // request has been dispatched. |
| 595 CancelableRequestProvider::Handle handle_; | 600 CancelableRequestProvider::Handle handle_; |
| 596 | 601 |
| 597 // Set if the caller cancels this request. No callbacks should be made when | 602 // Set if the caller cancels this request. No callbacks should be made when |
| 598 // this is set. | 603 // this is set. |
| 599 base::CancellationFlag canceled_; | 604 base::CancellationFlag canceled_; |
| 600 | 605 |
| 601 private: | 606 private: |
| 607 // Executes the |forwarded_call| and notifies the provider and the consumer |
| 608 // that this request has been completed. This must be called on the |
| 609 // |callback_thread_|. |
| 610 void ExecuteCallback(const base::Closure& forwarded_call); |
| 611 |
| 602 DISALLOW_COPY_AND_ASSIGN(CancelableRequestBase); | 612 DISALLOW_COPY_AND_ASSIGN(CancelableRequestBase); |
| 603 }; | 613 }; |
| 604 | 614 |
| 605 // Templatized class. This is the one you should use directly or inherit from. | 615 // Templatized class. This is the one you should use directly or inherit from. |
| 606 // The callback can be invoked by calling the ForwardResult() method. For this, | 616 // The callback can be invoked by calling the ForwardResult() method. For this, |
| 607 // you must either pack the parameters into a tuple, or use DispatchToMethod | 617 // you must either pack the parameters into a tuple, or use DispatchToMethod |
| 608 // (in tuple.h). | 618 // (in tuple.h). |
| 609 // | 619 // |
| 610 // If you inherit to add additional input parameters or to do more complex | 620 // If you inherit to add additional input parameters or to do more complex |
| 611 // memory management (see the bigger comment about this above), you can put | 621 // memory management (see the bigger comment about this above), you can put |
| (...skipping 12 matching lines...) Expand all Loading... |
| 624 // | 634 // |
| 625 // private: | 635 // private: |
| 626 // ~DoodieRequest() {} | 636 // ~DoodieRequest() {} |
| 627 // | 637 // |
| 628 // int input_arg1; | 638 // int input_arg1; |
| 629 // std::wstring input_arg2; | 639 // std::wstring input_arg2; |
| 630 // }; | 640 // }; |
| 631 template<typename CB> | 641 template<typename CB> |
| 632 class CancelableRequest : public CancelableRequestBase { | 642 class CancelableRequest : public CancelableRequestBase { |
| 633 public: | 643 public: |
| 644 // TODO(ajwong): After all calls have been migrated, remove this template in |
| 645 // favor of the specialization on base::Callback<Sig>. |
| 634 typedef CB CallbackType; // CallbackRunner<...> | 646 typedef CB CallbackType; // CallbackRunner<...> |
| 635 typedef typename CB::TupleType TupleType; // Tuple of the callback args. | 647 typedef typename CB::TupleType TupleType; // Tuple of the callback args. |
| 636 | 648 |
| 637 // The provider MUST call Init() (on the base class) before this is valid. | 649 // The provider MUST call Init() (on the base class) before this is valid. |
| 638 // This class will take ownership of the callback object and destroy it when | 650 // This class will take ownership of the callback object and destroy it when |
| 639 // appropriate. | 651 // appropriate. |
| 640 explicit CancelableRequest(CallbackType* callback) | 652 explicit CancelableRequest(CallbackType* callback) |
| 641 : CancelableRequestBase(), | 653 : CancelableRequestBase(), |
| 642 callback_(callback) { | 654 callback_(callback) { |
| 643 DCHECK(callback) << "We should always have a callback"; | 655 DCHECK(callback) << "We should always have a callback"; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 // cancel this request; we must check canceled again. | 705 // cancel this request; we must check canceled again. |
| 694 if (!canceled_.IsSet()) | 706 if (!canceled_.IsSet()) |
| 695 NotifyCompleted(); | 707 NotifyCompleted(); |
| 696 } | 708 } |
| 697 | 709 |
| 698 // This should only be executed if !canceled_.IsSet(), | 710 // This should only be executed if !canceled_.IsSet(), |
| 699 // otherwise the pointers may be invalid. | 711 // otherwise the pointers may be invalid. |
| 700 scoped_ptr<CallbackType> callback_; | 712 scoped_ptr<CallbackType> callback_; |
| 701 }; | 713 }; |
| 702 | 714 |
| 715 // CancelableRequest<> wraps a normal base::Callback<> for use in the |
| 716 // CancelableRequest framework. |
| 717 // |
| 718 // When using this class, the provider MUST call Init() (on the base class) |
| 719 // before this is valid. |
| 720 // |
| 721 // The two functions ForwardResult(), and ForwardResultAsync() are used to |
| 722 // invoke the wrapped callback on the correct thread. They are the same |
| 723 // except that ForwardResult() will run the callback synchronously if |
| 724 // we are already on the right thread. |
| 725 // |
| 726 // The caller does not need to check for cancel before calling ForwardResult() |
| 727 // or ForwardResultAsync(). If the request has not been canceled, one of the |
| 728 // these MUST be called. |
| 729 // |
| 730 // If there are any pointers in the parameters, they must live at least as |
| 731 // long as the request so that it can be forwarded to the other thread. |
| 732 // For complex objects, this would typically be done by having a derived |
| 733 // request own the data itself. |
| 734 // |
| 735 // The following specializations handle different arities of base::Callbacks<>. |
| 736 template<> |
| 737 class CancelableRequest<base::Closure> : public CancelableRequestBase { |
| 738 public: |
| 739 typedef base::Closure CallbackType; |
| 740 |
| 741 explicit CancelableRequest(const CallbackType& callback) |
| 742 : CancelableRequestBase(), |
| 743 callback_(callback) { |
| 744 DCHECK(!callback.is_null()) << "Callback must be initialized."; |
| 745 } |
| 746 |
| 747 void ForwardResult(void) { |
| 748 if (canceled()) return; |
| 749 DoForward(callback_, false); |
| 750 } |
| 751 |
| 752 void ForwardResultAsync() { |
| 753 if (canceled()) return; |
| 754 DoForward(callback_, true); |
| 755 } |
| 756 |
| 757 protected: |
| 758 virtual ~CancelableRequest() {} |
| 759 |
| 760 private: |
| 761 CallbackType callback_; |
| 762 }; |
| 763 |
| 764 template<typename A1> |
| 765 class CancelableRequest<base::Callback<void(A1)> > : |
| 766 public CancelableRequestBase { |
| 767 public: |
| 768 typedef base::Callback<void(A1)> CallbackType; |
| 769 |
| 770 explicit CancelableRequest(const CallbackType& callback) |
| 771 : CancelableRequestBase(), |
| 772 callback_(callback) { |
| 773 DCHECK(!callback.is_null()) << "Callback must be initialized."; |
| 774 } |
| 775 |
| 776 void ForwardResult(const A1& a1) { |
| 777 if (canceled()) return; |
| 778 DoForward(base::Bind(callback_, a1), false); |
| 779 } |
| 780 |
| 781 void ForwardResultAsync(const A1& a1) { |
| 782 if (canceled()) return; |
| 783 DoForward(base::Bind(callback_, a1), true); |
| 784 } |
| 785 |
| 786 protected: |
| 787 virtual ~CancelableRequest() {} |
| 788 |
| 789 private: |
| 790 CallbackType callback_; |
| 791 }; |
| 792 |
| 793 template<typename A1, typename A2> |
| 794 class CancelableRequest<base::Callback<void(A1,A2)> > : |
| 795 public CancelableRequestBase { |
| 796 public: |
| 797 typedef base::Callback<void(A1,A2)> CallbackType; |
| 798 |
| 799 explicit CancelableRequest(const CallbackType& callback) |
| 800 : CancelableRequestBase(), |
| 801 callback_(callback) { |
| 802 DCHECK(!callback.is_null()) << "Callback must be initialized."; |
| 803 } |
| 804 |
| 805 void ForwardResult(const A1& a1, const A2& a2) { |
| 806 if (canceled()) return; |
| 807 DoForward(base::Bind(callback_, a1, a2), false); |
| 808 } |
| 809 |
| 810 void ForwardResultAsync(const A1& a1, const A2& a2) { |
| 811 if (canceled()) return; |
| 812 DoForward(base::Bind(callback_, a1, a2), true); |
| 813 } |
| 814 |
| 815 protected: |
| 816 virtual ~CancelableRequest() {} |
| 817 |
| 818 private: |
| 819 CallbackType callback_; |
| 820 }; |
| 821 |
| 822 template<typename A1, typename A2, typename A3> |
| 823 class CancelableRequest<base::Callback<void(A1,A2,A3)> > : |
| 824 public CancelableRequestBase { |
| 825 public: |
| 826 typedef base::Callback<void(A1,A2,A3)> CallbackType; |
| 827 |
| 828 explicit CancelableRequest(const CallbackType& callback) |
| 829 : CancelableRequestBase(), |
| 830 callback_(callback) { |
| 831 DCHECK(!callback.is_null()) << "Callback must be initialized."; |
| 832 } |
| 833 |
| 834 void ForwardResult(const A1& a1, const A2& a2, const A3& a3) { |
| 835 if (canceled()) return; |
| 836 DoForward(base::Bind(callback_, a1, a2, a3), false); |
| 837 } |
| 838 |
| 839 void ForwardResultAsync(const A1& a1, const A2& a2, const A3& a3) { |
| 840 if (canceled()) return; |
| 841 DoForward(base::Bind(callback_, a1, a2, a3), true); |
| 842 } |
| 843 |
| 844 protected: |
| 845 virtual ~CancelableRequest() {} |
| 846 |
| 847 private: |
| 848 CallbackType callback_; |
| 849 }; |
| 850 |
| 851 template<typename A1, typename A2, typename A3, typename A4> |
| 852 class CancelableRequest<base::Callback<void(A1, A2, A3, A4)> > : |
| 853 public CancelableRequestBase { |
| 854 public: |
| 855 typedef base::Callback<void(A1, A2, A3, A4)> CallbackType; |
| 856 |
| 857 explicit CancelableRequest(const CallbackType& callback) |
| 858 : CancelableRequestBase(), |
| 859 callback_(callback) { |
| 860 DCHECK(!callback.is_null()) << "Callback must be initialized."; |
| 861 } |
| 862 |
| 863 void ForwardResult(const A1& a1, const A2& a2, const A3& a3, const A4& a4) { |
| 864 if (canceled()) return; |
| 865 DoForward(base::Bind(callback_, a1, a2, a3, a4), false); |
| 866 } |
| 867 |
| 868 void ForwardResultAsync(const A1& a1, const A2& a2, const A3& a3, |
| 869 const A4& a4) { |
| 870 if (canceled()) return; |
| 871 DoForward(base::Bind(callback_, a1, a2, a3, a4), true); |
| 872 } |
| 873 |
| 874 protected: |
| 875 virtual ~CancelableRequest() {} |
| 876 |
| 877 private: |
| 878 CallbackType callback_; |
| 879 }; |
| 880 |
| 881 template<typename A1, typename A2, typename A3, typename A4, typename A5> |
| 882 class CancelableRequest<base::Callback<void(A1, A2, A3, A4, A5)> > : |
| 883 public CancelableRequestBase { |
| 884 public: |
| 885 typedef base::Callback<void(A1, A2, A3, A4, A5)> CallbackType; |
| 886 |
| 887 explicit CancelableRequest(const CallbackType& callback) |
| 888 : CancelableRequestBase(), |
| 889 callback_(callback) { |
| 890 DCHECK(!callback.is_null()) << "Callback must be initialized."; |
| 891 } |
| 892 |
| 893 void ForwardResult(const A1& a1, const A2& a2, const A3& a3, const A4& a4, |
| 894 const A5& a5) { |
| 895 if (canceled()) return; |
| 896 DoForward(base::Bind(callback_, a1, a2, a3, a4, a5), false); |
| 897 } |
| 898 |
| 899 void ForwardResultAsync(const A1& a1, const A2& a2, const A3& a3, |
| 900 const A4& a4, const A5& a5) { |
| 901 if (canceled()) return; |
| 902 DoForward(base::Bind(callback_, a1, a2, a3, a4, a5), true); |
| 903 } |
| 904 |
| 905 protected: |
| 906 virtual ~CancelableRequest() {} |
| 907 |
| 908 private: |
| 909 CallbackType callback_; |
| 910 }; |
| 911 |
| 912 template<typename A1, typename A2, typename A3, typename A4, typename A5, |
| 913 typename A6> |
| 914 class CancelableRequest<base::Callback<void(A1, A2, A3, A4, A5, A6)> > : |
| 915 public CancelableRequestBase { |
| 916 public: |
| 917 typedef base::Callback<void(A1, A2, A3, A4, A5, A6)> CallbackType; |
| 918 |
| 919 explicit CancelableRequest(const CallbackType& callback) |
| 920 : CancelableRequestBase(), |
| 921 callback_(callback) { |
| 922 DCHECK(!callback.is_null()) << "Callback must be initialized."; |
| 923 } |
| 924 |
| 925 void ForwardResult(const A1& a1, const A2& a2, const A3& a3, const A4& a4, |
| 926 const A5& a5, const A6& a6) { |
| 927 if (canceled()) return; |
| 928 DoForward(base::Bind(callback_, a1, a2, a3, a4, a5, a6), false); |
| 929 } |
| 930 |
| 931 void ForwardResultAsync(const A1& a1, const A2& a2, const A3& a3, |
| 932 const A4& a4, const A5& a5, const A6& a6) { |
| 933 if (canceled()) return; |
| 934 DoForward(base::Bind(callback_, a1, a2, a3, a4, a5, a6), true); |
| 935 } |
| 936 |
| 937 protected: |
| 938 virtual ~CancelableRequest() {} |
| 939 |
| 940 private: |
| 941 CallbackType callback_; |
| 942 }; |
| 943 |
| 703 // A CancelableRequest with a single value. This is intended for use when | 944 // A CancelableRequest with a single value. This is intended for use when |
| 704 // the provider provides a single value. The provider fills the result into | 945 // the provider provides a single value. The provider fills the result into |
| 705 // the value, and notifies the request with a pointer to the value. For example, | 946 // the value, and notifies the request with a pointer to the value. For example, |
| 706 // HistoryService has many methods that callback with a vector. Use the | 947 // HistoryService has many methods that callback with a vector. Use the |
| 707 // following pattern for this: | 948 // following pattern for this: |
| 708 // 1. Define the callback: | 949 // 1. Define the callback: |
| 709 // typedef Callback2<Handle, std::vector<Foo>*>::Type FooCallback; | 950 // typedef Callback2<Handle, std::vector<Foo>*>::Type FooCallback; |
| 710 // 2. Define the CancelableRequest1 type. | 951 // 2. Define the CancelableRequest1 type. |
| 711 // typedef CancelableRequest1<FooCallback, std::vector<Foo>> FooRequest; | 952 // typedef CancelableRequest1<FooCallback, std::vector<Foo>> FooRequest; |
| 712 // 3. The provider method should then fillin the contents of the vector, | 953 // 3. The provider method should then fillin the contents of the vector, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 724 } | 965 } |
| 725 | 966 |
| 726 // The value. | 967 // The value. |
| 727 Type value; | 968 Type value; |
| 728 | 969 |
| 729 protected: | 970 protected: |
| 730 virtual ~CancelableRequest1() {} | 971 virtual ~CancelableRequest1() {} |
| 731 }; | 972 }; |
| 732 | 973 |
| 733 #endif // CONTENT_BROWSER_CANCELABLE_REQUEST_H_ | 974 #endif // CONTENT_BROWSER_CANCELABLE_REQUEST_H_ |
| OLD | NEW |