Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/payments/PaymentRequestUpdateEvent.cpp
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#include "mozilla/dom/PaymentRequestUpdateEvent.h"
8
9
namespace mozilla {
10
namespace dom {
11
12
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(PaymentRequestUpdateEvent)
18
0
NS_INTERFACE_MAP_END_INHERITING(Event)
19
20
NS_IMPL_ADDREF_INHERITED(PaymentRequestUpdateEvent, Event)
21
NS_IMPL_RELEASE_INHERITED(PaymentRequestUpdateEvent, Event)
22
23
already_AddRefed<PaymentRequestUpdateEvent>
24
PaymentRequestUpdateEvent::Constructor(mozilla::dom::EventTarget* aOwner,
25
                                       const nsAString& aType,
26
                                       const PaymentRequestUpdateEventInit& aEventInitDict)
27
0
{
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
0
}
35
36
already_AddRefed<PaymentRequestUpdateEvent>
37
PaymentRequestUpdateEvent::Constructor(const GlobalObject& aGlobal,
38
                                       const nsAString& aType,
39
                                       const PaymentRequestUpdateEventInit& aEventInitDict,
40
                                       ErrorResult& aRv)
41
0
{
42
0
  nsCOMPtr<mozilla::dom::EventTarget> owner = do_QueryInterface(aGlobal.GetAsSupports());
43
0
  return Constructor(owner, aType, aEventInitDict);
44
0
}
45
46
PaymentRequestUpdateEvent::PaymentRequestUpdateEvent(EventTarget* aOwner)
47
  : Event(aOwner, nullptr, nullptr)
48
  , mWaitForUpdate(false)
49
  , mRequest(nullptr)
50
0
{
51
0
  MOZ_ASSERT(aOwner);
52
0
}
53
54
void
55
PaymentRequestUpdateEvent::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
56
0
{
57
0
  MOZ_ASSERT(aCx);
58
0
  MOZ_ASSERT(mRequest);
59
0
60
0
  if (NS_WARN_IF(!aValue.isObject()) || !mWaitForUpdate) {
61
0
    return;
62
0
  }
63
0
64
0
  // Converting value to a PaymentDetailsUpdate dictionary
65
0
  PaymentDetailsUpdate details;
66
0
  if (!details.Init(aCx, aValue)) {
67
0
    mRequest->AbortUpdate(NS_ERROR_TYPE_ERR, false);
68
0
    JS_ClearPendingException(aCx);
69
0
    return;
70
0
  }
71
0
72
0
  // Validate and canonicalize the details
73
0
  // requestShipping must be true here. PaymentRequestUpdateEvent is only
74
0
  // dispatched when shippingAddress/shippingOption is changed, and it also means
75
0
  // Options.RequestShipping must be true while creating the corresponding
76
0
  // PaymentRequest.
77
0
  nsresult rv = mRequest->IsValidDetailsUpdate(details, true/*aRequestShipping*/);
78
0
  if (NS_FAILED(rv)) {
79
0
    mRequest->AbortUpdate(rv, false);
80
0
    return;
81
0
  }
82
0
83
0
  // Update the PaymentRequest with the new details
84
0
  if (NS_FAILED(mRequest->UpdatePayment(aCx, details, false))) {
85
0
    mRequest->AbortUpdate(NS_ERROR_DOM_ABORT_ERR, false);
86
0
    return;
87
0
  }
88
0
  mWaitForUpdate = false;
89
0
  mRequest->SetUpdating(false);
90
0
}
91
92
void
93
PaymentRequestUpdateEvent::RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue)
94
0
{
95
0
  MOZ_ASSERT(mRequest);
96
0
97
0
  mRequest->AbortUpdate(NS_ERROR_DOM_ABORT_ERR, false);
98
0
  mWaitForUpdate = false;
99
0
  mRequest->SetUpdating(false);
100
0
}
101
102
void
103
PaymentRequestUpdateEvent::UpdateWith(Promise& aPromise, ErrorResult& aRv)
104
0
{
105
0
  if (!IsTrusted()) {
106
0
    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
107
0
    return;
108
0
  }
109
0
110
0
  MOZ_ASSERT(mRequest);
111
0
112
0
  if (mWaitForUpdate || !mRequest->ReadyForUpdate()) {
113
0
    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
114
0
    return;
115
0
  }
116
0
117
0
  aPromise.AppendNativeHandler(this);
118
0
119
0
  StopPropagation();
120
0
  StopImmediatePropagation();
121
0
  mWaitForUpdate = true;
122
0
  mRequest->SetUpdating(true);
123
0
}
124
125
void
126
PaymentRequestUpdateEvent::SetRequest(PaymentRequest* aRequest)
127
0
{
128
0
  MOZ_ASSERT(IsTrusted());
129
0
  MOZ_ASSERT(!mRequest);
130
0
  MOZ_ASSERT(aRequest);
131
0
132
0
  mRequest = aRequest;
133
0
}
134
135
PaymentRequestUpdateEvent::~PaymentRequestUpdateEvent()
136
0
{
137
0
}
138
139
JSObject*
140
PaymentRequestUpdateEvent::WrapObjectInternal(JSContext* aCx,
141
                                              JS::Handle<JSObject*> aGivenProto)
142
0
{
143
0
  return PaymentRequestUpdateEvent_Binding::Wrap(aCx, this, aGivenProto);
144
0
}
145
146
147
} // namespace dom
148
} // namespace mozilla