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 14 matching lines...) Expand all Loading... | |
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/ScopedPersistent.h" | 34 #include "bindings/v8/ScopedPersistent.h" |
35 #include "bindings/v8/ScriptObject.h" | 35 #include "bindings/v8/ScriptPromise.h" |
36 #include "bindings/v8/ScriptState.h" | 36 #include "bindings/v8/ScriptState.h" |
37 #include "bindings/v8/ScriptValue.h" | 37 #include "bindings/v8/ScriptValue.h" |
38 #include "wtf/RefPtr.h" | 38 #include "wtf/RefPtr.h" |
39 | 39 |
40 #include <v8.h> | 40 #include <v8.h> |
41 | 41 |
42 namespace WebCore { | 42 namespace WebCore { |
43 | 43 |
44 class ScriptExecutionContext; | 44 class ScriptExecutionContext; |
45 | 45 |
46 // ScriptPromiseResolver is a class for accessing PromiseResolver methods | 46 // ScriptPromiseResolver is a class for accessing PromiseResolver methods |
47 // (fulfill / resolve / reject) from C++ world. | 47 // (fulfill / resolve / reject) from C++ world. |
48 // ScriptPromiseResolver holds a Promise and a PromiseResolver. | 48 // ScriptPromiseResolver holds a Promise and a PromiseResolver. |
49 // All methods of this class must be called from the main thread. | 49 // All methods of this class must be called from the main thread. |
50 // Here is a typical usage: | 50 // Here is a typical usage: |
51 // 1. Create a ScriptPromiseResolver. | 51 // 1. Create a ScriptPromiseResolver. |
52 // 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 |
53 // (such as XMLHttpRequest return value). | 53 // (such as XMLHttpRequest return value). |
54 // 3. Detach the promise object if you no long need it. | 54 // 3. Detach the promise object if you no long need it. |
55 // 4. Call fulfill or reject when the operation completes or | 55 // 4. Call fulfill or reject when the operation completes or |
56 // the operation fails respectively. | 56 // the operation fails respectively. |
57 // | 57 // |
58 // Most methods including constructors must be called within a v8 context. | 58 // Most methods including constructors must be called within a v8 context. |
59 // To use ScriptPromiseResolver out of a v8 context the caller must | 59 // To use ScriptPromiseResolver out of a v8 context the caller must |
60 // enter a v8 context, for example by using ScriptScope and ScriptState. | 60 // enter a v8 context, for example by using ScriptScope and ScriptState. |
61 // | 61 // |
62 class ScriptPromiseResolver : public RefCounted<ScriptPromiseResolver> { | 62 class ScriptPromiseResolver { |
yhirano
2013/09/02 11:58:17
Can ScriptPromiseResolver be a subclass of ScriptV
| |
63 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolver); | |
64 public: | 63 public: |
65 static PassRefPtr<ScriptPromiseResolver> create(ScriptExecutionContext*); | 64 ScriptPromiseResolver(); |
66 static PassRefPtr<ScriptPromiseResolver> create(); | 65 explicit ScriptPromiseResolver(ScriptValue resolver); |
66 ScriptPromiseResolver(v8::Handle<v8::Value> resolver, v8::Isolate*); | |
67 | |
68 static ScriptPromiseResolver create(ScriptExecutionContext*); | |
69 static ScriptPromiseResolver create(); | |
67 | 70 |
68 // A ScriptPromiseResolver should be fulfilled / resolved / rejected before | 71 // A ScriptPromiseResolver should be fulfilled / resolved / rejected before |
69 // its destruction. | 72 // its destruction. |
70 // A ScriptPromiseResolver can be destructed safely without | 73 // A ScriptPromiseResolver can be destructed safely without |
71 // entering a v8 context. | 74 // entering a v8 context. |
72 ~ScriptPromiseResolver(); | 75 ~ScriptPromiseResolver(); |
73 | 76 |
74 // Detach the promise object and reject the resolver object with undefined. | 77 // Detach the promise object and reject the resolver object with undefined. |
75 void detach(); | 78 void detach(); |
76 | 79 |
77 // Detach the promise object. | 80 // Detach the promise object. |
78 void detachPromise() { m_promise.clear(); } | 81 void detachPromise() { m_promise.clear(); } |
79 | 82 |
80 // Return true if the following conditions are met: | 83 // Return true if the following conditions are met: |
81 // - The resolver object is not detached. | 84 // - The resolver object is not detached. |
82 // - The resolver's promise object is in pending state. | 85 // - The resolver's promise object is in pending state. |
83 // - The resolver's resolved flag is not set. | 86 // - The resolver's resolved flag is not set. |
84 bool isPending() const; | 87 bool isPending() const; |
85 | 88 |
86 ScriptObject promise() | 89 ScriptPromise promise() |
87 { | 90 { |
88 ASSERT(v8::Context::InContext()); | 91 ASSERT(v8::Context::InContext()); |
89 return ScriptObject(ScriptState::current(), m_promise.newLocal(m_isolate )); | 92 return m_promise; |
90 } | 93 } |
91 | 94 |
92 // Fulfill with a C++ object which can be converted to a v8 object by toV8. | 95 // Fulfill with a C++ object which can be converted to a v8 object by toV8. |
93 template<typename T> | 96 template<typename T> |
94 inline void fulfill(PassRefPtr<T>); | 97 inline void fulfill(PassRefPtr<T>); |
95 // Resolve with a C++ object which can be converted to a v8 object by toV8. | 98 // Resolve with a C++ object which can be converted to a v8 object by toV8. |
96 template<typename T> | 99 template<typename T> |
97 inline void resolve(PassRefPtr<T>); | 100 inline void resolve(PassRefPtr<T>); |
98 // Reject with a C++ object which can be converted to a v8 object by toV8. | 101 // Reject with a C++ object which can be converted to a v8 object by toV8. |
99 template<typename T> | 102 template<typename T> |
100 inline void reject(PassRefPtr<T>); | 103 inline void reject(PassRefPtr<T>); |
101 | 104 |
102 void fulfill(ScriptValue); | 105 void fulfill(ScriptValue); |
103 void resolve(ScriptValue); | 106 void resolve(ScriptValue); |
104 void reject(ScriptValue); | 107 void reject(ScriptValue); |
105 | 108 |
109 bool isObject() const | |
110 { | |
111 return m_resolver.isObject(); | |
112 } | |
113 | |
114 bool isNull() const | |
115 { | |
116 return m_resolver.isNull(); | |
117 } | |
118 | |
119 bool isUndefinedOrNull() const | |
120 { | |
121 return m_resolver.isUndefined() || m_resolver.isNull(); | |
122 } | |
123 | |
124 v8::Handle<v8::Value> v8Value() const | |
125 { | |
126 return m_resolver.v8Value(); | |
127 } | |
128 | |
106 private: | 129 private: |
107 ScriptPromiseResolver(v8::Handle<v8::Object> creationContext, v8::Isolate*); | 130 ScriptPromiseResolver(v8::Handle<v8::Value> resolver, v8::Handle<v8::Value> promise, v8::Isolate*); |
108 void fulfill(v8::Handle<v8::Value>); | 131 void fulfill(v8::Handle<v8::Value>); |
109 void resolve(v8::Handle<v8::Value>); | 132 void resolve(v8::Handle<v8::Value>); |
110 void reject(v8::Handle<v8::Value>); | 133 void reject(v8::Handle<v8::Value>); |
111 | 134 |
112 v8::Isolate* m_isolate; | 135 v8::Isolate* m_isolate; |
113 ScopedPersistent<v8::Object> m_promise; | 136 ScriptPromise m_promise; |
114 ScopedPersistent<v8::Object> m_resolver; | 137 ScriptValue m_resolver; |
haraken
2013/09/02 21:44:21
Won't these cause memory leaks? Specifically, how
yusukesuzuki
2013/09/03 01:52:40
ScriptPromiseResolver is not a native class of DOM
haraken
2013/09/03 02:12:19
Sorry, actually I'm not a best reviewer here since
yusukesuzuki
2013/09/03 03:57:45
Right. This case causes memory leaks.
For example,
| |
115 bool isPendingInternal() const; | 138 bool isPendingInternal() const; |
116 }; | 139 }; |
117 | 140 |
118 template<typename T> | 141 template<typename T> |
119 void ScriptPromiseResolver::fulfill(PassRefPtr<T> value) | 142 void ScriptPromiseResolver::fulfill(PassRefPtr<T> value) |
120 { | 143 { |
121 ASSERT(v8::Context::InContext()); | 144 ASSERT(v8::Context::InContext()); |
122 fulfill(toV8(value.get(), v8::Object::New(), m_isolate)); | 145 fulfill(toV8(value.get(), v8::Object::New(), m_isolate)); |
123 } | 146 } |
124 | 147 |
125 template<typename T> | 148 template<typename T> |
126 void ScriptPromiseResolver::resolve(PassRefPtr<T> value) | 149 void ScriptPromiseResolver::resolve(PassRefPtr<T> value) |
127 { | 150 { |
128 ASSERT(v8::Context::InContext()); | 151 ASSERT(v8::Context::InContext()); |
129 resolve(toV8(value.get(), v8::Object::New(), m_isolate)); | 152 resolve(toV8(value.get(), v8::Object::New(), m_isolate)); |
130 } | 153 } |
131 | 154 |
132 template<typename T> | 155 template<typename T> |
133 void ScriptPromiseResolver::reject(PassRefPtr<T> value) | 156 void ScriptPromiseResolver::reject(PassRefPtr<T> value) |
134 { | 157 { |
135 ASSERT(v8::Context::InContext()); | 158 ASSERT(v8::Context::InContext()); |
136 reject(toV8(value.get(), v8::Object::New(), m_isolate)); | 159 reject(toV8(value.get(), v8::Object::New(), m_isolate)); |
137 } | 160 } |
138 | 161 |
139 } // namespace WebCore | 162 } // namespace WebCore |
140 | 163 |
141 | 164 |
142 #endif // ScriptPromiseResolver_h | 165 #endif // ScriptPromiseResolver_h |
OLD | NEW |