Chromium Code Reviews| 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 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 | 216 |
| 217 void validateDisplayItems(const HeapVector<PaymentItem>& items, | 217 void validateDisplayItems(const HeapVector<PaymentItem>& items, |
| 218 ExceptionState& exceptionState) { | 218 ExceptionState& exceptionState) { |
| 219 for (const auto& item : items) { | 219 for (const auto& item : items) { |
| 220 validateShippingOptionOrPaymentItem(item, exceptionState); | 220 validateShippingOptionOrPaymentItem(item, exceptionState); |
| 221 if (exceptionState.hadException()) | 221 if (exceptionState.hadException()) |
| 222 return; | 222 return; |
| 223 } | 223 } |
| 224 } | 224 } |
| 225 | 225 |
| 226 void validateShippingOptions(const HeapVector<PaymentShippingOption>& options, | 226 void validateAndFixupShippingOptions(PaymentDetails& details, |
|
please use gerrit instead
2016/10/12 01:28:02
nit: Pass in only "HeapVector<PaymentShippingOptio
zino
2016/10/12 18:05:24
Done.
| |
| 227 ExceptionState& exceptionState) { | 227 ExceptionState& exceptionState) { |
| 228 HashSet<String> uniqueIds; | 228 HashSet<String> uniqueIds; |
| 229 for (const auto& option : options) { | 229 for (const auto& option : details.shippingOptions()) { |
| 230 if (!option.hasId() || option.id().isEmpty()) { | 230 if (!option.hasId() || option.id().isEmpty()) { |
| 231 exceptionState.throwTypeError("ShippingOption id required"); | 231 exceptionState.throwTypeError("ShippingOption id required"); |
| 232 return; | 232 return; |
| 233 } | 233 } |
| 234 | 234 |
| 235 if (uniqueIds.contains(option.id())) { | 235 if (uniqueIds.contains(option.id())) { |
| 236 exceptionState.throwTypeError( | 236 details.setShippingOptions(HeapVector<PaymentShippingOption>()); |
| 237 "Duplicate shipping option identifiers are not allowed"); | |
| 238 return; | 237 return; |
| 239 } | 238 } |
| 240 uniqueIds.add(option.id()); | 239 uniqueIds.add(option.id()); |
| 241 | 240 |
| 242 validateShippingOptionOrPaymentItem(option, exceptionState); | 241 validateShippingOptionOrPaymentItem(option, exceptionState); |
| 243 if (exceptionState.hadException()) | 242 if (exceptionState.hadException()) |
| 244 return; | 243 return; |
| 245 } | 244 } |
| 246 } | 245 } |
| 247 | 246 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 } | 283 } |
| 285 | 284 |
| 286 if (modifier.hasAdditionalDisplayItems()) { | 285 if (modifier.hasAdditionalDisplayItems()) { |
| 287 validateDisplayItems(modifier.additionalDisplayItems(), exceptionState); | 286 validateDisplayItems(modifier.additionalDisplayItems(), exceptionState); |
| 288 if (exceptionState.hadException()) | 287 if (exceptionState.hadException()) |
| 289 return; | 288 return; |
| 290 } | 289 } |
| 291 } | 290 } |
| 292 } | 291 } |
| 293 | 292 |
| 294 void validatePaymentDetails(const PaymentDetails& details, | 293 void validateAndFixupPaymentDetails(PaymentDetails& details, |
| 295 ExceptionState& exceptionState) { | 294 ExceptionState& exceptionState) { |
| 296 if (!details.hasTotal()) { | 295 if (!details.hasTotal()) { |
| 297 exceptionState.throwTypeError("Must specify total"); | 296 exceptionState.throwTypeError("Must specify total"); |
| 298 return; | 297 return; |
| 299 } | 298 } |
| 300 | 299 |
| 301 validateShippingOptionOrPaymentItem(details.total(), exceptionState); | 300 validateShippingOptionOrPaymentItem(details.total(), exceptionState); |
| 302 if (exceptionState.hadException()) | 301 if (exceptionState.hadException()) |
| 303 return; | 302 return; |
| 304 | 303 |
| 305 if (details.total().amount().value()[0] == '-') { | 304 if (details.total().amount().value()[0] == '-') { |
| 306 exceptionState.throwTypeError("Total amount value should be non-negative"); | 305 exceptionState.throwTypeError("Total amount value should be non-negative"); |
| 307 return; | 306 return; |
| 308 } | 307 } |
| 309 | 308 |
| 310 if (details.hasDisplayItems()) { | 309 if (details.hasDisplayItems()) { |
| 311 validateDisplayItems(details.displayItems(), exceptionState); | 310 validateDisplayItems(details.displayItems(), exceptionState); |
| 312 if (exceptionState.hadException()) | 311 if (exceptionState.hadException()) |
| 313 return; | 312 return; |
| 314 } | 313 } |
| 315 | 314 |
| 316 if (details.hasShippingOptions()) { | 315 if (details.hasShippingOptions()) { |
| 317 validateShippingOptions(details.shippingOptions(), exceptionState); | 316 validateAndFixupShippingOptions(details, exceptionState); |
| 318 if (exceptionState.hadException()) | 317 if (exceptionState.hadException()) |
| 319 return; | 318 return; |
| 320 } | 319 } |
| 321 | 320 |
| 322 if (details.hasModifiers()) { | 321 if (details.hasModifiers()) { |
| 323 validatePaymentDetailsModifiers(details.modifiers(), exceptionState); | 322 validatePaymentDetailsModifiers(details.modifiers(), exceptionState); |
| 324 } | 323 } |
| 325 | 324 |
| 326 String errorMessage; | 325 String errorMessage; |
| 327 if (!PaymentsValidators::isValidErrorMsgFormat(details.error(), | 326 if (!PaymentsValidators::isValidErrorMsgFormat(details.error(), |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 517 V8PaymentDetails::toImpl(detailsScriptValue.isolate(), | 516 V8PaymentDetails::toImpl(detailsScriptValue.isolate(), |
| 518 detailsScriptValue.v8Value(), details, | 517 detailsScriptValue.v8Value(), details, |
| 519 exceptionState); | 518 exceptionState); |
| 520 if (exceptionState.hadException()) { | 519 if (exceptionState.hadException()) { |
| 521 m_showResolver->reject( | 520 m_showResolver->reject( |
| 522 DOMException::create(SyntaxError, exceptionState.message())); | 521 DOMException::create(SyntaxError, exceptionState.message())); |
| 523 clearResolversAndCloseMojoConnection(); | 522 clearResolversAndCloseMojoConnection(); |
| 524 return; | 523 return; |
| 525 } | 524 } |
| 526 | 525 |
| 527 validatePaymentDetails(details, exceptionState); | 526 validateAndFixupPaymentDetails(details, exceptionState); |
| 528 if (exceptionState.hadException()) { | 527 if (exceptionState.hadException()) { |
| 529 m_showResolver->reject( | 528 m_showResolver->reject( |
| 530 DOMException::create(SyntaxError, exceptionState.message())); | 529 DOMException::create(SyntaxError, exceptionState.message())); |
| 531 clearResolversAndCloseMojoConnection(); | 530 clearResolversAndCloseMojoConnection(); |
| 532 return; | 531 return; |
| 533 } | 532 } |
| 534 | 533 |
| 535 if (m_options.requestShipping()) | 534 if (m_options.requestShipping()) |
| 536 m_shippingOption = getSelectedShippingOption(details); | 535 m_shippingOption = getSelectedShippingOption(details); |
| 537 | 536 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 582 return; | 581 return; |
| 583 } | 582 } |
| 584 | 583 |
| 585 if (!scriptState->domWindow()->frame() || | 584 if (!scriptState->domWindow()->frame() || |
| 586 !scriptState->domWindow()->frame()->isMainFrame()) { | 585 !scriptState->domWindow()->frame()->isMainFrame()) { |
| 587 exceptionState.throwSecurityError( | 586 exceptionState.throwSecurityError( |
| 588 "Must be in a top-level browsing context"); | 587 "Must be in a top-level browsing context"); |
| 589 return; | 588 return; |
| 590 } | 589 } |
| 591 | 590 |
| 592 validatePaymentDetails(details, exceptionState); | 591 PaymentDetails fixedDetails(details); |
| 592 validateAndFixupPaymentDetails(fixedDetails, exceptionState); | |
| 593 if (exceptionState.hadException()) | 593 if (exceptionState.hadException()) |
| 594 return; | 594 return; |
| 595 | 595 |
| 596 if (details.hasError() && !details.error().isEmpty()) { | 596 if (details.hasError() && !details.error().isEmpty()) { |
|
please use gerrit instead
2016/10/12 01:28:02
To ensure correctness, stop using "details" immedi
zino
2016/10/12 18:05:24
Done.
| |
| 597 exceptionState.throwTypeError("Error value should be empty"); | 597 exceptionState.throwTypeError("Error value should be empty"); |
| 598 return; | 598 return; |
| 599 } | 599 } |
| 600 | 600 |
| 601 if (m_options.requestShipping()) { | 601 if (m_options.requestShipping()) { |
| 602 m_shippingOption = getSelectedShippingOption(details); | 602 m_shippingOption = getSelectedShippingOption(fixedDetails); |
| 603 m_shippingType = getValidShippingType(m_options.shippingType()); | 603 m_shippingType = getValidShippingType(m_options.shippingType()); |
| 604 } | 604 } |
| 605 | 605 |
| 606 scriptState->domWindow()->frame()->interfaceProvider()->getInterface( | 606 scriptState->domWindow()->frame()->interfaceProvider()->getInterface( |
| 607 mojo::GetProxy(&m_paymentProvider)); | 607 mojo::GetProxy(&m_paymentProvider)); |
| 608 m_paymentProvider.set_connection_error_handler(convertToBaseCallback( | 608 m_paymentProvider.set_connection_error_handler(convertToBaseCallback( |
| 609 WTF::bind(&PaymentRequest::OnError, wrapWeakPersistent(this), | 609 WTF::bind(&PaymentRequest::OnError, wrapWeakPersistent(this), |
| 610 mojom::blink::PaymentErrorReason::UNKNOWN))); | 610 mojom::blink::PaymentErrorReason::UNKNOWN))); |
| 611 m_paymentProvider->Init( | 611 m_paymentProvider->Init( |
| 612 m_clientBinding.CreateInterfacePtrAndBind(), | 612 m_clientBinding.CreateInterfacePtrAndBind(), |
| 613 mojo::WTFArray<mojom::blink::PaymentMethodDataPtr>::From( | 613 mojo::WTFArray<mojom::blink::PaymentMethodDataPtr>::From( |
| 614 validatedMethodData), | 614 validatedMethodData), |
| 615 mojom::blink::PaymentDetails::From(details), | 615 mojom::blink::PaymentDetails::From(fixedDetails), |
| 616 mojom::blink::PaymentOptions::From(m_options)); | 616 mojom::blink::PaymentOptions::From(m_options)); |
| 617 } | 617 } |
| 618 | 618 |
| 619 void PaymentRequest::contextDestroyed() { | 619 void PaymentRequest::contextDestroyed() { |
| 620 clearResolversAndCloseMojoConnection(); | 620 clearResolversAndCloseMojoConnection(); |
| 621 } | 621 } |
| 622 | 622 |
| 623 void PaymentRequest::OnShippingAddressChange( | 623 void PaymentRequest::OnShippingAddressChange( |
| 624 mojom::blink::PaymentAddressPtr address) { | 624 mojom::blink::PaymentAddressPtr address) { |
| 625 DCHECK(m_showResolver); | 625 DCHECK(m_showResolver); |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 783 m_completeTimer.stop(); | 783 m_completeTimer.stop(); |
| 784 m_completeResolver.clear(); | 784 m_completeResolver.clear(); |
| 785 m_showResolver.clear(); | 785 m_showResolver.clear(); |
| 786 m_abortResolver.clear(); | 786 m_abortResolver.clear(); |
| 787 if (m_clientBinding.is_bound()) | 787 if (m_clientBinding.is_bound()) |
| 788 m_clientBinding.Close(); | 788 m_clientBinding.Close(); |
| 789 m_paymentProvider.reset(); | 789 m_paymentProvider.reset(); |
| 790 } | 790 } |
| 791 | 791 |
| 792 } // namespace blink | 792 } // namespace blink |
| OLD | NEW |