01dfa864dd7cb27cd59c1372db9a0c0a1cc20add
[WebKit-https.git] / Source / WebKit2 / UIProcess / API / Cocoa / WKProcessPool.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 "WKProcessPoolInternal.h"
28
29 #if WK_API_ENABLED
30
31 #import "CacheModel.h"
32 #import "DownloadClient.h"
33 #import "ProcessModel.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 "_WKDownloadDelegate.h"
42 #import "_WKProcessPoolConfigurationInternal.h"
43 #import <WebCore/CFNetworkSPI.h>
44 #import <WebCore/CertificateInfo.h>
45 #import <wtf/RetainPtr.h>
46
47 #if PLATFORM(IOS)
48 #import <WebCore/WebCoreThreadSystemInterface.h>
49 #import "WKGeolocationProviderIOS.h"
50 #endif
51
52 @implementation WKProcessPool {
53     WebKit::WeakObjCPtr<id <_WKDownloadDelegate>> _downloadDelegate;
54
55 #if PLATFORM(IOS)
56     RetainPtr<WKGeolocationProviderIOS> _geolocationProvider;
57 #endif // PLATFORM(IOS)
58 }
59
60 - (instancetype)_initWithConfiguration:(_WKProcessPoolConfiguration *)configuration
61 {
62     if (!(self = [super init]))
63         return nil;
64
65 #if PLATFORM(IOS)
66     // FIXME: Remove once <rdar://problem/15256572> is fixed.
67     InitWebCoreThreadSystemInterface();
68 #endif
69
70     API::Object::constructInWrapper<WebKit::WebProcessPool>(self, *configuration->_processPoolConfiguration);
71
72     return self;
73 }
74
75 - (instancetype)init
76 {
77     return [self _initWithConfiguration:adoptNS([[_WKProcessPoolConfiguration alloc] init]).get()];
78 }
79
80 - (void)dealloc
81 {
82     _processPool->~WebProcessPool();
83
84     [super dealloc];
85 }
86
87 - (NSString *)description
88 {
89     return [NSString stringWithFormat:@"<%@: %p; configuration = %@>", NSStringFromClass(self.class), self, wrapper(_processPool->configuration())];
90 }
91
92 - (_WKProcessPoolConfiguration *)_configuration
93 {
94     return wrapper(_processPool->configuration().copy().leakRef());
95 }
96
97 - (API::Object&)_apiObject
98 {
99     return *_processPool;
100 }
101
102 #if PLATFORM(IOS)
103 - (WKGeolocationProviderIOS *)_geolocationProvider
104 {
105     if (!_geolocationProvider)
106         _geolocationProvider = adoptNS([[WKGeolocationProviderIOS alloc] initWithProcessPool:*_processPool]);
107     return _geolocationProvider.get();
108 }
109 #endif // PLATFORM(IOS)
110
111 @end
112
113 @implementation WKProcessPool (WKPrivate)
114
115 + (NSURL *)_websiteDataURLForContainerWithURL:(NSURL *)containerURL
116 {
117     return [WKProcessPool _websiteDataURLForContainerWithURL:containerURL bundleIdentifierIfNotInContainer:nil];
118 }
119
120 + (NSURL *)_websiteDataURLForContainerWithURL:(NSURL *)containerURL bundleIdentifierIfNotInContainer:(NSString *)bundleIdentifier
121 {
122     NSURL *url = [containerURL URLByAppendingPathComponent:@"Library" isDirectory:YES];
123     url = [url URLByAppendingPathComponent:@"WebKit" isDirectory:YES];
124
125     if (!WebKit::processHasContainer() && bundleIdentifier)
126         url = [url URLByAppendingPathComponent:bundleIdentifier isDirectory:YES];
127
128     return [url URLByAppendingPathComponent:@"WebsiteData" isDirectory:YES];
129 }
130
131 - (void)_setAllowsSpecificHTTPSCertificate:(NSArray *)certificateChain forHost:(NSString *)host
132 {
133     _processPool->allowSpecificHTTPSCertificateForHost(WebKit::WebCertificateInfo::create(WebCore::CertificateInfo((CFArrayRef)certificateChain)).ptr(), host);
134 }
135
136 - (void)_setCanHandleHTTPSServerTrustEvaluation:(BOOL)value
137 {
138     _processPool->setCanHandleHTTPSServerTrustEvaluation(value);
139 }
140
141 static WebKit::HTTPCookieAcceptPolicy toHTTPCookieAcceptPolicy(NSHTTPCookieAcceptPolicy policy)
142 {
143     switch (static_cast<NSUInteger>(policy)) {
144     case NSHTTPCookieAcceptPolicyAlways:
145         return WebKit::HTTPCookieAcceptPolicyAlways;
146     case NSHTTPCookieAcceptPolicyNever:
147         return WebKit::HTTPCookieAcceptPolicyNever;
148     case NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain:
149         return WebKit::HTTPCookieAcceptPolicyOnlyFromMainDocumentDomain;
150     case NSHTTPCookieAcceptPolicyExclusivelyFromMainDocumentDomain:
151         return WebKit::HTTPCookieAcceptPolicyExclusivelyFromMainDocumentDomain;
152     }
153
154     ASSERT_NOT_REACHED();
155     return WebKit::HTTPCookieAcceptPolicyAlways;
156 }
157
158 - (void)_setCookieAcceptPolicy:(NSHTTPCookieAcceptPolicy)policy
159 {
160     _processPool->supplement<WebKit::WebCookieManagerProxy>()->setHTTPCookieAcceptPolicy(toHTTPCookieAcceptPolicy(policy));
161 }
162
163 - (id)_objectForBundleParameter:(NSString *)parameter
164 {
165     return [_processPool->bundleParameters() objectForKey:parameter];
166 }
167
168 - (void)_setObject:(id <NSCopying, NSSecureCoding>)object forBundleParameter:(NSString *)parameter
169 {
170     auto copy = adoptNS([(NSObject *)object copy]);
171
172     auto data = adoptNS([[NSMutableData alloc] init]);
173     auto keyedArchiver = adoptNS([[NSKeyedArchiver alloc] initForWritingWithMutableData:data.get()]);
174     [keyedArchiver setRequiresSecureCoding:YES];
175
176     @try {
177         [keyedArchiver encodeObject:copy.get() forKey:@"parameter"];
178         [keyedArchiver finishEncoding];
179     } @catch (NSException *exception) {
180         LOG_ERROR("Failed to encode bundle parameter: %@", exception);
181     }
182
183     if (copy)
184         [_processPool->ensureBundleParameters() setObject:copy.get() forKey:parameter];
185     else
186         [_processPool->ensureBundleParameters() removeObjectForKey:parameter];
187
188     _processPool->sendToAllProcesses(Messages::WebProcess::SetInjectedBundleParameter(parameter, IPC::DataReference(static_cast<const uint8_t*>([data bytes]), [data length])));
189 }
190
191 - (void)_setObjectsForBundleParametersWithDictionary:(NSDictionary *)dictionary
192 {
193     auto copy = adoptNS([[NSDictionary alloc] initWithDictionary:dictionary copyItems:YES]);
194
195     auto data = adoptNS([[NSMutableData alloc] init]);
196     auto keyedArchiver = adoptNS([[NSKeyedArchiver alloc] initForWritingWithMutableData:data.get()]);
197     [keyedArchiver setRequiresSecureCoding:YES];
198
199     @try {
200         [keyedArchiver encodeObject:copy.get() forKey:@"parameters"];
201         [keyedArchiver finishEncoding];
202     } @catch (NSException *exception) {
203         LOG_ERROR("Failed to encode bundle parameters: %@", exception);
204     }
205
206     [_processPool->ensureBundleParameters() setValuesForKeysWithDictionary:copy.get()];
207
208     _processPool->sendToAllProcesses(Messages::WebProcess::SetInjectedBundleParameters(IPC::DataReference(static_cast<const uint8_t*>([data bytes]), [data length])));
209 }
210
211
212 - (id <_WKDownloadDelegate>)_downloadDelegate
213 {
214     return _downloadDelegate.getAutoreleased();
215 }
216
217 - (void)_setDownloadDelegate:(id <_WKDownloadDelegate>)downloadDelegate
218 {
219     _downloadDelegate = downloadDelegate;
220     _processPool->setDownloadClient(std::make_unique<WebKit::DownloadClient>(downloadDelegate));
221 }
222
223 @end
224
225 #endif // WK_API_ENABLED