Chromium Code Reviews| Index: base/cancelable_callback.h |
| diff --git a/base/cancelable_callback.h b/base/cancelable_callback.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5a612736542dd4ba484daf498ddf698d271e5a31 |
| --- /dev/null |
| +++ b/base/cancelable_callback.h |
| @@ -0,0 +1,63 @@ |
| +// 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. |
| + |
| +#ifndef BASE_CANCELABLE_CALLBACK_H_ |
| +#define BASE_CANCELABLE_CALLBACK_H_ |
| +#pragma once |
| + |
|
csilv
2011/11/23 19:37:30
Consider adding some overview documentation here a
|
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/callback.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/memory/singleton.h" |
|
csilv
2011/11/23 19:37:30
i dont think bind.h, bind_helpers.h, compiler_spec
James Hawkins
2011/11/23 22:12:52
Done.
|
| +#include "base/memory/weak_ptr.h" |
| + |
| +namespace base { |
| + |
| +// Wrapper used to cancel a callback. |
| +class CancelableCallback { |
|
csilv
2011/11/23 19:37:30
i think you may need to add BASE_EXPORT (along wit
James Hawkins
2011/11/23 22:12:52
Done.
|
| + public: |
| + CancelableCallback(); |
| + |
| + // |callback| must not be null. |
|
csilv
2011/11/23 19:37:30
Would it be better to say "must not be a null call
James Hawkins
2011/11/23 22:12:52
"|callback| must not be a null callback" sounds we
|
| + explicit CancelableCallback(const base::Closure& callback); |
| + |
| + ~CancelableCallback(); |
| + |
| + // Cancels the stored callback. |
| + void Cancel(); |
| + |
| + // Sets |callback| as the closure that may be cancelled. |callback| may not |
| + // be null. |
|
awong
2011/11/23 19:58:23
Should state what happens to outstanding callbacks
James Hawkins
2011/11/23 22:12:52
Done.
|
| + void Reset(const base::Closure& callback); |
| + |
| + // Returns the wrapped callback. |
|
awong
2011/11/23 19:58:23
Returns a callback that can disabled by calling Ca
James Hawkins
2011/11/23 22:12:52
Done.
|
| + const base::Closure& callback() const; |
| + |
| + // Returns true if the wrapped callback is null. |
| + bool is_null() const; |
| + |
| + private: |
| + // Closure that runs the stored callback. |
| + void RunCallback(); |
| + |
| + // Helper method to bind |forwarder_| using a weak pointer from |
| + // |weak_factory_|. |
| + void BindForwarder(); |
| + |
| + // 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_ |