| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 | 44 |
| 45 namespace WebCore { | 45 namespace WebCore { |
| 46 | 46 |
| 47 class ExecutionContext; | 47 class ExecutionContext; |
| 48 | 48 |
| 49 // ScriptPromiseResolver is a class for performing operations on Promise | 49 // ScriptPromiseResolver is a class for performing operations on Promise |
| 50 // (resolve / reject) from C++ world. | 50 // (resolve / reject) from C++ world. |
| 51 // ScriptPromiseResolver holds a PromiseResolver. | 51 // ScriptPromiseResolver holds a PromiseResolver. |
| 52 // Here is a typical usage: | 52 // Here is a typical usage: |
| 53 // 1. Create a ScriptPromiseResolver. | 53 // 1. Create a ScriptPromiseResolver. |
| 54 // 2. Pass the promise object of the holder to a JavaScript program | 54 // 2. Pass the associated promise object to a JavaScript program |
| 55 // (such as XMLHttpRequest return value). | 55 // (such as XMLHttpRequest return value). |
| 56 // 3. Call resolve or reject when the operation completes or | 56 // 3. Call resolve or reject when the operation completes or |
| 57 // the operation fails respectively. | 57 // the operation fails respectively. |
| 58 // | 58 // |
| 59 // Most methods including constructors must be called within a v8 context. | 59 // Most methods including constructors must be called within a v8 context. |
| 60 // To use ScriptPromiseResolver out of a v8 context the caller must | 60 // To use ScriptPromiseResolver out of a v8 context the caller must |
| 61 // enter a v8 context, for example by using ScriptScope and ScriptState. | 61 // enter a v8 context, for example by using ScriptScope and ScriptState. |
| 62 // | 62 // |
| 63 // To prevent memory leaks, you should release the reference manually | 63 // To prevent memory leaks, you should release the reference manually |
| 64 // by calling resolve or reject. | 64 // by calling resolve or reject. |
| 65 // Destroying the object will also release the reference. | 65 // Destroying the object will also release the reference. |
| 66 // | 66 // Note that ScriptPromiseResolver::promise returns an empty value when the |
| 67 // resolver is already resolved or rejected. If you want to resolve a resolver |
| 68 // immediately and return the associated promise, you should get the promise |
| 69 // before resolving. |
| 67 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> { | 70 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> { |
| 68 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); | 71 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); |
| 69 public: | 72 public: |
| 70 static PassRefPtr<ScriptPromiseResolver> create(ExecutionContext*); | 73 static PassRefPtr<ScriptPromiseResolver> create(ExecutionContext*); |
| 71 static PassRefPtr<ScriptPromiseResolver> create(v8::Isolate*); | 74 static PassRefPtr<ScriptPromiseResolver> create(v8::Isolate*); |
| 72 | 75 |
| 73 // A ScriptPromiseResolver should be resolved / rejected before | 76 // A ScriptPromiseResolver should be resolved / rejected before |
| 74 // its destruction. | 77 // its destruction. |
| 75 // A ScriptPromiseResolver can be destructed safely without | 78 // A ScriptPromiseResolver can be destructed safely without |
| 76 // entering a v8 context. | 79 // entering a v8 context. |
| 77 ~ScriptPromiseResolver(); | 80 ~ScriptPromiseResolver(); |
| 78 | 81 |
| 79 // Return true if the promise object is in pending state. | 82 // Returns the underlying Promise. |
| 80 bool isPending() const; | 83 // Note that the underlying Promise is cleared when |resolve| or |reject| |
| 81 | 84 // is called. |
| 82 ScriptPromise promise() | 85 ScriptPromise promise(); |
| 83 { | |
| 84 ASSERT(m_promise.isolate()->InContext()); | |
| 85 return m_promise; | |
| 86 } | |
| 87 | 86 |
| 88 // To use following template methods, T must be a DOM class. | 87 // To use following template methods, T must be a DOM class. |
| 89 | 88 |
| 90 // This method will be implemented by the code generator. | 89 // This method will be implemented by the code generator. |
| 91 template<typename T> | 90 template<typename T> |
| 92 void resolve(T* value, v8::Handle<v8::Object> creationContext) { resolve(toV
8NoInline(value, creationContext, m_isolate)); } | 91 void resolve(T* value, v8::Handle<v8::Object> creationContext) { resolve(toV
8NoInline(value, creationContext, m_isolate)); } |
| 93 template<typename T> | 92 template<typename T> |
| 94 void reject(T* value, v8::Handle<v8::Object> creationContext) { reject(toV8N
oInline(value, creationContext, m_isolate)); } | 93 void reject(T* value, v8::Handle<v8::Object> creationContext) { reject(toV8N
oInline(value, creationContext, m_isolate)); } |
| 95 | 94 |
| 96 template<typename T> | 95 template<typename T> |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 void reject(ScriptValue); | 133 void reject(ScriptValue); |
| 135 | 134 |
| 136 private: | 135 private: |
| 137 ScriptPromiseResolver(ExecutionContext*); | 136 ScriptPromiseResolver(ExecutionContext*); |
| 138 ScriptPromiseResolver(v8::Isolate*); | 137 ScriptPromiseResolver(v8::Isolate*); |
| 139 | 138 |
| 140 void resolve(v8::Handle<v8::Value>); | 139 void resolve(v8::Handle<v8::Value>); |
| 141 void reject(v8::Handle<v8::Value>); | 140 void reject(v8::Handle<v8::Value>); |
| 142 | 141 |
| 143 v8::Isolate* m_isolate; | 142 v8::Isolate* m_isolate; |
| 143 // Used when scriptPromiseOnV8Promise is disabled. |
| 144 ScriptPromise m_promise; | 144 ScriptPromise m_promise; |
| 145 // Used when scriptPromiseOnV8Promise is enabled. |
| 146 ScriptValue m_resolver; |
| 145 }; | 147 }; |
| 146 | 148 |
| 147 template<typename T> | 149 template<typename T> |
| 148 void ScriptPromiseResolver::resolve(T* value, ExecutionContext* context) | 150 void ScriptPromiseResolver::resolve(T* value, ExecutionContext* context) |
| 149 { | 151 { |
| 150 ASSERT(m_isolate->InContext()); | 152 ASSERT(m_isolate->InContext()); |
| 151 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::cu
rrent(m_isolate)); | 153 v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::cu
rrent(m_isolate)); |
| 152 resolve(value, v8Context->Global()); | 154 resolve(value, v8Context->Global()); |
| 153 } | 155 } |
| 154 | 156 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 171 void ScriptPromiseResolver::reject(T* value) | 173 void ScriptPromiseResolver::reject(T* value) |
| 172 { | 174 { |
| 173 ASSERT(m_isolate->InContext()); | 175 ASSERT(m_isolate->InContext()); |
| 174 reject(value, v8::Object::New(m_isolate)); | 176 reject(value, v8::Object::New(m_isolate)); |
| 175 } | 177 } |
| 176 | 178 |
| 177 } // namespace WebCore | 179 } // namespace WebCore |
| 178 | 180 |
| 179 | 181 |
| 180 #endif // ScriptPromiseResolver_h | 182 #endif // ScriptPromiseResolver_h |
| OLD | NEW |