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 20 matching lines...) Expand all Loading... |
95 }; | 83 }; |
96 | 84 |
97 explicit ScriptPromiseResolverWithContext(NewScriptState*); | 85 explicit ScriptPromiseResolverWithContext(NewScriptState*); |
98 | 86 |
99 template<typename T> | 87 template<typename T> |
100 v8::Handle<v8::Value> toV8Value(const T& value) | 88 v8::Handle<v8::Value> toV8Value(const T& value) |
101 { | 89 { |
102 return ToV8Value<ScriptPromiseResolverWithContext, NewScriptState*>::toV
8Value(value, m_scriptState.get(), m_scriptState->isolate()); | 90 return ToV8Value<ScriptPromiseResolverWithContext, NewScriptState*>::toV
8Value(value, m_scriptState.get(), m_scriptState->isolate()); |
103 } | 91 } |
104 | 92 |
105 void resolveOrRejectImmediately(Timer<ScriptPromiseResolverWithContext>*); | 93 template <typename T> |
| 94 void resolveOrReject(T value, ResolutionState newState) |
| 95 { |
| 96 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) |
| 97 return; |
| 98 m_state = newState; |
| 99 // Retain this object until it is actually resolved or rejected. |
| 100 // |deref| will be called in |clear|. |
| 101 ref(); |
| 102 |
| 103 NewScriptState::Scope scope(m_scriptState.get()); |
| 104 m_value.set(m_scriptState->isolate(), toV8Value(value)); |
| 105 if (!executionContext()->activeDOMObjectsAreSuspended()) { |
| 106 resolveOrRejectImmediately(); |
| 107 // |this| can't be deleted here, so it is safe to call an instance |
| 108 // method. |
| 109 postRunMicrotasks(); |
| 110 } |
| 111 } |
| 112 |
| 113 void resolveOrRejectImmediately(); |
| 114 void postRunMicrotasks(); |
| 115 void onTimerFired(Timer<ScriptPromiseResolverWithContext>*); |
106 void clear(); | 116 void clear(); |
107 | 117 |
108 ResolutionState m_state; | 118 ResolutionState m_state; |
109 const RefPtr<NewScriptState> m_scriptState; | 119 const RefPtr<NewScriptState> m_scriptState; |
110 Timer<ScriptPromiseResolverWithContext> m_timer; | 120 Timer<ScriptPromiseResolverWithContext> m_timer; |
111 RefPtr<ScriptPromiseResolver> m_resolver; | 121 RefPtr<ScriptPromiseResolver> m_resolver; |
112 ScopedPersistent<v8::Value> m_value; | 122 ScopedPersistent<v8::Value> m_value; |
113 }; | 123 }; |
114 | 124 |
115 } // namespace WebCore | 125 } // namespace WebCore |
116 | 126 |
117 #endif // #ifndef ScriptPromiseResolverWithContext_h | 127 #endif // #ifndef ScriptPromiseResolverWithContext_h |
OLD | NEW |