Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / ScriptPromiseResolver.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
29  */
30
31 #include "config.h"
32 #include "bindings/v8/ScriptPromiseResolver.h"
33
34 #include "RuntimeEnabledFeatures.h"
35 #include "bindings/v8/ScriptValue.h"
36 #include "bindings/v8/V8Binding.h"
37 #include "bindings/v8/V8DOMWrapper.h"
38 #include "bindings/v8/custom/V8PromiseCustom.h"
39
40 #include <v8.h>
41
42 namespace WebCore {
43
44 ScriptPromiseResolver::ScriptPromiseResolver(ScriptState* scriptState)
45     : m_scriptState(scriptState)
46 {
47     v8::Isolate* isolate = m_scriptState->isolate();
48     ASSERT(!m_scriptState->contextIsEmpty());
49     ASSERT(isolate->InContext());
50     if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) {
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 }
56
57 ScriptPromiseResolver::~ScriptPromiseResolver()
58 {
59     // We don't call "reject" here because it requires a caller
60     // to be in a v8 context.
61
62     m_promise.clear();
63     m_resolver.clear();
64 }
65
66 ScriptPromise ScriptPromiseResolver::promise()
67 {
68     ASSERT(!m_scriptState->contextIsEmpty());
69     ASSERT(m_scriptState->isolate()->InContext());
70     if (!m_resolver.isEmpty()) {
71         v8::Local<v8::Promise::Resolver> v8Resolver = m_resolver.v8Value().As<v8::Promise::Resolver>();
72         return ScriptPromise(m_scriptState.get(), v8Resolver->GetPromise());
73     }
74     return m_promise;
75 }
76
77 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ScriptState* scriptState)
78 {
79     ASSERT(scriptState->isolate()->InContext());
80     return adoptRef(new ScriptPromiseResolver(scriptState));
81 }
82
83 void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value)
84 {
85     ASSERT(m_scriptState->isolate()->InContext());
86     if (!m_resolver.isEmpty()) {
87         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     }
93     m_promise.clear();
94     m_resolver.clear();
95 }
96
97 void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value)
98 {
99     ASSERT(m_scriptState->isolate()->InContext());
100     if (!m_resolver.isEmpty()) {
101         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     }
107     m_promise.clear();
108     m_resolver.clear();
109 }
110
111 } // namespace WebCore