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/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
| 11 #include "core/EventTypeNames.h" |
10 #include "core/dom/DOMException.h" | 12 #include "core/dom/DOMException.h" |
11 #include "core/dom/ExceptionCode.h" | 13 #include "core/dom/ExceptionCode.h" |
| 14 #include "core/events/Event.h" |
| 15 #include "core/events/EventQueue.h" |
12 #include "modules/EventTargetModulesNames.h" | 16 #include "modules/EventTargetModulesNames.h" |
| 17 #include "modules/payments/PaymentItem.h" |
| 18 #include "modules/payments/PaymentResponse.h" |
13 #include "modules/payments/ShippingAddress.h" | 19 #include "modules/payments/ShippingAddress.h" |
| 20 #include "modules/payments/ShippingOption.h" |
| 21 #include "platform/payments/PlatformPaymentDetails.h" |
| 22 #include "platform/payments/PlatformPaymentItem.h" |
| 23 #include "platform/payments/PlatformPaymentOptions.h" |
| 24 #include "platform/payments/PlatformShippingOption.h" |
14 | 25 |
15 namespace blink { | 26 namespace blink { |
| 27 namespace { |
| 28 |
| 29 template <typename Output, typename Input> |
| 30 Output toPlatformItem(const Input& input) |
| 31 { |
| 32 Output output; |
| 33 output.id = input.id(); |
| 34 output.label = input.label(); |
| 35 if (input.hasAmount()) { |
| 36 output.amount.currencyCode = input.amount().currencyCode(); |
| 37 output.amount.value = input.amount().value(); |
| 38 } |
| 39 return output; |
| 40 } |
| 41 |
| 42 PlatformPaymentDetails toPlatformPaymentDetails(const PaymentDetails& input) |
| 43 { |
| 44 PlatformPaymentDetails output; |
| 45 if (input.hasItems()) { |
| 46 output.items.resize(input.items().size()); |
| 47 for (size_t i = 0; i < input.items().size(); ++i) |
| 48 output.items[i] = toPlatformItem<PlatformPaymentItem, PaymentItem>(i
nput.items()[i]); |
| 49 } |
| 50 if (input.hasShippingOptions()) { |
| 51 output.shippingOptions.resize(input.shippingOptions().size()); |
| 52 for (size_t i = 0; i < input.shippingOptions().size(); ++i) |
| 53 output.shippingOptions[i] = toPlatformItem<PlatformShippingOption, S
hippingOption>(input.shippingOptions()[i]); |
| 54 } |
| 55 return output; |
| 56 } |
| 57 |
| 58 PlatformPaymentOptions toPlatformPaymentOptions(const PaymentOptions& input) |
| 59 { |
| 60 PlatformPaymentOptions output; |
| 61 output.requestShipping = input.requestShipping(); |
| 62 return output; |
| 63 } |
| 64 |
| 65 } // namespace |
16 | 66 |
17 // static | 67 // static |
18 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St
ring>& supportedMethods, const PaymentDetails& details, ExceptionState& exceptio
nState) | 68 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St
ring>& supportedMethods, const PaymentDetails& details, ExceptionState& exceptio
nState) |
19 { | 69 { |
20 return new PaymentRequest(scriptState, supportedMethods, details, PaymentOpt
ions(), ScriptValue(), exceptionState); | 70 return new PaymentRequest(scriptState, supportedMethods, details, PaymentOpt
ions(), ScriptValue(), exceptionState); |
21 } | 71 } |
22 | 72 |
23 // static | 73 // static |
24 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St
ring>& supportedMethods, const PaymentDetails& details, const PaymentOptions& op
tions, ExceptionState& exceptionState) | 74 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St
ring>& supportedMethods, const PaymentDetails& details, const PaymentOptions& op
tions, ExceptionState& exceptionState) |
25 { | 75 { |
26 return new PaymentRequest(scriptState, supportedMethods, details, options, S
criptValue(), exceptionState); | 76 return new PaymentRequest(scriptState, supportedMethods, details, options, S
criptValue(), exceptionState); |
27 } | 77 } |
28 | 78 |
29 // static | 79 // static |
30 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St
ring>& supportedMethods, const PaymentDetails& details, const PaymentOptions& op
tions, const ScriptValue& data, ExceptionState& exceptionState) | 80 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St
ring>& supportedMethods, const PaymentDetails& details, const PaymentOptions& op
tions, const ScriptValue& data, ExceptionState& exceptionState) |
31 { | 81 { |
32 return new PaymentRequest(scriptState, supportedMethods, details, options, d
ata, exceptionState); | 82 return new PaymentRequest(scriptState, supportedMethods, details, options, d
ata, exceptionState); |
33 } | 83 } |
34 | 84 |
35 PaymentRequest::~PaymentRequest() | 85 PaymentRequest::~PaymentRequest() |
36 { | 86 { |
37 } | 87 } |
38 | 88 |
39 ScriptPromise PaymentRequest::show(ScriptState* scriptState) | 89 ScriptPromise PaymentRequest::show(ScriptState* scriptState) |
40 { | 90 { |
41 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(NotSupportedError, "Not implemented.")); | 91 if (m_showResolver) |
| 92 return m_showResolver->promise().rejectWithDOMException(scriptState, DOM
Exception::create(SyntaxError, "Already called show() once.")); |
| 93 |
| 94 m_showResolver = ScriptPromiseResolver::create(scriptState); |
| 95 m_paymentRequestProxy.show(m_supportedMethods, toPlatformPaymentDetails(m_de
tails), toPlatformPaymentOptions(m_options), m_stringifiedData); |
| 96 |
| 97 return m_showResolver->promise(); |
42 } | 98 } |
43 | 99 |
44 void PaymentRequest::abort() | 100 void PaymentRequest::abort(ExceptionState& exceptionState) |
45 { | 101 { |
| 102 if (!m_showResolver) { |
| 103 exceptionState.throwDOMException(SyntaxError, "Never called show(), so n
othing to abort."); |
| 104 return; |
| 105 } |
| 106 |
| 107 m_paymentRequestProxy.abort(); |
46 } | 108 } |
47 | 109 |
48 const AtomicString& PaymentRequest::interfaceName() const | 110 const AtomicString& PaymentRequest::interfaceName() const |
49 { | 111 { |
50 return EventTargetNames::PaymentRequest; | 112 return EventTargetNames::PaymentRequest; |
51 } | 113 } |
52 | 114 |
53 ExecutionContext* PaymentRequest::executionContext() const | 115 ExecutionContext* PaymentRequest::executionContext() const |
54 { | 116 { |
55 return m_scriptState->executionContext(); | 117 return m_scriptState->executionContext(); |
56 } | 118 } |
57 | 119 |
| 120 ScriptPromise PaymentRequest::complete(ScriptState* scriptState, bool success) |
| 121 { |
| 122 if (m_completeResolver) |
| 123 return m_completeResolver->promise().rejectWithDOMException(scriptState,
DOMException::create(SyntaxError, "Already called complete() once.")); |
| 124 |
| 125 m_completeResolver = ScriptPromiseResolver::create(scriptState); |
| 126 m_paymentRequestProxy.complete(success); |
| 127 |
| 128 return m_completeResolver->promise(); |
| 129 } |
| 130 |
58 DEFINE_TRACE(PaymentRequest) | 131 DEFINE_TRACE(PaymentRequest) |
59 { | 132 { |
60 visitor->trace(m_details); | 133 visitor->trace(m_details); |
61 visitor->trace(m_options); | 134 visitor->trace(m_options); |
62 visitor->trace(m_shippingAddress); | 135 visitor->trace(m_shippingAddress); |
| 136 visitor->trace(m_showResolver); |
| 137 visitor->trace(m_completeResolver); |
63 RefCountedGarbageCollectedEventTargetWithInlineData<PaymentRequest>::trace(v
isitor); | 138 RefCountedGarbageCollectedEventTargetWithInlineData<PaymentRequest>::trace(v
isitor); |
64 } | 139 } |
65 | 140 |
66 PaymentRequest::PaymentRequest(ScriptState* scriptState, const Vector<String>& s
upportedMethods, const PaymentDetails& details, const PaymentOptions& options, c
onst ScriptValue& data, ExceptionState& exceptionState) | 141 PaymentRequest::PaymentRequest(ScriptState* scriptState, const Vector<String>& s
upportedMethods, const PaymentDetails& details, const PaymentOptions& options, c
onst ScriptValue& data, ExceptionState& exceptionState) |
67 : m_scriptState(scriptState) | 142 : m_scriptState(scriptState) |
68 , m_supportedMethods(supportedMethods) | 143 , m_supportedMethods(supportedMethods) |
69 , m_details(details) | 144 , m_details(details) |
70 , m_options(options) | 145 , m_options(options) |
| 146 , m_paymentRequestProxy(this) |
71 { | 147 { |
72 if (!data.isEmpty()) { | 148 if (!data.isEmpty()) { |
73 RefPtr<JSONValue> value = toJSONValue(data.context(), data.v8Value()); | 149 RefPtr<JSONValue> value = toJSONValue(data.context(), data.v8Value()); |
74 if (value && value->getType() == JSONValue::TypeObject) | 150 if (value && value->getType() == JSONValue::TypeObject) |
75 m_stringifiedData = JSONObject::cast(value)->toJSONString(); | 151 m_stringifiedData = JSONObject::cast(value)->toJSONString(); |
76 } | 152 } |
77 } | 153 } |
78 | 154 |
| 155 void PaymentRequest::onShippingAddressChange(const PlatformShippingAddress& addr
ess) |
| 156 { |
| 157 ASSERT(m_showResolver); |
| 158 m_shippingAddress = new ShippingAddress(address); |
| 159 RefPtrWillBeRawPtr<Event> event = Event::create(EventTypeNames::shippingaddr
esschange); |
| 160 event->setTarget(this); |
| 161 executionContext()->eventQueue()->enqueueEvent(event); |
| 162 } |
| 163 |
| 164 void PaymentRequest::onShippingOptionChange(const String& shippingOption) |
| 165 { |
| 166 ASSERT(m_showResolver); |
| 167 m_shippingOption = shippingOption; |
| 168 RefPtrWillBeRawPtr<Event> event = Event::create(EventTypeNames::shippingopti
onchange); |
| 169 event->setTarget(this); |
| 170 executionContext()->eventQueue()->enqueueEvent(event); |
| 171 } |
| 172 |
| 173 void PaymentRequest::onPaymentResponse(const PlatformPaymentResponse& response) |
| 174 { |
| 175 ASSERT(m_showResolver); |
| 176 m_showResolver->resolve(new PaymentResponse(response, this)); |
| 177 } |
| 178 |
| 179 void PaymentRequest::onError() |
| 180 { |
| 181 ASSERT(m_showResolver); |
| 182 m_showResolver->reject(DOMException::create(SyntaxError, "Request cancelled.
")); |
| 183 } |
| 184 |
| 185 void PaymentRequest::onComplete() |
| 186 { |
| 187 ASSERT(m_completeResolver); |
| 188 m_completeResolver->resolve(); |
| 189 } |
| 190 |
79 } // namespace blink | 191 } // namespace blink |
OLD | NEW |