Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(143)

Side by Side Diff: base/cancelable_callback.h

Issue 8662047: base::Bind: Implement a 1-arity CancelableCallback and use this to implement (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review fixes. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // CancelableCallback is a wrapper around base::Callback that allows 5 // CancelableCallback is a wrapper around base::Callback that allows
6 // cancellation of a callback. CancelableCallback takes a reference on the 6 // cancellation of a callback. CancelableCallback takes a reference on the
7 // wrapped callback until this object is destroyed or Reset()/Cancel() are 7 // wrapped callback until this object is destroyed or Reset()/Cancel() are
8 // called. 8 // called.
9 // 9 //
10 // Thread-safety notes: 10 // Thread-safety notes:
11 // 11 //
12 // CancelableCallback objects must be created on, posted to, cancelled on, and 12 // CancelableCallback objects must be created on, posted to, cancelled on, and
13 // destroyed on the same thread. 13 // destroyed on the same thread.
14 // 14 //
15 // 15 //
16 // EXAMPLE USAGE: 16 // EXAMPLE USAGE:
17 // 17 //
18 // In the following example, the test is verifying that RunIntensiveTest() 18 // In the following example, the test is verifying that RunIntensiveTest()
19 // Quit()s the message loop within 4 seconds. The cancelable callback is posted 19 // Quit()s the message loop within 4 seconds. The cancelable callback is posted
20 // to the message loop, the intensive test runs, the message loop is run, 20 // to the message loop, the intensive test runs, the message loop is run,
21 // then the callback is cancelled. 21 // then the callback is cancelled.
22 // 22 //
23 // void TimeoutCallback(const std::string& timeout_message) { 23 // void TimeoutCallback(const std::string& timeout_message) {
24 // FAIL() << timeout_message; 24 // FAIL() << timeout_message;
25 // MessageLoop::current()->Quit(); 25 // MessageLoop::current()->Quit();
26 // } 26 // }
27 // 27 //
28 // CancelableCallback timeout(base::Bind(&TimeoutCallback, "Test timed out.")); 28 // CancelableClosure timeout(base::Bind(&TimeoutCallback, "Test timed out."));
29 // MessageLoop::current()->PostDelayedTask(FROM_HERE, timeout.callback(), 29 // MessageLoop::current()->PostDelayedTask(FROM_HERE, timeout.callback(),
30 // 4000) // 4 seconds to run. 30 // 4000) // 4 seconds to run.
31 // RunIntensiveTest(); 31 // RunIntensiveTest();
32 // MessageLoop::current()->Run(); 32 // MessageLoop::current()->Run();
33 // timeout.Cancel(); // Hopefully this is hit before the timeout callback runs. 33 // timeout.Cancel(); // Hopefully this is hit before the timeout callback runs.
34 // 34 //
35 35
36 #ifndef BASE_CANCELABLE_CALLBACK_H_ 36 #ifndef BASE_CANCELABLE_CALLBACK_H_
37 #define BASE_CANCELABLE_CALLBACK_H_ 37 #define BASE_CANCELABLE_CALLBACK_H_
38 #pragma once 38 #pragma once
39 39
40 #include "base/base_export.h"
41 #include "base/bind.h"
40 #include "base/callback.h" 42 #include "base/callback.h"
41 #include "base/base_export.h" 43 #include "base/callback_internal.h"
44 #include "base/compiler_specific.h"
45 #include "base/logging.h"
42 #include "base/memory/weak_ptr.h" 46 #include "base/memory/weak_ptr.h"
43 47
44 namespace base { 48 namespace base {
45 49
46 class BASE_EXPORT CancelableCallback { 50 template <typename Sig>
51 class CancelableCallback;
52
53 template <>
54 class CancelableCallback<void(void)> {
47 public: 55 public:
48 CancelableCallback(); 56 CancelableCallback() : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
49 57
50 // |callback| must not be null. 58 // |callback| must not be null.
51 explicit CancelableCallback(const base::Closure& callback); 59 explicit CancelableCallback(const base::Callback<void(void)>& callback)
60 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
61 callback_(callback) {
62 DCHECK(!callback.is_null());
63 InitializeForwarder();
64 }
52 65
53 ~CancelableCallback(); 66 ~CancelableCallback() {}
54 67
55 // Cancels and drops the reference to the wrapped callback. 68 // Cancels and drops the reference to the wrapped callback.
56 void Cancel(); 69 void Cancel() {
70 weak_factory_.InvalidateWeakPtrs();
71 callback_.Reset();
awong 2011/11/30 01:55:39 Should we also call forwarder_.Reset() here? Othe
James Hawkins 2011/11/30 03:06:03 Done.
72 }
57 73
58 // Returns true if the wrapped callback has been cancelled. 74 // Returns true if the wrapped callback has been cancelled.
59 bool IsCancelled() const; 75 bool IsCancelled() const {
76 return callback_.is_null();
77 }
60 78
61 // Sets |callback| as the closure that may be cancelled. |callback| may not 79 // Sets |callback| as the closure that may be cancelled. |callback| may not
62 // be null. Outstanding and any previously wrapped callbacks are cancelled. 80 // be null. Outstanding and any previously wrapped callbacks are cancelled.
63 void Reset(const base::Closure& callback); 81 void Reset(const base::Callback<void(void)>& callback) {
82 DCHECK(!callback.is_null());
83
84 // Outstanding tasks (e.g., posted to a message loop) must not be called.
85 Cancel();
86
87 // |forwarder_| is no longer valid after Cancel(), so re-bind.
88 InitializeForwarder();
89
90 callback_ = callback;
91 }
64 92
65 // Returns a callback that can be disabled by calling Cancel(). 93 // Returns a callback that can be disabled by calling Cancel().
66 const base::Closure& callback() const; 94 const base::Callback<void(void)>& callback() const {
95 return forwarder_;
96 }
67 97
68 private: 98 private:
69 void RunCallback(); 99 void Forward() {
100 callback_.Run();
101 }
70 102
71 // Helper method to bind |forwarder_| using a weak pointer from 103 // Helper method to bind |forwarder_| using a weak pointer from
72 // |weak_factory_|. 104 // |weak_factory_|.
73 void InitializeForwarder(); 105 void InitializeForwarder() {
106 forwarder_ = base::Bind(&CancelableCallback<void(void)>::Forward,
107 weak_factory_.GetWeakPtr());
108 }
74 109
75 // Used to ensure RunCallback() is not run when this object is destroyed. 110 // Used to ensure Forward() is not run when this object is destroyed.
76 base::WeakPtrFactory<CancelableCallback> weak_factory_; 111 base::WeakPtrFactory<CancelableCallback<void(void)> > weak_factory_;
77 112
78 // The wrapper closure. 113 // The wrapper closure.
79 base::Closure forwarder_; 114 base::Callback<void(void)> forwarder_;
80 115
81 // The stored closure that may be cancelled. 116 // The stored closure that may be cancelled.
82 base::Closure callback_; 117 base::Callback<void(void)> callback_;
83 118
84 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); 119 DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
85 }; 120 };
86 121
122 template <typename A1>
123 class CancelableCallback<void(A1)> {
124 public:
125 CancelableCallback() : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
126
127 // |callback| must not be null.
128 explicit CancelableCallback(const base::Callback<void(A1)>& callback)
129 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
130 callback_(callback) {
131 DCHECK(!callback.is_null());
132 InitializeForwarder();
133 }
134
135 ~CancelableCallback() {}
136
137 // Cancels and drops the reference to the wrapped callback.
138 void Cancel() {
139 weak_factory_.InvalidateWeakPtrs();
140 callback_.Reset();
141 }
142
143 // Returns true if the wrapped callback has been cancelled.
144 bool IsCancelled() const {
145 return callback_.is_null();
146 }
147
148 // Sets |callback| as the closure that may be cancelled. |callback| may not
149 // be null. Outstanding and any previously wrapped callbacks are cancelled.
150 void Reset(const base::Callback<void(A1)>& callback) {
151 DCHECK(!callback.is_null());
152
153 // Outstanding tasks (e.g., posted to a message loop) must not be called.
154 Cancel();
155
156 // |forwarder_| is no longer valid after Cancel(), so re-bind.
157 InitializeForwarder();
158
159 callback_ = callback;
160 }
161
162 // Returns a callback that can be disabled by calling Cancel().
163 const base::Callback<void(A1)>& callback() const {
164 return forwarder_;
165 }
166
167 private:
168 void Forward(
169 typename internal::CallbackParamTraits<A1>::ForwardType a1) const {
170 callback_.Run(a1);
171 }
172
173 // Helper method to bind |forwarder_| using a weak pointer from
174 // |weak_factory_|.
175 void InitializeForwarder() {
176 forwarder_ = base::Bind(&CancelableCallback<void(A1)>::Forward,
177 weak_factory_.GetWeakPtr());
178 }
179
180 // Used to ensure Forward() is not run when this object is destroyed.
181 base::WeakPtrFactory<CancelableCallback<void(A1)> > weak_factory_;
182
183 // The wrapper closure.
184 base::Callback<void(A1)> forwarder_;
185
186 // The stored closure that may be cancelled.
187 base::Callback<void(A1)> callback_;
188
189 DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
190 };
191
192 typedef CancelableCallback<void(void)> CancelableClosure;
193
87 } // namespace base 194 } // namespace base
88 195
89 #endif // BASE_CANCELABLE_CALLBACK_H_ 196 #endif // BASE_CANCELABLE_CALLBACK_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698