| 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 18 matching lines...) Expand all Loading... |
| 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/PtrUtil.h" |
| 37 #include "wtf/TypeTraits.h" | 37 #include "wtf/TypeTraits.h" |
| 38 #include <memory> | 38 #include <memory> |
| 39 #include <utility> |
| 39 | 40 |
| 40 namespace blink { | 41 namespace blink { |
| 41 | 42 |
| 42 // CallbackPromiseAdapter is a WebCallbacks subclass and resolves / rejects the | 43 // CallbackPromiseAdapter is a WebCallbacks subclass and resolves / rejects the |
| 43 // stored resolver when onSuccess / onError is called, respectively. | 44 // stored resolver when onSuccess / onError is called, respectively. |
| 44 // | 45 // |
| 45 // Basically CallbackPromiseAdapter<S, T> is a subclass of | 46 // Basically CallbackPromiseAdapter<S, T> is a subclass of |
| 46 // WebCallbacks<S::WebType, T::WebType>. There are some exceptions: | 47 // 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 | 48 // - 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, | 49 // called trivial WebType holder is used. For example, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 // resolver)); | 86 // resolver)); |
| 86 // ... | 87 // ... |
| 87 // | 88 // |
| 88 // std::unique_ptr<WebCallbacks<bool, const WebMyErrorClass&>> callbacks2 = | 89 // std::unique_ptr<WebCallbacks<bool, const WebMyErrorClass&>> callbacks2 = |
| 89 // wrapUnique(new CallbackPromiseAdapter<bool, MyErrorClass>(resolver)); | 90 // wrapUnique(new CallbackPromiseAdapter<bool, MyErrorClass>(resolver)); |
| 90 // ... | 91 // ... |
| 91 // | 92 // |
| 92 // | 93 // |
| 93 // In order to implement the above exceptions, we have template classes below. | 94 // In order to implement the above exceptions, we have template classes below. |
| 94 // OnSuccess and OnError provide onSuccess and onError implementation, and there | 95 // OnSuccess and OnError provide onSuccess and onError implementation, and there |
| 95 // are utility templates that provide | 96 // are utility templates that provide the trivial WebType holder. |
| 96 // - std::unique_ptr - WebPassOwnPtr translation ([Web]PassType[Impl], adopt, p
ass), | |
| 97 // - trivial WebType holder (TrivialWebTypeHolder). | |
| 98 | 97 |
| 99 namespace internal { | 98 namespace internal { |
| 100 | 99 |
| 101 // This template is placed outside of CallbackPromiseAdapterInternal because | 100 // This template is placed outside of CallbackPromiseAdapterInternal because |
| 102 // explicit specialization is forbidden in a class scope. | 101 // explicit specialization is forbidden in a class scope. |
| 103 template <typename T> | 102 template <typename T> |
| 104 struct CallbackPromiseAdapterTrivialWebTypeHolder { | 103 struct CallbackPromiseAdapterTrivialWebTypeHolder { |
| 105 using WebType = T; | 104 using WebType = T; |
| 106 static T take(ScriptPromiseResolver*, const T& x) { return x; } | 105 static T take(ScriptPromiseResolver*, const T& x) { return x; } |
| 107 }; | 106 }; |
| 108 template <> | 107 template <> |
| 109 struct CallbackPromiseAdapterTrivialWebTypeHolder<void> { | 108 struct CallbackPromiseAdapterTrivialWebTypeHolder<void> { |
| 110 using WebType = void; | 109 using WebType = void; |
| 111 }; | 110 }; |
| 112 | 111 |
| 113 class CallbackPromiseAdapterInternal { | 112 class CallbackPromiseAdapterInternal { |
| 114 private: | 113 private: |
| 115 template <typename T> static T webTypeHolderMatcher(typename std::remove_ref
erence<typename T::WebType>::type*); | 114 template <typename T> static T webTypeHolderMatcher(typename std::remove_ref
erence<typename T::WebType>::type*); |
| 116 template <typename T> static CallbackPromiseAdapterTrivialWebTypeHolder<T> w
ebTypeHolderMatcher(...); | 115 template <typename T> static CallbackPromiseAdapterTrivialWebTypeHolder<T> w
ebTypeHolderMatcher(...); |
| 117 template <typename T> using WebTypeHolder = decltype(webTypeHolderMatcher<T>
(nullptr)); | 116 template <typename T> using WebTypeHolder = decltype(webTypeHolderMatcher<T>
(nullptr)); |
| 118 | 117 |
| 119 template <typename T> static T& adopt(T& x) { return x; } | |
| 120 template <typename T> static std::unique_ptr<T> adopt(std::unique_ptr<T>& x)
{ return std::move(x); } | |
| 121 template <typename T> static T pass(T& x) { return x; } | |
| 122 template <typename T> static std::unique_ptr<T> pass(std::unique_ptr<T>& x)
{ return std::move(x); } | |
| 123 | |
| 124 template <typename S, typename T> | 118 template <typename S, typename T> |
| 125 class Base : public WebCallbacks<typename S::WebType, typename T::WebType> { | 119 class Base : public WebCallbacks<typename S::WebType, typename T::WebType> { |
| 126 public: | 120 public: |
| 127 explicit Base(ScriptPromiseResolver* resolver) : m_resolver(resolver) {} | 121 explicit Base(ScriptPromiseResolver* resolver) : m_resolver(resolver) {} |
| 128 ScriptPromiseResolver* resolver() { return m_resolver; } | 122 ScriptPromiseResolver* resolver() { return m_resolver; } |
| 129 | 123 |
| 130 private: | 124 private: |
| 131 Persistent<ScriptPromiseResolver> m_resolver; | 125 Persistent<ScriptPromiseResolver> m_resolver; |
| 132 }; | 126 }; |
| 133 | 127 |
| 134 template <typename S, typename T> | 128 template <typename S, typename T> |
| 135 class OnSuccess : public Base<S, T> { | 129 class OnSuccess : public Base<S, T> { |
| 136 public: | 130 public: |
| 137 explicit OnSuccess(ScriptPromiseResolver* resolver) : Base<S, T>(resolve
r) {} | 131 explicit OnSuccess(ScriptPromiseResolver* resolver) : Base<S, T>(resolve
r) {} |
| 138 void onSuccess(typename S::WebType r) override | 132 void onSuccess(typename S::WebType result) override |
| 139 { | 133 { |
| 140 typename S::WebType result(adopt(r)); | |
| 141 ScriptPromiseResolver* resolver = this->resolver(); | 134 ScriptPromiseResolver* resolver = this->resolver(); |
| 142 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 135 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| 143 return; | 136 return; |
| 144 resolver->resolve(S::take(resolver, pass(result))); | 137 resolver->resolve(S::take(resolver, std::move(result))); |
| 145 } | 138 } |
| 146 }; | 139 }; |
| 147 template <typename T> | 140 template <typename T> |
| 148 class OnSuccess<CallbackPromiseAdapterTrivialWebTypeHolder<void>, T> : publi
c Base<CallbackPromiseAdapterTrivialWebTypeHolder<void>, T> { | 141 class OnSuccess<CallbackPromiseAdapterTrivialWebTypeHolder<void>, T> : publi
c Base<CallbackPromiseAdapterTrivialWebTypeHolder<void>, T> { |
| 149 public: | 142 public: |
| 150 explicit OnSuccess(ScriptPromiseResolver* resolver) : Base<CallbackPromi
seAdapterTrivialWebTypeHolder<void>, T>(resolver) {} | 143 explicit OnSuccess(ScriptPromiseResolver* resolver) : Base<CallbackPromi
seAdapterTrivialWebTypeHolder<void>, T>(resolver) {} |
| 151 void onSuccess() override | 144 void onSuccess() override |
| 152 { | 145 { |
| 153 ScriptPromiseResolver* resolver = this->resolver(); | 146 ScriptPromiseResolver* resolver = this->resolver(); |
| 154 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 147 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| 155 return; | 148 return; |
| 156 resolver->resolve(); | 149 resolver->resolve(); |
| 157 } | 150 } |
| 158 }; | 151 }; |
| 159 template <typename S, typename T> | 152 template <typename S, typename T> |
| 160 class OnError : public OnSuccess<S, T> { | 153 class OnError : public OnSuccess<S, T> { |
| 161 public: | 154 public: |
| 162 explicit OnError(ScriptPromiseResolver* resolver) : OnSuccess<S, T>(reso
lver) {} | 155 explicit OnError(ScriptPromiseResolver* resolver) : OnSuccess<S, T>(reso
lver) {} |
| 163 void onError(typename T::WebType e) override | 156 void onError(typename T::WebType e) override |
| 164 { | 157 { |
| 165 typename T::WebType result(adopt(e)); | |
| 166 ScriptPromiseResolver* resolver = this->resolver(); | 158 ScriptPromiseResolver* resolver = this->resolver(); |
| 167 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 159 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| 168 return; | 160 return; |
| 169 ScriptState::Scope scope(resolver->getScriptState()); | 161 ScriptState::Scope scope(resolver->getScriptState()); |
| 170 resolver->reject(T::take(resolver, pass(result))); | 162 resolver->reject(T::take(resolver, std::move(e))); |
| 171 } | 163 } |
| 172 }; | 164 }; |
| 173 template <typename S> | 165 template <typename S> |
| 174 class OnError<S, CallbackPromiseAdapterTrivialWebTypeHolder<void>> : public
OnSuccess<S, CallbackPromiseAdapterTrivialWebTypeHolder<void>> { | 166 class OnError<S, CallbackPromiseAdapterTrivialWebTypeHolder<void>> : public
OnSuccess<S, CallbackPromiseAdapterTrivialWebTypeHolder<void>> { |
| 175 public: | 167 public: |
| 176 explicit OnError(ScriptPromiseResolver* resolver) : OnSuccess<S, Callbac
kPromiseAdapterTrivialWebTypeHolder<void>>(resolver) {} | 168 explicit OnError(ScriptPromiseResolver* resolver) : OnSuccess<S, Callbac
kPromiseAdapterTrivialWebTypeHolder<void>>(resolver) {} |
| 177 void onError() override | 169 void onError() override |
| 178 { | 170 { |
| 179 ScriptPromiseResolver* resolver = this->resolver(); | 171 ScriptPromiseResolver* resolver = this->resolver(); |
| 180 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 172 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 193 }; | 185 }; |
| 194 | 186 |
| 195 } // namespace internal | 187 } // namespace internal |
| 196 | 188 |
| 197 template <typename S, typename T> | 189 template <typename S, typename T> |
| 198 using CallbackPromiseAdapter = internal::CallbackPromiseAdapterInternal::Callbac
kPromiseAdapter<S, T>; | 190 using CallbackPromiseAdapter = internal::CallbackPromiseAdapterInternal::Callbac
kPromiseAdapter<S, T>; |
| 199 | 191 |
| 200 } // namespace blink | 192 } // namespace blink |
| 201 | 193 |
| 202 #endif | 194 #endif |
| OLD | NEW |