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/PaymentRequestUpdateEvent.h" |
| 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "bindings/core/v8/ScriptFunction.h" |
| 9 #include "core/dom/DOMException.h" |
| 10 #include "core/dom/ExceptionCode.h" |
| 11 #include "modules/payments/PaymentUpdater.h" |
| 12 |
| 13 namespace blink { |
| 14 namespace { |
| 15 |
| 16 class UpdatePaymentDetailsFunction : public ScriptFunction { |
| 17 public: |
| 18 static v8::Local<v8::Function> createFunction(ScriptState* scriptState, Paym
entUpdater* updater) |
| 19 { |
| 20 UpdatePaymentDetailsFunction* self = new UpdatePaymentDetailsFunction(sc
riptState, updater); |
| 21 return self->bindToV8Function(); |
| 22 } |
| 23 |
| 24 DEFINE_INLINE_VIRTUAL_TRACE() |
| 25 { |
| 26 visitor->trace(m_updater); |
| 27 ScriptFunction::trace(visitor); |
| 28 } |
| 29 |
| 30 private: |
| 31 UpdatePaymentDetailsFunction(ScriptState* scriptState, PaymentUpdater* updat
er) |
| 32 : ScriptFunction(scriptState) |
| 33 , m_updater(updater) |
| 34 { |
| 35 DCHECK(m_updater); |
| 36 } |
| 37 |
| 38 ScriptValue call(ScriptValue value) override |
| 39 { |
| 40 m_updater->onUpdatePaymentDetails(value); |
| 41 return ScriptValue(); |
| 42 } |
| 43 |
| 44 Member<PaymentUpdater> m_updater; |
| 45 }; |
| 46 |
| 47 class UpdatePaymentDetailsErrorFunction : public ScriptFunction { |
| 48 public: |
| 49 static v8::Local<v8::Function> createFunction(ScriptState* scriptState, Paym
entUpdater* updater) |
| 50 { |
| 51 UpdatePaymentDetailsErrorFunction* self = new UpdatePaymentDetailsErrorF
unction(scriptState, updater); |
| 52 return self->bindToV8Function(); |
| 53 } |
| 54 |
| 55 DEFINE_INLINE_VIRTUAL_TRACE() |
| 56 { |
| 57 visitor->trace(m_updater); |
| 58 ScriptFunction::trace(visitor); |
| 59 } |
| 60 |
| 61 private: |
| 62 UpdatePaymentDetailsErrorFunction(ScriptState* scriptState, PaymentUpdater*
updater) |
| 63 : ScriptFunction(scriptState) |
| 64 , m_updater(updater) |
| 65 { |
| 66 DCHECK(m_updater); |
| 67 } |
| 68 |
| 69 ScriptValue call(ScriptValue value) override |
| 70 { |
| 71 m_updater->onUpdatePaymentDetailsFailure(value); |
| 72 return ScriptValue(); |
| 73 } |
| 74 |
| 75 Member<PaymentUpdater> m_updater; |
| 76 }; |
| 77 |
| 78 } // namespace |
| 79 |
| 80 PaymentRequestUpdateEvent::~PaymentRequestUpdateEvent() |
| 81 { |
| 82 } |
| 83 |
| 84 PaymentRequestUpdateEvent* PaymentRequestUpdateEvent::create() |
| 85 { |
| 86 return new PaymentRequestUpdateEvent(); |
| 87 } |
| 88 |
| 89 PaymentRequestUpdateEvent* PaymentRequestUpdateEvent::create(const AtomicString&
type, const PaymentRequestUpdateEventInit& init) |
| 90 { |
| 91 return new PaymentRequestUpdateEvent(type, init); |
| 92 } |
| 93 |
| 94 void PaymentRequestUpdateEvent::setPaymentDetailsUpdater(PaymentUpdater* updater
) |
| 95 { |
| 96 m_updater = updater; |
| 97 } |
| 98 |
| 99 void PaymentRequestUpdateEvent::updateWith(ScriptState* scriptState, ScriptPromi
se promise, ExceptionState& exceptionState) |
| 100 { |
| 101 if (!m_updater) |
| 102 return; |
| 103 |
| 104 if (!isBeingDispatched()) { |
| 105 exceptionState.throwDOMException(InvalidStateError, "Cannot update detai
ls when the event is not being dispatched"); |
| 106 return; |
| 107 } |
| 108 |
| 109 if (m_waitForUpdate) { |
| 110 exceptionState.throwDOMException(InvalidStateError, "Cannot update detai
ls twice"); |
| 111 return; |
| 112 } |
| 113 |
| 114 stopImmediatePropagation(); |
| 115 m_waitForUpdate = true; |
| 116 |
| 117 promise.then(UpdatePaymentDetailsFunction::createFunction(scriptState, m_upd
ater), |
| 118 UpdatePaymentDetailsErrorFunction::createFunction(scriptState, m_updater
)); |
| 119 } |
| 120 |
| 121 DEFINE_TRACE(PaymentRequestUpdateEvent) |
| 122 { |
| 123 visitor->trace(m_updater); |
| 124 Event::trace(visitor); |
| 125 } |
| 126 |
| 127 PaymentRequestUpdateEvent::PaymentRequestUpdateEvent() |
| 128 : m_waitForUpdate(false) |
| 129 { |
| 130 } |
| 131 |
| 132 PaymentRequestUpdateEvent::PaymentRequestUpdateEvent(const AtomicString& type, c
onst PaymentRequestUpdateEventInit& init) |
| 133 : Event(type, init) |
| 134 , m_waitForUpdate(false) |
| 135 { |
| 136 } |
| 137 |
| 138 } // namespace blink |
OLD | NEW |