| 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 #include "bindings/core/v8/ScriptPromisePropertyBase.h" | 5 #include "bindings/core/v8/ScriptPromisePropertyBase.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScopedPersistent.h" | 7 #include "bindings/core/v8/ScopedPersistent.h" |
| 8 #include "bindings/core/v8/ScriptState.h" | 8 #include "bindings/core/v8/ScriptState.h" |
| 9 #include "bindings/core/v8/V8Binding.h" | 9 #include "bindings/core/v8/V8Binding.h" |
| 10 #include "bindings/core/v8/V8HiddenValue.h" | 10 #include "bindings/core/v8/V8HiddenValue.h" |
| 11 #include "core/dom/ExecutionContext.h" | 11 #include "core/dom/ExecutionContext.h" |
| 12 #include "wtf/PtrUtil.h" | 12 #include "wtf/PtrUtil.h" |
| 13 #include <memory> | 13 #include <memory> |
| 14 | 14 |
| 15 namespace blink { | 15 namespace blink { |
| 16 | 16 |
| 17 ScriptPromisePropertyBase::ScriptPromisePropertyBase( | 17 ScriptPromisePropertyBase::ScriptPromisePropertyBase( |
| 18 ExecutionContext* executionContext, | 18 ExecutionContext* executionContext, |
| 19 Name name) | 19 Name name) |
| 20 : ContextLifecycleObserver(executionContext), | 20 : m_executionContext(executionContext), |
| 21 m_isolate(toIsolate(executionContext)), | 21 m_isolate(toIsolate(executionContext)), |
| 22 m_name(name), | 22 m_name(name), |
| 23 m_state(Pending) {} | 23 m_state(Pending) {} |
| 24 | 24 |
| 25 ScriptPromisePropertyBase::~ScriptPromisePropertyBase() { | 25 ScriptPromisePropertyBase::~ScriptPromisePropertyBase() { |
| 26 clearWrappers(); | 26 clearWrappers(); |
| 27 } | 27 } |
| 28 | 28 |
| 29 ExecutionContext* ScriptPromisePropertyBase::getExecutionContext() const { |
| 30 return m_executionContext; |
| 31 } |
| 32 |
| 29 ScriptPromise ScriptPromisePropertyBase::promise(DOMWrapperWorld& world) { | 33 ScriptPromise ScriptPromisePropertyBase::promise(DOMWrapperWorld& world) { |
| 30 if (!getExecutionContext()) | 34 if (m_executionContext->isContextDestroyed()) |
| 31 return ScriptPromise(); | 35 return ScriptPromise(); |
| 32 | 36 |
| 33 v8::HandleScope handleScope(m_isolate); | 37 v8::HandleScope handleScope(m_isolate); |
| 34 v8::Local<v8::Context> context = toV8Context(getExecutionContext(), world); | 38 v8::Local<v8::Context> context = toV8Context(m_executionContext, world); |
| 35 if (context.IsEmpty()) | 39 if (context.IsEmpty()) |
| 36 return ScriptPromise(); | 40 return ScriptPromise(); |
| 37 ScriptState* scriptState = ScriptState::from(context); | 41 ScriptState* scriptState = ScriptState::from(context); |
| 38 ScriptState::Scope scope(scriptState); | 42 ScriptState::Scope scope(scriptState); |
| 39 | 43 |
| 40 v8::Local<v8::Object> wrapper = ensureHolderWrapper(scriptState); | 44 v8::Local<v8::Object> wrapper = ensureHolderWrapper(scriptState); |
| 41 ASSERT(wrapper->CreationContext() == context); | 45 DCHECK(wrapper->CreationContext() == context); |
| 42 | 46 |
| 43 v8::Local<v8::Value> cachedPromise = | 47 v8::Local<v8::Value> cachedPromise = |
| 44 V8HiddenValue::getHiddenValue(scriptState, wrapper, promiseName()); | 48 V8HiddenValue::getHiddenValue(scriptState, wrapper, promiseName()); |
| 45 if (!cachedPromise.IsEmpty() && cachedPromise->IsPromise()) | 49 if (!cachedPromise.IsEmpty() && cachedPromise->IsPromise()) |
| 46 return ScriptPromise(scriptState, cachedPromise); | 50 return ScriptPromise(scriptState, cachedPromise); |
| 47 | 51 |
| 48 // Create and cache the Promise | 52 // Create and cache the Promise |
| 49 v8::Local<v8::Promise::Resolver> resolver; | 53 v8::Local<v8::Promise::Resolver> resolver; |
| 50 if (!v8::Promise::Resolver::New(context).ToLocal(&resolver)) | 54 if (!v8::Promise::Resolver::New(context).ToLocal(&resolver)) |
| 51 return ScriptPromise(); | 55 return ScriptPromise(); |
| 52 v8::Local<v8::Promise> promise = resolver->GetPromise(); | 56 v8::Local<v8::Promise> promise = resolver->GetPromise(); |
| 53 V8HiddenValue::setHiddenValue(scriptState, wrapper, promiseName(), promise); | 57 V8HiddenValue::setHiddenValue(scriptState, wrapper, promiseName(), promise); |
| 54 | 58 |
| 55 switch (m_state) { | 59 switch (m_state) { |
| 56 case Pending: | 60 case Pending: |
| 57 // Cache the resolver too | 61 // Cache the resolver too |
| 58 V8HiddenValue::setHiddenValue(scriptState, wrapper, resolverName(), | 62 V8HiddenValue::setHiddenValue(scriptState, wrapper, resolverName(), |
| 59 resolver); | 63 resolver); |
| 60 break; | 64 break; |
| 61 case Resolved: | 65 case Resolved: |
| 62 case Rejected: | 66 case Rejected: |
| 63 resolveOrRejectInternal(resolver); | 67 resolveOrRejectInternal(resolver); |
| 64 break; | 68 break; |
| 65 } | 69 } |
| 66 | 70 |
| 67 return ScriptPromise(scriptState, promise); | 71 return ScriptPromise(scriptState, promise); |
| 68 } | 72 } |
| 69 | 73 |
| 70 void ScriptPromisePropertyBase::resolveOrReject(State targetState) { | 74 void ScriptPromisePropertyBase::resolveOrReject(State targetState) { |
| 71 ASSERT(getExecutionContext()); | 75 DCHECK(!m_executionContext->isContextDestroyed()); |
| 72 ASSERT(m_state == Pending); | 76 DCHECK(m_state == Pending); |
| 73 ASSERT(targetState == Resolved || targetState == Rejected); | 77 DCHECK(targetState == Resolved || targetState == Rejected); |
| 74 | 78 |
| 75 m_state = targetState; | 79 m_state = targetState; |
| 76 | 80 |
| 77 v8::HandleScope handleScope(m_isolate); | 81 v8::HandleScope handleScope(m_isolate); |
| 78 size_t i = 0; | 82 size_t i = 0; |
| 79 while (i < m_wrappers.size()) { | 83 while (i < m_wrappers.size()) { |
| 80 const std::unique_ptr<ScopedPersistent<v8::Object>>& persistent = | 84 const std::unique_ptr<ScopedPersistent<v8::Object>>& persistent = |
| 81 m_wrappers[i]; | 85 m_wrappers[i]; |
| 82 if (persistent->isEmpty()) { | 86 if (persistent->isEmpty()) { |
| 83 // wrapper has died. | 87 // wrapper has died. |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 | 204 |
| 201 SCRIPT_PROMISE_PROPERTIES(P) | 205 SCRIPT_PROMISE_PROPERTIES(P) |
| 202 | 206 |
| 203 #undef P | 207 #undef P |
| 204 } | 208 } |
| 205 ASSERT_NOT_REACHED(); | 209 ASSERT_NOT_REACHED(); |
| 206 return v8::Local<v8::String>(); | 210 return v8::Local<v8::String>(); |
| 207 } | 211 } |
| 208 | 212 |
| 209 DEFINE_TRACE(ScriptPromisePropertyBase) { | 213 DEFINE_TRACE(ScriptPromisePropertyBase) { |
| 210 ContextLifecycleObserver::trace(visitor); | 214 visitor->trace(m_executionContext); |
| 211 } | 215 } |
| 212 | 216 |
| 213 } // namespace blink | 217 } // namespace blink |
| OLD | NEW |