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

Side by Side Diff: base/cancelable_callback.h

Issue 1647803004: Move base to DEPS (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 months 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
« no previous file with comments | « base/callback_unittest.nc ('k') | base/cancelable_callback_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4 //
5 // CancelableCallback is a wrapper around base::Callback that allows
6 // cancellation of a callback. CancelableCallback takes a reference on the
7 // wrapped callback until this object is destroyed or Reset()/Cancel() are
8 // called.
9 //
10 // NOTE:
11 //
12 // Calling CancelableCallback::Cancel() brings the object back to its natural,
13 // default-constructed state, i.e., CancelableCallback::callback() will return
14 // a null callback.
15 //
16 // THREAD-SAFETY:
17 //
18 // CancelableCallback objects must be created on, posted to, cancelled on, and
19 // destroyed on the same thread.
20 //
21 //
22 // EXAMPLE USAGE:
23 //
24 // In the following example, the test is verifying that RunIntensiveTest()
25 // Quit()s the message loop within 4 seconds. The cancelable callback is posted
26 // to the message loop, the intensive test runs, the message loop is run,
27 // then the callback is cancelled.
28 //
29 // void TimeoutCallback(const std::string& timeout_message) {
30 // FAIL() << timeout_message;
31 // MessageLoop::current()->QuitWhenIdle();
32 // }
33 //
34 // CancelableClosure timeout(base::Bind(&TimeoutCallback, "Test timed out."));
35 // MessageLoop::current()->PostDelayedTask(FROM_HERE, timeout.callback(),
36 // 4000) // 4 seconds to run.
37 // RunIntensiveTest();
38 // MessageLoop::current()->Run();
39 // timeout.Cancel(); // Hopefully this is hit before the timeout callback runs.
40 //
41
42 #ifndef BASE_CANCELABLE_CALLBACK_H_
43 #define BASE_CANCELABLE_CALLBACK_H_
44
45 #include "base/base_export.h"
46 #include "base/bind.h"
47 #include "base/callback.h"
48 #include "base/callback_internal.h"
49 #include "base/compiler_specific.h"
50 #include "base/logging.h"
51 #include "base/memory/weak_ptr.h"
52
53 namespace base {
54
55 template <typename Sig>
56 class CancelableCallback;
57
58 template <typename... A>
59 class CancelableCallback<void(A...)> {
60 public:
61 CancelableCallback() : weak_factory_(this) {}
62
63 // |callback| must not be null.
64 explicit CancelableCallback(const base::Callback<void(A...)>& callback)
65 : callback_(callback), weak_factory_(this) {
66 DCHECK(!callback.is_null());
67 InitializeForwarder();
68 }
69
70 ~CancelableCallback() {}
71
72 // Cancels and drops the reference to the wrapped callback.
73 void Cancel() {
74 weak_factory_.InvalidateWeakPtrs();
75 forwarder_.Reset();
76 callback_.Reset();
77 }
78
79 // Returns true if the wrapped callback has been cancelled.
80 bool IsCancelled() const {
81 return callback_.is_null();
82 }
83
84 // Sets |callback| as the closure that may be cancelled. |callback| may not
85 // be null. Outstanding and any previously wrapped callbacks are cancelled.
86 void Reset(const base::Callback<void(A...)>& callback) {
87 DCHECK(!callback.is_null());
88
89 // Outstanding tasks (e.g., posted to a message loop) must not be called.
90 Cancel();
91
92 // |forwarder_| is no longer valid after Cancel(), so re-bind.
93 InitializeForwarder();
94
95 callback_ = callback;
96 }
97
98 // Returns a callback that can be disabled by calling Cancel().
99 const base::Callback<void(A...)>& callback() const {
100 return forwarder_;
101 }
102
103 private:
104 void Forward(A... args) const {
105 callback_.Run(args...);
106 }
107
108 // Helper method to bind |forwarder_| using a weak pointer from
109 // |weak_factory_|.
110 void InitializeForwarder() {
111 forwarder_ = base::Bind(&CancelableCallback<void(A...)>::Forward,
112 weak_factory_.GetWeakPtr());
113 }
114
115 // The wrapper closure.
116 base::Callback<void(A...)> forwarder_;
117
118 // The stored closure that may be cancelled.
119 base::Callback<void(A...)> callback_;
120
121 // Used to ensure Forward() is not run when this object is destroyed.
122 base::WeakPtrFactory<CancelableCallback<void(A...)>> weak_factory_;
123
124 DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
125 };
126
127 typedef CancelableCallback<void(void)> CancelableClosure;
128
129 } // namespace base
130
131 #endif // BASE_CANCELABLE_CALLBACK_H_
OLDNEW
« no previous file with comments | « base/callback_unittest.nc ('k') | base/cancelable_callback_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698