Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/payments/MerchantValidationEvent.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/MerchantValidationEvent.h"
8
#include "nsNetCID.h"
9
#include "mozilla/dom/PaymentRequest.h"
10
#include "mozilla/dom/Location.h"
11
#include "mozilla/dom/URL.h"
12
#include "nsIURI.h"
13
#include "nsNetUtil.h"
14
#include "nsContentUtils.h"
15
16
namespace mozilla {
17
namespace dom {
18
19
NS_IMPL_CYCLE_COLLECTION_INHERITED(MerchantValidationEvent, Event, mRequest)
20
21
0
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(MerchantValidationEvent, Event)
22
0
NS_IMPL_CYCLE_COLLECTION_TRACE_END
23
24
0
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MerchantValidationEvent)
25
0
NS_INTERFACE_MAP_END_INHERITING(Event)
26
27
NS_IMPL_ADDREF_INHERITED(MerchantValidationEvent, Event)
28
NS_IMPL_RELEASE_INHERITED(MerchantValidationEvent, Event)
29
30
// User-land code constructor
31
already_AddRefed<MerchantValidationEvent>
32
MerchantValidationEvent::Constructor(
33
  const GlobalObject& aGlobal,
34
  const nsAString& aType,
35
  const MerchantValidationEventInit& aEventInitDict,
36
  ErrorResult& aRv)
37
0
{
38
0
  // validate passed URL
39
0
  nsCOMPtr<mozilla::dom::EventTarget> owner =
40
0
    do_QueryInterface(aGlobal.GetAsSupports());
41
0
  return Constructor(owner, aType, aEventInitDict, aRv);
42
0
}
43
44
// Internal JS object constructor
45
already_AddRefed<MerchantValidationEvent>
46
MerchantValidationEvent::Constructor(
47
  EventTarget* aOwner,
48
  const nsAString& aType,
49
  const MerchantValidationEventInit& aEventInitDict,
50
  ErrorResult& aRv)
51
0
{
52
0
  RefPtr<MerchantValidationEvent> e = new MerchantValidationEvent(aOwner);
53
0
  bool trusted = e->Init(aOwner);
54
0
  e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
55
0
  if (!e->init(aEventInitDict, aRv)) {
56
0
    return nullptr;
57
0
  }
58
0
  e->SetTrusted(trusted);
59
0
  e->SetComposed(aEventInitDict.mComposed);
60
0
  return e.forget();
61
0
}
62
63
bool
64
MerchantValidationEvent::init(const MerchantValidationEventInit& aEventInitDict,
65
                              ErrorResult& aRv)
66
0
{
67
0
  // Check methodName is valid
68
0
  if (!aEventInitDict.mMethodName.IsEmpty()) {
69
0
    nsString errMsg;
70
0
    auto rv = PaymentRequest::IsValidPaymentMethodIdentifier(
71
0
      aEventInitDict.mMethodName, errMsg);
72
0
    if (NS_FAILED(rv)) {
73
0
      aRv.ThrowRangeError<MSG_ILLEGAL_RANGE_PR_CONSTRUCTOR>(errMsg);
74
0
      return false;
75
0
    }
76
0
  }
77
0
  SetMethodName(aEventInitDict.mMethodName);
78
0
  nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(GetParentObject());
79
0
  auto doc = window->GetExtantDoc();
80
0
  if (!doc) {
81
0
    aRv.Throw(NS_ERROR_UNEXPECTED);
82
0
    return false;
83
0
  }
84
0
  auto principal = doc->NodePrincipal();
85
0
86
0
  nsCOMPtr<nsIURI> baseURI;
87
0
  principal->GetURI(getter_AddRefs(baseURI));
88
0
89
0
  nsresult rv;
90
0
  nsCOMPtr<nsIURI> validationUri;
91
0
  rv = NS_NewURI(getter_AddRefs(validationUri),
92
0
                 aEventInitDict.mValidationURL,
93
0
                 nullptr,
94
0
                 baseURI,
95
0
                 nsContentUtils::GetIOService());
96
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
97
0
    aRv.ThrowTypeError<MSG_INVALID_URL>(aEventInitDict.mValidationURL);
98
0
    return false;
99
0
  }
100
0
  nsAutoCString utf8href;
101
0
  rv = validationUri->GetSpec(utf8href);
102
0
  if (NS_FAILED(rv)) {
103
0
    aRv.Throw(NS_ERROR_DOM_BAD_URI);
104
0
    return false;
105
0
  }
106
0
  CopyUTF8toUTF16(utf8href, mValidationURL);
107
0
  return true;
108
0
}
109
110
MerchantValidationEvent::MerchantValidationEvent(EventTarget* aOwner)
111
  : Event(aOwner, nullptr, nullptr)
