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

Unified Diff: base/cancelable_callback.cc

Issue 8673008: base::Bind: Implement CancelableCallback to replace CancelableTaske. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Moar. 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
Index: base/cancelable_callback.cc
diff --git a/base/cancelable_callback.cc b/base/cancelable_callback.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d132973739ad76f752d65a41db07046f10b3a1be
--- /dev/null
+++ b/base/cancelable_callback.cc
@@ -0,0 +1,60 @@
+// 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.
+
+#include "base/cancelable_callback.h"
+
+#include "base/bind.h"
+#include "base/compiler_specific.h"
+#include "base/logging.h"
+
+namespace base {
+
+CancelableCallback::CancelableCallback()
+ : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
+}
+
+CancelableCallback::CancelableCallback(const base::Closure& callback)
+ : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
+ callback_(callback) {
+ DCHECK_EQ(false, callback.is_null());
willchan no longer on Chromium 2011/11/24 00:44:30 Nit: Is there a reason you prefer DCHECK_EQ (false
James Hawkins 2011/11/24 02:47:48 It's more explicit; however, I don't care to push
+ InitializeForwarder();
+}
+
+CancelableCallback::~CancelableCallback() {}
+
+void CancelableCallback::Cancel() {
+ weak_factory_.InvalidateWeakPtrs();
+ callback_.Reset();
+}
+
+bool CancelableCallback::IsCancelled() const {
+ return callback_.is_null();
+}
+
+void CancelableCallback::Reset(const base::Closure& callback) {
+ DCHECK_EQ(false, 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;
+}
+
+const base::Closure& CancelableCallback::callback() const {
+ return forwarder_;
+}
+
+void CancelableCallback::RunCallback() {
+ callback_.Run();
+}
+
+void CancelableCallback::InitializeForwarder() {
+ forwarder_ = base::Bind(&CancelableCallback::RunCallback,
+ weak_factory_.GetWeakPtr());
+}
+
+} // namespace bind

Powered by Google App Engine
This is Rietveld 408576698