Chromium Code Reviews| Index: third_party/WebKit/Source/wtf/MakeCancellable.h |
| diff --git a/third_party/WebKit/Source/wtf/MakeCancellable.h b/third_party/WebKit/Source/wtf/MakeCancellable.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b298e7eaaa6e537198496caf09de5ff76da8f0ca |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/wtf/MakeCancellable.h |
| @@ -0,0 +1,109 @@ |
| +// Copyright 2016 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 WTF_MakeCancellable_h |
| +#define WTF_MakeCancellable_h |
| + |
| +#include "base/logging.h" |
| +#include "wtf/Functional.h" |
| +#include "wtf/RefCounted.h" |
| +#include "wtf/WTFExport.h" |
| +#include <memory> |
| + |
| +namespace WTF { |
| + |
| +class WTF_EXPORT FunctionCanceller : public RefCounted<FunctionCanceller> { |
| +public: |
| + virtual void cancel() = 0; |
| + |
| +protected: |
| + FunctionCanceller(); |
| + virtual ~FunctionCanceller(); |
| + friend RefCounted<FunctionCanceller>; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FunctionCanceller); |
| +}; |
| + |
| +class WTF_EXPORT ScopedFunctionCanceller { |
|
Yuta Kitamura
2016/07/29 05:58:28
A brief comment about the usage of this class woul
tzik
2016/07/29 13:47:11
Done.
|
| +public: |
| + ScopedFunctionCanceller(); |
| + explicit ScopedFunctionCanceller(PassRefPtr<FunctionCanceller>); |
| + |
| + ScopedFunctionCanceller(ScopedFunctionCanceller&&); |
| + ScopedFunctionCanceller& operator=(ScopedFunctionCanceller&&); |
| + ScopedFunctionCanceller(const ScopedFunctionCanceller&) = delete; |
| + ScopedFunctionCanceller& operator=(const ScopedFunctionCanceller&) = delete; |
| + |
| + ~ScopedFunctionCanceller(); |
| + void detach(); |
| + void cancel(); |
| + |
| +private: |
| + RefPtr<FunctionCanceller> m_canceller; |
| +}; |
| + |
| +namespace internal { |
| + |
| +template <typename... Params> |
| +class FunctionCancellerImpl final : public FunctionCanceller { |
| +public: |
| + FunctionCancellerImpl(std::unique_ptr<Function<void(Params...)>> function) |
| + : m_function(std::move(function)) |
| + { |
| + DCHECK(m_function); |
| + } |
| + |
| + void runUnlessCancelled(const ScopedFunctionCanceller&, Params... params) |
| + { |
| + if (m_function) |
| + (*m_function)(std::forward<Params>(params)...); |
| + } |
| + |
| + void cancel() override |
| + { |
| + m_function = nullptr; |
| + } |
| + |
| +private: |
| + ~FunctionCancellerImpl() override = default; |
| + |
| + std::unique_ptr<WTF::Function<void(Params...)>> m_function; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(FunctionCancellerImpl); |
| +}; |
| + |
| +} // namespace internal |
| + |
| +// makeCancellable wraps a WTF::Function to make cancellable function. |
| +// This function returns a tuple of a WTF::Function, and a FunctionCanceller. |
| +// An invocation of the resulting function is relayed to the original function |
| +// if the FunctionCanceller::cancel() is not called. |
| +// |
| +// Example: |
| +// void foo() {} |
| +// std::unique_ptr<Function<void()>> function = WTF::bind(&foo); |
| +// |
| +// std::unique_ptr<Function<void()>> wrappedFunction; |
| +// RefPtr<FunctionCanceller> canceller; |
| +// std::tie(wrappedFunction, canceller) = makeCancellable(std::move(function)); |
| +// |
| +// (*wrappedFunction)(); // Not cancelled. foo() is called. |
| +// canceller->cancel(); |
| +// (*wrappedFunction)(); // Cancelled. foo() is not called. |
| +// |
| +template <typename... Params> |
| +std::tuple<std::unique_ptr<Function<void(Params...)>>, PassRefPtr<FunctionCanceller>> |
| +makeCancellable(std::unique_ptr<Function<void(Params...)>> function) |
| +{ |
| + using Canceller = internal::FunctionCancellerImpl<Params...>; |
| + RefPtr<Canceller> canceller = adoptRef(new Canceller(std::move(function))); |
| + // Keep a ScopedFunctionCanceller instance in |wrappedFunction|, so that the |
| + // destruction of |wrappedFunction| implies the destruction of |function|. |
| + std::unique_ptr<Function<void(Params...)>> wrappedFunction = bind(&Canceller::runUnlessCancelled, canceller, ScopedFunctionCanceller(canceller)); |
|
Yuta Kitamura
2016/07/29 00:57:42
I'm still unsure about the merit of cancelling on
tzik
2016/07/29 05:25:40
My concern is that, without the auto destruction,
Yuta Kitamura
2016/07/29 05:58:28
OK, that makes sense.
Maybe you could add some mo
tzik
2016/07/29 13:47:11
Done.
|
| + return std::make_tuple(std::move(wrappedFunction), canceller.release()); |
| +} |
| + |
| +} // namespace WTF |
| + |
| +#endif // WTF_MakeCancellable_h |