Upstream version 6.35.121.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/ScriptState.h"
36 #include "bindings/v8/ScriptValue.h"
37 #include "bindings/v8/V8Binding.h"
38 #include "bindings/v8/V8DOMWrapper.h"
39 #include "bindings/v8/custom/V8PromiseCustom.h"
40
41 #include <v8.h>
42
43 namespace WebCore {
44
45 ScriptPromiseResolver::ScriptPromiseResolver(ExecutionContext* context)
46     : m_isolate(toIsolate(context))
47 {
48     ASSERT(context);
49     v8::Isolate* isolate = toIsolate(context);
50     ASSERT(isolate->InContext());
51     if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) {
52         m_resolver = ScriptValue(v8::Promise::Resolver::New(isolate), isolate);
53     } else {
54         v8::Handle<v8::Context> v8Context = toV8Context(context, DOMWrapperWorld::current(isolate));
55         v8::Handle<v8::Object> creationContext = v8Context.IsEmpty() ? v8::Object::New(isolate) : v8Context->Global();
56         m_promise = ScriptPromise(V8PromiseCustom::createPromise(creationContext, isolate), isolate);
57     }
58 }
59
60 ScriptPromiseResolver::ScriptPromiseResolver(v8::Isolate* isolate)
61     : m_isolate(isolate)
62 {
63     ASSERT(isolate->InContext());
64     if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) {
65         m_resolver = ScriptValue(v8::Promise::Resolver::New(isolate), isolate);
66     } else {
67         m_promise = ScriptPromise(V8PromiseCustom::createPromise(v8::Object::New(isolate), isolate), isolate);
68     }
69 }
70
71 ScriptPromiseResolver::~ScriptPromiseResolver()
72 {
73     // We don't call "reject" here because it requires a caller
74     // to be in a v8 context.
75
76     m_promise.clear();
77     m_resolver.clear();
78 }
79
80 ScriptPromise ScriptPromiseResolver::promise()
81 {
82     ASSERT(m_isolate->InContext());
83     if (!m_resolver.hasNoValue()) {
84         v8::Local<v8::Promise::Resolver> v8Resolver = m_resolver.v8Value().As<v8::Promise::Resolver>();
85         return ScriptPromise(v8Resolver->GetPromise(), m_isolate);
86     }
87     return m_promise;
88 }
89
90 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(ExecutionContext* context)
91 {
92     ASSERT(context);
93     ASSERT(toIsolate(context)->InContext());
94     return adoptRef(new ScriptPromiseResolver(context));
95 }
96
97 PassRefPtr<ScriptPromiseResolver> ScriptPromiseResolver::create(v8::Isolate* isolate)
98 {
99     ASSERT(isolate->InContext());
100     return adoptRef(new ScriptPromiseResolver(isolate));
101 }
102
103 void ScriptPromiseResolver::resolve(v8::Handle<v8::Value> value)
104 {
105     ASSERT(m_isolate->InContext());
106     if (!m_resolver.hasNoValue()) {
107         m_resolver.v8Value().As<v8::Promise::Resolver>()->Resolve(value);
108     } else if (!m_promise.hasNoValue()) {
109         v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
110         ASSERT(V8PromiseCustom::isPromise(promise, m_isolate));
111         V8PromiseCustom::resolve(promise, value, m_isolate);
112     }
113     m_promise.clear();
114     m_resolver.clear();
115 }
116
117 void ScriptPromiseResolver::reject(v8::Handle<v8::Value> value)
118 {
119     ASSERT(m_isolate->InContext());
120     if (!m_resolver.hasNoValue()) {
121         m_resolver.v8Value().As<v8::Promise::Resolver>()->Reject(value);
122     } else if (!m_promise.hasNoValue()) {
123         v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
124         ASSERT(V8PromiseCustom::isPromise(promise, m_isolate));
125         V8PromiseCustom::reject(promise, value, m_isolate);
126     }
127     m_promise.clear();
128     m_resolver.clear();
129 }
130
131 void ScriptPromiseResolver::resolve(ScriptValue value)
132 {
133     ASSERT(m_isolate->InContext());
134     resolve(value.v8Value());
135 }
136
137 void ScriptPromiseResolver::reject(ScriptValue value)
138 {
139     ASSERT(m_isolate->InContext());
140     reject(value.v8Value());
141 }
142
143 } // namespace WebCore