| 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 13 matching lines...) Expand all Loading... |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 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 ScriptPromiseResolver_h | 31 #ifndef ScriptPromiseResolver_h |
| 32 #define ScriptPromiseResolver_h | 32 #define ScriptPromiseResolver_h |
| 33 | 33 |
| 34 #include "bindings/v8/DOMWrapperWorld.h" | 34 #include "bindings/v8/NewScriptState.h" |
| 35 #include "bindings/v8/ScopedPersistent.h" | |
| 36 #include "bindings/v8/ScriptObject.h" | |
| 37 #include "bindings/v8/ScriptPromise.h" | 35 #include "bindings/v8/ScriptPromise.h" |
| 38 #include "bindings/v8/ScriptState.h" | |
| 39 #include "bindings/v8/ScriptValue.h" | 36 #include "bindings/v8/ScriptValue.h" |
| 40 #include "bindings/v8/V8Binding.h" | 37 #include "bindings/v8/V8Binding.h" |
| 41 #include "wtf/RefPtr.h" | 38 #include "wtf/RefPtr.h" |
| 42 | 39 |
| 43 #include <v8.h> | 40 #include <v8.h> |
| 44 | 41 |
| 45 namespace WebCore { | 42 namespace WebCore { |
| 46 | 43 |
| 47 class ExecutionContext; | 44 class ExecutionContext; |
| 48 | 45 |
| 49 // ScriptPromiseResolver is a class for performing operations on Promise | 46 // ScriptPromiseResolver is a class for performing operations on Promise |
| 50 // (resolve / reject) from C++ world. | 47 // (resolve / reject) from C++ world. |
| 51 // ScriptPromiseResolver holds a PromiseResolver. | 48 // ScriptPromiseResolver holds a PromiseResolver. |
| 52 // Here is a typical usage: | 49 // Here is a typical usage: |
| 53 // 1. Create a ScriptPromiseResolver. | 50 // 1. Create a ScriptPromiseResolver. |
| 54 // 2. Pass the associated promise object to a JavaScript program | 51 // 2. Pass the associated promise object to a JavaScript program |
| 55 // (such as XMLHttpRequest return value). | 52 // (such as XMLHttpRequest return value). |
| 56 // 3. Call resolve or reject when the operation completes or | 53 // 3. Call resolve or reject when the operation completes or |
| 57 // the operation fails respectively. | 54 // the operation fails respectively. |
| 58 // | 55 // |
| 59 // Most methods including constructors must be called within a v8 context. | 56 // Most methods including constructors must be called within a v8 context. |
| 60 // To use ScriptPromiseResolver out of a v8 context the caller must | 57 // To use ScriptPromiseResolver out of a v8 context the caller must |
| 61 // enter a v8 context. ScriptPromiseResolverWithContext provides such | 58 // enter a v8 context. Please use ScriptPromiseResolverWithContext |
| 62 // functionality. | 59 // in such cases. |
| 63 // | 60 // |
| 64 // To prevent memory leaks, you should release the reference manually | 61 // To prevent memory leaks, you should release the reference manually |
| 65 // by calling resolve or reject. | 62 // by calling resolve or reject. |
| 66 // Destroying the object will also release the reference. | 63 // Destroying the object will also release the reference. |
| 67 // Note that ScriptPromiseResolver::promise returns an empty value when the | 64 // Note that ScriptPromiseResolver::promise returns an empty value when the |
| 68 // resolver is already resolved or rejected. If you want to resolve a resolver | 65 // resolver is already resolved or rejected. If you want to resolve a resolver |
| 69 // immediately and return the associated promise, you should get the promise | 66 // immediately and return the associated promise, you should get the promise |
| 70 // before resolving. | 67 // before resolving. |
| 71 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> { | 68 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> { |
| 72 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); | 69 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); |
| 73 public: | 70 public: |
| 74 static PassRefPtr<ScriptPromiseResolver> create(ExecutionContext*); | 71 static PassRefPtr<ScriptPromiseResolver> create(ExecutionContext*); |
| 75 static PassRefPtr<ScriptPromiseResolver> create(v8::Isolate*); | 72 static PassRefPtr<ScriptPromiseResolver> create(v8::Isolate*); |
| 76 | 73 |
| 77 // A ScriptPromiseResolver should be resolved / rejected before | 74 // A ScriptPromiseResolver should be resolved / rejected before |
| 78 // its destruction. | 75 // its destruction. |
| 79 // A ScriptPromiseResolver can be destructed safely without | 76 // A ScriptPromiseResolver can be destructed safely without |
| 80 // entering a v8 context. | 77 // entering a v8 context. |
| 81 ~ScriptPromiseResolver(); | 78 ~ScriptPromiseResolver(); |
| 82 | 79 |
| 83 // Returns the underlying Promise. | 80 // Returns the underlying Promise. |
| 84 // Note that the underlying Promise is cleared when |resolve| or |reject| | 81 // Note that the underlying Promise is cleared when |resolve| or |reject| |
| 85 // is called. | 82 // is called. |
| 86 ScriptPromise promise(); | 83 ScriptPromise promise(); |
| 87 | 84 |
| 88 // To use following template methods, T must be a DOM class. | |
| 89 template<typename T> | 85 template<typename T> |
| 90 void resolve(T* value, v8::Handle<v8::Object> creationContext) { resolve(toV
8NoInline(value, creationContext, m_isolate)); } | 86 void resolve(const T& value, ExecutionContext* executionContext) |
| 87 { |
| 88 ASSERT(m_isolate->InContext()); |
| 89 // You should use ScriptPromiseResolverWithContext when you want |
| 90 // to resolve a Promise in a non-original V8 context. |
| 91 return resolve(value); |
| 92 } |
| 91 template<typename T> | 93 template<typename T> |
| 92 void reject(T* value, v8::Handle<v8::Object> creationContext) { reject(toV8N
oInline(value, creationContext, m_isolate)); } | 94 void reject(const T& value, ExecutionContext* executionContext) |
| 93 | 95 { |
| 96 ASSERT(m_isolate->InContext()); |
| 97 // You should use ScriptPromiseResolverWithContext when you want |
| 98 // to reject a Promise in a non-original V8 context. |
| 99 return reject(value); |
| 100 } |
| 94 template<typename T> | 101 template<typename T> |
| 95 void resolve(PassRefPtr<T> value, v8::Handle<v8::Object> creationContext) {
resolve(value.get(), creationContext); } | 102 void resolve(const T& value) |
| 103 { |
| 104 ASSERT(m_isolate->InContext()); |
| 105 resolve(toV8Value(value)); |
| 106 } |
| 96 template<typename T> | 107 template<typename T> |
| 97 void resolve(RawPtr<T> value, v8::Handle<v8::Object> creationContext) { reso
lve(value.get(), creationContext); } | 108 void reject(const T& value) |
| 98 template<typename T> | 109 { |
| 99 void reject(PassRefPtr<T> value, v8::Handle<v8::Object> creationContext) { r
eject(value.get(), creationContext); } | 110 ASSERT(m_isolate->InContext()); |
| 100 template<typename T> | 111 reject(toV8Value(value)); |
| 101 void reject(RawPtr<T> value, v8::Handle<v8::Object> creationContext) { rejec
t(value.get(), creationContext); } | 112 } |
| 102 | 113 template<typename T> void resolve(const T& value, v8::Handle<v8::Object> cre
ationContext) |
| 103 template<typename T> | 114 { |
| 104 inline void resolve(T* value, ExecutionContext*); | 115 ASSERT(m_isolate->InContext()); |
| 105 template<typename T> | 116 resolve(toV8Value(value, creationContext)); |
| 106 inline void reject(T* value, ExecutionContext*); | 117 } |
| 107 | 118 template<typename T> void reject(const T& value, v8::Handle<v8::Object> crea
tionContext) |
| 108 template<typename T> | 119 { |
| 109 void resolve(PassRefPtr<T> value, ExecutionContext* context) { resolve(value
.get(), context); } | 120 ASSERT(m_isolate->InContext()); |
| 110 template<typename T> | 121 reject(toV8Value(value, creationContext)); |
| 111 void resolve(RawPtr<T> value, ExecutionContext* context) { resolve(value.get
(), context); } | 122 } |
| 112 template<typename T> | |
| 113 void reject(PassRefPtr<T> value, ExecutionContext* context) { reject(value.g
et(), context); } | |
| 114 template<typename T> | |
| 115 void reject(RawPtr<T> value, ExecutionContext* context) { reject(value.get()
, context); } | |
| 116 | |
| 117 template<typename T> | |
| 118 inline void resolve(T* value); | |
| 119 template<typename T> | |
| 120 inline void reject(T* value); | |
| 121 | |
| 122 template<typename T, size_t inlineCapacity> | |
| 123 void resolve(const Vector<T, inlineCapacity>& iterator) { resolve(v8ArrayNoI
nline(iterator, m_isolate)); } | |
| 124 template<typename T, size_t inlineCapacity> | |
| 125 void reject(const Vector<T, inlineCapacity>& iterator) { reject(v8ArrayNoInl
ine(iterator, m_isolate)); } | |
| 126 | |
| 127 template<typename T> | |
| 128 void resolve(PassRefPtr<T> value) { resolve(value.get()); } | |
| 129 template<typename T> | |
| 130 void resolve(RawPtr<T> value) { resolve(value.get()); } | |
| 131 template<typename T> | |
| 132 void reject(PassRefPtr<T> value) { reject(value.get()); } | |
| 133 template<typename T> | |
| 134 void reject(RawPtr<T> value) { reject(value.get()); } | |
| 135 | |
| 136 void resolve(ScriptValue); | |
| 137 void reject(ScriptValue); | |
| 138 | 123 |
| 139 v8::Isolate* isolate() const { return m_isolate; } | 124 v8::Isolate* isolate() const { return m_isolate; } |
| 140 | 125 |
| 141 void resolve(v8::Handle<v8::Value>); | 126 void resolve(v8::Handle<v8::Value>); |
| 142 void reject(v8::Handle<v8::Value>); | 127 void reject(v8::Handle<v8::Value>); |
| 143 | 128 |
| 144 private: | 129 private: |
| 130 template<typename T> |
| 131 v8::Handle<v8::Value> toV8Value(const T& value, v8::Handle<v8::Object>) |
| 132 { |
| 133 // Default implementaion: for types that don't need the |
| 134 // creation context. |
| 135 return toV8Value(value); |
| 136 } |
| 137 template<typename T> |
| 138 v8::Handle<v8::Value> toV8Value(const T& value) |
| 139 { |
| 140 return V8ValueTraits<T>::toV8Value(value, m_isolate); |
| 141 } |
| 142 |
| 143 // Pointer specializations. |
| 144 template<typename T> |
| 145 v8::Handle<v8::Value> toV8Value(T* value, v8::Handle<v8::Object> creationCon
text) |
| 146 { |
| 147 return toV8NoInline(value, creationContext, m_isolate); |
| 148 } |
| 149 template<typename T> |
| 150 v8::Handle<v8::Value> toV8Value(T* value) |
| 151 { |
| 152 return toV8Value(value, NewScriptState::current(m_isolate)->context()->G
lobal()); |
| 153 } |
| 154 template<typename T> |
| 155 v8::Handle<v8::Value> toV8Value(RefPtr<T> value, v8::Handle<v8::Object> crea
tionContext) { return toV8Value(value.get(), creationContext); } |
| 156 template<typename T> |
| 157 v8::Handle<v8::Value> toV8Value(PassRefPtr<T> value, v8::Handle<v8::Object>
creationContext) { return toV8Value(value.get(), creationContext); } |
| 158 template<typename T> |
| 159 v8::Handle<v8::Value> toV8Value(OwnPtr<T> value, v8::Handle<v8::Object> crea
tionContext) { return toV8Value(value.get(), creationContext); } |
| 160 template<typename T> |
| 161 v8::Handle<v8::Value> toV8Value(PassOwnPtr<T> value, v8::Handle<v8::Object>
creationContext) { return toV8Value(value.get(), creationContext); } |
| 162 template<typename T> |
| 163 v8::Handle<v8::Value> toV8Value(RawPtr<T> value, v8::Handle<v8::Object> crea
tionContext) { return toV8Value(value.get(), creationContext); } |
| 164 template<typename T> v8::Handle<v8::Value> toV8Value(RefPtr<T> value) { retu
rn toV8Value(value.get()); } |
| 165 template<typename T> v8::Handle<v8::Value> toV8Value(PassRefPtr<T> value) {
return toV8Value(value.get()); } |
| 166 template<typename T> v8::Handle<v8::Value> toV8Value(OwnPtr<T> value) { retu
rn toV8Value(value.get()); } |
| 167 template<typename T> v8::Handle<v8::Value> toV8Value(PassOwnPtr<T> value) {
return toV8Value(value.get()); } |
| 168 template<typename T> v8::Handle<v8::Value> toV8Value(RawPtr<T> value) { retu
rn toV8Value(value.get()); } |
| 169 |
| 170 // const char* should use V8ValueTraits. |
| 171 v8::Handle<v8::Value> toV8Value(const char* value, v8::Handle<v8::Object>) {
return toV8Value(value); } |
| 172 v8::Handle<v8::Value> toV8Value(const char* value) { return V8ValueTraits<co
nst char*>::toV8Value(value, m_isolate); } |
| 173 |
| 174 template<typename T, size_t inlineCapacity> |
| 175 v8::Handle<v8::Value> toV8Value(const Vector<T, inlineCapacity>& value) |
| 176 { |
| 177 return v8ArrayNoInline(value, m_isolate); |
| 178 } |
| 145 ScriptPromiseResolver(ExecutionContext*); | 179 ScriptPromiseResolver(ExecutionContext*); |
| 146 ScriptPromiseResolver(v8::Isolate*); | 180 ScriptPromiseResolver(v8::Isolate*); |
| 147 | 181 |
| 148 v8::Isolate* m_isolate; | 182 v8::Isolate* m_isolate; |
| 149 // Used when scriptPromiseOnV8Promise is disabled. | 183 // Used when scriptPromiseOnV8Promise is disabled. |
| 150 ScriptPromise m_promise; | 184 ScriptPromise m_promise; |
| 151 // Used when scriptPromiseOnV8Promise is enabled. | 185 // Used when scriptPromiseOnV8Promise is enabled. |
| 152 ScriptValue m_resolver; | 186 ScriptValue m_resolver; |
| 153 }; | 187 }; |
| 154 | 188 |
| 155 template<typename T> | |
| 156 void ScriptPromiseResolver::resolve(T* value, ExecutionContext* context) | |
| 157 { | |
| 158 ASSERT(m_isolate->InContext()); | |
| 159 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::cu
rrent(m_isolate)); | |
| 160 resolve(value, v8Context->Global()); | |
| 161 } | |
| 162 | |
| 163 template<typename T> | |
| 164 void ScriptPromiseResolver::reject(T* value, ExecutionContext* context) | |
| 165 { | |
| 166 ASSERT(m_isolate->InContext()); | |
| 167 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::cu
rrent(m_isolate)); | |
| 168 reject(value, v8Context->Global()); | |
| 169 } | |
| 170 | |
| 171 template<typename T> | |
| 172 void ScriptPromiseResolver::resolve(T* value) | |
| 173 { | |
| 174 ASSERT(m_isolate->InContext()); | |
| 175 resolve(value, v8::Object::New(m_isolate)); | |
| 176 } | |
| 177 | |
| 178 template<typename T> | |
| 179 void ScriptPromiseResolver::reject(T* value) | |
| 180 { | |
| 181 ASSERT(m_isolate->InContext()); | |
| 182 reject(value, v8::Object::New(m_isolate)); | |
| 183 } | |
| 184 | |
| 185 } // namespace WebCore | 189 } // namespace WebCore |
| 186 | 190 |
| 187 | 191 |
| 188 #endif // ScriptPromiseResolver_h | 192 #endif // ScriptPromiseResolver_h |
| OLD | NEW |