OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef WTF_MakeCancellable_h | 5 #ifndef WTF_MakeCancellable_h |
6 #define WTF_MakeCancellable_h | 6 #define WTF_MakeCancellable_h |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "wtf/Functional.h" | 9 #include "wtf/Functional.h" |
10 #include "wtf/RefCounted.h" | 10 #include "wtf/RefCounted.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 friend RefCounted<FunctionCanceller>; | 28 friend RefCounted<FunctionCanceller>; |
29 | 29 |
30 DISALLOW_COPY_AND_ASSIGN(FunctionCanceller); | 30 DISALLOW_COPY_AND_ASSIGN(FunctionCanceller); |
31 }; | 31 }; |
32 | 32 |
33 template <typename... Params> | 33 template <typename... Params> |
34 class FunctionCancellerImpl final : public FunctionCanceller { | 34 class FunctionCancellerImpl final : public FunctionCanceller { |
35 public: | 35 public: |
36 FunctionCancellerImpl(std::unique_ptr<Function<void(Params...)>> function) | 36 FunctionCancellerImpl(std::unique_ptr<Function<void(Params...)>> function) |
37 : m_function(std::move(function)) | 37 : m_function(std::move(function)) |
| 38 , m_weakPtrFactory(this) |
38 { | 39 { |
39 DCHECK(m_function); | 40 DCHECK(m_function); |
40 } | 41 } |
41 | 42 |
42 void runUnlessCancelled(const ScopedFunctionCanceller&, Params... params) | 43 void run(const ScopedFunctionCanceller&, Params... params) |
43 { | 44 { |
44 if (m_function) | 45 DCHECK(m_function); |
45 (*m_function)(std::forward<Params>(params)...); | 46 (*m_function)(std::forward<Params>(params)...); |
| 47 } |
| 48 |
| 49 WeakPtr<FunctionCancellerImpl> createWeakPtr() |
| 50 { |
| 51 return m_weakPtrFactory.createWeakPtr(); |
46 } | 52 } |
47 | 53 |
48 void cancel() override | 54 void cancel() override |
49 { | 55 { |
| 56 m_weakPtrFactory.revokeAll(); |
50 m_function = nullptr; | 57 m_function = nullptr; |
51 } | 58 } |
52 | 59 |
53 bool isActive() const override | 60 bool isActive() const override |
54 { | 61 { |
55 return !!m_function; | 62 return !!m_function; |
56 } | 63 } |
57 | 64 |
58 private: | 65 private: |
59 ~FunctionCancellerImpl() override = default; | 66 ~FunctionCancellerImpl() override = default; |
60 | 67 |
61 std::unique_ptr<WTF::Function<void(Params...)>> m_function; | 68 std::unique_ptr<WTF::Function<void(Params...)>> m_function; |
| 69 WeakPtrFactory<FunctionCancellerImpl<Params...>> m_weakPtrFactory; |
62 | 70 |
63 DISALLOW_COPY_AND_ASSIGN(FunctionCancellerImpl); | 71 DISALLOW_COPY_AND_ASSIGN(FunctionCancellerImpl); |
64 }; | 72 }; |
65 | 73 |
66 } // namespace internal | 74 } // namespace internal |
67 | 75 |
68 // ScopedFunctionCanceller is a handle associated to a Function, and cancels the | 76 // ScopedFunctionCanceller is a handle associated to a Function, and cancels the |
69 // invocation of the Function on the scope out or cancel() call. | 77 // invocation of the Function on the scope out or cancel() call. |
70 // Example: | 78 // Example: |
71 // void Foo() {} | 79 // void Foo() {} |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 // }; | 164 // }; |
157 // | 165 // |
158 // Foo* foo = new Foo; | 166 // Foo* foo = new Foo; |
159 // auto result = makeCancellable(bind(&Foo::bar, wrapPersistent(foo))); | 167 // auto result = makeCancellable(bind(&Foo::bar, wrapPersistent(foo))); |
160 // | 168 // |
161 // // Destruction of the resulting Function implies the destruction of | 169 // // Destruction of the resulting Function implies the destruction of |
162 // // the original function via ScopedFunctionCanceller below, that | 170 // // the original function via ScopedFunctionCanceller below, that |
163 // // resolves a circular strong reference: | 171 // // resolves a circular strong reference: |
164 // // foo -> m_canceller -> m_function -> foo | 172 // // foo -> m_canceller -> m_function -> foo |
165 // result.function = nullptr; | 173 // result.function = nullptr; |
166 auto wrappedFunction = bind(&Canceller::runUnlessCancelled, canceller, Scope
dFunctionCanceller(canceller)); | 174 auto wrappedFunction = bind(&Canceller::run, canceller->createWeakPtr(), Sco
pedFunctionCanceller(canceller)); |
167 return MakeCancellableResult<Params...>(ScopedFunctionCanceller(canceller.re
lease()), std::move(wrappedFunction)); | 175 return MakeCancellableResult<Params...>(ScopedFunctionCanceller(canceller.re
lease()), std::move(wrappedFunction)); |
168 } | 176 } |
169 | 177 |
170 } // namespace WTF | 178 } // namespace WTF |
171 | 179 |
172 #endif // WTF_MakeCancellable_h | 180 #endif // WTF_MakeCancellable_h |
OLD | NEW |