Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / payments / PaymentRequestUpdateEvent.cpp
blob12c4a8c287a1bad287471425fb67a0d7699951e8
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);
33 return e.forget();
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),
48 mRequest(nullptr) {
49 MOZ_ASSERT(aOwner);
52 void PaymentRequestUpdateEvent::ResolvedCallback(JSContext* aCx,
53 JS::Handle<JS::Value> aValue,
54 ErrorResult& aRv) {
55 MOZ_ASSERT(aCx);
56 MOZ_ASSERT(mRequest);
57 if (!mRequest->InFullyActiveDocument()) {
58 return;
61 if (NS_WARN_IF(!aValue.isObject()) || !mWaitForUpdate) {
62 return;
65 ErrorResult rv;
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);
71 return;
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
78 // PaymentRequest.
79 mRequest->IsValidDetailsUpdate(details, true /*aRequestShipping*/, rv);
80 if (rv.Failed()) {
81 mRequest->AbortUpdate(rv);
82 return;
85 // Update the PaymentRequest with the new details
86 mRequest->UpdatePayment(aCx, details, rv);
87 if (rv.Failed()) {
88 mRequest->AbortUpdate(rv);
89 return;
92 mWaitForUpdate = false;
93 mRequest->SetUpdating(false);
96 void PaymentRequestUpdateEvent::RejectedCallback(JSContext* aCx,
97 JS::Handle<JS::Value> aValue,
98 ErrorResult& aRv) {
99 MOZ_ASSERT(mRequest);
100 if (!mRequest->InFullyActiveDocument()) {
101 return;
104 ErrorResult rejectReason;
105 rejectReason.ThrowAbortError(
106 "Details promise for PaymentRequestUpdateEvent.updateWith() is rejected "
107 "by merchant");
108 mRequest->AbortUpdate(rejectReason);
109 mWaitForUpdate = false;
110 mRequest->SetUpdating(false);
113 void PaymentRequestUpdateEvent::UpdateWith(Promise& aPromise,
114 ErrorResult& aRv) {
115 if (!IsTrusted()) {
116 aRv.ThrowInvalidStateError("Called on an untrusted event");
117 return;
120 MOZ_ASSERT(mRequest);
121 if (!mRequest->InFullyActiveDocument()) {
122 return;
125 if (mWaitForUpdate || !mRequest->ReadyForUpdate()) {
126 aRv.ThrowInvalidStateError(
127 "The PaymentRequestUpdateEvent is waiting for update");
128 return;
131 if (!mRequest->ReadyForUpdate()) {
132 aRv.ThrowInvalidStateError(
133 "The PaymentRequest state is not eInteractive or is the PaymentRequest "
134 "is updating");
135 return;
138 aPromise.AppendNativeHandler(this);
140 StopPropagation();
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);
151 mRequest = 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