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

Side by Side Diff: third_party/WebKit/Source/modules/payments/PaymentRequest.cpp

Issue 2030193002: Add 'total' field to 'PaymentDetails'. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase, address comments, and add more tests Created 4 years, 6 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
OLDNEW
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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 output->amount = CurrencyAmount::From(input.amount()); 72 output->amount = CurrencyAmount::From(input.amount());
73 return output; 73 return output;
74 } 74 }
75 }; 75 };
76 76
77 template <> 77 template <>
78 struct TypeConverter<PaymentDetailsPtr, blink::PaymentDetails> { 78 struct TypeConverter<PaymentDetailsPtr, blink::PaymentDetails> {
79 static PaymentDetailsPtr Convert(const blink::PaymentDetails& input) 79 static PaymentDetailsPtr Convert(const blink::PaymentDetails& input)
80 { 80 {
81 PaymentDetailsPtr output = PaymentDetails::New(); 81 PaymentDetailsPtr output = PaymentDetails::New();
82 output->display_items = mojo::WTFArray<PaymentItemPtr>::From(input.displ ayItems()); 82 output->total = PaymentItem::From(input.total());
83
84 if (input.hasDisplayItems())
85 output->display_items = mojo::WTFArray<PaymentItemPtr>::From(input.d isplayItems());
86 else
87 output->display_items = mojo::WTFArray<PaymentItemPtr>::New(0);
88
83 if (input.hasShippingOptions()) 89 if (input.hasShippingOptions())
84 output->shipping_options = mojo::WTFArray<ShippingOptionPtr>::From(i nput.shippingOptions()); 90 output->shipping_options = mojo::WTFArray<ShippingOptionPtr>::From(i nput.shippingOptions());
85 else 91 else
86 output->shipping_options = mojo::WTFArray<ShippingOptionPtr>::New(0) ; 92 output->shipping_options = mojo::WTFArray<ShippingOptionPtr>::New(0) ;
93
87 return output; 94 return output;
88 } 95 }
89 }; 96 };
90 97
91 template <> 98 template <>
92 struct TypeConverter<PaymentOptionsPtr, blink::PaymentOptions> { 99 struct TypeConverter<PaymentOptionsPtr, blink::PaymentOptions> {
93 static PaymentOptionsPtr Convert(const blink::PaymentOptions& input) 100 static PaymentOptionsPtr Convert(const blink::PaymentOptions& input)
94 { 101 {
95 PaymentOptionsPtr output = PaymentOptions::New(); 102 PaymentOptionsPtr output = PaymentOptions::New();
96 output->request_shipping = input.requestShipping(); 103 output->request_shipping = input.requestShipping();
97 return output; 104 return output;
98 } 105 }
99 }; 106 };
100 107
101 } // namespace mojo 108 } // namespace mojo
102 109
103 namespace blink { 110 namespace blink {
104 namespace { 111 namespace {
105 112
106 // Validates ShippingOption and PaymentItem dictionaries, which happen to have i dentical fields, 113 // Validates ShippingOption or PaymentItem, which happen to have identical field s,
107 // except for "id", which is present only in ShippingOption. 114 // except for "id", which is present only in ShippingOption.
108 template <typename T> 115 template <typename T>
109 void validateShippingOptionsOrPaymentItems(HeapVector<T> items, ExceptionState& exceptionState) 116 void validateShippingOptionOrPaymentItem(const T& item, ExceptionState& exceptio nState)
110 { 117 {
118 if (!item.hasLabel() || item.label().isEmpty()) {
119 exceptionState.throwTypeError("Item label required");
120 return;
121 }
122
123 if (!item.hasAmount()) {
124 exceptionState.throwTypeError("Currency amount required");
125 return;
126 }
127
128 if (!item.amount().hasCurrency()) {
129 exceptionState.throwTypeError("Currency code required");
130 return;
131 }
132
133 if (!item.amount().hasValue()) {
134 exceptionState.throwTypeError("Currency value required");
135 return;
136 }
137
111 String errorMessage; 138 String errorMessage;
112 for (const auto& item : items) { 139 if (!PaymentsValidators::isValidCurrencyCodeFormat(item.amount().currency(), &errorMessage)) {
113 if (!item.hasLabel() || item.label().isEmpty()) { 140 exceptionState.throwTypeError(errorMessage);
114 exceptionState.throwTypeError("Item label required"); 141 return;
115 return; 142 }
116 }
117 143
118 if (!item.hasAmount()) { 144 if (!PaymentsValidators::isValidAmountFormat(item.amount().value(), &errorMe ssage)) {
119 exceptionState.throwTypeError("Currency amount required"); 145 exceptionState.throwTypeError(errorMessage);
120 return; 146 return;
121 }
122
123 if (!item.amount().hasCurrency()) {
124 exceptionState.throwTypeError("Currency code required");
125 return;
126 }
127
128 if (!item.amount().hasValue()) {
129 exceptionState.throwTypeError("Currency value required");
130 return;
131 }
132
133 if (!PaymentsValidators::isValidCurrencyCodeFormat(item.amount().currenc y(), &errorMessage)) {
134 exceptionState.throwTypeError(errorMessage);
135 return;
136 }
137
138 if (!PaymentsValidators::isValidAmountFormat(item.amount().value(), &err orMessage)) {
139 exceptionState.throwTypeError(errorMessage);
140 return;
141 }
142 } 147 }
143 } 148 }
144 149
145 void validateShippingOptionsIds(HeapVector<ShippingOption> options, ExceptionSta te& exceptionState) 150 void validateDisplayItems(const HeapVector<PaymentItem>& items, ExceptionState& exceptionState)
151 {
152 for (const auto& item : items) {
153 validateShippingOptionOrPaymentItem(item, exceptionState);
154 if (exceptionState.hadException())
155 return;
156 }
157 }
158
159 void validateShippingOptions(const HeapVector<ShippingOption>& options, Exceptio nState& exceptionState)
146 { 160 {
147 for (const auto& option : options) { 161 for (const auto& option : options) {
148 if (!option.hasId() || option.id().isEmpty()) { 162 if (!option.hasId() || option.id().isEmpty()) {
149 exceptionState.throwTypeError("ShippingOption id required"); 163 exceptionState.throwTypeError("ShippingOption id required");
150 return; 164 return;
151 } 165 }
166
167 validateShippingOptionOrPaymentItem(option, exceptionState);
168 if (exceptionState.hadException())
169 return;
152 } 170 }
153 } 171 }
154 172
155 void validatePaymentDetails(const PaymentDetails& details, ExceptionState& excep tionState) 173 void validatePaymentDetails(const PaymentDetails& details, ExceptionState& excep tionState)
156 { 174 {
157 if (!details.hasDisplayItems()) { 175 if (!details.hasTotal()) {
158 exceptionState.throwTypeError("Must specify display items"); 176 exceptionState.throwTypeError("Must specify total");
159 return; 177 return;
160 } 178 }
161 179
162 if (details.displayItems().isEmpty()) { 180 validateShippingOptionOrPaymentItem(details.total(), exceptionState);
163 exceptionState.throwTypeError("Must specify at least one item"); 181 if (exceptionState.hadException())
182 return;
183
184 if (details.total().amount().value()[0] == '-') {
185 exceptionState.throwTypeError("Total amount value should be non-negative ");
164 return; 186 return;
165 } 187 }
166 188
167 validateShippingOptionsOrPaymentItems(details.displayItems(), exceptionState ); 189 if (details.hasDisplayItems()) {
168 if (exceptionState.hadException()) 190 validateDisplayItems(details.displayItems(), exceptionState);
169 return; 191 if (exceptionState.hadException())
192 return;
193 }
170 194
171 if (details.hasShippingOptions()) { 195 if (details.hasShippingOptions()) {
172 validateShippingOptionsOrPaymentItems(details.shippingOptions(), excepti onState); 196 validateShippingOptions(details.shippingOptions(), exceptionState);
173 validateShippingOptionsIds(details.shippingOptions(), exceptionState);
174 } 197 }
175 } 198 }
176 199
177 } // namespace 200 } // namespace
178 201
179 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St ring>& supportedMethods, const PaymentDetails& details, ExceptionState& exceptio nState) 202 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St ring>& supportedMethods, const PaymentDetails& details, ExceptionState& exceptio nState)
180 { 203 {
181 return new PaymentRequest(scriptState, supportedMethods, details, PaymentOpt ions(), ScriptValue(), exceptionState); 204 return new PaymentRequest(scriptState, supportedMethods, details, PaymentOpt ions(), ScriptValue(), exceptionState);
182 } 205 }
183 206
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 void PaymentRequest::clearResolversAndCloseMojoConnection() 466 void PaymentRequest::clearResolversAndCloseMojoConnection()
444 { 467 {
445 m_completeResolver.clear(); 468 m_completeResolver.clear();
446 m_showResolver.clear(); 469 m_showResolver.clear();
447 if (m_clientBinding.is_bound()) 470 if (m_clientBinding.is_bound())
448 m_clientBinding.Close(); 471 m_clientBinding.Close();
449 m_paymentProvider.reset(); 472 m_paymentProvider.reset();
450 } 473 }
451 474
452 } // namespace blink 475 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698