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 13 matching lines...) Expand all Loading... | |
24 // - A ScriptPromiseResolverWithContext retains a ScriptState. A caller | 24 // - A ScriptPromiseResolverWithContext retains a ScriptState. A caller |
25 // can call resolve or reject from outside of a V8 context. | 25 // can call resolve or reject from outside of a V8 context. |
26 // - This class is an ActiveDOMObject and keeps track of the associated | 26 // - This class is an ActiveDOMObject and keeps track of the associated |
27 // ExecutionContext state. When the ExecutionContext is suspended, | 27 // ExecutionContext state. When the ExecutionContext is suspended, |
28 // resolve or reject will be delayed. When it is stopped, resolve or reject | 28 // resolve or reject will be delayed. When it is stopped, resolve or reject |
29 // will be ignored. | 29 // will be ignored. |
30 class ScriptPromiseResolverWithContext : public ActiveDOMObject, public RefCount ed<ScriptPromiseResolverWithContext> { | 30 class ScriptPromiseResolverWithContext : public ActiveDOMObject, public RefCount ed<ScriptPromiseResolverWithContext> { |
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 enum Mode { |
35 Default, | |
36 KeepAliveWhilePending, | |
37 }; | |
38 | |
39 // Create a ScriptPromiseResolverWithContext. | |
40 // If |mode| is KeepAliveWhilePending, the resolver refers itself in order | |
41 // to stay alive while the promise is pending and the accosiated | |
eroman
2014/06/16 19:36:35
typo: accosiated --> associated
yhirano
2014/06/17 01:45:44
Done.
yhirano
2014/06/17 01:45:44
Done.
| |
42 // ExecutionContext isn't stopped. | |
43 static PassRefPtr<ScriptPromiseResolverWithContext> create(ScriptState* scri ptState, Mode mode = Default) | |
35 { | 44 { |
36 RefPtr<ScriptPromiseResolverWithContext> resolver = adoptRef(new ScriptP romiseResolverWithContext(scriptState)); | 45 RefPtr<ScriptPromiseResolverWithContext> resolver = adoptRef(new ScriptP romiseResolverWithContext(scriptState, mode)); |
37 resolver->suspendIfNeeded(); | 46 resolver->suspendIfNeeded(); |
38 return resolver.release(); | 47 return resolver.release(); |
39 } | 48 } |
40 | 49 |
50 virtual ~ScriptPromiseResolverWithContext(); | |
51 | |
41 // Anything that can be passed to toV8Value can be passed to this function. | 52 // Anything that can be passed to toV8Value can be passed to this function. |
42 template <typename T> | 53 template <typename T> |
43 void resolve(T value) | 54 void resolve(T value) |
44 { | 55 { |
45 resolveOrReject(value, Resolving); | 56 resolveOrReject(value, Resolving); |
46 } | 57 } |
47 | 58 |
48 // Anything that can be passed to toV8Value can be passed to this function. | 59 // Anything that can be passed to toV8Value can be passed to this function. |
49 template <typename T> | 60 template <typename T> |
50 void reject(T value) | 61 void reject(T value) |
(...skipping 11 matching lines...) Expand all Loading... | |
62 } | 73 } |
63 | 74 |
64 ScriptState* scriptState() const { return m_scriptState.get(); } | 75 ScriptState* scriptState() const { return m_scriptState.get(); } |
65 | 76 |
66 // ActiveDOMObject implementation. | 77 // ActiveDOMObject implementation. |
67 virtual void suspend() OVERRIDE; | 78 virtual void suspend() OVERRIDE; |
68 virtual void resume() OVERRIDE; | 79 virtual void resume() OVERRIDE; |
69 virtual void stop() OVERRIDE; | 80 virtual void stop() OVERRIDE; |
70 | 81 |
71 protected: | 82 protected: |
72 explicit ScriptPromiseResolverWithContext(ScriptState*); | 83 ScriptPromiseResolverWithContext(ScriptState*, Mode); |
73 | 84 |
74 private: | 85 private: |
75 enum ResolutionState { | 86 enum ResolutionState { |
76 Pending, | 87 Pending, |
77 Resolving, | 88 Resolving, |
78 Rejecting, | 89 Rejecting, |
79 ResolvedOrRejected, | 90 ResolvedOrRejected, |
80 }; | 91 }; |
81 | 92 |
82 template<typename T> | 93 template<typename T> |
83 v8::Handle<v8::Value> toV8Value(const T& value) | 94 v8::Handle<v8::Value> toV8Value(const T& value) |
84 { | 95 { |
85 return ToV8Value<ScriptPromiseResolverWithContext, v8::Handle<v8::Object > >::toV8Value(value, m_scriptState->context()->Global(), m_scriptState->isolate ()); | 96 return ToV8Value<ScriptPromiseResolverWithContext, v8::Handle<v8::Object > >::toV8Value(value, m_scriptState->context()->Global(), m_scriptState->isolate ()); |
86 } | 97 } |
87 | 98 |
88 template <typename T> | 99 template <typename T> |
89 void resolveOrReject(T value, ResolutionState newState) | 100 void resolveOrReject(T value, ResolutionState newState) |
90 { | 101 { |
91 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) | 102 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) |
92 return; | 103 return; |
104 ASSERT(newState == Resolving || newState == Rejecting); | |
93 m_state = newState; | 105 m_state = newState; |
94 // Retain this object until it is actually resolved or rejected. | 106 // Retain this object until it is actually resolved or rejected. |
95 // |deref| will be called in |clear|. | 107 // |deref| will be called in |clear|. |
96 ref(); | 108 ref(); |
97 | 109 |
98 ScriptState::Scope scope(m_scriptState.get()); | 110 ScriptState::Scope scope(m_scriptState.get()); |
99 m_value.set(m_scriptState->isolate(), toV8Value(value)); | 111 m_value.set(m_scriptState->isolate(), toV8Value(value)); |
100 if (!executionContext()->activeDOMObjectsAreSuspended()) | 112 if (!executionContext()->activeDOMObjectsAreSuspended()) |
101 resolveOrRejectImmediately(); | 113 resolveOrRejectImmediately(); |
102 } | 114 } |
103 | 115 |
104 void resolveOrRejectImmediately(); | 116 void resolveOrRejectImmediately(); |
105 void onTimerFired(Timer<ScriptPromiseResolverWithContext>*); | 117 void onTimerFired(Timer<ScriptPromiseResolverWithContext>*); |
106 void clear(); | 118 void clear(); |
107 | 119 |
108 ResolutionState m_state; | 120 ResolutionState m_state; |
109 const RefPtr<ScriptState> m_scriptState; | 121 const RefPtr<ScriptState> m_scriptState; |
122 Mode m_mode; | |
110 Timer<ScriptPromiseResolverWithContext> m_timer; | 123 Timer<ScriptPromiseResolverWithContext> m_timer; |
111 RefPtr<ScriptPromiseResolver> m_resolver; | 124 RefPtr<ScriptPromiseResolver> m_resolver; |
112 ScopedPersistent<v8::Value> m_value; | 125 ScopedPersistent<v8::Value> m_value; |
113 }; | 126 }; |
114 | 127 |
115 } // namespace WebCore | 128 } // namespace WebCore |
116 | 129 |
117 #endif // #ifndef ScriptPromiseResolverWithContext_h | 130 #endif // #ifndef ScriptPromiseResolverWithContext_h |
OLD | NEW |