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