[Apple Pay] Introduce new values for -apple-pay-button-type
[WebKit-https.git] / Source / WebCore / rendering / RenderThemeCocoa.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 #include "config.h"
27 #include "RenderThemeCocoa.h"
28
29 #if ENABLE(APPLE_PAY)
30
31 #include "RenderElement.h"
32 #include "RenderStyle.h"
33 #include "TranslateTransformOperation.h"
34 #include <pal/spi/cocoa/PassKitSPI.h>
35 #include <wtf/SoftLinking.h>
36
37 #if PLATFORM(MAC)
38 SOFT_LINK_PRIVATE_FRAMEWORK(PassKit);
39 #else
40 SOFT_LINK_FRAMEWORK(PassKit);
41 #endif
42
43 SOFT_LINK_MAY_FAIL(PassKit, PKDrawApplePayButton, void, (CGContextRef context, CGRect drawRect, CGFloat scale, PKPaymentButtonType type, PKPaymentButtonStyle style, NSString *languageCode), (context, drawRect, scale, type, style, languageCode));
44
45 #endif // ENABLE(APPLE_PAY)
46
47 #if ENABLE(VIDEO)
48 #include "LocalizedStrings.h"
49 #include <wtf/BlockObjCExceptions.h>
50 #endif
51
52 namespace WebCore {
53
54 #if ENABLE(APPLE_PAY)
55
56 static const auto applePayButtonMinimumWidth = 140;
57 static const auto applePayButtonPlainMinimumWidth = 100;
58 static const auto applePayButtonMinimumHeight = 30;
59
60 void RenderThemeCocoa::adjustApplePayButtonStyle(StyleResolver&, RenderStyle& style, const Element*) const
61 {
62     if (style.applePayButtonType() == ApplePayButtonType::Plain)
63         style.setMinWidth(Length(applePayButtonPlainMinimumWidth, Fixed));
64     else
65         style.setMinWidth(Length(applePayButtonMinimumWidth, Fixed));
66     style.setMinHeight(Length(applePayButtonMinimumHeight, Fixed));
67 }
68
69 static PKPaymentButtonStyle toPKPaymentButtonStyle(ApplePayButtonStyle style)
70 {
71     switch (style) {
72     case ApplePayButtonStyle::White:
73         return PKPaymentButtonStyleWhite;
74     case ApplePayButtonStyle::WhiteOutline:
75         return PKPaymentButtonStyleWhiteOutline;
76     case ApplePayButtonStyle::Black:
77         return PKPaymentButtonStyleBlack;
78     }
79 }
80
81 static PKPaymentButtonType toPKPaymentButtonType(ApplePayButtonType type)
82 {
83     switch (type) {
84     case ApplePayButtonType::Plain:
85         return PKPaymentButtonTypePlain;
86     case ApplePayButtonType::Buy:
87         return PKPaymentButtonTypeBuy;
88     case ApplePayButtonType::SetUp:
89         return PKPaymentButtonTypeSetUp;
90     case ApplePayButtonType::InStore:
91         return PKPaymentButtonTypeInStore;
92     case ApplePayButtonType::Donate:
93         return PKPaymentButtonTypeDonate;
94 #if ENABLE(APPLE_PAY_SESSION_V4)
95     case ApplePayButtonType::Checkout:
96         return PKPaymentButtonTypeCheckout;
97     case ApplePayButtonType::Book:
98         return PKPaymentButtonTypeBook;
99     case ApplePayButtonType::Subscribe:
100         return PKPaymentButtonTypeSubscribe;
101 #endif
102     }
103 }
104
105 bool RenderThemeCocoa::paintApplePayButton(const RenderObject& renderer, const PaintInfo& paintInfo, const IntRect& paintRect)
106 {
107     if (!canLoadPKDrawApplePayButton())
108         return false;
109
110     GraphicsContextStateSaver stateSaver(paintInfo.context());
111
112     paintInfo.context().setShouldSmoothFonts(true);
113     paintInfo.context().scale(FloatSize(1, -1));
114
115     PKDrawApplePayButton(paintInfo.context().platformContext(), CGRectMake(paintRect.x(), -paintRect.maxY(), paintRect.width(), paintRect.height()), 1.0, toPKPaymentButtonType(renderer.style().applePayButtonType()), toPKPaymentButtonStyle(renderer.style().applePayButtonStyle()), renderer.style().locale());
116
117     return false;
118 }
119
120 #endif // ENABLE(APPLE_PAY)
121
122 #if ENABLE(VIDEO)
123
124 String RenderThemeCocoa::mediaControlsFormattedStringForDuration(const double durationInSeconds)
125 {
126 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
127     if (!std::isfinite(durationInSeconds))
128         return WEB_UI_STRING("indefinite time", "accessibility help text for an indefinite media controller time value");
129
130     BEGIN_BLOCK_OBJC_EXCEPTIONS;
131     if (!m_durationFormatter) {
132         m_durationFormatter = adoptNS([NSDateComponentsFormatter new]);
133         m_durationFormatter.get().unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
134         m_durationFormatter.get().allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
135         m_durationFormatter.get().formattingContext = NSFormattingContextStandalone;
136         m_durationFormatter.get().maximumUnitCount = 2;
137     }
138     return [m_durationFormatter.get() stringFromTimeInterval:durationInSeconds];
139     END_BLOCK_OBJC_EXCEPTIONS;
140 #else
141     return emptyString();
142 #endif
143 }
144
145 #endif // ENABLE(VIDEO)
146
147 }