Index: third_party/WebKit/Source/modules/payments/PaymentRequest.cpp |
diff --git a/third_party/WebKit/Source/modules/payments/PaymentRequest.cpp b/third_party/WebKit/Source/modules/payments/PaymentRequest.cpp |
index d4a1c5ffa348b3314d630c14d50f7e1331f4e948..efc8454691d6ae314bacd4302f32bba14275a58d 100644 |
--- a/third_party/WebKit/Source/modules/payments/PaymentRequest.cpp |
+++ b/third_party/WebKit/Source/modules/payments/PaymentRequest.cpp |
@@ -6,13 +6,63 @@ |
#include "bindings/core/v8/ExceptionState.h" |
#include "bindings/core/v8/JSONValuesForV8.h" |
+#include "bindings/core/v8/ScriptPromiseResolver.h" |
#include "bindings/core/v8/ScriptState.h" |
+#include "core/EventTypeNames.h" |
#include "core/dom/DOMException.h" |
#include "core/dom/ExceptionCode.h" |
+#include "core/events/Event.h" |
+#include "core/events/EventQueue.h" |
#include "modules/EventTargetModulesNames.h" |
+#include "modules/payments/PaymentItem.h" |
+#include "modules/payments/PaymentResponse.h" |
#include "modules/payments/ShippingAddress.h" |
+#include "modules/payments/ShippingOption.h" |
+#include "platform/payments/PlatformPaymentDetails.h" |
+#include "platform/payments/PlatformPaymentItem.h" |
+#include "platform/payments/PlatformPaymentOptions.h" |
+#include "platform/payments/PlatformShippingOption.h" |
namespace blink { |
+namespace { |
+ |
+template <typename Output, typename Input> |
+Output toPlatformItem(const Input& input) |
+{ |
+ Output output; |
+ output.id = input.id(); |
+ output.label = input.label(); |
+ if (input.hasAmount()) { |
+ output.amount.currencyCode = input.amount().currencyCode(); |
+ output.amount.value = input.amount().value(); |
+ } |
+ return output; |
+} |
+ |
+PlatformPaymentDetails toPlatformPaymentDetails(const PaymentDetails& input) |
+{ |
+ PlatformPaymentDetails output; |
+ if (input.hasItems()) { |
+ output.items.resize(input.items().size()); |
+ for (size_t i = 0; i < input.items().size(); ++i) |
+ output.items[i] = toPlatformItem<PlatformPaymentItem, PaymentItem>(input.items()[i]); |
+ } |
+ if (input.hasShippingOptions()) { |
+ output.shippingOptions.resize(input.shippingOptions().size()); |
+ for (size_t i = 0; i < input.shippingOptions().size(); ++i) |
+ output.shippingOptions[i] = toPlatformItem<PlatformShippingOption, ShippingOption>(input.shippingOptions()[i]); |
+ } |
+ return output; |
+} |
+ |
+PlatformPaymentOptions toPlatformPaymentOptions(const PaymentOptions& input) |
+{ |
+ PlatformPaymentOptions output; |
+ output.requestShipping = input.requestShipping(); |
+ return output; |
+} |
+ |
+} // namespace |
// static |
PaymentRequest* PaymentRequest::create(ScriptState* scriptState, const Vector<String>& supportedMethods, const PaymentDetails& details, ExceptionState& exceptionState) |
@@ -38,11 +88,23 @@ PaymentRequest::~PaymentRequest() |
ScriptPromise PaymentRequest::show(ScriptState* scriptState) |
{ |
- return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(NotSupportedError, "Not implemented.")); |
+ if (m_showResolver) |
+ return m_showResolver->promise().rejectWithDOMException(scriptState, DOMException::create(SyntaxError, "Already called show() once.")); |
+ |
+ m_showResolver = ScriptPromiseResolver::create(scriptState); |
+ m_paymentRequestProxy.show(m_supportedMethods, toPlatformPaymentDetails(m_details), toPlatformPaymentOptions(m_options), m_stringifiedData); |
+ |
+ return m_showResolver->promise(); |
} |
-void PaymentRequest::abort() |
+void PaymentRequest::abort(ExceptionState& exceptionState) |
{ |
+ if (!m_showResolver) { |
+ exceptionState.throwDOMException(SyntaxError, "Never called show(), so nothing to abort."); |
+ return; |
+ } |
+ |
+ m_paymentRequestProxy.abort(); |
} |
const AtomicString& PaymentRequest::interfaceName() const |
@@ -55,11 +117,24 @@ ExecutionContext* PaymentRequest::executionContext() const |
return m_scriptState->executionContext(); |
} |
+ScriptPromise PaymentRequest::complete(ScriptState* scriptState, bool success) |
+{ |
+ if (m_completeResolver) |
+ return m_completeResolver->promise().rejectWithDOMException(scriptState, DOMException::create(SyntaxError, "Already called complete() once.")); |
+ |
+ m_completeResolver = ScriptPromiseResolver::create(scriptState); |
+ m_paymentRequestProxy.complete(success); |
+ |
+ return m_completeResolver->promise(); |
+} |
+ |
DEFINE_TRACE(PaymentRequest) |
{ |
visitor->trace(m_details); |
visitor->trace(m_options); |
visitor->trace(m_shippingAddress); |
+ visitor->trace(m_showResolver); |
+ visitor->trace(m_completeResolver); |
RefCountedGarbageCollectedEventTargetWithInlineData<PaymentRequest>::trace(visitor); |
} |
@@ -68,6 +143,7 @@ PaymentRequest::PaymentRequest(ScriptState* scriptState, const Vector<String>& s |
, m_supportedMethods(supportedMethods) |
, m_details(details) |
, m_options(options) |
+ , m_paymentRequestProxy(this) |
{ |
if (!data.isEmpty()) { |
RefPtr<JSONValue> value = toJSONValue(data.context(), data.v8Value()); |
@@ -76,4 +152,40 @@ PaymentRequest::PaymentRequest(ScriptState* scriptState, const Vector<String>& s |
} |
} |
+void PaymentRequest::onShippingAddressChange(const PlatformShippingAddress& address) |
+{ |
+ ASSERT(m_showResolver); |
+ m_shippingAddress = new ShippingAddress(address); |
+ RefPtrWillBeRawPtr<Event> event = Event::create(EventTypeNames::shippingaddresschange); |
+ event->setTarget(this); |
+ executionContext()->eventQueue()->enqueueEvent(event); |
+} |
+ |
+void PaymentRequest::onShippingOptionChange(const String& shippingOption) |
+{ |
+ ASSERT(m_showResolver); |
+ m_shippingOption = shippingOption; |
+ RefPtrWillBeRawPtr<Event> event = Event::create(EventTypeNames::shippingoptionchange); |
+ event->setTarget(this); |
+ executionContext()->eventQueue()->enqueueEvent(event); |
+} |
+ |
+void PaymentRequest::onPaymentResponse(const PlatformPaymentResponse& response) |
+{ |
+ ASSERT(m_showResolver); |
+ m_showResolver->resolve(new PaymentResponse(response, this)); |
+} |
+ |
+void PaymentRequest::onError() |
+{ |
+ ASSERT(m_showResolver); |
+ m_showResolver->reject(DOMException::create(SyntaxError, "Request cancelled.")); |
+} |
+ |
+void PaymentRequest::onComplete() |
+{ |
+ ASSERT(m_completeResolver); |
+ m_completeResolver->resolve(); |
+} |
+ |
} // namespace blink |