| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "bindings/v8/ScriptPromiseResolver.h" | 32 #include "bindings/v8/ScriptPromiseResolver.h" |
| 33 | 33 |
| 34 #include "RuntimeEnabledFeatures.h" | |
| 35 #include "bindings/v8/ScriptValue.h" | 34 #include "bindings/v8/ScriptValue.h" |
| 36 #include "bindings/v8/V8Binding.h" | 35 #include "bindings/v8/V8Binding.h" |
| 37 #include "bindings/v8/V8DOMWrapper.h" | 36 #include "bindings/v8/V8DOMWrapper.h" |
| 38 #include "bindings/v8/custom/V8PromiseCustom.h" | |
| 39 | 37 |
| 40 #include <v8.h> | 38 #include <v8.h> |
| 41 | 39 |
| 42 namespace WebCore { | 40 namespace WebCore { |
| 43 | 41 |
| 44 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* scriptState) | 42 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* scriptState) |
| 45 : m_scriptState(scriptState) | 43 : m_scriptState(scriptState) |
| 46 { | 44 { |
| 47 v8::Isolate* isolate = m_scriptState->isolate(); | 45 v8::Isolate* isolate = m_scriptState->isolate(); |
| 48 ASSERT(!m_scriptState->contextIsEmpty()); | 46 ASSERT(!m_scriptState->contextIsEmpty()); |
| 49 ASSERT(isolate->InContext()); | 47 ASSERT(isolate->InContext()); |
| 50 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) { | 48 m_resolver = ScriptValue(scriptState, v8::Promise::Resolver::New(isolate)); |
| 51 m_resolver = ScriptValue(scriptState, v8::Promise::Resolver::New(isolate
)); | |
| 52 } else { | |
| 53 m_promise = ScriptPromise(scriptState, V8PromiseCustom::createPromise(m_
scriptState->context()->Global(), isolate)); | |
| 54 } | |
| 55 } | 49 } |
| 56 | 50 |
| 57 ScriptPromiseResolver::~ScriptPromiseResolver() | 51 ScriptPromiseResolver::~ScriptPromiseResolver() |
| 58 { | 52 { |
| 59 // We don't call "reject" here because it requires a caller | 53 // We don't call "reject" here because it requires a caller |
| 60 // to be in a v8 context. | 54 // to be in a v8 context. |
| 61 | 55 |
| 62 m_promise.clear(); | |
| 63 m_resolver.clear(); | 56 m_resolver.clear(); |
| 64 } | 57 } |
| 65 | 58 |
| 66 ScriptPromise ScriptPromiseResolver::promise() | 59 ScriptPromise ScriptPromiseResolver::promise() |
| 67 { | 60 { |
| 68 ASSERT(!m_scriptState->contextIsEmpty()); | 61 ASSERT(!m_scriptState->contextIsEmpty()); |
| 69 ASSERT(m_scriptState->isolate()->InContext()); | 62 ASSERT(m_scriptState->isolate()->InContext()); |
| 70 if (!m_resolver.isEmpty()) { | 63 if (!m_resolver.isEmpty()) { |
| 71 v8::Local<v8::Promise::Resolver> v8Resolver = m_resolver.v8Value().As<v8
::Promise::Resolver>(); | 64 v8::Local<v8::Promise::Resolver> v8Resolver = m_resolver.v8Value().As<v8
::Promise::Resolver>(); |
| 72 return ScriptPromise(m_scriptState.get(), v8Resolver->GetPromise()); | 65 return ScriptPromise(m_scriptState.get(), v8Resolver->GetPromise()); |
| 73 } | 66 } |
| 74 return m_promise; | 67 return ScriptPromise(); |
| 75 } | 68 } |
| 76 | 69 |
| 77 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ScriptState* scr
iptState) | 70 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ScriptState* scr
iptState) |
| 78 { | 71 { |
| 79 ASSERT(scriptState->isolate()->InContext()); | 72 ASSERT(scriptState->isolate()->InContext()); |
| 80 return adoptRef(new ScriptPromiseResolver(scriptState)); | 73 return adoptRef(new ScriptPromiseResolver(scriptState)); |
| 81 } | 74 } |
| 82 | 75 |
| 83 void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value) | 76 void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value) |
| 84 { | 77 { |
| 85 ASSERT(m_scriptState->isolate()->InContext()); | 78 ASSERT(m_scriptState->isolate()->InContext()); |
| 86 if (!m_resolver.isEmpty()) { | 79 if (!m_resolver.isEmpty()) { |
| 87 m_resolver.v8Value().As<v8::Promise::Resolver>()->Resolve(value); | 80 m_resolver.v8Value().As<v8::Promise::Resolver>()->Resolve(value); |
| 88 } else if (!m_promise.isEmpty()) { | |
| 89 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); | |
| 90 ASSERT(V8PromiseCustom::isPromise(promise, m_scriptState->isolate())); | |
| 91 V8PromiseCustom::resolve(promise, value, m_scriptState->isolate()); | |
| 92 } | 81 } |
| 93 m_promise.clear(); | |
| 94 m_resolver.clear(); | 82 m_resolver.clear(); |
| 95 } | 83 } |
| 96 | 84 |
| 97 void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value) | 85 void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value) |
| 98 { | 86 { |
| 99 ASSERT(m_scriptState->isolate()->InContext()); | 87 ASSERT(m_scriptState->isolate()->InContext()); |
| 100 if (!m_resolver.isEmpty()) { | 88 if (!m_resolver.isEmpty()) { |
| 101 m_resolver.v8Value().As<v8::Promise::Resolver>()->Reject(value); | 89 m_resolver.v8Value().As<v8::Promise::Resolver>()->Reject(value); |
| 102 } else if (!m_promise.isEmpty()) { | |
| 103 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>(); | |
| 104 ASSERT(V8PromiseCustom::isPromise(promise, m_scriptState->isolate())); | |
| 105 V8PromiseCustom::reject(promise, value, m_scriptState->isolate()); | |
| 106 } | 90 } |
| 107 m_promise.clear(); | |
| 108 m_resolver.clear(); | 91 m_resolver.clear(); |
| 109 } | 92 } |
| 110 | 93 |
| 111 } // namespace WebCore | 94 } // namespace WebCore |
| OLD | NEW |