diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-24 04:12:04 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-24 04:12:04 +0000 |
commit | b77576f54910cfb2e64ef91f3b3bb197136553b0 (patch) | |
tree | f1a78f1e6983ff1a44e0afb8fdca3b32785263c1 /base/cancelable_callback.h | |
parent | a8135bc3dc27230e8107ebf44f99826a05989365 (diff) | |
download | chromium_src-b77576f54910cfb2e64ef91f3b3bb197136553b0.zip chromium_src-b77576f54910cfb2e64ef91f3b3bb197136553b0.tar.gz chromium_src-b77576f54910cfb2e64ef91f3b3bb197136553b0.tar.bz2 |
base::Bind: Implement CancelableCallback to replace CancelableTask.
Designed by groby,binji,csilv,jhawkins.
BUG=96749
TEST=CancelableCallbackTest.*
R=ajwong@chromium.org,darin@chromium.org
Review URL: http://codereview.chromium.org/8673008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@111493 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/cancelable_callback.h')
-rw-r--r-- | base/cancelable_callback.h | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/base/cancelable_callback.h b/base/cancelable_callback.h new file mode 100644 index 0000000..7f423ba --- /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_ |