1 | /* |
2 | * Copyright (C) 2015-2019 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 "MessageSender.h" |
32 | #include "PaymentAuthorizationPresenter.h" |
33 | #include <WebCore/PageIdentifier.h> |
34 | #include <WebCore/PaymentHeaders.h> |
35 | #include <wtf/Forward.h> |
36 | #include <wtf/RetainPtr.h> |
37 | #include <wtf/WeakPtr.h> |
38 | #include <wtf/WorkQueue.h> |
39 | |
40 | #if USE(APPLE_INTERNAL_SDK) |
41 | #include <WebKitAdditions/WebPaymentCoordinatorProxyAdditions.h> |
42 | #endif |
43 | |
44 | namespace IPC { |
45 | class Connection; |
46 | } |
47 | |
48 | namespace PAL { |
49 | class SessionID; |
50 | } |
51 | |
52 | namespace WebCore { |
53 | enum class PaymentAuthorizationStatus; |
54 | class Payment; |
55 | class PaymentContact; |
56 | class PaymentMerchantSession; |
57 | class PaymentMethod; |
58 | } |
59 | |
60 | OBJC_CLASS NSObject; |
61 | OBJC_CLASS NSWindow; |
62 | OBJC_CLASS PKPaymentAuthorizationViewController; |
63 | OBJC_CLASS PKPaymentRequest; |
64 | OBJC_CLASS UIViewController; |
65 | |
66 | namespace WebKit { |
67 | |
68 | class WebPageProxy; |
69 | |
70 | class WebPaymentCoordinatorProxy |
71 | : private IPC::MessageReceiver |
72 | , private IPC::MessageSender |
73 | , public CanMakeWeakPtr<WebPaymentCoordinatorProxy> |
74 | , public PaymentAuthorizationPresenter::Client { |
75 | public: |
76 | struct Client { |
77 | virtual ~Client() = default; |
78 | |
79 | virtual IPC::Connection* paymentCoordinatorConnection(const WebPaymentCoordinatorProxy&) = 0; |
80 | virtual const String& paymentCoordinatorBoundInterfaceIdentifier(const WebPaymentCoordinatorProxy&, PAL::SessionID) = 0; |
81 | virtual const String& paymentCoordinatorSourceApplicationBundleIdentifier(const WebPaymentCoordinatorProxy&, PAL::SessionID) = 0; |
82 | virtual const String& paymentCoordinatorSourceApplicationSecondaryIdentifier(const WebPaymentCoordinatorProxy&, PAL::SessionID) = 0; |
83 | virtual void paymentCoordinatorAddMessageReceiver(WebPaymentCoordinatorProxy&, const IPC::StringReference&, IPC::MessageReceiver&) = 0; |
84 | virtual void paymentCoordinatorRemoveMessageReceiver(WebPaymentCoordinatorProxy&, const IPC::StringReference&) = 0; |
85 | #if PLATFORM(IOS_FAMILY) |
86 | virtual UIViewController *paymentCoordinatorPresentingViewController(const WebPaymentCoordinatorProxy&) = 0; |
87 | virtual const String& paymentCoordinatorCTDataConnectionServiceType(const WebPaymentCoordinatorProxy&, PAL::SessionID) = 0; |
88 | virtual std::unique_ptr<PaymentAuthorizationPresenter> paymentCoordinatorAuthorizationPresenter(WebPaymentCoordinatorProxy&, PKPaymentRequest *) = 0; |
89 | #endif |
90 | #if PLATFORM(MAC) |
91 | virtual NSWindow *paymentCoordinatorPresentingWindow(const WebPaymentCoordinatorProxy&) = 0; |
92 | #endif |
93 | }; |
94 | |
95 | friend class NetworkConnectionToWebProcess; |
96 | explicit WebPaymentCoordinatorProxy(Client&); |
97 | ~WebPaymentCoordinatorProxy(); |
98 | |
99 | private: |
100 | // IPC::MessageReceiver |
101 | void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override; |
102 | void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override; |
103 | |
104 | // IPC::MessageSender |
105 | IPC::Connection* messageSenderConnection() const final; |
106 | uint64_t messageSenderDestinationID() const final; |
107 | |
108 | // PaymentAuthorizationPresenter::Client |
109 | void presenterDidAuthorizePayment(PaymentAuthorizationPresenter&, const WebCore::Payment&) final; |
110 | void presenterDidFinish(PaymentAuthorizationPresenter&, bool didReachFinalState) final; |
111 | void presenterDidSelectPaymentMethod(PaymentAuthorizationPresenter&, const WebCore::PaymentMethod&) final; |
112 | void presenterDidSelectShippingContact(PaymentAuthorizationPresenter&, const WebCore::PaymentContact&) final; |
113 | void presenterDidSelectShippingMethod(PaymentAuthorizationPresenter&, const WebCore::ApplePaySessionPaymentRequest::ShippingMethod&) final; |
114 | void presenterWillValidateMerchant(PaymentAuthorizationPresenter&, const URL&) final; |
115 | |
116 | // Message handlers |
117 | void canMakePayments(CompletionHandler<void(bool)>&&); |
118 | void canMakePaymentsWithActiveCard(const String& merchantIdentifier, const String& domainName, PAL::SessionID, CompletionHandler<void(bool)>&&); |
119 | void openPaymentSetup(const String& merchantIdentifier, const String& domainName, CompletionHandler<void(bool)>&&); |
120 | void showPaymentUI(WebCore::PageIdentifier destinationID, PAL::SessionID, const String& originatingURLString, const Vector<String>& linkIconURLStrings, const WebCore::ApplePaySessionPaymentRequest&, CompletionHandler<void(bool)>&&); |
121 | void completeMerchantValidation(const WebCore::PaymentMerchantSession&); |
122 | void completeShippingMethodSelection(const Optional<WebCore::ShippingMethodUpdate>&); |
123 | void completeShippingContactSelection(const Optional<WebCore::ShippingContactUpdate>&); |
124 | void completePaymentMethodSelection(const Optional<WebCore::PaymentMethodUpdate>&); |
125 | void completePaymentSession(const Optional<WebCore::PaymentAuthorizationResult>&); |
126 | void abortPaymentSession(); |
127 | void cancelPaymentSession(); |
128 | |
129 | bool canBegin() const; |
130 | bool canCancel() const; |
131 | bool canCompletePayment() const; |
132 | bool canAbort() const; |
133 | |
134 | void didCancelPaymentSession(); |
135 | void didReachFinalState(); |
136 | void hidePaymentUI(); |
137 | |
138 | void platformCanMakePayments(CompletionHandler<void(bool)>&&); |
139 | void platformCanMakePaymentsWithActiveCard(const String& merchantIdentifier, const String& domainName, PAL::SessionID, WTF::Function<void(bool)>&& completionHandler); |
140 | void platformOpenPaymentSetup(const String& merchantIdentifier, const String& domainName, WTF::Function<void(bool)>&& completionHandler); |
141 | void platformShowPaymentUI(const URL& originatingURL, const Vector<URL>& linkIconURLs, PAL::SessionID, const WebCore::ApplePaySessionPaymentRequest&, CompletionHandler<void(bool)>&&); |
142 | void platformCompleteMerchantValidation(const WebCore::PaymentMerchantSession&); |
143 | void platformCompleteShippingMethodSelection(const Optional<WebCore::ShippingMethodUpdate>&); |
144 | void platformCompleteShippingContactSelection(const Optional<WebCore::ShippingContactUpdate>&); |
145 | void platformCompletePaymentMethodSelection(const Optional<WebCore::PaymentMethodUpdate>&); |
146 | void platformCompletePaymentSession(const Optional<WebCore::PaymentAuthorizationResult>&); |
147 | #if PLATFORM(COCOA) |
148 | RetainPtr<PKPaymentRequest> platformPaymentRequest(const URL& originatingURL, const Vector<URL>& linkIconURLs, PAL::SessionID, const WebCore::ApplePaySessionPaymentRequest&); |
149 | #endif |
150 | |
151 | Client& m_client; |
152 | Optional<WebCore::PageIdentifier> m_destinationID; |
153 | |
154 | enum class State { |
155 | // Idle - Nothing's happening. |
156 | Idle, |
157 | |
158 | // Activating - Waiting to show the payment UI. |
159 | Activating, |
160 | |
161 | // Active - Showing payment UI. |
162 | Active, |
163 | |
164 | // Authorized - Dispatching the authorized event and waiting for the paymentSessionCompleted message. |
165 | Authorized, |
166 | |
167 | // ShippingMethodSelected - Dispatching the shippingmethodselected event and waiting for a reply. |
168 | ShippingMethodSelected, |
169 | |
170 | // ShippingContactSelected - Dispatching the shippingcontactselected event and waiting for a reply. |
171 | ShippingContactSelected, |
172 | |
173 | // PaymentMethodSelected - Dispatching the paymentmethodselected event and waiting for a reply. |
174 | PaymentMethodSelected, |
175 | } m_state { State::Idle }; |
176 | |
177 | enum class MerchantValidationState { |
178 | // Idle - Nothing's happening. |
179 | Idle, |
180 | |
181 | // Validating - Dispatching the validatemerchant event and waiting for a reply. |
182 | Validating, |
183 | |
184 | // ValidationComplete - A merchant session has been sent along to PassKit. |
185 | ValidationComplete |
186 | } m_merchantValidationState { MerchantValidationState::Idle }; |
187 | |
188 | std::unique_ptr<PaymentAuthorizationPresenter> m_authorizationPresenter; |
189 | Ref<WorkQueue> m_canMakePaymentsQueue; |
190 | |
191 | #if PLATFORM(MAC) |
192 | uint64_t m_showPaymentUIRequestSeed { 0 }; |
193 | RetainPtr<NSWindow> m_sheetWindow; |
194 | RetainPtr<NSObject> m_sheetWindowWillCloseObserver; |
195 | #endif |
196 | |
197 | #if defined(WEBPAYMENTCOORDINATORPROXY_ADDITIONS) |
198 | WEBPAYMENTCOORDINATORPROXY_ADDITIONS |
199 | #undef WEBPAYMENTCOORDINATORPROXY_ADDITIONS |
200 | #else |
201 | void finishConstruction(WebPaymentCoordinatorProxy&) { } |
202 | #endif |
203 | }; |
204 | |
205 | } |
206 | |
207 | #endif |
208 | |