]> git.sesse.net Git - casparcg/blob - dependencies64/cef/include/base/cef_cancelable_callback.h
* Merged html producer and updated to latest CEF version (does not have satisfactory...
[casparcg] / dependencies64 / cef / include / base / cef_cancelable_callback.h
1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2011
2 // 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 // CancelableCallback is a wrapper around base::Callback that allows
32 // cancellation of a callback. CancelableCallback takes a reference on the
33 // wrapped callback until this object is destroyed or Reset()/Cancel() are
34 // called.
35 //
36 // NOTE:
37 //
38 // Calling CancelableCallback::Cancel() brings the object back to its natural,
39 // default-constructed state, i.e., CancelableCallback::callback() will return
40 // a null callback.
41 //
42 // THREAD-SAFETY:
43 //
44 // CancelableCallback objects must be created on, posted to, cancelled on, and
45 // destroyed on the same thread.
46 //
47 //
48 // EXAMPLE USAGE:
49 //
50 // In the following example, the test is verifying that RunIntensiveTest()
51 // Quit()s the message loop within 4 seconds. The cancelable callback is posted
52 // to the message loop, the intensive test runs, the message loop is run,
53 // then the callback is cancelled.
54 //
55 // void TimeoutCallback(const std::string& timeout_message) {
56 //   FAIL() << timeout_message;
57 //   MessageLoop::current()->QuitWhenIdle();
58 // }
59 //
60 // CancelableClosure timeout(base::Bind(&TimeoutCallback, "Test timed out."));
61 // MessageLoop::current()->PostDelayedTask(FROM_HERE, timeout.callback(),
62 //                                         4000)  // 4 seconds to run.
63 // RunIntensiveTest();
64 // MessageLoop::current()->Run();
65 // timeout.Cancel();  // Hopefully this is hit before the timeout callback runs.
66 //
67
68 #ifndef CEF_INCLUDE_BASE_CEF_CANCELABLE_CALLBACK_H_
69 #define CEF_INCLUDE_BASE_CEF_CANCELABLE_CALLBACK_H_
70 #pragma once
71
72 #if defined(BASE_CANCELABLE_CALLBACK_H_)
73 // Do nothing if the Chromium header has already been included.
74 // This can happen in cases where Chromium code is used directly by the
75 // client application. When using Chromium code directly always include
76 // the Chromium header first to avoid type conflicts.
77 #elif defined(BUILDING_CEF_SHARED)
78 // When building CEF include the Chromium header directly.
79 #include "base/cancelable_callback.h"
80 #else  // !BUILDING_CEF_SHARED
81 // The following is substantially similar to the Chromium implementation.
82 // If the Chromium implementation diverges the below implementation should be
83 // updated to match.
84
85 #include "include/base/cef_bind.h"
86 #include "include/base/cef_callback.h"
87 #include "include/base/cef_build.h"
88 #include "include/base/cef_logging.h"
89 #include "include/base/cef_macros.h"
90 #include "include/base/cef_weak_ptr.h"
91 #include "include/base/internal/cef_callback_internal.h"
92
93 namespace base {
94
95 template <typename Sig>
96 class CancelableCallback;
97
98 template <>
99 class CancelableCallback<void(void)> {
100  public:
101   CancelableCallback() : weak_factory_(this) {}
102
103   // |callback| must not be null.
104   explicit CancelableCallback(const base::Callback<void(void)>& callback)
105       : weak_factory_(this),
106         callback_(callback) {
107     DCHECK(!callback.is_null());
108     InitializeForwarder();
109   }
110
111   ~CancelableCallback() {}
112
113   // Cancels and drops the reference to the wrapped callback.
114   void Cancel() {
115     weak_factory_.InvalidateWeakPtrs();
116     forwarder_.Reset();
117     callback_.Reset();
118   }
119
120   // Returns true if the wrapped callback has been cancelled.
121   bool IsCancelled() const {
122     return callback_.is_null();
123   }
124
125   // Sets |callback| as the closure that may be cancelled. |callback| may not
126   // be null. Outstanding and any previously wrapped callbacks are cancelled.
127   void Reset(const base::Callback<void(void)>& callback) {
128     DCHECK(!callback.is_null());
129
130     // Outstanding tasks (e.g., posted to a message loop) must not be called.
131     Cancel();
132
133     // |forwarder_| is no longer valid after Cancel(), so re-bind.
134     InitializeForwarder();
135
136     callback_ = callback;
137   }
138
139   // Returns a callback that can be disabled by calling Cancel().
140   const base::Callback<void(void)>& callback() const {
141     return forwarder_;
142   }
143
144  private:
145   void Forward() {
146     callback_.Run();
147   }
148
149   // Helper method to bind |forwarder_| using a weak pointer from
150   // |weak_factory_|.
151   void InitializeForwarder() {
152     forwarder_ = base::Bind(&CancelableCallback<void(void)>::Forward,
153                             weak_factory_.GetWeakPtr());
154   }
155
156   // Used to ensure Forward() is not run when this object is destroyed.
157   base::WeakPtrFactory<CancelableCallback<void(void)> > weak_factory_;
158
159   // The wrapper closure.
160   base::Callback<void(void)> forwarder_;
161
162   // The stored closure that may be cancelled.
163   base::Callback<void(void)> callback_;
164
165   DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
166 };
167
168 template <typename A1>
169 class CancelableCallback<void(A1)> {
170  public:
171   CancelableCallback() : weak_factory_(this) {}
172
173   // |callback| must not be null.
174   explicit CancelableCallback(const base::Callback<void(A1)>& callback)
175       : weak_factory_(this),
176         callback_(callback) {
177     DCHECK(!callback.is_null());
178     InitializeForwarder();
179   }
180
181   ~CancelableCallback() {}
182
183   // Cancels and drops the reference to the wrapped callback.
184   void Cancel() {
185     weak_factory_.InvalidateWeakPtrs();
186     forwarder_.Reset();
187     callback_.Reset();
188   }
189
190   // Returns true if the wrapped callback has been cancelled.
191   bool IsCancelled() const {
192     return callback_.is_null();
193   }
194
195   // Sets |callback| as the closure that may be cancelled. |callback| may not
196   // be null. Outstanding and any previously wrapped callbacks are cancelled.
197   void Reset(const base::Callback<void(A1)>& callback) {
198     DCHECK(!callback.is_null());
199
200     // Outstanding tasks (e.g., posted to a message loop) must not be called.
201     Cancel();
202
203     // |forwarder_| is no longer valid after Cancel(), so re-bind.
204     InitializeForwarder();
205
206     callback_ = callback;
207   }
208
209   // Returns a callback that can be disabled by calling Cancel().
210   const base::Callback<void(A1)>& callback() const {
211     return forwarder_;
212   }
213
214  private:
215   void Forward(A1 a1) const {
216     callback_.Run(a1);
217   }
218
219   // Helper method to bind |forwarder_| using a weak pointer from
220   // |weak_factory_|.
221   void InitializeForwarder() {
222     forwarder_ = base::Bind(&CancelableCallback<void(A1)>::Forward,
223                             weak_factory_.GetWeakPtr());
224   }
225
226   // Used to ensure Forward() is not run when this object is destroyed.
227   base::WeakPtrFactory<CancelableCallback<void(A1)> > weak_factory_;
228
229   // The wrapper closure.
230   base::Callback<void(A1)> forwarder_;
231
232   // The stored closure that may be cancelled.
233   base::Callback<void(A1)> callback_;
234
235   DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
236 };
237
238 template <typename A1, typename A2>
239 class CancelableCallback<void(A1, A2)> {
240  public:
241   CancelableCallback() : weak_factory_(this) {}
242
243   // |callback| must not be null.
244   explicit CancelableCallback(const base::Callback<void(A1, A2)>& callback)
245       : weak_factory_(this),
246         callback_(callback) {
247     DCHECK(!callback.is_null());
248     InitializeForwarder();
249   }
250
251   ~CancelableCallback() {}
252
253   // Cancels and drops the reference to the wrapped callback.
254   void Cancel() {
255     weak_factory_.InvalidateWeakPtrs();
256     forwarder_.Reset();
257     callback_.Reset();
258   }
259
260   // Returns true if the wrapped callback has been cancelled.
261   bool IsCancelled() const {
262     return callback_.is_null();
263   }
264
265   // Sets |callback| as the closure that may be cancelled. |callback| may not
266   // be null. Outstanding and any previously wrapped callbacks are cancelled.
267   void Reset(const base::Callback<void(A1, A2)>& callback) {
268     DCHECK(!callback.is_null());
269
270     // Outstanding tasks (e.g., posted to a message loop) must not be called.
271     Cancel();
272
273     // |forwarder_| is no longer valid after Cancel(), so re-bind.
274     InitializeForwarder();
275
276     callback_ = callback;
277   }
278
279   // Returns a callback that can be disabled by calling Cancel().
280   const base::Callback<void(A1, A2)>& callback() const {
281     return forwarder_;
282   }
283
284  private:
285   void Forward(A1 a1, A2 a2) const {
286     callback_.Run(a1, a2);
287   }
288
289   // Helper method to bind |forwarder_| using a weak pointer from
290   // |weak_factory_|.
291   void InitializeForwarder() {
292     forwarder_ = base::Bind(&CancelableCallback<void(A1, A2)>::Forward,
293                             weak_factory_.GetWeakPtr());
294   }
295
296   // Used to ensure Forward() is not run when this object is destroyed.
297   base::WeakPtrFactory<CancelableCallback<void(A1, A2)> > weak_factory_;
298
299   // The wrapper closure.
300   base::Callback<void(A1, A2)> forwarder_;
301
302   // The stored closure that may be cancelled.
303   base::Callback<void(A1, A2)> callback_;
304
305   DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
306 };
307
308 typedef CancelableCallback<void(void)> CancelableClosure;
309
310 }  // namespace base
311
312 #endif  // !BUILDING_CEF_SHARED
313
314 #endif  // CEF_INCLUDE_BASE_CEF_CANCELABLE_CALLBACK_H_