| 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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 struct CallbackPromiseAdapterTrivialWebTypeHolder<void> { | 109 struct CallbackPromiseAdapterTrivialWebTypeHolder<void> { |
| 110 using WebType = void; | 110 using WebType = void; |
| 111 }; | 111 }; |
| 112 | 112 |
| 113 class CallbackPromiseAdapterInternal { | 113 class CallbackPromiseAdapterInternal { |
| 114 private: | 114 private: |
| 115 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*); |
| 116 template <typename T> static CallbackPromiseAdapterTrivialWebTypeHolder<T> w
ebTypeHolderMatcher(...); | 116 template <typename T> static CallbackPromiseAdapterTrivialWebTypeHolder<T> w
ebTypeHolderMatcher(...); |
| 117 template <typename T> using WebTypeHolder = decltype(webTypeHolderMatcher<T>
(nullptr)); | 117 template <typename T> using WebTypeHolder = decltype(webTypeHolderMatcher<T>
(nullptr)); |
| 118 | 118 |
| 119 // The following templates should be gone when the repositories are merged | |
| 120 // and we can use C++11 libraries. | |
| 121 template <typename T> | |
| 122 struct PassTypeImpl { | |
| 123 using Type = T; | |
| 124 }; | |
| 125 template <typename T> | |
| 126 struct PassTypeImpl<std::unique_ptr<T>> { | |
| 127 using Type = std::unique_ptr<T>; | |
| 128 }; | |
| 129 template <typename T> | |
| 130 struct WebPassTypeImpl { | |
| 131 using Type = T; | |
| 132 }; | |
| 133 template <typename T> | |
| 134 struct WebPassTypeImpl<std::unique_ptr<T>> { | |
| 135 using Type = std::unique_ptr<T>; | |
| 136 }; | |
| 137 template <typename T> using PassType = typename PassTypeImpl<T>::Type; | |
| 138 template <typename T> using WebPassType = typename WebPassTypeImpl<T>::Type; | |
| 139 template <typename T> static T& adopt(T& x) { return x; } | 119 template <typename T> static T& adopt(T& x) { return x; } |
| 140 template <typename T> | 120 template <typename T> static std::unique_ptr<T> adopt(std::unique_ptr<T>& x)
{ return std::move(x); } |
| 141 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; } |
| 142 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); } | 122 template <typename T> static std::unique_ptr<T> pass(std::unique_ptr<T>& x)
{ return std::move(x); } |
| 144 | 123 |
| 145 template <typename S, typename T> | 124 template <typename S, typename T> |
| 146 class Base : public WebCallbacks<WebPassType<typename S::WebType>, WebPassTy
pe<typename T::WebType>> { | 125 class Base : public WebCallbacks<typename S::WebType, typename T::WebType> { |
| 147 public: | 126 public: |
| 148 explicit Base(ScriptPromiseResolver* resolver) : m_resolver(resolver) {} | 127 explicit Base(ScriptPromiseResolver* resolver) : m_resolver(resolver) {} |
| 149 ScriptPromiseResolver* resolver() { return m_resolver; } | 128 ScriptPromiseResolver* resolver() { return m_resolver; } |
| 150 | 129 |
| 151 private: | 130 private: |
| 152 Persistent<ScriptPromiseResolver> m_resolver; | 131 Persistent<ScriptPromiseResolver> m_resolver; |
| 153 }; | 132 }; |
| 154 | 133 |
| 155 template <typename S, typename T> | 134 template <typename S, typename T> |
| 156 class OnSuccess : public Base<S, T> { | 135 class OnSuccess : public Base<S, T> { |
| 157 public: | 136 public: |
| 158 explicit OnSuccess(ScriptPromiseResolver* resolver) : Base<S, T>(resolve
r) {} | 137 explicit OnSuccess(ScriptPromiseResolver* resolver) : Base<S, T>(resolve
r) {} |
| 159 void onSuccess(WebPassType<typename S::WebType> r) override | 138 void onSuccess(typename S::WebType r) override |
| 160 { | 139 { |
| 161 typename S::WebType result(adopt(r)); | 140 typename S::WebType result(adopt(r)); |
| 162 ScriptPromiseResolver* resolver = this->resolver(); | 141 ScriptPromiseResolver* resolver = this->resolver(); |
| 163 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 142 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| 164 return; | 143 return; |
| 165 resolver->resolve(S::take(resolver, pass(result))); | 144 resolver->resolve(S::take(resolver, pass(result))); |
| 166 } | 145 } |
| 167 }; | 146 }; |
| 168 template <typename T> | 147 template <typename T> |
| 169 class OnSuccess<CallbackPromiseAdapterTrivialWebTypeHolder<void>, T> : publi
c Base<CallbackPromiseAdapterTrivialWebTypeHolder<void>, T> { | 148 class OnSuccess<CallbackPromiseAdapterTrivialWebTypeHolder<void>, T> : publi
c Base<CallbackPromiseAdapterTrivialWebTypeHolder<void>, T> { |
| 170 public: | 149 public: |
| 171 explicit OnSuccess(ScriptPromiseResolver* resolver) : Base<CallbackPromi
seAdapterTrivialWebTypeHolder<void>, T>(resolver) {} | 150 explicit OnSuccess(ScriptPromiseResolver* resolver) : Base<CallbackPromi
seAdapterTrivialWebTypeHolder<void>, T>(resolver) {} |
| 172 void onSuccess() override | 151 void onSuccess() override |
| 173 { | 152 { |
| 174 ScriptPromiseResolver* resolver = this->resolver(); | 153 ScriptPromiseResolver* resolver = this->resolver(); |
| 175 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 154 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| 176 return; | 155 return; |
| 177 resolver->resolve(); | 156 resolver->resolve(); |
| 178 } | 157 } |
| 179 }; | 158 }; |
| 180 template <typename S, typename T> | 159 template <typename S, typename T> |
| 181 class OnError : public OnSuccess<S, T> { | 160 class OnError : public OnSuccess<S, T> { |
| 182 public: | 161 public: |
| 183 explicit OnError(ScriptPromiseResolver* resolver) : OnSuccess<S, T>(reso
lver) {} | 162 explicit OnError(ScriptPromiseResolver* resolver) : OnSuccess<S, T>(reso
lver) {} |
| 184 void onError(WebPassType<typename T::WebType> e) override | 163 void onError(typename T::WebType e) override |
| 185 { | 164 { |
| 186 typename T::WebType result(adopt(e)); | 165 typename T::WebType result(adopt(e)); |
| 187 ScriptPromiseResolver* resolver = this->resolver(); | 166 ScriptPromiseResolver* resolver = this->resolver(); |
| 188 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) | 167 if (!resolver->getExecutionContext() || resolver->getExecutionContex
t()->activeDOMObjectsAreStopped()) |
| 189 return; | 168 return; |
| 190 ScriptState::Scope scope(resolver->getScriptState()); | 169 ScriptState::Scope scope(resolver->getScriptState()); |
| 191 resolver->reject(T::take(resolver, pass(result))); | 170 resolver->reject(T::take(resolver, pass(result))); |
| 192 } | 171 } |
| 193 }; | 172 }; |
| 194 template <typename S> | 173 template <typename S> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 214 }; | 193 }; |
| 215 | 194 |
| 216 } // namespace internal | 195 } // namespace internal |
| 217 | 196 |
| 218 template <typename S, typename T> | 197 template <typename S, typename T> |
| 219 using CallbackPromiseAdapter = internal::CallbackPromiseAdapterInternal::Callbac
kPromiseAdapter<S, T>; | 198 using CallbackPromiseAdapter = internal::CallbackPromiseAdapterInternal::Callbac
kPromiseAdapter<S, T>; |
| 220 | 199 |
| 221 } // namespace blink | 200 } // namespace blink |
| 222 | 201 |
| 223 #endif | 202 #endif |
| OLD | NEW |