Add WebProcessPool option for process swapping on navigation.
[WebKit-https.git] / Source / WebKit / UIProcess / API / Cocoa / _WKProcessPoolConfiguration.mm
1 /*
2  * Copyright (C) 2014 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 "_WKProcessPoolConfigurationInternal.h"
28
29 #if WK_API_ENABLED
30
31 #import <wtf/RetainPtr.h>
32
33 @implementation _WKProcessPoolConfiguration
34
35 - (instancetype)init
36 {
37     if (!(self = [super init]))
38         return nil;
39
40     API::Object::constructInWrapper<API::ProcessPoolConfiguration>(self);
41
42     return self;
43 }
44
45 - (void)dealloc
46 {
47     _processPoolConfiguration->~ProcessPoolConfiguration();
48
49     [super dealloc];
50 }
51
52 - (NSURL *)injectedBundleURL
53 {
54     return [NSURL fileURLWithPath:_processPoolConfiguration->injectedBundlePath()];
55 }
56
57 - (void)setInjectedBundleURL:(NSURL *)injectedBundleURL
58 {
59     if (injectedBundleURL && !injectedBundleURL.isFileURL)
60         [NSException raise:NSInvalidArgumentException format:@"Injected Bundle URL must be a file URL"];
61
62     _processPoolConfiguration->setInjectedBundlePath(injectedBundleURL.path);
63 }
64
65 - (NSUInteger)maximumProcessCount
66 {
67     return _processPoolConfiguration->maximumProcessCount();
68 }
69
70 - (void)setMaximumProcessCount:(NSUInteger)maximumProcessCount
71 {
72     _processPoolConfiguration->setMaximumProcessCount(maximumProcessCount);
73 }
74
75 - (NSInteger)diskCacheSizeOverride
76 {
77     return _processPoolConfiguration->diskCacheSizeOverride();
78 }
79
80 - (void)setDiskCacheSizeOverride:(NSInteger)size
81 {
82     _processPoolConfiguration->setDiskCacheSizeOverride(size);
83 }
84
85 - (BOOL)diskCacheSpeculativeValidationEnabled
86 {
87     return _processPoolConfiguration->diskCacheSpeculativeValidationEnabled();
88 }
89
90 - (void)setDiskCacheSpeculativeValidationEnabled:(BOOL)enabled
91 {
92     _processPoolConfiguration->setDiskCacheSpeculativeValidationEnabled(enabled);
93 }
94
95 - (BOOL)ignoreSynchronousMessagingTimeoutsForTesting
96 {
97     return _processPoolConfiguration->ignoreSynchronousMessagingTimeoutsForTesting();
98 }
99
100 - (void)setIgnoreSynchronousMessagingTimeoutsForTesting:(BOOL)ignoreSynchronousMessagingTimeoutsForTesting
101 {
102     _processPoolConfiguration->setIgnoreSynchronousMessagingTimeoutsForTesting(ignoreSynchronousMessagingTimeoutsForTesting);
103 }
104
105 - (NSArray<NSURL *> *)additionalReadAccessAllowedURLs
106 {
107     auto paths = _processPoolConfiguration->additionalReadAccessAllowedPaths();
108     if (paths.isEmpty())
109         return @[ ];
110
111     NSMutableArray *urls = [NSMutableArray arrayWithCapacity:paths.size()];
112     for (const auto& path : paths)
113         [urls addObject:[NSURL fileURLWithFileSystemRepresentation:path.data() isDirectory:NO relativeToURL:nil]];
114
115     return urls;
116 }
117
118 - (void)setAdditionalReadAccessAllowedURLs:(NSArray<NSURL *> *)additionalReadAccessAllowedURLs
119 {
120     Vector<CString> paths;
121     paths.reserveInitialCapacity(additionalReadAccessAllowedURLs.count);
122     for (NSURL *url in additionalReadAccessAllowedURLs) {
123         if (!url.isFileURL)
124             [NSException raise:NSInvalidArgumentException format:@"%@ is not a file URL", url];
125
126         paths.uncheckedAppend(url.fileSystemRepresentation);
127     }
128
129     _processPoolConfiguration->setAdditionalReadAccessAllowedPaths(WTFMove(paths));
130 }
131
132 - (NSArray *)cachePartitionedURLSchemes
133 {
134     auto schemes = _processPoolConfiguration->cachePartitionedURLSchemes();
135     if (schemes.isEmpty())
136         return @[];
137
138     NSMutableArray *array = [NSMutableArray arrayWithCapacity:schemes.size()];
139     for (const auto& scheme : schemes)
140         [array addObject:(NSString *)scheme];
141
142     return array;
143 }
144
145 - (void)setCachePartitionedURLSchemes:(NSArray *)cachePartitionedURLSchemes
146 {
147     Vector<String> schemes;
148     for (id urlScheme in cachePartitionedURLSchemes) {
149         if ([urlScheme isKindOfClass:[NSString class]])
150             schemes.append(String((NSString *)urlScheme));
151     }
152     
153     _processPoolConfiguration->setCachePartitionedURLSchemes(WTFMove(schemes));
154 }
155
156 - (NSArray *)alwaysRevalidatedURLSchemes
157 {
158     auto& schemes = _processPoolConfiguration->alwaysRevalidatedURLSchemes();
159     if (schemes.isEmpty())
160         return @[];
161
162     NSMutableArray *array = [NSMutableArray arrayWithCapacity:schemes.size()];
163     for (auto& scheme : schemes)
164         [array addObject:(NSString *)scheme];
165
166     return array;
167 }
168
169 - (void)setAlwaysRevalidatedURLSchemes:(NSArray *)alwaysRevalidatedURLSchemes
170 {
171     Vector<String> schemes;
172     schemes.reserveInitialCapacity(alwaysRevalidatedURLSchemes.count);
173     for (id scheme in alwaysRevalidatedURLSchemes) {
174         if ([scheme isKindOfClass:[NSString class]])
175             schemes.append((NSString *)scheme);
176     }
177
178     _processPoolConfiguration->setAlwaysRevalidatedURLSchemes(WTFMove(schemes));
179 }
180
181 - (NSString *)sourceApplicationBundleIdentifier
182 {
183     return _processPoolConfiguration->sourceApplicationBundleIdentifier();
184 }
185
186 - (void)setSourceApplicationBundleIdentifier:(NSString *)sourceApplicationBundleIdentifier
187 {
188     _processPoolConfiguration->setSourceApplicationBundleIdentifier(sourceApplicationBundleIdentifier);
189 }
190
191 - (NSString *)sourceApplicationSecondaryIdentifier
192 {
193     return _processPoolConfiguration->sourceApplicationSecondaryIdentifier();
194 }
195
196 - (void)setSourceApplicationSecondaryIdentifier:(NSString *)sourceApplicationSecondaryIdentifier
197 {
198     _processPoolConfiguration->setSourceApplicationSecondaryIdentifier(sourceApplicationSecondaryIdentifier);
199 }
200
201 - (BOOL)allowsCellularAccess
202 {
203     return YES;
204 }
205
206 - (void)setAllowsCellularAccess:(BOOL)allowsCellularAccess
207 {
208 }
209
210 - (BOOL)shouldCaptureAudioInUIProcess
211 {
212     return _processPoolConfiguration->shouldCaptureAudioInUIProcess();
213 }
214
215 - (void)setShouldCaptureAudioInUIProcess:(BOOL)shouldCaptureAudioInUIProcess
216 {
217     _processPoolConfiguration->setShouldCaptureAudioInUIProcess(shouldCaptureAudioInUIProcess);
218 }
219
220 - (void)setPresentingApplicationPID:(pid_t)presentingApplicationPID
221 {
222     _processPoolConfiguration->setPresentingApplicationPID(presentingApplicationPID);
223 }
224
225 - (pid_t)presentingApplicationPID
226 {
227     return _processPoolConfiguration->presentingApplicationPID();
228 }
229
230 - (void)setProcessSwapsOnNavigation:(BOOL)swaps
231 {
232     _processPoolConfiguration->setProcessSwapsOnNavigation(swaps);
233 }
234
235 - (BOOL)processSwapsOnNavigation
236 {
237     return _processPoolConfiguration->processSwapsOnNavigation();
238 }
239
240 #if PLATFORM(IOS)
241 - (NSString *)CTDataConnectionServiceType
242 {
243     return _processPoolConfiguration->ctDataConnectionServiceType();
244 }
245
246 - (void)setCTDataConnectionServiceType:(NSString *)ctDataConnectionServiceType
247 {
248     _processPoolConfiguration->setCTDataConnectionServiceType(ctDataConnectionServiceType);
249 }
250
251 - (BOOL)alwaysRunsAtBackgroundPriority
252 {
253     return _processPoolConfiguration->alwaysRunsAtBackgroundPriority();
254 }
255
256 - (void)setAlwaysRunsAtBackgroundPriority:(BOOL)alwaysRunsAtBackgroundPriority
257 {
258     _processPoolConfiguration->setAlwaysRunsAtBackgroundPriority(alwaysRunsAtBackgroundPriority);
259 }
260
261 - (BOOL)shouldTakeUIBackgroundAssertion
262 {
263     return _processPoolConfiguration->shouldTakeUIBackgroundAssertion();
264 }
265
266 - (void)setShouldTakeUIBackgroundAssertion:(BOOL)shouldTakeUIBackgroundAssertion
267 {
268     return _processPoolConfiguration->setShouldTakeUIBackgroundAssertion(shouldTakeUIBackgroundAssertion);
269 }
270 #endif
271
272 - (NSString *)description
273 {
274     NSString *description = [NSString stringWithFormat:@"<%@: %p; maximumProcessCount = %lu", NSStringFromClass(self.class), self, static_cast<unsigned long>([self maximumProcessCount])];
275
276     if (!_processPoolConfiguration->injectedBundlePath().isEmpty())
277         return [description stringByAppendingFormat:@"; injectedBundleURL: \"%@\">", [self injectedBundleURL]];
278
279     return [description stringByAppendingString:@">"];
280 }
281
282 - (id)copyWithZone:(NSZone *)zone
283 {
284     return wrapper(_processPoolConfiguration->copy().leakRef());
285 }
286
287 #pragma mark WKObject protocol implementation
288
289 - (API::Object&)_apiObject
290 {
291     return *_processPoolConfiguration;
292 }
293
294 @end
295
296 #endif