| 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 28 matching lines...) Expand all Loading... |
| 39 #include "wtf/RefPtr.h" | 39 #include "wtf/RefPtr.h" |
| 40 | 40 |
| 41 #include <v8.h> | 41 #include <v8.h> |
| 42 | 42 |
| 43 namespace WebCore { | 43 namespace WebCore { |
| 44 | 44 |
| 45 class ExecutionContext; | 45 class ExecutionContext; |
| 46 | 46 |
| 47 // ScriptPromiseResolver is a class for performing operations on Promise | 47 // ScriptPromiseResolver is a class for performing operations on Promise |
| 48 // (resolve / reject) from C++ world. | 48 // (resolve / reject) from C++ world. |
| 49 // ScriptPromiseResolver holds a Promise and a PromiseResolver. | 49 // ScriptPromiseResolver holds a PromiseResolver. |
| 50 // All methods of this class must be called from the main thread. | |
| 51 // Here is a typical usage: | 50 // Here is a typical usage: |
| 52 // 1. Create a ScriptPromiseResolver. | 51 // 1. Create a ScriptPromiseResolver from a ScriptPromise. |
| 53 // 2. Pass the promise object of the holder to a JavaScript program | 52 // 2. Pass the promise object of the holder to a JavaScript program |
| 54 // (such as XMLHttpRequest return value). | 53 // (such as XMLHttpRequest return value). |
| 55 // 3. Detach the promise object if you no longer need it. | 54 // 3. Call resolve or reject when the operation completes or |
| 56 // 4. Call resolve or reject when the operation completes or | |
| 57 // the operation fails respectively. | 55 // the operation fails respectively. |
| 58 // | 56 // |
| 59 // Most methods including constructors must be called within a v8 context. | 57 // Most methods including constructors must be called within a v8 context. |
| 60 // To use ScriptPromiseResolver out of a v8 context the caller must | 58 // To use ScriptPromiseResolver out of a v8 context the caller must |
| 61 // enter a v8 context, for example by using ScriptScope and ScriptState. | 59 // enter a v8 context, for example by using ScriptScope and ScriptState. |
| 62 // | 60 // |
| 63 // If you hold ScriptPromiseResolver as a member variable in a DOM object, | 61 // To prevent memory leaks, you should release the reference manually |
| 64 // it causes memory leaks unless you detach the promise object manually. | 62 // by calling resolve or reject. |
| 65 // Logically ScriptPromiseResolver has 2 references to the promise object. | 63 // Destroying the object will also release the reference. |
| 66 // One is for exposing the promise object, another is for resolving it. | |
| 67 // To prevent memory leaks, you should release these 2 references manually. | |
| 68 // Following operations release references to the promise object. | |
| 69 // 1. detachPromise releases the reference for exposing. | |
| 70 // 2. resolve / reject operations release the reference for resolving. | |
| 71 // 3. detach releases both references. | |
| 72 // 4. Destroying ScriptPromiseResolver releases both references. | |
| 73 // | |
| 74 // So if you no longer need the promise object, you should call detachPromise. | |
| 75 // And if the operation completes or fails, you should call resolve / reject. | |
| 76 // Destroying ScriptPromiseResolver will also detach the promise object. | |
| 77 // | 64 // |
| 78 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> { | 65 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> { |
| 79 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); | 66 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); |
| 80 public: | 67 public: |
| 81 static PassRefPtr<ScriptPromiseResolver> create(ExecutionContext*); | 68 static PassRefPtr<ScriptPromiseResolver> create(ScriptPromise, ExecutionCont
ext*); |
| 82 static PassRefPtr<ScriptPromiseResolver> create(); | 69 static PassRefPtr<ScriptPromiseResolver> create(ScriptPromise); |
| 83 | 70 |
| 84 // A ScriptPromiseResolver should be resolved / rejected before | 71 // A ScriptPromiseResolver should be resolved / rejected before |
| 85 // its destruction. | 72 // its destruction. |
| 86 // A ScriptPromiseResolver can be destructed safely without | 73 // A ScriptPromiseResolver can be destructed safely without |
| 87 // entering a v8 context. | 74 // entering a v8 context. |
| 88 ~ScriptPromiseResolver(); | 75 ~ScriptPromiseResolver(); |
| 89 | 76 |
| 90 // Reject the promise with undefined and detach it. | |
| 91 void detach(); | |
| 92 | |
| 93 // Detach the promise object. | |
| 94 void detachPromise(); | |
| 95 | |
| 96 // Return true if the promise object is in pending state. | 77 // Return true if the promise object is in pending state. |
| 97 bool isPending() const; | 78 bool isPending() const; |
| 98 | 79 |
| 99 ScriptPromise promise() | 80 ScriptPromise promise() |
| 100 { | 81 { |
| 101 ASSERT(v8::Context::InContext()); | 82 ASSERT(v8::Context::InContext()); |
| 102 return m_promise; | 83 return m_promise; |
| 103 } | 84 } |
| 104 | 85 |
| 105 // Fulfill with a C++ object which can be converted to a v8 object by toV8. | |
| 106 // This method "fulfill" is the deprecated alias to resolve. | |
| 107 template<typename T> | |
| 108 inline void fulfill(PassRefPtr<T>); | |
| 109 // Resolve with a C++ object which can be converted to a v8 object by toV8. | 86 // Resolve with a C++ object which can be converted to a v8 object by toV8. |
| 110 template<typename T> | 87 template<typename T> |
| 111 inline void resolve(PassRefPtr<T>); | 88 inline void resolve(PassRefPtr<T>); |
| 112 // Reject with a C++ object which can be converted to a v8 object by toV8. | 89 // Reject with a C++ object which can be converted to a v8 object by toV8. |
| 113 template<typename T> | 90 template<typename T> |
| 114 inline void reject(PassRefPtr<T>); | 91 inline void reject(PassRefPtr<T>); |
| 115 | 92 |
| 116 // This method "fulfill" is the deprecated alias to resolve. | |
| 117 void fulfill(ScriptValue); | |
| 118 void resolve(ScriptValue); | 93 void resolve(ScriptValue); |
| 119 void reject(ScriptValue); | 94 void reject(ScriptValue); |
| 120 | 95 |
| 121 private: | 96 private: |
| 122 ScriptPromiseResolver(v8::Handle<v8::Object> creationContext, v8::Isolate*); | 97 ScriptPromiseResolver(ScriptPromise, v8::Isolate*); |
| 123 void fulfill(v8::Handle<v8::Value>); | |
| 124 void resolve(v8::Handle<v8::Value>); | 98 void resolve(v8::Handle<v8::Value>); |
| 125 void reject(v8::Handle<v8::Value>); | 99 void reject(v8::Handle<v8::Value>); |
| 126 | 100 |
| 127 v8::Isolate* m_isolate; | 101 v8::Isolate* m_isolate; |
| 128 ScriptPromise m_promise; | 102 ScriptPromise m_promise; |
| 129 bool m_promiseForExposeDetached : 1; | |
| 130 bool m_promiseForResolveDetached : 1; | |
| 131 | |
| 132 void detachPromiseForExpose(); | |
| 133 void detachPromiseForResolve(); | |
| 134 }; | 103 }; |
| 135 | 104 |
| 136 template<typename T> | 105 template<typename T> |
| 137 void ScriptPromiseResolver::fulfill(PassRefPtr<T> value) | |
| 138 { | |
| 139 ASSERT(v8::Context::InContext()); | |
| 140 fulfill(toV8(value.get(), v8::Object::New(), m_isolate)); | |
| 141 } | |
| 142 | |
| 143 template<typename T> | |
| 144 void ScriptPromiseResolver::resolve(PassRefPtr<T> value) | 106 void ScriptPromiseResolver::resolve(PassRefPtr<T> value) |
| 145 { | 107 { |
| 146 ASSERT(v8::Context::InContext()); | 108 ASSERT(v8::Context::InContext()); |
| 147 resolve(toV8(value.get(), v8::Object::New(), m_isolate)); | 109 resolve(toV8(value.get(), v8::Object::New(), m_isolate)); |
| 148 } | 110 } |
| 149 | 111 |
| 150 template<typename T> | 112 template<typename T> |
| 151 void ScriptPromiseResolver::reject(PassRefPtr<T> value) | 113 void ScriptPromiseResolver::reject(PassRefPtr<T> value) |
| 152 { | 114 { |
| 153 ASSERT(v8::Context::InContext()); | 115 ASSERT(v8::Context::InContext()); |
| 154 reject(toV8(value.get(), v8::Object::New(), m_isolate)); | 116 reject(toV8(value.get(), v8::Object::New(), m_isolate)); |
| 155 } | 117 } |
| 156 | 118 |
| 157 } // namespace WebCore | 119 } // namespace WebCore |
| 158 | 120 |
| 159 | 121 |
| 160 #endif // ScriptPromiseResolver_h | 122 #endif // ScriptPromiseResolver_h |
| OLD | NEW |