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 the callback. | |
7 // | |
8 // EXAMPLE USAGE: | |
9 // | |
10 // In the following example, the Fetcher object cancels an outstanding callback | |
11 // posted to the MessageLoop on destruction so the fetch operation does not | |
12 // occur after the object has been destroyed. | |
13 // | |
14 // class Fetcher { | |
15 // public: | |
16 // ~Fetcher() { | |
17 // // The system is shutting down; cancel the fetch operation. | |
18 // if (!cancelable_.is_null()) | |
19 // cancelable_.Cancel(); | |
20 // } | |
21 // | |
22 // // Asynchronously fetches data. | |
23 // Fetch(const base::Closure& callback) { | |
24 // cancelable_.Reset(base::Bind(&Fetcher::FetchNow, base::Unretained(this), | |
25 // callback)); | |
26 // MessageLoop::current()->PostTask(FROM_HERE, cancelable_.callback()); | |
27 // } | |
28 // | |
29 // private: | |
30 // // Must be called on the IO thread. | |
31 // FetchNow(const base::Closure& callback) { | |
32 // ... | |
33 // } | |
34 // | |
35 // // Used to cancel the callback on destruction. | |
36 // CancelableCallback cancelable_; | |
37 // }; | |
awong
2011/11/23 23:41:07
This use case looks exactly like what you'd use a
James Hawkins
2011/11/24 00:33:45
Done.
| |
38 // | |
39 | |
40 #ifndef BASE_CANCELABLE_CALLBACK_H_ | |
41 #define BASE_CANCELABLE_CALLBACK_H_ | |
42 #pragma once | |
43 | |
44 #include "base/callback.h" | |
45 #include "base/base_export.h" | |
46 #include "base/memory/weak_ptr.h" | |
47 | |
48 namespace base { | |
49 | |
50 // Wrapper used to cancel a callback. | |
awong
2011/11/23 23:41:07
nit: The file comment already covers this. Remove
James Hawkins
2011/11/24 00:11:15
Done.
| |
51 class BASE_EXPORT CancelableCallback { | |
52 public: | |
53 CancelableCallback(); | |
54 | |
55 // |callback| must not be null. | |
56 explicit CancelableCallback(const base::Closure& callback); | |
57 | |
58 ~CancelableCallback(); | |
59 | |
60 // Cancels the stored callback. | |
awong
2011/11/23 23:41:07
This should also specify that it's going to drop r
James Hawkins
2011/11/24 00:11:15
Done.
| |
61 void Cancel(); | |
62 | |
63 // Sets |callback| as the closure that may be cancelled. |callback| may not | |
64 // be null. Outstanding and any previously wrapped callbacks are reset. | |
awong
2011/11/23 23:41:07
This this be "are reset" or "are canceled?"
James Hawkins
2011/11/24 00:11:15
Done.
| |
65 void Reset(const base::Closure& callback); | |
66 | |
67 // Returns a callback that can be disabled by calling Cancel(). | |
68 const base::Closure& callback() const; | |
69 | |
70 // Returns true if the wrapped callback is null. | |
awong
2011/11/23 23:41:07
This isn't true is it? It's if the CancelableCall
James Hawkins
2011/11/24 00:11:15
In the current implementation it is the former (wh
| |
71 bool is_null() const; | |
72 | |
73 private: | |
74 // Closure that runs the stored callback. | |
awong
2011/11/23 23:41:07
Comment is too close to function name. remove?
James Hawkins
2011/11/24 00:11:15
Done.
| |
75 void RunCallback(); | |
76 | |
77 // Helper method to bind |forwarder_| using a weak pointer from | |
78 // |weak_factory_|. | |
awong
2011/11/23 23:41:07
nit: How about InitializeForwarder instead of Bind
James Hawkins
2011/11/24 00:11:15
Done.
| |
79 void BindForwarder(); | |
80 | |
81 // Used to ensure RunCallback() is not run when this object is destroyed. | |
82 base::WeakPtrFactory<CancelableCallback> weak_factory_; | |
83 | |
84 // The wrapper closure. | |
85 base::Closure forwarder_; | |
86 | |
87 // The stored closure that may be cancelled. | |
88 base::Closure callback_; | |
89 | |
90 DISALLOW_COPY_AND_ASSIGN(CancelableCallback); | |
91 }; | |
92 | |
93 } // namespace base | |
94 | |
95 #endif // BASE_CANCELABLE_CALLBACK_H_ | |
OLD | NEW |