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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 return ScriptValue(); | 97 return ScriptValue(); |
98 } | 98 } |
99 | 99 |
100 const ResolveType resolve_type_; | 100 const ResolveType resolve_type_; |
101 const size_t index_; | 101 const size_t index_; |
102 Member<PromiseAllHandler> handler_; | 102 Member<PromiseAllHandler> handler_; |
103 }; | 103 }; |
104 | 104 |
105 PromiseAllHandler(ScriptState* script_state, Vector<ScriptPromise> promises) | 105 PromiseAllHandler(ScriptState* script_state, Vector<ScriptPromise> promises) |
106 : number_of_pending_promises_(promises.size()), resolver_(script_state) { | 106 : number_of_pending_promises_(promises.size()), resolver_(script_state) { |
107 ASSERT(!promises.IsEmpty()); | 107 DCHECK(!promises.IsEmpty()); |
108 values_.Resize(promises.size()); | 108 values_.Resize(promises.size()); |
109 for (size_t i = 0; i < promises.size(); ++i) | 109 for (size_t i = 0; i < promises.size(); ++i) |
110 promises[i].Then(CreateFulfillFunction(script_state, i), | 110 promises[i].Then(CreateFulfillFunction(script_state, i), |
111 CreateRejectFunction(script_state)); | 111 CreateRejectFunction(script_state)); |
112 } | 112 } |
113 | 113 |
114 v8::Local<v8::Function> CreateFulfillFunction(ScriptState* script_state, | 114 v8::Local<v8::Function> CreateFulfillFunction(ScriptState* script_state, |
115 size_t index) { | 115 size_t index) { |
116 return AdapterFunction::Create(script_state, AdapterFunction::kFulfilled, | 116 return AdapterFunction::Create(script_state, AdapterFunction::kFulfilled, |
117 index, this); | 117 index, this); |
118 } | 118 } |
119 | 119 |
120 v8::Local<v8::Function> CreateRejectFunction(ScriptState* script_state) { | 120 v8::Local<v8::Function> CreateRejectFunction(ScriptState* script_state) { |
121 return AdapterFunction::Create(script_state, AdapterFunction::kRejected, 0, | 121 return AdapterFunction::Create(script_state, AdapterFunction::kRejected, 0, |
122 this); | 122 this); |
123 } | 123 } |
124 | 124 |
125 void OnFulfilled(size_t index, const ScriptValue& value) { | 125 void OnFulfilled(size_t index, const ScriptValue& value) { |
126 if (is_settled_) | 126 if (is_settled_) |
127 return; | 127 return; |
128 | 128 |
129 ASSERT(index < values_.size()); | 129 DCHECK_LT(index, values_.size()); |
130 values_[index] = value; | 130 values_[index] = value; |
131 if (--number_of_pending_promises_ > 0) | 131 if (--number_of_pending_promises_ > 0) |
132 return; | 132 return; |
133 | 133 |
134 v8::Local<v8::Array> values = | 134 v8::Local<v8::Array> values = |
135 v8::Array::New(value.GetIsolate(), values_.size()); | 135 v8::Array::New(value.GetIsolate(), values_.size()); |
136 for (size_t i = 0; i < values_.size(); ++i) { | 136 for (size_t i = 0; i < values_.size(); ++i) { |
137 if (!V8CallBoolean(values->CreateDataProperty(value.GetContext(), i, | 137 if (!V8CallBoolean(values->CreateDataProperty(value.GetContext(), i, |
138 values_[i].V8Value()))) | 138 values_[i].V8Value()))) |
139 return; | 139 return; |
140 } | 140 } |
141 | 141 |
142 MarkPromiseSettled(); | 142 MarkPromiseSettled(); |
143 resolver_.Resolve(values); | 143 resolver_.Resolve(values); |
144 } | 144 } |
145 | 145 |
146 void OnRejected(const ScriptValue& value) { | 146 void OnRejected(const ScriptValue& value) { |
147 if (is_settled_) | 147 if (is_settled_) |
148 return; | 148 return; |
149 MarkPromiseSettled(); | 149 MarkPromiseSettled(); |
150 resolver_.Reject(value.V8Value()); | 150 resolver_.Reject(value.V8Value()); |
151 } | 151 } |
152 | 152 |
153 void MarkPromiseSettled() { | 153 void MarkPromiseSettled() { |
154 ASSERT(!is_settled_); | 154 DCHECK(!is_settled_); |
155 is_settled_ = true; | 155 is_settled_ = true; |
156 values_.Clear(); | 156 values_.Clear(); |
157 } | 157 } |
158 | 158 |
159 size_t number_of_pending_promises_; | 159 size_t number_of_pending_promises_; |
160 ScriptPromise::InternalResolver resolver_; | 160 ScriptPromise::InternalResolver resolver_; |
161 bool is_settled_ = false; | 161 bool is_settled_ = false; |
162 | 162 |
163 // This is cleared when owners of this handler, that is, given promises are | 163 // This is cleared when owners of this handler, that is, given promises are |
164 // settled. | 164 // settled. |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 DecreaseInstanceCount(); | 235 DecreaseInstanceCount(); |
236 } | 236 } |
237 | 237 |
238 ScriptPromise ScriptPromise::Then(v8::Local<v8::Function> on_fulfilled, | 238 ScriptPromise ScriptPromise::Then(v8::Local<v8::Function> on_fulfilled, |
239 v8::Local<v8::Function> on_rejected) { | 239 v8::Local<v8::Function> on_rejected) { |
240 if (promise_.IsEmpty()) | 240 if (promise_.IsEmpty()) |
241 return ScriptPromise(); | 241 return ScriptPromise(); |
242 | 242 |
243 v8::Local<v8::Object> promise = promise_.V8Value().As<v8::Object>(); | 243 v8::Local<v8::Object> promise = promise_.V8Value().As<v8::Object>(); |
244 | 244 |
245 ASSERT(promise->IsPromise()); | 245 DCHECK(promise->IsPromise()); |
246 // Return this Promise if no handlers are given. | 246 // Return this Promise if no handlers are given. |
247 // In fact it is not the exact bahavior of Promise.prototype.then | 247 // In fact it is not the exact bahavior of Promise.prototype.then |
248 // but that is not a problem in this case. | 248 // but that is not a problem in this case. |
249 v8::Local<v8::Promise> result_promise = promise.As<v8::Promise>(); | 249 v8::Local<v8::Promise> result_promise = promise.As<v8::Promise>(); |
250 if (!on_fulfilled.IsEmpty()) { | 250 if (!on_fulfilled.IsEmpty()) { |
251 if (!result_promise->Then(script_state_->GetContext(), on_fulfilled) | 251 if (!result_promise->Then(script_state_->GetContext(), on_fulfilled) |
252 .ToLocal(&result_promise)) | 252 .ToLocal(&result_promise)) |
253 return ScriptPromise(); | 253 return ScriptPromise(); |
254 } | 254 } |
255 if (!on_rejected.IsEmpty()) { | 255 if (!on_rejected.IsEmpty()) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 if (value.IsEmpty()) | 294 if (value.IsEmpty()) |
295 return ScriptPromise(); | 295 return ScriptPromise(); |
296 InternalResolver resolver(script_state); | 296 InternalResolver resolver(script_state); |
297 ScriptPromise promise = resolver.Promise(); | 297 ScriptPromise promise = resolver.Promise(); |
298 resolver.Reject(value); | 298 resolver.Reject(value); |
299 return promise; | 299 return promise; |
300 } | 300 } |
301 | 301 |
302 ScriptPromise ScriptPromise::RejectWithDOMException(ScriptState* script_state, | 302 ScriptPromise ScriptPromise::RejectWithDOMException(ScriptState* script_state, |
303 DOMException* exception) { | 303 DOMException* exception) { |
304 ASSERT(script_state->GetIsolate()->InContext()); | 304 DCHECK(script_state->GetIsolate()->InContext()); |
305 return Reject(script_state, | 305 return Reject(script_state, |
306 ToV8(exception, script_state->GetContext()->Global(), | 306 ToV8(exception, script_state->GetContext()->Global(), |
307 script_state->GetIsolate())); | 307 script_state->GetIsolate())); |
308 } | 308 } |
309 | 309 |
310 v8::Local<v8::Promise> ScriptPromise::RejectRaw(ScriptState* script_state, | 310 v8::Local<v8::Promise> ScriptPromise::RejectRaw(ScriptState* script_state, |
311 v8::Local<v8::Value> value) { | 311 v8::Local<v8::Value> value) { |
312 if (value.IsEmpty()) | 312 if (value.IsEmpty()) |
313 return v8::Local<v8::Promise>(); | 313 return v8::Local<v8::Promise>(); |
314 v8::Local<v8::Promise::Resolver> resolver; | 314 v8::Local<v8::Promise::Resolver> resolver; |
(...skipping 12 matching lines...) Expand all Loading... |
327 | 327 |
328 void ScriptPromise::IncreaseInstanceCount() { | 328 void ScriptPromise::IncreaseInstanceCount() { |
329 InstanceCounters::IncrementCounter(InstanceCounters::kScriptPromiseCounter); | 329 InstanceCounters::IncrementCounter(InstanceCounters::kScriptPromiseCounter); |
330 } | 330 } |
331 | 331 |
332 void ScriptPromise::DecreaseInstanceCount() { | 332 void ScriptPromise::DecreaseInstanceCount() { |
333 InstanceCounters::DecrementCounter(InstanceCounters::kScriptPromiseCounter); | 333 InstanceCounters::DecrementCounter(InstanceCounters::kScriptPromiseCounter); |
334 } | 334 } |
335 | 335 |
336 } // namespace blink | 336 } // namespace blink |
OLD | NEW |