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/ScopedPersistent.h" | 8 #include "bindings/v8/ScopedPersistent.h" |
9 #include "bindings/v8/ScriptPromise.h" | 9 #include "bindings/v8/ScriptPromise.h" |
10 #include "bindings/v8/ScriptPromiseResolver.h" | 10 #include "bindings/v8/ScriptPromiseResolver.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(ScriptState* scri
ptState) | 34 static PassRefPtr<ScriptPromiseResolverWithContext> create(ScriptState* scri
ptState) |
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 virtual ~ScriptPromiseResolverWithContext(); |
| 42 |
41 // Anything that can be passed to toV8Value can be passed to this function. | 43 // Anything that can be passed to toV8Value can be passed to this function. |
42 template <typename T> | 44 template <typename T> |
43 void resolve(T value) | 45 void resolve(T value) |
44 { | 46 { |
45 resolveOrReject(value, Resolving); | 47 resolveOrReject(value, Resolving); |
46 } | 48 } |
47 | 49 |
48 // Anything that can be passed to toV8Value can be passed to this function. | 50 // Anything that can be passed to toV8Value can be passed to this function. |
49 template <typename T> | 51 template <typename T> |
50 void reject(T value) | 52 void reject(T value) |
(...skipping 10 matching lines...) Expand all Loading... |
61 return m_resolver ? m_resolver->promise() : ScriptPromise(); | 63 return m_resolver ? m_resolver->promise() : ScriptPromise(); |
62 } | 64 } |
63 | 65 |
64 ScriptState* scriptState() const { return m_scriptState.get(); } | 66 ScriptState* scriptState() const { return m_scriptState.get(); } |
65 | 67 |
66 // ActiveDOMObject implementation. | 68 // ActiveDOMObject implementation. |
67 virtual void suspend() OVERRIDE; | 69 virtual void suspend() OVERRIDE; |
68 virtual void resume() OVERRIDE; | 70 virtual void resume() OVERRIDE; |
69 virtual void stop() OVERRIDE; | 71 virtual void stop() OVERRIDE; |
70 | 72 |
| 73 // Once this function is called this resolver stays alive while the |
| 74 // promise is pending and the associated ExecutionContext isn't stopped. |
| 75 void keepAliveWhilePending(); |
| 76 |
71 protected: | 77 protected: |
| 78 // You need to call suspendIfNeeded after the construction because |
| 79 // this is an ActiveDOMObject. |
72 explicit ScriptPromiseResolverWithContext(ScriptState*); | 80 explicit ScriptPromiseResolverWithContext(ScriptState*); |
73 | 81 |
74 private: | 82 private: |
75 enum ResolutionState { | 83 enum ResolutionState { |
76 Pending, | 84 Pending, |
77 Resolving, | 85 Resolving, |
78 Rejecting, | 86 Rejecting, |
79 ResolvedOrRejected, | 87 ResolvedOrRejected, |
80 }; | 88 }; |
| 89 enum LifetimeMode { |
| 90 Default, |
| 91 KeepAliveWhilePending, |
| 92 }; |
81 | 93 |
82 template<typename T> | 94 template<typename T> |
83 v8::Handle<v8::Value> toV8Value(const T& value) | 95 v8::Handle<v8::Value> toV8Value(const T& value) |
84 { | 96 { |
85 return ToV8Value<ScriptPromiseResolverWithContext, v8::Handle<v8::Object
> >::toV8Value(value, m_scriptState->context()->Global(), m_scriptState->isolate
()); | 97 return ToV8Value<ScriptPromiseResolverWithContext, v8::Handle<v8::Object
> >::toV8Value(value, m_scriptState->context()->Global(), m_scriptState->isolate
()); |
86 } | 98 } |
87 | 99 |
88 template <typename T> | 100 template <typename T> |
89 void resolveOrReject(T value, ResolutionState newState) | 101 void resolveOrReject(T value, ResolutionState newState) |
90 { | 102 { |
91 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) | 103 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) |
92 return; | 104 return; |
| 105 ASSERT(newState == Resolving || newState == Rejecting); |
93 m_state = newState; | 106 m_state = newState; |
94 // Retain this object until it is actually resolved or rejected. | 107 // Retain this object until it is actually resolved or rejected. |
95 // |deref| will be called in |clear|. | 108 // |deref| will be called in |clear|. |
96 ref(); | 109 ref(); |
97 | 110 |
98 ScriptState::Scope scope(m_scriptState.get()); | 111 ScriptState::Scope scope(m_scriptState.get()); |
99 m_value.set(m_scriptState->isolate(), toV8Value(value)); | 112 m_value.set(m_scriptState->isolate(), toV8Value(value)); |
100 if (!executionContext()->activeDOMObjectsAreSuspended()) | 113 if (!executionContext()->activeDOMObjectsAreSuspended()) |
101 resolveOrRejectImmediately(); | 114 resolveOrRejectImmediately(); |
102 } | 115 } |
103 | 116 |
104 void resolveOrRejectImmediately(); | 117 void resolveOrRejectImmediately(); |
105 void onTimerFired(Timer<ScriptPromiseResolverWithContext>*); | 118 void onTimerFired(Timer<ScriptPromiseResolverWithContext>*); |
106 void clear(); | 119 void clear(); |
107 | 120 |
108 ResolutionState m_state; | 121 ResolutionState m_state; |
109 const RefPtr<ScriptState> m_scriptState; | 122 const RefPtr<ScriptState> m_scriptState; |
| 123 LifetimeMode m_mode; |
110 Timer<ScriptPromiseResolverWithContext> m_timer; | 124 Timer<ScriptPromiseResolverWithContext> m_timer; |
111 RefPtr<ScriptPromiseResolver> m_resolver; | 125 RefPtr<ScriptPromiseResolver> m_resolver; |
112 ScopedPersistent<v8::Value> m_value; | 126 ScopedPersistent<v8::Value> m_value; |
113 }; | 127 }; |
114 | 128 |
115 } // namespace WebCore | 129 } // namespace WebCore |
116 | 130 |
117 #endif // #ifndef ScriptPromiseResolverWithContext_h | 131 #endif // #ifndef ScriptPromiseResolverWithContext_h |
OLD | NEW |