209a85dea3b630d27c1c21251556dcc0c5a82b97
[WebKit-https.git] / Source / WebKit / WebProcess / ApplePay / WebPaymentCoordinator.cpp
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 #include "config.h"
27 #include "WebPaymentCoordinator.h"
28
29 #if ENABLE(APPLE_PAY)
30
31 #include "DataReference.h"
32 #include "WebCoreArgumentCoders.h"
33 #include "WebPage.h"
34 #include "WebPaymentCoordinatorMessages.h"
35 #include "WebPaymentCoordinatorProxyMessages.h"
36 #include "WebProcess.h"
37 #include <WebCore/Frame.h>
38 #include <WebCore/PaymentCoordinator.h>
39 #include <WebCore/URL.h>
40
41 namespace WebKit {
42
43 WebPaymentCoordinator::WebPaymentCoordinator(WebPage& webPage)
44     : m_webPage(webPage)
45 {
46     WebProcess::singleton().addMessageReceiver(Messages::WebPaymentCoordinator::messageReceiverName(), m_webPage.pageID(), *this);
47 }
48
49 WebPaymentCoordinator::~WebPaymentCoordinator()
50 {
51     WebProcess::singleton().removeMessageReceiver(*this);
52 }
53
54 bool WebPaymentCoordinator::supportsVersion(unsigned version)
55 {
56     ASSERT(version > 0);
57
58 #if !ENABLE(APPLE_PAY_SESSION_V3)
59     static const unsigned currentVersion = 2;
60 #elif !ENABLE(APPLE_PAY_SESSION_V4)
61     static const unsigned currentVersion = 3;
62 #else
63     static const unsigned currentVersion = 4;
64 #endif
65
66     return version <= currentVersion;
67 }
68
69 const WebPaymentCoordinator::AvailablePaymentNetworksSet& WebPaymentCoordinator::availablePaymentNetworks()
70 {
71     if (m_availablePaymentNetworks)
72         return *m_availablePaymentNetworks;
73
74     m_availablePaymentNetworks = WebPaymentCoordinator::AvailablePaymentNetworksSet();
75
76     Vector<String> availablePaymentNetworks;
77     using AvailablePaymentNetworksMessage = Messages::WebPaymentCoordinatorProxy::AvailablePaymentNetworks;
78     if (m_webPage.sendSync(AvailablePaymentNetworksMessage(), AvailablePaymentNetworksMessage::Reply(availablePaymentNetworks))) {
79         for (auto& network : availablePaymentNetworks)
80             m_availablePaymentNetworks->add(network);
81     }
82
83     return *m_availablePaymentNetworks;
84 }
85
86 std::optional<String> WebPaymentCoordinator::validatedPaymentNetwork(const String& paymentNetwork)
87 {
88     auto& paymentNetworks = availablePaymentNetworks();
89     auto result = paymentNetworks.find(paymentNetwork);
90     if (result == paymentNetworks.end())
91         return std::nullopt;
92     return *result;
93 }
94
95 bool WebPaymentCoordinator::canMakePayments()
96 {
97     bool canMakePayments;
98     if (!m_webPage.sendSync(Messages::WebPaymentCoordinatorProxy::CanMakePayments(), Messages::WebPaymentCoordinatorProxy::CanMakePayments::Reply(canMakePayments)))
99         return false;
100
101     return canMakePayments;
102 }
103
104 static uint64_t generateCanMakePaymentsWithActiveCardReplyID()
105 {
106     static uint64_t canMakePaymentsWithActiveCardReplyID;
107
108     return ++canMakePaymentsWithActiveCardReplyID;
109 }
110
111 void WebPaymentCoordinator::canMakePaymentsWithActiveCard(const String& merchantIdentifier, const String& domainName, WTF::Function<void (bool)>&& completionHandler)
112 {
113     auto replyID = generateCanMakePaymentsWithActiveCardReplyID();
114
115     m_pendingCanMakePaymentsWithActiveCardCallbacks.add(replyID, WTFMove(completionHandler));
116     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CanMakePaymentsWithActiveCard(merchantIdentifier, domainName, replyID));
117 }
118
119 static uint64_t generateOpenPaymentSetupReplyID()
120 {
121     static uint64_t openPaymentSetupReplyID;
122
123     return ++openPaymentSetupReplyID;
124 }
125
126 void WebPaymentCoordinator::openPaymentSetup(const String& merchantIdentifier, const String& domainName, WTF::Function<void (bool)>&& completionHandler)
127 {
128     auto replyID = generateOpenPaymentSetupReplyID();
129
130     m_pendingOpenPaymentSetupCallbacks.add(replyID, WTFMove(completionHandler));
131     m_webPage.send(Messages::WebPaymentCoordinatorProxy::OpenPaymentSetup(merchantIdentifier, domainName, replyID));
132 }
133
134 bool WebPaymentCoordinator::showPaymentUI(const WebCore::URL& originatingURL, const Vector<WebCore::URL>& linkIconURLs, const WebCore::ApplePaySessionPaymentRequest& paymentRequest)
135 {
136     Vector<String> linkIconURLStrings;
137     for (const auto& linkIconURL : linkIconURLs)
138         linkIconURLStrings.append(linkIconURL.string());
139
140     bool result;
141     if (!m_webPage.sendSync(Messages::WebPaymentCoordinatorProxy::ShowPaymentUI(originatingURL.string(), linkIconURLStrings, paymentRequest), Messages::WebPaymentCoordinatorProxy::ShowPaymentUI::Reply(result)))
142         return false;
143
144     return result;
145 }
146
147 void WebPaymentCoordinator::completeMerchantValidation(const WebCore::PaymentMerchantSession& paymentMerchantSession)
148 {
149     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CompleteMerchantValidation(paymentMerchantSession));
150 }
151
152 void WebPaymentCoordinator::completeShippingMethodSelection(std::optional<WebCore::ShippingMethodUpdate>&& update)
153 {
154     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CompleteShippingMethodSelection(update));
155 }
156
157 void WebPaymentCoordinator::completeShippingContactSelection(std::optional<WebCore::ShippingContactUpdate>&& update)
158 {
159     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CompleteShippingContactSelection(update));
160 }
161
162 void WebPaymentCoordinator::completePaymentMethodSelection(std::optional<WebCore::PaymentMethodUpdate>&& update)
163 {
164     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CompletePaymentMethodSelection(update));
165 }
166
167 void WebPaymentCoordinator::completePaymentSession(std::optional<WebCore::PaymentAuthorizationResult>&& result)
168 {
169     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CompletePaymentSession(result));
170 }
171
172 void WebPaymentCoordinator::abortPaymentSession()
173 {
174     m_webPage.send(Messages::WebPaymentCoordinatorProxy::AbortPaymentSession());
175 }
176
177 void WebPaymentCoordinator::cancelPaymentSession()
178 {
179     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CancelPaymentSession());
180 }
181
182 void WebPaymentCoordinator::paymentCoordinatorDestroyed()
183 {
184     delete this;
185 }
186
187 void WebPaymentCoordinator::validateMerchant(const String& validationURLString)
188 {
189     paymentCoordinator().validateMerchant(WebCore::URL(WebCore::URL(), validationURLString));
190 }
191
192 void WebPaymentCoordinator::didAuthorizePayment(const WebCore::Payment& payment)
193 {
194     paymentCoordinator().didAuthorizePayment(payment);
195 }
196
197 void WebPaymentCoordinator::didSelectShippingMethod(const WebCore::ApplePaySessionPaymentRequest::ShippingMethod& shippingMethod)
198 {
199     paymentCoordinator().didSelectShippingMethod(shippingMethod);
200 }
201
202 void WebPaymentCoordinator::didSelectShippingContact(const WebCore::PaymentContact& shippingContact)
203 {
204     paymentCoordinator().didSelectShippingContact(shippingContact);
205 }
206
207 void WebPaymentCoordinator::didSelectPaymentMethod(const WebCore::PaymentMethod& paymentMethod)
208 {
209     paymentCoordinator().didSelectPaymentMethod(paymentMethod);
210 }
211
212 void WebPaymentCoordinator::didCancelPaymentSession()
213 {
214     paymentCoordinator().didCancelPaymentSession();
215 }
216
217 void WebPaymentCoordinator::canMakePaymentsWithActiveCardReply(uint64_t requestID, bool canMakePayments)
218 {
219     auto callback = m_pendingCanMakePaymentsWithActiveCardCallbacks.take(requestID);
220     callback(canMakePayments);
221 }
222
223 void WebPaymentCoordinator::openPaymentSetupReply(uint64_t requestID, bool result)
224 {
225     auto callback = m_pendingOpenPaymentSetupCallbacks.take(requestID);
226     callback(result);
227 }
228
229 WebCore::PaymentCoordinator& WebPaymentCoordinator::paymentCoordinator()
230 {
231     return m_webPage.corePage()->paymentCoordinator();
232 }
233
234 }
235
236 #endif