Use Optional::valueOr() instead of Optional::value_or()
[WebKit-https.git] / Source / WebKit / UIProcess / API / Cocoa / WKWebViewConfiguration.mm
1 /*
2  * Copyright (C) 2014-2017 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 "WKWebViewConfigurationInternal.h"
28 #import <pal/spi/cocoa/NSKeyedArchiverSPI.h>
29
30 #if WK_API_ENABLED
31
32 #import "APIPageConfiguration.h"
33 #import "VersionChecks.h"
34 #import "WKPreferences.h"
35 #import "WKProcessPool.h"
36 #import "WKRetainPtr.h"
37 #import "WKUserContentController.h"
38 #import "WKWebView.h"
39 #import "WKWebViewContentProviderRegistry.h"
40 #import "WebKit2Initialize.h"
41 #import "WebURLSchemeHandlerCocoa.h"
42 #import "_WKApplicationManifestInternal.h"
43 #import "_WKVisitedLinkStore.h"
44 #import "_WKWebsiteDataStoreInternal.h"
45 #import <WebCore/RuntimeApplicationChecks.h>
46 #import <WebCore/Settings.h>
47 #import <wtf/RetainPtr.h>
48 #import <wtf/URLParser.h>
49 #import <wtf/WeakObjCPtr.h>
50
51 #if PLATFORM(IOS_FAMILY)
52 #import "UIKitSPI.h"
53 #import <WebCore/Device.h>
54 #endif
55
56 template<typename T> class LazyInitialized {
57 public:
58     typedef typename WTF::GetPtrHelper<T>::PtrType PtrType;
59
60     template<typename F>
61     PtrType get(F&& f)
62     {
63         if (!m_isInitialized) {
64             m_value = f();
65             m_isInitialized = true;
66         }
67
68         return m_value.get();
69     }
70
71     void set(PtrType t)
72     {
73         m_value = t;
74         m_isInitialized = true;
75     }
76
77     void set(T&& t)
78     {
79         m_value = WTFMove(t);
80         m_isInitialized = true;
81     }
82
83     PtrType peek()
84     {
85         return m_value.get();
86     }
87
88 private:
89     bool m_isInitialized = false;
90     T m_value;
91 };
92
93 #if PLATFORM(IOS_FAMILY)
94
95 static _WKDragLiftDelay toDragLiftDelay(NSUInteger value)
96 {
97     if (value == _WKDragLiftDelayMedium)
98         return _WKDragLiftDelayMedium;
99     if (value == _WKDragLiftDelayLong)
100         return _WKDragLiftDelayLong;
101     return _WKDragLiftDelayShort;
102 }
103
104 #endif
105
106 @implementation WKWebViewConfiguration {
107     RefPtr<API::PageConfiguration> _pageConfiguration;
108     LazyInitialized<RetainPtr<WKProcessPool>> _processPool;
109     LazyInitialized<RetainPtr<WKPreferences>> _preferences;
110     LazyInitialized<RetainPtr<WKUserContentController>> _userContentController;
111     LazyInitialized<RetainPtr<_WKVisitedLinkStore>> _visitedLinkStore;
112     LazyInitialized<RetainPtr<WKWebsiteDataStore>> _websiteDataStore;
113     WeakObjCPtr<WKWebView> _relatedWebView;
114     WeakObjCPtr<WKWebView> _alternateWebViewForNavigationGestures;
115     RetainPtr<NSString> _groupIdentifier;
116     LazyInitialized<RetainPtr<NSString>> _applicationNameForUserAgent;
117     NSTimeInterval _incrementalRenderingSuppressionTimeout;
118     BOOL _respectsImageOrientation;
119     BOOL _printsBackgrounds;
120     BOOL _allowsJavaScriptMarkup;
121     BOOL _convertsPositionStyleOnCopy;
122     BOOL _allowsMetaRefresh;
123     BOOL _allowUniversalAccessFromFileURLs;
124
125 #if PLATFORM(IOS_FAMILY)
126     LazyInitialized<RetainPtr<WKWebViewContentProviderRegistry>> _contentProviderRegistry;
127     BOOL _allowsInlineMediaPlayback;
128     BOOL _inlineMediaPlaybackRequiresPlaysInlineAttribute;
129     BOOL _allowsInlineMediaPlaybackAfterFullscreen;
130     _WKDragLiftDelay _dragLiftDelay;
131     BOOL _textInteractionGesturesEnabled;
132     BOOL _longPressActionsEnabled;
133     BOOL _systemPreviewEnabled;
134 #endif
135
136     BOOL _invisibleAutoplayNotPermitted;
137     BOOL _mediaDataLoadsAutomatically;
138     BOOL _attachmentElementEnabled;
139     Class _attachmentFileWrapperClass;
140     BOOL _mainContentUserGestureOverrideEnabled;
141
142 #if PLATFORM(MAC)
143     WKRetainPtr<WKPageGroupRef> _pageGroup;
144     BOOL _showsURLsInToolTips;
145     BOOL _serviceControlsEnabled;
146     BOOL _imageControlsEnabled;
147     BOOL _requiresUserActionForEditingControlsManager;
148 #endif
149     BOOL _waitsForPaintAfterViewDidMoveToWindow;
150     BOOL _controlledByAutomation;
151
152 #if ENABLE(APPLE_PAY)
153     BOOL _applePayEnabled;
154 #endif
155     BOOL _needsStorageAccessFromFileURLsQuirk;
156     BOOL _legacyEncryptedMediaAPIEnabled;
157     BOOL _allowMediaContentTypesRequiringHardwareSupportAsFallback;
158     BOOL _colorFilterEnabled;
159     BOOL _incompleteImageBorderEnabled;
160     BOOL _shouldDeferAsynchronousScriptsUntilAfterDocumentLoad;
161     BOOL _drawsBackground;
162     BOOL _editableImagesEnabled;
163
164     RetainPtr<NSString> _mediaContentTypesRequiringHardwareSupport;
165     RetainPtr<NSArray<NSString *>> _additionalSupportedImageTypes;
166 }
167
168 - (instancetype)init
169 {
170     if (!(self = [super init]))
171         return nil;
172
173     WebKit::InitializeWebKit2();
174
175     _pageConfiguration = API::PageConfiguration::create();
176
177 #if PLATFORM(IOS_FAMILY)
178 #if !PLATFORM(WATCHOS)
179     _allowsPictureInPictureMediaPlayback = YES;
180 #endif
181     _allowsInlineMediaPlayback = WebCore::deviceClass() == MGDeviceClassiPad;
182     _inlineMediaPlaybackRequiresPlaysInlineAttribute = !_allowsInlineMediaPlayback;
183     _allowsInlineMediaPlaybackAfterFullscreen = !_allowsInlineMediaPlayback;
184     _mediaDataLoadsAutomatically = NO;
185 #if !PLATFORM(WATCHOS)
186     if (WebKit::linkedOnOrAfter(WebKit::SDKVersion::FirstWithMediaTypesRequiringUserActionForPlayback))
187         _mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAudio;
188     else
189 #endif
190         _mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll;
191     _ignoresViewportScaleLimits = NO;
192     _legacyEncryptedMediaAPIEnabled = NO;
193 #else
194     _mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
195     _mediaDataLoadsAutomatically = YES;
196     _userInterfaceDirectionPolicy = WKUserInterfaceDirectionPolicyContent;
197     _legacyEncryptedMediaAPIEnabled = YES;
198 #endif
199     _mainContentUserGestureOverrideEnabled = NO;
200     _invisibleAutoplayNotPermitted = NO;
201     _attachmentElementEnabled = NO;
202
203 #if PLATFORM(IOS_FAMILY)
204     _respectsImageOrientation = YES;
205     _printsBackgrounds = YES;
206 #endif
207
208 #if PLATFORM(MAC)
209     _printsBackgrounds = NO;
210     _respectsImageOrientation = NO;
211     _showsURLsInToolTips = NO;
212     _serviceControlsEnabled = NO;
213     _imageControlsEnabled = NO;
214     _requiresUserActionForEditingControlsManager = NO;
215 #endif
216     _waitsForPaintAfterViewDidMoveToWindow = YES;
217
218 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
219     _allowsAirPlayForMediaPlayback = YES;
220 #endif
221
222     _incrementalRenderingSuppressionTimeout = 5;
223     _allowsJavaScriptMarkup = YES;
224     _convertsPositionStyleOnCopy = NO;
225     _allowsMetaRefresh = YES;
226     _allowUniversalAccessFromFileURLs = NO;
227     _needsStorageAccessFromFileURLsQuirk = YES;
228
229 #if PLATFORM(IOS_FAMILY)
230     _selectionGranularity = WKSelectionGranularityDynamic;
231     _dragLiftDelay = toDragLiftDelay([[NSUserDefaults standardUserDefaults] integerForKey:@"WebKitDebugDragLiftDelay"]);
232 #if PLATFORM(WATCHOS)
233     _textInteractionGesturesEnabled = NO;
234     _longPressActionsEnabled = NO;
235 #else
236     _textInteractionGesturesEnabled = YES;
237     _longPressActionsEnabled = YES;
238 #endif
239     _systemPreviewEnabled = NO;
240 #endif // PLATFORM(IOS_FAMILY)
241
242     _mediaContentTypesRequiringHardwareSupport = WebCore::Settings::defaultMediaContentTypesRequiringHardwareSupport();
243     _allowMediaContentTypesRequiringHardwareSupportAsFallback = YES;
244
245     _colorFilterEnabled = NO;
246     _incompleteImageBorderEnabled = NO;
247     _shouldDeferAsynchronousScriptsUntilAfterDocumentLoad = NO;
248     _drawsBackground = YES;
249
250     _editableImagesEnabled = NO;
251
252     return self;
253 }
254
255 - (NSString *)description
256 {
257     return [NSString stringWithFormat:@"<%@: %p; processPool = %@; preferences = %@>", NSStringFromClass(self.class), self, self.processPool, self.preferences];
258 }
259
260 + (BOOL)supportsSecureCoding
261 {
262     return YES;
263 }
264
265 - (void)encodeWithCoder:(NSCoder *)coder
266 {
267     [coder encodeObject:self.processPool forKey:@"processPool"];
268     [coder encodeObject:self.preferences forKey:@"preferences"];
269     [coder encodeObject:self.userContentController forKey:@"userContentController"];
270     [coder encodeObject:self.websiteDataStore forKey:@"websiteDataStore"];
271
272     [coder encodeBool:self.suppressesIncrementalRendering forKey:@"suppressesIncrementalRendering"];
273     [coder encodeObject:self.applicationNameForUserAgent forKey:@"applicationNameForUserAgent"];
274     [coder encodeBool:self.allowsAirPlayForMediaPlayback forKey:@"allowsAirPlayForMediaPlayback"];
275
276     [coder encodeBool:self._drawsBackground forKey:@"drawsBackground"];
277
278 #if PLATFORM(IOS_FAMILY)
279     [coder encodeInteger:self.dataDetectorTypes forKey:@"dataDetectorTypes"];
280     [coder encodeBool:self.allowsInlineMediaPlayback forKey:@"allowsInlineMediaPlayback"];
281     [coder encodeBool:self._allowsInlineMediaPlaybackAfterFullscreen forKey:@"allowsInlineMediaPlaybackAfterFullscreen"];
282     [coder encodeBool:self.mediaTypesRequiringUserActionForPlayback forKey:@"mediaTypesRequiringUserActionForPlayback"];
283     [coder encodeInteger:self.selectionGranularity forKey:@"selectionGranularity"];
284     [coder encodeBool:self.allowsPictureInPictureMediaPlayback forKey:@"allowsPictureInPictureMediaPlayback"];
285     [coder encodeBool:self.ignoresViewportScaleLimits forKey:@"ignoresViewportScaleLimits"];
286     [coder encodeInteger:self._dragLiftDelay forKey:@"dragLiftDelay"];
287     [coder encodeBool:self._textInteractionGesturesEnabled forKey:@"textInteractionGesturesEnabled"];
288     [coder encodeBool:self._longPressActionsEnabled forKey:@"longPressActionsEnabled"];
289     [coder encodeBool:self._systemPreviewEnabled forKey:@"systemPreviewEnabled"];
290 #else
291     [coder encodeInteger:self.userInterfaceDirectionPolicy forKey:@"userInterfaceDirectionPolicy"];
292 #endif
293 }
294
295 - (instancetype)initWithCoder:(NSCoder *)coder
296 {
297     if (!(self = [self init]))
298         return nil;
299
300     self.processPool = decodeObjectOfClassForKeyFromCoder([WKProcessPool class], @"processPool", coder);
301     self.preferences = decodeObjectOfClassForKeyFromCoder([WKPreferences class], @"preferences", coder);
302     self.userContentController = decodeObjectOfClassForKeyFromCoder([WKUserContentController class], @"userContentController", coder);
303     self.websiteDataStore = decodeObjectOfClassForKeyFromCoder([WKWebsiteDataStore class], @"websiteDataStore", coder);
304
305     self.suppressesIncrementalRendering = [coder decodeBoolForKey:@"suppressesIncrementalRendering"];
306     self.applicationNameForUserAgent = decodeObjectOfClassForKeyFromCoder([NSString class], @"applicationNameForUserAgent", coder);
307     self.allowsAirPlayForMediaPlayback = [coder decodeBoolForKey:@"allowsAirPlayForMediaPlayback"];
308
309     if ([coder containsValueForKey:@"drawsBackground"])
310         self._drawsBackground = [coder decodeBoolForKey:@"drawsBackground"];
311
312 #if PLATFORM(IOS_FAMILY)
313     self.dataDetectorTypes = [coder decodeIntegerForKey:@"dataDetectorTypes"];
314     self.allowsInlineMediaPlayback = [coder decodeBoolForKey:@"allowsInlineMediaPlayback"];
315     self._allowsInlineMediaPlaybackAfterFullscreen = [coder decodeBoolForKey:@"allowsInlineMediaPlaybackAfterFullscreen"];
316     self.mediaTypesRequiringUserActionForPlayback = [coder decodeBoolForKey:@"mediaTypesRequiringUserActionForPlayback"];
317     self.selectionGranularity = static_cast<WKSelectionGranularity>([coder decodeIntegerForKey:@"selectionGranularity"]);
318     self.allowsPictureInPictureMediaPlayback = [coder decodeBoolForKey:@"allowsPictureInPictureMediaPlayback"];
319     self.ignoresViewportScaleLimits = [coder decodeBoolForKey:@"ignoresViewportScaleLimits"];
320     self._dragLiftDelay = toDragLiftDelay([coder decodeIntegerForKey:@"dragLiftDelay"]);
321     self._textInteractionGesturesEnabled = [coder decodeBoolForKey:@"textInteractionGesturesEnabled"];
322     self._longPressActionsEnabled = [coder decodeBoolForKey:@"longPressActionsEnabled"];
323     self._systemPreviewEnabled = [coder decodeBoolForKey:@"systemPreviewEnabled"];
324 #else
325     auto userInterfaceDirectionPolicyCandidate = static_cast<WKUserInterfaceDirectionPolicy>([coder decodeIntegerForKey:@"userInterfaceDirectionPolicy"]);
326     if (userInterfaceDirectionPolicyCandidate == WKUserInterfaceDirectionPolicyContent || userInterfaceDirectionPolicyCandidate == WKUserInterfaceDirectionPolicySystem)
327         self.userInterfaceDirectionPolicy = userInterfaceDirectionPolicyCandidate;
328 #endif
329
330     return self;
331 }
332
333 - (id)copyWithZone:(NSZone *)zone
334 {
335     WKWebViewConfiguration *configuration = [(WKWebViewConfiguration *)[[self class] allocWithZone:zone] init];
336
337     configuration->_pageConfiguration = _pageConfiguration->copy();
338     configuration.processPool = self.processPool;
339     configuration.preferences = self.preferences;
340     configuration.userContentController = self.userContentController;
341     configuration.websiteDataStore = self.websiteDataStore;
342     configuration._visitedLinkStore = self._visitedLinkStore;
343     configuration._relatedWebView = _relatedWebView.get().get();
344     configuration._alternateWebViewForNavigationGestures = _alternateWebViewForNavigationGestures.get().get();
345 #if PLATFORM(IOS_FAMILY)
346     configuration._contentProviderRegistry = self._contentProviderRegistry;
347 #endif
348
349     configuration->_suppressesIncrementalRendering = self->_suppressesIncrementalRendering;
350     configuration.applicationNameForUserAgent = self.applicationNameForUserAgent;
351
352     configuration->_respectsImageOrientation = self->_respectsImageOrientation;
353     configuration->_printsBackgrounds = self->_printsBackgrounds;
354     configuration->_incrementalRenderingSuppressionTimeout = self->_incrementalRenderingSuppressionTimeout;
355     configuration->_allowsJavaScriptMarkup = self->_allowsJavaScriptMarkup;
356     configuration->_convertsPositionStyleOnCopy = self->_convertsPositionStyleOnCopy;
357     configuration->_allowsMetaRefresh = self->_allowsMetaRefresh;
358     configuration->_allowUniversalAccessFromFileURLs = self->_allowUniversalAccessFromFileURLs;
359
360     configuration->_invisibleAutoplayNotPermitted = self->_invisibleAutoplayNotPermitted;
361     configuration->_mediaDataLoadsAutomatically = self->_mediaDataLoadsAutomatically;
362     configuration->_attachmentElementEnabled = self->_attachmentElementEnabled;
363     configuration->_attachmentFileWrapperClass = self->_attachmentFileWrapperClass;
364     configuration->_mediaTypesRequiringUserActionForPlayback = self->_mediaTypesRequiringUserActionForPlayback;
365     configuration->_mainContentUserGestureOverrideEnabled = self->_mainContentUserGestureOverrideEnabled;
366     configuration->_waitsForPaintAfterViewDidMoveToWindow = self->_waitsForPaintAfterViewDidMoveToWindow;
367     configuration->_controlledByAutomation = self->_controlledByAutomation;
368
369 #if PLATFORM(IOS_FAMILY)
370     configuration->_allowsInlineMediaPlayback = self->_allowsInlineMediaPlayback;
371     configuration->_allowsInlineMediaPlaybackAfterFullscreen = self->_allowsInlineMediaPlaybackAfterFullscreen;
372     configuration->_inlineMediaPlaybackRequiresPlaysInlineAttribute = self->_inlineMediaPlaybackRequiresPlaysInlineAttribute;
373     configuration->_allowsPictureInPictureMediaPlayback = self->_allowsPictureInPictureMediaPlayback;
374     configuration->_selectionGranularity = self->_selectionGranularity;
375     configuration->_ignoresViewportScaleLimits = self->_ignoresViewportScaleLimits;
376     configuration->_dragLiftDelay = self->_dragLiftDelay;
377     configuration->_textInteractionGesturesEnabled = self->_textInteractionGesturesEnabled;
378     configuration->_longPressActionsEnabled = self->_longPressActionsEnabled;
379     configuration->_systemPreviewEnabled = self->_systemPreviewEnabled;
380 #endif
381 #if PLATFORM(MAC)
382     configuration->_userInterfaceDirectionPolicy = self->_userInterfaceDirectionPolicy;
383     configuration->_showsURLsInToolTips = self->_showsURLsInToolTips;
384     configuration->_serviceControlsEnabled = self->_serviceControlsEnabled;
385     configuration->_imageControlsEnabled = self->_imageControlsEnabled;
386     configuration->_requiresUserActionForEditingControlsManager = self->_requiresUserActionForEditingControlsManager;
387     configuration->_pageGroup = self._pageGroup;
388 #endif
389 #if ENABLE(DATA_DETECTION) && PLATFORM(IOS_FAMILY)
390     configuration->_dataDetectorTypes = self->_dataDetectorTypes;
391 #endif
392 #if ENABLE(WIRELESS_TARGET_PLAYBACK)
393     configuration->_allowsAirPlayForMediaPlayback = self->_allowsAirPlayForMediaPlayback;
394 #endif
395 #if ENABLE(APPLE_PAY)
396     configuration->_applePayEnabled = self->_applePayEnabled;
397 #endif
398     configuration->_needsStorageAccessFromFileURLsQuirk = self->_needsStorageAccessFromFileURLsQuirk;
399
400     configuration->_mediaContentTypesRequiringHardwareSupport = adoptNS([self._mediaContentTypesRequiringHardwareSupport copyWithZone:zone]);
401     configuration->_additionalSupportedImageTypes = adoptNS([self->_additionalSupportedImageTypes copyWithZone:zone]);
402     configuration->_legacyEncryptedMediaAPIEnabled = self->_legacyEncryptedMediaAPIEnabled;
403     configuration->_allowMediaContentTypesRequiringHardwareSupportAsFallback = self->_allowMediaContentTypesRequiringHardwareSupportAsFallback;
404
405     configuration->_groupIdentifier = adoptNS([self->_groupIdentifier copyWithZone:zone]);
406     configuration->_colorFilterEnabled = self->_colorFilterEnabled;
407     configuration->_incompleteImageBorderEnabled = self->_incompleteImageBorderEnabled;
408     configuration->_shouldDeferAsynchronousScriptsUntilAfterDocumentLoad = self->_shouldDeferAsynchronousScriptsUntilAfterDocumentLoad;
409     configuration->_drawsBackground = self->_drawsBackground;
410
411     configuration->_editableImagesEnabled = self->_editableImagesEnabled;
412
413     return configuration;
414 }
415
416 - (WKProcessPool *)processPool
417 {
418     return _processPool.get([] { return adoptNS([[WKProcessPool alloc] init]); });
419 }
420
421 - (void)setProcessPool:(WKProcessPool *)processPool
422 {
423     _processPool.set(processPool);
424 }
425
426 - (WKPreferences *)preferences
427 {
428     return _preferences.get([] { return adoptNS([[WKPreferences alloc] init]); });
429 }
430
431 - (void)setPreferences:(WKPreferences *)preferences
432 {
433     _preferences.set(preferences);
434 }
435
436 - (WKUserContentController *)userContentController
437 {
438     return _userContentController.get([] { return adoptNS([[WKUserContentController alloc] init]); });
439 }
440
441 - (void)setUserContentController:(WKUserContentController *)userContentController
442 {
443     _userContentController.set(userContentController);
444 }
445
446 - (WKWebsiteDataStore *)websiteDataStore
447 {
448     return _websiteDataStore.get([] { return [WKWebsiteDataStore defaultDataStore]; });
449 }
450
451 - (void)setWebsiteDataStore:(WKWebsiteDataStore *)websiteDataStore
452 {
453     _websiteDataStore.set(websiteDataStore);
454 }
455
456 static NSString *defaultApplicationNameForUserAgent()
457 {
458 #if PLATFORM(IOS_FAMILY)
459     return @"Mobile/15E148";
460 #else
461     return nil;
462 #endif
463 }
464
465 - (NSString *)applicationNameForUserAgent
466 {
467     return _applicationNameForUserAgent.get([] { return defaultApplicationNameForUserAgent(); });
468 }
469
470 - (void)setApplicationNameForUserAgent:(NSString *)applicationNameForUserAgent
471 {
472     _applicationNameForUserAgent.set(adoptNS([applicationNameForUserAgent copy]));
473 }
474
475 - (_WKVisitedLinkStore *)_visitedLinkStore
476 {
477     return _visitedLinkStore.get([] { return adoptNS([[_WKVisitedLinkStore alloc] init]); });
478 }
479
480 - (void)_setVisitedLinkStore:(_WKVisitedLinkStore *)visitedLinkStore
481 {
482     _visitedLinkStore.set(visitedLinkStore);
483 }
484
485 - (void)setURLSchemeHandler:(id <WKURLSchemeHandler>)urlSchemeHandler forURLScheme:(NSString *)urlScheme
486 {
487     if ([WKWebView handlesURLScheme:urlScheme])
488         [NSException raise:NSInvalidArgumentException format:@"'%@' is a URL scheme that WKWebView handles natively", urlScheme];
489
490     auto canonicalScheme = WTF::URLParser::maybeCanonicalizeScheme(urlScheme);
491     if (!canonicalScheme)
492         [NSException raise:NSInvalidArgumentException format:@"'%@' is not a valid URL scheme", urlScheme];
493
494     if (_pageConfiguration->urlSchemeHandlerForURLScheme(*canonicalScheme))
495         [NSException raise:NSInvalidArgumentException format:@"URL scheme '%@' already has a registered URL scheme handler", urlScheme];
496
497     _pageConfiguration->setURLSchemeHandlerForURLScheme(WebKit::WebURLSchemeHandlerCocoa::create(urlSchemeHandler), *canonicalScheme);
498 }
499
500 - (id <WKURLSchemeHandler>)urlSchemeHandlerForURLScheme:(NSString *)urlScheme
501 {
502     auto canonicalScheme = WTF::URLParser::maybeCanonicalizeScheme(urlScheme);
503     if (!canonicalScheme)
504         return nil;
505
506     auto handler = _pageConfiguration->urlSchemeHandlerForURLScheme(*canonicalScheme);
507     return handler ? static_cast<WebKit::WebURLSchemeHandlerCocoa*>(handler.get())->apiHandler() : nil;
508 }
509
510 ALLOW_DEPRECATED_DECLARATIONS_BEGIN
511
512 IGNORE_WARNINGS_BEGIN("deprecated-implementations")
513 - (_WKWebsiteDataStore *)_websiteDataStore
514 IGNORE_WARNINGS_END
515 {
516     return self.websiteDataStore ? adoptNS([[_WKWebsiteDataStore alloc] initWithDataStore:self.websiteDataStore]).autorelease() : nullptr;
517 }
518
519 IGNORE_WARNINGS_BEGIN("deprecated-implementations")
520 - (void)_setWebsiteDataStore:(_WKWebsiteDataStore *)websiteDataStore
521 IGNORE_WARNINGS_END
522 {
523     self.websiteDataStore = websiteDataStore ? websiteDataStore->_dataStore.get() : nullptr;
524 }
525
526 ALLOW_DEPRECATED_DECLARATIONS_END
527
528 #if PLATFORM(IOS_FAMILY)
529 - (WKWebViewContentProviderRegistry *)_contentProviderRegistry
530 {
531     return _contentProviderRegistry.get([self] { return adoptNS([[WKWebViewContentProviderRegistry alloc] initWithConfiguration:self]); });
532 }
533
534 - (void)_setContentProviderRegistry:(WKWebViewContentProviderRegistry *)registry
535 {
536     _contentProviderRegistry.set(registry);
537 }
538 #endif
539
540 - (Ref<API::PageConfiguration>)copyPageConfiguration
541 {
542     return _pageConfiguration->copy();
543 }
544
545 @end
546
547 @implementation WKWebViewConfiguration (WKPrivate)
548
549 - (WKWebView *)_relatedWebView
550 {
551     return _relatedWebView.getAutoreleased();
552 }
553
554 - (void)_setRelatedWebView:(WKWebView *)relatedWebView
555 {
556     _relatedWebView = relatedWebView;
557 }
558
559 - (WKWebView *)_alternateWebViewForNavigationGestures
560 {
561     return _alternateWebViewForNavigationGestures.getAutoreleased();
562 }
563
564 - (void)_setAlternateWebViewForNavigationGestures:(WKWebView *)alternateView
565 {
566     _alternateWebViewForNavigationGestures = alternateView;
567 }
568
569 - (NSString *)_groupIdentifier
570 {
571     return _groupIdentifier.get();
572 }
573
574 - (void)_setGroupIdentifier:(NSString *)groupIdentifier
575 {
576     _groupIdentifier = groupIdentifier;
577 }
578
579 - (BOOL)_treatsSHA1SignedCertificatesAsInsecure
580 {
581     return _pageConfiguration->treatsSHA1SignedCertificatesAsInsecure();
582 }
583
584 - (void)_setTreatsSHA1SignedCertificatesAsInsecure:(BOOL)insecure
585 {
586     _pageConfiguration->setTreatsSHA1SignedCertificatesAsInsecure(insecure);
587 }
588
589 - (BOOL)_respectsImageOrientation
590 {
591     return _respectsImageOrientation;
592 }
593
594 - (void)_setRespectsImageOrientation:(BOOL)respectsImageOrientation
595 {
596     _respectsImageOrientation = respectsImageOrientation;
597 }
598
599 - (BOOL)_printsBackgrounds
600 {
601     return _printsBackgrounds;
602 }
603
604 - (void)_setPrintsBackgrounds:(BOOL)printsBackgrounds
605 {
606     _printsBackgrounds = printsBackgrounds;
607 }
608
609 - (NSTimeInterval)_incrementalRenderingSuppressionTimeout
610 {
611     return _incrementalRenderingSuppressionTimeout;
612 }
613
614 - (void)_setIncrementalRenderingSuppressionTimeout:(NSTimeInterval)incrementalRenderingSuppressionTimeout
615 {
616     _incrementalRenderingSuppressionTimeout = incrementalRenderingSuppressionTimeout;
617 }
618
619 - (BOOL)_allowsJavaScriptMarkup
620 {
621     return _allowsJavaScriptMarkup;
622 }
623
624 - (void)_setAllowsJavaScriptMarkup:(BOOL)allowsJavaScriptMarkup
625 {
626     _allowsJavaScriptMarkup = allowsJavaScriptMarkup;
627 }
628
629 - (BOOL)_allowUniversalAccessFromFileURLs
630 {
631     return _allowUniversalAccessFromFileURLs;
632 }
633
634 - (void)_setAllowUniversalAccessFromFileURLs:(BOOL)allowUniversalAccessFromFileURLs
635 {
636     _allowUniversalAccessFromFileURLs = allowUniversalAccessFromFileURLs;
637 }
638
639 - (BOOL)_convertsPositionStyleOnCopy
640 {
641     return _convertsPositionStyleOnCopy;
642 }
643
644 - (void)_setConvertsPositionStyleOnCopy:(BOOL)convertsPositionStyleOnCopy
645 {
646     _convertsPositionStyleOnCopy = convertsPositionStyleOnCopy;
647 }
648
649 - (BOOL)_allowsMetaRefresh
650 {
651     return _allowsMetaRefresh;
652 }
653
654 - (void)_setAllowsMetaRefresh:(BOOL)allowsMetaRefresh
655 {
656     _allowsMetaRefresh = allowsMetaRefresh;
657 }
658
659 #if PLATFORM(IOS_FAMILY)
660 - (BOOL)_alwaysRunsAtForegroundPriority
661 {
662     return _pageConfiguration->alwaysRunsAtForegroundPriority();
663 }
664
665 - (void)_setAlwaysRunsAtForegroundPriority:(BOOL)alwaysRunsAtForegroundPriority
666 {
667     _pageConfiguration->setAlwaysRunsAtForegroundPriority(alwaysRunsAtForegroundPriority);
668 }
669
670 - (BOOL)_inlineMediaPlaybackRequiresPlaysInlineAttribute
671 {
672     return _inlineMediaPlaybackRequiresPlaysInlineAttribute;
673 }
674
675 - (void)_setInlineMediaPlaybackRequiresPlaysInlineAttribute:(BOOL)requires
676 {
677     _inlineMediaPlaybackRequiresPlaysInlineAttribute = requires;
678 }
679
680 - (BOOL)_allowsInlineMediaPlaybackAfterFullscreen
681 {
682     return _allowsInlineMediaPlaybackAfterFullscreen;
683 }
684
685 - (void)_setAllowsInlineMediaPlaybackAfterFullscreen:(BOOL)allows
686 {
687     _allowsInlineMediaPlaybackAfterFullscreen = allows;
688 }
689
690 - (_WKDragLiftDelay)_dragLiftDelay
691 {
692     return _dragLiftDelay;
693 }
694
695 - (void)_setDragLiftDelay:(_WKDragLiftDelay)dragLiftDelay
696 {
697     _dragLiftDelay = dragLiftDelay;
698 }
699
700 - (BOOL)_textInteractionGesturesEnabled
701 {
702     return _textInteractionGesturesEnabled;
703 }
704
705 - (void)_setTextInteractionGesturesEnabled:(BOOL)enabled
706 {
707     _textInteractionGesturesEnabled = enabled;
708 }
709
710 - (BOOL)_longPressActionsEnabled
711 {
712     return _longPressActionsEnabled;
713 }
714
715 - (void)_setLongPressActionsEnabled:(BOOL)enabled
716 {
717     _longPressActionsEnabled = enabled;
718 }
719
720 - (BOOL)_systemPreviewEnabled
721 {
722     return _systemPreviewEnabled;
723 }
724
725 - (void)_setSystemPreviewEnabled:(BOOL)enabled
726 {
727     _systemPreviewEnabled = enabled;
728 }
729
730 #endif // PLATFORM(IOS_FAMILY)
731
732 - (BOOL)_invisibleAutoplayNotPermitted
733 {
734     return _invisibleAutoplayNotPermitted;
735 }
736
737 - (void)_setInvisibleAutoplayNotPermitted:(BOOL)notPermitted
738 {
739     _invisibleAutoplayNotPermitted = notPermitted;
740 }
741
742 - (BOOL)_mediaDataLoadsAutomatically
743 {
744     return _mediaDataLoadsAutomatically;
745 }
746
747 - (void)_setMediaDataLoadsAutomatically:(BOOL)mediaDataLoadsAutomatically
748 {
749     _mediaDataLoadsAutomatically = mediaDataLoadsAutomatically;
750 }
751
752 - (BOOL)_attachmentElementEnabled
753 {
754     return _attachmentElementEnabled;
755 }
756
757 - (void)_setAttachmentElementEnabled:(BOOL)attachmentElementEnabled
758 {
759     _attachmentElementEnabled = attachmentElementEnabled;
760 }
761
762 - (Class)_attachmentFileWrapperClass
763 {
764     return _attachmentFileWrapperClass;
765 }
766
767 - (void)_setAttachmentFileWrapperClass:(Class)attachmentFileWrapperClass
768 {
769     if (attachmentFileWrapperClass && ![attachmentFileWrapperClass isSubclassOfClass:[NSFileWrapper self]])
770         [NSException raise:NSInvalidArgumentException format:@"Class %@ does not inherit from NSFileWrapper", attachmentFileWrapperClass];
771
772     _attachmentFileWrapperClass = attachmentFileWrapperClass;
773 }
774
775 - (BOOL)_colorFilterEnabled
776 {
777     return _colorFilterEnabled;
778 }
779
780 - (void)_setColorFilterEnabled:(BOOL)colorFilterEnabled
781 {
782     _colorFilterEnabled = colorFilterEnabled;
783 }
784
785 - (BOOL)_incompleteImageBorderEnabled
786 {
787     return _incompleteImageBorderEnabled;
788 }
789
790 - (void)_setIncompleteImageBorderEnabled:(BOOL)incompleteImageBorderEnabled
791 {
792     _incompleteImageBorderEnabled = incompleteImageBorderEnabled;
793 }
794
795 - (BOOL)_shouldDeferAsynchronousScriptsUntilAfterDocumentLoad
796 {
797     return _shouldDeferAsynchronousScriptsUntilAfterDocumentLoad;
798 }
799
800 - (void)_setShouldDeferAsynchronousScriptsUntilAfterDocumentLoad:(BOOL)shouldDeferAsynchronousScriptsUntilAfterDocumentLoad
801 {
802     _shouldDeferAsynchronousScriptsUntilAfterDocumentLoad = shouldDeferAsynchronousScriptsUntilAfterDocumentLoad;
803 }
804
805 - (BOOL)_drawsBackground
806 {
807     return _drawsBackground;
808 }
809
810 - (void)_setDrawsBackground:(BOOL)drawsBackground
811 {
812     _drawsBackground = drawsBackground;
813 }
814
815 - (BOOL)_requiresUserActionForVideoPlayback
816 {
817     return self.mediaTypesRequiringUserActionForPlayback & WKAudiovisualMediaTypeVideo;
818 }
819
820 - (void)_setRequiresUserActionForVideoPlayback:(BOOL)requiresUserActionForVideoPlayback
821 {
822     if (requiresUserActionForVideoPlayback)
823         self.mediaTypesRequiringUserActionForPlayback |= WKAudiovisualMediaTypeVideo;
824     else
825         self.mediaTypesRequiringUserActionForPlayback &= ~WKAudiovisualMediaTypeVideo;
826 }
827
828 - (BOOL)_requiresUserActionForAudioPlayback
829 {
830     return self.mediaTypesRequiringUserActionForPlayback & WKAudiovisualMediaTypeAudio;
831 }
832
833 - (void)_setRequiresUserActionForAudioPlayback:(BOOL)requiresUserActionForAudioPlayback
834 {
835     if (requiresUserActionForAudioPlayback)
836         self.mediaTypesRequiringUserActionForPlayback |= WKAudiovisualMediaTypeAudio;
837     else
838         self.mediaTypesRequiringUserActionForPlayback &= ~WKAudiovisualMediaTypeAudio;
839 }
840
841 - (BOOL)_mainContentUserGestureOverrideEnabled
842 {
843     return _mainContentUserGestureOverrideEnabled;
844 }
845
846 - (void)_setMainContentUserGestureOverrideEnabled:(BOOL)mainContentUserGestureOverrideEnabled
847 {
848     _mainContentUserGestureOverrideEnabled = mainContentUserGestureOverrideEnabled;
849 }
850
851 - (BOOL)_initialCapitalizationEnabled
852 {
853     return _pageConfiguration->initialCapitalizationEnabled();
854 }
855
856 - (void)_setInitialCapitalizationEnabled:(BOOL)initialCapitalizationEnabled
857 {
858     _pageConfiguration->setInitialCapitalizationEnabled(initialCapitalizationEnabled);
859 }
860
861 - (BOOL)_waitsForPaintAfterViewDidMoveToWindow
862 {
863     return _waitsForPaintAfterViewDidMoveToWindow;
864 }
865
866 - (void)_setWaitsForPaintAfterViewDidMoveToWindow:(BOOL)shouldSynchronize
867 {
868     _waitsForPaintAfterViewDidMoveToWindow = shouldSynchronize;
869 }
870
871 - (BOOL)_isControlledByAutomation
872 {
873     return _controlledByAutomation;
874 }
875
876 - (void)_setControlledByAutomation:(BOOL)controlledByAutomation
877 {
878     _controlledByAutomation = controlledByAutomation;
879 }
880
881 - (_WKApplicationManifest *)_applicationManifest
882 {
883     return wrapper(_pageConfiguration->applicationManifest());
884 }
885
886 - (void)_setApplicationManifest:(_WKApplicationManifest *)applicationManifest
887 {
888     _pageConfiguration->setApplicationManifest(applicationManifest ? applicationManifest->_applicationManifest.get() : nullptr);
889 }
890
891 #if PLATFORM(MAC)
892 - (BOOL)_showsURLsInToolTips
893 {
894     return _showsURLsInToolTips;
895 }
896
897 - (void)_setShowsURLsInToolTips:(BOOL)showsURLsInToolTips
898 {
899     _showsURLsInToolTips = showsURLsInToolTips;
900 }
901
902 - (BOOL)_serviceControlsEnabled
903 {
904     return _serviceControlsEnabled;
905 }
906
907 - (void)_setServiceControlsEnabled:(BOOL)serviceControlsEnabled
908 {
909     _serviceControlsEnabled = serviceControlsEnabled;
910 }
911
912 - (BOOL)_imageControlsEnabled
913 {
914     return _imageControlsEnabled;
915 }
916
917 - (void)_setImageControlsEnabled:(BOOL)imageControlsEnabled
918 {
919     _imageControlsEnabled = imageControlsEnabled;
920 }
921
922 - (BOOL)_requiresUserActionForEditingControlsManager
923 {
924     return _requiresUserActionForEditingControlsManager;
925 }
926
927 - (void)_setRequiresUserActionForEditingControlsManager:(BOOL)requiresUserAction
928 {
929     _requiresUserActionForEditingControlsManager = requiresUserAction;
930 }
931
932 - (WKPageGroupRef)_pageGroup
933 {
934     return _pageGroup.get();
935 }
936
937 - (void)_setPageGroup:(WKPageGroupRef)pageGroup
938 {
939     _pageGroup = pageGroup;
940 }
941
942 - (void)_setCPULimit:(double)cpuLimit
943 {
944     _pageConfiguration->setCPULimit(cpuLimit);
945 }
946
947 - (double)_cpuLimit
948 {
949     return _pageConfiguration->cpuLimit().valueOr(0);
950 }
951
952 #endif // PLATFORM(MAC)
953
954 - (BOOL)_applePayEnabled
955 {
956 #if ENABLE(APPLE_PAY)
957     return _applePayEnabled;
958 #else
959     return NO;
960 #endif
961 }
962
963 - (void)_setApplePayEnabled:(BOOL)applePayEnabled
964 {
965 #if ENABLE(APPLE_PAY)
966     _applePayEnabled = applePayEnabled;
967 #endif
968 }
969
970 - (BOOL)_needsStorageAccessFromFileURLsQuirk
971 {
972     return _needsStorageAccessFromFileURLsQuirk;
973 }
974
975 - (void)_setNeedsStorageAccessFromFileURLsQuirk:(BOOL)needsLocalStorageQuirk
976 {
977     _needsStorageAccessFromFileURLsQuirk = needsLocalStorageQuirk;
978 }
979
980 - (NSString *)_overrideContentSecurityPolicy
981 {
982     return _pageConfiguration->overrideContentSecurityPolicy();
983 }
984
985 - (void)_setOverrideContentSecurityPolicy:(NSString *)overrideContentSecurityPolicy
986 {
987     _pageConfiguration->setOverrideContentSecurityPolicy(overrideContentSecurityPolicy);
988 }
989
990 - (NSString *)_mediaContentTypesRequiringHardwareSupport
991 {
992     return _mediaContentTypesRequiringHardwareSupport.get();
993 }
994
995 - (void)_setMediaContentTypesRequiringHardwareSupport:(NSString *)mediaContentTypesRequiringHardwareSupport
996 {
997     _mediaContentTypesRequiringHardwareSupport = adoptNS([mediaContentTypesRequiringHardwareSupport copy]);
998 }
999
1000 - (NSArray<NSString *> *)_additionalSupportedImageTypes
1001 {
1002     return _additionalSupportedImageTypes.get();
1003 }
1004
1005 - (void)_setAdditionalSupportedImageTypes:(NSArray<NSString *> *)additionalSupportedImageTypes
1006 {
1007     _additionalSupportedImageTypes = adoptNS([additionalSupportedImageTypes copy]);
1008 }
1009
1010 - (void)_setLegacyEncryptedMediaAPIEnabled:(BOOL)enabled
1011 {
1012     _legacyEncryptedMediaAPIEnabled = enabled;
1013 }
1014
1015 - (BOOL)_legacyEncryptedMediaAPIEnabled
1016 {
1017     return _legacyEncryptedMediaAPIEnabled;
1018 }
1019
1020 - (void)_setAllowMediaContentTypesRequiringHardwareSupportAsFallback:(BOOL)allow
1021 {
1022     _allowMediaContentTypesRequiringHardwareSupportAsFallback = allow;
1023 }
1024
1025 - (BOOL)_allowMediaContentTypesRequiringHardwareSupportAsFallback
1026 {
1027     return _allowMediaContentTypesRequiringHardwareSupportAsFallback;
1028 }
1029
1030 - (void)_setEditableImagesEnabled:(BOOL)enabled
1031 {
1032     _editableImagesEnabled = enabled;
1033 }
1034
1035 - (BOOL)_editableImagesEnabled
1036 {
1037     return _editableImagesEnabled;
1038 }
1039
1040 @end
1041
1042 @implementation WKWebViewConfiguration (WKDeprecated)
1043
1044 #if PLATFORM(IOS_FAMILY)
1045 - (BOOL)mediaPlaybackAllowsAirPlay
1046 {
1047     return self.allowsAirPlayForMediaPlayback;
1048 }
1049
1050 - (void)setMediaPlaybackAllowsAirPlay:(BOOL)allowed
1051 {
1052     self.allowsAirPlayForMediaPlayback = allowed;
1053 }
1054
1055 - (BOOL)mediaPlaybackRequiresUserAction
1056 {
1057     return self.requiresUserActionForMediaPlayback;
1058 }
1059
1060 - (void)setMediaPlaybackRequiresUserAction:(BOOL)required
1061 {
1062     self.requiresUserActionForMediaPlayback = required;
1063 }
1064
1065 - (BOOL)requiresUserActionForMediaPlayback
1066 {
1067     return self.mediaTypesRequiringUserActionForPlayback == WKAudiovisualMediaTypeAll;
1068 }
1069
1070 - (void)setRequiresUserActionForMediaPlayback:(BOOL)requiresUserActionForMediaPlayback
1071 {
1072     self.mediaTypesRequiringUserActionForPlayback = requiresUserActionForMediaPlayback ? WKAudiovisualMediaTypeAll : WKAudiovisualMediaTypeNone;
1073 }
1074
1075 #endif // PLATFORM(IOS_FAMILY)
1076
1077 @end
1078
1079 @implementation WKWebViewConfiguration (WKBinaryCompatibilityWithIOS10)
1080
1081 -(_WKVisitedLinkStore *)_visitedLinkProvider
1082 {
1083     return self._visitedLinkStore;
1084 }
1085
1086 - (void)_setVisitedLinkProvider:(_WKVisitedLinkStore *)visitedLinkProvider
1087 {
1088     self._visitedLinkStore = visitedLinkProvider;
1089 }
1090
1091 @end
1092
1093 #endif // WK_API_ENABLED