]> git.sesse.net Git - casparcg/blob - dependencies64/cef/include/cef_runnable.h
* Merged html producer and updated to latest CEF version (does not have satisfactory...
[casparcg] / dependencies64 / cef / include / cef_runnable.h
1 // Copyright (c) 2013 Marshall A. Greenblatt. Portions Copyright (c)
2 // 2006-2011 Google Inc. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
18 //
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.
30 //
31 // The contents of this file are a modified extract of base/task.h
32
33 #ifndef CEF_INCLUDE_CEF_RUNNABLE_H_
34 #define CEF_INCLUDE_CEF_RUNNABLE_H_
35 #pragma once
36
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.
42 #endif
43
44 #include "include/base/cef_tuple.h"
45 #include "include/cef_base.h"
46 #include "include/cef_task.h"
47
48 // CefRunnableMethodTraits -----------------------------------------------------
49 //
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.
56 //
57 // The DISABLE_RUNNABLE_METHOD_REFCOUNT macro is provided as a convenient way
58 // for declaring a CefRunnableMethodTraits that disables refcounting.
59
60 template <class T>
61 struct CefRunnableMethodTraits {
62   CefRunnableMethodTraits() {
63   }
64
65   ~CefRunnableMethodTraits() {
66   }
67
68   void RetainCallee(T* obj) {
69 #ifndef NDEBUG
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.
73     obj->AddRef();
74     obj->Release();
75 #endif
76     obj->AddRef();
77   }
78
79   void ReleaseCallee(T* obj) {
80     obj->Release();
81   }
82 };
83
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
87 // counts.
88 //
89 // The invocation of DISABLE_RUNNABLE_METHOD_REFCOUNT should be done at the
90 // global namespace scope.  Example:
91 //
92 //   namespace foo {
93 //   class Bar {
94 //     ...
95 //   };
96 //   }  // namespace foo
97 //
98 //   DISABLE_RUNNABLE_METHOD_REFCOUNT(foo::Bar);
99 //
100 // This is different from DISALLOW_COPY_AND_ASSIGN which is declared inside the
101 // class.
102 #define DISABLE_RUNNABLE_METHOD_REFCOUNT(TypeName) \
103   template <>                                      \
104   struct CefRunnableMethodTraits<TypeName> {          \
105     void RetainCallee(TypeName* manager) {}        \
106     void ReleaseCallee(TypeName* manager) {}       \
107   }
108
109 // CefRunnableMethod and CefRunnableFunction ----------------------------------
110 //
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.
116 //
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
121 //                    want to call
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
125 //
126 // Put these all together and you get an object that can call a method whose
127 // signature is:
128 //   R T::MyFunction([A[, B]])
129 //
130 // Usage:
131 // CefPostTask(TID_UI, NewCefRunnableMethod(object, &Object::method[, a[, b]])
132 // CefPostTask(TID_UI, NewCefRunnableFunction(&function[, a[, b]])
133
134 // CefRunnableMethod and NewCefRunnableMethod implementation ------------------
135
136 template <class T, class Method, class Params>
137 class CefRunnableMethod : public CefTask {
138  public:
139   CefRunnableMethod(T* obj, Method meth, const Params& params)
140       : obj_(obj), meth_(meth), params_(params) {
141     traits_.RetainCallee(obj_);
142   }
143
144   ~CefRunnableMethod() {
145     T* obj = obj_;
146     obj_ = NULL;
147     if (obj)
148       traits_.ReleaseCallee(obj);
149   }
150
151   virtual void Execute() {
152     if (obj_)
153       DispatchToMethod(obj_, meth_, params_);
154   }
155
156  private:
157   T* obj_;
158   Method meth_;
159   Params params_;
160   CefRunnableMethodTraits<T> traits_;
161
162   IMPLEMENT_REFCOUNTING(CefRunnableMethod);
163 };
164
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());
168 }
169
170 template <class T, class Method, class A>
171 inline CefRefPtr<CefTask> NewCefRunnableMethod(T* object, Method method,
172                                                const A& a) {
173   return new CefRunnableMethod<T, Method, Tuple1<A> >(object,
174                                                       method,
175                                                       MakeTuple(a));
176 }
177
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,
182                                                          MakeTuple(a, b));
183 }
184
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,
188                                                const C& c) {
189   return new CefRunnableMethod<T, Method, Tuple3<A, B, C> >(object, method,
190                                                             MakeTuple(a, b,
191                                                                       c));
192 }
193
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,
199                                                                MakeTuple(a, b,
200                                                                          c,
201                                                                          d));
202 }
203
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,
208                                                const E& e) {
209   return new CefRunnableMethod<T,
210                                Method,
211                                Tuple5<A, B, C, D, E> >(object,
212                                                        method,
213                                                        MakeTuple(a, b, c, d,
214                                                                  e));
215 }
216
217 template <class T, class Method, class A, class B, class C, class D, class E,
218           class F>
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,
224                                Method,
225                                Tuple6<A, B, C, D, E, F> >(object,
226                                                           method,
227                                                           MakeTuple(a, b, c, d,
228                                                                     e, f));
229 }
230
231 template <class T, class Method, class A, class B, class C, class D, class E,
232           class F, class G>
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,
237                                                const G& g) {
238   return new CefRunnableMethod<T,
239                                Method,
240                                Tuple7<A, B, C, D, E, F, G> >(object,
241                                                              method,
242                                                              MakeTuple(a, b, c,
243                                                                        d, e, f,
244                                                                        g));
245 }
246
247 // CefRunnableFunction and NewCefRunnableFunction implementation --------------
248
249 template <class Function, class Params>
250 class CefRunnableFunction : public CefTask {
251  public:
252   CefRunnableFunction(Function function, const Params& params)
253       : function_(function), params_(params) {
254   }
255
256   ~CefRunnableFunction() {
257   }
258
259   virtual void Execute() {
260     if (function_)
261       DispatchToFunction(function_, params_);
262   }
263
264  private:
265   Function function_;
266   Params params_;
267
268   IMPLEMENT_REFCOUNTING(CefRunnableFunction);
269 };
270
271 template <class Function>
272 inline CefRefPtr<CefTask> NewCefRunnableFunction(Function function) {
273   return new CefRunnableFunction<Function, Tuple0>(function, MakeTuple());
274 }
275
276 template <class Function, class A>
277 inline CefRefPtr<CefTask> NewCefRunnableFunction(Function function,
278                                                  const A& a) {
279   return new CefRunnableFunction<Function, Tuple1<A> >(function, MakeTuple(a));
280 }
281
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,
286                                                           MakeTuple(a, b));
287 }
288
289 template <class Function, class A, class B, class C>
290 inline CefRefPtr<CefTask> NewCefRunnableFunction(Function function,
291                                                  const A& a, const B& b,
292                                                  const C& c) {
293   return new CefRunnableFunction<Function, Tuple3<A, B, C> >(function,
294                                                              MakeTuple(a, b,
295                                                                        c));
296 }
297
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,
303                                                                 MakeTuple(a, b,
304                                                                           c,
305                                                                           d));
306 }
307
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,
312                                                  const E& e) {
313   return new CefRunnableFunction<Function, Tuple5<A, B, C, D, E> >(function,
314       MakeTuple(a, b, c, d, e));
315 }
316
317 template <class Function, class A, class B, class C, class D, class E,
318           class F>
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));
325 }
326
327 template <class Function, class A, class B, class C, class D, class E,
328           class F, class G>
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,
333                                                  const G& g) {
334   return new CefRunnableFunction<Function, Tuple7<A, B, C, D, E, F, G> >(
335       function, MakeTuple(a, b, c, d, e, f, g));
336 }
337
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));
347 }
348
349 #endif  // CEF_INCLUDE_CEF_RUNNABLE_H_