1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2011
2 // 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 // 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
38 // Calling CancelableCallback::Cancel() brings the object back to its natural,
39 // default-constructed state, i.e., CancelableCallback::callback() will return
44 // CancelableCallback objects must be created on, posted to, cancelled on, and
45 // destroyed on the same thread.
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.
55 // void TimeoutCallback(const std::string& timeout_message) {
56 // FAIL() << timeout_message;
57 // MessageLoop::current()->QuitWhenIdle();
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.
68 #ifndef CEF_INCLUDE_BASE_CEF_CANCELABLE_CALLBACK_H_
69 #define CEF_INCLUDE_BASE_CEF_CANCELABLE_CALLBACK_H_
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
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"
95 template <typename Sig>
96 class CancelableCallback;
99 class CancelableCallback<void(void)> {
101 CancelableCallback() : weak_factory_(this) {}
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();
111 ~CancelableCallback() {}
113 // Cancels and drops the reference to the wrapped callback.
115 weak_factory_.InvalidateWeakPtrs();
120 // Returns true if the wrapped callback has been cancelled.
121 bool IsCancelled() const {
122 return callback_.is_null();
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());
130 // Outstanding tasks (e.g., posted to a message loop) must not be called.
133 // |forwarder_| is no longer valid after Cancel(), so re-bind.
134 InitializeForwarder();
136 callback_ = callback;
139 // Returns a callback that can be disabled by calling Cancel().
140 const base::Callback<void(void)>& callback() const {
149 // Helper method to bind |forwarder_| using a weak pointer from
151 void InitializeForwarder() {
152 forwarder_ = base::Bind(&CancelableCallback<void(void)>::Forward,
153 weak_factory_.GetWeakPtr());
156 // Used to ensure Forward() is not run when this object is destroyed.
157 base::WeakPtrFactory<CancelableCallback<void(void)> > weak_factory_;
159 // The wrapper closure.
160 base::Callback<void(void)> forwarder_;
162 // The stored closure that may be cancelled.
163 base::Callback<void(void)> callback_;
165 DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
168 template <typename A1>
169 class CancelableCallback<void(A1)> {
171 CancelableCallback() : weak_factory_(this) {}
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();
181 ~CancelableCallback() {}
183 // Cancels and drops the reference to the wrapped callback.
185 weak_factory_.InvalidateWeakPtrs();
190 // Returns true if the wrapped callback has been cancelled.
191 bool IsCancelled() const {
192 return callback_.is_null();
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());
200 // Outstanding tasks (e.g., posted to a message loop) must not be called.
203 // |forwarder_| is no longer valid after Cancel(), so re-bind.
204 InitializeForwarder();
206 callback_ = callback;
209 // Returns a callback that can be disabled by calling Cancel().
210 const base::Callback<void(A1)>& callback() const {
215 void Forward(A1 a1) const {
219 // Helper method to bind |forwarder_| using a weak pointer from
221 void InitializeForwarder() {
222 forwarder_ = base::Bind(&CancelableCallback<void(A1)>::Forward,
223 weak_factory_.GetWeakPtr());
226 // Used to ensure Forward() is not run when this object is destroyed.
227 base::WeakPtrFactory<CancelableCallback<void(A1)> > weak_factory_;
229 // The wrapper closure.
230 base::Callback<void(A1)> forwarder_;
232 // The stored closure that may be cancelled.
233 base::Callback<void(A1)> callback_;
235 DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
238 template <typename A1, typename A2>
239 class CancelableCallback<void(A1, A2)> {
241 CancelableCallback() : weak_factory_(this) {}
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();
251 ~CancelableCallback() {}
253 // Cancels and drops the reference to the wrapped callback.
255 weak_factory_.InvalidateWeakPtrs();
260 // Returns true if the wrapped callback has been cancelled.
261 bool IsCancelled() const {
262 return callback_.is_null();
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());
270 // Outstanding tasks (e.g., posted to a message loop) must not be called.
273 // |forwarder_| is no longer valid after Cancel(), so re-bind.
274 InitializeForwarder();
276 callback_ = callback;
279 // Returns a callback that can be disabled by calling Cancel().
280 const base::Callback<void(A1, A2)>& callback() const {
285 void Forward(A1 a1, A2 a2) const {
286 callback_.Run(a1, a2);
289 // Helper method to bind |forwarder_| using a weak pointer from
291 void InitializeForwarder() {
292 forwarder_ = base::Bind(&CancelableCallback<void(A1, A2)>::Forward,
293 weak_factory_.GetWeakPtr());
296 // Used to ensure Forward() is not run when this object is destroyed.
297 base::WeakPtrFactory<CancelableCallback<void(A1, A2)> > weak_factory_;
299 // The wrapper closure.
300 base::Callback<void(A1, A2)> forwarder_;
302 // The stored closure that may be cancelled.
303 base::Callback<void(A1, A2)> callback_;
305 DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
308 typedef CancelableCallback<void(void)> CancelableClosure;
312 #endif // !BUILDING_CEF_SHARED
314 #endif // CEF_INCLUDE_BASE_CEF_CANCELABLE_CALLBACK_H_