ef1ed91d3cfce31c5753f15edde49c6e7ee77102
[WebKit-https.git] / Source / WebKit / UIProcess / API / Cocoa / _WKWebsiteDataStoreConfiguration.mm
1 /*
2  * Copyright (C) 2017 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 "_WKWebsiteDataStoreConfigurationInternal.h"
28
29 #import <wtf/RetainPtr.h>
30
31 static void checkURLArgument(NSURL *url)
32 {
33     if (url && ![url isFileURL])
34         [NSException raise:NSInvalidArgumentException format:@"%@ is not a file URL", url];
35 }
36
37 @implementation _WKWebsiteDataStoreConfiguration
38
39 - (instancetype)init
40 {
41     self = [super init];
42     if (!self)
43         return nil;
44
45     _configuration->setPersistent(true);
46
47     return self;
48 }
49
50 - (instancetype)initNonPersistentConfiguration
51 {
52     self = [super init];
53     if (!self)
54         return nil;
55
56     _configuration->setPersistent(false);
57
58     return self;
59 }
60
61 - (BOOL)isPersistent
62 {
63     return _configuration->isPersistent();
64 }
65
66 - (NSURL *)_webStorageDirectory
67 {
68     return [NSURL fileURLWithPath:_configuration->localStorageDirectory() isDirectory:YES];
69 }
70
71 - (void)_setWebStorageDirectory:(NSURL *)url
72 {
73     if (!_configuration->isPersistent())
74         [NSException raise:NSInvalidArgumentException format:@"Cannot set _webStorageDirectory on a non-persistent _WKWebsiteDataStoreConfiguration."];
75     checkURLArgument(url);
76     _configuration->setLocalStorageDirectory(url.path);
77 }
78
79 - (NSURL *)_indexedDBDatabaseDirectory
80 {
81     return [NSURL fileURLWithPath:_configuration->indexedDBDatabaseDirectory() isDirectory:YES];
82 }
83
84 - (void)_setIndexedDBDatabaseDirectory:(NSURL *)url
85 {
86     if (!_configuration->isPersistent())
87         [NSException raise:NSInvalidArgumentException format:@"Cannot set _indexedDBDatabaseDirectory on a non-persistent _WKWebsiteDataStoreConfiguration."];
88     checkURLArgument(url);
89     _configuration->setIndexedDBDatabaseDirectory(url.path);
90 }
91
92 - (NSURL *)networkCacheDirectory
93 {
94     return [NSURL fileURLWithPath:_configuration->networkCacheDirectory() isDirectory:YES];
95 }
96
97 - (void)setNetworkCacheDirectory:(NSURL *)url
98 {
99     if (!_configuration->isPersistent())
100         [NSException raise:NSInvalidArgumentException format:@"Cannot set networkCacheDirectory on a non-persistent _WKWebsiteDataStoreConfiguration."];
101     checkURLArgument(url);
102     _configuration->setNetworkCacheDirectory(url.path);
103 }
104
105 - (NSURL *)deviceIdHashSaltsStorageDirectory
106 {
107     return [NSURL fileURLWithPath:_configuration->deviceIdHashSaltsStorageDirectory() isDirectory:YES];
108 }
109
110 - (void)setDeviceIdHashSaltsStorageDirectory:(NSURL *)url
111 {
112     if (!_configuration->isPersistent())
113         [NSException raise:NSInvalidArgumentException format:@"Cannot set deviceIdHashSaltsStorageDirectory on a non-persistent _WKWebsiteDataStoreConfiguration."];
114     checkURLArgument(url);
115     _configuration->setDeviceIdHashSaltsStorageDirectory(url.path);
116 }
117
118 - (NSURL *)_webSQLDatabaseDirectory
119 {
120     return [NSURL fileURLWithPath:_configuration->webSQLDatabaseDirectory() isDirectory:YES];
121 }
122
123 - (void)_setWebSQLDatabaseDirectory:(NSURL *)url
124 {
125     if (!_configuration->isPersistent())
126         [NSException raise:NSInvalidArgumentException format:@"Cannot set _webSQLDatabaseDirectory on a non-persistent _WKWebsiteDataStoreConfiguration."];
127     checkURLArgument(url);
128     _configuration->setWebSQLDatabaseDirectory(url.path);
129 }
130
131 - (NSURL *)httpProxy
132 {
133     return _configuration->httpProxy();
134 }
135
136 - (void)setHTTPProxy:(NSURL *)proxy
137 {
138     _configuration->setHTTPProxy(proxy);
139 }
140
141 - (NSURL *)httpsProxy
142 {
143     return _configuration->httpsProxy();
144 }
145
146 - (void)setHTTPSProxy:(NSURL *)proxy
147 {
148     _configuration->setHTTPSProxy(proxy);
149 }
150
151 - (NSURL *)_cookieStorageFile
152 {
153     return [NSURL fileURLWithPath:_configuration->cookieStorageFile() isDirectory:NO];
154 }
155
156 - (void)_setCookieStorageFile:(NSURL *)url
157 {
158     if (!_configuration->isPersistent())
159         [NSException raise:NSInvalidArgumentException format:@"Cannot set _cookieStorageFile on a non-persistent _WKWebsiteDataStoreConfiguration."];
160     checkURLArgument(url);
161     if ([url hasDirectoryPath])
162         [NSException raise:NSInvalidArgumentException format:@"The cookie storage path must point to a file, not a directory."];
163
164     _configuration->setCookieStorageFile(url.path);
165 }
166
167 - (NSURL *)_resourceLoadStatisticsDirectory
168 {
169     return [NSURL fileURLWithPath:_configuration->resourceLoadStatisticsDirectory() isDirectory:YES];
170 }
171
172 - (void)_setResourceLoadStatisticsDirectory:(NSURL *)url
173 {
174     if (!_configuration->isPersistent())
175         [NSException raise:NSInvalidArgumentException format:@"Cannot set _resourceLoadStatisticsDirectory on a non-persistent _WKWebsiteDataStoreConfiguration."];
176     checkURLArgument(url);
177     _configuration->setResourceLoadStatisticsDirectory(url.path);
178 }
179
180 - (NSURL *)_cacheStorageDirectory
181 {
182     return [NSURL fileURLWithPath:_configuration->cacheStorageDirectory() isDirectory:YES];
183 }
184
185 - (void)_setCacheStorageDirectory:(NSURL *)url
186 {
187     if (!_configuration->isPersistent())
188         [NSException raise:NSInvalidArgumentException format:@"Cannot set _cacheStorageDirectory on a non-persistent _WKWebsiteDataStoreConfiguration."];
189     checkURLArgument(url);
190     _configuration->setCacheStorageDirectory(url.path);
191 }
192
193 - (NSURL *)_serviceWorkerRegistrationDirectory
194 {
195     return [NSURL fileURLWithPath:_configuration->serviceWorkerRegistrationDirectory() isDirectory:YES];
196 }
197
198 - (void)_setServiceWorkerRegistrationDirectory:(NSURL *)url
199 {
200     if (!_configuration->isPersistent())
201         [NSException raise:NSInvalidArgumentException format:@"Cannot set _serviceWorkerRegistrationDirectory on a non-persistent _WKWebsiteDataStoreConfiguration."];
202     checkURLArgument(url);
203     _configuration->setServiceWorkerRegistrationDirectory(url.path);
204 }
205
206 - (void)setSourceApplicationBundleIdentifier:(NSString *)identifier
207 {
208     _configuration->setSourceApplicationBundleIdentifier(identifier);
209 }
210
211 - (NSString *)sourceApplicationBundleIdentifier
212 {
213     return _configuration->sourceApplicationBundleIdentifier();
214 }
215
216 - (NSString *)sourceApplicationSecondaryIdentifier
217 {
218     return _configuration->sourceApplicationSecondaryIdentifier();
219 }
220
221 - (void)setSourceApplicationSecondaryIdentifier:(NSString *)identifier
222 {
223     _configuration->setSourceApplicationSecondaryIdentifier(identifier);
224 }
225
226 - (NSURL *)applicationCacheDirectory
227 {
228     return [NSURL fileURLWithPath:_configuration->applicationCacheDirectory() isDirectory:YES];
229 }
230
231 - (void)setApplicationCacheDirectory:(NSURL *)url
232 {
233     if (!_configuration->isPersistent())
234         [NSException raise:NSInvalidArgumentException format:@"Cannot set applicationCacheDirectory on a non-persistent _WKWebsiteDataStoreConfiguration."];
235     checkURLArgument(url);
236     _configuration->setApplicationCacheDirectory(url.path);
237 }
238
239 - (NSString *)applicationCacheFlatFileSubdirectoryName
240 {
241     return _configuration->applicationCacheFlatFileSubdirectoryName();
242 }
243
244 - (void)setApplicationCacheFlatFileSubdirectoryName:(NSString *)name
245 {
246     if (!_configuration->isPersistent())
247         [NSException raise:NSInvalidArgumentException format:@"Cannot set applicationCacheFlatFileSubdirectoryName on a non-persistent _WKWebsiteDataStoreConfiguration."];
248     _configuration->setApplicationCacheFlatFileSubdirectoryName(name);
249 }
250
251 - (BOOL)deviceManagementRestrictionsEnabled
252 {
253     return _configuration->deviceManagementRestrictionsEnabled();
254 }
255
256 - (void)setDeviceManagementRestrictionsEnabled:(BOOL)enabled
257 {
258     _configuration->setDeviceManagementRestrictionsEnabled(enabled);
259 }
260
261 - (BOOL)allLoadsBlockedByDeviceManagementRestrictionsForTesting
262 {
263     return _configuration->allLoadsBlockedByDeviceManagementRestrictionsForTesting();
264 }
265
266 - (void)setAllLoadsBlockedByDeviceManagementRestrictionsForTesting:(BOOL)blocked
267 {
268     _configuration->setAllLoadsBlockedByDeviceManagementRestrictionsForTesting(blocked);
269 }
270
271 - (API::Object&)_apiObject
272 {
273     return *_configuration;
274 }
275
276 @end