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 + (NSSet<NSString *> *)_allWebsiteDataTypesIncludingPrivate
162 static dispatch_once_t onceToken;
163 static NSSet *allWebsiteDataTypes;
164 dispatch_once(&onceToken, ^ {
165 auto *privateTypes = @[_WKWebsiteDataTypeHSTSCache, _WKWebsiteDataTypeMediaKeys, _WKWebsiteDataTypeSearchFieldRecentSearches, _WKWebsiteDataTypeResourceLoadStatistics, _WKWebsiteDataTypeCredentials
166 #if !TARGET_OS_IPHONE
167 , _WKWebsiteDataTypePlugInData
171 allWebsiteDataTypes = [[[self allWebsiteDataTypes] setByAddingObjectsFromArray:privateTypes] retain];
174 return allWebsiteDataTypes;
177 - (instancetype)_initWithConfiguration:(_WKWebsiteDataStoreConfiguration *)configuration
179 if (!(self = [super init]))
182 auto config = API::WebsiteDataStore::defaultDataStoreConfiguration();
184 if (configuration._webStorageDirectory)
185 config.localStorageDirectory = configuration._webStorageDirectory.path;
186 if (configuration._webSQLDatabaseDirectory)
187 config.webSQLDatabaseDirectory = configuration._webSQLDatabaseDirectory.path;
188 if (configuration._indexedDBDatabaseDirectory)
189 config.indexedDBDatabaseDirectory = configuration._indexedDBDatabaseDirectory.path;
190 if (configuration._cookieStorageFile)
191 config.cookieStorageFile = configuration._cookieStorageFile.path;
193 API::Object::constructInWrapper<API::WebsiteDataStore>(self, config, PAL::SessionID::generatePersistentSessionID());
198 - (void)_fetchDataRecordsOfTypes:(NSSet<NSString *> *)dataTypes withOptions:(_WKWebsiteDataStoreFetchOptions)options completionHandler:(void (^)(NSArray<WKWebsiteDataRecord *> *))completionHandler
200 auto completionHandlerCopy = makeBlockPtr(completionHandler);
202 OptionSet<WebKit::WebsiteDataFetchOption> fetchOptions;
203 if (options & _WKWebsiteDataStoreFetchOptionComputeSizes)
204 fetchOptions |= WebKit::WebsiteDataFetchOption::ComputeSizes;
206 _websiteDataStore->websiteDataStore().fetchData(WebKit::toWebsiteDataTypes(dataTypes), fetchOptions, [completionHandlerCopy = WTFMove(completionHandlerCopy)](auto websiteDataRecords) {
207 Vector<RefPtr<API::Object>> elements;
208 elements.reserveInitialCapacity(websiteDataRecords.size());
210 for (auto& websiteDataRecord : websiteDataRecords)
211 elements.uncheckedAppend(API::WebsiteDataRecord::create(WTFMove(websiteDataRecord)));
213 completionHandlerCopy(wrapper(API::Array::create(WTFMove(elements))));
217 - (BOOL)_resourceLoadStatisticsEnabled
219 return _websiteDataStore->websiteDataStore().resourceLoadStatisticsEnabled();
222 - (void)_setResourceLoadStatisticsEnabled:(BOOL)enabled
224 _websiteDataStore->websiteDataStore().setResourceLoadStatisticsEnabled(enabled);
227 - (void)_resourceLoadStatisticsSetLastSeen:(double)seconds forHost:(NSString *)host
229 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
233 store->setLastSeen(URL(URL(), host), Seconds { seconds });
236 - (void)_resourceLoadStatisticsSetIsPrevalentResource:(BOOL)value forHost:(NSString *)host
238 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
243 store->setPrevalentResource(URL(URL(), host));
245 store->clearPrevalentResource(URL(URL(), host));
248 - (void)_resourceLoadStatisticsIsPrevalentResource:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
250 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
252 completionHandler(NO);
256 auto completionHandlerCopy = makeBlockPtr(completionHandler);
257 store->isPrevalentResource(URL(URL(), host), [completionHandlerCopy](bool isPrevalentResource) {
258 completionHandlerCopy(isPrevalentResource);
262 - (void)_resourceLoadStatisticsSetHadUserInteraction:(BOOL)value forHost:(NSString *)host
264 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
269 store->logUserInteraction(URL(URL(), host));
271 store->clearUserInteraction(URL(URL(), host));
274 - (void)_resourceLoadStatisticsHadUserInteraction:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
276 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
278 completionHandler(NO);
282 auto completionHandlerCopy = makeBlockPtr(completionHandler);
283 store->hasHadUserInteraction(URL(URL(), host), [completionHandlerCopy](bool hasHadUserInteraction) {
284 completionHandlerCopy(hasHadUserInteraction);
288 - (void)_resourceLoadStatisticsSetIsGrandfathered:(BOOL)value forHost:(NSString *)host
290 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
294 store->setGrandfathered(URL(URL(), host), value);
297 - (void)_resourceLoadStatisticsIsGrandfathered:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
299 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
301 completionHandler(NO);
305 auto completionHandlerCopy = makeBlockPtr(completionHandler);
306 store->isGrandfathered(URL(URL(), host), [completionHandlerCopy](bool isGrandfathered) {
307 completionHandlerCopy(isGrandfathered);
311 - (void)_resourceLoadStatisticsSetSubframeUnderTopFrameOrigin:(NSString *)topFrameHostName forHost:(NSString *)host
313 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
317 store->setSubframeUnderTopFrameOrigin(URL(URL(), host), URL(URL(), topFrameHostName));
320 - (void)_resourceLoadStatisticsSetSubresourceUnderTopFrameOrigin:(NSString *)topFrameHostName forHost:(NSString *)host
322 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
326 store->setSubresourceUnderTopFrameOrigin(URL(URL(), host), URL(URL(), topFrameHostName));
329 - (void)_resourceLoadStatisticsSetSubresourceUniqueRedirectTo:(NSString *)hostNameRedirectedTo forHost:(NSString *)host
331 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
335 store->setSubresourceUniqueRedirectTo(URL(URL(), host), URL(URL(), hostNameRedirectedTo));
338 - (void)_resourceLoadStatisticsSetTimeToLiveUserInteraction:(double)seconds
340 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
344 store->setTimeToLiveUserInteraction(Seconds { seconds });
347 - (void)_resourceLoadStatisticsSetTimeToLiveCookiePartitionFree:(double)seconds
349 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
353 store->setTimeToLiveCookiePartitionFree(Seconds { seconds });
356 - (void)_resourceLoadStatisticsSetMinimumTimeBetweenDataRecordsRemoval:(double)seconds
358 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
362 store->setMinimumTimeBetweenDataRecordsRemoval(Seconds { seconds });
365 - (void)_resourceLoadStatisticsSetGrandfatheringTime:(double)seconds
367 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
371 store->setGrandfatheringTime(Seconds {seconds });
374 - (void)_resourceLoadStatisticsSetMaxStatisticsEntries:(size_t)entries
376 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
380 store->setMaxStatisticsEntries(entries);
383 - (void)_resourceLoadStatisticsSetPruneEntriesDownTo:(size_t)entries
385 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
389 store->setPruneEntriesDownTo(entries);
392 - (void)_resourceLoadStatisticsProcessStatisticsAndDataRecords
394 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
398 store->scheduleStatisticsAndDataRecordsProcessing();
401 - (void)_resourceLoadStatisticsUpdateCookiePartitioning
403 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
407 store->scheduleCookiePartitioningUpdate();
410 - (void)_resourceLoadStatisticsSetShouldPartitionCookies:(BOOL)value forHost:(NSString *)host
412 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
417 store->scheduleCookiePartitioningUpdateForDomains({ }, { host }, WebKit::ShouldClearFirst::No);
419 store->scheduleCookiePartitioningUpdateForDomains({ host }, { }, WebKit::ShouldClearFirst::No);
422 - (void)_resourceLoadStatisticsSubmitTelemetry
424 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
428 store->submitTelemetry();
431 - (void)_resourceLoadStatisticsSetNotifyPagesWhenDataRecordsWereScanned:(BOOL)value
433 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
437 store->setNotifyPagesWhenDataRecordsWereScanned(value);
440 - (void)_resourceLoadStatisticsSetShouldClassifyResourcesBeforeDataRecordsRemoval:(BOOL)value
442 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
446 store->setShouldClassifyResourcesBeforeDataRecordsRemoval(value);
449 - (void)_resourceLoadStatisticsSetNotifyPagesWhenTelemetryWasCaptured:(BOOL)value
451 WebKit::WebResourceLoadStatisticsTelemetry::setNotifyPagesWhenTelemetryWasCaptured(value);
454 - (void)_resourceLoadStatisticsSetShouldSubmitTelemetry:(BOOL)value
456 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
460 store->setShouldSubmitTelemetry(value);
463 - (void)_resourceLoadStatisticsClearInMemoryAndPersistentStore
465 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
469 store->scheduleClearInMemoryAndPersistent(WebKit::WebResourceLoadStatisticsStore::ShouldGrandfather::Yes);
472 - (void)_resourceLoadStatisticsClearInMemoryAndPersistentStoreModifiedSinceHours:(unsigned)hours
474 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
478 store->scheduleClearInMemoryAndPersistent(std::chrono::system_clock::now() - std::chrono::hours(hours), WebKit::WebResourceLoadStatisticsStore::ShouldGrandfather::Yes);
481 - (void)_resourceLoadStatisticsResetToConsistentState
483 WebKit::WebResourceLoadStatisticsTelemetry::setNotifyPagesWhenTelemetryWasCaptured(false);
485 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
489 store->resetParametersToDefaultValues();
490 store->scheduleClearInMemory();
493 - (void)_setResourceLoadStatisticsTestingCallback:(void (^)(WKWebsiteDataStore *, NSString *))callback
496 _websiteDataStore->websiteDataStore().enableResourceLoadStatisticsAndSetTestingCallback([callback = makeBlockPtr(callback), self](const String& event) {
497 callback(self, (NSString *)event);
502 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
506 store->setStatisticsTestingCallback(nullptr);
511 #endif // WK_API_ENABLED