[WTF] Import std::optional reference implementation as WTF::Optional
[WebKit-https.git] / Source / WebKit2 / WebProcess / ApplePay / WebPaymentCoordinator.cpp
1 /*
2  * Copyright (C) 2015, 2016 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 "WebPage.h"
33 #include "WebPaymentCoordinatorMessages.h"
34 #include "WebPaymentCoordinatorProxyMessages.h"
35 #include "WebProcess.h"
36 #include <WebCore/MainFrame.h>
37 #include <WebCore/PaymentCoordinator.h>
38 #include <WebCore/URL.h>
39
40 namespace WebKit {
41
42 WebPaymentCoordinator::WebPaymentCoordinator(WebPage& webPage)
43     : m_webPage(webPage)
44 {
45     WebProcess::singleton().addMessageReceiver(Messages::WebPaymentCoordinator::messageReceiverName(), m_webPage.pageID(), *this);
46 }
47
48 WebPaymentCoordinator::~WebPaymentCoordinator()
49 {
50     WebProcess::singleton().removeMessageReceiver(*this);
51 }
52
53 bool WebPaymentCoordinator::supportsVersion(unsigned version)
54 {
55     ASSERT(version > 0);
56
57     if (version <= 2)
58         return true;
59
60     return false;
61 }
62
63 bool WebPaymentCoordinator::canMakePayments()
64 {
65     bool canMakePayments;
66     if (!m_webPage.sendSync(Messages::WebPaymentCoordinatorProxy::CanMakePayments(), Messages::WebPaymentCoordinatorProxy::CanMakePayments::Reply(canMakePayments)))
67         return false;
68
69     return canMakePayments;
70 }
71
72 static uint64_t generateCanMakePaymentsWithActiveCardReplyID()
73 {
74     static uint64_t canMakePaymentsWithActiveCardReplyID;
75
76     return ++canMakePaymentsWithActiveCardReplyID;
77 }
78
79 void WebPaymentCoordinator::canMakePaymentsWithActiveCard(const String& merchantIdentifier, const String& domainName, std::function<void (bool)> completionHandler)
80 {
81     auto replyID = generateCanMakePaymentsWithActiveCardReplyID();
82
83     m_pendingCanMakePaymentsWithActiveCardCallbacks.add(replyID, WTFMove(completionHandler));
84     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CanMakePaymentsWithActiveCard(merchantIdentifier, domainName, replyID));
85 }
86
87 static uint64_t generateOpenPaymentSetupReplyID()
88 {
89     static uint64_t openPaymentSetupReplyID;
90
91     return ++openPaymentSetupReplyID;
92 }
93
94 void WebPaymentCoordinator::openPaymentSetup(const String& merchantIdentifier, const String& domainName, std::function<void (bool)> completionHandler)
95 {
96     auto replyID = generateOpenPaymentSetupReplyID();
97
98     m_pendingOpenPaymentSetupCallbacks.add(replyID, WTFMove(completionHandler));
99     m_webPage.send(Messages::WebPaymentCoordinatorProxy::OpenPaymentSetup(merchantIdentifier, domainName, replyID));
100 }
101
102 bool WebPaymentCoordinator::showPaymentUI(const WebCore::URL& originatingURL, const Vector<WebCore::URL>& linkIconURLs, const WebCore::PaymentRequest& paymentRequest)
103 {
104     Vector<String> linkIconURLStrings;
105     for (const auto& linkIconURL : linkIconURLs)
106         linkIconURLStrings.append(linkIconURL.string());
107
108     bool result;
109     if (!m_webPage.sendSync(Messages::WebPaymentCoordinatorProxy::ShowPaymentUI(originatingURL.string(), linkIconURLStrings, paymentRequest), Messages::WebPaymentCoordinatorProxy::ShowPaymentUI::Reply(result)))
110         return false;
111
112     return result;
113 }
114
115 void WebPaymentCoordinator::completeMerchantValidation(const WebCore::PaymentMerchantSession& paymentMerchantSession)
116 {
117     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CompleteMerchantValidation(paymentMerchantSession));
118 }
119
120 void WebPaymentCoordinator::completeShippingMethodSelection(WebCore::PaymentAuthorizationStatus status, std::optional<WebCore::PaymentRequest::TotalAndLineItems> newTotalAndItems)
121 {
122     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CompleteShippingMethodSelection(static_cast<uint32_t>(status), newTotalAndItems));
123 }
124
125 void WebPaymentCoordinator::completeShippingContactSelection(WebCore::PaymentAuthorizationStatus status, const Vector<WebCore::PaymentRequest::ShippingMethod>& newShippingMethods, std::optional<WebCore::PaymentRequest::TotalAndLineItems> newTotalAndItems)
126 {
127     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CompleteShippingContactSelection(static_cast<uint32_t>(status), newShippingMethods, newTotalAndItems));
128 }
129
130 void WebPaymentCoordinator::completePaymentMethodSelection(std::optional<WebCore::PaymentRequest::TotalAndLineItems> newTotalAndItems)
131 {
132     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CompletePaymentMethodSelection(newTotalAndItems));
133 }
134
135 void WebPaymentCoordinator::completePaymentSession(WebCore::PaymentAuthorizationStatus status)
136 {
137     m_webPage.send(Messages::WebPaymentCoordinatorProxy::CompletePaymentSession(static_cast<uint32_t>(status)));
138 }
139
140 void WebPaymentCoordinator::abortPaymentSession()
141 {
142     m_webPage.send(Messages::WebPaymentCoordinatorProxy::AbortPaymentSession());
143 }
144
145 void WebPaymentCoordinator::paymentCoordinatorDestroyed()
146 {
147     delete this;
148 }
149
150 void WebPaymentCoordinator::validateMerchant(const String& validationURLString)
151 {
152     paymentCoordinator().validateMerchant(WebCore::URL(WebCore::URL(), validationURLString));
153 }
154
155 void WebPaymentCoordinator::didAuthorizePayment(const WebCore::Payment& payment)
156 {
157     paymentCoordinator().didAuthorizePayment(payment);
158 }
159
160 void WebPaymentCoordinator::didSelectShippingMethod(const WebCore::PaymentRequest::ShippingMethod& shippingMethod)
161 {
162     paymentCoordinator().didSelectShippingMethod(shippingMethod);
163 }
164
165 void WebPaymentCoordinator::didSelectShippingContact(const WebCore::PaymentContact& shippingContact)
166 {
167     paymentCoordinator().didSelectShippingContact(shippingContact);
168 }
169
170 void WebPaymentCoordinator::didSelectPaymentMethod(const WebCore::PaymentMethod& paymentMethod)
171 {
172     paymentCoordinator().didSelectPaymentMethod(paymentMethod);
173 }
174
175 void WebPaymentCoordinator::didCancelPayment()
176 {
177     paymentCoordinator().didCancelPayment();
178 }
179
180 void WebPaymentCoordinator::canMakePaymentsWithActiveCardReply(uint64_t requestID, bool canMakePayments)
181 {
182     auto callback = m_pendingCanMakePaymentsWithActiveCardCallbacks.take(requestID);
183     callback(canMakePayments);
184 }
185
186 void WebPaymentCoordinator::openPaymentSetupReply(uint64_t requestID, bool result)
187 {
188     auto callback = m_pendingOpenPaymentSetupCallbacks.take(requestID);
189     callback(result);
190 }
191
192 WebCore::PaymentCoordinator& WebPaymentCoordinator::paymentCoordinator()
193 {
194     return m_webPage.mainFrame()->paymentCoordinator();
195 }
196
197 }
198
199 #endif