Fix macros for wireless playback
[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<_WKVisitedLinkProvider>> _visitedLinkProvider;
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 _allowsAlternateFullscreen;
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     _allowsAlternateFullscreen = YES;
108 #endif
109 #if ENABLE(WIRELESS_TARGET_PLAYBACK)
110     _allowsAirPlayForMediaPlayback = YES;
111 #endif
112
113     return self;
114 }
115
116 - (NSString *)description
117 {
118     return [NSString stringWithFormat:@"<%@: %p; processPool = %@; preferences = %@>", NSStringFromClass(self.class), self, self.processPool, self.preferences];
119 }
120
121 - (id)copyWithZone:(NSZone *)zone
122 {
123     WKWebViewConfiguration *configuration = [(WKWebViewConfiguration *)[[self class] allocWithZone:zone] init];
124
125     configuration.processPool = self.processPool;
126     configuration.preferences = self.preferences;
127     configuration.userContentController = self.userContentController;
128     configuration.websiteDataStore = self.websiteDataStore;
129     configuration._visitedLinkProvider = self._visitedLinkProvider;
130     configuration._relatedWebView = _relatedWebView.get().get();
131     configuration._alternateWebViewForNavigationGestures = _alternateWebViewForNavigationGestures.get().get();
132     configuration->_treatsSHA1SignedCertificatesAsInsecure = _treatsSHA1SignedCertificatesAsInsecure;
133 #if PLATFORM(IOS)
134     configuration._contentProviderRegistry = self._contentProviderRegistry;
135 #endif
136
137     configuration->_suppressesIncrementalRendering = self->_suppressesIncrementalRendering;
138     configuration.applicationNameForUserAgent = self.applicationNameForUserAgent;
139
140 #if PLATFORM(IOS)
141     configuration->_allowsInlineMediaPlayback = self->_allowsInlineMediaPlayback;
142     configuration->_allowsAlternateFullscreen = self->_allowsAlternateFullscreen;
143     configuration->_requiresUserActionForMediaPlayback = self->_requiresUserActionForMediaPlayback;
144     configuration->_selectionGranularity = self->_selectionGranularity;
145 #endif
146 #if ENABLE(WIRELESS_TARGET_PLAYBACK)
147     configuration->_allowsAirPlayForMediaPlayback = self->_allowsAirPlayForMediaPlayback;
148 #endif
149
150     return configuration;
151 }
152
153 - (WKProcessPool *)processPool
154 {
155     return _processPool.get([] { return adoptNS([[WKProcessPool alloc] init]); });
156 }
157
158 - (void)setProcessPool:(WKProcessPool *)processPool
159 {
160     _processPool.set(processPool);
161 }
162
163 - (WKPreferences *)preferences
164 {
165     return _preferences.get([] { return adoptNS([[WKPreferences alloc] init]); });
166 }
167
168 - (void)setPreferences:(WKPreferences *)preferences
169 {
170     _preferences.set(preferences);
171 }
172
173 - (WKUserContentController *)userContentController
174 {
175     return _userContentController.get([] { return adoptNS([[WKUserContentController alloc] init]); });
176 }
177
178 - (void)setUserContentController:(WKUserContentController *)userContentController
179 {
180     _userContentController.set(userContentController);
181 }
182
183 - (WKWebsiteDataStore *)websiteDataStore
184 {
185     return _websiteDataStore.get([] { return [WKWebsiteDataStore defaultDataStore]; });
186 }
187
188 - (void)setWebsiteDataStore:(WKWebsiteDataStore *)websiteDataStore
189 {
190     _websiteDataStore.set(websiteDataStore);
191 }
192
193 static NSString *defaultApplicationNameForUserAgent()
194 {
195 #if PLATFORM(IOS)
196     return [@"Mobile/" stringByAppendingString:[UIDevice currentDevice].buildVersion];
197 #else
198     return nil;
199 #endif
200 }
201
202 - (NSString *)applicationNameForUserAgent
203 {
204     return _applicationNameForUserAgent.get([] { return defaultApplicationNameForUserAgent(); });
205 }
206
207 - (void)setApplicationNameForUserAgent:(NSString *)applicationNameForUserAgent
208 {
209     _applicationNameForUserAgent.set(adoptNS([applicationNameForUserAgent copy]));
210 }
211
212 - (_WKVisitedLinkProvider *)_visitedLinkProvider
213 {
214     return _visitedLinkProvider.get([] { return adoptNS([[_WKVisitedLinkProvider alloc] init]); });
215 }
216
217 - (void)_setVisitedLinkProvider:(_WKVisitedLinkProvider *)visitedLinkProvider
218 {
219     _visitedLinkProvider.set(visitedLinkProvider);
220 }
221
222 #pragma clang diagnostic push
223 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
224
225 - (_WKWebsiteDataStore *)_websiteDataStore
226 {
227     return self.websiteDataStore ? adoptNS([[_WKWebsiteDataStore alloc] initWithDataStore:self.websiteDataStore]).autorelease() : nullptr;
228 }
229
230 - (void)_setWebsiteDataStore:(_WKWebsiteDataStore *)websiteDataStore
231 {
232     self.websiteDataStore = websiteDataStore ? websiteDataStore->_dataStore.get() : nullptr;
233 }
234
235 #pragma clang diagnostic pop
236
237 #if PLATFORM(IOS)
238 - (WKWebViewContentProviderRegistry *)_contentProviderRegistry
239 {
240     return _contentProviderRegistry.get([] { return adoptNS([[WKWebViewContentProviderRegistry alloc] init]); });
241 }
242
243 - (void)_setContentProviderRegistry:(WKWebViewContentProviderRegistry *)registry
244 {
245     _contentProviderRegistry.set(registry);
246 }
247 #endif
248
249 - (void)_validate
250 {
251     if (!self.processPool)
252         [NSException raise:NSInvalidArgumentException format:@"configuration.processPool is nil"];
253
254     if (!self.preferences)
255         [NSException raise:NSInvalidArgumentException format:@"configuration.preferences is nil"];
256
257     if (!self.userContentController)
258         [NSException raise:NSInvalidArgumentException format:@"configuration.userContentController is nil"];
259
260     if (!self.websiteDataStore)
261         [NSException raise:NSInvalidArgumentException format:@"configuration.websiteDataStore is nil"];
262
263     if (!self._visitedLinkProvider)
264         [NSException raise:NSInvalidArgumentException format:@"configuration._visitedLinkProvider is nil"];
265
266 #if PLATFORM(IOS)
267     if (!self._contentProviderRegistry)
268         [NSException raise:NSInvalidArgumentException format:@"configuration._contentProviderRegistry is nil"];
269 #endif
270 }
271
272 @end
273
274 @implementation WKWebViewConfiguration (WKPrivate)
275
276 - (WKWebView *)_relatedWebView
277 {
278     return _relatedWebView.getAutoreleased();
279 }
280
281 - (void)_setRelatedWebView:(WKWebView *)relatedWebView
282 {
283     _relatedWebView = relatedWebView;
284 }
285
286 - (WKWebView *)_alternateWebViewForNavigationGestures
287 {
288     return _alternateWebViewForNavigationGestures.getAutoreleased();
289 }
290
291 - (void)_setAlternateWebViewForNavigationGestures:(WKWebView *)alternateView
292 {
293     _alternateWebViewForNavigationGestures = alternateView;
294 }
295
296 - (NSString *)_groupIdentifier
297 {
298     return _groupIdentifier.get();
299 }
300
301 - (void)_setGroupIdentifier:(NSString *)groupIdentifier
302 {
303     _groupIdentifier = groupIdentifier;
304 }
305
306 - (BOOL)_treatsSHA1SignedCertificatesAsInsecure
307 {
308     return _treatsSHA1SignedCertificatesAsInsecure;
309 }
310
311 - (void)_setTreatsSHA1SignedCertificatesAsInsecure:(BOOL)insecure
312 {
313     _treatsSHA1SignedCertificatesAsInsecure = insecure;
314 }
315
316 #if PLATFORM(IOS)
317 - (BOOL)_allowsAlternateFullscreen
318 {
319     return _allowsAlternateFullscreen;
320 }
321
322 - (void)_setAllowsAlternateFullscreen:(BOOL)allowed
323 {
324     _allowsAlternateFullscreen = allowed;
325 }
326 #endif
327
328 @end
329
330 @implementation WKWebViewConfiguration (WKDeprecated)
331
332 #if PLATFORM(IOS)
333 - (BOOL)mediaPlaybackAllowsAirPlay
334 {
335     return self.allowsAirPlayForMediaPlayback;
336 }
337
338 - (void)setMediaPlaybackAllowsAirPlay:(BOOL)allowed
339 {
340     self.allowsAirPlayForMediaPlayback = allowed;
341 }
342
343 - (BOOL)mediaPlaybackRequiresUserAction
344 {
345     return self.requiresUserActionForMediaPlayback;
346 }
347
348 - (void)setMediaPlaybackRequiresUserAction:(BOOL)required
349 {
350     self.requiresUserActionForMediaPlayback = required;
351 }
352 #endif // PLATFORM(IOS)
353
354 @end
355
356 #endif // WK_API_ENABLED