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/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
| 10 #include "bindings/core/v8/V8StringResource.h" | 10 #include "bindings/core/v8/V8StringResource.h" |
| 11 #include "bindings/modules/v8/V8AndroidPayMethodData.h" | 11 #include "bindings/modules/v8/V8AndroidPayMethodData.h" |
| 12 #include "bindings/modules/v8/V8BasicCardRequest.h" | |
| 12 #include "bindings/modules/v8/V8PaymentDetails.h" | 13 #include "bindings/modules/v8/V8PaymentDetails.h" |
| 13 #include "core/EventTypeNames.h" | 14 #include "core/EventTypeNames.h" |
| 14 #include "core/dom/DOMException.h" | 15 #include "core/dom/DOMException.h" |
| 15 #include "core/dom/ExceptionCode.h" | 16 #include "core/dom/ExceptionCode.h" |
| 16 #include "core/events/Event.h" | 17 #include "core/events/Event.h" |
| 17 #include "core/events/EventQueue.h" | 18 #include "core/events/EventQueue.h" |
| 18 #include "core/frame/FrameOwner.h" | 19 #include "core/frame/FrameOwner.h" |
| 19 #include "core/html/HTMLIFrameElement.h" | 20 #include "core/html/HTMLIFrameElement.h" |
| 20 #include "modules/EventTargetModulesNames.h" | 21 #include "modules/EventTargetModulesNames.h" |
| 21 #include "modules/payments/AndroidPayMethodData.h" | 22 #include "modules/payments/AndroidPayMethodData.h" |
| 22 #include "modules/payments/AndroidPayTokenization.h" | 23 #include "modules/payments/AndroidPayTokenization.h" |
| 24 #include "modules/payments/BasicCardRequest.h" | |
| 23 #include "modules/payments/HTMLIFrameElementPayments.h" | 25 #include "modules/payments/HTMLIFrameElementPayments.h" |
| 24 #include "modules/payments/PaymentAddress.h" | 26 #include "modules/payments/PaymentAddress.h" |
| 25 #include "modules/payments/PaymentItem.h" | 27 #include "modules/payments/PaymentItem.h" |
| 26 #include "modules/payments/PaymentRequestUpdateEvent.h" | 28 #include "modules/payments/PaymentRequestUpdateEvent.h" |
| 27 #include "modules/payments/PaymentResponse.h" | 29 #include "modules/payments/PaymentResponse.h" |
| 28 #include "modules/payments/PaymentShippingOption.h" | 30 #include "modules/payments/PaymentShippingOption.h" |
| 29 #include "modules/payments/PaymentsValidators.h" | 31 #include "modules/payments/PaymentsValidators.h" |
| 30 #include "mojo/public/cpp/bindings/interface_request.h" | 32 #include "mojo/public/cpp/bindings/interface_request.h" |
| 31 #include "mojo/public/cpp/bindings/wtf_array.h" | 33 #include "mojo/public/cpp/bindings/wtf_array.h" |
| 32 #include "platform/RuntimeEnabledFeatures.h" | 34 #include "platform/RuntimeEnabledFeatures.h" |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 } | 175 } |
| 174 }; | 176 }; |
| 175 | 177 |
| 176 } // namespace mojo | 178 } // namespace mojo |
| 177 | 179 |
| 178 namespace blink { | 180 namespace blink { |
| 179 namespace { | 181 namespace { |
| 180 | 182 |
| 181 using payments::mojom::blink::AndroidPayCardNetwork; | 183 using payments::mojom::blink::AndroidPayCardNetwork; |
| 182 using payments::mojom::blink::AndroidPayTokenization; | 184 using payments::mojom::blink::AndroidPayTokenization; |
| 185 using payments::mojom::blink::BasicCardNetwork; | |
| 186 using payments::mojom::blink::BasicCardType; | |
| 183 | 187 |
| 184 // If the website does not call complete() 60 seconds after show() has been | 188 // If the website does not call complete() 60 seconds after show() has been |
| 185 // resolved, then behave as if the website called complete("fail"). | 189 // resolved, then behave as if the website called complete("fail"). |
| 186 static const int completeTimeoutSeconds = 60; | 190 static const int completeTimeoutSeconds = 60; |
| 187 | 191 |
| 192 static const char* androidPayMethodName = "https://android.com/pay"; | |
| 193 static const char* basicCardMethodName = "basic-card"; | |
| 194 | |
| 188 const struct { | 195 const struct { |
| 189 const AndroidPayCardNetwork code; | 196 const AndroidPayCardNetwork code; |
| 190 const char* name; | 197 const char* name; |
| 191 } kAndroidPayNetwork[] = {{AndroidPayCardNetwork::AMEX, "AMEX"}, | 198 } kAndroidPayNetwork[] = {{AndroidPayCardNetwork::AMEX, "AMEX"}, |
| 192 {AndroidPayCardNetwork::DISCOVER, "DISCOVER"}, | 199 {AndroidPayCardNetwork::DISCOVER, "DISCOVER"}, |
| 193 {AndroidPayCardNetwork::MASTERCARD, "MASTERCARD"}, | 200 {AndroidPayCardNetwork::MASTERCARD, "MASTERCARD"}, |
| 194 {AndroidPayCardNetwork::VISA, "VISA"}}; | 201 {AndroidPayCardNetwork::VISA, "VISA"}}; |
| 195 | 202 |
| 196 const struct { | 203 const struct { |
| 197 const AndroidPayTokenization code; | 204 const AndroidPayTokenization code; |
| 198 const char* name; | 205 const char* name; |
| 199 } kAndroidPayTokenization[] = { | 206 } kAndroidPayTokenization[] = { |
| 200 {AndroidPayTokenization::GATEWAY_TOKEN, "GATEWAY_TOKEN"}, | 207 {AndroidPayTokenization::GATEWAY_TOKEN, "GATEWAY_TOKEN"}, |
| 201 {AndroidPayTokenization::NETWORK_TOKEN, "NETWORK_TOKEN"}}; | 208 {AndroidPayTokenization::NETWORK_TOKEN, "NETWORK_TOKEN"}}; |
| 202 | 209 |
| 210 const struct { | |
| 211 const BasicCardNetwork code; | |
| 212 const char* name; | |
| 213 } kBasicCardNetworks[] = {{BasicCardNetwork::AMEX, "amex"}, | |
| 214 {BasicCardNetwork::DINERS, "diners"}, | |
| 215 {BasicCardNetwork::DISCOVER, "discover"}, | |
| 216 {BasicCardNetwork::JCB, "jcb"}, | |
| 217 {BasicCardNetwork::MASTERCARD, "mastercard"}, | |
| 218 {BasicCardNetwork::UNIONPAY, "unionpay"}, | |
| 219 {BasicCardNetwork::VISA, "visa"}}; | |
| 220 | |
| 221 const struct { | |
| 222 const BasicCardType code; | |
| 223 const char* name; | |
| 224 } kBasicCardTypes[] = {{BasicCardType::CREDIT, "credit"}, | |
| 225 {BasicCardType::DEBIT, "debit"}, | |
| 226 {BasicCardType::PREPAID, "prepaid"}}; | |
| 227 | |
| 203 // Validates ShippingOption or PaymentItem, which happen to have identical | 228 // Validates ShippingOption or PaymentItem, which happen to have identical |
| 204 // fields, except for "id", which is present only in ShippingOption. | 229 // fields, except for "id", which is present only in ShippingOption. |
| 205 template <typename T> | 230 template <typename T> |
| 206 void validateShippingOptionOrPaymentItem(const T& item, | 231 void validateShippingOptionOrPaymentItem(const T& item, |
| 207 ExceptionState& exceptionState) { | 232 ExceptionState& exceptionState) { |
| 208 if (!item.hasLabel() || item.label().isEmpty()) { | 233 if (!item.hasLabel() || item.label().isEmpty()) { |
| 209 exceptionState.throwTypeError("Item label required"); | 234 exceptionState.throwTypeError("Item label required"); |
| 210 return; | 235 return; |
| 211 } | 236 } |
| 212 | 237 |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 351 | 376 |
| 352 String errorMessage; | 377 String errorMessage; |
| 353 if (!PaymentsValidators::isValidErrorMsgFormat(details.error(), | 378 if (!PaymentsValidators::isValidErrorMsgFormat(details.error(), |
| 354 &errorMessage)) { | 379 &errorMessage)) { |
| 355 exceptionState.throwTypeError(errorMessage); | 380 exceptionState.throwTypeError(errorMessage); |
| 356 } | 381 } |
| 357 | 382 |
| 358 return keepShippingOptions; | 383 return keepShippingOptions; |
| 359 } | 384 } |
| 360 | 385 |
| 361 void maybeSetAndroidPayMethodData( | 386 // Parses Android Pay data to avoid parsing JSON in the browser. |
| 387 void setAndroidPayMethodData( | |
| 362 const ScriptValue& input, | 388 const ScriptValue& input, |
| 363 payments::mojom::blink::PaymentMethodDataPtr& output, | 389 payments::mojom::blink::PaymentMethodDataPtr& output, |
| 364 ExceptionState& exceptionState) { | 390 ExceptionState& exceptionState) { |
| 365 AndroidPayMethodData androidPay; | 391 AndroidPayMethodData androidPay; |
| 366 TrackExceptionState trackExceptionState; | |
| 367 V8AndroidPayMethodData::toImpl(input.isolate(), input.v8Value(), androidPay, | 392 V8AndroidPayMethodData::toImpl(input.isolate(), input.v8Value(), androidPay, |
| 368 trackExceptionState); | 393 exceptionState); |
| 369 if (trackExceptionState.hadException()) | 394 if (exceptionState.hadException()) |
| 370 return; | 395 return; |
| 371 | 396 |
| 372 if (androidPay.hasEnvironment() && androidPay.environment() == "TEST") | 397 if (androidPay.hasEnvironment() && androidPay.environment() == "TEST") |
| 373 output->environment = payments::mojom::blink::AndroidPayEnvironment::TEST; | 398 output->environment = payments::mojom::blink::AndroidPayEnvironment::TEST; |
| 374 | 399 |
| 375 output->merchant_name = androidPay.merchantName(); | 400 output->merchant_name = androidPay.merchantName(); |
| 376 output->merchant_id = androidPay.merchantId(); | 401 output->merchant_id = androidPay.merchantId(); |
| 377 | 402 |
| 378 if (androidPay.hasAllowedCardNetworks()) { | 403 if (androidPay.hasAllowedCardNetworks()) { |
| 379 output->allowed_card_networks.resize( | 404 output->allowed_card_networks.resize( |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 421 payments::mojom::blink::AndroidPayTokenizationParameter::New(); | 446 payments::mojom::blink::AndroidPayTokenizationParameter::New(); |
| 422 output->parameters[numberOfParameters]->key = keys[i]; | 447 output->parameters[numberOfParameters]->key = keys[i]; |
| 423 output->parameters[numberOfParameters]->value = value; | 448 output->parameters[numberOfParameters]->value = value; |
| 424 ++numberOfParameters; | 449 ++numberOfParameters; |
| 425 } | 450 } |
| 426 output->parameters.resize(numberOfParameters); | 451 output->parameters.resize(numberOfParameters); |
| 427 } | 452 } |
| 428 } | 453 } |
| 429 } | 454 } |
| 430 | 455 |
| 456 // Parses basic-card data to avoid parsing JSON in the browser. | |
| 457 void setBasicCardMethodData( | |
| 458 const ScriptValue& input, | |
| 459 payments::mojom::blink::PaymentMethodDataPtr& output, | |
| 460 ExceptionState& exceptionState) { | |
| 461 BasicCardRequest basicCard; | |
| 462 V8BasicCardRequest::toImpl(input.isolate(), input.v8Value(), basicCard, | |
| 463 exceptionState); | |
| 464 if (exceptionState.hadException()) | |
| 465 return; | |
| 466 | |
| 467 if (basicCard.hasSupportedNetworks()) { | |
| 468 output->supported_networks.resize(basicCard.supportedNetworks().size()); | |
| 469 size_t numberOfNetworks = 0; | |
| 470 for (size_t i = 0; i < basicCard.supportedNetworks().size(); ++i) { | |
| 471 for (size_t j = 0; j < arraysize(kBasicCardNetworks); ++j) { | |
| 472 if (basicCard.supportedNetworks()[i] == kBasicCardNetworks[j].name) { | |
| 473 output->supported_networks[numberOfNetworks++] = | |
| 474 kBasicCardNetworks[j].code; | |
| 475 break; | |
| 476 } | |
| 477 } | |
| 478 } | |
| 479 output->supported_networks.resize(numberOfNetworks); | |
| 480 } | |
| 481 | |
| 482 if (basicCard.hasSupportedTypes()) { | |
| 483 output->supported_types.resize(basicCard.supportedTypes().size()); | |
| 484 size_t numberOfTypes = 0; | |
| 485 for (size_t i = 0; i < basicCard.supportedTypes().size(); ++i) { | |
| 486 for (size_t j = 0; j < arraysize(kBasicCardTypes); ++j) { | |
| 487 if (basicCard.supportedTypes()[i] == kBasicCardTypes[j].name) { | |
| 488 output->supported_types[numberOfTypes++] = kBasicCardTypes[j].code; | |
| 489 break; | |
| 490 } | |
| 491 } | |
| 492 } | |
| 493 output->supported_types.resize(numberOfTypes); | |
| 494 } | |
| 495 } | |
| 496 | |
| 431 void validateAndConvertPaymentMethodData( | 497 void validateAndConvertPaymentMethodData( |
| 432 const HeapVector<PaymentMethodData>& input, | 498 const HeapVector<PaymentMethodData>& input, |
| 433 Vector<payments::mojom::blink::PaymentMethodDataPtr>& output, | 499 Vector<payments::mojom::blink::PaymentMethodDataPtr>& output, |
| 434 ExceptionState& exceptionState) { | 500 ExceptionState& exceptionState) { |
| 435 if (input.isEmpty()) { | 501 if (input.isEmpty()) { |
| 436 exceptionState.throwTypeError( | 502 exceptionState.throwTypeError( |
| 437 "Must specify at least one payment method identifier"); | 503 "Must specify at least one payment method identifier"); |
| 438 return; | 504 return; |
| 439 } | 505 } |
| 440 | 506 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 465 "Unable to parse payment method specific data"); | 531 "Unable to parse payment method specific data"); |
| 466 return; | 532 return; |
| 467 } | 533 } |
| 468 stringifiedData = | 534 stringifiedData = |
| 469 v8StringToWebCoreString<String>(value, DoNotExternalize); | 535 v8StringToWebCoreString<String>(value, DoNotExternalize); |
| 470 } | 536 } |
| 471 | 537 |
| 472 output[i] = payments::mojom::blink::PaymentMethodData::New(); | 538 output[i] = payments::mojom::blink::PaymentMethodData::New(); |
| 473 output[i]->supported_methods = paymentMethodData.supportedMethods(); | 539 output[i]->supported_methods = paymentMethodData.supportedMethods(); |
| 474 output[i]->stringified_data = stringifiedData; | 540 output[i]->stringified_data = stringifiedData; |
| 475 maybeSetAndroidPayMethodData(paymentMethodData.data(), output[i], | 541 |
| 476 exceptionState); | 542 if (paymentMethodData.supportedMethods().contains(androidPayMethodName)) { |
| 477 if (exceptionState.hadException()) | 543 setAndroidPayMethodData(paymentMethodData.data(), output[i], |
| 478 return; | 544 exceptionState); |
| 545 if (exceptionState.hadException()) | |
|
Marijn Kruisselbrink
2016/11/29 21:09:53
So after this change the PaymentRequest constructo
| |
| 546 return; | |
| 547 } | |
| 548 | |
| 549 if (RuntimeEnabledFeatures::paymentRequestBasicCardEnabled() && | |
| 550 paymentMethodData.supportedMethods().contains(basicCardMethodName)) { | |
| 551 setBasicCardMethodData(paymentMethodData.data(), output[i], | |
| 552 exceptionState); | |
| 553 if (exceptionState.hadException()) | |
| 554 return; | |
| 555 } | |
| 479 } | 556 } |
| 480 } | 557 } |
| 481 | 558 |
| 482 String getSelectedShippingOption(const PaymentDetails& details) { | 559 String getSelectedShippingOption(const PaymentDetails& details) { |
| 483 String result; | 560 String result; |
| 484 if (!details.hasShippingOptions()) | 561 if (!details.hasShippingOptions()) |
| 485 return result; | 562 return result; |
| 486 | 563 |
| 487 for (int i = details.shippingOptions().size() - 1; i >= 0; --i) { | 564 for (int i = details.shippingOptions().size() - 1; i >= 0; --i) { |
| 488 if (details.shippingOptions()[i].hasSelected() && | 565 if (details.shippingOptions()[i].hasSelected() && |
| (...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 972 m_completeResolver.clear(); | 1049 m_completeResolver.clear(); |
| 973 m_showResolver.clear(); | 1050 m_showResolver.clear(); |
| 974 m_abortResolver.clear(); | 1051 m_abortResolver.clear(); |
| 975 m_canMakeActivePaymentResolver.clear(); | 1052 m_canMakeActivePaymentResolver.clear(); |
| 976 if (m_clientBinding.is_bound()) | 1053 if (m_clientBinding.is_bound()) |
| 977 m_clientBinding.Close(); | 1054 m_clientBinding.Close(); |
| 978 m_paymentProvider.reset(); | 1055 m_paymentProvider.reset(); |
| 979 } | 1056 } |
| 980 | 1057 |
| 981 } // namespace blink | 1058 } // namespace blink |
| OLD | NEW |