| 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 ScriptPromiseResolver_h | 31 #ifndef ScriptPromiseResolver_h |
| 32 #define ScriptPromiseResolver_h | 32 #define ScriptPromiseResolver_h |
| 33 | 33 |
| 34 #include "bindings/v8/ScopedPersistent.h" | 34 #include "bindings/v8/ScopedPersistent.h" |
| 35 #include "bindings/v8/ScriptObject.h" | 35 #include "bindings/v8/ScriptObject.h" |
| 36 #include "bindings/v8/ScriptPromise.h" |
| 36 #include "bindings/v8/ScriptState.h" | 37 #include "bindings/v8/ScriptState.h" |
| 37 #include "bindings/v8/ScriptValue.h" | 38 #include "bindings/v8/ScriptValue.h" |
| 38 #include "wtf/RefPtr.h" | 39 #include "wtf/RefPtr.h" |
| 39 | 40 |
| 40 #include <v8.h> | 41 #include <v8.h> |
| 41 | 42 |
| 42 namespace WebCore { | 43 namespace WebCore { |
| 43 | 44 |
| 44 class ScriptExecutionContext; | 45 class ScriptExecutionContext; |
| 45 | 46 |
| 46 // ScriptPromiseResolver is a class for accessing PromiseResolver methods | 47 // ScriptPromiseResolver is a class for accessing PromiseResolver methods |
| 47 // (fulfill / resolve / reject) from C++ world. | 48 // (fulfill / resolve / reject) from C++ world. |
| 48 // ScriptPromiseResolver holds a Promise and a PromiseResolver. | 49 // ScriptPromiseResolver holds a Promise and a PromiseResolver. |
| 49 // All methods of this class must be called from the main thread. | 50 // All methods of this class must be called from the main thread. |
| 50 // Here is a typical usage: | 51 // Here is a typical usage: |
| 51 // 1. Create a ScriptPromiseResolver. | 52 // 1. Create a ScriptPromiseResolver. |
| 52 // 2. Pass the promise object of the holder to a JavaScript program | 53 // 2. Pass the promise object of the holder to a JavaScript program |
| 53 // (such as XMLHttpRequest return value). | 54 // (such as XMLHttpRequest return value). |
| 54 // 3. Detach the promise object if you no long need it. | 55 // 3. Detach the promise object if you no longer need it. |
| 55 // 4. Call fulfill or reject when the operation completes or | 56 // 4. Call fulfill or reject when the operation completes or |
| 56 // the operation fails respectively. | 57 // the operation fails respectively. |
| 57 // | 58 // |
| 58 // Most methods including constructors must be called within a v8 context. | 59 // Most methods including constructors must be called within a v8 context. |
| 59 // To use ScriptPromiseResolver out of a v8 context the caller must | 60 // To use ScriptPromiseResolver out of a v8 context the caller must |
| 60 // enter a v8 context, for example by using ScriptScope and ScriptState. | 61 // enter a v8 context, for example by using ScriptScope and ScriptState. |
| 61 // | 62 // |
| 63 // If you hold ScriptPromiseResolver as a member variable in a DOM object, |
| 64 // it causes memory leaks unless you detach the promise and resolver object |
| 65 // manually. |
| 66 // So if you no longer need the promise object, you should call detachPromise. |
| 67 // And if the operation completes or fails, you should call fulfill / resolve / |
| 68 // reject. Destroying ScriptPromiseResolver will also detach the promise and |
| 69 // resolver object. |
| 70 // |
| 62 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> { | 71 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> { |
| 63 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); | 72 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); |
| 64 public: | 73 public: |
| 65 static PassRefPtr<ScriptPromiseResolver> create(ScriptExecutionContext*); | 74 static PassRefPtr<ScriptPromiseResolver> create(ScriptExecutionContext*); |
| 66 static PassRefPtr<ScriptPromiseResolver> create(); | 75 static PassRefPtr<ScriptPromiseResolver> create(); |
| 67 | 76 |
| 68 // A ScriptPromiseResolver should be fulfilled / resolved / rejected before | 77 // A ScriptPromiseResolver should be fulfilled / resolved / rejected before |
| 69 // its destruction. | 78 // its destruction. |
| 70 // A ScriptPromiseResolver can be destructed safely without | 79 // A ScriptPromiseResolver can be destructed safely without |
| 71 // entering a v8 context. | 80 // entering a v8 context. |
| 72 ~ScriptPromiseResolver(); | 81 ~ScriptPromiseResolver(); |
| 73 | 82 |
| 74 // Detach the promise object and reject the resolver object with undefined. | 83 // Detach the promise object and reject the resolver object with undefined. |
| 75 void detach(); | 84 void detach(); |
| 76 | 85 |
| 77 // Detach the promise object. | 86 // Detach the promise object. |
| 78 void detachPromise() { m_promise.clear(); } | 87 void detachPromise() { m_promise.clear(); } |
| 79 | 88 |
| 80 // Return true if the following conditions are met: | 89 // Return true if the following conditions are met: |
| 81 // - The resolver object is not detached. | 90 // - The resolver object is not detached. |
| 82 // - The resolver's promise object is in pending state. | 91 // - The resolver's promise object is in pending state. |
| 83 // - The resolver's resolved flag is not set. | 92 // - The resolver's resolved flag is not set. |
| 84 bool isPending() const; | 93 bool isPending() const; |
| 85 | 94 |
| 86 ScriptObject promise() | 95 ScriptPromise promise() |
| 87 { | 96 { |
| 88 ASSERT(v8::Context::InContext()); | 97 ASSERT(v8::Context::InContext()); |
| 89 return ScriptObject(ScriptState::current(), m_promise.newLocal(m_isolate
)); | 98 return m_promise; |
| 90 } | 99 } |
| 91 | 100 |
| 92 // Fulfill with a C++ object which can be converted to a v8 object by toV8. | 101 // Fulfill with a C++ object which can be converted to a v8 object by toV8. |
| 93 template<typename T> | 102 template<typename T> |
| 94 inline void fulfill(PassRefPtr<T>); | 103 inline void fulfill(PassRefPtr<T>); |
| 95 // Resolve with a C++ object which can be converted to a v8 object by toV8. | 104 // Resolve with a C++ object which can be converted to a v8 object by toV8. |
| 96 template<typename T> | 105 template<typename T> |
| 97 inline void resolve(PassRefPtr<T>); | 106 inline void resolve(PassRefPtr<T>); |
| 98 // Reject with a C++ object which can be converted to a v8 object by toV8. | 107 // Reject with a C++ object which can be converted to a v8 object by toV8. |
| 99 template<typename T> | 108 template<typename T> |
| 100 inline void reject(PassRefPtr<T>); | 109 inline void reject(PassRefPtr<T>); |
| 101 | 110 |
| 102 void fulfill(ScriptValue); | 111 void fulfill(ScriptValue); |
| 103 void resolve(ScriptValue); | 112 void resolve(ScriptValue); |
| 104 void reject(ScriptValue); | 113 void reject(ScriptValue); |
| 105 | 114 |
| 106 private: | 115 private: |
| 107 ScriptPromiseResolver(v8::Handle<v8::Object> creationContext, v8::Isolate*); | 116 ScriptPromiseResolver(v8::Handle<v8::Object> creationContext, v8::Isolate*); |
| 108 void fulfill(v8::Handle<v8::Value>); | 117 void fulfill(v8::Handle<v8::Value>); |
| 109 void resolve(v8::Handle<v8::Value>); | 118 void resolve(v8::Handle<v8::Value>); |
| 110 void reject(v8::Handle<v8::Value>); | 119 void reject(v8::Handle<v8::Value>); |
| 111 | 120 |
| 112 v8::Isolate* m_isolate; | 121 v8::Isolate* m_isolate; |
| 113 ScopedPersistent<v8::Object> m_promise; | 122 ScriptPromise m_promise; |
| 114 ScopedPersistent<v8::Object> m_resolver; | 123 ScopedPersistent<v8::Object> m_resolver; |
| 115 bool isPendingInternal() const; | 124 bool isPendingInternal() const; |
| 116 }; | 125 }; |
| 117 | 126 |
| 118 template<typename T> | 127 template<typename T> |
| 119 void ScriptPromiseResolver::fulfill(PassRefPtr<T> value) | 128 void ScriptPromiseResolver::fulfill(PassRefPtr<T> value) |
| 120 { | 129 { |
| 121 ASSERT(v8::Context::InContext()); | 130 ASSERT(v8::Context::InContext()); |
| 122 fulfill(toV8(value.get(), v8::Object::New(), m_isolate)); | 131 fulfill(toV8(value.get(), v8::Object::New(), m_isolate)); |
| 123 } | 132 } |
| 124 | 133 |
| 125 template<typename T> | 134 template<typename T> |
| 126 void ScriptPromiseResolver::resolve(PassRefPtr<T> value) | 135 void ScriptPromiseResolver::resolve(PassRefPtr<T> value) |
| 127 { | 136 { |
| 128 ASSERT(v8::Context::InContext()); | 137 ASSERT(v8::Context::InContext()); |
| 129 resolve(toV8(value.get(), v8::Object::New(), m_isolate)); | 138 resolve(toV8(value.get(), v8::Object::New(), m_isolate)); |
| 130 } | 139 } |
| 131 | 140 |
| 132 template<typename T> | 141 template<typename T> |
| 133 void ScriptPromiseResolver::reject(PassRefPtr<T> value) | 142 void ScriptPromiseResolver::reject(PassRefPtr<T> value) |
| 134 { | 143 { |
| 135 ASSERT(v8::Context::InContext()); | 144 ASSERT(v8::Context::InContext()); |
| 136 reject(toV8(value.get(), v8::Object::New(), m_isolate)); | 145 reject(toV8(value.get(), v8::Object::New(), m_isolate)); |
| 137 } | 146 } |
| 138 | 147 |
| 139 } // namespace WebCore | 148 } // namespace WebCore |
| 140 | 149 |
| 141 | 150 |
| 142 #endif // ScriptPromiseResolver_h | 151 #endif // ScriptPromiseResolver_h |
| OLD | NEW |