1 // Copyright (c) 2013 Marshall A. Greenblatt. Portions Copyright (c)
2 // 2006-2011 Google Inc. All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the name Chromium Embedded
15 // Framework nor the names of its contributors may be used to endorse
16 // or promote products derived from this software without specific prior
17 // written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // The contents of this file are a modified extract of base/task.h
33 #ifndef CEF_INCLUDE_CEF_RUNNABLE_H_
34 #define CEF_INCLUDE_CEF_RUNNABLE_H_
37 #if defined(BUILDING_CEF_SHARED)
38 // The implementation of cef_runnable.h depends on an obsolete version of
39 // base/tuple.h that is implemented by cef_tuple.h for client applications but
40 // is not compatible with the version used when building Chromium/CEF.
41 #error This header cannot be used when building Chromium/CEF.
44 #include "include/base/cef_tuple.h"
45 #include "include/cef_base.h"
46 #include "include/cef_task.h"
48 // CefRunnableMethodTraits -----------------------------------------------------
50 // This traits-class is used by CefRunnableMethod to manage the lifetime of the
51 // callee object. By default, it is assumed that the callee supports AddRef
52 // and Release methods. A particular class can specialize this template to
53 // define other lifetime management. For example, if the callee is known to
54 // live longer than the CefRunnableMethod object, then a CefRunnableMethodTraits
55 // struct could be defined with empty RetainCallee and ReleaseCallee methods.
57 // The DISABLE_RUNNABLE_METHOD_REFCOUNT macro is provided as a convenient way
58 // for declaring a CefRunnableMethodTraits that disables refcounting.
61 struct CefRunnableMethodTraits {
62 CefRunnableMethodTraits() {
65 ~CefRunnableMethodTraits() {
68 void RetainCallee(T* obj) {
70 // Catch NewCefRunnableMethod being called in an object's constructor.
71 // This isn't safe since the method can be invoked before the constructor
72 // completes, causing the object to be deleted.
79 void ReleaseCallee(T* obj) {
84 // Convenience macro for declaring a CefRunnableMethodTraits that disables
85 // refcounting of a class. This is useful if you know that the callee
86 // will outlive the CefRunnableMethod object and thus do not need the ref
89 // The invocation of DISABLE_RUNNABLE_METHOD_REFCOUNT should be done at the
90 // global namespace scope. Example:
98 // DISABLE_RUNNABLE_METHOD_REFCOUNT(foo::Bar);
100 // This is different from DISALLOW_COPY_AND_ASSIGN which is declared inside the
102 #define DISABLE_RUNNABLE_METHOD_REFCOUNT(TypeName) \
104 struct CefRunnableMethodTraits<TypeName> { \
105 void RetainCallee(TypeName* manager) {} \
106 void ReleaseCallee(TypeName* manager) {} \
109 // CefRunnableMethod and CefRunnableFunction ----------------------------------
111 // CefRunnable methods are a type of task that call a function on an object
112 // when they are run. We implement both an object and a set of
113 // NewCefRunnableMethod and NewCefRunnableFunction functions for convenience.
114 // These functions are overloaded and will infer the template types,
115 // simplifying calling code.
117 // The template definitions all use the following names:
118 // T - the class type of the object you're supplying
119 // this is not needed for the Static version of the call
120 // Method/Function - the signature of a pointer to the method or function you
122 // Param - the parameter(s) to the method, possibly packed as a Tuple
123 // A - the first parameter (if any) to the method
124 // B - the second parameter (if any) to the method
126 // Put these all together and you get an object that can call a method whose
128 // R T::MyFunction([A[, B]])
131 // CefPostTask(TID_UI, NewCefRunnableMethod(object, &Object::method[, a[, b]])
132 // CefPostTask(TID_UI, NewCefRunnableFunction(&function[, a[, b]])
134 // CefRunnableMethod and NewCefRunnableMethod implementation ------------------
136 template <class T, class Method, class Params>
137 class CefRunnableMethod : public CefTask {
139 CefRunnableMethod(T* obj, Method meth, const Params& params)
140 : obj_(obj), meth_(meth), params_(params) {
141 traits_.RetainCallee(obj_);
144 ~CefRunnableMethod() {
148 traits_.ReleaseCallee(obj);
151 virtual void Execute() {
153 DispatchToMethod(obj_, meth_, params_);
160 CefRunnableMethodTraits<T> traits_;
162 IMPLEMENT_REFCOUNTING(CefRunnableMethod);
165 template <class T, class Method>
166 inline CefRefPtr<CefTask> NewCefRunnableMethod(T* object, Method method) {
167 return new CefRunnableMethod<T, Method, Tuple0>(object, method, MakeTuple());
170 template <class T, class Method, class A>
171 inline CefRefPtr<CefTask> NewCefRunnableMethod(T* object, Method method,
173 return new CefRunnableMethod<T, Method, Tuple1<A> >(object,
178 template <class T, class Method, class A, class B>
179 inline CefRefPtr<CefTask> NewCefRunnableMethod(T* object, Method method,
180 const A& a, const B& b) {
181 return new CefRunnableMethod<T, Method, Tuple2<A, B> >(object, method,
185 template <class T, class Method, class A, class B, class C>
186 inline CefRefPtr<CefTask> NewCefRunnableMethod(T* object, Method method,
187 const A& a, const B& b,
189 return new CefRunnableMethod<T, Method, Tuple3<A, B, C> >(object, method,
194 template <class T, class Method, class A, class B, class C, class D>
195 inline CefRefPtr<CefTask> NewCefRunnableMethod(T* object, Method method,
196 const A& a, const B& b,
197 const C& c, const D& d) {
198 return new CefRunnableMethod<T, Method, Tuple4<A, B, C, D> >(object, method,
204 template <class T, class Method, class A, class B, class C, class D, class E>
205 inline CefRefPtr<CefTask> NewCefRunnableMethod(T* object, Method method,
206 const A& a, const B& b,
207 const C& c, const D& d,
209 return new CefRunnableMethod<T,
211 Tuple5<A, B, C, D, E> >(object,
213 MakeTuple(a, b, c, d,
217 template <class T, class Method, class A, class B, class C, class D, class E,
219 inline CefRefPtr<CefTask> NewCefRunnableMethod(T* object, Method method,
220 const A& a, const B& b,
221 const C& c, const D& d,
222 const E& e, const F& f) {
223 return new CefRunnableMethod<T,
225 Tuple6<A, B, C, D, E, F> >(object,
227 MakeTuple(a, b, c, d,
231 template <class T, class Method, class A, class B, class C, class D, class E,
233 inline CefRefPtr<CefTask> NewCefRunnableMethod(T* object, Method method,
234 const A& a, const B& b,
235 const C& c, const D& d,
236 const E& e, const F& f,
238 return new CefRunnableMethod<T,
240 Tuple7<A, B, C, D, E, F, G> >(object,
247 // CefRunnableFunction and NewCefRunnableFunction implementation --------------
249 template <class Function, class Params>
250 class CefRunnableFunction : public CefTask {
252 CefRunnableFunction(Function function, const Params& params)
253 : function_(function), params_(params) {
256 ~CefRunnableFunction() {
259 virtual void Execute() {
261 DispatchToFunction(function_, params_);
268 IMPLEMENT_REFCOUNTING(CefRunnableFunction);
271 template <class Function>
272 inline CefRefPtr<CefTask> NewCefRunnableFunction(Function function) {
273 return new CefRunnableFunction<Function, Tuple0>(function, MakeTuple());
276 template <class Function, class A>
277 inline CefRefPtr<CefTask> NewCefRunnableFunction(Function function,
279 return new CefRunnableFunction<Function, Tuple1<A> >(function, MakeTuple(a));
282 template <class Function, class A, class B>
283 inline CefRefPtr<CefTask> NewCefRunnableFunction(Function function,
284 const A& a, const B& b) {
285 return new CefRunnableFunction<Function, Tuple2<A, B> >(function,
289 template <class Function, class A, class B, class C>
290 inline CefRefPtr<CefTask> NewCefRunnableFunction(Function function,
291 const A& a, const B& b,
293 return new CefRunnableFunction<Function, Tuple3<A, B, C> >(function,
298 template <class Function, class A, class B, class C, class D>
299 inline CefRefPtr<CefTask> NewCefRunnableFunction(Function function,
300 const A& a, const B& b,
301 const C& c, const D& d) {
302 return new CefRunnableFunction<Function, Tuple4<A, B, C, D> >(function,
308 template <class Function, class A, class B, class C, class D, class E>
309 inline CefRefPtr<CefTask> NewCefRunnableFunction(Function function,
310 const A& a, const B& b,
311 const C& c, const D& d,
313 return new CefRunnableFunction<Function, Tuple5<A, B, C, D, E> >(function,
314 MakeTuple(a, b, c, d, e));
317 template <class Function, class A, class B, class C, class D, class E,
319 inline CefRefPtr<CefTask> NewCefRunnableFunction(Function function,
320 const A& a, const B& b,
321 const C& c, const D& d,
322 const E& e, const F& f) {
323 return new CefRunnableFunction<Function, Tuple6<A, B, C, D, E, F> >(function,
324 MakeTuple(a, b, c, d, e, f));
327 template <class Function, class A, class B, class C, class D, class E,
329 inline CefRefPtr<CefTask> NewCefRunnableFunction(Function function,
330 const A& a, const B& b,
331 const C& c, const D& d,
332 const E& e, const F& f,
334 return new CefRunnableFunction<Function, Tuple7<A, B, C, D, E, F, G> >(
335 function, MakeTuple(a, b, c, d, e, f, g));
338 template <class Function, class A, class B, class C, class D, class E,
339 class F, class G, class H>
340 inline CefRefPtr<CefTask> NewCefRunnableFunction(Function function,
341 const A& a, const B& b,
342 const C& c, const D& d,
343 const E& e, const F& f,
344 const G& g, const H& h) {
345 return new CefRunnableFunction<Function, Tuple8<A, B, C, D, E, F, G, H> >(
346 function, MakeTuple(a, b, c, d, e, f, g, h));
349 #endif // CEF_INCLUDE_CEF_RUNNABLE_H_