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 "BasicCardPayment.h"
8 #include "mozilla/HoldDropJSObjects.h"
9 #include "mozilla/dom/PaymentMethodChangeEvent.h"
10 #include "mozilla/dom/PaymentRequestUpdateEvent.h"
11 #include "PaymentRequestUtils.h"
13 namespace mozilla::dom
{
15 NS_IMPL_CYCLE_COLLECTION_CLASS(PaymentMethodChangeEvent
)
16 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(PaymentMethodChangeEvent
,
17 PaymentRequestUpdateEvent
)
18 mozilla::DropJSObjects(tmp
);
19 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
21 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(PaymentMethodChangeEvent
,
22 PaymentRequestUpdateEvent
)
23 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
25 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(PaymentMethodChangeEvent
,
26 PaymentRequestUpdateEvent
)
27 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mMethodDetails
)
28 NS_IMPL_CYCLE_COLLECTION_TRACE_END
30 NS_IMPL_ADDREF_INHERITED(PaymentMethodChangeEvent
, PaymentRequestUpdateEvent
)
31 NS_IMPL_RELEASE_INHERITED(PaymentMethodChangeEvent
, PaymentRequestUpdateEvent
)
33 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PaymentMethodChangeEvent
)
34 NS_INTERFACE_MAP_END_INHERITING(PaymentRequestUpdateEvent
)
36 already_AddRefed
<PaymentMethodChangeEvent
>
37 PaymentMethodChangeEvent::Constructor(
38 mozilla::dom::EventTarget
* aOwner
, const nsAString
& aType
,
39 const PaymentRequestUpdateEventInit
& aEventInitDict
,
40 const nsAString
& aMethodName
, const ChangeDetails
& aMethodDetails
) {
41 RefPtr
<PaymentMethodChangeEvent
> e
= new PaymentMethodChangeEvent(aOwner
);
42 bool trusted
= e
->Init(aOwner
);
43 e
->InitEvent(aType
, aEventInitDict
.mBubbles
, aEventInitDict
.mCancelable
);
44 e
->SetTrusted(trusted
);
45 e
->SetComposed(aEventInitDict
.mComposed
);
46 e
->SetMethodName(aMethodName
);
47 e
->SetMethodDetails(aMethodDetails
);
51 already_AddRefed
<PaymentMethodChangeEvent
>
52 PaymentMethodChangeEvent::Constructor(
53 const GlobalObject
& aGlobal
, const nsAString
& aType
,
54 const PaymentMethodChangeEventInit
& aEventInitDict
) {
55 nsCOMPtr
<mozilla::dom::EventTarget
> owner
=
56 do_QueryInterface(aGlobal
.GetAsSupports());
57 RefPtr
<PaymentMethodChangeEvent
> e
= new PaymentMethodChangeEvent(owner
);
58 bool trusted
= e
->Init(owner
);
59 e
->InitEvent(aType
, aEventInitDict
.mBubbles
, aEventInitDict
.mCancelable
);
60 e
->SetTrusted(trusted
);
61 e
->SetComposed(aEventInitDict
.mComposed
);
62 e
->init(aEventInitDict
);
66 PaymentMethodChangeEvent::PaymentMethodChangeEvent(EventTarget
* aOwner
)
67 : PaymentRequestUpdateEvent(aOwner
) {
69 mozilla::HoldJSObjects(this);
72 void PaymentMethodChangeEvent::init(
73 const PaymentMethodChangeEventInit
& aEventInitDict
) {
74 mMethodName
.Assign(aEventInitDict
.mMethodName
);
75 mMethodDetails
= aEventInitDict
.mMethodDetails
;
78 void PaymentMethodChangeEvent::GetMethodName(nsAString
& aMethodName
) {
79 aMethodName
.Assign(mMethodName
);
82 void PaymentMethodChangeEvent::SetMethodName(const nsAString
& aMethodName
) {
83 mMethodName
= aMethodName
;
86 void PaymentMethodChangeEvent::GetMethodDetails(
87 JSContext
* aCx
, JS::MutableHandle
<JSObject
*> aRetVal
) {
91 aRetVal
.set(mMethodDetails
.get());
95 RefPtr
<BasicCardService
> service
= BasicCardService::GetService();
98 switch (mInternalDetails
.type()) {
99 case ChangeDetails::GeneralMethodDetails
: {
100 const GeneralDetails
& rawDetails
= mInternalDetails
.generalDetails();
101 DeserializeToJSObject(rawDetails
.details
, aCx
, aRetVal
);
104 case ChangeDetails::BasicCardMethodDetails
: {
105 const BasicCardDetails
& rawDetails
= mInternalDetails
.basicCardDetails();
106 BasicCardChangeDetails basicCardDetails
;
107 PaymentOptions options
;
108 mRequest
->GetOptions(options
);
109 if (options
.mRequestBillingAddress
) {
110 if (!rawDetails
.billingAddress
.country
.IsEmpty() ||
111 !rawDetails
.billingAddress
.addressLine
.IsEmpty() ||
112 !rawDetails
.billingAddress
.region
.IsEmpty() ||
113 !rawDetails
.billingAddress
.regionCode
.IsEmpty() ||
114 !rawDetails
.billingAddress
.city
.IsEmpty() ||
115 !rawDetails
.billingAddress
.dependentLocality
.IsEmpty() ||
116 !rawDetails
.billingAddress
.postalCode
.IsEmpty() ||
117 !rawDetails
.billingAddress
.sortingCode
.IsEmpty() ||
118 !rawDetails
.billingAddress
.organization
.IsEmpty() ||
119 !rawDetails
.billingAddress
.recipient
.IsEmpty() ||
120 !rawDetails
.billingAddress
.phone
.IsEmpty()) {
121 nsCOMPtr
<nsPIDOMWindowInner
> window
=
122 do_QueryInterface(GetParentObject());
123 basicCardDetails
.mBillingAddress
=
124 new PaymentAddress(window
, rawDetails
.billingAddress
.country
,
125 rawDetails
.billingAddress
.addressLine
,
126 rawDetails
.billingAddress
.region
,
127 rawDetails
.billingAddress
.regionCode
,
128 rawDetails
.billingAddress
.city
,
129 rawDetails
.billingAddress
.dependentLocality
,
130 rawDetails
.billingAddress
.postalCode
,
131 rawDetails
.billingAddress
.sortingCode
,
132 rawDetails
.billingAddress
.organization
,
133 rawDetails
.billingAddress
.recipient
,
134 rawDetails
.billingAddress
.phone
);
138 JS::Rooted
<JS::Value
> value(aCx
);
139 if (NS_WARN_IF(!basicCardDetails
.ToObjectInternal(aCx
, &value
))) {
142 aRetVal
.set(&value
.toObject());
151 void PaymentMethodChangeEvent::SetMethodDetails(
152 const ChangeDetails
& aMethodDetails
) {
153 mInternalDetails
= aMethodDetails
;
156 PaymentMethodChangeEvent::~PaymentMethodChangeEvent() {
157 mozilla::DropJSObjects(this);
160 JSObject
* PaymentMethodChangeEvent::WrapObjectInternal(
161 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
162 return PaymentMethodChangeEvent_Binding::Wrap(aCx
, this, aGivenProto
);
165 } // namespace mozilla::dom