2 * Copyright (C) 2014-2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #import "WKWebsiteDataStoreInternal.h"
31 #import "WKHTTPCookieStoreInternal.h"
33 #import "WKWebsiteDataRecordInternal.h"
34 #import "WebResourceLoadStatisticsStore.h"
35 #import "WebResourceLoadStatisticsTelemetry.h"
36 #import "WebsiteDataFetchOption.h"
37 #import "_WKWebsiteDataStoreConfiguration.h"
38 #import <WebCore/URL.h>
39 #import <wtf/BlockPtr.h>
41 using namespace WebCore;
43 @implementation WKWebsiteDataStore
45 + (WKWebsiteDataStore *)defaultDataStore
47 return WebKit::wrapper(API::WebsiteDataStore::defaultDataStore().get());
50 + (WKWebsiteDataStore *)nonPersistentDataStore
52 return [WebKit::wrapper(API::WebsiteDataStore::createNonPersistentDataStore().leakRef()) autorelease];
57 _websiteDataStore->API::WebsiteDataStore::~WebsiteDataStore();
62 - (instancetype)initWithCoder:(NSCoder *)coder
64 if (!(self = [super init]))
67 RetainPtr<WKWebsiteDataStore> dataStore;
68 if ([coder decodeBoolForKey:@"isDefaultDataStore"])
69 dataStore = [WKWebsiteDataStore defaultDataStore];
71 dataStore = [WKWebsiteDataStore nonPersistentDataStore];
75 return dataStore.leakRef();
78 - (void)encodeWithCoder:(NSCoder *)coder
80 if (self == [WKWebsiteDataStore defaultDataStore]) {
81 [coder encodeBool:YES forKey:@"isDefaultDataStore"];
85 ASSERT(!self.persistent);
90 return _websiteDataStore->isPersistent();
93 + (NSSet *)allWebsiteDataTypes
95 static dispatch_once_t onceToken;
96 static NSSet *allWebsiteDataTypes;
97 dispatch_once(&onceToken, ^{
98 allWebsiteDataTypes = [[NSSet alloc] initWithArray:@[ WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache, WKWebsiteDataTypeOfflineWebApplicationCache, WKWebsiteDataTypeCookies, WKWebsiteDataTypeSessionStorage, WKWebsiteDataTypeLocalStorage, WKWebsiteDataTypeIndexedDBDatabases, WKWebsiteDataTypeWebSQLDatabases ]];
101 return allWebsiteDataTypes;
104 - (WKHTTPCookieStore *)httpCookieStore
106 return WebKit::wrapper(_websiteDataStore->httpCookieStore());
109 static std::chrono::system_clock::time_point toSystemClockTime(NSDate *date)
112 using namespace std::chrono;
114 return system_clock::time_point(duration_cast<system_clock::duration>(duration<double>(date.timeIntervalSince1970)));
117 - (void)fetchDataRecordsOfTypes:(NSSet *)dataTypes completionHandler:(void (^)(NSArray<WKWebsiteDataRecord *> *))completionHandler
119 [self _fetchDataRecordsOfTypes:dataTypes withOptions:0 completionHandler:completionHandler];
122 - (void)removeDataOfTypes:(NSSet *)dataTypes modifiedSince:(NSDate *)date completionHandler:(void (^)(void))completionHandler
124 auto completionHandlerCopy = makeBlockPtr(completionHandler);
125 _websiteDataStore->websiteDataStore().removeData(WebKit::toWebsiteDataTypes(dataTypes), toSystemClockTime(date ? date : [NSDate distantPast]), [completionHandlerCopy] {
126 completionHandlerCopy();
130 static Vector<WebKit::WebsiteDataRecord> toWebsiteDataRecords(NSArray *dataRecords)
132 Vector<WebKit::WebsiteDataRecord> result;
134 for (WKWebsiteDataRecord *dataRecord in dataRecords)
135 result.append(dataRecord->_websiteDataRecord->websiteDataRecord());
140 - (void)removeDataOfTypes:(NSSet *)dataTypes forDataRecords:(NSArray *)dataRecords completionHandler:(void (^)(void))completionHandler
142 auto completionHandlerCopy = makeBlockPtr(completionHandler);
144 _websiteDataStore->websiteDataStore().removeData(WebKit::toWebsiteDataTypes(dataTypes), toWebsiteDataRecords(dataRecords), [completionHandlerCopy] {
145 completionHandlerCopy();
149 #pragma mark WKObject protocol implementation
151 - (API::Object&)_apiObject
153 return *_websiteDataStore;
158 @implementation WKWebsiteDataStore (WKPrivate)
160 - (instancetype)_initWithConfiguration:(_WKWebsiteDataStoreConfiguration *)configuration
162 if (!(self = [super init]))
165 auto config = API::WebsiteDataStore::defaultDataStoreConfiguration();
167 if (configuration._webStorageDirectory)
168 config.localStorageDirectory = configuration._webStorageDirectory.path;
169 if (configuration._webSQLDatabaseDirectory)
170 config.webSQLDatabaseDirectory = configuration._webSQLDatabaseDirectory.path;
171 if (configuration._indexedDBDatabaseDirectory)
172 config.indexedDBDatabaseDirectory = configuration._indexedDBDatabaseDirectory.path;
173 if (configuration._cookieStorageFile)
174 config.cookieStorageFile = configuration._cookieStorageFile.path;
176 API::Object::constructInWrapper<API::WebsiteDataStore>(self, config, WebCore::SessionID::generatePersistentSessionID());
181 - (void)_fetchDataRecordsOfTypes:(NSSet<NSString *> *)dataTypes withOptions:(_WKWebsiteDataStoreFetchOptions)options completionHandler:(void (^)(NSArray<WKWebsiteDataRecord *> *))completionHandler
183 auto completionHandlerCopy = makeBlockPtr(completionHandler);
185 OptionSet<WebKit::WebsiteDataFetchOption> fetchOptions;
186 if (options & _WKWebsiteDataStoreFetchOptionComputeSizes)
187 fetchOptions |= WebKit::WebsiteDataFetchOption::ComputeSizes;
189 _websiteDataStore->websiteDataStore().fetchData(WebKit::toWebsiteDataTypes(dataTypes), fetchOptions, [completionHandlerCopy = WTFMove(completionHandlerCopy)](auto websiteDataRecords) {
190 Vector<RefPtr<API::Object>> elements;
191 elements.reserveInitialCapacity(websiteDataRecords.size());
193 for (auto& websiteDataRecord : websiteDataRecords)
194 elements.uncheckedAppend(API::WebsiteDataRecord::create(WTFMove(websiteDataRecord)));
196 completionHandlerCopy(wrapper(API::Array::create(WTFMove(elements))));
200 - (BOOL)_resourceLoadStatisticsEnabled
202 return _websiteDataStore->websiteDataStore().resourceLoadStatisticsEnabled();
205 - (void)_setResourceLoadStatisticsEnabled:(BOOL)enabled
207 _websiteDataStore->websiteDataStore().setResourceLoadStatisticsEnabled(enabled);
210 - (void)_resourceLoadStatisticsSetLastSeen:(double)seconds forHost:(NSString *)host
212 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
216 store->setLastSeen(URL(URL(), host), Seconds { seconds });
219 - (void)_resourceLoadStatisticsSetIsPrevalentResource:(BOOL)value forHost:(NSString *)host
221 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
226 store->setPrevalentResource(URL(URL(), host));
228 store->clearPrevalentResource(URL(URL(), host));
231 - (void)_resourceLoadStatisticsIsPrevalentResource:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
233 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
235 completionHandler(NO);
239 auto completionHandlerCopy = makeBlockPtr(completionHandler);
240 store->isPrevalentResource(URL(URL(), host), [completionHandlerCopy](bool isPrevalentResource) {
241 completionHandlerCopy(isPrevalentResource);
245 - (void)_resourceLoadStatisticsSetHadUserInteraction:(BOOL)value forHost:(NSString *)host
247 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
252 store->logUserInteraction(URL(URL(), host));
254 store->clearUserInteraction(URL(URL(), host));
257 - (void)_resourceLoadStatisticsHadUserInteraction:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
259 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
261 completionHandler(NO);
265 auto completionHandlerCopy = makeBlockPtr(completionHandler);
266 store->hasHadUserInteraction(URL(URL(), host), [completionHandlerCopy](bool hasHadUserInteraction) {
267 completionHandlerCopy(hasHadUserInteraction);
271 - (void)_resourceLoadStatisticsSetIsGrandfathered:(BOOL)value forHost:(NSString *)host
273 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
277 store->setGrandfathered(URL(URL(), host), value);
280 - (void)_resourceLoadStatisticsIsGrandfathered:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
282 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
284 completionHandler(NO);
288 auto completionHandlerCopy = makeBlockPtr(completionHandler);
289 store->isGrandfathered(URL(URL(), host), [completionHandlerCopy](bool isGrandfathered) {
290 completionHandlerCopy(isGrandfathered);
294 - (void)_resourceLoadStatisticsSetSubframeUnderTopFrameOrigin:(NSString *)topFrameHostName forHost:(NSString *)host
296 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
300 store->setSubframeUnderTopFrameOrigin(URL(URL(), host), URL(URL(), topFrameHostName));
303 - (void)_resourceLoadStatisticsSetSubresourceUnderTopFrameOrigin:(NSString *)topFrameHostName forHost:(NSString *)host
305 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
309 store->setSubresourceUnderTopFrameOrigin(URL(URL(), host), URL(URL(), topFrameHostName));
312 - (void)_resourceLoadStatisticsSetSubresourceUniqueRedirectTo:(NSString *)hostNameRedirectedTo forHost:(NSString *)host
314 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
318 store->setSubresourceUniqueRedirectTo(URL(URL(), host), URL(URL(), hostNameRedirectedTo));
321 - (void)_resourceLoadStatisticsSetTimeToLiveUserInteraction:(double)seconds
323 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
327 store->setTimeToLiveUserInteraction(Seconds { seconds });
330 - (void)_resourceLoadStatisticsSetTimeToLiveCookiePartitionFree:(double)seconds
332 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
336 store->setTimeToLiveCookiePartitionFree(Seconds { seconds });
339 - (void)_resourceLoadStatisticsSetMinimumTimeBetweenDataRecordsRemoval:(double)seconds
341 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
345 store->setMinimumTimeBetweenDataRecordsRemoval(Seconds { seconds });
348 - (void)_resourceLoadStatisticsSetGrandfatheringTime:(double)seconds
350 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
354 store->setGrandfatheringTime(Seconds {seconds });
357 - (void)_resourceLoadStatisticsSetMaxStatisticsEntries:(size_t)entries
359 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
363 store->setMaxStatisticsEntries(entries);
366 - (void)_resourceLoadStatisticsSetPruneEntriesDownTo:(size_t)entries
368 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
372 store->setPruneEntriesDownTo(entries);
375 - (void)_resourceLoadStatisticsProcessStatisticsAndDataRecords
377 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
381 store->scheduleStatisticsAndDataRecordsProcessing();
384 - (void)_resourceLoadStatisticsUpdateCookiePartitioning
386 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
390 store->scheduleCookiePartitioningUpdate();
393 - (void)_resourceLoadStatisticsSetShouldPartitionCookies:(BOOL)value forHost:(NSString *)host
395 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
400 store->scheduleCookiePartitioningUpdateForDomains({ }, { host }, WebKit::ShouldClearFirst::No);
402 store->scheduleCookiePartitioningUpdateForDomains({ host }, { }, WebKit::ShouldClearFirst::No);
405 - (void)_resourceLoadStatisticsSubmitTelemetry
407 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
411 store->submitTelemetry();
414 - (void)_resourceLoadStatisticsSetNotifyPagesWhenDataRecordsWereScanned:(BOOL)value
416 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
420 store->setNotifyPagesWhenDataRecordsWereScanned(value);
423 - (void)_resourceLoadStatisticsSetShouldClassifyResourcesBeforeDataRecordsRemoval:(BOOL)value
425 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
429 store->setShouldClassifyResourcesBeforeDataRecordsRemoval(value);
432 - (void)_resourceLoadStatisticsSetNotifyPagesWhenTelemetryWasCaptured:(BOOL)value
434 WebKit::WebResourceLoadStatisticsTelemetry::setNotifyPagesWhenTelemetryWasCaptured(value);
437 - (void)_resourceLoadStatisticsSetShouldSubmitTelemetry:(BOOL)value
439 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
443 store->setShouldSubmitTelemetry(value);
446 - (void)_resourceLoadStatisticsClearInMemoryAndPersistentStore
448 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
452 store->scheduleClearInMemoryAndPersistent();
455 - (void)_resourceLoadStatisticsClearInMemoryAndPersistentStoreModifiedSinceHours:(unsigned)hours
457 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
461 store->scheduleClearInMemoryAndPersistent(std::chrono::system_clock::now() - std::chrono::hours(hours));
464 - (void)_resourceLoadStatisticsResetToConsistentState
466 WebKit::WebResourceLoadStatisticsTelemetry::setNotifyPagesWhenTelemetryWasCaptured(false);
468 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
472 store->resetParametersToDefaultValues();
473 store->scheduleClearInMemory();
478 #endif // WK_API_ENABLED