Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebKit / UIProcess / ApplePay / WebPaymentCoordinatorProxy.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/PaymentHeaders.h>
32 #include <wtf/Forward.h>
33 #include <wtf/RetainPtr.h>
34 #include <wtf/WeakPtr.h>
35
36 #if USE(APPLE_INTERNAL_SDK)
37 #import <WebKitAdditions/WebPaymentCoordinatorProxyAdditions.h>
38 #endif
39
40 namespace IPC {
41 class DataReference;
42 }
43
44 namespace WebCore {
45 enum class PaymentAuthorizationStatus;
46 class Payment;
47 class PaymentContact;
48 class PaymentMerchantSession;
49 class PaymentMethod;
50 }
51
52 OBJC_CLASS NSObject;
53 OBJC_CLASS NSWindow;
54 OBJC_CLASS PKPaymentAuthorizationViewController;
55 OBJC_CLASS WKPaymentAuthorizationViewControllerDelegate;
56
57 namespace WebKit {
58
59 class WebPageProxy;
60
61 class WebPaymentCoordinatorProxy : private IPC::MessageReceiver, public CanMakeWeakPtr<WebPaymentCoordinatorProxy> {
62 public:
63     explicit WebPaymentCoordinatorProxy(WebPageProxy&);
64     ~WebPaymentCoordinatorProxy();
65
66     void didCancelPaymentSession();
67     void validateMerchant(const URL&);
68     void didAuthorizePayment(const WebCore::Payment&);
69     void didSelectShippingMethod(const WebCore::ApplePaySessionPaymentRequest::ShippingMethod&);
70     void didSelectShippingContact(const WebCore::PaymentContact&);
71     void didSelectPaymentMethod(const WebCore::PaymentMethod&);
72
73     void hidePaymentUI();
74
75 private:
76     // IPC::MessageReceiver.
77     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
78     void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
79
80     // Message handlers.
81     void availablePaymentNetworks(Vector<String>&);
82     void canMakePayments(bool& reply);
83     void canMakePaymentsWithActiveCard(const String& merchantIdentifier, const String& domainName, uint64_t requestID);
84     void openPaymentSetup(const String& merchantIdentifier, const String& domainName, uint64_t requestID);
85     void showPaymentUI(const String& originatingURLString, const Vector<String>& linkIconURLStrings, const WebCore::ApplePaySessionPaymentRequest&, bool& result);
86     void completeMerchantValidation(const WebCore::PaymentMerchantSession&);
87     void completeShippingMethodSelection(const std::optional<WebCore::ShippingMethodUpdate>&);
88     void completeShippingContactSelection(const std::optional<WebCore::ShippingContactUpdate>&);
89     void completePaymentMethodSelection(const std::optional<WebCore::PaymentMethodUpdate>&);
90     void completePaymentSession(const std::optional<WebCore::PaymentAuthorizationResult>&);
91     void abortPaymentSession();
92     void cancelPaymentSession();
93
94     bool canBegin() const;
95     bool canCancel() const;
96     bool canCompletePayment() const;
97     bool canAbort() const;
98
99     void didReachFinalState();
100
101     Vector<String> platformAvailablePaymentNetworks();
102     bool platformCanMakePayments();
103     void platformCanMakePaymentsWithActiveCard(const String& merchantIdentifier, const String& domainName, WTF::Function<void (bool)>&& completionHandler);
104     void platformOpenPaymentSetup(const String& merchantIdentifier, const String& domainName, WTF::Function<void (bool)>&& completionHandler);
105     void platformShowPaymentUI(const URL& originatingURL, const Vector<URL>& linkIconURLs, const WebCore::ApplePaySessionPaymentRequest&, WTF::Function<void(bool)>&& completionHandler);
106     void platformCompleteMerchantValidation(const WebCore::PaymentMerchantSession&);
107     void platformCompleteShippingMethodSelection(const std::optional<WebCore::ShippingMethodUpdate>&);
108     void platformCompleteShippingContactSelection(const std::optional<WebCore::ShippingContactUpdate>&);
109     void platformCompletePaymentMethodSelection(const std::optional<WebCore::PaymentMethodUpdate>&);
110     void platformCompletePaymentSession(const std::optional<WebCore::PaymentAuthorizationResult>&);
111
112     WebPageProxy& m_webPageProxy;
113
114     enum class State {
115         // Idle - Nothing's happening.
116         Idle,
117
118         // Activating - Waiting to show the payment UI.
119         Activating,
120
121         // Active - Showing payment UI.
122         Active,
123
124         // Authorized - Dispatching the authorized event and waiting for the paymentSessionCompleted message.
125         Authorized,
126
127         // ShippingMethodSelected - Dispatching the shippingmethodselected event and waiting for a reply.
128         ShippingMethodSelected,
129
130         // ShippingContactSelected - Dispatching the shippingcontactselected event and waiting for a reply.
131         ShippingContactSelected,
132
133         // PaymentMethodSelected - Dispatching the paymentmethodselected event and waiting for a reply.
134         PaymentMethodSelected,
135     } m_state;
136
137     enum class MerchantValidationState {
138         // Idle - Nothing's happening.
139         Idle,
140
141         // Validating - Dispatching the validatemerchant event and waiting for a reply.
142         Validating,
143
144         // ValidationComplete - A merchant session has been sent along to PassKit.
145         ValidationComplete
146     } m_merchantValidationState;
147
148     RetainPtr<PKPaymentAuthorizationViewController> m_paymentAuthorizationViewController;
149     RetainPtr<WKPaymentAuthorizationViewControllerDelegate> m_paymentAuthorizationViewControllerDelegate;
150
151 #if PLATFORM(MAC)
152     uint64_t m_showPaymentUIRequestSeed { 0 };
153     RetainPtr<NSWindow> m_sheetWindow;
154     RetainPtr<NSObject *> m_sheetWindowWillCloseObserver;
155 #endif
156     
157 #if defined(WEBPAYMENTCOORDINATORPROXY_ADDITIONS)
158 WEBPAYMENTCOORDINATORPROXY_ADDITIONS
159 #undef WEBPAYMENTCOORDINATORPROXY_ADDITIONS
160 #else
161     void finishConstruction(WebPaymentCoordinatorProxy&) { }
162 #endif
163 };
164
165 }
166
167 #endif