fb74d30ff95f3f72a554aa1d0a1e351797f2e844
[WebKit-https.git] / Source / WebKit / WebProcess / ApplePay / WebPaymentCoordinator.h
1 /*
2  * Copyright (C) 2015-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 #pragma once
27
28 #if ENABLE(APPLE_PAY)
29
30 #include "MessageReceiver.h"
31 #include <WebCore/PaymentCoordinatorClient.h>
32 #include <WebCore/PaymentHeaders.h>
33 #include <wtf/Forward.h>
34 #include <wtf/HashMap.h>
35 #include <wtf/HashSet.h>
36 #include <wtf/text/StringHash.h>
37
38 namespace IPC {
39 class DataReference;
40 }
41
42 namespace WebCore {
43 class PaymentCoordinator;
44 class PaymentContact;
45 }
46
47 namespace WebKit {
48
49 class WebPage;
50
51 class WebPaymentCoordinator final : public WebCore::PaymentCoordinatorClient, private IPC::MessageReceiver {
52 public:
53     explicit WebPaymentCoordinator(WebPage&);
54     ~WebPaymentCoordinator();
55
56 private:
57     // WebCore::PaymentCoordinatorClient.
58     bool supportsVersion(unsigned version) override;
59     std::optional<String> validatedPaymentNetwork(const String&) override;
60     bool canMakePayments() override;
61     void canMakePaymentsWithActiveCard(const String& merchantIdentifier, const String& domainName, WTF::Function<void (bool)>&& completionHandler) override;
62     void openPaymentSetup(const String& merchantIdentifier, const String& domainName, WTF::Function<void (bool)>&& completionHandler) override;
63     bool showPaymentUI(const URL& originatingURL, const Vector<URL>& linkIconURLs, const WebCore::ApplePaySessionPaymentRequest&) override;
64     void completeMerchantValidation(const WebCore::PaymentMerchantSession&) override;
65     void completeShippingMethodSelection(std::optional<WebCore::ShippingMethodUpdate>&&) override;
66     void completeShippingContactSelection(std::optional<WebCore::ShippingContactUpdate>&&) override;
67     void completePaymentMethodSelection(std::optional<WebCore::PaymentMethodUpdate>&&) override;
68     void completePaymentSession(std::optional<WebCore::PaymentAuthorizationResult>&&) override;
69
70     void abortPaymentSession() override;
71     void cancelPaymentSession() override;
72
73     void paymentCoordinatorDestroyed() override;
74
75     // IPC::MessageReceiver.
76     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
77
78     // Message handlers.
79     void validateMerchant(const String& validationURLString);
80     void didAuthorizePayment(const WebCore::Payment&);
81     void didSelectShippingMethod(const WebCore::ApplePaySessionPaymentRequest::ShippingMethod&);
82     void didSelectShippingContact(const WebCore::PaymentContact&);
83     void didSelectPaymentMethod(const WebCore::PaymentMethod&);
84     void didCancelPaymentSession();
85     void canMakePaymentsWithActiveCardReply(uint64_t requestID, bool canMakePayments);
86     void openPaymentSetupReply(uint64_t requestID, bool result);
87
88     WebCore::PaymentCoordinator& paymentCoordinator();
89     
90     using AvailablePaymentNetworksSet = HashSet<String, ASCIICaseInsensitiveHash>;
91     const AvailablePaymentNetworksSet& availablePaymentNetworks();
92
93     WebPage& m_webPage;
94
95     HashMap<uint64_t, WTF::Function<void (bool)>> m_pendingCanMakePaymentsWithActiveCardCallbacks;
96     HashMap<uint64_t, WTF::Function<void (bool)>> m_pendingOpenPaymentSetupCallbacks;
97
98     std::optional<AvailablePaymentNetworksSet> m_availablePaymentNetworks;
99
100 #if USE(APPLE_INTERNAL_SDK)
101 #import <WebKitAdditions/WebPaymentCoordinatorAdditions.h>
102 #endif
103 };
104
105 }
106 #endif