Chromium Code Reviews| 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" |
| 11 #include "bindings/v8/ScriptPromiseResolver.h" | 11 #include "bindings/v8/ScriptPromiseResolver.h" |
| 12 #include "bindings/v8/V8Binding.h" | 12 #include "bindings/v8/V8Binding.h" |
| 13 #include "core/dom/ActiveDOMObject.h" | 13 #include "core/dom/ActiveDOMObject.h" |
| 14 #include "core/dom/ExecutionContext.h" | 14 #include "core/dom/ExecutionContext.h" |
| 15 #include "platform/Timer.h" | 15 #include "platform/Timer.h" |
| 16 #include "wtf/RefCounted.h" | 16 #include "wtf/RefCounted.h" |
| 17 #include "wtf/Vector.h" | 17 #include "wtf/Vector.h" |
| 18 #include <v8.h> | 18 #include <v8.h> |
| 19 | 19 |
| 20 namespace WebCore { | 20 namespace WebCore { |
| 21 | 21 |
| 22 // This class wraps ScriptPromiseResolver and provides the following | 22 // This class wraps ScriptPromiseResolver and provides the following |
| 23 // functionalities in addition to ScriptPromiseResolver's. | 23 // functionalities in addition to ScriptPromiseResolver's. |
| 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 // | |
| 31 // If you use ScriptPromiseResolverWithContext::resolve or reject from a | |
|
adamk
2014/04/24 17:34:17
Is this comment now dead?
yhirano
2014/04/24 21:20:02
Yes, Thanks!
| |
| 32 // non script execution environment, you should NOT enter a v8 context | |
| 33 // at the call site. | |
| 30 class ScriptPromiseResolverWithContext FINAL : public ActiveDOMObject, public Re fCounted<ScriptPromiseResolverWithContext> { | 34 class ScriptPromiseResolverWithContext FINAL : public ActiveDOMObject, public Re fCounted<ScriptPromiseResolverWithContext> { |
| 31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolverWithContext); | 35 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolverWithContext); |
| 32 | 36 |
| 33 public: | 37 public: |
| 34 static PassRefPtr<ScriptPromiseResolverWithContext> create(NewScriptState* s criptState) | 38 static PassRefPtr<ScriptPromiseResolverWithContext> create(NewScriptState* s criptState) |
| 35 { | 39 { |
| 36 RefPtr<ScriptPromiseResolverWithContext> resolver = adoptRef(new ScriptP romiseResolverWithContext(scriptState)); | 40 RefPtr<ScriptPromiseResolverWithContext> resolver = adoptRef(new ScriptP romiseResolverWithContext(scriptState)); |
| 37 resolver->suspendIfNeeded(); | 41 resolver->suspendIfNeeded(); |
| 38 return resolver.release(); | 42 return resolver.release(); |
| 39 } | 43 } |
| 40 | 44 |
| 41 // Anything that can be passed to toV8Value can be passed to this function. | 45 // Anything that can be passed to toV8Value can be passed to this function. |
| 42 template <typename T> | 46 template <typename T> |
| 43 void resolve(T value) | 47 void resolve(T value) |
| 44 { | 48 { |
| 45 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) | 49 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 } | 50 } |
| 53 | 51 |
| 54 // 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. |
| 55 template <typename T> | 53 template <typename T> |
| 56 void reject(T value) | 54 void reject(T value) |
| 57 { | 55 { |
| 58 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) | 56 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 } | 57 } |
| 66 | 58 |
| 67 NewScriptState* scriptState() { return m_scriptState.get(); } | 59 NewScriptState* scriptState() { return m_scriptState.get(); } |
| 68 | 60 |
| 69 // Note that an empty ScriptPromise will be returned after resolve or | 61 // Note that an empty ScriptPromise will be returned after resolve or |
| 70 // reject is called. | 62 // reject is called. |
| 71 ScriptPromise promise() | 63 ScriptPromise promise() |
| 72 { | 64 { |
| 73 return m_resolver ? m_resolver->promise() : ScriptPromise(); | 65 return m_resolver ? m_resolver->promise() : ScriptPromise(); |
| 74 } | 66 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 95 }; | 87 }; |
| 96 | 88 |
| 97 explicit ScriptPromiseResolverWithContext(NewScriptState*); | 89 explicit ScriptPromiseResolverWithContext(NewScriptState*); |
| 98 | 90 |
| 99 template<typename T> | 91 template<typename T> |
| 100 v8::Handle<v8::Value> toV8Value(const T& value) | 92 v8::Handle<v8::Value> toV8Value(const T& value) |
| 101 { | 93 { |
| 102 return ToV8Value<ScriptPromiseResolverWithContext, NewScriptState*>::toV 8Value(value, m_scriptState.get(), m_scriptState->isolate()); | 94 return ToV8Value<ScriptPromiseResolverWithContext, NewScriptState*>::toV 8Value(value, m_scriptState.get(), m_scriptState->isolate()); |
| 103 } | 95 } |
| 104 | 96 |
| 105 void resolveOrRejectImmediately(Timer<ScriptPromiseResolverWithContext>*); | 97 template <typename T> |
| 98 void resolveOrReject(T value, ResolutionState newState) | |
| 99 { | |
| 100 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) | |
| 101 return; | |
| 102 m_state = newState; | |
| 103 // Retain this object until it is actually resolved or rejected. | |
| 104 // |deref| will be called in |clear|. | |
| 105 ref(); | |
| 106 | |
| 107 NewScriptState::Scope scope(m_scriptState.get()); | |
| 108 m_value.set(m_scriptState->isolate(), toV8Value(value)); | |
| 109 if (!executionContext()->activeDOMObjectsAreSuspended()) { | |
| 110 resolveOrRejectImmediately(); | |
| 111 // |this| can't be deleted here, so it is safe to call an instance | |
| 112 // method. | |
| 113 postRunMicrotasks(); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 void resolveOrRejectImmediately(); | |
| 118 void postRunMicrotasks(); | |
| 119 void onTimerFired(Timer<ScriptPromiseResolverWithContext>*); | |
| 106 void clear(); | 120 void clear(); |
| 107 | 121 |
| 108 ResolutionState m_state; | 122 ResolutionState m_state; |
| 109 const RefPtr<NewScriptState> m_scriptState; | 123 const RefPtr<NewScriptState> m_scriptState; |
| 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 |