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 24 matching lines...) Expand all Loading... |
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 // Anything that can be passed to toV8Value can be passed to this function. |
42 template <typename T> | 42 template <typename T> |
43 void resolve(T value) | 43 void resolve(T value) |
44 { | 44 { |
45 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) | 45 resolveOrReject(value, Resolving); |
46 return; | |
47 m_state = Resolving; | |
48 NewScriptState::Scope scope(m_scriptState.get()); | |
49 m_value.set(m_scriptState->isolate(), toV8Value(value)); | |
50 if (!executionContext()->activeDOMObjectsAreSuspended()) | |
51 resolveOrRejectImmediately(&m_timer); | |
52 } | 46 } |
53 | 47 |
54 // Anything that can be passed to toV8Value can be passed to this function. | 48 // Anything that can be passed to toV8Value can be passed to this function. |
55 template <typename T> | 49 template <typename T> |
56 void reject(T value) | 50 void reject(T value) |
57 { | 51 { |
58 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) | 52 resolveOrReject(value, Rejecting); |
59 return; | |
60 m_state = Rejecting; | |
61 NewScriptState::Scope scope(m_scriptState.get()); | |
62 m_value.set(m_scriptState->isolate(), toV8Value(value)); | |
63 if (!executionContext()->activeDOMObjectsAreSuspended()) | |
64 resolveOrRejectImmediately(&m_timer); | |
65 } | 53 } |
66 | 54 |
67 NewScriptState* scriptState() { return m_scriptState.get(); } | 55 NewScriptState* scriptState() { return m_scriptState.get(); } |
68 | 56 |
69 // Note that an empty ScriptPromise will be returned after resolve or | 57 // Note that an empty ScriptPromise will be returned after resolve or |
70 // reject is called. | 58 // reject is called. |
71 ScriptPromise promise() | 59 ScriptPromise promise() |
72 { | 60 { |
73 return m_resolver ? m_resolver->promise() : ScriptPromise(); | 61 return m_resolver ? m_resolver->promise() : ScriptPromise(); |
74 } | 62 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 | 101 |
114 // const char* should use V8ValueTraits. | 102 // const char* should use V8ValueTraits. |
115 v8::Handle<v8::Value> toV8Value(const char* value) { return V8ValueTraits<co
nst char*>::toV8Value(value, m_scriptState->isolate()); } | 103 v8::Handle<v8::Value> toV8Value(const char* value) { return V8ValueTraits<co
nst char*>::toV8Value(value, m_scriptState->isolate()); } |
116 | 104 |
117 template<typename T, size_t inlineCapacity> | 105 template<typename T, size_t inlineCapacity> |
118 v8::Handle<v8::Value> toV8Value(const Vector<T, inlineCapacity>& value) | 106 v8::Handle<v8::Value> toV8Value(const Vector<T, inlineCapacity>& value) |
119 { | 107 { |
120 return v8ArrayNoInline(value, m_scriptState->isolate()); | 108 return v8ArrayNoInline(value, m_scriptState->isolate()); |
121 } | 109 } |
122 | 110 |
123 void resolveOrRejectImmediately(Timer<ScriptPromiseResolverWithContext>*); | 111 template <typename T> |
| 112 void resolveOrReject(T value, ResolutionState newState) |
| 113 { |
| 114 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) |
| 115 return; |
| 116 m_state = newState; |
| 117 // Retain this object until it is actually resolved or rejected. |
| 118 // deref will be called in resolveOrRejectImmediately. |
| 119 ref(); |
| 120 |
| 121 bool isInContext = m_scriptState->isolate()->InContext(); |
| 122 NewScriptState::Scope scope(m_scriptState.get()); |
| 123 m_value.set(m_scriptState->isolate(), toV8Value(value)); |
| 124 if (!executionContext()->activeDOMObjectsAreSuspended()) { |
| 125 if (isInContext) { |
| 126 resolveOrRejectImmediately(); |
| 127 } else { |
| 128 m_timer.startOneShot(0, FROM_HERE); |
| 129 } |
| 130 } |
| 131 } |
| 132 |
| 133 void resolveOrRejectImmediately(); |
| 134 void onTimerFired(Timer<ScriptPromiseResolverWithContext>*); |
124 void clear(); | 135 void clear(); |
125 | 136 |
126 ResolutionState m_state; | 137 ResolutionState m_state; |
127 const RefPtr<NewScriptState> m_scriptState; | 138 const RefPtr<NewScriptState> m_scriptState; |
128 Timer<ScriptPromiseResolverWithContext> m_timer; | 139 Timer<ScriptPromiseResolverWithContext> m_timer; |
129 RefPtr<ScriptPromiseResolver> m_resolver; | 140 RefPtr<ScriptPromiseResolver> m_resolver; |
130 ScopedPersistent<v8::Value> m_value; | 141 ScopedPersistent<v8::Value> m_value; |
131 }; | 142 }; |
132 | 143 |
133 } // namespace WebCore | 144 } // namespace WebCore |
134 | 145 |
135 #endif // #ifndef ScriptPromiseResolverWithContext_h | 146 #endif // #ifndef ScriptPromiseResolverWithContext_h |
OLD | NEW |