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 | |
| 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 |
| 45 virtual ~ScriptPromiseResolverWithContext(); | |
| 46 | |
| 41 // Anything that can be passed to toV8Value can be passed to this function. | 47 // Anything that can be passed to toV8Value can be passed to this function. |
| 42 template <typename T> | 48 template <typename T> |
| 43 void resolve(T value) | 49 void resolve(T value) |
| 44 { | 50 { |
| 45 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) | 51 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 } | 52 } |
| 53 | 53 |
| 54 // Anything that can be passed to toV8Value can be passed to this function. | 54 // Anything that can be passed to toV8Value can be passed to this function. |
| 55 template <typename T> | 55 template <typename T> |
| 56 void reject(T value) | 56 void reject(T value) |
| 57 { | 57 { |
| 58 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) | 58 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 } | 59 } |
| 66 | 60 |
| 67 NewScriptState* scriptState() { return m_scriptState.get(); } | 61 NewScriptState* scriptState() { return m_scriptState.get(); } |
| 68 | 62 |
| 69 // Note that an empty ScriptPromise will be returned after resolve or | 63 // Note that an empty ScriptPromise will be returned after resolve or |
| 70 // reject is called. | 64 // reject is called. |
| 71 ScriptPromise promise() | 65 ScriptPromise promise() |
| 72 { | 66 { |
| 73 return m_resolver ? m_resolver->promise() : ScriptPromise(); | 67 return m_resolver ? m_resolver->promise() : ScriptPromise(); |
| 74 } | 68 } |
| 75 | 69 |
| 76 NewScriptState* scriptState() const { return m_scriptState.get(); } | 70 NewScriptState* scriptState() const { return m_scriptState.get(); } |
| 77 | 71 |
| 78 // ActiveDOMObject implementation. | 72 // ActiveDOMObject implementation. |
| 79 virtual void suspend() OVERRIDE; | 73 virtual void suspend() OVERRIDE; |
| 80 virtual void resume() OVERRIDE; | 74 virtual void resume() OVERRIDE; |
| 81 virtual void stop() OVERRIDE; | 75 virtual void stop() OVERRIDE; |
| 82 | 76 |
| 83 private: | 77 private: |
| 78 class RetainThis; | |
|
jochen (gone - plz use gerrit)
2014/04/22 06:49:41
we'd usually just ref() and deref()
yhirano
2014/04/22 07:49:05
Done.
| |
| 84 enum ResolutionState { | 79 enum ResolutionState { |
| 85 Pending, | 80 Pending, |
| 86 Resolving, | 81 Resolving, |
| 87 Rejecting, | 82 Rejecting, |
| 88 ResolvedOrRejected, | 83 ResolvedOrRejected, |
| 89 }; | 84 }; |
| 90 | 85 |
| 91 explicit ScriptPromiseResolverWithContext(NewScriptState*); | 86 explicit ScriptPromiseResolverWithContext(NewScriptState*); |
| 92 | 87 |
| 93 template<typename T> | 88 template<typename T> |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 113 | 108 |
| 114 // const char* should use V8ValueTraits. | 109 // const char* should use V8ValueTraits. |
| 115 v8::Handle<v8::Value> toV8Value(const char* value) { return V8ValueTraits<co nst char*>::toV8Value(value, m_scriptState->isolate()); } | 110 v8::Handle<v8::Value> toV8Value(const char* value) { return V8ValueTraits<co nst char*>::toV8Value(value, m_scriptState->isolate()); } |
| 116 | 111 |
| 117 template<typename T, size_t inlineCapacity> | 112 template<typename T, size_t inlineCapacity> |
| 118 v8::Handle<v8::Value> toV8Value(const Vector<T, inlineCapacity>& value) | 113 v8::Handle<v8::Value> toV8Value(const Vector<T, inlineCapacity>& value) |
| 119 { | 114 { |
| 120 return v8ArrayNoInline(value, m_scriptState->isolate()); | 115 return v8ArrayNoInline(value, m_scriptState->isolate()); |
| 121 } | 116 } |
| 122 | 117 |
| 123 void resolveOrRejectImmediately(Timer<ScriptPromiseResolverWithContext>*); | 118 template <typename T> |
| 119 void resolveOrReject(T value, ResolutionState newState) | |
| 120 { | |
| 121 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) | |
| 122 return; | |
| 123 m_state = newState; | |
| 124 retainThis(); | |
| 125 | |
| 126 bool isInContext = m_scriptState->isolate()->InContext(); | |
| 127 NewScriptState::Scope scope(m_scriptState.get()); | |
| 128 m_value.set(m_scriptState->isolate(), toV8Value(value)); | |
| 129 if (!executionContext()->activeDOMObjectsAreSuspended()) { | |
| 130 if (isInContext) { | |
| 131 resolveOrRejectImmediately(); | |
| 132 } else { | |
| 133 m_timer.startOneShot(0, FROM_HERE); | |
| 134 } | |
| 135 } | |
| 136 } | |
| 137 | |
| 138 void resolveOrRejectImmediately(); | |
| 139 void onTimerFired(Timer<ScriptPromiseResolverWithContext>*); | |
| 140 void retainThis(); | |
| 124 void clear(); | 141 void clear(); |
| 125 | 142 |
| 126 ResolutionState m_state; | 143 ResolutionState m_state; |
| 127 const RefPtr<NewScriptState> m_scriptState; | 144 const RefPtr<NewScriptState> m_scriptState; |
| 128 Timer<ScriptPromiseResolverWithContext> m_timer; | 145 Timer<ScriptPromiseResolverWithContext> m_timer; |
| 129 RefPtr<ScriptPromiseResolver> m_resolver; | 146 RefPtr<ScriptPromiseResolver> m_resolver; |
| 130 ScopedPersistent<v8::Value> m_value; | 147 ScopedPersistent<v8::Value> m_value; |
| 148 // Retain this object during waiting for the timer. | |
| 149 RefPtr<RetainThis> m_retainThis; | |
| 131 }; | 150 }; |
| 132 | 151 |
| 133 } // namespace WebCore | 152 } // namespace WebCore |
| 134 | 153 |
| 135 #endif // #ifndef ScriptPromiseResolverWithContext_h | 154 #endif // #ifndef ScriptPromiseResolverWithContext_h |
| OLD | NEW |