OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/payments/PaymentAppRequestDataConversion.h" |
| 6 |
| 7 #include "bindings/core/v8/ScriptState.h" |
| 8 #include "bindings/core/v8/ToV8.h" |
| 9 #include "modules/payments/PaymentAppRequestData.h" |
| 10 #include "modules/payments/PaymentCurrencyAmount.h" |
| 11 #include "modules/payments/PaymentDetailsModifier.h" |
| 12 #include "modules/payments/PaymentItem.h" |
| 13 #include "modules/payments/PaymentMethodData.h" |
| 14 #include "public/platform/modules/payments/WebPaymentAppRequestData.h" |
| 15 #include "public/platform/modules/payments/WebPaymentMethodData.h" |
| 16 |
| 17 namespace blink { |
| 18 namespace { |
| 19 |
| 20 PaymentCurrencyAmount toPaymentCurrencyAmount( |
| 21 const WebPaymentCurrencyAmount& webAmount) { |
| 22 PaymentCurrencyAmount amount; |
| 23 amount.setCurrency(webAmount.currency); |
| 24 amount.setValue(webAmount.value); |
| 25 amount.setCurrencySystem(webAmount.currencySystem); |
| 26 return amount; |
| 27 } |
| 28 |
| 29 PaymentItem toPaymentItem(const WebPaymentItem& webItem) { |
| 30 PaymentItem item; |
| 31 item.setLabel(webItem.label); |
| 32 item.setAmount(toPaymentCurrencyAmount(webItem.amount)); |
| 33 item.setPending(webItem.pending); |
| 34 return item; |
| 35 } |
| 36 |
| 37 PaymentDetailsModifier toPaymentDetailsModifier( |
| 38 ScriptState* scriptState, |
| 39 const WebPaymentDetailsModifier& webModifier) { |
| 40 PaymentDetailsModifier modifier; |
| 41 Vector<String> supportedMethods; |
| 42 for (const auto& webMethod : webModifier.supportedMethods) { |
| 43 supportedMethods.append(webMethod); |
| 44 } |
| 45 modifier.setSupportedMethods(supportedMethods); |
| 46 modifier.setTotal(toPaymentItem(webModifier.total)); |
| 47 HeapVector<PaymentItem> additionalDisplayItems; |
| 48 for (const auto& webItem : webModifier.additionalDisplayItems) { |
| 49 additionalDisplayItems.append(toPaymentItem(webItem)); |
| 50 } |
| 51 modifier.setAdditionalDisplayItems(additionalDisplayItems); |
| 52 return modifier; |
| 53 } |
| 54 |
| 55 ScriptValue stringDataToScriptValue(ScriptState* scriptState, |
| 56 const WebString& stringifiedData) { |
| 57 if (!scriptState->contextIsValid()) |
| 58 return ScriptValue(); |
| 59 |
| 60 ScriptState::Scope scope(scriptState); |
| 61 v8::Local<v8::Value> v8Value; |
| 62 if (!v8::JSON::Parse(scriptState->isolate(), |
| 63 v8String(scriptState->isolate(), stringifiedData)) |
| 64 .ToLocal(&v8Value)) { |
| 65 return ScriptValue(); |
| 66 } |
| 67 return ScriptValue(scriptState, v8Value); |
| 68 } |
| 69 |
| 70 PaymentMethodData toPaymentMethodData( |
| 71 ScriptState* scriptState, |
| 72 const WebPaymentMethodData& webMethodData) { |
| 73 PaymentMethodData methodData; |
| 74 Vector<String> supportedMethods; |
| 75 for (const auto& method : webMethodData.supportedMethods) { |
| 76 supportedMethods.append(method); |
| 77 } |
| 78 methodData.setSupportedMethods(supportedMethods); |
| 79 methodData.setData( |
| 80 stringDataToScriptValue(scriptState, webMethodData.stringifiedData)); |
| 81 return methodData; |
| 82 } |
| 83 |
| 84 } // namespace |
| 85 |
| 86 PaymentAppRequestData PaymentAppRequestDataConversion::toPaymentAppRequestData( |
| 87 ScriptState* scriptState, |
| 88 const WebPaymentAppRequestData& webData) { |
| 89 PaymentAppRequestData data; |
| 90 |
| 91 data.setOrigin(webData.origin); |
| 92 HeapVector<PaymentMethodData> methodData; |
| 93 for (const auto& md : webData.methodData) { |
| 94 methodData.append(toPaymentMethodData(scriptState, md)); |
| 95 } |
| 96 data.setMethodData(methodData); |
| 97 data.setTotal(toPaymentItem(webData.total)); |
| 98 HeapVector<PaymentDetailsModifier> modifiers; |
| 99 for (const auto& modifier : webData.modifiers) { |
| 100 modifiers.append(toPaymentDetailsModifier(scriptState, modifier)); |
| 101 } |
| 102 data.setOptionId(webData.optionId); |
| 103 return data; |
| 104 } |
| 105 |
| 106 } // namespace blink |
OLD | NEW |