| 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" |
| (...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(NewScriptState* s
criptState) | 34 static PassRefPtr<ScriptPromiseResolverWithContext> create(NewScriptState* s
criptState) |
| 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 // Anything that can be passed to ToV8Value::toV8Value can be passed to |
| 42 // this function. |
| 41 template <typename T> | 43 template <typename T> |
| 42 void resolve(T value) | 44 void resolve(const T& value) |
| 43 { | 45 { |
| 44 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) | 46 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) |
| 45 return; | 47 return; |
| 46 m_state = Resolving; | 48 m_state = Resolving; |
| 47 NewScriptState::Scope scope(m_scriptState.get()); | 49 NewScriptState::Scope scope(m_scriptState.get()); |
| 48 m_value.set(m_scriptState->isolate(), toV8(value)); | 50 m_value.set(m_scriptState->isolate(), toV8Value(value)); |
| 49 if (!executionContext()->activeDOMObjectsAreSuspended()) | 51 if (!executionContext()->activeDOMObjectsAreSuspended()) |
| 50 resolveOrRejectImmediately(&m_timer); | 52 resolveOrRejectImmediately(&m_timer); |
| 51 } | 53 } |
| 52 | 54 |
| 55 // Anything that can be passed to ToV8Value::toV8Value can be passed to |
| 56 // this function. |
| 53 template <typename T> | 57 template <typename T> |
| 54 void reject(T value) | 58 void reject(const T& value) |
| 55 { | 59 { |
| 56 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) | 60 if (m_state != Pending || !executionContext() || executionContext()->act
iveDOMObjectsAreStopped()) |
| 57 return; | 61 return; |
| 58 m_state = Rejecting; | 62 m_state = Rejecting; |
| 59 NewScriptState::Scope scope(m_scriptState.get()); | 63 NewScriptState::Scope scope(m_scriptState.get()); |
| 60 m_value.set(m_scriptState->isolate(), toV8(value)); | 64 m_value.set(m_scriptState->isolate(), toV8Value(value)); |
| 61 if (!executionContext()->activeDOMObjectsAreSuspended()) | 65 if (!executionContext()->activeDOMObjectsAreSuspended()) |
| 62 resolveOrRejectImmediately(&m_timer); | 66 resolveOrRejectImmediately(&m_timer); |
| 63 } | 67 } |
| 64 | 68 |
| 65 // Note that an empty ScriptPromise will be returned after resolve or | 69 // Note that an empty ScriptPromise will be returned after resolve or |
| 66 // reject is called. | 70 // reject is called. |
| 67 ScriptPromise promise() | 71 ScriptPromise promise() |
| 68 { | 72 { |
| 69 return m_resolver ? m_resolver->promise() : ScriptPromise(); | 73 return m_resolver ? m_resolver->promise() : ScriptPromise(); |
| 70 } | 74 } |
| 71 | 75 |
| 72 NewScriptState* scriptState() const { return m_scriptState.get(); } | 76 NewScriptState* scriptState() const { return m_scriptState.get(); } |
| 73 | 77 |
| 74 // ActiveDOMObject implementation. | 78 // ActiveDOMObject implementation. |
| 75 virtual void suspend() OVERRIDE; | 79 virtual void suspend() OVERRIDE; |
| 76 virtual void resume() OVERRIDE; | 80 virtual void resume() OVERRIDE; |
| 77 virtual void stop() OVERRIDE; | 81 virtual void stop() OVERRIDE; |
| 78 | 82 |
| 79 private: | 83 private: |
| 80 enum ResolutionState { | 84 enum ResolutionState { |
| 81 Pending, | 85 Pending, |
| 82 Resolving, | 86 Resolving, |
| 83 Rejecting, | 87 Rejecting, |
| 84 ResolvedOrRejected, | 88 ResolvedOrRejected, |
| 85 }; | 89 }; |
| 86 | 90 |
| 87 explicit ScriptPromiseResolverWithContext(NewScriptState*); | 91 explicit ScriptPromiseResolverWithContext(NewScriptState*); |
| 92 void resolveOrRejectImmediately(Timer<ScriptPromiseResolverWithContext>*); |
| 93 void clear(); |
| 94 |
| 95 template <typename T, bool toV8ValueNeedsCreationContext> |
| 96 struct ToV8ValueHelper; |
| 97 template <typename T> |
| 98 struct ToV8ValueHelper<T, true> { |
| 99 static v8::Handle<v8::Value> toV8Value(const T& value, NewScriptState* s
criptState) |
| 100 { |
| 101 return V8ValueTraits<T>::toV8Value(value, scriptState->context()->Gl
obal(), scriptState->isolate()); |
| 102 } |
| 103 }; |
| 104 template <typename T> |
| 105 struct ToV8ValueHelper<T, false> { |
| 106 static v8::Handle<v8::Value> toV8Value(const T& value, NewScriptState* s
criptState) |
| 107 { |
| 108 return V8ValueTraits<T>::toV8Value(value, scriptState->isolate()); |
| 109 } |
| 110 }; |
| 88 | 111 |
| 89 template <typename T> | 112 template <typename T> |
| 90 v8::Handle<v8::Value> toV8(T* value) | 113 v8::Handle<v8::Value> toV8Value(const T& value) |
| 91 { | 114 { |
| 92 ASSERT(m_scriptState); | 115 return ToV8ValueHelper<T, V8ValueTraits<T>::toV8ValueNeedsCreationContex
t>::toV8Value(value, m_scriptState.get()); |
| 93 ASSERT(!m_scriptState->contextIsEmpty()); | |
| 94 return toV8NoInline(value, m_scriptState->context()->Global(), m_scriptS
tate->isolate()); | |
| 95 } | 116 } |
| 96 template <typename T> v8::Handle<v8::Value> toV8(PassRefPtr<T> value) { retu
rn toV8(value.get()); } | |
| 97 template <typename T> v8::Handle<v8::Value> toV8(RawPtr<T> value) { return t
oV8(value.get()); } | |
| 98 template <typename T, size_t inlineCapacity> | |
| 99 v8::Handle<v8::Value> toV8(const Vector<T, inlineCapacity>& value) | |
| 100 { | |
| 101 ASSERT(m_scriptState); | |
| 102 return v8ArrayNoInline(value, m_scriptState->isolate()); | |
| 103 } | |
| 104 v8::Handle<v8::Value> toV8(ScriptValue value) | |
| 105 { | |
| 106 return value.v8Value(); | |
| 107 } | |
| 108 | |
| 109 void resolveOrRejectImmediately(Timer<ScriptPromiseResolverWithContext>*); | |
| 110 void clear(); | |
| 111 | 117 |
| 112 ResolutionState m_state; | 118 ResolutionState m_state; |
| 113 RefPtr<NewScriptState> m_scriptState; | 119 RefPtr<NewScriptState> m_scriptState; |
| 114 Timer<ScriptPromiseResolverWithContext> m_timer; | 120 Timer<ScriptPromiseResolverWithContext> m_timer; |
| 115 RefPtr<ScriptPromiseResolver> m_resolver; | 121 RefPtr<ScriptPromiseResolver> m_resolver; |
| 116 ScopedPersistent<v8::Value> m_value; | 122 ScopedPersistent<v8::Value> m_value; |
| 117 }; | 123 }; |
| 118 | 124 |
| 119 } // namespace WebCore | 125 } // namespace WebCore |
| 120 | 126 |
| 121 #endif // #ifndef ScriptPromiseResolverWithContext_h | 127 #endif // #ifndef ScriptPromiseResolverWithContext_h |
| OLD | NEW |