Chromium Code Reviews| Index: base/cancelable_callback.cc |
| diff --git a/base/cancelable_callback.cc b/base/cancelable_callback.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..eebddcb78015c9b34243624a6da625e098e804af |
| --- /dev/null |
| +++ b/base/cancelable_callback.cc |
| @@ -0,0 +1,58 @@ |
| +// 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" |
|
csilv
2011/11/23 19:37:30
add includes for base/bind.h and base/compiler_spe
James Hawkins
2011/11/23 22:12:52
Done.
|
| + |
| +#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()); |
| + BindForwarder(); |
| +} |
| + |
| +CancelableCallback::~CancelableCallback() {} |
| + |
| +void CancelableCallback::Cancel() { |
| + weak_factory_.InvalidateWeakPtrs(); |
| + callback_.Reset(); |
|
awong
2011/11/23 19:58:23
We should have a unittest assert that the wrapped
James Hawkins
2011/11/23 22:12:52
Done.
|
| +} |
| + |
| +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. |
| + BindForwarder(); |
| + |
| + callback_ = callback; |
| +} |
| + |
| +const base::Closure& CancelableCallback::callback() const { |
| + return forwarder_; |
| +} |
| + |
| +bool CancelableCallback::is_null() const { |
| + return callback_.is_null(); |
| +} |
| + |
| +void CancelableCallback::RunCallback() { |
| + callback_.Run(); |
| +} |
| + |
| +void CancelableCallback::BindForwarder() { |
| + forwarder_ = base::Bind(&CancelableCallback::RunCallback, |
| + weak_factory_.GetWeakPtr()); |
| +} |
| + |
| +} |