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

Unified Diff: base/cancelable_callback.h

Issue 8673008: base::Bind: Implement CancelableCallback to replace CancelableTaske. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Test build fix. Created 9 years, 1 month 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 | « base/callback_unittest.cc ('k') | base/cancelable_callback.cc » ('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
new file mode 100644
index 0000000000000000000000000000000000000000..7f423bab69d0ae371107bc179717343976d19689
--- /dev/null
+++ b/base/cancelable_callback.h
@@ -0,0 +1,89 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+// CancelableCallback is a wrapper around base::Callback that allows
+// cancellation of a callback. CancelableCallback takes a reference on the
+// wrapped callback until this object is destroyed or Reset()/Cancel() are
+// called.
+//
+// Thread-safety notes:
+//
+// CancelableCallback objects must be created on, posted to, cancelled on, and
+// destroyed on the same thread.
+//
+//
+// EXAMPLE USAGE:
+//
+// In the following example, the test is verifying that RunIntensiveTest()
+// Quit()s the message loop within 4 seconds. The cancelable callback is posted
+// to the message loop, the intensive test runs, the message loop is run,
+// then the callback is cancelled.
+//
+// void TimeoutCallback(const std::string& timeout_message) {
+// FAIL() << timeout_message;
+// MessageLoop::current()->Quit();
+// }
+//
+// CancelableCallback timeout(base::Bind(TimeoutCallback));
+// MessageLoop::current()->PostDelayedTask(FROM_HERE, timeout.callback(),
+// 4000) // 4 seconds to run.
+// RunIntensiveTest();
+// MessageLoop::current()->Run();
+// timeout.Cancel(); // Hopefully this is hit before the timeout callback runs.
+//
+
+#ifndef BASE_CANCELABLE_CALLBACK_H_
+#define BASE_CANCELABLE_CALLBACK_H_
+#pragma once
+
+#include "base/callback.h"
+#include "base/base_export.h"
+#include "base/memory/weak_ptr.h"
+
+namespace base {
+
+class BASE_EXPORT CancelableCallback {
+ public:
+ CancelableCallback();
+
+ // |callback| must not be null.
+ explicit CancelableCallback(const base::Closure& callback);
+
+ ~CancelableCallback();
+
+ // Cancels and drops the reference to the wrapped callback.
+ void Cancel();
+
+ // Returns true if the wrapped callback has been cancelled.
+ bool IsCancelled() const;
+
+ // 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::Closure& callback);
+
+ // Returns a callback that can be disabled by calling Cancel().
+ const base::Closure& callback() const;
+
+ private:
+ void RunCallback();
+
+ // Helper method to bind |forwarder_| using a weak pointer from
+ // |weak_factory_|.
+ void InitializeForwarder();
+
+ // Used to ensure RunCallback() is not run when this object is destroyed.
+ base::WeakPtrFactory<CancelableCallback> weak_factory_;
+
+ // The wrapper closure.
+ base::Closure forwarder_;
+
+ // The stored closure that may be cancelled.
+ base::Closure callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(CancelableCallback);
+};
+
+} // namespace base
+
+#endif // BASE_CANCELABLE_CALLBACK_H_
« no previous file with comments | « base/callback_unittest.cc ('k') | base/cancelable_callback.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698