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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/PaymentRequestUpdateEvent.h"
#include "mozilla/dom/PaymentRequest.h"
#include "mozilla/dom/RootedDictionary.h"
namespace mozilla::dom {
NS_IMPL_CYCLE_COLLECTION_INHERITED(PaymentRequestUpdateEvent, Event, mRequest)
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(PaymentRequestUpdateEvent, Event)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PaymentRequestUpdateEvent)
NS_INTERFACE_MAP_END_INHERITING(Event)
NS_IMPL_ADDREF_INHERITED(PaymentRequestUpdateEvent, Event)
NS_IMPL_RELEASE_INHERITED(PaymentRequestUpdateEvent, Event)
already_AddRefed<PaymentRequestUpdateEvent>
PaymentRequestUpdateEvent::Constructor(
mozilla::dom::EventTarget* aOwner, const nsAString& aType,
const PaymentRequestUpdateEventInit& aEventInitDict) {
RefPtr<PaymentRequestUpdateEvent> e = new PaymentRequestUpdateEvent(aOwner);
bool trusted = e->Init(aOwner);
e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
e->SetTrusted(trusted);
e->SetComposed(aEventInitDict.mComposed);
return e.forget();
}
already_AddRefed<PaymentRequestUpdateEvent>
PaymentRequestUpdateEvent::Constructor(
const GlobalObject& aGlobal, const nsAString& aType,
const PaymentRequestUpdateEventInit& aEventInitDict) {
nsCOMPtr<mozilla::dom::EventTarget> owner =
do_QueryInterface(aGlobal.GetAsSupports());
return Constructor(owner, aType, aEventInitDict);
}
PaymentRequestUpdateEvent::PaymentRequestUpdateEvent(EventTarget* aOwner)
: Event(aOwner, nullptr, nullptr),
mWaitForUpdate(false),
mRequest(nullptr) {
MOZ_ASSERT(aOwner);
}
void PaymentRequestUpdateEvent::ResolvedCallback(JSContext* aCx,
JS::Handle<JS::Value> aValue) {
MOZ_ASSERT(aCx);
MOZ_ASSERT(mRequest);
if (!mRequest->InFullyActiveDocument()) {
return;
}
if (NS_WARN_IF(!aValue.isObject()) || !mWaitForUpdate) {
return;
}
ErrorResult rv;
// Converting value to a PaymentDetailsUpdate dictionary
RootedDictionary<PaymentDetailsUpdate> details(aCx);
if (!details.Init(aCx, aValue)) {
rv.StealExceptionFromJSContext(aCx);
mRequest->AbortUpdate(rv);
return;
}
// Validate and canonicalize the details
// requestShipping must be true here. PaymentRequestUpdateEvent is only
// dispatched when shippingAddress/shippingOption is changed, and it also
// means Options.RequestShipping must be true while creating the corresponding
// PaymentRequest.
mRequest->IsValidDetailsUpdate(details, true /*aRequestShipping*/, rv);
if (rv.Failed()) {
mRequest->AbortUpdate(rv);
return;
}
// Update the PaymentRequest with the new details
mRequest->UpdatePayment(aCx, details, rv);
if (rv.Failed()) {
mRequest->AbortUpdate(rv);
return;
}
mWaitForUpdate = false;
mRequest->SetUpdating(false);
}
void PaymentRequestUpdateEvent::RejectedCallback(JSContext* aCx,
JS::Handle<JS::Value> aValue) {
MOZ_ASSERT(mRequest);
if (!mRequest->InFullyActiveDocument()) {
return;
}
ErrorResult rejectReason;
rejectReason.ThrowAbortError(
"Details promise for PaymentRequestUpdateEvent.updateWith() is rejected "
"by merchant");
mRequest->AbortUpdate(rejectReason);
mWaitForUpdate = false;
mRequest->SetUpdating(false);
}
void PaymentRequestUpdateEvent::UpdateWith(Promise& aPromise,
ErrorResult& aRv) {
if (!IsTrusted()) {
aRv.ThrowInvalidStateError("Called on an untrusted event");
return;
}
MOZ_ASSERT(mRequest);
if (!mRequest->InFullyActiveDocument()) {
return;
}
if (mWaitForUpdate || !mRequest->ReadyForUpdate()) {
aRv.ThrowInvalidStateError(
"The PaymentRequestUpdateEvent is waiting for update");
return;
}
if (!mRequest->ReadyForUpdate()) {
aRv.ThrowInvalidStateError(
"The PaymentRequest state is not eInteractive or is the PaymentRequest "
"is updating");
return;
}
aPromise.AppendNativeHandler(this);
StopPropagation();
StopImmediatePropagation();
mWaitForUpdate = true;
mRequest->SetUpdating(true);
}
void PaymentRequestUpdateEvent::SetRequest(PaymentRequest* aRequest) {
MOZ_ASSERT(IsTrusted());
MOZ_ASSERT(!mRequest);
MOZ_ASSERT(aRequest);
mRequest = aRequest;
}
PaymentRequestUpdateEvent::~PaymentRequestUpdateEvent() = default;
JSObject* PaymentRequestUpdateEvent::WrapObjectInternal(
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
return PaymentRequestUpdateEvent_Binding::Wrap(aCx, this, aGivenProto);
}
} // namespace mozilla::dom
|