Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim:set ts=2 sw=2 sts=2 et cindent: */
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/. */
6 :
7 : #include "mozilla/dom/PaymentRequestUpdateEvent.h"
8 :
9 : namespace mozilla {
10 : namespace dom {
11 :
12 0 : NS_IMPL_CYCLE_COLLECTION_INHERITED(PaymentRequestUpdateEvent, Event, mRequest)
13 :
14 0 : NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(PaymentRequestUpdateEvent, Event)
15 0 : NS_IMPL_CYCLE_COLLECTION_TRACE_END
16 :
17 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(PaymentRequestUpdateEvent)
18 0 : NS_INTERFACE_MAP_END_INHERITING(Event)
19 :
20 0 : NS_IMPL_ADDREF_INHERITED(PaymentRequestUpdateEvent, Event)
21 0 : NS_IMPL_RELEASE_INHERITED(PaymentRequestUpdateEvent, Event)
22 :
23 : already_AddRefed<PaymentRequestUpdateEvent>
24 0 : PaymentRequestUpdateEvent::Constructor(mozilla::dom::EventTarget* aOwner,
25 : const nsAString& aType,
26 : const PaymentRequestUpdateEventInit& aEventInitDict)
27 : {
28 0 : RefPtr<PaymentRequestUpdateEvent> e = new PaymentRequestUpdateEvent(aOwner);
29 0 : bool trusted = e->Init(aOwner);
30 0 : e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
31 0 : e->SetTrusted(trusted);
32 0 : e->SetComposed(aEventInitDict.mComposed);
33 0 : return e.forget();
34 : }
35 :
36 : already_AddRefed<PaymentRequestUpdateEvent>
37 0 : PaymentRequestUpdateEvent::Constructor(const GlobalObject& aGlobal,
38 : const nsAString& aType,
39 : const PaymentRequestUpdateEventInit& aEventInitDict,
40 : ErrorResult& aRv)
41 : {
42 0 : nsCOMPtr<mozilla::dom::EventTarget> owner = do_QueryInterface(aGlobal.GetAsSupports());
43 0 : return Constructor(owner, aType, aEventInitDict);
44 : }
45 :
46 0 : PaymentRequestUpdateEvent::PaymentRequestUpdateEvent(EventTarget* aOwner)
47 : : Event(aOwner, nullptr, nullptr)
48 0 : , mWaitForUpdate(false)
49 : {
50 0 : MOZ_ASSERT(aOwner);
51 :
52 : // event's target should be a PaymentRequest object
53 0 : mRequest = static_cast<PaymentRequest *>(aOwner);
54 0 : }
55 :
56 : void
57 0 : PaymentRequestUpdateEvent::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
58 : {
59 0 : MOZ_ASSERT(mRequest);
60 :
61 0 : if (NS_WARN_IF(!aValue.isObject()) || !mWaitForUpdate) {
62 0 : return;
63 : }
64 :
65 : // Converting value to a PaymentDetailsUpdate dictionary
66 0 : PaymentDetailsUpdate details;
67 0 : if (!details.Init(aCx, aValue)) {
68 0 : return;
69 : }
70 :
71 : // Validate and canonicalize the details
72 0 : if (!mRequest->IsValidDetailsUpdate(details)) {
73 0 : mRequest->AbortUpdate(NS_ERROR_TYPE_ERR);
74 0 : return;
75 : }
76 :
77 : // [TODO]
78 : // If the data member of modifier is present,
79 : // let serializedData be the result of JSON-serializing modifier.data into a string.
80 : // null if it is not.
81 :
82 : // Update the PaymentRequest with the new details
83 0 : if (NS_FAILED(mRequest->UpdatePayment(details))) {
84 0 : mRequest->AbortUpdate(NS_ERROR_DOM_ABORT_ERR);
85 0 : return;
86 : }
87 0 : mWaitForUpdate = false;
88 0 : mRequest->SetUpdating(false);
89 : }
90 :
91 : void
92 0 : PaymentRequestUpdateEvent::RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
93 : {
94 0 : MOZ_ASSERT(mRequest);
95 :
96 0 : mRequest->AbortUpdate(NS_ERROR_DOM_ABORT_ERR);
97 0 : mWaitForUpdate = false;
98 0 : mRequest->SetUpdating(false);
99 0 : }
100 :
101 : void
102 0 : PaymentRequestUpdateEvent::UpdateWith(Promise& aPromise, ErrorResult& aRv)
103 : {
104 0 : MOZ_ASSERT(mRequest);
105 :
106 0 : if (mWaitForUpdate || !mRequest->ReadyForUpdate() ||
107 0 : !mEvent->mFlags.mIsBeingDispatched) {
108 0 : aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
109 0 : return;
110 : }
111 :
112 0 : aPromise.AppendNativeHandler(this);
113 :
114 0 : StopPropagation();
115 0 : StopImmediatePropagation();
116 0 : mWaitForUpdate = true;
117 0 : mRequest->SetUpdating(true);
118 : }
119 :
120 : bool
121 0 : PaymentRequestUpdateEvent::IsTrusted() const
122 : {
123 0 : return true;
124 : }
125 :
126 0 : PaymentRequestUpdateEvent::~PaymentRequestUpdateEvent()
127 : {
128 0 : }
129 :
130 : JSObject*
131 0 : PaymentRequestUpdateEvent::WrapObjectInternal(JSContext* aCx,
132 : JS::Handle<JSObject*> aGivenProto)
133 : {
134 0 : return PaymentRequestUpdateEventBinding::Wrap(aCx, this, aGivenProto);
135 : }
136 :
137 :
138 : } // namespace dom
139 : } // namespace mozilla
|