Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: Source/bindings/v8/ScriptPromise.cpp

Issue 351423002: Moved files under Source/bindings/v8 to Source/bindings/core/v8. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/bindings/v8/ScriptPromise.h ('k') | Source/bindings/v8/ScriptPromiseResolver.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "bindings/v8/V8Binding.h"
35 #include "core/dom/DOMException.h"
36
37 #include <v8.h>
38
39 namespace WebCore {
40
41 namespace {
42
43 struct WithScriptState {
44 // Used by ToV8Value<WithScriptState, ScriptState*>.
45 static v8::Handle<v8::Object> getCreationContext(ScriptState* scriptState)
46 {
47 return scriptState->context()->Global();
48 }
49 };
50
51 } // namespace
52
53 ScriptPromise::InternalResolver::InternalResolver(ScriptState* scriptState)
54 : m_resolver(scriptState, v8::Promise::Resolver::New(scriptState->isolate()) ) { }
55
56 v8::Local<v8::Promise> ScriptPromise::InternalResolver::v8Promise() const
57 {
58 if (m_resolver.isEmpty())
59 return v8::Local<v8::Promise>();
60 return m_resolver.v8Value().As<v8::Promise::Resolver>()->GetPromise();
61 }
62
63 ScriptPromise ScriptPromise::InternalResolver::promise() const
64 {
65 if (m_resolver.isEmpty())
66 return ScriptPromise();
67 return ScriptPromise(m_resolver.scriptState(), v8Promise());
68 }
69
70 void ScriptPromise::InternalResolver::resolve(v8::Local<v8::Value> value)
71 {
72 if (m_resolver.isEmpty())
73 return;
74 m_resolver.v8Value().As<v8::Promise::Resolver>()->Resolve(value);
75 clear();
76 }
77
78 void ScriptPromise::InternalResolver::reject(v8::Local<v8::Value> value)
79 {
80 if (m_resolver.isEmpty())
81 return;
82 m_resolver.v8Value().As<v8::Promise::Resolver>()->Reject(value);
83 clear();
84 }
85
86 ScriptPromise::ScriptPromise(ScriptState* scriptState, v8::Handle<v8::Value> val ue)
87 : m_scriptState(scriptState)
88 {
89 if (value.IsEmpty())
90 return;
91
92 if (!value->IsPromise()) {
93 m_promise = ScriptValue(scriptState, v8::Handle<v8::Value>());
94 V8ThrowException::throwTypeError("the given value is not a Promise", scr iptState->isolate());
95 return;
96 }
97 m_promise = ScriptValue(scriptState, value);
98 }
99
100 ScriptPromise ScriptPromise::then(PassOwnPtr<ScriptFunction> onFulfilled, PassOw nPtr<ScriptFunction> onRejected)
101 {
102 if (m_promise.isEmpty())
103 return ScriptPromise();
104
105 v8::Local<v8::Object> promise = m_promise.v8Value().As<v8::Object>();
106 v8::Local<v8::Function> v8OnFulfilled = ScriptFunction::adoptByGarbageCollec tor(onFulfilled);
107 v8::Local<v8::Function> v8OnRejected = ScriptFunction::adoptByGarbageCollect or(onRejected);
108
109 ASSERT(promise->IsPromise());
110 // Return this Promise if no handlers are given.
111 // In fact it is not the exact bahavior of Promise.prototype.then
112 // but that is not a problem in this case.
113 v8::Local<v8::Promise> resultPromise = promise.As<v8::Promise>();
114 if (!v8OnFulfilled.IsEmpty()) {
115 resultPromise = resultPromise->Then(v8OnFulfilled);
116 if (resultPromise.IsEmpty()) {
117 // v8::Promise::Then may return an empty value, for example when
118 // the stack is exhausted.
119 return ScriptPromise();
120 }
121 }
122 if (!v8OnRejected.IsEmpty())
123 resultPromise = resultPromise->Catch(v8OnRejected);
124
125 return ScriptPromise(m_scriptState.get(), resultPromise);
126 }
127
128 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, const ScriptValue& v alue)
129 {
130 return ScriptPromise::cast(scriptState, value.v8Value());
131 }
132
133 ScriptPromise ScriptPromise::cast(ScriptState* scriptState, v8::Handle<v8::Value > value)
134 {
135 if (value.IsEmpty())
136 return ScriptPromise();
137 if (value->IsPromise()) {
138 return ScriptPromise(scriptState, value);
139 }
140 InternalResolver resolver(scriptState);
141 ScriptPromise promise = resolver.promise();
142 resolver.resolve(value);
143 return promise;
144 }
145
146 ScriptPromise ScriptPromise::reject(ScriptState* scriptState, const ScriptValue& value)
147 {
148 return ScriptPromise::reject(scriptState, value.v8Value());
149 }
150
151 ScriptPromise ScriptPromise::reject(ScriptState* scriptState, v8::Handle<v8::Val ue> value)
152 {
153 if (value.IsEmpty())
154 return ScriptPromise();
155 InternalResolver resolver(scriptState);
156 ScriptPromise promise = resolver.promise();
157 resolver.reject(value);
158 return promise;
159 }
160
161 ScriptPromise ScriptPromise::rejectWithDOMException(ScriptState* scriptState, Pa ssRefPtrWillBeRawPtr<DOMException> exception)
162 {
163 ASSERT(scriptState->isolate()->InContext());
164 return reject(scriptState, V8ValueTraits<PassRefPtrWillBeRawPtr<DOMException > >::toV8Value(exception, scriptState->context()->Global(), scriptState->isolate ()));
165 }
166
167 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/bindings/v8/ScriptPromise.h ('k') | Source/bindings/v8/ScriptPromiseResolver.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698