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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 const blink::PaymentDetailsModifier& input) { | 87 const blink::PaymentDetailsModifier& input) { |
| 88 PaymentDetailsModifierPtr output = PaymentDetailsModifier::New(); | 88 PaymentDetailsModifierPtr output = PaymentDetailsModifier::New(); |
| 89 output->supported_methods = | 89 output->supported_methods = |
| 90 WTF::Vector<WTF::String>(input.supportedMethods()); | 90 WTF::Vector<WTF::String>(input.supportedMethods()); |
| 91 | 91 |
| 92 if (input.hasTotal()) | 92 if (input.hasTotal()) |
| 93 output->total = PaymentItem::From(input.total()); | 93 output->total = PaymentItem::From(input.total()); |
| 94 else | 94 else |
| 95 output->total = PaymentItem::New(); | 95 output->total = PaymentItem::New(); |
| 96 | 96 |
| 97 if (input.hasAdditionalDisplayItems()) | 97 if (input.hasAdditionalDisplayItems()) { |
| 98 output->additional_display_items = | 98 for (size_t i = 0; i < input.additionalDisplayItems().size(); ++i) { |
| 99 mojo::WTFArray<PaymentItemPtr>::From(input.additionalDisplayItems()); | 99 output->additional_display_items.append( |
|
please use gerrit instead
2016/10/12 23:51:55
Is it not possible to continue using WTFArray::Fro
Kevin Bailey
2016/10/13 18:30:42
The destination field has changed from mojo::WTFAr
please use gerrit instead
2016/10/13 20:24:00
Please see if there's a shorter way to write this,
| |
| 100 else | 100 TypeConverter<PaymentItemPtr, blink::PaymentItem>::Convert( |
| 101 output->additional_display_items = mojo::WTFArray<PaymentItemPtr>::New(0); | 101 input.additionalDisplayItems()[i])); |
|
please use gerrit instead
2016/10/12 23:51:55
It's shorter to call "PaymentItem::From()" instead
Kevin Bailey
2016/10/13 18:30:42
Ok, changed all 4 occurrences.
| |
| 102 | 102 } |
| 103 } | |
| 103 return output; | 104 return output; |
| 104 } | 105 } |
| 105 }; | 106 }; |
| 106 | 107 |
| 107 template <> | 108 template <> |
| 108 struct TypeConverter<PaymentDetailsPtr, blink::PaymentDetails> { | 109 struct TypeConverter<PaymentDetailsPtr, blink::PaymentDetails> { |
| 109 static PaymentDetailsPtr Convert(const blink::PaymentDetails& input) { | 110 static PaymentDetailsPtr Convert(const blink::PaymentDetails& input) { |
| 110 PaymentDetailsPtr output = PaymentDetails::New(); | 111 PaymentDetailsPtr output = PaymentDetails::New(); |
| 111 output->total = PaymentItem::From(input.total()); | 112 output->total = PaymentItem::From(input.total()); |
| 112 | 113 |
| 113 if (input.hasDisplayItems()) | 114 if (input.hasDisplayItems()) { |
| 114 output->display_items = | 115 for (size_t i = 0; i < input.displayItems().size(); ++i) { |
| 115 mojo::WTFArray<PaymentItemPtr>::From(input.displayItems()); | 116 output->display_items.append( |
| 116 else | 117 TypeConverter<PaymentItemPtr, blink::PaymentItem>::Convert( |
| 117 output->display_items = mojo::WTFArray<PaymentItemPtr>::New(0); | 118 input.displayItems()[i])); |
| 119 } | |
| 120 } | |
| 118 | 121 |
| 119 if (input.hasShippingOptions()) | 122 if (input.hasShippingOptions()) { |
| 120 output->shipping_options = mojo::WTFArray<PaymentShippingOptionPtr>::From( | 123 for (size_t i = 0; i < input.shippingOptions().size(); ++i) { |
| 121 input.shippingOptions()); | 124 output->shipping_options.append( |
| 122 else | 125 TypeConverter<PaymentShippingOptionPtr, |
| 123 output->shipping_options = | 126 blink::PaymentShippingOption>:: |
| 124 mojo::WTFArray<PaymentShippingOptionPtr>::New(0); | 127 Convert(input.shippingOptions()[i])); |
| 128 } | |
| 129 } | |
| 125 | 130 |
| 126 if (input.hasModifiers()) | 131 if (input.hasModifiers()) { |
| 127 output->modifiers = | 132 for (size_t i = 0; i < input.modifiers().size(); ++i) { |
| 128 mojo::WTFArray<PaymentDetailsModifierPtr>::From(input.modifiers()); | 133 output->modifiers.append( |
| 129 else | 134 TypeConverter< |
| 130 output->modifiers = mojo::WTFArray<PaymentDetailsModifierPtr>::New(0); | 135 PaymentDetailsModifierPtr, |
| 136 blink::PaymentDetailsModifier>::Convert(input.modifiers()[i])); | |
| 137 } | |
| 138 } | |
| 131 | 139 |
| 132 if (input.hasError()) | 140 if (input.hasError()) |
| 133 output->error = input.error(); | 141 output->error = input.error(); |
| 134 else | 142 else |
| 135 output->error = WTF::emptyString(); | 143 output->error = WTF::emptyString(); |
| 136 | 144 |
| 137 return output; | 145 return output; |
| 138 } | 146 } |
| 139 }; | 147 }; |
| 140 | 148 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 162 WTF::Vector<WTF::String>(input[i].supportedMethods); | 170 WTF::Vector<WTF::String>(input[i].supportedMethods); |
| 163 output[i]->stringified_data = input[i].stringifiedData; | 171 output[i]->stringified_data = input[i].stringifiedData; |
| 164 } | 172 } |
| 165 return output; | 173 return output; |
| 166 } | 174 } |
| 167 }; | 175 }; |
| 168 | 176 |
| 169 } // namespace mojo | 177 } // namespace mojo |
| 170 | 178 |
| 171 namespace blink { | 179 namespace blink { |
| 180 | |
| 181 bool validatePaymentDetails(const mojo::PaymentDetailsPtr&); | |
|
please use gerrit instead
2016/10/12 23:51:55
Pretty sure that this declaration is no-op, becaus
Kevin Bailey
2016/10/13 18:30:42
Was left-over. Gone.
| |
| 182 | |
| 172 namespace { | 183 namespace { |
| 173 | 184 |
| 174 // If the website does not call complete() 60 seconds after show() has been | 185 // If the website does not call complete() 60 seconds after show() has been |
| 175 // resolved, then behave as if the website called complete("fail"). | 186 // resolved, then behave as if the website called complete("fail"). |
| 176 static const int completeTimeoutSeconds = 60; | 187 static const int completeTimeoutSeconds = 60; |
| 177 | 188 |
| 178 // Validates ShippingOption or PaymentItem, which happen to have identical | 189 // Validates ShippingOption or PaymentItem, which happen to have identical |
| 179 // fields, except for "id", which is present only in ShippingOption. | 190 // fields, except for "id", which is present only in ShippingOption. |
| 180 template <typename T> | 191 template <typename T> |
| 181 void validateShippingOptionOrPaymentItem(const T& item, | 192 void validateShippingOptionOrPaymentItem(const T& item, |
| (...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 581 exceptionState.throwSecurityError("Must be in a secure context"); | 592 exceptionState.throwSecurityError("Must be in a secure context"); |
| 582 return; | 593 return; |
| 583 } | 594 } |
| 584 | 595 |
| 585 if (!scriptState->domWindow()->frame() || | 596 if (!scriptState->domWindow()->frame() || |
| 586 !scriptState->domWindow()->frame()->isMainFrame()) { | 597 !scriptState->domWindow()->frame()->isMainFrame()) { |
| 587 exceptionState.throwSecurityError( | 598 exceptionState.throwSecurityError( |
| 588 "Must be in a top-level browsing context"); | 599 "Must be in a top-level browsing context"); |
| 589 return; | 600 return; |
| 590 } | 601 } |
| 591 | |
| 592 validatePaymentDetails(details, exceptionState); | |
| 593 if (exceptionState.hadException()) | 602 if (exceptionState.hadException()) |
| 594 return; | 603 return; |
| 595 | 604 |
| 596 if (details.hasError() && !details.error().isEmpty()) { | 605 if (details.hasError() && !details.error().isEmpty()) { |
| 597 exceptionState.throwTypeError("Error value should be empty"); | 606 exceptionState.throwTypeError("Error value should be empty"); |
| 598 return; | 607 return; |
| 599 } | 608 } |
| 600 | 609 |
| 601 if (m_options.requestShipping()) { | 610 if (m_options.requestShipping()) { |
| 602 m_shippingOption = getSelectedShippingOption(details); | 611 m_shippingOption = getSelectedShippingOption(details); |
| 603 m_shippingType = getValidShippingType(m_options.shippingType()); | 612 m_shippingType = getValidShippingType(m_options.shippingType()); |
| 604 } | 613 } |
| 605 | 614 |
| 606 scriptState->domWindow()->frame()->interfaceProvider()->getInterface( | 615 scriptState->domWindow()->frame()->interfaceProvider()->getInterface( |
| 607 mojo::GetProxy(&m_paymentProvider)); | 616 mojo::GetProxy(&m_paymentProvider)); |
| 608 m_paymentProvider.set_connection_error_handler(convertToBaseCallback( | 617 m_paymentProvider.set_connection_error_handler(convertToBaseCallback( |
| 609 WTF::bind(&PaymentRequest::OnError, wrapWeakPersistent(this), | 618 WTF::bind(&PaymentRequest::OnError, wrapWeakPersistent(this), |
| 610 mojom::blink::PaymentErrorReason::UNKNOWN))); | 619 mojom::blink::PaymentErrorReason::UNKNOWN))); |
| 611 m_paymentProvider->Init( | 620 |
| 612 m_clientBinding.CreateInterfacePtrAndBind(), | 621 WTF::Vector<mojom::blink::PaymentMethodDataPtr> methods( |
| 613 mojo::WTFArray<mojom::blink::PaymentMethodDataPtr>::From( | 622 validatedMethodData.size()); |
| 614 validatedMethodData), | 623 for (size_t i = 0; i < validatedMethodData.size(); ++i) { |
| 615 mojom::blink::PaymentDetails::From(details), | 624 methods[i] = mojom::blink::PaymentMethodData::New(); |
| 616 mojom::blink::PaymentOptions::From(m_options)); | 625 methods[i]->supported_methods = |
| 626 WTF::Vector<WTF::String>(validatedMethodData[i].supportedMethods); | |
| 627 methods[i]->stringified_data = validatedMethodData[i].stringifiedData; | |
| 628 } | |
| 629 m_paymentProvider->Init(m_clientBinding.CreateInterfacePtrAndBind(), | |
| 630 std::move(methods), | |
| 631 mojom::blink::PaymentDetails::From(details), | |
| 632 mojom::blink::PaymentOptions::From(m_options)); | |
| 617 } | 633 } |
| 618 | 634 |
| 619 void PaymentRequest::contextDestroyed() { | 635 void PaymentRequest::contextDestroyed() { |
| 620 clearResolversAndCloseMojoConnection(); | 636 clearResolversAndCloseMojoConnection(); |
| 621 } | 637 } |
| 622 | 638 |
| 623 void PaymentRequest::OnShippingAddressChange( | 639 void PaymentRequest::OnShippingAddressChange( |
| 624 mojom::blink::PaymentAddressPtr address) { | 640 mojom::blink::PaymentAddressPtr address) { |
| 625 DCHECK(m_showResolver); | 641 DCHECK(m_showResolver); |
| 626 DCHECK(!m_completeResolver); | 642 DCHECK(!m_completeResolver); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 783 m_completeTimer.stop(); | 799 m_completeTimer.stop(); |
| 784 m_completeResolver.clear(); | 800 m_completeResolver.clear(); |
| 785 m_showResolver.clear(); | 801 m_showResolver.clear(); |
| 786 m_abortResolver.clear(); | 802 m_abortResolver.clear(); |
| 787 if (m_clientBinding.is_bound()) | 803 if (m_clientBinding.is_bound()) |
| 788 m_clientBinding.Close(); | 804 m_clientBinding.Close(); |
| 789 m_paymentProvider.reset(); | 805 m_paymentProvider.reset(); |
| 790 } | 806 } |
| 791 | 807 |
| 792 } // namespace blink | 808 } // namespace blink |
| OLD | NEW |