OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef WebFunction_h |
| 6 #define WebFunction_h |
| 7 |
| 8 #include "base/bind.h" |
| 9 #include "base/callback.h" |
| 10 #include "base/callback_helpers.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" |
| 13 #include "public/platform/WebCommon.h" |
| 14 |
| 15 #include <memory> |
| 16 #include <utility> |
| 17 |
| 18 #if BLINK_IMPLEMENTATION |
| 19 #include "wtf/Functional.h" |
| 20 #else |
| 21 #include "base/logging.h" |
| 22 #endif |
| 23 |
| 24 namespace blink { |
| 25 |
| 26 template <typename...> |
| 27 class WebFunction; |
| 28 |
| 29 // Conversion from WTF closures to base closures to pass a callback out of |
| 30 // blink. |
| 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 |
| 38 public: |
| 39 #if BLINK_IMPLEMENTATION |
| 40 WebFunction() |
| 41 { |
| 42 } |
| 43 |
| 44 explicit WebFunction(PassOwnPtr<WTFFunction> c) |
| 45 { |
| 46 // Make the base::Callback own the WTF::Function, allowing it to call th
e |
| 47 // WTF::Function more than once but destroy it when the base::Callback i
s |
| 48 // destroyed. |
| 49 m_callback = base::Bind(&RunWTFFunction<R, Args...>, base::Owned(c.leakP
tr())); |
| 50 } |
| 51 #endif |
| 52 |
| 53 WebFunction(WebFunction&& other) |
| 54 { |
| 55 *this = std::move(other); |
| 56 } |
| 57 WebFunction& operator=(WebFunction&& other) |
| 58 { |
| 59 #if DCHECK_IS_ON() |
| 60 m_haveClosure = other.m_haveClosure; |
| 61 other.m_haveClosure = false; |
| 62 #endif |
| 63 m_callback = std::move(other.m_callback); |
| 64 return *this; |
| 65 } |
| 66 |
| 67 #if !BLINK_IMPLEMENTATION |
| 68 // TODO(danakj): This could be rvalue-ref-qualified. |
| 69 base::Callback<R(Args...)> TakeBaseCallback() |
| 70 { |
| 71 #if DCHECK_IS_ON() |
| 72 // Don't call this more than once! |
| 73 DCHECK(m_haveClosure); |
| 74 m_haveClosure = false; |
| 75 #endif |
| 76 return std::move(m_callback); |
| 77 } |
| 78 #endif |
| 79 |
| 80 private: |
| 81 #if BLINK_IMPLEMENTATION |
| 82 template <typename RunR, typename... RunArgs> |
| 83 static RunR RunWTFFunction(WTFFunction* c, RunArgs... args) |
| 84 { |
| 85 return (*c)(std::forward<RunArgs>(args)...); |
| 86 } |
| 87 #endif |
| 88 |
| 89 #if DCHECK_IS_ON() |
| 90 bool m_haveClosure = true; |
| 91 #endif |
| 92 base::Callback<R(Args...)> m_callback; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(WebFunction); |
| 95 }; |
| 96 |
| 97 using WebClosure = WebFunction<void()>; |
| 98 |
| 99 } // namespace blink |
| 100 |
| 101 #endif // WebClosure_h |
OLD | NEW |