Chromium Code Reviews| Index: third_party/WebKit/public/platform/callback/WebClosure.h |
| diff --git a/third_party/WebKit/public/platform/callback/WebClosure.h b/third_party/WebKit/public/platform/callback/WebClosure.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2d8ef7e03c1cec28024d0e3b6c05caad43c72610 |
| --- /dev/null |
| +++ b/third_party/WebKit/public/platform/callback/WebClosure.h |
| @@ -0,0 +1,70 @@ |
| +// 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 WebClosure_h |
| +#define WebClosure_h |
| + |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/callback_helpers.h" |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "public/platform/WebCommon.h" |
| + |
| +#include <utility> |
| + |
| +#if BLINK_IMPLEMENTATION |
| +#include "wtf/Functional.h" |
| +#else |
| +#include "base/logging.h" |
| +#endif |
| + |
| +namespace blink { |
| + |
| +// Conversion from WTF closures to base closures to pass a callback out of blink. |
| +class WebClosure { |
| +public: |
| +#if BLINK_IMPLEMENTATION |
| + WebClosure() {} |
| + |
| + explicit WebClosure(PassOwnPtr<SameThreadClosure> c) |
| + { |
| + auto run_and_delete = +[](scoped_ptr<SameThreadClosure> c) |
| + { |
| + (*c)(); |
| + }; |
| + m_closure = base::Bind(run_and_delete, base::Passed(make_scoped_ptr(c.leakPtr()))); |
| + } |
| +#endif |
| + |
| + // TODO(danakj): These could be =default with MSVC 2015. |
| + WebClosure(WebClosure&& other) { *this = std::move(other); } |
| + WebClosure& operator=(WebClosure&& other) |
| + { |
| + m_haveClosure = other.m_haveClosure; |
|
dcheng
2016/03/24 15:53:56
Should m_have closure be set to false?
danakj
2016/03/24 17:53:51
But what if the |other| had a closure, now this on
dcheng
2016/03/24 18:00:18
Sorry, to clarify, I meant other.m_haveClosure sho
danakj
2016/03/24 18:03:16
Oh oh ok!
|
| + m_closure = base::ResetAndReturn(&other.m_closure); |
|
dcheng
2016/03/24 15:53:56
std::move() this too?
danakj
2016/03/24 17:53:51
But it's an rvalue no?
dcheng
2016/03/24 18:00:18
Not sure what you mean. I believe that
m_closure
danakj
2016/03/24 18:03:16
OOh, I see. Does callback null itself when you mov
danakj
2016/03/24 18:06:04
It totes does. Done.
|
| + return *this; |
| + } |
| + |
| +#if !BLINK_IMPLEMENTATION |
| + // TODO(danakj): This could be rvalue-ref-qualified. |
| + base::Closure ToBaseClosure() |
| + { |
| + // Don't call this more than once! |
| + DCHECK(m_haveClosure); |
| + m_haveClosure = false; |
| + return std::move(m_closure); |
| + } |
| +#endif |
| + |
| +private: |
| + bool m_haveClosure = true; |
| + base::Closure m_closure; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(WebClosure); |
| +}; |
| + |
| +} // namespace blink |
| + |
| +#endif // WebClosure_h |