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" |
| 19 #include "modules/payments/PaymentsValidators.h" |
13 #include "modules/payments/ShippingAddress.h" | 20 #include "modules/payments/ShippingAddress.h" |
| 21 #include "modules/payments/ShippingOption.h" |
| 22 #include "mojo/public/cpp/bindings/interface_request.h" |
| 23 #include "mojo/public/cpp/bindings/wtf_array.h" |
| 24 #include "platform/threading/BindForMojo.h" |
| 25 #include "public/platform/Platform.h" |
| 26 #include <utility> |
| 27 |
| 28 namespace mojo { |
| 29 |
| 30 template <> |
| 31 struct TypeConverter<mojom::wtf::CurrencyAmountPtr, blink::CurrencyAmount> { |
| 32 static mojom::wtf::CurrencyAmountPtr Convert(const blink::CurrencyAmount& in
put) |
| 33 { |
| 34 mojom::wtf::CurrencyAmountPtr output = mojom::wtf::CurrencyAmount::New()
; |
| 35 output->currency_code = input.currencyCode(); |
| 36 output->value = input.value(); |
| 37 return output; |
| 38 } |
| 39 }; |
| 40 |
| 41 template <> |
| 42 struct TypeConverter<mojom::wtf::PaymentItemPtr, blink::PaymentItem> { |
| 43 static mojom::wtf::PaymentItemPtr Convert(const blink::PaymentItem& input) |
| 44 { |
| 45 mojom::wtf::PaymentItemPtr output = mojom::wtf::PaymentItem::New(); |
| 46 output->id = input.id(); |
| 47 output->label = input.label(); |
| 48 output->amount = mojom::wtf::CurrencyAmount::From(input.amount()); |
| 49 return output; |
| 50 } |
| 51 }; |
| 52 |
| 53 template <> |
| 54 struct TypeConverter<mojom::wtf::ShippingOptionPtr, blink::ShippingOption> { |
| 55 static mojom::wtf::ShippingOptionPtr Convert(const blink::ShippingOption& in
put) |
| 56 { |
| 57 mojom::wtf::ShippingOptionPtr output = mojom::wtf::ShippingOption::New()
; |
| 58 output->id = input.id(); |
| 59 output->label = input.label(); |
| 60 output->amount = mojom::wtf::CurrencyAmount::From(input.amount()); |
| 61 return output; |
| 62 } |
| 63 }; |
| 64 |
| 65 template <> |
| 66 struct TypeConverter<mojom::wtf::PaymentDetailsPtr, blink::PaymentDetails> { |
| 67 static mojom::wtf::PaymentDetailsPtr Convert(const blink::PaymentDetails& in
put) |
| 68 { |
| 69 mojom::wtf::PaymentDetailsPtr output = mojom::wtf::PaymentDetails::New()
; |
| 70 output->items = mojo::WTFArray<mojom::wtf::PaymentItemPtr>::From(static_
cast<const WTF::Vector<blink::PaymentItem, 0, blink::HeapAllocator>>(input.items
())); |
| 71 if (input.hasShippingOptions()) |
| 72 output->shipping_options = mojo::WTFArray<mojom::wtf::ShippingOption
Ptr>::From(static_cast<const WTF::Vector<blink::ShippingOption, 0, blink::HeapAl
locator>>(input.shippingOptions())); |
| 73 else |
| 74 output->shipping_options = mojo::WTFArray<mojom::wtf::ShippingOption
Ptr>::New(0); |
| 75 return output; |
| 76 } |
| 77 }; |
| 78 |
| 79 template <> |
| 80 struct TypeConverter<mojom::wtf::PaymentOptionsPtr, blink::PaymentOptions> { |
| 81 static mojom::wtf::PaymentOptionsPtr Convert(const blink::PaymentOptions& in
put) |
| 82 { |
| 83 mojom::wtf::PaymentOptionsPtr output = mojom::wtf::PaymentOptions::New()
; |
| 84 output->request_shipping = input.requestShipping(); |
| 85 return output; |
| 86 } |
| 87 }; |
| 88 |
| 89 } // namespace mojo |
14 | 90 |
15 namespace blink { | 91 namespace blink { |
| 92 namespace { |
| 93 |
| 94 // Validates ShippingOption and PaymentItem dictionaries, which happen to have i
dentical fields. |
| 95 template <typename T> |
| 96 void validateShippingOptionsOrPaymentItems(HeapVector<T> items, ExceptionState&
exceptionState) |
| 97 { |
| 98 String errorMessage; |
| 99 for (const auto& item : items) { |
| 100 if (!item.hasId()) { |
| 101 exceptionState.throwTypeError("Item id required"); |
| 102 return; |
| 103 } |
| 104 |
| 105 if (!item.hasLabel()) { |
| 106 exceptionState.throwTypeError("Item label required"); |
| 107 return; |
| 108 } |
| 109 |
| 110 if (!item.hasAmount()) { |
| 111 exceptionState.throwTypeError("Currency amount required"); |
| 112 return; |
| 113 } |
| 114 |
| 115 if (!item.amount().hasCurrencyCode()) { |
| 116 exceptionState.throwTypeError("Currency code required"); |
| 117 return; |
| 118 } |
| 119 |
| 120 if (!item.amount().hasValue()) { |
| 121 exceptionState.throwTypeError("Currency value required"); |
| 122 return; |
| 123 } |
| 124 |
| 125 if (!isValidCurrencyCodeFormat(item.amount().currencyCode(), &errorMessa
ge)) { |
| 126 exceptionState.throwTypeError(errorMessage); |
| 127 return; |
| 128 } |
| 129 |
| 130 if (!isValidAmountFormat(item.amount().value(), &errorMessage)) { |
| 131 exceptionState.throwTypeError(errorMessage); |
| 132 return; |
| 133 } |
| 134 } |
| 135 } |
| 136 |
| 137 } // namespace |
16 | 138 |
17 // static | 139 // static |
18 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St
ring>& supportedMethods, const PaymentDetails& details, ExceptionState& exceptio
nState) | 140 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St
ring>& supportedMethods, const PaymentDetails& details, ExceptionState& exceptio
nState) |
19 { | 141 { |
20 return new PaymentRequest(scriptState, supportedMethods, details, PaymentOpt
ions(), ScriptValue(), exceptionState); | 142 return new PaymentRequest(scriptState, supportedMethods, details, PaymentOpt
ions(), ScriptValue(), exceptionState); |
21 } | 143 } |
22 | 144 |
23 // static | 145 // static |
24 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St
ring>& supportedMethods, const PaymentDetails& details, const PaymentOptions& op
tions, ExceptionState& exceptionState) | 146 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St
ring>& supportedMethods, const PaymentDetails& details, const PaymentOptions& op
tions, ExceptionState& exceptionState) |
25 { | 147 { |
26 return new PaymentRequest(scriptState, supportedMethods, details, options, S
criptValue(), exceptionState); | 148 return new PaymentRequest(scriptState, supportedMethods, details, options, S
criptValue(), exceptionState); |
27 } | 149 } |
28 | 150 |
29 // static | 151 // static |
30 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St
ring>& supportedMethods, const PaymentDetails& details, const PaymentOptions& op
tions, const ScriptValue& data, ExceptionState& exceptionState) | 152 PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<St
ring>& supportedMethods, const PaymentDetails& details, const PaymentOptions& op
tions, const ScriptValue& data, ExceptionState& exceptionState) |
31 { | 153 { |
32 return new PaymentRequest(scriptState, supportedMethods, details, options, d
ata, exceptionState); | 154 return new PaymentRequest(scriptState, supportedMethods, details, options, d
ata, exceptionState); |
33 } | 155 } |
34 | 156 |
35 PaymentRequest::~PaymentRequest() | 157 PaymentRequest::~PaymentRequest() |
36 { | 158 { |
37 } | 159 } |
38 | 160 |
39 ScriptPromise PaymentRequest::show(ScriptState* scriptState) | 161 ScriptPromise PaymentRequest::show(ScriptState* scriptState) |
40 { | 162 { |
41 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea
te(NotSupportedError, "Not implemented.")); | 163 if (m_showResolver) |
| 164 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "Already called show() once")); |
| 165 |
| 166 DCHECK(!m_paymentProvider.is_bound()); |
| 167 blink::Platform::current()->connectToRemoteService(mojo::GetProxy(&m_payment
Provider)); |
| 168 m_paymentProvider.set_connection_error_handler(sameThreadBindForMojo(&Paymen
tRequest::OnError, this)); |
| 169 m_paymentProvider->SetClient(m_clientBinding.CreateInterfacePtrAndBind()); |
| 170 m_paymentProvider->Show(std::move(m_supportedMethods), mojom::wtf::PaymentDe
tails::From(m_details), mojom::wtf::PaymentOptions::From(m_options), m_stringifi
edData.isNull() ? "" : m_stringifiedData); |
| 171 |
| 172 m_showResolver = ScriptPromiseResolver::create(scriptState); |
| 173 return m_showResolver->promise(); |
42 } | 174 } |
43 | 175 |
44 void PaymentRequest::abort() | 176 void PaymentRequest::abort(ExceptionState& exceptionState) |
45 { | 177 { |
| 178 if (!m_showResolver) { |
| 179 exceptionState.throwDOMException(InvalidStateError, "Never called show()
, so nothing to abort"); |
| 180 return; |
| 181 } |
| 182 |
| 183 m_paymentProvider->Abort(); |
46 } | 184 } |
47 | 185 |
48 const AtomicString& PaymentRequest::interfaceName() const | 186 const AtomicString& PaymentRequest::interfaceName() const |
49 { | 187 { |
50 return EventTargetNames::PaymentRequest; | 188 return EventTargetNames::PaymentRequest; |
51 } | 189 } |
52 | 190 |
53 ExecutionContext* PaymentRequest::getExecutionContext() const | 191 ExecutionContext* PaymentRequest::getExecutionContext() const |
54 { | 192 { |
55 return m_scriptState->getExecutionContext(); | 193 return m_scriptState->getExecutionContext(); |
56 } | 194 } |
57 | 195 |
| 196 ScriptPromise PaymentRequest::complete(ScriptState* scriptState, bool success) |
| 197 { |
| 198 if (m_completeResolver) |
| 199 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(InvalidStateError, "Already called complete() once")); |
| 200 |
| 201 m_completeResolver = ScriptPromiseResolver::create(scriptState); |
| 202 m_paymentProvider->Complete(success); |
| 203 |
| 204 return m_completeResolver->promise(); |
| 205 } |
| 206 |
58 DEFINE_TRACE(PaymentRequest) | 207 DEFINE_TRACE(PaymentRequest) |
59 { | 208 { |
60 visitor->trace(m_details); | 209 visitor->trace(m_details); |
61 visitor->trace(m_options); | 210 visitor->trace(m_options); |
62 visitor->trace(m_shippingAddress); | 211 visitor->trace(m_shippingAddress); |
| 212 visitor->trace(m_showResolver); |
| 213 visitor->trace(m_completeResolver); |
63 RefCountedGarbageCollectedEventTargetWithInlineData<PaymentRequest>::trace(v
isitor); | 214 RefCountedGarbageCollectedEventTargetWithInlineData<PaymentRequest>::trace(v
isitor); |
64 } | 215 } |
65 | 216 |
66 PaymentRequest::PaymentRequest(ScriptState* scriptState, const Vector<String>& s
upportedMethods, const PaymentDetails& details, const PaymentOptions& options, c
onst ScriptValue& data, ExceptionState& exceptionState) | 217 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) | 218 : m_scriptState(scriptState) |
68 , m_supportedMethods(supportedMethods) | 219 , m_supportedMethods(supportedMethods) |
69 , m_details(details) | 220 , m_details(details) |
70 , m_options(options) | 221 , m_options(options) |
| 222 , m_clientBinding(this) |
71 { | 223 { |
| 224 // TODO(rouslan): Also check for a top-level browsing context. |
| 225 // https://github.com/w3c/browser-payment-api/issues/2 |
| 226 if (!scriptState->getExecutionContext()->isSecureContext()) { |
| 227 exceptionState.throwSecurityError("Must be in a secure context"); |
| 228 return; |
| 229 } |
| 230 |
| 231 if (supportedMethods.isEmpty()) { |
| 232 exceptionState.throwTypeError("Must specify at least one payment method
identifier"); |
| 233 return; |
| 234 } |
| 235 |
| 236 if (!details.hasItems()) { |
| 237 exceptionState.throwTypeError("Must specify items"); |
| 238 return; |
| 239 } |
| 240 |
| 241 if (details.items().isEmpty()) { |
| 242 exceptionState.throwTypeError("Must specify at least one item"); |
| 243 return; |
| 244 } |
| 245 |
| 246 validateShippingOptionsOrPaymentItems(details.items(), exceptionState); |
| 247 if (exceptionState.hadException()) |
| 248 return; |
| 249 |
| 250 if (details.hasShippingOptions()) { |
| 251 validateShippingOptionsOrPaymentItems(details.shippingOptions(), excepti
onState); |
| 252 if (exceptionState.hadException()) |
| 253 return; |
| 254 } |
| 255 |
72 if (!data.isEmpty()) { | 256 if (!data.isEmpty()) { |
73 RefPtr<JSONValue> value = toJSONValue(data.context(), data.v8Value()); | 257 RefPtr<JSONValue> value = toJSONValue(data.context(), data.v8Value()); |
74 if (value && value->getType() == JSONValue::TypeObject) | 258 if (!value) { |
75 m_stringifiedData = JSONObject::cast(value)->toJSONString(); | 259 exceptionState.throwTypeError("Unable to parse payment method specif
ic data"); |
| 260 return; |
| 261 } |
| 262 if (!value->isNull()) { |
| 263 if (value->getType() != JSONValue::TypeObject) { |
| 264 exceptionState.throwTypeError("Payment method specific data shou
ld be a JSON-serializable object"); |
| 265 return; |
| 266 } |
| 267 |
| 268 RefPtr<JSONObject> jsonData = JSONObject::cast(value); |
| 269 for (const auto& paymentMethodSpecificKeyValue : *jsonData) { |
| 270 if (!supportedMethods.contains(paymentMethodSpecificKeyValue.key
)) { |
| 271 exceptionState.throwTypeError("'" + paymentMethodSpecificKey
Value.key + "' should match one of the payment method identifiers"); |
| 272 return; |
| 273 } |
| 274 if (paymentMethodSpecificKeyValue.value->getType() != JSONValue:
:TypeObject) { |
| 275 exceptionState.throwTypeError("Data for '" + paymentMethodSp
ecificKeyValue.key + "' should be a JSON-serializable object"); |
| 276 return; |
| 277 } |
| 278 } |
| 279 |
| 280 m_stringifiedData = jsonData->toJSONString(); |
| 281 } |
76 } | 282 } |
| 283 |
| 284 // Set the currently selected option if only one option was passed. |
| 285 if (details.hasShippingOptions() && details.shippingOptions().size() == 1) |
| 286 m_shippingOption = details.shippingOptions().begin()->id(); |
| 287 } |
| 288 |
| 289 void PaymentRequest::OnShippingAddressChange(mojom::wtf::ShippingAddressPtr addr
ess) |
| 290 { |
| 291 DCHECK(m_showResolver); |
| 292 DCHECK(!m_completeResolver); |
| 293 |
| 294 // TODO(rouslan): Should the merchant website be notified of invalid shippin
g address |
| 295 // from the browser or the payment app? |
| 296 String errorMessage; |
| 297 if (!isValidRegionCodeFormat(address->region_code, &errorMessage) |
| 298 || !isValidLanguageCodeFormat(address->language_code, &errorMessage) |
| 299 || !isValidScriptCodeFormat(address->script_code, &errorMessage)) { |
| 300 m_showResolver->reject(DOMException::create(SyntaxError, errorMessage)); |
| 301 return; |
| 302 } |
| 303 |
| 304 if (address->language_code.isEmpty() && !address->script_code.isEmpty()) { |
| 305 m_showResolver->reject(DOMException::create(SyntaxError, "If language co
de is empty, then script code should also be empty")); |
| 306 return; |
| 307 } |
| 308 |
| 309 m_shippingAddress = new ShippingAddress(std::move(address)); |
| 310 RefPtrWillBeRawPtr<Event> event = Event::create(EventTypeNames::shippingaddr
esschange); |
| 311 event->setTarget(this); |
| 312 getExecutionContext()->getEventQueue()->enqueueEvent(event); |
| 313 } |
| 314 |
| 315 void PaymentRequest::OnShippingOptionChange(const String& shippingOptionId) |
| 316 { |
| 317 DCHECK(m_showResolver); |
| 318 DCHECK(!m_completeResolver); |
| 319 m_shippingOption = shippingOptionId; |
| 320 RefPtrWillBeRawPtr<Event> event = Event::create(EventTypeNames::shippingopti
onchange); |
| 321 event->setTarget(this); |
| 322 getExecutionContext()->getEventQueue()->enqueueEvent(event); |
| 323 } |
| 324 |
| 325 void PaymentRequest::OnPaymentResponse(mojom::wtf::PaymentResponsePtr response) |
| 326 { |
| 327 DCHECK(m_showResolver); |
| 328 DCHECK(!m_completeResolver); |
| 329 m_showResolver->resolve(new PaymentResponse(std::move(response), this)); |
| 330 } |
| 331 |
| 332 void PaymentRequest::OnError() |
| 333 { |
| 334 if (m_completeResolver) |
| 335 m_completeResolver->reject(DOMException::create(SyntaxError, "Request ca
ncelled")); |
| 336 else if (m_showResolver) |
| 337 m_showResolver->reject(DOMException::create(SyntaxError, "Request cancel
led")); |
| 338 m_clientBinding.Close(); |
| 339 m_paymentProvider.reset(); |
| 340 } |
| 341 |
| 342 void PaymentRequest::OnComplete() |
| 343 { |
| 344 DCHECK(m_completeResolver); |
| 345 m_completeResolver->resolve(); |
| 346 m_clientBinding.Close(); |
| 347 m_paymentProvider.reset(); |
77 } | 348 } |
78 | 349 |
79 } // namespace blink | 350 } // namespace blink |
OLD | NEW |