2018-09-12 22:56:00 +03:00
|
|
|
/* -*- 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"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2018-09-12 22:56:00 +03:00
|
|
|
#include "mozilla/dom/PaymentRequest.h"
|
|
|
|
#include "mozilla/dom/Location.h"
|
|
|
|
#include "mozilla/dom/URL.h"
|
2019-08-20 06:45:58 +03:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
2018-09-12 22:56:00 +03:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom {
|
2018-09-12 22:56:00 +03:00
|
|
|
|
2019-08-20 06:45:58 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(MerchantValidationEvent, Event,
|
|
|
|
mValidationURL, mRequest)
|
2018-09-12 22:56:00 +03:00
|
|
|
|
|
|
|
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);
|
2019-09-20 02:53:14 +03:00
|
|
|
e->init(aEventInitDict, aRv);
|
|
|
|
if (aRv.Failed()) {
|
2018-09-12 22:56:00 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
e->SetTrusted(trusted);
|
|
|
|
e->SetComposed(aEventInitDict.mComposed);
|
|
|
|
return e.forget();
|
|
|
|
}
|
|
|
|
|
2019-09-20 02:53:14 +03:00
|
|
|
void MerchantValidationEvent::init(
|
|
|
|
const MerchantValidationEventInit& aEventInitDict, ErrorResult& aRv) {
|
2018-09-13 21:06:00 +03:00
|
|
|
// Check methodName is valid
|
|
|
|
if (!aEventInitDict.mMethodName.IsEmpty()) {
|
2019-09-20 02:53:14 +03:00
|
|
|
PaymentRequest::IsValidPaymentMethodIdentifier(aEventInitDict.mMethodName,
|
|
|
|
aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
2018-09-13 21:06:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
SetMethodName(aEventInitDict.mMethodName);
|
2018-09-12 22:56:00 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(GetParentObject());
|
|
|
|
auto doc = window->GetExtantDoc();
|
|
|
|
if (!doc) {
|
2020-02-11 11:45:49 +03:00
|
|
|
aRv.ThrowAbortError("The owner document does not exist");
|
2019-09-20 02:53:14 +03:00
|
|
|
return;
|
2018-09-12 22:56:00 +03:00
|
|
|
}
|
2019-08-20 06:45:58 +03:00
|
|
|
|
2020-09-30 00:50:21 +03:00
|
|
|
Result<OwningNonNull<nsIURI>, nsresult> rv =
|
2019-09-20 02:53:14 +03:00
|
|
|
doc->ResolveWithBaseURI(aEventInitDict.mValidationURL);
|
|
|
|
if (rv.isErr()) {
|
2020-03-07 00:04:58 +03:00
|
|
|
aRv.ThrowTypeError("validationURL cannot be parsed");
|
2019-09-20 02:53:14 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
mValidationURL = rv.unwrap();
|
2018-09-12 22:56:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MerchantValidationEvent::MerchantValidationEvent(EventTarget* aOwner)
|
|
|
|
: Event(aOwner, nullptr, nullptr), mWaitForUpdate(false) {
|
|
|
|
MOZ_ASSERT(aOwner);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MerchantValidationEvent::ResolvedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) {
|
|
|
|
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
|
2018-09-13 21:06:00 +03:00
|
|
|
// conformance, which is why at this point we throw a
|
|
|
|
// NS_ERROR_DOM_NOT_SUPPORTED_ERR.
|
2018-09-12 22:56:00 +03:00
|
|
|
|
2020-02-11 11:45:49 +03:00
|
|
|
ErrorResult result;
|
|
|
|
result.ThrowNotSupportedError(
|
|
|
|
"complete() is not supported by Firefox currently");
|
|
|
|
mRequest->AbortUpdate(result);
|
2018-09-12 22:56:00 +03:00
|
|
|
mRequest->SetUpdating(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MerchantValidationEvent::RejectedCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue) {
|
|
|
|
MOZ_ASSERT(mRequest);
|
|
|
|
if (!mWaitForUpdate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mWaitForUpdate = false;
|
2020-02-11 11:45:49 +03:00
|
|
|
ErrorResult result;
|
|
|
|
result.ThrowAbortError(
|
|
|
|
"The promise for MerchantValidtaionEvent.complete() is rejected");
|
|
|
|
mRequest->AbortUpdate(result);
|
2018-09-12 22:56:00 +03:00
|
|
|
mRequest->SetUpdating(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MerchantValidationEvent::Complete(Promise& aPromise, ErrorResult& aRv) {
|
|
|
|
if (!IsTrusted()) {
|
2020-02-11 11:45:49 +03:00
|
|
|
aRv.ThrowInvalidStateError("Called on an untrusted event");
|
2018-09-12 22:56:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mRequest);
|
|
|
|
|
2020-02-11 11:45:49 +03:00
|
|
|
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");
|
2018-09-12 22:56:00 +03:00
|
|
|
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) {
|
2019-08-20 06:45:58 +03:00
|
|
|
nsAutoCString utf8href;
|
|
|
|
nsresult rv = mValidationURL->GetSpec(utf8href);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
Unused << rv;
|
|
|
|
aValidationURL.Assign(NS_ConvertUTF8toUTF16(utf8href));
|
2018-09-12 22:56:00 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 21:06:00 +03:00
|
|
|
void MerchantValidationEvent::GetMethodName(nsAString& aMethodName) {
|
|
|
|
aMethodName.Assign(mMethodName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MerchantValidationEvent::SetMethodName(const nsAString& aMethodName) {
|
|
|
|
mMethodName.Assign(aMethodName);
|
|
|
|
}
|
|
|
|
|
2019-08-20 06:45:58 +03:00
|
|
|
MerchantValidationEvent::~MerchantValidationEvent() = default;
|
2018-09-12 22:56:00 +03:00
|
|
|
|
|
|
|
JSObject* MerchantValidationEvent::WrapObjectInternal(
|
|
|
|
JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
|
|
|
|
return MerchantValidationEvent_Binding::Wrap(aCx, this, aGivenProto);
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom
|