1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/payments/can_make_payment_event.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/core/workers/worker_location.h"
#include "third_party/blink/renderer/modules/payments/can_make_payment_respond_with_observer.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
namespace blink {
class CanMakePaymentRespondWithFulfill final
: public ThenCallable<IDLBoolean, CanMakePaymentRespondWithFulfill> {
public:
explicit CanMakePaymentRespondWithFulfill(
CanMakePaymentRespondWithObserver* observer)
: observer_(observer) {}
void Trace(Visitor* visitor) const override {
visitor->Trace(observer_);
ThenCallable<IDLBoolean, CanMakePaymentRespondWithFulfill>::Trace(visitor);
}
void React(ScriptState* script_state, bool response) {
DCHECK(observer_);
observer_->OnResponseFulfilled(script_state, response);
}
private:
Member<CanMakePaymentRespondWithObserver> observer_;
};
CanMakePaymentEvent* CanMakePaymentEvent::Create(
const AtomicString& type,
const CanMakePaymentEventInit* initializer) {
return MakeGarbageCollected<CanMakePaymentEvent>(type, initializer, nullptr,
nullptr);
}
CanMakePaymentEvent* CanMakePaymentEvent::Create(
const AtomicString& type,
const CanMakePaymentEventInit* initializer,
CanMakePaymentRespondWithObserver* respond_with_observer,
WaitUntilObserver* wait_until_observer) {
return MakeGarbageCollected<CanMakePaymentEvent>(
type, initializer, respond_with_observer, wait_until_observer);
}
CanMakePaymentEvent::~CanMakePaymentEvent() = default;
const AtomicString& CanMakePaymentEvent::InterfaceName() const {
return event_interface_names::kCanMakePaymentEvent;
}
const String& CanMakePaymentEvent::topOrigin() const {
return top_origin_;
}
const String& CanMakePaymentEvent::paymentRequestOrigin() const {
return payment_request_origin_;
}
const HeapVector<Member<PaymentMethodData>>& CanMakePaymentEvent::methodData()
const {
return method_data_;
}
const HeapVector<Member<PaymentDetailsModifier>>&
CanMakePaymentEvent::modifiers() const {
return modifiers_;
}
void CanMakePaymentEvent::respondWith(ScriptState* script_state,
ScriptPromise<IDLBoolean> script_promise,
ExceptionState& exception_state) {
if (!isTrusted()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kInvalidStateError,
"Cannot respond with data when the event is not trusted");
return;
}
stopImmediatePropagation();
if (observer_) {
observer_->RespondWith(
script_state, script_promise,
MakeGarbageCollected<CanMakePaymentRespondWithFulfill>(observer_),
exception_state);
}
}
void CanMakePaymentEvent::Trace(Visitor* visitor) const {
visitor->Trace(method_data_);
visitor->Trace(modifiers_);
visitor->Trace(observer_);
ExtendableEvent::Trace(visitor);
}
// TODO(crbug.com/1070871): Use fooOr() in members' initializers.
CanMakePaymentEvent::CanMakePaymentEvent(
const AtomicString& type,
const CanMakePaymentEventInit* initializer,
CanMakePaymentRespondWithObserver* respond_with_observer,
WaitUntilObserver* wait_until_observer)
: ExtendableEvent(type, initializer, wait_until_observer),
top_origin_(initializer->hasTopOrigin() ? initializer->topOrigin()
: String()),
payment_request_origin_(initializer->hasPaymentRequestOrigin()
? initializer->paymentRequestOrigin()
: String()),
method_data_(initializer->hasMethodData()
? initializer->methodData()
: HeapVector<Member<PaymentMethodData>>()),
modifiers_(initializer->hasModifiers()
? initializer->modifiers()
: HeapVector<Member<PaymentDetailsModifier>>()),
observer_(respond_with_observer) {}
} // namespace blink
|