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" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 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 class ScriptPromiseResolverWithContext FINAL : public ActiveDOMObject, public Re fCounted<ScriptPromiseResolverWithContext> { | 30 class ScriptPromiseResolverWithContext FINAL : public ActiveDOMObject, public Re fCounted<ScriptPromiseResolverWithContext> { |
| 31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolverWithContext); | 31 WTF_MAKE_NONCOPYABLE(ScriptPromiseResolverWithContext); |
| 32 | 32 |
| 33 class NullType { | |
| 34 }; | |
| 35 class UndefinedType { | |
| 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 // Anything that can be passed to one of the toV8 functions defined in this | |
| 46 // class can be passed to this function. | |
| 41 template <typename T> | 47 template <typename T> |
| 42 void resolve(T value) | 48 void resolve(T value) |
| 43 { | 49 { |
| 44 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) | 50 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) |
| 45 return; | 51 return; |
| 46 m_state = Resolving; | 52 m_state = Resolving; |
| 47 NewScriptState::Scope scope(m_scriptState.get()); | 53 NewScriptState::Scope scope(m_scriptState.get()); |
| 48 m_value.set(m_scriptState->isolate(), toV8(value)); | 54 m_value.set(m_scriptState->isolate(), toV8(value)); |
|
haraken
2014/04/17 03:46:23
I'm not sure if it's a good idea to introduce toV8
yhirano
2014/04/17 03:52:24
I think HandleScope is needed to use v8::Undefined
| |
| 49 if (!executionContext()->activeDOMObjectsAreSuspended()) | 55 if (!executionContext()->activeDOMObjectsAreSuspended()) |
| 50 resolveOrRejectImmediately(&m_timer); | 56 resolveOrRejectImmediately(&m_timer); |
| 51 } | 57 } |
| 52 | 58 |
| 59 // Anything that can be passed to one of the toV8 functions defined in this | |
| 60 // class can be passed to this function. | |
| 53 template <typename T> | 61 template <typename T> |
| 54 void reject(T value) | 62 void reject(T value) |
| 55 { | 63 { |
| 56 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) | 64 if (m_state != Pending || !executionContext() || executionContext()->act iveDOMObjectsAreStopped()) |
| 57 return; | 65 return; |
| 58 m_state = Rejecting; | 66 m_state = Rejecting; |
| 59 NewScriptState::Scope scope(m_scriptState.get()); | 67 NewScriptState::Scope scope(m_scriptState.get()); |
| 60 m_value.set(m_scriptState->isolate(), toV8(value)); | 68 m_value.set(m_scriptState->isolate(), toV8(value)); |
| 61 if (!executionContext()->activeDOMObjectsAreSuspended()) | 69 if (!executionContext()->activeDOMObjectsAreSuspended()) |
| 62 resolveOrRejectImmediately(&m_timer); | 70 resolveOrRejectImmediately(&m_timer); |
| 63 } | 71 } |
| 64 | 72 |
| 73 // null and undefined can be passed to resolve and reject functions. | |
| 74 static NullType null() { return NullType(); } | |
| 75 static UndefinedType undefined() { return UndefinedType(); } | |
| 76 struct Boolean { | |
| 77 explicit Boolean(bool value) : value(value) { } | |
| 78 const bool value; | |
| 79 }; | |
| 80 | |
| 65 // Note that an empty ScriptPromise will be returned after resolve or | 81 // Note that an empty ScriptPromise will be returned after resolve or |
| 66 // reject is called. | 82 // reject is called. |
| 67 ScriptPromise promise() | 83 ScriptPromise promise() |
| 68 { | 84 { |
| 69 return m_resolver ? m_resolver->promise() : ScriptPromise(); | 85 return m_resolver ? m_resolver->promise() : ScriptPromise(); |
| 70 } | 86 } |
| 71 | 87 |
| 72 // ActiveDOMObject implementation. | 88 // ActiveDOMObject implementation. |
| 73 virtual void suspend() OVERRIDE; | 89 virtual void suspend() OVERRIDE; |
| 74 virtual void resume() OVERRIDE; | 90 virtual void resume() OVERRIDE; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 96 template <typename T, size_t inlineCapacity> | 112 template <typename T, size_t inlineCapacity> |
| 97 v8::Handle<v8::Value> toV8(const Vector<T, inlineCapacity>& value) | 113 v8::Handle<v8::Value> toV8(const Vector<T, inlineCapacity>& value) |
| 98 { | 114 { |
| 99 ASSERT(m_scriptState); | 115 ASSERT(m_scriptState); |
| 100 return v8ArrayNoInline(value, m_scriptState->isolate()); | 116 return v8ArrayNoInline(value, m_scriptState->isolate()); |
| 101 } | 117 } |
| 102 v8::Handle<v8::Value> toV8(ScriptValue value) | 118 v8::Handle<v8::Value> toV8(ScriptValue value) |
| 103 { | 119 { |
| 104 return value.v8Value(); | 120 return value.v8Value(); |
| 105 } | 121 } |
| 122 v8::Handle<v8::Value> toV8(v8::Handle<v8::Value> value) { return value; } | |
| 123 v8::Handle<v8::Value> toV8(NullType) { return v8::Null(m_scriptState->isolat e()); } | |
| 124 v8::Handle<v8::Value> toV8(UndefinedType) { return v8::Undefined(m_scriptSta te->isolate()); } | |
| 125 // We don't define toV8(bool) to avoid implicit conversion. | |
| 126 v8::Handle<v8::Value> toV8(Boolean value) { return v8::Boolean::New(m_script State->isolate(), value.value); } | |
| 127 v8::Handle<v8::Value> toV8(int8_t value) { return v8::Integer::New(m_scriptS tate->isolate(), value); } | |
| 128 v8::Handle<v8::Value> toV8(uint8_t value) { return v8::Integer::New(m_script State->isolate(), value); } | |
|
haraken
2014/04/17 03:46:23
Don't you want to use v8::Integer::NewFromUnsigned
| |
| 129 v8::Handle<v8::Value> toV8(int16_t value) { return v8::Integer::New(m_script State->isolate(), value); } | |
| 130 v8::Handle<v8::Value> toV8(uint16_t value) { return v8::Integer::New(m_scrip tState->isolate(), value); } | |
|
haraken
2014/04/17 03:46:23
Ditto.
| |
| 131 v8::Handle<v8::Value> toV8(int32_t value) { return v8::Integer::New(m_script State->isolate(), value); } | |
| 132 v8::Handle<v8::Value> toV8(uint32_t value) { return v8::Number::New(m_script State->isolate(), value); } | |
|
haraken
2014/04/17 03:46:23
Ditto.
| |
| 133 v8::Handle<v8::Value> toV8(int64_t value) { return v8::Number::New(m_scriptS tate->isolate(), value); } | |
| 134 v8::Handle<v8::Value> toV8(uint64_t value) { return v8::Number::New(m_script State->isolate(), value); } | |
| 135 v8::Handle<v8::Value> toV8(double value) { return v8::Number::New(m_scriptSt ate->isolate(), value); } | |
| 136 v8::Handle<v8::Value> toV8(const char* value) | |
| 137 { | |
| 138 return v8::String::NewFromUtf8(m_scriptState->isolate(), value, v8::Stri ng::kInternalizedString, strlen(value)); | |
| 139 } | |
| 140 v8::Handle<v8::Value> toV8(const String& value) | |
| 141 { | |
| 142 return v8String(m_scriptState->isolate(), value); | |
| 143 } | |
| 106 | 144 |
| 107 void resolveOrRejectImmediately(Timer<ScriptPromiseResolverWithContext>*); | 145 void resolveOrRejectImmediately(Timer<ScriptPromiseResolverWithContext>*); |
| 108 void clear(); | 146 void clear(); |
| 109 | 147 |
| 110 ResolutionState m_state; | 148 ResolutionState m_state; |
| 111 RefPtr<NewScriptState> m_scriptState; | 149 RefPtr<NewScriptState> m_scriptState; |
| 112 Timer<ScriptPromiseResolverWithContext> m_timer; | 150 Timer<ScriptPromiseResolverWithContext> m_timer; |
| 113 RefPtr<ScriptPromiseResolver> m_resolver; | 151 RefPtr<ScriptPromiseResolver> m_resolver; |
| 114 ScopedPersistent<v8::Value> m_value; | 152 ScopedPersistent<v8::Value> m_value; |
| 115 }; | 153 }; |
| 116 | 154 |
| 117 } // namespace WebCore | 155 } // namespace WebCore |
| 118 | 156 |
| 119 #endif // #ifndef ScriptPromiseResolverWithContext_h | 157 #endif // #ifndef ScriptPromiseResolverWithContext_h |
| OLD | NEW |