1/*
2 * Copyright (C) 2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "MerchantValidationEvent.h"
28
29#if ENABLE(PAYMENT_REQUEST)
30
31#include "Document.h"
32#include "PaymentRequest.h"
33
34namespace WebCore {
35
36Ref<MerchantValidationEvent> MerchantValidationEvent::create(const AtomString& type, const String& methodName, URL&& validationURL)
37{
38 return adoptRef(*new MerchantValidationEvent(type, methodName, WTFMove(validationURL)));
39}
40
41ExceptionOr<Ref<MerchantValidationEvent>> MerchantValidationEvent::create(Document& document, const AtomString& type, Init&& eventInit)
42{
43 URL validationURL { document.url(), eventInit.validationURL };
44 if (!validationURL.isValid())
45 return Exception { TypeError };
46
47 auto methodName = WTFMove(eventInit.methodName);
48 if (!methodName.isEmpty()) {
49 auto validatedMethodName = convertAndValidatePaymentMethodIdentifier(methodName);
50 if (!validatedMethodName)
51 return Exception { RangeError, makeString('"', methodName, "\" is an invalid payment method identifier.") };
52 }
53
54 return adoptRef(*new MerchantValidationEvent(type, WTFMove(methodName), WTFMove(validationURL), WTFMove(eventInit)));
55}
56
57MerchantValidationEvent::MerchantValidationEvent(const AtomString& type, const String& methodName, URL&& validationURL)
58 : Event { type, Event::CanBubble::No, Event::IsCancelable::No }
59 , m_methodName { methodName }
60 , m_validationURL { WTFMove(validationURL) }
61{
62 ASSERT(isTrusted());
63 ASSERT(m_validationURL.isValid());
64}
65
66MerchantValidationEvent::MerchantValidationEvent(const AtomString& type, String&& methodName, URL&& validationURL, Init&& eventInit)
67 : Event { type, WTFMove(eventInit), IsTrusted::No }
68 , m_methodName { WTFMove(methodName) }
69 , m_validationURL { WTFMove(validationURL) }
70{
71 ASSERT(!isTrusted());
72 ASSERT(m_validationURL.isValid());
73}
74
75EventInterface MerchantValidationEvent::eventInterface() const
76{
77 return MerchantValidationEventInterfaceType;
78}
79
80ExceptionOr<void> MerchantValidationEvent::complete(Ref<DOMPromise>&& merchantSessionPromise)
81{
82 if (!isTrusted())
83 return Exception { InvalidStateError };
84
85 if (m_isCompleted)
86 return Exception { InvalidStateError };
87
88 auto exception = downcast<PaymentRequest>(target())->completeMerchantValidation(*this, WTFMove(merchantSessionPromise));
89 if (exception.hasException())
90 return exception.releaseException();
91
92 m_isCompleted = true;
93 return { };
94}
95
96} // namespace WebCore
97
98#endif // ENABLE(PAYMENT_REQUEST)
99