OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // ============================================================================ | 5 // ============================================================================ |
6 // **************************************************************************** | 6 // **************************************************************************** |
7 // * THIS HEADER IS DEPRECATED, SEE base/callback.h FOR NEW IMPLEMENTATION * | 7 // * THIS HEADER IS DEPRECATED, SEE base/callback.h FOR NEW IMPLEMENTATION * |
8 // **************************************************************************** | 8 // **************************************************************************** |
9 // ============================================================================ | 9 // ============================================================================ |
10 // ============================================================================ | 10 // ============================================================================ |
11 // **************************************************************************** | 11 // **************************************************************************** |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 | 58 |
59 class BASE_EXPORT CancelableTask : public Task { | 59 class BASE_EXPORT CancelableTask : public Task { |
60 public: | 60 public: |
61 CancelableTask(); | 61 CancelableTask(); |
62 virtual ~CancelableTask(); | 62 virtual ~CancelableTask(); |
63 | 63 |
64 // Not all tasks support cancellation. | 64 // Not all tasks support cancellation. |
65 virtual void Cancel() = 0; | 65 virtual void Cancel() = 0; |
66 }; | 66 }; |
67 | 67 |
68 // Scoped Factories ------------------------------------------------------------ | |
69 // | |
70 // These scoped factory objects can be used by non-refcounted objects to safely | |
71 // place tasks in a message loop. Each factory guarantees that the tasks it | |
72 // produces will not run after the factory is destroyed. Commonly, factories | |
73 // are declared as class members, so the class' tasks will automatically cancel | |
74 // when the class instance is destroyed. | |
75 // | |
76 // Exampe Usage: | |
77 // | |
78 // class MyClass { | |
79 // private: | |
80 // // This factory will be used to schedule invocations of SomeMethod. | |
81 // ScopedRunnableMethodFactory<MyClass> some_method_factory_; | |
82 // | |
83 // public: | |
84 // // It is safe to suppress warning 4355 here. | |
85 // MyClass() : ALLOW_THIS_IN_INITIALIZER_LIST(some_method_factory_(this)) { } | |
86 // | |
87 // void SomeMethod() { | |
88 // // If this function might be called directly, you might want to revoke | |
89 // // any outstanding runnable methods scheduled to call it. If it's not | |
90 // // referenced other than by the factory, this is unnecessary. | |
91 // some_method_factory_.RevokeAll(); | |
92 // ... | |
93 // } | |
94 // | |
95 // void ScheduleSomeMethod() { | |
96 // // If you'd like to only only have one pending task at a time, test for | |
97 // // |empty| before manufacturing another task. | |
98 // if (!some_method_factory_.empty()) | |
99 // return; | |
100 // | |
101 // // The factories are not thread safe, so always invoke on | |
102 // // |MessageLoop::current()|. | |
103 // MessageLoop::current()->PostDelayedTask( | |
104 // FROM_HERE, | |
105 // some_method_factory_.NewRunnableMethod(&MyClass::SomeMethod), | |
106 // kSomeMethodDelayMS); | |
107 // } | |
108 // }; | |
109 | |
110 // A ScopedRunnableMethodFactory creates runnable methods for a specified | |
111 // object. This is particularly useful for generating callbacks for | |
112 // non-reference counted objects when the factory is a member of the object. | |
113 template<class T> | |
114 class ScopedRunnableMethodFactory { | |
115 public: | |
116 explicit ScopedRunnableMethodFactory(T* object) : weak_factory_(object) { | |
117 } | |
118 | |
119 template <class Method> | |
120 inline CancelableTask* NewRunnableMethod(Method method) { | |
121 return new RunnableMethod<Method, Tuple0>( | |
122 weak_factory_.GetWeakPtr(), method, MakeTuple()); | |
123 } | |
124 | |
125 template <class Method, class A> | |
126 inline CancelableTask* NewRunnableMethod(Method method, const A& a) { | |
127 return new RunnableMethod<Method, Tuple1<A> >( | |
128 weak_factory_.GetWeakPtr(), method, MakeTuple(a)); | |
129 } | |
130 | |
131 template <class Method, class A, class B> | |
132 inline CancelableTask* NewRunnableMethod(Method method, const A& a, | |
133 const B& b) { | |
134 return new RunnableMethod<Method, Tuple2<A, B> >( | |
135 weak_factory_.GetWeakPtr(), method, MakeTuple(a, b)); | |
136 } | |
137 | |
138 template <class Method, class A, class B, class C> | |
139 inline CancelableTask* NewRunnableMethod(Method method, | |
140 const A& a, | |
141 const B& b, | |
142 const C& c) { | |
143 return new RunnableMethod<Method, Tuple3<A, B, C> >( | |
144 weak_factory_.GetWeakPtr(), method, MakeTuple(a, b, c)); | |
145 } | |
146 | |
147 template <class Method, class A, class B, class C, class D> | |
148 inline CancelableTask* NewRunnableMethod(Method method, | |
149 const A& a, | |
150 const B& b, | |
151 const C& c, | |
152 const D& d) { | |
153 return new RunnableMethod<Method, Tuple4<A, B, C, D> >( | |
154 weak_factory_.GetWeakPtr(), method, MakeTuple(a, b, c, d)); | |
155 } | |
156 | |
157 template <class Method, class A, class B, class C, class D, class E> | |
158 inline CancelableTask* NewRunnableMethod(Method method, | |
159 const A& a, | |
160 const B& b, | |
161 const C& c, | |
162 const D& d, | |
163 const E& e) { | |
164 return new RunnableMethod<Method, Tuple5<A, B, C, D, E> >( | |
165 weak_factory_.GetWeakPtr(), method, MakeTuple(a, b, c, d, e)); | |
166 } | |
167 | |
168 void RevokeAll() { weak_factory_.InvalidateWeakPtrs(); } | |
169 | |
170 bool empty() const { return !weak_factory_.HasWeakPtrs(); } | |
171 | |
172 protected: | |
173 template <class Method, class Params> | |
174 class RunnableMethod : public CancelableTask { | |
175 public: | |
176 RunnableMethod(const base::WeakPtr<T>& obj, | |
177 Method meth, | |
178 const Params& params) | |
179 : obj_(obj), | |
180 meth_(meth), | |
181 params_(params) { | |
182 COMPILE_ASSERT( | |
183 (base::internal::ParamsUseScopedRefptrCorrectly<Params>::value), | |
184 badscopedrunnablemethodparams); | |
185 } | |
186 | |
187 virtual void Run() { | |
188 if (obj_) | |
189 DispatchToMethod(obj_.get(), meth_, params_); | |
190 } | |
191 | |
192 virtual void Cancel() { | |
193 obj_.reset(); | |
194 } | |
195 | |
196 private: | |
197 base::WeakPtr<T> obj_; | |
198 Method meth_; | |
199 Params params_; | |
200 | |
201 DISALLOW_COPY_AND_ASSIGN(RunnableMethod); | |
202 }; | |
203 | |
204 private: | |
205 base::WeakPtrFactory<T> weak_factory_; | |
206 }; | |
207 | |
208 // Delete helper for use with base::Bind(). If you're posting a task to delete | |
209 // an object, prefer DeleteSoon(). | |
210 template<typename T> | 68 template<typename T> |
211 void DeletePointer(T* obj) { | 69 void DeletePointer(T* obj) { |
212 delete obj; | 70 delete obj; |
213 } | 71 } |
214 | 72 |
215 // RunnableMethodTraits -------------------------------------------------------- | 73 // RunnableMethodTraits -------------------------------------------------------- |
216 // | 74 // |
217 // This traits-class is used by RunnableMethod to manage the lifetime of the | 75 // This traits-class is used by RunnableMethod to manage the lifetime of the |
218 // callee object. By default, it is assumed that the callee supports AddRef | 76 // callee object. By default, it is assumed that the callee supports AddRef |
219 // and Release methods. A particular class can specialize this template to | 77 // and Release methods. A particular class can specialize this template to |
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
504 Task* task_; | 362 Task* task_; |
505 bool* should_leak_task_; | 363 bool* should_leak_task_; |
506 static bool kTaskLeakingDefault; | 364 static bool kTaskLeakingDefault; |
507 }; | 365 }; |
508 | 366 |
509 } // namespace subtle | 367 } // namespace subtle |
510 | 368 |
511 } // namespace base | 369 } // namespace base |
512 | 370 |
513 #endif // BASE_TASK_H_ | 371 #endif // BASE_TASK_H_ |
OLD | NEW |