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/JSONValuesForV8.h" | 8 #include "bindings/core/v8/JSONValuesForV8.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 m_paymentProvider->Abort(); | 194 m_paymentProvider->Abort(); |
| 195 } | 195 } |
| 196 | 196 |
| 197 const AtomicString& PaymentRequest::interfaceName() const | 197 const AtomicString& PaymentRequest::interfaceName() const |
| 198 { | 198 { |
| 199 return EventTargetNames::PaymentRequest; | 199 return EventTargetNames::PaymentRequest; |
| 200 } | 200 } |
| 201 | 201 |
| 202 ExecutionContext* PaymentRequest::getExecutionContext() const | 202 ExecutionContext* PaymentRequest::getExecutionContext() const |
| 203 { | 203 { |
| 204 return m_scriptState->getExecutionContext(); | 204 return ContextLifecycleObserver::getExecutionContext(); |
| 205 } | |
| 206 | |
| 207 ScriptPromise PaymentRequest::complete(ScriptState* scriptState, bool success) | |
| 208 { | |
| 209 if (m_completeResolver) | |
| 210 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(InvalidStateError, "Already called complete() once")); | |
| 211 | |
| 212 m_completeResolver = ScriptPromiseResolver::create(scriptState); | |
| 213 m_paymentProvider->Complete(success); | |
| 214 | |
| 215 return m_completeResolver->promise(); | |
| 216 } | 205 } |
| 217 | 206 |
| 218 DEFINE_TRACE(PaymentRequest) | 207 DEFINE_TRACE(PaymentRequest) |
| 219 { | 208 { |
| 220 visitor->trace(m_details); | 209 visitor->trace(m_details); |
| 221 visitor->trace(m_options); | 210 visitor->trace(m_options); |
| 222 visitor->trace(m_shippingAddress); | 211 visitor->trace(m_shippingAddress); |
| 223 visitor->trace(m_showResolver); | 212 visitor->trace(m_showResolver); |
| 224 visitor->trace(m_completeResolver); | 213 visitor->trace(m_completeResolver); |
| 225 EventTargetWithInlineData::trace(visitor); | 214 EventTargetWithInlineData::trace(visitor); |
| 215 ContextLifecycleObserver::trace(visitor); | |
| 226 } | 216 } |
| 227 | 217 |
| 228 PaymentRequest::PaymentRequest(ScriptState* scriptState, const Vector<String>& s upportedMethods, const PaymentDetails& details, const PaymentOptions& options, c onst ScriptValue& data, ExceptionState& exceptionState) | 218 PaymentRequest::PaymentRequest(ScriptState* scriptState, const Vector<String>& s upportedMethods, const PaymentDetails& details, const PaymentOptions& options, c onst ScriptValue& data, ExceptionState& exceptionState) |
| 229 : m_scriptState(scriptState) | 219 : ContextLifecycleObserver(scriptState->getExecutionContext()) |
| 230 , m_supportedMethods(supportedMethods) | 220 , m_supportedMethods(supportedMethods) |
| 231 , m_details(details) | 221 , m_details(details) |
| 232 , m_options(options) | 222 , m_options(options) |
| 233 , m_clientBinding(this) | 223 , m_clientBinding(this) |
| 234 { | 224 { |
| 235 // TODO(rouslan): Also check for a top-level browsing context. | 225 // TODO(rouslan): Also check for a top-level browsing context. |
| 236 // https://github.com/w3c/browser-payment-api/issues/2 | 226 // https://github.com/w3c/browser-payment-api/issues/2 |
| 237 if (!scriptState->getExecutionContext()->isSecureContext()) { | 227 if (!scriptState->getExecutionContext()->isSecureContext()) { |
| 238 exceptionState.throwSecurityError("Must be in a secure context"); | 228 exceptionState.throwSecurityError("Must be in a secure context"); |
| 239 return; | 229 return; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 | 280 |
| 291 m_stringifiedData = jsonData->toJSONString(); | 281 m_stringifiedData = jsonData->toJSONString(); |
| 292 } | 282 } |
| 293 } | 283 } |
| 294 | 284 |
| 295 // Set the currently selected option if only one option was passed. | 285 // Set the currently selected option if only one option was passed. |
| 296 if (details.hasShippingOptions() && details.shippingOptions().size() == 1) | 286 if (details.hasShippingOptions() && details.shippingOptions().size() == 1) |
| 297 m_shippingOption = details.shippingOptions().begin()->id(); | 287 m_shippingOption = details.shippingOptions().begin()->id(); |
| 298 } | 288 } |
| 299 | 289 |
| 290 ScriptPromise PaymentRequest::complete(ScriptState* scriptState, bool success) | |
|
please use gerrit instead
2016/04/13 22:56:46
Let's keep the order of methods in PaymentRequest.
rwlbuis
2016/04/14 00:28:33
Done.
| |
| 291 { | |
| 292 if (m_completeResolver) | |
| 293 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(InvalidStateError, "Already called complete() once")); | |
| 294 | |
| 295 m_completeResolver = ScriptPromiseResolver::create(scriptState); | |
| 296 m_paymentProvider->Complete(success); | |
| 297 | |
| 298 return m_completeResolver->promise(); | |
| 299 } | |
| 300 | |
| 301 void PaymentRequest::contextDestroyed() | |
| 302 { | |
| 303 cleanUp(); | |
| 304 } | |
| 305 | |
| 300 void PaymentRequest::OnShippingAddressChange(mojom::wtf::ShippingAddressPtr addr ess) | 306 void PaymentRequest::OnShippingAddressChange(mojom::wtf::ShippingAddressPtr addr ess) |
| 301 { | 307 { |
| 302 DCHECK(m_showResolver); | 308 DCHECK(m_showResolver); |
| 303 DCHECK(!m_completeResolver); | 309 DCHECK(!m_completeResolver); |
| 304 | 310 |
| 305 String errorMessage; | 311 String errorMessage; |
| 306 if (!PaymentsValidators::isValidRegionCodeFormat(address->region_code, &erro rMessage) | 312 if (!PaymentsValidators::isValidRegionCodeFormat(address->region_code, &erro rMessage) |
| 307 || !PaymentsValidators::isValidLanguageCodeFormat(address->language_code , &errorMessage) | 313 || !PaymentsValidators::isValidLanguageCodeFormat(address->language_code , &errorMessage) |
| 308 || !PaymentsValidators::isValidScriptCodeFormat(address->script_code, &e rrorMessage)) { | 314 || !PaymentsValidators::isValidScriptCodeFormat(address->script_code, &e rrorMessage)) { |
| 309 m_showResolver->reject(DOMException::create(SyntaxError, errorMessage)); | 315 m_showResolver->reject(DOMException::create(SyntaxError, errorMessage)); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 { | 359 { |
| 354 DCHECK(m_completeResolver); | 360 DCHECK(m_completeResolver); |
| 355 m_completeResolver->resolve(); | 361 m_completeResolver->resolve(); |
| 356 cleanUp(); | 362 cleanUp(); |
| 357 } | 363 } |
| 358 | 364 |
| 359 void PaymentRequest::cleanUp() | 365 void PaymentRequest::cleanUp() |
| 360 { | 366 { |
| 361 m_completeResolver.clear(); | 367 m_completeResolver.clear(); |
| 362 m_showResolver.clear(); | 368 m_showResolver.clear(); |
| 363 m_clientBinding.Close(); | 369 m_clientBinding.Close(); |
|
please use gerrit instead
2016/04/13 22:56:46
I recommend checking Binding::is_bound() in here i
rwlbuis
2016/04/14 00:28:33
Done.
| |
| 364 m_paymentProvider.reset(); | 370 m_paymentProvider.reset(); |
| 365 } | 371 } |
| 366 | 372 |
| 367 } // namespace blink | 373 } // namespace blink |
| OLD | NEW |