Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef WebClosure_h | 5 #ifndef WebFunction_h |
| 6 #define WebClosure_h | 6 #define WebFunction_h |
| 7 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "public/platform/WebCommon.h" | 13 #include "public/platform/WebCommon.h" |
| 14 | 14 |
| 15 #include <memory> | 15 #include <memory> |
| 16 #include <utility> | 16 #include <utility> |
| 17 | 17 |
| 18 #if BLINK_IMPLEMENTATION | 18 #if BLINK_IMPLEMENTATION |
| 19 #include "wtf/Functional.h" | 19 #include "wtf/Functional.h" |
| 20 #else | 20 #else |
| 21 #include "base/logging.h" | 21 #include "base/logging.h" |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 namespace blink { | 24 namespace blink { |
| 25 | 25 |
| 26 template <typename...> | |
| 27 class WebFunction; | |
| 28 | |
| 26 // Conversion from WTF closures to base closures to pass a callback out of | 29 // Conversion from WTF closures to base closures to pass a callback out of |
| 27 // blink. | 30 // blink. |
| 28 class WebClosure { | 31 template <typename R, typename... Args> |
| 32 class WebFunction<R(Args...)> { | |
| 33 private: | |
| 34 #if BLINK_IMPLEMENTATION | |
| 35 using WTFFunction = WTF::Function<R(Args...), WTF::SameThreadAffinity>; | |
| 36 #endif | |
| 37 | |
| 29 public: | 38 public: |
| 30 #if BLINK_IMPLEMENTATION | 39 #if BLINK_IMPLEMENTATION |
| 31 WebClosure() {} | 40 WebFunction() |
| 41 { | |
| 42 } | |
| 32 | 43 |
| 33 explicit WebClosure(PassOwnPtr<SameThreadClosure> c) | 44 explicit WebFunction(PassOwnPtr<WTFFunction> c) |
| 34 { | 45 { |
| 35 m_closure = base::Bind(&RunAndDelete, base::Passed(base::WrapUnique(c.le akPtr()))); | 46 m_callback = base::Bind(&RunAndDelete<R, Args...>, base::Passed(base::Wr apUnique(c.leakPtr()))); |
|
danakj
2016/04/12 21:42:16
A problem came up in the webgl tests. Using Passed
| |
| 36 } | 47 } |
| 37 #endif | 48 #endif |
| 38 | 49 |
| 39 // TODO(danakj): These could be =default with MSVC 2015. | 50 WebFunction(WebFunction&& other) |
| 40 WebClosure(WebClosure&& other) { *this = std::move(other); } | 51 { |
| 41 WebClosure& operator=(WebClosure&& other) | 52 *this = std::move(other); |
| 53 } | |
| 54 WebFunction& operator=(WebFunction&& other) | |
| 42 { | 55 { |
| 43 #if DCHECK_IS_ON() | 56 #if DCHECK_IS_ON() |
| 44 m_haveClosure = other.m_haveClosure; | 57 m_haveClosure = other.m_haveClosure; |
| 45 other.m_haveClosure = false; | 58 other.m_haveClosure = false; |
| 46 #endif | 59 #endif |
| 47 m_closure = std::move(other.m_closure); | 60 m_callback = std::move(other.m_callback); |
| 48 return *this; | 61 return *this; |
| 49 } | 62 } |
| 50 | 63 |
| 51 #if !BLINK_IMPLEMENTATION | 64 #if !BLINK_IMPLEMENTATION |
| 52 // TODO(danakj): This could be rvalue-ref-qualified. | 65 // TODO(danakj): This could be rvalue-ref-qualified. |
| 53 base::Closure TakeBaseClosure() | 66 base::Callback<R(Args...)> TakeBaseCallback() |
| 54 { | 67 { |
| 55 #if DCHECK_IS_ON() | 68 #if DCHECK_IS_ON() |
| 56 // Don't call this more than once! | 69 // Don't call this more than once! |
| 57 DCHECK(m_haveClosure); | 70 DCHECK(m_haveClosure); |
| 58 m_haveClosure = false; | 71 m_haveClosure = false; |
| 59 #endif | 72 #endif |
| 60 return std::move(m_closure); | 73 return std::move(m_callback); |
| 61 } | 74 } |
| 62 #endif | 75 #endif |
| 63 | 76 |
| 64 private: | 77 private: |
| 65 #if BLINK_IMPLEMENTATION | 78 #if BLINK_IMPLEMENTATION |
| 66 static void RunAndDelete(std::unique_ptr<SameThreadClosure> c) { (*c)(); } | 79 template <typename RunR, typename... RunArgs> |
| 80 static RunR RunAndDelete(std::unique_ptr<WTFFunction> c, RunArgs... args) | |
| 81 { | |
| 82 return (*c)(std::forward<RunArgs>(args)...); | |
| 83 } | |
| 67 #endif | 84 #endif |
| 68 | 85 |
| 69 #if DCHECK_IS_ON() | 86 #if DCHECK_IS_ON() |
| 70 bool m_haveClosure = true; | 87 bool m_haveClosure = true; |
| 71 #endif | 88 #endif |
| 72 base::Closure m_closure; | 89 base::Callback<R(Args...)> m_callback; |
| 73 | 90 |
| 74 DISALLOW_COPY_AND_ASSIGN(WebClosure); | 91 DISALLOW_COPY_AND_ASSIGN(WebFunction); |
| 75 }; | 92 }; |
| 76 | 93 |
| 94 using WebClosure = WebFunction<void()>; | |
| 95 | |
| 77 } // namespace blink | 96 } // namespace blink |
| 78 | 97 |
| 79 #endif // WebClosure_h | 98 #endif // WebClosure_h |
| OLD | NEW |