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