Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / ScriptPromise.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/ScriptPromise.h"
33
34 #include "RuntimeEnabledFeatures.h"
35 #include "bindings/v8/V8Binding.h"
36 #include "bindings/v8/V8DOMWrapper.h"
37 #include "bindings/v8/custom/V8PromiseCustom.h"
38
39 #include <v8.h>
40
41 namespace WebCore {
42
43 ScriptPromise::ScriptPromise(ScriptState* scriptState, v8::Handle<v8::Value> value)
44     : m_scriptState(scriptState)
45 {
46     if (value.IsEmpty())
47         return;
48
49     if (!V8PromiseCustom::isPromise(value, scriptState->isolate()) && !value->IsPromise()) {
50         m_promise = ScriptValue(scriptState, v8::Handle<v8::Value>());
51         V8ThrowException::throwTypeError("the given value is not a Promise", scriptState->isolate());
52         return;
53     }
54     m_promise = ScriptValue(scriptState, value);
55 }
56
57 ScriptPromise ScriptPromise::then(PassOwnPtr<ScriptFunction> onFulfilled, PassOwnPtr<ScriptFunction> onRejected)
58 {
59     if (m_promise.isEmpty())
60         return ScriptPromise();
61
62     v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
63     v8::Local<v8::Function> v8OnFulfilled = ScriptFunction::adoptByGarbageCollector(onFulfilled);
64     v8::Local<v8::Function> v8OnRejected = ScriptFunction::adoptByGarbageCollector(onRejected);
65
66     if (V8PromiseCustom::isPromise(promise, m_scriptState->isolate()))
67         return ScriptPromise(m_scriptState.get(), V8PromiseCustom::then(promise, v8OnFulfilled, v8OnRejected, m_scriptState->isolate()));
68
69     ASSERT(promise->IsPromise());
70     // Return this Promise if no handlers are given.
71     // In fact it is not the exact bahavior of Promise.prototype.then
72     // but that is not a problem in this case.
73     v8::Local<v8::Promise> resultPromise = promise.As<v8::Promise>();
74     // FIXME: Use Then once it is introduced.
75     if (!v8OnFulfilled.IsEmpty()) {
76         resultPromise = resultPromise->Chain(v8OnFulfilled);
77         if (resultPromise.IsEmpty()) {
78             // v8::Promise::Chain may return an empty value, for example when
79             // the stack is exhausted.
80             return ScriptPromise();
81         }
82     }
83     if (!v8OnRejected.IsEmpty())
84         resultPromise = resultPromise->Catch(v8OnRejected);
85
86     return ScriptPromise(m_scriptState.get(), resultPromise);
87 }
88
89 ScriptPromise ScriptPromise::cast(const ScriptValue& value)
90 {
91     if (value.isEmpty())
92         return ScriptPromise();
93     v8::Local<v8::Value> v8Value(value.v8Value());
94     v8::Isolate* isolate = value.isolate();
95     if (V8PromiseCustom::isPromise(v8Value, isolate) || v8Value->IsPromise()) {
96         return ScriptPromise(value.scriptState(), v8Value);
97     }
98     if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled()) {
99         v8::Local<v8::Promise::Resolver> resolver = v8::Promise::Resolver::New(isolate);
100         if (resolver.IsEmpty()) {
101             // The Promise constructor may return an empty value, for example
102             // when the stack is exhausted.
103             return ScriptPromise();
104         }
105         resolver->Resolve(v8Value);
106         return ScriptPromise(value.scriptState(), resolver->GetPromise());
107     }
108     return ScriptPromise(value.scriptState(), V8PromiseCustom::toPromise(v8Value, isolate));
109 }
110
111 } // namespace WebCore