Unreviewed, fix iOS build with recent SDKs.
[WebKit-https.git] / Source / WebKit / UIProcess / API / Cocoa / WKWebpagePreferences.mm
1 /*
2  * Copyright (C) 2019 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 "WKWebpagePreferences.h"
28
29 #import "WKWebpagePreferencesInternal.h"
30 #import "WKWebsiteDataStoreInternal.h"
31 #import "WebCompatibilityMode.h"
32 #import "_WKWebsitePoliciesInternal.h"
33 #import <wtf/RetainPtr.h>
34
35 #if USE(APPLE_INTERNAL_SDK)
36 #import <WebKitAdditions/WKWebpagePreferencesAdditionsBefore.mm>
37 #endif
38
39 @implementation WKWebpagePreferences
40
41 + (instancetype)defaultPreferences
42 {
43     return [[[self alloc] init] autorelease];
44 }
45
46 - (void)dealloc
47 {
48     _websitePolicies->API::WebsitePolicies::~WebsitePolicies();
49
50     [super dealloc];
51 }
52
53 - (instancetype)init
54 {
55     if (!(self = [super init]))
56         return nil;
57
58     API::Object::constructInWrapper<API::WebsitePolicies>(self);
59
60     return self;
61 }
62
63 - (void)_setContentBlockersEnabled:(BOOL)contentBlockersEnabled
64 {
65     _websitePolicies->setContentBlockersEnabled(contentBlockersEnabled);
66 }
67
68 - (BOOL)_contentBlockersEnabled
69 {
70     return _websitePolicies->contentBlockersEnabled();
71 }
72
73 - (void)_setAllowedAutoplayQuirks:(_WKWebsiteAutoplayQuirk)allowedQuirks
74 {
75     OptionSet<WebKit::WebsiteAutoplayQuirk> quirks;
76
77     if (allowedQuirks & _WKWebsiteAutoplayQuirkInheritedUserGestures)
78         quirks.add(WebKit::WebsiteAutoplayQuirk::InheritedUserGestures);
79
80     if (allowedQuirks & _WKWebsiteAutoplayQuirkSynthesizedPauseEvents)
81         quirks.add(WebKit::WebsiteAutoplayQuirk::SynthesizedPauseEvents);
82
83     if (allowedQuirks & _WKWebsiteAutoplayQuirkArbitraryUserGestures)
84         quirks.add(WebKit::WebsiteAutoplayQuirk::ArbitraryUserGestures);
85
86     if (allowedQuirks & _WKWebsiteAutoplayQuirkPerDocumentAutoplayBehavior)
87         quirks.add(WebKit::WebsiteAutoplayQuirk::PerDocumentAutoplayBehavior);
88
89     _websitePolicies->setAllowedAutoplayQuirks(quirks);
90 }
91
92 - (_WKWebsiteAutoplayQuirk)_allowedAutoplayQuirks
93 {
94     _WKWebsiteAutoplayQuirk quirks = 0;
95     auto allowedQuirks = _websitePolicies->allowedAutoplayQuirks();
96
97     if (allowedQuirks.contains(WebKit::WebsiteAutoplayQuirk::InheritedUserGestures))
98         quirks |= _WKWebsiteAutoplayQuirkInheritedUserGestures;
99
100     if (allowedQuirks.contains(WebKit::WebsiteAutoplayQuirk::SynthesizedPauseEvents))
101         quirks |= _WKWebsiteAutoplayQuirkSynthesizedPauseEvents;
102
103     if (allowedQuirks.contains(WebKit::WebsiteAutoplayQuirk::ArbitraryUserGestures))
104         quirks |= _WKWebsiteAutoplayQuirkArbitraryUserGestures;
105
106     if (allowedQuirks.contains(WebKit::WebsiteAutoplayQuirk::PerDocumentAutoplayBehavior))
107         quirks |= _WKWebsiteAutoplayQuirkPerDocumentAutoplayBehavior;
108
109     return quirks;
110 }
111
112 - (void)_setAutoplayPolicy:(_WKWebsiteAutoplayPolicy)policy
113 {
114     switch (policy) {
115     case _WKWebsiteAutoplayPolicyDefault:
116         _websitePolicies->setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy::Default);
117         break;
118     case _WKWebsiteAutoplayPolicyAllow:
119         _websitePolicies->setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy::Allow);
120         break;
121     case _WKWebsiteAutoplayPolicyAllowWithoutSound:
122         _websitePolicies->setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy::AllowWithoutSound);
123         break;
124     case _WKWebsiteAutoplayPolicyDeny:
125         _websitePolicies->setAutoplayPolicy(WebKit::WebsiteAutoplayPolicy::Deny);
126         break;
127     }
128 }
129
130 - (_WKWebsiteAutoplayPolicy)_autoplayPolicy
131 {
132     switch (_websitePolicies->autoplayPolicy()) {
133     case WebKit::WebsiteAutoplayPolicy::Default:
134         return _WKWebsiteAutoplayPolicyDefault;
135     case WebKit::WebsiteAutoplayPolicy::Allow:
136         return _WKWebsiteAutoplayPolicyAllow;
137     case WebKit::WebsiteAutoplayPolicy::AllowWithoutSound:
138         return _WKWebsiteAutoplayPolicyAllowWithoutSound;
139     case WebKit::WebsiteAutoplayPolicy::Deny:
140         return _WKWebsiteAutoplayPolicyDeny;
141     }
142 }
143
144 #if ENABLE(DEVICE_ORIENTATION)
145 static WebCore::DeviceOrientationOrMotionPermissionState toDeviceOrientationOrMotionPermissionState(_WKWebsiteDeviceOrientationAndMotionAccessPolicy policy)
146 {
147     switch (policy) {
148     case _WKWebsiteDeviceOrientationAndMotionAccessPolicyAsk:
149         return WebCore::DeviceOrientationOrMotionPermissionState::Prompt;
150     case _WKWebsiteDeviceOrientationAndMotionAccessPolicyGrant:
151         return WebCore::DeviceOrientationOrMotionPermissionState::Granted;
152     case _WKWebsiteDeviceOrientationAndMotionAccessPolicyDeny:
153         break;
154     }
155     return WebCore::DeviceOrientationOrMotionPermissionState::Denied;
156 }
157 #endif
158
159 - (void)_setDeviceOrientationAndMotionAccessPolicy:(_WKWebsiteDeviceOrientationAndMotionAccessPolicy)policy
160 {
161 #if ENABLE(DEVICE_ORIENTATION)
162     _websitePolicies->setDeviceOrientationAndMotionAccessState(toDeviceOrientationOrMotionPermissionState(policy));
163 #endif
164 }
165
166 #if ENABLE(DEVICE_ORIENTATION)
167 static _WKWebsiteDeviceOrientationAndMotionAccessPolicy toWKWebsiteDeviceOrientationAndMotionAccessPolicy(WebCore::DeviceOrientationOrMotionPermissionState state)
168 {
169     switch (state) {
170     case WebCore::DeviceOrientationOrMotionPermissionState::Prompt:
171         return _WKWebsiteDeviceOrientationAndMotionAccessPolicyAsk;
172     case WebCore::DeviceOrientationOrMotionPermissionState::Granted:
173         return _WKWebsiteDeviceOrientationAndMotionAccessPolicyGrant;
174     case WebCore::DeviceOrientationOrMotionPermissionState::Denied:
175         break;
176     }
177     return _WKWebsiteDeviceOrientationAndMotionAccessPolicyDeny;
178 }
179 #endif
180
181 - (_WKWebsiteDeviceOrientationAndMotionAccessPolicy)_deviceOrientationAndMotionAccessPolicy
182 {
183 #if ENABLE(DEVICE_ORIENTATION)
184     return toWKWebsiteDeviceOrientationAndMotionAccessPolicy(_websitePolicies->deviceOrientationAndMotionAccessState());
185 #else
186     return _WKWebsiteDeviceOrientationAndMotionAccessPolicyDeny;
187 #endif
188 }
189
190 - (void)_setPopUpPolicy:(_WKWebsitePopUpPolicy)policy
191 {
192     switch (policy) {
193     case _WKWebsitePopUpPolicyDefault:
194         _websitePolicies->setPopUpPolicy(WebKit::WebsitePopUpPolicy::Default);
195         break;
196     case _WKWebsitePopUpPolicyAllow:
197         _websitePolicies->setPopUpPolicy(WebKit::WebsitePopUpPolicy::Allow);
198         break;
199     case _WKWebsitePopUpPolicyBlock:
200         _websitePolicies->setPopUpPolicy(WebKit::WebsitePopUpPolicy::Block);
201         break;
202     }
203 }
204
205 - (_WKWebsitePopUpPolicy)_popUpPolicy
206 {
207     switch (_websitePolicies->popUpPolicy()) {
208     case WebKit::WebsitePopUpPolicy::Default:
209         return _WKWebsitePopUpPolicyDefault;
210     case WebKit::WebsitePopUpPolicy::Allow:
211         return _WKWebsitePopUpPolicyAllow;
212     case WebKit::WebsitePopUpPolicy::Block:
213         return _WKWebsitePopUpPolicyBlock;
214     }
215 }
216
217 - (NSDictionary<NSString *, NSString *> *)_customHeaderFields
218 {
219     const auto& fields = _websitePolicies->customHeaderFields();
220     auto dictionary = adoptNS([[NSMutableDictionary alloc] initWithCapacity:fields.size()]);
221     for (const auto& field : fields)
222         [dictionary setObject:field.value() forKey:field.name()];
223     return dictionary.autorelease();
224 }
225
226 - (void)_setCustomHeaderFields:(NSDictionary<NSString *, NSString *> *)fields
227 {
228     Vector<WebCore::HTTPHeaderField> parsedFields;
229     parsedFields.reserveInitialCapacity(fields.count);
230
231     for (NSString *name in fields) {
232         auto field = WebCore::HTTPHeaderField::create(name, [fields objectForKey:name]);
233         if (field && startsWithLettersIgnoringASCIICase(field->name(), "x-"))
234             parsedFields.uncheckedAppend(WTFMove(*field));
235     }
236     _websitePolicies->setCustomHeaderFields(WTFMove(parsedFields));
237 }
238
239 - (WKWebsiteDataStore *)_websiteDataStore
240 {
241     return wrapper(_websitePolicies->websiteDataStore());
242 }
243
244 - (void)_setWebsiteDataStore:(WKWebsiteDataStore *)websiteDataStore
245 {
246     _websitePolicies->setWebsiteDataStore(websiteDataStore->_websiteDataStore.get());
247 }
248
249 - (void)_setCustomUserAgent:(NSString *)customUserAgent
250 {
251     _websitePolicies->setCustomUserAgent(customUserAgent);
252 }
253
254 - (NSString *)_customUserAgent
255 {
256     return _websitePolicies->customUserAgent();
257 }
258
259 - (void)_setCustomJavaScriptUserAgentAsSiteSpecificQuirks:(NSString *)customUserAgent
260 {
261     _websitePolicies->setCustomJavaScriptUserAgentAsSiteSpecificQuirks(customUserAgent);
262 }
263
264 - (NSString *)_customJavaScriptUserAgentAsSiteSpecificQuirks
265 {
266     return _websitePolicies->customJavaScriptUserAgentAsSiteSpecificQuirks();
267 }
268
269 - (void)_setCustomNavigatorPlatform:(NSString *)customNavigatorPlatform
270 {
271     _websitePolicies->setCustomNavigatorPlatform(customNavigatorPlatform);
272 }
273
274 - (NSString *)_customNavigatorPlatform
275 {
276     return _websitePolicies->customNavigatorPlatform();
277 }
278
279 - (API::Object&)_apiObject
280 {
281     return *_websitePolicies;
282 }
283
284 #if USE(APPLE_INTERNAL_SDK)
285 #import <WebKitAdditions/WKWebpagePreferencesAdditionsAfter.mm>
286 #endif
287
288 @end