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