OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/payments/PaymentRequest.h" | 5 #include "modules/payments/PaymentRequest.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/core/v8/JSONValuesForV8.h" | 8 #include "bindings/core/v8/JSONValuesForV8.h" |
9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 m_paymentProvider->Abort(); | 194 m_paymentProvider->Abort(); |
195 } | 195 } |
196 | 196 |
197 const AtomicString& PaymentRequest::interfaceName() const | 197 const AtomicString& PaymentRequest::interfaceName() const |
198 { | 198 { |
199 return EventTargetNames::PaymentRequest; | 199 return EventTargetNames::PaymentRequest; |
200 } | 200 } |
201 | 201 |
202 ExecutionContext* PaymentRequest::getExecutionContext() const | 202 ExecutionContext* PaymentRequest::getExecutionContext() const |
203 { | 203 { |
204 return m_scriptState->getExecutionContext(); | 204 return ContextLifecycleObserver::getExecutionContext(); |
205 } | 205 } |
206 | 206 |
207 ScriptPromise PaymentRequest::complete(ScriptState* scriptState, bool success) | 207 ScriptPromise PaymentRequest::complete(ScriptState* scriptState, bool success) |
208 { | 208 { |
209 if (m_completeResolver) | 209 if (m_completeResolver) |
210 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "Already called complete() once")); | 210 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "Already called complete() once")); |
211 | 211 |
212 m_completeResolver = ScriptPromiseResolver::create(scriptState); | 212 m_completeResolver = ScriptPromiseResolver::create(scriptState); |
213 m_paymentProvider->Complete(success); | 213 m_paymentProvider->Complete(success); |
214 | 214 |
215 return m_completeResolver->promise(); | 215 return m_completeResolver->promise(); |
216 } | 216 } |
217 | 217 |
218 DEFINE_TRACE(PaymentRequest) | 218 DEFINE_TRACE(PaymentRequest) |
219 { | 219 { |
220 visitor->trace(m_details); | 220 visitor->trace(m_details); |
221 visitor->trace(m_options); | 221 visitor->trace(m_options); |
222 visitor->trace(m_shippingAddress); | 222 visitor->trace(m_shippingAddress); |
223 visitor->trace(m_showResolver); | 223 visitor->trace(m_showResolver); |
224 visitor->trace(m_completeResolver); | 224 visitor->trace(m_completeResolver); |
225 EventTargetWithInlineData::trace(visitor); | 225 EventTargetWithInlineData::trace(visitor); |
| 226 ContextLifecycleObserver::trace(visitor); |
226 } | 227 } |
227 | 228 |
228 PaymentRequest::PaymentRequest(ScriptState* scriptState, const Vector<String>& s
upportedMethods, const PaymentDetails& details, const PaymentOptions& options, c
onst ScriptValue& data, ExceptionState& exceptionState) | 229 PaymentRequest::PaymentRequest(ScriptState* scriptState, const Vector<String>& s
upportedMethods, const PaymentDetails& details, const PaymentOptions& options, c
onst ScriptValue& data, ExceptionState& exceptionState) |
229 : m_scriptState(scriptState) | 230 : ContextLifecycleObserver(scriptState->getExecutionContext()) |
230 , m_supportedMethods(supportedMethods) | 231 , m_supportedMethods(supportedMethods) |
231 , m_details(details) | 232 , m_details(details) |
232 , m_options(options) | 233 , m_options(options) |
233 , m_clientBinding(this) | 234 , m_clientBinding(this) |
234 { | 235 { |
235 // TODO(rouslan): Also check for a top-level browsing context. | 236 // TODO(rouslan): Also check for a top-level browsing context. |
236 // https://github.com/w3c/browser-payment-api/issues/2 | 237 // https://github.com/w3c/browser-payment-api/issues/2 |
237 if (!scriptState->getExecutionContext()->isSecureContext()) { | 238 if (!scriptState->getExecutionContext()->isSecureContext()) { |
238 exceptionState.throwSecurityError("Must be in a secure context"); | 239 exceptionState.throwSecurityError("Must be in a secure context"); |
239 return; | 240 return; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 | 291 |
291 m_stringifiedData = jsonData->toJSONString(); | 292 m_stringifiedData = jsonData->toJSONString(); |
292 } | 293 } |
293 } | 294 } |
294 | 295 |
295 // Set the currently selected option if only one option was passed. | 296 // Set the currently selected option if only one option was passed. |
296 if (details.hasShippingOptions() && details.shippingOptions().size() == 1) | 297 if (details.hasShippingOptions() && details.shippingOptions().size() == 1) |
297 m_shippingOption = details.shippingOptions().begin()->id(); | 298 m_shippingOption = details.shippingOptions().begin()->id(); |
298 } | 299 } |
299 | 300 |
| 301 void PaymentRequest::contextDestroyed() |
| 302 { |
| 303 cleanUp(); |
| 304 } |
| 305 |
300 void PaymentRequest::OnShippingAddressChange(mojom::wtf::ShippingAddressPtr addr
ess) | 306 void PaymentRequest::OnShippingAddressChange(mojom::wtf::ShippingAddressPtr addr
ess) |
301 { | 307 { |
302 DCHECK(m_showResolver); | 308 DCHECK(m_showResolver); |
303 DCHECK(!m_completeResolver); | 309 DCHECK(!m_completeResolver); |
304 | 310 |
305 String errorMessage; | 311 String errorMessage; |
306 if (!PaymentsValidators::isValidRegionCodeFormat(address->region_code, &erro
rMessage) | 312 if (!PaymentsValidators::isValidRegionCodeFormat(address->region_code, &erro
rMessage) |
307 || !PaymentsValidators::isValidLanguageCodeFormat(address->language_code
, &errorMessage) | 313 || !PaymentsValidators::isValidLanguageCodeFormat(address->language_code
, &errorMessage) |
308 || !PaymentsValidators::isValidScriptCodeFormat(address->script_code, &e
rrorMessage)) { | 314 || !PaymentsValidators::isValidScriptCodeFormat(address->script_code, &e
rrorMessage)) { |
309 m_showResolver->reject(DOMException::create(SyntaxError, errorMessage)); | 315 m_showResolver->reject(DOMException::create(SyntaxError, errorMessage)); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 { | 359 { |
354 DCHECK(m_completeResolver); | 360 DCHECK(m_completeResolver); |
355 m_completeResolver->resolve(); | 361 m_completeResolver->resolve(); |
356 cleanUp(); | 362 cleanUp(); |
357 } | 363 } |
358 | 364 |
359 void PaymentRequest::cleanUp() | 365 void PaymentRequest::cleanUp() |
360 { | 366 { |
361 m_completeResolver.clear(); | 367 m_completeResolver.clear(); |
362 m_showResolver.clear(); | 368 m_showResolver.clear(); |
363 m_clientBinding.Close(); | 369 if (m_clientBinding.is_bound()) |
| 370 m_clientBinding.Close(); |
364 m_paymentProvider.reset(); | 371 m_paymentProvider.reset(); |
365 } | 372 } |
366 | 373 |
367 } // namespace blink | 374 } // namespace blink |
OLD | NEW |