OLD | NEW |
---|---|
(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 // Thread-safety notes: | |
11 // | |
12 // CancellableCallback objects must be created on, posted to, cancelled on, and | |
willchan no longer on Chromium
2011/11/24 00:44:30
Cancelable
James Hawkins
2011/11/24 02:47:48
Done.
| |
13 // destroyed on the same thread. | |
14 // | |
15 // | |
16 // EXAMPLE USAGE: | |
17 // | |
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 | |
20 // to the message loop, the intensive test runs, the message loop is run, | |
21 // then the callback is cancelled. | |
22 // | |
23 // void TimeoutCallback(const std::string& timeout_message) { | |
24 // FAIL() << timeout_message; | |
25 // MessageLoop::current()->Quit(); | |
26 // } | |
27 // | |
28 // CancelableCallback timeout(base::Bind(TimeoutCallback)); | |
29 // MessageLoop::current()->PostDelayedTask(FROM_HERE, timeout.callback(), | |
30 // 4000) // 4 seconds to run. | |
31 // RunIntensiveTest(); | |
32 // MessageLoop::current()->Run(); | |
33 // timeout.Cancel(); // Hopefully this is hit before the timeout callback runs. | |
34 // | |
35 | |
36 #ifndef BASE_CANCELABLE_CALLBACK_H_ | |
37 #define BASE_CANCELABLE_CALLBACK_H_ | |
38 #pragma once | |
39 | |
40 #include "base/callback.h" | |
41 #include "base/base_export.h" | |
42 #include "base/memory/weak_ptr.h" | |
43 | |
44 namespace base { | |
45 | |
46 class BASE_EXPORT CancelableCallback { | |
willchan no longer on Chromium
2011/11/24 00:44:30
Should we inherit from NonThreadSafe here?
James Hawkins
2011/11/24 02:47:48
I'll make that change in a followup CL since it ma
| |
47 public: | |
48 CancelableCallback(); | |
49 | |
50 // |callback| must not be null. | |
51 explicit CancelableCallback(const base::Closure& callback); | |
52 | |
53 ~CancelableCallback(); | |
54 | |
55 // Cancels and drops the reference to the wrapped callback. | |
56 void Cancel(); | |
57 | |
58 // Returns true if the wrapped callback has been cancelled. | |
59 bool IsCancelled() const; | |
60 | |
61 // Sets |callback| as the closure that may be cancelled. |callback| may not | |
62 // be null. Outstanding and any previously wrapped callbacks are cancelled. | |
63 void Reset(const base::Closure& callback); | |
64 | |
65 // Returns a callback that can be disabled by calling Cancel(). | |
66 const base::Closure& callback() const; | |
67 | |
68 private: | |
69 void RunCallback(); | |
70 | |
71 // Helper method to bind |forwarder_| using a weak pointer from | |
72 // |weak_factory_|. | |
73 void InitializeForwarder(); | |
74 | |
75 // Used to ensure RunCallback() is not run when this object is destroyed. | |
76 base::WeakPtrFactory<CancelableCallback> weak_factory_; | |
77 | |
78 // The wrapper closure. | |
79 base::Closure forwarder_; | |
80 | |
81 // The stored closure that may be cancelled. | |
82 base::Closure callback_; | |
83 | |
84 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | |
85 }; | |
86 | |
87 } // namespace base | |
88 | |
89 #endif // BASE_CANCELABLE_CALLBACK_H_ | |
OLD | NEW |