summaryrefslogtreecommitdiffstats
path: root/dom/payments/MerchantValidationEvent.cpp
blob: cd1c8f1800b7d9394aee18b0c21d5ba1b335cc51 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* -*- 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/MerchantValidationEvent.h"
#include "nsNetCID.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/PaymentRequest.h"
#include "mozilla/dom/Location.h"
#include "mozilla/dom/URL.h"
#include "mozilla/ResultExtensions.h"
#include "nsIURI.h"
#include "nsNetUtil.h"

namespace mozilla::dom {

NS_IMPL_CYCLE_COLLECTION_INHERITED(MerchantValidationEvent, Event,
                                   mValidationURL, mRequest)

NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(MerchantValidationEvent, Event)
NS_IMPL_CYCLE_COLLECTION_TRACE_END

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MerchantValidationEvent)
NS_INTERFACE_MAP_END_INHERITING(Event)

NS_IMPL_ADDREF_INHERITED(MerchantValidationEvent, Event)
NS_IMPL_RELEASE_INHERITED(MerchantValidationEvent, Event)

// User-land code constructor
already_AddRefed<MerchantValidationEvent> MerchantValidationEvent::Constructor(
    const GlobalObject& aGlobal, const nsAString& aType,
    const MerchantValidationEventInit& aEventInitDict, ErrorResult& aRv) {
  // validate passed URL
  nsCOMPtr<mozilla::dom::EventTarget> owner =
      do_QueryInterface(aGlobal.GetAsSupports());
  return Constructor(owner, aType, aEventInitDict, aRv);
}

// Internal JS object constructor
already_AddRefed<MerchantValidationEvent> MerchantValidationEvent::Constructor(
    EventTarget* aOwner, const nsAString& aType,
    const MerchantValidationEventInit& aEventInitDict, ErrorResult& aRv) {
  RefPtr<MerchantValidationEvent> e = new MerchantValidationEvent(aOwner);
  bool trusted = e->Init(aOwner);
  e->InitEvent(aType, aEventInitDict.mBubbles, aEventInitDict.mCancelable);
  e->init(aEventInitDict, aRv);
  if (aRv.Failed()) {
    return nullptr;
  }
  e->SetTrusted(trusted);
  e->SetComposed(aEventInitDict.mComposed);
  return e.forget();
}

void MerchantValidationEvent::init(
    const MerchantValidationEventInit& aEventInitDict, ErrorResult& aRv) {
  // Check methodName is valid
  if (!aEventInitDict.mMethodName.IsEmpty()) {
    PaymentRequest::IsValidPaymentMethodIdentifier(aEventInitDict.mMethodName,
                                                   aRv);
    if (aRv.Failed()) {
      return;
    }
  }
  SetMethodName(aEventInitDict.mMethodName);
  nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(GetParentObject());
  auto doc = window->GetExtantDoc();
  if (!doc) {
    aRv.ThrowAbortError("The owner document does not exist");
    return;
  }

  Result<OwningNonNull<nsIURI>, nsresult> rv =
      doc->ResolveWithBaseURI(aEventInitDict.mValidationURL);
  if (rv.isErr()) {
    aRv.ThrowTypeError("validationURL cannot be parsed");
    return;
  }
  mValidationURL = rv.unwrap();
}

MerchantValidationEvent::MerchantValidationEvent(EventTarget* aOwner)
    : Event(aOwner, nullptr, nullptr), mWaitForUpdate(false) {
  MOZ_ASSERT(aOwner);
}

void MerchantValidationEvent::ResolvedCallback(JSContext* aCx,
                                               JS::Handle<JS::Value> aValue,
                                               ErrorResult& aRv) {
  MOZ_ASSERT(aCx);
  MOZ_ASSERT(mRequest);

  if (!mWaitForUpdate) {
    return;
  }
  mWaitForUpdate = false;

  // If we eventually end up supporting merchant validation
  // we would validate `aValue` here, as per:
  // https://w3c.github.io/payment-request/#validate-merchant-s-details-algorithm
  //
  // Right now, MerchantValidationEvent is only implemented for standards
  // conformance, which is why at this point we throw a
  // NS_ERROR_DOM_NOT_SUPPORTED_ERR.

  ErrorResult result;
  result.ThrowNotSupportedError(
      "complete() is not supported by Firefox currently");
  mRequest->AbortUpdate(result);
  mRequest->SetUpdating(false);
}

void MerchantValidationEvent::RejectedCallback(JSContext* aCx,
                                               JS::Handle<JS::Value> aValue,
                                               ErrorResult& aRv) {
  MOZ_ASSERT(mRequest);
  if (!mWaitForUpdate) {
    return;
  }
  mWaitForUpdate = false;
  ErrorResult result;
  result.ThrowAbortError(
      "The promise for MerchantValidtaionEvent.complete() is rejected");
  mRequest->AbortUpdate(result);
  mRequest->SetUpdating(false);
}

void MerchantValidationEvent::Complete(Promise& aPromise, ErrorResult& aRv) {
  if (!IsTrusted()) {
    aRv.ThrowInvalidStateError("Called on an untrusted event");
    return;
  }

  MOZ_ASSERT(mRequest);

  if (mWaitForUpdate) {
    aRv.ThrowInvalidStateError(
        "The MerchantValidationEvent is waiting for update");
    return;
  }

  if (!mRequest->ReadyForUpdate()) {
    aRv.ThrowInvalidStateError(
        "The PaymentRequest state is not eInteractive or the PaymentRequest is "
        "updating");
    return;
  }

  aPromise.AppendNativeHandler(this);

  StopPropagation();
  StopImmediatePropagation();
  mWaitForUpdate = true;
  mRequest->SetUpdating(true);
}

void MerchantValidationEvent::SetRequest(PaymentRequest* aRequest) {
  MOZ_ASSERT(IsTrusted());
  MOZ_ASSERT(!mRequest);
  MOZ_ASSERT(aRequest);

  mRequest = aRequest;
}

void MerchantValidationEvent::GetValidationURL(nsAString& aValidationURL) {
  nsAutoCString utf8href;
  nsresult rv = mValidationURL->GetSpec(utf8href);
  MOZ_ASSERT(NS_SUCCEEDED(rv));
  Unused << rv;
  aValidationURL.Assign(NS_ConvertUTF8toUTF16(utf8href));
}

void MerchantValidationEvent::GetMethodName(nsAString& aMethodName) {
  aMethodName.Assign(mMethodName);
}

void MerchantValidationEvent::SetMethodName(const nsAString& aMethodName) {
  mMethodName.Assign(aMethodName);
}

MerchantValidationEvent::~MerchantValidationEvent() = default;

JSObject* MerchantValidationEvent::WrapObjectInternal(
    JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
  return MerchantValidationEvent_Binding::Wrap(aCx, this, aGivenProto);
}

}  // namespace mozilla::dom