Modern IDB: New database versions are never committed to SQLite.
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / Cocoa / WKProcessPool.mm
1 /*
2  * Copyright (C) 2014, 2016 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 "WKProcessPoolInternal.h"
28
29 #if WK_API_ENABLED
30
31 #import "AutomationClient.h"
32 #import "CacheModel.h"
33 #import "DownloadClient.h"
34 #import "SandboxUtilities.h"
35 #import "WKObject.h"
36 #import "WeakObjCPtr.h"
37 #import "WebCertificateInfo.h"
38 #import "WebCookieManagerProxy.h"
39 #import "WebProcessMessages.h"
40 #import "WebProcessPool.h"
41 #import "_WKAutomationDelegate.h"
42 #import "_WKAutomationSessionInternal.h"
43 #import "_WKDownloadDelegate.h"
44 #import "_WKProcessPoolConfigurationInternal.h"
45 #import <WebCore/CFNetworkSPI.h>
46 #import <WebCore/CertificateInfo.h>
47 #import <wtf/RetainPtr.h>
48
49 #if PLATFORM(IOS)
50 #import <WebCore/WebCoreThreadSystemInterface.h>
51 #import "WKGeolocationProviderIOS.h"
52 #endif
53
54 static WKProcessPool *sharedProcessPool;
55
56 @implementation WKProcessPool {
57     WebKit::WeakObjCPtr<id <_WKAutomationDelegate>> _automationDelegate;
58     WebKit::WeakObjCPtr<id <_WKDownloadDelegate>> _downloadDelegate;
59
60     RetainPtr<_WKAutomationSession> _automationSession;
61 #if PLATFORM(IOS)
62     RetainPtr<WKGeolocationProviderIOS> _geolocationProvider;
63 #endif // PLATFORM(IOS)
64 }
65
66 - (instancetype)_initWithConfiguration:(_WKProcessPoolConfiguration *)configuration
67 {
68     if (!(self = [super init]))
69         return nil;
70
71 #if PLATFORM(IOS)
72     // FIXME: Remove once <rdar://problem/15256572> is fixed.
73     InitWebCoreThreadSystemInterface();
74 #endif
75
76     API::Object::constructInWrapper<WebKit::WebProcessPool>(self, *configuration->_processPoolConfiguration);
77
78     return self;
79 }
80
81 - (instancetype)init
82 {
83     return [self _initWithConfiguration:adoptNS([[_WKProcessPoolConfiguration alloc] init]).get()];
84 }
85
86 - (void)dealloc
87 {
88     _processPool->~WebProcessPool();
89
90     [super dealloc];
91 }
92
93 - (void)encodeWithCoder:(NSCoder *)coder
94 {
95     if (self == sharedProcessPool) {
96         [coder encodeBool:YES forKey:@"isSharedProcessPool"];
97         return;
98     }
99 }
100
101 - (instancetype)initWithCoder:(NSCoder *)coder
102 {
103     if (!(self = [self init]))
104         return nil;
105
106     if ([coder decodeBoolForKey:@"isSharedProcessPool"]) {
107         [self release];
108
109         return [[WKProcessPool _sharedProcessPool] retain];
110     }
111
112     return self;
113 }
114
115 - (NSString *)description
116 {
117     return [NSString stringWithFormat:@"<%@: %p; configuration = %@>", NSStringFromClass(self.class), self, wrapper(_processPool->configuration())];
118 }
119
120 - (_WKProcessPoolConfiguration *)_configuration
121 {
122     return wrapper(_processPool->configuration().copy().leakRef());
123 }
124
125 - (API::Object&)_apiObject
126 {
127     return *_processPool;
128 }
129
130 #if PLATFORM(IOS)
131 - (WKGeolocationProviderIOS *)_geolocationProvider
132 {
133     if (!_geolocationProvider)
134         _geolocationProvider = adoptNS([[WKGeolocationProviderIOS alloc] initWithProcessPool:*_processPool]);
135     return _geolocationProvider.get();
136 }
137 #endif // PLATFORM(IOS)
138
139 @end
140
141 @implementation WKProcessPool (WKPrivate)
142
143 + (WKProcessPool *)_sharedProcessPool
144 {
145     static dispatch_once_t onceToken;
146     dispatch_once(&onceToken, ^{
147         sharedProcessPool = [[WKProcessPool alloc] init];
148     });
149
150     return sharedProcessPool;
151 }
152
153 + (NSURL *)_websiteDataURLForContainerWithURL:(NSURL *)containerURL
154 {
155     return [WKProcessPool _websiteDataURLForContainerWithURL:containerURL bundleIdentifierIfNotInContainer:nil];
156 }
157
158 + (NSURL *)_websiteDataURLForContainerWithURL:(NSURL *)containerURL bundleIdentifierIfNotInContainer:(NSString *)bundleIdentifier
159 {
160     NSURL *url = [containerURL URLByAppendingPathComponent:@"Library" isDirectory:YES];
161     url = [url URLByAppendingPathComponent:@"WebKit" isDirectory:YES];
162
163     if (!WebKit::processHasContainer() && bundleIdentifier)
164         url = [url URLByAppendingPathComponent:bundleIdentifier isDirectory:YES];
165
166     return [url URLByAppendingPathComponent:@"WebsiteData" isDirectory:YES];
167 }
168
169 - (void)_setAllowsSpecificHTTPSCertificate:(NSArray *)certificateChain forHost:(NSString *)host
170 {
171     _processPool->allowSpecificHTTPSCertificateForHost(WebKit::WebCertificateInfo::create(WebCore::CertificateInfo((CFArrayRef)certificateChain)).ptr(), host);
172 }
173
174 - (void)_setCanHandleHTTPSServerTrustEvaluation:(BOOL)value
175 {
176     _processPool->setCanHandleHTTPSServerTrustEvaluation(value);
177 }
178
179 static WebKit::HTTPCookieAcceptPolicy toHTTPCookieAcceptPolicy(NSHTTPCookieAcceptPolicy policy)
180 {
181     switch (static_cast<NSUInteger>(policy)) {
182     case NSHTTPCookieAcceptPolicyAlways:
183         return WebKit::HTTPCookieAcceptPolicyAlways;
184     case NSHTTPCookieAcceptPolicyNever:
185         return WebKit::HTTPCookieAcceptPolicyNever;
186     case NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain:
187         return WebKit::HTTPCookieAcceptPolicyOnlyFromMainDocumentDomain;
188     case NSHTTPCookieAcceptPolicyExclusivelyFromMainDocumentDomain:
189         return WebKit::HTTPCookieAcceptPolicyExclusivelyFromMainDocumentDomain;
190     }
191
192     ASSERT_NOT_REACHED();
193     return WebKit::HTTPCookieAcceptPolicyAlways;
194 }
195
196 - (void)_setCookieAcceptPolicy:(NSHTTPCookieAcceptPolicy)policy
197 {
198     _processPool->supplement<WebKit::WebCookieManagerProxy>()->setHTTPCookieAcceptPolicy(toHTTPCookieAcceptPolicy(policy));
199 }
200
201 - (id)_objectForBundleParameter:(NSString *)parameter
202 {
203     return [_processPool->bundleParameters() objectForKey:parameter];
204 }
205
206 - (void)_setObject:(id <NSCopying, NSSecureCoding>)object forBundleParameter:(NSString *)parameter
207 {
208     auto copy = adoptNS([(NSObject *)object copy]);
209
210     auto data = adoptNS([[NSMutableData alloc] init]);
211     auto keyedArchiver = adoptNS([[NSKeyedArchiver alloc] initForWritingWithMutableData:data.get()]);
212     [keyedArchiver setRequiresSecureCoding:YES];
213
214     @try {
215         [keyedArchiver encodeObject:copy.get() forKey:@"parameter"];
216         [keyedArchiver finishEncoding];
217     } @catch (NSException *exception) {
218         LOG_ERROR("Failed to encode bundle parameter: %@", exception);
219     }
220
221     if (copy)
222         [_processPool->ensureBundleParameters() setObject:copy.get() forKey:parameter];
223     else
224         [_processPool->ensureBundleParameters() removeObjectForKey:parameter];
225
226     _processPool->sendToAllProcesses(Messages::WebProcess::SetInjectedBundleParameter(parameter, IPC::DataReference(static_cast<const uint8_t*>([data bytes]), [data length])));
227 }
228
229 - (void)_setObjectsForBundleParametersWithDictionary:(NSDictionary *)dictionary
230 {
231     auto copy = adoptNS([[NSDictionary alloc] initWithDictionary:dictionary copyItems:YES]);
232
233     auto data = adoptNS([[NSMutableData alloc] init]);
234     auto keyedArchiver = adoptNS([[NSKeyedArchiver alloc] initForWritingWithMutableData:data.get()]);
235     [keyedArchiver setRequiresSecureCoding:YES];
236
237     @try {
238         [keyedArchiver encodeObject:copy.get() forKey:@"parameters"];
239         [keyedArchiver finishEncoding];
240     } @catch (NSException *exception) {
241         LOG_ERROR("Failed to encode bundle parameters: %@", exception);
242     }
243
244     [_processPool->ensureBundleParameters() setValuesForKeysWithDictionary:copy.get()];
245
246     _processPool->sendToAllProcesses(Messages::WebProcess::SetInjectedBundleParameters(IPC::DataReference(static_cast<const uint8_t*>([data bytes]), [data length])));
247 }
248
249
250 - (id <_WKDownloadDelegate>)_downloadDelegate
251 {
252     return _downloadDelegate.getAutoreleased();
253 }
254
255 - (void)_setDownloadDelegate:(id <_WKDownloadDelegate>)downloadDelegate
256 {
257     _downloadDelegate = downloadDelegate;
258     _processPool->setDownloadClient(std::make_unique<WebKit::DownloadClient>(downloadDelegate));
259 }
260
261 - (id <_WKAutomationDelegate>)_automationDelegate
262 {
263     return _automationDelegate.getAutoreleased();
264 }
265
266 - (void)_setAutomationDelegate:(id <_WKAutomationDelegate>)automationDelegate
267 {
268     _automationDelegate = automationDelegate;
269     _processPool->setAutomationClient(std::make_unique<WebKit::AutomationClient>(self, automationDelegate));
270 }
271
272 - (void)_warmInitialProcess
273 {
274     _processPool->warmInitialProcess();
275 }
276
277 - (void)_automationCapabilitiesDidChange
278 {
279     _processPool->updateAutomationCapabilities();
280 }
281
282 - (void)_setAutomationSession:(_WKAutomationSession *)automationSession
283 {
284     _automationSession = automationSession;
285     _processPool->setAutomationSession(automationSession ? automationSession->_session.get() : nullptr);
286 }
287
288 - (void)_terminateDatabaseProcess
289 {
290     _processPool->terminateDatabaseProcess();
291 }
292
293 @end
294
295 #endif // WK_API_ENABLED