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;
192 if (configuration._resourceLoadStatisticsDirectory)
193 config.resourceLoadStatisticsDirectory = configuration._resourceLoadStatisticsDirectory.path;
195 API::Object::constructInWrapper<API::WebsiteDataStore>(self, config, PAL::SessionID::generatePersistentSessionID());
200 - (void)_fetchDataRecordsOfTypes:(NSSet<NSString *> *)dataTypes withOptions:(_WKWebsiteDataStoreFetchOptions)options completionHandler:(void (^)(NSArray<WKWebsiteDataRecord *> *))completionHandler
202 auto completionHandlerCopy = makeBlockPtr(completionHandler);
204 OptionSet<WebKit::WebsiteDataFetchOption> fetchOptions;
205 if (options & _WKWebsiteDataStoreFetchOptionComputeSizes)
206 fetchOptions |= WebKit::WebsiteDataFetchOption::ComputeSizes;
208 _websiteDataStore->websiteDataStore().fetchData(WebKit::toWebsiteDataTypes(dataTypes), fetchOptions, [completionHandlerCopy = WTFMove(completionHandlerCopy)](auto websiteDataRecords) {
209 Vector<RefPtr<API::Object>> elements;
210 elements.reserveInitialCapacity(websiteDataRecords.size());
212 for (auto& websiteDataRecord : websiteDataRecords)
213 elements.uncheckedAppend(API::WebsiteDataRecord::create(WTFMove(websiteDataRecord)));
215 completionHandlerCopy(wrapper(API::Array::create(WTFMove(elements))));
219 - (BOOL)_resourceLoadStatisticsEnabled
221 return _websiteDataStore->websiteDataStore().resourceLoadStatisticsEnabled();
224 - (void)_setResourceLoadStatisticsEnabled:(BOOL)enabled
226 _websiteDataStore->websiteDataStore().setResourceLoadStatisticsEnabled(enabled);
229 - (void)_resourceLoadStatisticsSetLastSeen:(double)seconds forHost:(NSString *)host
231 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
235 store->setLastSeen(URL(URL(), host), Seconds { seconds });
238 - (void)_resourceLoadStatisticsSetIsPrevalentResource:(BOOL)value forHost:(NSString *)host
240 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
245 store->setPrevalentResource(URL(URL(), host));
247 store->clearPrevalentResource(URL(URL(), host));
250 - (void)_resourceLoadStatisticsIsPrevalentResource:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
252 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
254 completionHandler(NO);
258 auto completionHandlerCopy = makeBlockPtr(completionHandler);
259 store->isPrevalentResource(URL(URL(), host), [completionHandlerCopy](bool isPrevalentResource) {
260 completionHandlerCopy(isPrevalentResource);
264 - (void)_resourceLoadStatisticsSetHadUserInteraction:(BOOL)value forHost:(NSString *)host
266 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
271 store->logUserInteraction(URL(URL(), host));
273 store->clearUserInteraction(URL(URL(), host));
276 - (void)_resourceLoadStatisticsHadUserInteraction:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
278 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
280 completionHandler(NO);
284 auto completionHandlerCopy = makeBlockPtr(completionHandler);
285 store->hasHadUserInteraction(URL(URL(), host), [completionHandlerCopy](bool hasHadUserInteraction) {
286 completionHandlerCopy(hasHadUserInteraction);
290 - (void)_resourceLoadStatisticsSetIsGrandfathered:(BOOL)value forHost:(NSString *)host
292 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
296 store->setGrandfathered(URL(URL(), host), value);
299 - (void)_resourceLoadStatisticsIsGrandfathered:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
301 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
303 completionHandler(NO);
307 auto completionHandlerCopy = makeBlockPtr(completionHandler);
308 store->isGrandfathered(URL(URL(), host), [completionHandlerCopy](bool isGrandfathered) {
309 completionHandlerCopy(isGrandfathered);
313 - (void)_resourceLoadStatisticsSetSubframeUnderTopFrameOrigin:(NSString *)topFrameHostName forHost:(NSString *)host
315 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
319 store->setSubframeUnderTopFrameOrigin(URL(URL(), host), URL(URL(), topFrameHostName));
322 - (void)_resourceLoadStatisticsSetSubresourceUnderTopFrameOrigin:(NSString *)topFrameHostName forHost:(NSString *)host
324 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
328 store->setSubresourceUnderTopFrameOrigin(URL(URL(), host), URL(URL(), topFrameHostName));
331 - (void)_resourceLoadStatisticsSetSubresourceUniqueRedirectTo:(NSString *)hostNameRedirectedTo forHost:(NSString *)host
333 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
337 store->setSubresourceUniqueRedirectTo(URL(URL(), host), URL(URL(), hostNameRedirectedTo));
340 - (void)_resourceLoadStatisticsSetTimeToLiveUserInteraction:(double)seconds
342 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
346 store->setTimeToLiveUserInteraction(Seconds { seconds });
349 - (void)_resourceLoadStatisticsSetTimeToLiveCookiePartitionFree:(double)seconds
351 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
355 store->setTimeToLiveCookiePartitionFree(Seconds { seconds });
358 - (void)_resourceLoadStatisticsSetMinimumTimeBetweenDataRecordsRemoval:(double)seconds
360 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
364 store->setMinimumTimeBetweenDataRecordsRemoval(Seconds { seconds });
367 - (void)_resourceLoadStatisticsSetGrandfatheringTime:(double)seconds
369 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
373 store->setGrandfatheringTime(Seconds {seconds });
376 - (void)_resourceLoadStatisticsSetMaxStatisticsEntries:(size_t)entries
378 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
382 store->setMaxStatisticsEntries(entries);
385 - (void)_resourceLoadStatisticsSetPruneEntriesDownTo:(size_t)entries
387 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
391 store->setPruneEntriesDownTo(entries);
394 - (void)_resourceLoadStatisticsProcessStatisticsAndDataRecords
396 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
400 store->scheduleStatisticsAndDataRecordsProcessing();
403 - (void)_resourceLoadStatisticsUpdateCookiePartitioning
405 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
409 store->scheduleCookiePartitioningUpdate();
412 - (void)_resourceLoadStatisticsSetShouldPartitionCookies:(BOOL)value forHost:(NSString *)host
414 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
419 store->scheduleCookiePartitioningUpdateForDomains({ }, { host }, WebKit::ShouldClearFirst::No);
421 store->scheduleClearPartitioningStateForDomains({ host });
424 - (void)_resourceLoadStatisticsSubmitTelemetry
426 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
430 store->submitTelemetry();
433 - (void)_resourceLoadStatisticsSetNotifyPagesWhenDataRecordsWereScanned:(BOOL)value
435 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
439 store->setNotifyPagesWhenDataRecordsWereScanned(value);
442 - (void)_resourceLoadStatisticsSetShouldClassifyResourcesBeforeDataRecordsRemoval:(BOOL)value
444 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
448 store->setShouldClassifyResourcesBeforeDataRecordsRemoval(value);
451 - (void)_resourceLoadStatisticsSetNotifyPagesWhenTelemetryWasCaptured:(BOOL)value
453 WebKit::WebResourceLoadStatisticsTelemetry::setNotifyPagesWhenTelemetryWasCaptured(value);
456 - (void)_resourceLoadStatisticsSetShouldSubmitTelemetry:(BOOL)value
458 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
462 store->setShouldSubmitTelemetry(value);
465 - (void)_resourceLoadStatisticsClearInMemoryAndPersistentStore
467 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
471 store->scheduleClearInMemoryAndPersistent(WebKit::WebResourceLoadStatisticsStore::ShouldGrandfather::Yes);
474 - (void)_resourceLoadStatisticsClearInMemoryAndPersistentStoreModifiedSinceHours:(unsigned)hours
476 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
480 store->scheduleClearInMemoryAndPersistent(std::chrono::system_clock::now() - std::chrono::hours(hours), WebKit::WebResourceLoadStatisticsStore::ShouldGrandfather::Yes);
483 - (void)_resourceLoadStatisticsResetToConsistentState
485 WebKit::WebResourceLoadStatisticsTelemetry::setNotifyPagesWhenTelemetryWasCaptured(false);
487 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
491 store->resetParametersToDefaultValues();
492 store->scheduleClearInMemory();
495 - (void)_setResourceLoadStatisticsTestingCallback:(void (^)(WKWebsiteDataStore *, NSString *))callback
498 _websiteDataStore->websiteDataStore().enableResourceLoadStatisticsAndSetTestingCallback([callback = makeBlockPtr(callback), self](const String& event) {
499 callback(self, (NSString *)event);
504 auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
508 store->setStatisticsTestingCallback(nullptr);
513 #endif // WK_API_ENABLED