Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebKit / UIProcess / ApplePay / ios / WebPaymentCoordinatorProxyIOS.mm
1 /*
2  * Copyright (C) 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 #import "config.h"
27 #import "WebPaymentCoordinatorProxy.h"
28
29 #if PLATFORM(IOS_FAMILY) && ENABLE(APPLE_PAY)
30
31 #import "APIUIClient.h"
32 #import "WebPageProxy.h"
33 #import "WebPaymentCoordinatorProxyCocoa.h"
34 #import <PassKit/PassKit.h>
35 #import <UIKit/UIViewController.h>
36 #import <WebCore/PaymentAuthorizationStatus.h>
37 #import <pal/cocoa/PassKitSoftLink.h>
38
39 namespace WebKit {
40
41 void WebPaymentCoordinatorProxy::platformShowPaymentUI(const URL& originatingURL, const Vector<URL>& linkIconURLStrings, const WebCore::ApplePaySessionPaymentRequest& request, WTF::Function<void(bool)>&& completionHandler)
42 {
43     UIViewController *presentingViewController = m_webPageProxy.uiClient().presentingViewController();
44
45     if (!presentingViewController) {
46         completionHandler(false);
47         return;
48     }
49
50     ASSERT(!m_paymentAuthorizationViewController);
51
52     auto paymentRequest = toPKPaymentRequest(m_webPageProxy, originatingURL, linkIconURLStrings, request);
53
54     m_paymentAuthorizationViewController = adoptNS([PAL::allocPKPaymentAuthorizationViewControllerInstance() initWithPaymentRequest:paymentRequest.get()]);
55     if (!m_paymentAuthorizationViewController) {
56         completionHandler(false);
57         return;
58     }
59
60     m_paymentAuthorizationViewControllerDelegate = adoptNS([[WKPaymentAuthorizationViewControllerDelegate alloc] initWithPaymentCoordinatorProxy:*this]);
61     m_paymentAuthorizationViewControllerDelegate->_paymentSummaryItems = [paymentRequest paymentSummaryItems];
62     m_paymentAuthorizationViewControllerDelegate->_shippingMethods = [paymentRequest shippingMethods];
63
64     [m_paymentAuthorizationViewController setDelegate:m_paymentAuthorizationViewControllerDelegate.get()];
65     [m_paymentAuthorizationViewController setPrivateDelegate:m_paymentAuthorizationViewControllerDelegate.get()];
66
67     [presentingViewController presentViewController:m_paymentAuthorizationViewController.get() animated:YES completion:nullptr];
68
69     completionHandler(true);
70 }
71
72 void WebPaymentCoordinatorProxy::hidePaymentUI()
73 {
74     ASSERT(m_paymentAuthorizationViewController);
75     ASSERT(m_paymentAuthorizationViewControllerDelegate);
76
77     [[m_paymentAuthorizationViewController presentingViewController] dismissViewControllerAnimated:YES completion:nullptr];
78     [m_paymentAuthorizationViewController setDelegate:nil];
79     [m_paymentAuthorizationViewController setPrivateDelegate:nil];
80     m_paymentAuthorizationViewController = nullptr;
81
82     [m_paymentAuthorizationViewControllerDelegate invalidate];
83     m_paymentAuthorizationViewControllerDelegate = nullptr;
84 }
85
86 }
87
88 #endif