Rename _WKVisitedLinkProvider to _WKVisitedLinkStore for consistency
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / Cocoa / WKWebViewConfiguration.mm
1 /*
2  * Copyright (C) 2014, 2015 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
29 #if WK_API_ENABLED
30
31 #import "APIPageConfiguration.h"
32 #import "WKPreferences.h"
33 #import "WKProcessPool.h"
34 #import "WKUserContentController.h"
35 #import "WKWebViewContentProviderRegistry.h"
36 #import "WeakObjCPtr.h"
37 #import "_WKVisitedLinkProvider.h"
38 #import "_WKWebsiteDataStoreInternal.h"
39 #import <wtf/RetainPtr.h>
40
41 #if PLATFORM(IOS)
42 #import "UIKitSPI.h"
43 #endif
44
45 template<typename T> class LazyInitialized {
46 public:
47     typedef typename WTF::GetPtrHelper<T>::PtrType PtrType;
48
49     template<typename F>
50     PtrType get(F&& f)
51     {
52         if (!m_isInitialized) {
53             m_value = f();
54             m_isInitialized = true;
55         }
56
57         return m_value.get();
58     }
59
60     void set(PtrType t)
61     {
62         m_value = t;
63         m_isInitialized = true;
64     }
65
66     void set(T&& t)
67     {
68         m_value = WTF::move(t);
69         m_isInitialized = true;
70     }
71
72     PtrType peek()
73     {
74         return m_value.get();
75     }
76
77 private:
78     bool m_isInitialized = false;
79     T m_value;
80 };
81
82 @implementation WKWebViewConfiguration {
83     LazyInitialized<RetainPtr<WKProcessPool>> _processPool;
84     LazyInitialized<RetainPtr<WKPreferences>> _preferences;
85     LazyInitialized<RetainPtr<WKUserContentController>> _userContentController;
86     LazyInitialized<RetainPtr<_WKVisitedLinkStore>> _visitedLinkStore;
87     LazyInitialized<RetainPtr<WKWebsiteDataStore>> _websiteDataStore;
88     WebKit::WeakObjCPtr<WKWebView> _relatedWebView;
89     WebKit::WeakObjCPtr<WKWebView> _alternateWebViewForNavigationGestures;
90     BOOL _treatsSHA1SignedCertificatesAsInsecure;
91     RetainPtr<NSString> _groupIdentifier;
92     LazyInitialized<RetainPtr<NSString>> _applicationNameForUserAgent;
93
94 #if PLATFORM(IOS)
95     LazyInitialized<RetainPtr<WKWebViewContentProviderRegistry>> _contentProviderRegistry;
96     BOOL _alwaysRunsAtForegroundPriority;
97 #endif
98 }
99
100 - (instancetype)init
101 {
102     if (!(self = [super init]))
103         return nil;
104     
105 #if PLATFORM(IOS)
106     _requiresUserActionForMediaPlayback = YES;
107     _allowsPictureInPictureMediaPlayback = YES;
108 #endif
109
110 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
111     _allowsAirPlayForMediaPlayback = YES;
112 #endif
113
114     return self;
115 }
116
117 - (NSString *)description
118 {
119     return [NSString stringWithFormat:@"<%@: %p; processPool = %@; preferences = %@>", NSStringFromClass(self.class), self, self.processPool, self.preferences];
120 }
121
122 - (id)copyWithZone:(NSZone *)zone
123 {
124     WKWebViewConfiguration *configuration = [(WKWebViewConfiguration *)[[self class] allocWithZone:zone] init];
125
126     configuration.processPool = self.processPool;
127     configuration.preferences = self.preferences;
128     configuration.userContentController = self.userContentController;
129     configuration.websiteDataStore = self.websiteDataStore;
130     configuration._visitedLinkStore = self._visitedLinkStore;
131     configuration._relatedWebView = _relatedWebView.get().get();
132     configuration._alternateWebViewForNavigationGestures = _alternateWebViewForNavigationGestures.get().get();
133     configuration->_treatsSHA1SignedCertificatesAsInsecure = _treatsSHA1SignedCertificatesAsInsecure;
134 #if PLATFORM(IOS)
135     configuration._contentProviderRegistry = self._contentProviderRegistry;
136 #endif
137
138     configuration->_suppressesIncrementalRendering = self->_suppressesIncrementalRendering;
139     configuration.applicationNameForUserAgent = self.applicationNameForUserAgent;
140
141 #if PLATFORM(IOS)
142     configuration->_allowsInlineMediaPlayback = self->_allowsInlineMediaPlayback;
143     configuration->_allowsPictureInPictureMediaPlayback = self->_allowsPictureInPictureMediaPlayback;
144     configuration->_alwaysRunsAtForegroundPriority = _alwaysRunsAtForegroundPriority;
145     configuration->_requiresUserActionForMediaPlayback = self->_requiresUserActionForMediaPlayback;
146     configuration->_selectionGranularity = self->_selectionGranularity;
147 #endif
148 #if ENABLE(WIRELESS_TARGET_PLAYBACK)
149     configuration->_allowsAirPlayForMediaPlayback = self->_allowsAirPlayForMediaPlayback;
150 #endif
151
152     return configuration;
153 }
154
155 - (WKProcessPool *)processPool
156 {
157     return _processPool.get([] { return adoptNS([[WKProcessPool alloc] init]); });
158 }
159
160 - (void)setProcessPool:(WKProcessPool *)processPool
161 {
162     _processPool.set(processPool);
163 }
164
165 - (WKPreferences *)preferences
166 {
167     return _preferences.get([] { return adoptNS([[WKPreferences alloc] init]); });
168 }
169
170 - (void)setPreferences:(WKPreferences *)preferences
171 {
172     _preferences.set(preferences);
173 }
174
175 - (WKUserContentController *)userContentController
176 {
177     return _userContentController.get([] { return adoptNS([[WKUserContentController alloc] init]); });
178 }
179
180 - (void)setUserContentController:(WKUserContentController *)userContentController
181 {
182     _userContentController.set(userContentController);
183 }
184
185 - (WKWebsiteDataStore *)websiteDataStore
186 {
187     return _websiteDataStore.get([] { return [WKWebsiteDataStore defaultDataStore]; });
188 }
189
190 - (void)setWebsiteDataStore:(WKWebsiteDataStore *)websiteDataStore
191 {
192     _websiteDataStore.set(websiteDataStore);
193 }
194
195 static NSString *defaultApplicationNameForUserAgent()
196 {
197 #if PLATFORM(IOS)
198     return [@"Mobile/" stringByAppendingString:[UIDevice currentDevice].buildVersion];
199 #else
200     return nil;
201 #endif
202 }
203
204 - (NSString *)applicationNameForUserAgent
205 {
206     return _applicationNameForUserAgent.get([] { return defaultApplicationNameForUserAgent(); });
207 }
208
209 - (void)setApplicationNameForUserAgent:(NSString *)applicationNameForUserAgent
210 {
211     _applicationNameForUserAgent.set(adoptNS([applicationNameForUserAgent copy]));
212 }
213
214 - (_WKVisitedLinkStore *)_visitedLinkStore
215 {
216     return _visitedLinkStore.get([] { return adoptNS([[_WKVisitedLinkStore alloc] init]); });
217 }
218
219 - (void)_setVisitedLinkStore:(_WKVisitedLinkStore *)visitedLinkStore
220 {
221     _visitedLinkStore.set(visitedLinkStore);
222 }
223
224 #pragma clang diagnostic push
225 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
226
227 - (_WKWebsiteDataStore *)_websiteDataStore
228 {
229     return self.websiteDataStore ? adoptNS([[_WKWebsiteDataStore alloc] initWithDataStore:self.websiteDataStore]).autorelease() : nullptr;
230 }
231
232 - (void)_setWebsiteDataStore:(_WKWebsiteDataStore *)websiteDataStore
233 {
234     self.websiteDataStore = websiteDataStore ? websiteDataStore->_dataStore.get() : nullptr;
235 }
236
237 -(_WKVisitedLinkProvider *)_visitedLinkProvider
238 {
239     return (_WKVisitedLinkProvider *)self._visitedLinkStore;
240 }
241
242 - (void)_setVisitedLinkProvider:(_WKVisitedLinkProvider *)_visitedLinkProvider
243 {
244     self._visitedLinkStore = _visitedLinkProvider;
245 }
246
247 #pragma clang diagnostic pop
248
249 #if PLATFORM(IOS)
250 - (WKWebViewContentProviderRegistry *)_contentProviderRegistry
251 {
252     return _contentProviderRegistry.get([] { return adoptNS([[WKWebViewContentProviderRegistry alloc] init]); });
253 }
254
255 - (void)_setContentProviderRegistry:(WKWebViewContentProviderRegistry *)registry
256 {
257     _contentProviderRegistry.set(registry);
258 }
259 #endif
260
261 - (void)_validate
262 {
263     if (!self.processPool)
264         [NSException raise:NSInvalidArgumentException format:@"configuration.processPool is nil"];
265
266     if (!self.preferences)
267         [NSException raise:NSInvalidArgumentException format:@"configuration.preferences is nil"];
268
269     if (!self.userContentController)
270         [NSException raise:NSInvalidArgumentException format:@"configuration.userContentController is nil"];
271
272     if (!self.websiteDataStore)
273         [NSException raise:NSInvalidArgumentException format:@"configuration.websiteDataStore is nil"];
274
275     if (!self._visitedLinkStore)
276         [NSException raise:NSInvalidArgumentException format:@"configuration._visitedLinkStore is nil"];
277
278 #if PLATFORM(IOS)
279     if (!self._contentProviderRegistry)
280         [NSException raise:NSInvalidArgumentException format:@"configuration._contentProviderRegistry is nil"];
281 #endif
282 }
283
284 @end
285
286 @implementation WKWebViewConfiguration (WKPrivate)
287
288 - (WKWebView *)_relatedWebView
289 {
290     return _relatedWebView.getAutoreleased();
291 }
292
293 - (void)_setRelatedWebView:(WKWebView *)relatedWebView
294 {
295     _relatedWebView = relatedWebView;
296 }
297
298 - (WKWebView *)_alternateWebViewForNavigationGestures
299 {
300     return _alternateWebViewForNavigationGestures.getAutoreleased();
301 }
302
303 - (void)_setAlternateWebViewForNavigationGestures:(WKWebView *)alternateView
304 {
305     _alternateWebViewForNavigationGestures = alternateView;
306 }
307
308 - (NSString *)_groupIdentifier
309 {
310     return _groupIdentifier.get();
311 }
312
313 - (void)_setGroupIdentifier:(NSString *)groupIdentifier
314 {
315     _groupIdentifier = groupIdentifier;
316 }
317
318 - (BOOL)_treatsSHA1SignedCertificatesAsInsecure
319 {
320     return _treatsSHA1SignedCertificatesAsInsecure;
321 }
322
323 - (void)_setTreatsSHA1SignedCertificatesAsInsecure:(BOOL)insecure
324 {
325     _treatsSHA1SignedCertificatesAsInsecure = insecure;
326 }
327
328 #if PLATFORM(IOS)
329 - (BOOL)_alwaysRunsAtForegroundPriority
330 {
331     return _alwaysRunsAtForegroundPriority;
332 }
333
334 - (void)_setAlwaysRunsAtForegroundPriority:(BOOL)alwaysRunsAtForegroundPriority
335 {
336     _alwaysRunsAtForegroundPriority = alwaysRunsAtForegroundPriority;
337 }
338 #endif
339
340 @end
341
342 @implementation WKWebViewConfiguration (WKDeprecated)
343
344 #if PLATFORM(IOS)
345 - (BOOL)mediaPlaybackAllowsAirPlay
346 {
347     return self.allowsAirPlayForMediaPlayback;
348 }
349
350 - (void)setMediaPlaybackAllowsAirPlay:(BOOL)allowed
351 {
352     self.allowsAirPlayForMediaPlayback = allowed;
353 }
354
355 - (BOOL)mediaPlaybackRequiresUserAction
356 {
357     return self.requiresUserActionForMediaPlayback;
358 }
359
360 - (void)setMediaPlaybackRequiresUserAction:(BOOL)required
361 {
362     self.requiresUserActionForMediaPlayback = required;
363 }
364 #endif // PLATFORM(IOS)
365
366 @end
367
368 #endif // WK_API_ENABLED