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

Unified Diff: base/cancelable_callback.h

Issue 14600025: Replace AudioSilenceDetector with an AudioPowerMonitor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use CancelableCallback instead of extra-task for close reply. Created 7 years, 5 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/float_util.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/cancelable_callback.h
diff --git a/base/cancelable_callback.h b/base/cancelable_callback.h
index 1cfcf2b6d6593f7711092e64f38516c642b94058..8ef01996a93922f1befad24e6be8bd40d29bdbdb 100644
--- a/base/cancelable_callback.h
+++ b/base/cancelable_callback.h
@@ -195,6 +195,76 @@ class CancelableCallback<void(A1)> {
DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
};
+template <typename A1, typename A2>
+class CancelableCallback<void(A1, A2)> {
+ public:
+ CancelableCallback() : weak_factory_(this) {}
+
+ // |callback| must not be null.
+ explicit CancelableCallback(const base::Callback<void(A1, A2)>& callback)
+ : weak_factory_(this),
+ callback_(callback) {
+ DCHECK(!callback.is_null());
+ InitializeForwarder();
+ }
+
+ ~CancelableCallback() {}
+
+ // Cancels and drops the reference to the wrapped callback.
+ void Cancel() {
+ weak_factory_.InvalidateWeakPtrs();
+ forwarder_.Reset();
+ callback_.Reset();
+ }
+
+ // Returns true if the wrapped callback has been cancelled.
+ bool IsCancelled() const {
+ return callback_.is_null();
+ }
+
+ // Sets |callback| as the closure that may be cancelled. |callback| may not
+ // be null. Outstanding and any previously wrapped callbacks are cancelled.
+ void Reset(const base::Callback<void(A1, A2)>& callback) {
+ DCHECK(!callback.is_null());
+
+ // Outstanding tasks (e.g., posted to a message loop) must not be called.
+ Cancel();
+
+ // |forwarder_| is no longer valid after Cancel(), so re-bind.
+ InitializeForwarder();
+
+ callback_ = callback;
+ }
+
+ // Returns a callback that can be disabled by calling Cancel().
+ const base::Callback<void(A1, A2)>& callback() const {
+ return forwarder_;
+ }
+
+ private:
+ void Forward(A1 a1, A2 a2) const {
+ callback_.Run(a1, a2);
+ }
+
+ // Helper method to bind |forwarder_| using a weak pointer from
+ // |weak_factory_|.
+ void InitializeForwarder() {
+ forwarder_ = base::Bind(&CancelableCallback<void(A1, A2)>::Forward,
+ weak_factory_.GetWeakPtr());
+ }
+
+ // Used to ensure Forward() is not run when this object is destroyed.
+ base::WeakPtrFactory<CancelableCallback<void(A1, A2)> > weak_factory_;
+
+ // The wrapper closure.
+ base::Callback<void(A1, A2)> forwarder_;
+
+ // The stored closure that may be cancelled.
+ base::Callback<void(A1, A2)> callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
+};
+
typedef CancelableCallback<void(void)> CancelableClosure;
} // namespace base
« no previous file with comments | « no previous file | base/float_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698