OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 15 matching lines...) Expand all Loading... |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #ifndef CallbackPromiseAdapter_h | 31 #ifndef CallbackPromiseAdapter_h |
32 #define CallbackPromiseAdapter_h | 32 #define CallbackPromiseAdapter_h |
33 | 33 |
34 #include "bindings/core/v8/ScriptPromiseResolver.h" | 34 #include "bindings/core/v8/ScriptPromiseResolver.h" |
35 #include "public/platform/WebCallbacks.h" | 35 #include "public/platform/WebCallbacks.h" |
36 #include "wtf/OwnPtr.h" | 36 #include "wtf/PtrUtil.h" |
37 #include "wtf/PassOwnPtr.h" | |
38 #include "wtf/TypeTraits.h" | 37 #include "wtf/TypeTraits.h" |
39 | |
40 #include <memory> | 38 #include <memory> |
41 | 39 |
42 namespace blink { | 40 namespace blink { |
43 | 41 |
44 // CallbackPromiseAdapter is a WebCallbacks subclass and resolves / rejects the | 42 // CallbackPromiseAdapter is a WebCallbacks subclass and resolves / rejects the |
45 // stored resolver when onSuccess / onError is called, respectively. | 43 // stored resolver when onSuccess / onError is called, respectively. |
46 // | 44 // |
47 // Basically CallbackPromiseAdapter<S, T> is a subclass of | 45 // Basically CallbackPromiseAdapter<S, T> is a subclass of |
48 // WebCallbacks<S::WebType, T::WebType>. There are some exceptions: | 46 // WebCallbacks<S::WebType, T::WebType>. There are some exceptions: |
49 // - If S or T don't have WebType (e.g. S = bool), a default WebType holder | 47 // - If S or T don't have WebType (e.g. S = bool), a default WebType holder |
50 // called trivial WebType holder is used. For example, | 48 // called trivial WebType holder is used. For example, |
51 // CallbackPromiseAdapter<bool, void> is a subclass of | 49 // CallbackPromiseAdapter<bool, void> is a subclass of |
52 // WebCallbacks<bool, void>. | 50 // WebCallbacks<bool, void>. |
53 // - If a WebType is OwnPtr<T>, its corresponding type parameter on | 51 // - If a WebType is std::unique_ptr<T>, its corresponding type parameter on |
54 // WebCallbacks is std::unique_ptr<T>, because WebCallbacks must be exposed t
o | 52 // WebCallbacks is std::unique_ptr<T>, because WebCallbacks must be exposed t
o |
55 // Chromium. | 53 // Chromium. |
56 // | 54 // |
57 // When onSuccess is called with a S::WebType value, the value is passed to | 55 // When onSuccess is called with a S::WebType value, the value is passed to |
58 // S::take and the resolver is resolved with its return value. Ditto for | 56 // S::take and the resolver is resolved with its return value. Ditto for |
59 // onError. | 57 // onError. |
60 // | 58 // |
61 // ScriptPromiseResolver::resolve / reject will not be called when the execution | 59 // ScriptPromiseResolver::resolve / reject will not be called when the execution |
62 // context is stopped. | 60 // context is stopped. |
63 // | 61 // |
64 // Example: | 62 // Example: |
65 // class MyClass { | 63 // class MyClass { |
66 // public: | 64 // public: |
67 // using WebType = OwnPtr<WebMyClass>; | 65 // using WebType = std::unique_ptr<WebMyClass>; |
68 // static PassRefPtr<MyClass> take(ScriptPromiseResolver* resolver, | 66 // static PassRefPtr<MyClass> take(ScriptPromiseResolver* resolver, |
69 // PassOwnPtr<WebMyClass> webInstance) | 67 // std::unique_ptr<WebMyClass> webInstance) |
70 // { | 68 // { |
71 // return MyClass::create(webInstance); | 69 // return MyClass::create(webInstance); |
72 // } | 70 // } |
73 // ... | 71 // ... |
74 // }; | 72 // }; |
75 // class MyErrorClass { | 73 // class MyErrorClass { |
76 // public: | 74 // public: |
77 // using WebType = const WebMyErrorClass&; | 75 // using WebType = const WebMyErrorClass&; |
78 // static MyErrorClass take(ScriptPromiseResolver* resolver, | 76 // static MyErrorClass take(ScriptPromiseResolver* resolver, |
79 // const WebErrorClass& webError) | 77 // const WebErrorClass& webError) |
80 // { | 78 // { |
81 // return MyErrorClass(webError); | 79 // return MyErrorClass(webError); |
82 // } | 80 // } |
83 // ... | 81 // ... |
84 // }; | 82 // }; |
85 // OwnPtr<WebCallbacks<std::unique_ptr<WebMyClass>, const WebMyErrorClass&>> | 83 // std::unique_ptr<WebCallbacks<std::unique_ptr<WebMyClass>, const WebMyErrorCla
ss&>> |
86 // callbacks = adoptPtr(new CallbackPromiseAdapter<MyClass, MyErrorClass>( | 84 // callbacks = wrapUnique(new CallbackPromiseAdapter<MyClass, MyErrorClass>( |
87 // resolver)); | 85 // resolver)); |
88 // ... | 86 // ... |
89 // | 87 // |
90 // OwnPtr<WebCallbacks<bool, const WebMyErrorClass&>> callbacks2 = | 88 // std::unique_ptr<WebCallbacks<bool, const WebMyErrorClass&>> callbacks2 = |
91 // adoptPtr(new CallbackPromiseAdapter<bool, MyErrorClass>(resolver)); | 89 // wrapUnique(new CallbackPromiseAdapter<bool, MyErrorClass>(resolver)); |
92 // ... | 90 // ... |
93 // | 91 // |
94 // | 92 // |
95 // In order to implement the above exceptions, we have template classes below. | 93 // In order to implement the above exceptions, we have template classes below. |
96 // OnSuccess and OnError provide onSuccess and onError implementation, and there | 94 // OnSuccess and OnError provide onSuccess and onError implementation, and there |
97 // are utility templates that provide | 95 // are utility templates that provide |
98 // - OwnPtr - WebPassOwnPtr translation ([Web]PassType[Impl], adopt, pass), | 96 // - std::unique_ptr - WebPassOwnPtr translation ([Web]PassType[Impl], adopt, p
ass), |
99 // - trivial WebType holder (TrivialWebTypeHolder). | 97 // - trivial WebType holder (TrivialWebTypeHolder). |
100 | 98 |
101 namespace internal { | 99 namespace internal { |
102 | 100 |
103 // This template is placed outside of CallbackPromiseAdapterInternal because | 101 // This template is placed outside of CallbackPromiseAdapterInternal because |
104 // explicit specialization is forbidden in a class scope. | 102 // explicit specialization is forbidden in a class scope. |
105 template <typename T> | 103 template <typename T> |
106 struct CallbackPromiseAdapterTrivialWebTypeHolder { | 104 struct CallbackPromiseAdapterTrivialWebTypeHolder { |
107 using WebType = T; | 105 using WebType = T; |
108 static T take(ScriptPromiseResolver*, const T& x) { return x; } | 106 static T take(ScriptPromiseResolver*, const T& x) { return x; } |
109 }; | 107 }; |
110 template <> | 108 template <> |
111 struct CallbackPromiseAdapterTrivialWebTypeHolder<void> { | 109 struct CallbackPromiseAdapterTrivialWebTypeHolder<void> { |
112 using WebType = void; | 110 using WebType = void; |
113 }; | 111 }; |
114 | 112 |
115 class CallbackPromiseAdapterInternal { | 113 class CallbackPromiseAdapterInternal { |
116 private: | 114 private: |
117 template <typename T> static T webTypeHolderMatcher(typename std::remove_ref
erence<typename T::WebType>::type*); | 115 template <typename T> static T webTypeHolderMatcher(typename std::remove_ref
erence<typename T::WebType>::type*); |
118 template <typename T> static CallbackPromiseAdapterTrivialWebTypeHolder<T> w
ebTypeHolderMatcher(...); | 116 template <typename T> static CallbackPromiseAdapterTrivialWebTypeHolder<T> w
ebTypeHolderMatcher(...); |
119 template <typename T> using WebTypeHolder = decltype(webTypeHolderMatcher<T>
(nullptr)); | 117 template <typename T> using WebTypeHolder = decltype(webTypeHolderMatcher<T>
(nullptr)); |
120 | 118 |
121 // The following templates should be gone when the repositories are merged | 119 // The following templates should be gone when the repositories are merged |
122 // and we can use C++11 libraries. | 120 // and we can use C++11 libraries. |
123 template <typename T> | 121 template <typename T> |
124 struct PassTypeImpl { | 122 struct PassTypeImpl { |
125 using Type = T; | 123 using Type = T; |
126 }; | 124 }; |
127 template <typename T> | 125 template <typename T> |
128 struct PassTypeImpl<OwnPtr<T>> { | 126 struct PassTypeImpl<std::unique_ptr<T>> { |
129 using Type = PassOwnPtr<T>; | 127 using Type = std::unique_ptr<T>; |
130 }; | 128 }; |
131 template <typename T> | 129 template <typename T> |
132 struct WebPassTypeImpl { | 130 struct WebPassTypeImpl { |
133 using Type = T; | 131 using Type = T; |
134 }; | 132 }; |
135 template <typename T> | 133 template <typename T> |
136 struct WebPassTypeImpl<OwnPtr<T>> { | 134 struct WebPassTypeImpl<std::unique_ptr<T>> { |
137 using Type = std::unique_ptr<T>; | 135 using Type = std::unique_ptr<T>; |
138 }; | 136 }; |
139 template <typename T> using PassType = typename PassTypeImpl<T>::Type; | 137 template <typename T> using PassType = typename PassTypeImpl<T>::Type; |
140 template <typename T> using WebPassType = typename WebPassTypeImpl<T>::Type; | 138 template <typename T> using WebPassType = typename WebPassTypeImpl<T>::Type; |
141 template <typename T> static T& adopt(T& x) { return x; } | 139 template <typename T> static T& adopt(T& x) { return x; } |
142 template <typename T> | 140 template <typename T> |
143 static PassOwnPtr<T> adopt(std::unique_ptr<T>& x) { return adoptPtr(x.releas
e()); } | 141 static std::unique_ptr<T> adopt(std::unique_ptr<T>& x) { return std::move(x)
; } |
144 template <typename T> static PassType<T> pass(T& x) { return x; } | 142 template <typename T> static PassType<T> pass(T& x) { return x; } |
145 template <typename T> static PassOwnPtr<T> pass(OwnPtr<T>& x) { return std::
move(x); } | 143 template <typename T> static std::unique_ptr<T> pass(std::unique_ptr<T>& x)
{ return std::move(x); } |
146 | 144 |
147 template <typename S, typename T> | 145 template <typename S, typename T> |
148 class Base : public WebCallbacks<WebPassType<typename S::WebType>, WebPassTy
pe<typename T::WebType>> { | 146 class Base : public WebCallbacks<WebPassType<typename S::WebType>, WebPassTy
pe<typename T::WebType>> { |
149 public: | 147 public: |
150 explicit Base(ScriptPromiseResolver* resolver) : m_resolver(resolver) {} | 148 explicit Base(ScriptPromiseResolver* resolver) : m_resolver(resolver) {} |
151 ScriptPromiseResolver* resolver() { return m_resolver; } | 149 ScriptPromiseResolver* resolver() { return m_resolver; } |
152 | 150 |
153 private: | 151 private: |
154 Persistent<ScriptPromiseResolver> m_resolver; | 152 Persistent<ScriptPromiseResolver> m_resolver; |
155 }; | 153 }; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 }; | 213 }; |
216 | 214 |
217 } // namespace internal | 215 } // namespace internal |
218 | 216 |
219 template <typename S, typename T> | 217 template <typename S, typename T> |
220 using CallbackPromiseAdapter = internal::CallbackPromiseAdapterInternal::Callbac
kPromiseAdapter<S, T>; | 218 using CallbackPromiseAdapter = internal::CallbackPromiseAdapterInternal::Callbac
kPromiseAdapter<S, T>; |
221 | 219 |
222 } // namespace blink | 220 } // namespace blink |
223 | 221 |
224 #endif | 222 #endif |
OLD | NEW |