OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef ScriptPromiseResolverWithContext_h | 5 #ifndef ScriptPromiseResolverWithContext_h |
6 #define ScriptPromiseResolverWithContext_h | 6 #define ScriptPromiseResolverWithContext_h |
7 | 7 |
8 #include "bindings/v8/NewScriptState.h" | 8 #include "bindings/v8/NewScriptState.h" |
9 #include "bindings/v8/ScopedPersistent.h" | 9 #include "bindings/v8/ScopedPersistent.h" |
10 #include "bindings/v8/ScriptPromise.h" | 10 #include "bindings/v8/ScriptPromise.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolverWithContext); | 31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolverWithContext); |
32 | 32 |
33 public: | 33 public: |
34 static PassRefPtr<ScriptPromiseResolverWithContext> create(NewScriptState* s
criptState) | 34 static PassRefPtr<ScriptPromiseResolverWithContext> create(NewScriptState* s
criptState) |
35 { | 35 { |
36 RefPtr<ScriptPromiseResolverWithContext> resolver = adoptRef(new ScriptP
romiseResolverWithContext(scriptState)); | 36 RefPtr<ScriptPromiseResolverWithContext> resolver = adoptRef(new ScriptP
romiseResolverWithContext(scriptState)); |
37 resolver->suspendIfNeeded(); | 37 resolver->suspendIfNeeded(); |
38 return resolver.release(); | 38 return resolver.release(); |
39 } | 39 } |
40 | 40 |
| 41 // Anything that can be passed to toV8Value can be passed to this function. |
41 template <typename T> | 42 template <typename T> |
42 void resolve(T value) | 43 void resolve(T value) |
43 { | 44 { |
44 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) | 45 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) |
45 return; | 46 return; |
46 m_state = Resolving; | 47 m_state = Resolving; |
47 NewScriptState::Scope scope(m_scriptState.get()); | 48 NewScriptState::Scope scope(m_scriptState.get()); |
48 m_value.set(m_scriptState->isolate(), toV8(value)); | 49 m_value.set(m_scriptState->isolate(), toV8Value(value)); |
49 if (!executionContext()->activeDOMObjectsAreSuspended()) | 50 if (!executionContext()->activeDOMObjectsAreSuspended()) |
50 resolveOrRejectImmediately(&m_timer); | 51 resolveOrRejectImmediately(&m_timer); |
51 } | 52 } |
52 | 53 |
| 54 // Anything that can be passed to toV8Value can be passed to this function. |
53 template <typename T> | 55 template <typename T> |
54 void reject(T value) | 56 void reject(T value) |
55 { | 57 { |
56 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) | 58 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) |
57 return; | 59 return; |
58 m_state = Rejecting; | 60 m_state = Rejecting; |
59 NewScriptState::Scope scope(m_scriptState.get()); | 61 NewScriptState::Scope scope(m_scriptState.get()); |
60 m_value.set(m_scriptState->isolate(), toV8(value)); | 62 m_value.set(m_scriptState->isolate(), toV8Value(value)); |
61 if (!executionContext()->activeDOMObjectsAreSuspended()) | 63 if (!executionContext()->activeDOMObjectsAreSuspended()) |
62 resolveOrRejectImmediately(&m_timer); | 64 resolveOrRejectImmediately(&m_timer); |
63 } | 65 } |
64 | 66 |
65 // Note that an empty ScriptPromise will be returned after resolve or | 67 // Note that an empty ScriptPromise will be returned after resolve or |
66 // reject is called. | 68 // reject is called. |
67 ScriptPromise promise() | 69 ScriptPromise promise() |
68 { | 70 { |
69 return m_resolver ? m_resolver->promise() : ScriptPromise(); | 71 return m_resolver ? m_resolver->promise() : ScriptPromise(); |
70 } | 72 } |
71 | 73 |
72 NewScriptState* scriptState() const { return m_scriptState.get(); } | 74 NewScriptState* scriptState() const { return m_scriptState.get(); } |
73 | 75 |
74 // ActiveDOMObject implementation. | 76 // ActiveDOMObject implementation. |
75 virtual void suspend() OVERRIDE; | 77 virtual void suspend() OVERRIDE; |
76 virtual void resume() OVERRIDE; | 78 virtual void resume() OVERRIDE; |
77 virtual void stop() OVERRIDE; | 79 virtual void stop() OVERRIDE; |
78 | 80 |
79 private: | 81 private: |
80 enum ResolutionState { | 82 enum ResolutionState { |
81 Pending, | 83 Pending, |
82 Resolving, | 84 Resolving, |
83 Rejecting, | 85 Rejecting, |
84 ResolvedOrRejected, | 86 ResolvedOrRejected, |
85 }; | 87 }; |
86 | 88 |
87 explicit ScriptPromiseResolverWithContext(NewScriptState*); | 89 explicit ScriptPromiseResolverWithContext(NewScriptState*); |
88 | 90 |
| 91 template<typename T> |
| 92 v8::Handle<v8::Value> toV8Value(const T& value) |
| 93 { |
| 94 // Default implementaion: for types that don't need the |
| 95 // creation context. |
| 96 return V8ValueTraits<T>::toV8Value(value, m_scriptState->isolate()); |
| 97 } |
| 98 |
| 99 // Pointer specializations. |
89 template <typename T> | 100 template <typename T> |
90 v8::Handle<v8::Value> toV8(T* value) | 101 v8::Handle<v8::Value> toV8Value(T* value) |
91 { | 102 { |
92 ASSERT(m_scriptState); | |
93 ASSERT(!m_scriptState->contextIsEmpty()); | 103 ASSERT(!m_scriptState->contextIsEmpty()); |
94 return toV8NoInline(value, m_scriptState->context()->Global(), m_scriptS
tate->isolate()); | 104 return toV8NoInline(value, m_scriptState->context()->Global(), m_scriptS
tate->isolate()); |
95 } | 105 } |
96 template <typename T> v8::Handle<v8::Value> toV8(PassRefPtr<T> value) { retu
rn toV8(value.get()); } | 106 template<typename T> v8::Handle<v8::Value> toV8Value(RefPtr<T> value) { retu
rn toV8Value(value.get()); } |
97 template <typename T> v8::Handle<v8::Value> toV8(RawPtr<T> value) { return t
oV8(value.get()); } | 107 template<typename T> v8::Handle<v8::Value> toV8Value(PassRefPtr<T> value) {
return toV8Value(value.get()); } |
98 template <typename T, size_t inlineCapacity> | 108 template<typename T> v8::Handle<v8::Value> toV8Value(OwnPtr<T> value) { retu
rn toV8Value(value.get()); } |
99 v8::Handle<v8::Value> toV8(const Vector<T, inlineCapacity>& value) | 109 template<typename T> v8::Handle<v8::Value> toV8Value(PassOwnPtr<T> value) {
return toV8Value(value.get()); } |
| 110 template<typename T> v8::Handle<v8::Value> toV8Value(RawPtr<T> value) { retu
rn toV8Value(value.get()); } |
| 111 |
| 112 // const char* should use V8ValueTraits. |
| 113 v8::Handle<v8::Value> toV8Value(const char* value) { return V8ValueTraits<co
nst char*>::toV8Value(value, m_scriptState->isolate()); } |
| 114 |
| 115 template<typename T, size_t inlineCapacity> |
| 116 v8::Handle<v8::Value> toV8Value(const Vector<T, inlineCapacity>& value) |
100 { | 117 { |
101 ASSERT(m_scriptState); | |
102 return v8ArrayNoInline(value, m_scriptState->isolate()); | 118 return v8ArrayNoInline(value, m_scriptState->isolate()); |
103 } | 119 } |
104 v8::Handle<v8::Value> toV8(ScriptValue value) | |
105 { | |
106 return value.v8Value(); | |
107 } | |
108 | 120 |
109 void resolveOrRejectImmediately(Timer<ScriptPromiseResolverWithContext>*); | 121 void resolveOrRejectImmediately(Timer<ScriptPromiseResolverWithContext>*); |
110 void clear(); | 122 void clear(); |
111 | 123 |
112 ResolutionState m_state; | 124 ResolutionState m_state; |
113 RefPtr<NewScriptState> m_scriptState; | 125 RefPtr<NewScriptState> m_scriptState; |
114 Timer<ScriptPromiseResolverWithContext> m_timer; | 126 Timer<ScriptPromiseResolverWithContext> m_timer; |
115 RefPtr<ScriptPromiseResolver> m_resolver; | 127 RefPtr<ScriptPromiseResolver> m_resolver; |
116 ScopedPersistent<v8::Value> m_value; | 128 ScopedPersistent<v8::Value> m_value; |
117 }; | 129 }; |
118 | 130 |
119 } // namespace WebCore | 131 } // namespace WebCore |
120 | 132 |
121 #endif // #ifndef ScriptPromiseResolverWithContext_h | 133 #endif // #ifndef ScriptPromiseResolverWithContext_h |
OLD | NEW |