[WTF] Import std::optional reference implementation as WTF::Optional
[WebKit-https.git] / Source / WebCore / Modules / applepay / cocoa / PaymentContactCocoa.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 "PaymentContact.h"
28
29 #if ENABLE(APPLE_PAY)
30
31 #import "JSMainThreadExecState.h"
32 #import "PassKitSPI.h"
33 #import "SoftLinking.h"
34 #import <Contacts/Contacts.h>
35 #import <runtime/JSONObject.h>
36
37 SOFT_LINK_FRAMEWORK(Contacts)
38 SOFT_LINK_CLASS(Contacts, CNMutablePostalAddress)
39 SOFT_LINK_CLASS(Contacts, CNPhoneNumber)
40
41 #if PLATFORM(MAC)
42 SOFT_LINK_PRIVATE_FRAMEWORK(PassKit)
43 #else
44 SOFT_LINK_FRAMEWORK(PassKit)
45 #endif
46
47 SOFT_LINK_CLASS(PassKit, PKContact)
48
49 namespace WebCore {
50
51 static bool isValidPaymentContactPropertyName(NSString* propertyName)
52 {
53     static NSSet *validPropertyNames = [[NSSet alloc] initWithObjects:@"familyName", @"givenName", @"emailAddress", @"phoneNumber", @"addressLines", @"locality", @"postalCode", @"administrativeArea", @"country", @"countryCode", nil ];
54
55     return [validPropertyNames containsObject:propertyName];
56 }
57
58 static RetainPtr<PKContact> fromDictionary(NSDictionary *dictionary, String& errorMessage)
59 {
60     for (NSString *propertyName in dictionary) {
61         if (!isValidPaymentContactPropertyName(propertyName)) {
62             errorMessage = makeString("\"" + String(propertyName), "\" is not a valid payment contact property name.");
63             return nullptr;
64         }
65     }
66
67     auto result = adoptNS([allocPKContactInstance() init]);
68
69     NSString *familyName = dynamic_objc_cast<NSString>(dictionary[@"familyName"]);
70     NSString *givenName = dynamic_objc_cast<NSString>(dictionary[@"givenName"]);
71
72     if (familyName || givenName) {
73         auto name = adoptNS([[NSPersonNameComponents alloc] init]);
74         [name setFamilyName:familyName];
75         [name setGivenName:givenName];
76         [result setName:name.get()];
77     }
78
79     [result setEmailAddress:dynamic_objc_cast<NSString>(dictionary[@"emailAddress"])];
80
81     if (NSString *phoneNumber = dynamic_objc_cast<NSString>(dictionary[@"phoneNumber"]))
82         [result setPhoneNumber:adoptNS([allocCNPhoneNumberInstance() initWithStringValue:phoneNumber]).get()];
83
84     NSArray *addressLines = dynamic_objc_cast<NSArray>(dictionary[@"addressLines"]);
85     if (addressLines.count) {
86         auto address = adoptNS([allocCNMutablePostalAddressInstance() init]);
87
88         [address setStreet:[addressLines componentsJoinedByString:@"\n"]];
89
90         if (NSString *locality = dynamic_objc_cast<NSString>(dictionary[@"locality"]))
91             [address setCity:locality];
92         if (NSString *postalCode = dynamic_objc_cast<NSString>(dictionary[@"postalCode"]))
93             [address setPostalCode:postalCode];
94         if (NSString *administrativeArea = dynamic_objc_cast<NSString>(dictionary[@"administrativeArea"]))
95             [address setState:administrativeArea];
96         if (NSString *country = dynamic_objc_cast<NSString>(dictionary[@"country"]))
97             [address setCountry:country];
98         if (NSString *countryCode = dynamic_objc_cast<NSString>(dictionary[@"countryCode"]))
99             [address setISOCountryCode:countryCode];
100
101         [result setPostalAddress:address.get()];
102     }
103
104     return result;
105 }
106
107 std::optional<PaymentContact> PaymentContact::fromJS(JSC::ExecState& state, JSC::JSValue value, String& errorMessage)
108 {
109     // FIXME: Don't round-trip using NSString.
110     auto jsonString = JSONStringify(&state, value, 0);
111     if (!jsonString)
112         return std::nullopt;
113
114     auto dictionary = dynamic_objc_cast<NSDictionary>([NSJSONSerialization JSONObjectWithData:[(NSString *)jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]);
115     if (!dictionary || ![dictionary isKindOfClass:[NSDictionary class]])
116         return std::nullopt;
117
118     auto pkContact = fromDictionary(dictionary, errorMessage);
119     if (!pkContact)
120         return std::nullopt;
121
122     return PaymentContact(pkContact.get());
123 }
124
125 // FIXME: This should use the PassKit SPI.
126 RetainPtr<NSDictionary> toDictionary(PKContact *contact)
127 {
128     ASSERT(contact);
129
130     auto result = adoptNS([[NSMutableDictionary alloc] init]);
131
132     if (contact.phoneNumber)
133         [result setObject:contact.phoneNumber.stringValue forKey:@"phoneNumber"];
134     if (contact.emailAddress)
135         [result setObject:contact.emailAddress forKey:@"emailAddress"];
136     if (contact.name.givenName)
137         [result setObject:contact.name.givenName forKey:@"givenName"];
138     if (contact.name.familyName)
139         [result setObject:contact.name.familyName forKey:@"familyName"];
140     if (contact.postalAddress.street.length)
141         [result setObject:[contact.postalAddress.street componentsSeparatedByString:@"\n"] forKey:@"addressLines"];
142     if (contact.postalAddress.city)
143         [result setObject:contact.postalAddress.city forKey:@"locality"];
144     if (contact.postalAddress.city)
145         [result setObject:contact.postalAddress.postalCode forKey:@"postalCode"];
146     if (contact.postalAddress.city)
147         [result setObject:contact.postalAddress.state forKey:@"administrativeArea"];
148     if (contact.postalAddress.city)
149         [result setObject:contact.postalAddress.country forKey:@"country"];
150     if (contact.postalAddress.city)
151         [result setObject:contact.postalAddress.ISOCountryCode forKey:@"countryCode"];
152
153     return result;
154 }
155
156 JSC::JSValue PaymentContact::toJS(JSC::ExecState& exec) const
157 {
158     auto dictionary = toDictionary(m_pkContact.get());
159
160     // FIXME: Don't round-trip using NSString.
161     auto jsonString = adoptNS([[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dictionary.get() options:0 error:nullptr] encoding:NSUTF8StringEncoding]);
162     return JSONParse(&exec, jsonString.get());
163 }
164
165
166 }
167
168 #endif