1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/PaymentRequestUpdateEvent.h"
8 #include "mozilla/dom/PaymentRequest.h"
9 #include "mozilla/dom/RootedDictionary.h"
11 namespace mozilla::dom
{
13 NS_IMPL_CYCLE_COLLECTION_INHERITED(PaymentRequestUpdateEvent
, Event
, mRequest
)
15 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(PaymentRequestUpdateEvent
, Event
)
16 NS_IMPL_CYCLE_COLLECTION_TRACE_END
18 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PaymentRequestUpdateEvent
)
19 NS_INTERFACE_MAP_END_INHERITING(Event
)
21 NS_IMPL_ADDREF_INHERITED(PaymentRequestUpdateEvent
, Event
)
22 NS_IMPL_RELEASE_INHERITED(PaymentRequestUpdateEvent
, Event
)
24 already_AddRefed
<PaymentRequestUpdateEvent
>
25 PaymentRequestUpdateEvent::Constructor(
26 mozilla::dom::EventTarget
* aOwner
, const nsAString
& aType
,
27 const PaymentRequestUpdateEventInit
& aEventInitDict
) {
28 RefPtr
<PaymentRequestUpdateEvent
> e
= new PaymentRequestUpdateEvent(aOwner
);
29 bool trusted
= e
->Init(aOwner
);
30 e
->InitEvent(aType
, aEventInitDict
.mBubbles
, aEventInitDict
.mCancelable
);
31 e
->SetTrusted(trusted
);
32 e
->SetComposed(aEventInitDict
.mComposed
);
36 already_AddRefed
<PaymentRequestUpdateEvent
>
37 PaymentRequestUpdateEvent::Constructor(
38 const GlobalObject
& aGlobal
, const nsAString
& aType
,
39 const PaymentRequestUpdateEventInit
& aEventInitDict
) {
40 nsCOMPtr
<mozilla::dom::EventTarget
> owner
=
41 do_QueryInterface(aGlobal
.GetAsSupports());
42 return Constructor(owner
, aType
, aEventInitDict
);
45 PaymentRequestUpdateEvent::PaymentRequestUpdateEvent(EventTarget
* aOwner
)
46 : Event(aOwner
, nullptr, nullptr),
47 mWaitForUpdate(false),
52 void PaymentRequestUpdateEvent::ResolvedCallback(JSContext
* aCx
,
53 JS::Handle
<JS::Value
> aValue
,
57 if (!mRequest
->InFullyActiveDocument()) {
61 if (NS_WARN_IF(!aValue
.isObject()) || !mWaitForUpdate
) {
66 // Converting value to a PaymentDetailsUpdate dictionary
67 RootedDictionary
<PaymentDetailsUpdate
> details(aCx
);
68 if (!details
.Init(aCx
, aValue
)) {
69 rv
.StealExceptionFromJSContext(aCx
);
70 mRequest
->AbortUpdate(rv
);
74 // Validate and canonicalize the details
75 // requestShipping must be true here. PaymentRequestUpdateEvent is only
76 // dispatched when shippingAddress/shippingOption is changed, and it also
77 // means Options.RequestShipping must be true while creating the corresponding
79 mRequest
->IsValidDetailsUpdate(details
, true /*aRequestShipping*/, rv
);
81 mRequest
->AbortUpdate(rv
);
85 // Update the PaymentRequest with the new details
86 mRequest
->UpdatePayment(aCx
, details
, rv
);
88 mRequest
->AbortUpdate(rv
);
92 mWaitForUpdate
= false;
93 mRequest
->SetUpdating(false);
96 void PaymentRequestUpdateEvent::RejectedCallback(JSContext
* aCx
,
97 JS::Handle
<JS::Value
> aValue
,
100 if (!mRequest
->InFullyActiveDocument()) {
104 ErrorResult rejectReason
;
105 rejectReason
.ThrowAbortError(
106 "Details promise for PaymentRequestUpdateEvent.updateWith() is rejected "
108 mRequest
->AbortUpdate(rejectReason
);
109 mWaitForUpdate
= false;
110 mRequest
->SetUpdating(false);
113 void PaymentRequestUpdateEvent::UpdateWith(Promise
& aPromise
,
116 aRv
.ThrowInvalidStateError("Called on an untrusted event");
120 MOZ_ASSERT(mRequest
);
121 if (!mRequest
->InFullyActiveDocument()) {
125 if (mWaitForUpdate
|| !mRequest
->ReadyForUpdate()) {
126 aRv
.ThrowInvalidStateError(
127 "The PaymentRequestUpdateEvent is waiting for update");
131 if (!mRequest
->ReadyForUpdate()) {
132 aRv
.ThrowInvalidStateError(
133 "The PaymentRequest state is not eInteractive or is the PaymentRequest "
138 aPromise
.AppendNativeHandler(this);
141 StopImmediatePropagation();
142 mWaitForUpdate
= true;
143 mRequest
->SetUpdating(true);
146 void PaymentRequestUpdateEvent::SetRequest(PaymentRequest
* aRequest
) {
147 MOZ_ASSERT(IsTrusted());
148 MOZ_ASSERT(!mRequest
);
149 MOZ_ASSERT(aRequest
);
154 PaymentRequestUpdateEvent::~PaymentRequestUpdateEvent() = default;
156 JSObject
* PaymentRequestUpdateEvent::WrapObjectInternal(
157 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
158 return PaymentRequestUpdateEvent_Binding::Wrap(aCx
, this, aGivenProto
);
161 } // namespace mozilla::dom