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