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