112
  , mWaitForUpdate(false)
113
0
{
114
0
  MOZ_ASSERT(aOwner);
115
0
}
116
117
void
118
MerchantValidationEvent::ResolvedCallback(JSContext* aCx,
119
                                          JS::Handle<JS::Value> aValue)
120
0
{
121
0
  MOZ_ASSERT(aCx);
122
0
  MOZ_ASSERT(mRequest);
123
0
124
0
  if (!mWaitForUpdate) {
125
0
    return;
126
0
  }
127
0
  mWaitForUpdate = false;
128
0
129
0
  // If we eventually end up supporting merchant validation
130
0
  // we would validate `aValue` here, as per:
131
0
  // https://w3c.github.io/payment-request/#validate-merchant-s-details-algorithm
132
0
  //
133
0
  // Right now, MerchantValidationEvent is only implemented for standards
134
0
  // conformance, which is why at this point we throw a
135
0
  // NS_ERROR_DOM_NOT_SUPPORTED_ERR.
136
0
137
0
  mRequest->AbortUpdate(NS_ERROR_DOM_NOT_SUPPORTED_ERR, false);
138
0
  mRequest->SetUpdating(false);
139
0
}
140
141
void
142
MerchantValidationEvent::RejectedCallback(JSContext* aCx,
143
                                          JS::Handle<JS::Value> aValue)
144
0
{
145
0
  MOZ_ASSERT(mRequest);
146
0
  if (!mWaitForUpdate) {
147
0
    return;
148
0
  }
149
0
  mWaitForUpdate = false;
150
0
  mRequest->AbortUpdate(NS_ERROR_DOM_ABORT_ERR, false);
151
0
  mRequest->SetUpdating(false);
152
0
}
153
154
void
155
MerchantValidationEvent::Complete(Promise& aPromise, ErrorResult& aRv)
156
0
{
157
0
  if (!IsTrusted()) {
158
0
    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
159
0
    return;
160
0
  }
161
0
162
0
  MOZ_ASSERT(mRequest);
163
0
164
0
  if (mWaitForUpdate || !mRequest->ReadyForUpdate()) {
165
0
    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
166
0
    return;
167
0
  }
168
0
169
0
  aPromise.AppendNativeHandler(this);
170
0
171
0
  StopPropagation();
172
0
  StopImmediatePropagation();
173
0
  mWaitForUpdate = true;
174
0
  mRequest->SetUpdating(true);
175
0
}
176
177
void
178
MerchantValidationEvent::SetRequest(PaymentRequest* aRequest)
179
0
{
180
0
  MOZ_ASSERT(IsTrusted());
181
0
  MOZ_ASSERT(!mRequest);
182
0
  MOZ_ASSERT(aRequest);
183
0
184
0
  mRequest = aRequest;
185
0
}
186
187
void
188
MerchantValidationEvent::GetValidationURL(nsAString& aValidationURL)
189
0
{
190
0
  aValidationURL.Assign(mValidationURL);
191
0
}
192
193
void
194
MerchantValidationEvent::SetValidationURL(nsAString& aValidationURL)
195
0
{
196
0
  mValidationURL.Assign(aValidationURL);
197
0
}
198
199
void
200
MerchantValidationEvent::GetMethodName(nsAString& aMethodName)
201
0
{
202
0
  aMethodName.Assign(mMethodName);
203
0
}
204
205
void
206
MerchantValidationEvent::SetMethodName(const nsAString& aMethodName)
207
0
{
208
0
  mMethodName.Assign(aMethodName);
209
0
}
210
211
0
MerchantValidationEvent::~MerchantValidationEvent() {}
212
213
JSObject*
214
MerchantValidationEvent::WrapObjectInternal(JSContext* aCx,
215
                                            JS::Handle<JSObject*> aGivenProto)
216
0
{
217
0
  return MerchantValidationEvent_Binding::Wrap(aCx, this, aGivenProto);
218
0
}
219
220
} // namespace dom
221
} // namespace mozilla