summaryrefslogtreecommitdiff
path: root/chromium/base/callback_helpers.h
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2017-07-17 13:57:45 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2017-07-19 13:44:40 +0000
commit6ec7b8da05d21a3878bd21c691b41e675d74bb1c (patch)
treeb87f250bc19413750b9bb9cdbf2da20ef5014820 /chromium/base/callback_helpers.h
parentec02ee4181c49b61fce1c8fb99292dbb8139cc90 (diff)
downloadqtwebengine-chromium-6ec7b8da05d21a3878bd21c691b41e675d74bb1c.tar.gz
BASELINE: Update Chromium to 60.0.3112.70
Change-Id: I9911c2280a014d4632f254857876a395d4baed2d Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
Diffstat (limited to 'chromium/base/callback_helpers.h')
-rw-r--r--chromium/base/callback_helpers.h48
1 files changed, 45 insertions, 3 deletions
diff --git a/chromium/base/callback_helpers.h b/chromium/base/callback_helpers.h
index 6e0aee88822..8d622f86552 100644
--- a/chromium/base/callback_helpers.h
+++ b/chromium/base/callback_helpers.h
@@ -14,22 +14,64 @@
#ifndef BASE_CALLBACK_HELPERS_H_
#define BASE_CALLBACK_HELPERS_H_
+#include <utility>
+
+#include "base/atomicops.h"
+#include "base/bind.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/macros.h"
+#include "base/memory/ptr_util.h"
namespace base {
template <typename Signature,
internal::CopyMode copy_mode,
internal::RepeatMode repeat_mode>
-base::Callback<Signature, copy_mode, repeat_mode> ResetAndReturn(
- base::Callback<Signature, copy_mode, repeat_mode>* cb) {
- base::Callback<Signature, copy_mode, repeat_mode> ret(std::move(*cb));
+Callback<Signature, copy_mode, repeat_mode> ResetAndReturn(
+ Callback<Signature, copy_mode, repeat_mode>* cb) {
+ Callback<Signature, copy_mode, repeat_mode> ret(std::move(*cb));
DCHECK(!*cb);
return ret;
}
+namespace internal {
+
+template <typename... Args>
+class AdaptCallbackForRepeatingHelper final {
+ public:
+ explicit AdaptCallbackForRepeatingHelper(OnceCallback<void(Args...)> callback)
+ : callback_(std::move(callback)) {
+ DCHECK(callback_);
+ }
+
+ void Run(Args... args) {
+ if (subtle::NoBarrier_AtomicExchange(&has_run_, 1))
+ return;
+ DCHECK(callback_);
+ std::move(callback_).Run(std::forward<Args>(args)...);
+ }
+
+ private:
+ volatile subtle::Atomic32 has_run_ = 0;
+ base::OnceCallback<void(Args...)> callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(AdaptCallbackForRepeatingHelper);
+};
+
+} // namespace internal
+
+// Wraps the given OnceCallback into a RepeatingCallback that relays its
+// invocation to the original OnceCallback on the first invocation. The
+// following invocations are just ignored.
+template <typename... Args>
+RepeatingCallback<void(Args...)> AdaptCallbackForRepeating(
+ OnceCallback<void(Args...)> callback) {
+ using Helper = internal::AdaptCallbackForRepeatingHelper<Args...>;
+ return base::BindRepeating(&Helper::Run,
+ base::MakeUnique<Helper>(std::move(callback)));
+}
+
// ScopedClosureRunner is akin to std::unique_ptr<> for Closures. It ensures
// that the Closure is executed no matter how the current scope exits.
class BASE_EXPORT ScopedClosureRunner {