Avoid unnecessary WorkQueue dispatch in WebResourceLoadStatisticsStore::processStatis...
[WebKit-https.git] / Source / WebKit / UIProcess / API / Cocoa / WKWebsiteDataStore.mm
1 /*
2  * Copyright (C) 2014-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 "WKWebsiteDataStoreInternal.h"
28
29 #if WK_API_ENABLED
30
31 #import "WKHTTPCookieStoreInternal.h"
32 #import "WKNSArray.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>
40
41 using namespace WebCore;
42
43 @implementation WKWebsiteDataStore
44
45 + (WKWebsiteDataStore *)defaultDataStore
46 {
47     return WebKit::wrapper(API::WebsiteDataStore::defaultDataStore().get());
48 }
49
50 + (WKWebsiteDataStore *)nonPersistentDataStore
51 {
52     return [WebKit::wrapper(API::WebsiteDataStore::createNonPersistentDataStore().leakRef()) autorelease];
53 }
54
55 - (void)dealloc
56 {
57     _websiteDataStore->API::WebsiteDataStore::~WebsiteDataStore();
58
59     [super dealloc];
60 }
61
62 - (instancetype)initWithCoder:(NSCoder *)coder
63 {
64     if (!(self = [super init]))
65         return nil;
66
67     RetainPtr<WKWebsiteDataStore> dataStore;
68     if ([coder decodeBoolForKey:@"isDefaultDataStore"])
69         dataStore = [WKWebsiteDataStore defaultDataStore];
70     else
71         dataStore = [WKWebsiteDataStore nonPersistentDataStore];
72
73     [self release];
74
75     return dataStore.leakRef();
76 }
77
78 - (void)encodeWithCoder:(NSCoder *)coder
79 {
80     if (self == [WKWebsiteDataStore defaultDataStore]) {
81         [coder encodeBool:YES forKey:@"isDefaultDataStore"];
82         return;
83     }
84
85     ASSERT(!self.persistent);
86 }
87
88 - (BOOL)isPersistent
89 {
90     return _websiteDataStore->isPersistent();
91 }
92
93 + (NSSet *)allWebsiteDataTypes
94 {
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 ]];
99     });
100
101     return allWebsiteDataTypes;
102 }
103
104 - (WKHTTPCookieStore *)httpCookieStore
105 {
106     return WebKit::wrapper(_websiteDataStore->httpCookieStore());
107 }
108
109 static std::chrono::system_clock::time_point toSystemClockTime(NSDate *date)
110 {
111     ASSERT(date);
112     using namespace std::chrono;
113
114     return system_clock::time_point(duration_cast<system_clock::duration>(duration<double>(date.timeIntervalSince1970)));
115 }
116
117 - (void)fetchDataRecordsOfTypes:(NSSet *)dataTypes completionHandler:(void (^)(NSArray<WKWebsiteDataRecord *> *))completionHandler
118 {
119     [self _fetchDataRecordsOfTypes:dataTypes withOptions:0 completionHandler:completionHandler];
120 }
121
122 - (void)removeDataOfTypes:(NSSet *)dataTypes modifiedSince:(NSDate *)date completionHandler:(void (^)(void))completionHandler
123 {
124     auto completionHandlerCopy = makeBlockPtr(completionHandler);
125     _websiteDataStore->websiteDataStore().removeData(WebKit::toWebsiteDataTypes(dataTypes), toSystemClockTime(date ? date : [NSDate distantPast]), [completionHandlerCopy] {
126         completionHandlerCopy();
127     });
128 }
129
130 static Vector<WebKit::WebsiteDataRecord> toWebsiteDataRecords(NSArray *dataRecords)
131 {
132     Vector<WebKit::WebsiteDataRecord> result;
133
134     for (WKWebsiteDataRecord *dataRecord in dataRecords)
135         result.append(dataRecord->_websiteDataRecord->websiteDataRecord());
136
137     return result;
138 }
139
140 - (void)removeDataOfTypes:(NSSet *)dataTypes forDataRecords:(NSArray *)dataRecords completionHandler:(void (^)(void))completionHandler
141 {
142     auto completionHandlerCopy = makeBlockPtr(completionHandler);
143
144     _websiteDataStore->websiteDataStore().removeData(WebKit::toWebsiteDataTypes(dataTypes), toWebsiteDataRecords(dataRecords), [completionHandlerCopy] {
145         completionHandlerCopy();
146     });
147 }
148
149 #pragma mark WKObject protocol implementation
150
151 - (API::Object&)_apiObject
152 {
153     return *_websiteDataStore;
154 }
155
156 @end
157
158 @implementation WKWebsiteDataStore (WKPrivate)
159
160 - (instancetype)_initWithConfiguration:(_WKWebsiteDataStoreConfiguration *)configuration
161 {
162     if (!(self = [super init]))
163         return nil;
164
165     auto config = API::WebsiteDataStore::defaultDataStoreConfiguration();
166
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;
175
176     API::Object::constructInWrapper<API::WebsiteDataStore>(self, config, WebCore::SessionID::generatePersistentSessionID());
177
178     return self;
179 }
180
181 - (void)_fetchDataRecordsOfTypes:(NSSet<NSString *> *)dataTypes withOptions:(_WKWebsiteDataStoreFetchOptions)options completionHandler:(void (^)(NSArray<WKWebsiteDataRecord *> *))completionHandler
182 {
183     auto completionHandlerCopy = makeBlockPtr(completionHandler);
184
185     OptionSet<WebKit::WebsiteDataFetchOption> fetchOptions;
186     if (options & _WKWebsiteDataStoreFetchOptionComputeSizes)
187         fetchOptions |= WebKit::WebsiteDataFetchOption::ComputeSizes;
188
189     _websiteDataStore->websiteDataStore().fetchData(WebKit::toWebsiteDataTypes(dataTypes), fetchOptions, [completionHandlerCopy = WTFMove(completionHandlerCopy)](auto websiteDataRecords) {
190         Vector<RefPtr<API::Object>> elements;
191         elements.reserveInitialCapacity(websiteDataRecords.size());
192
193         for (auto& websiteDataRecord : websiteDataRecords)
194             elements.uncheckedAppend(API::WebsiteDataRecord::create(WTFMove(websiteDataRecord)));
195
196         completionHandlerCopy(wrapper(API::Array::create(WTFMove(elements))));
197     });
198 }
199
200 - (BOOL)_resourceLoadStatisticsEnabled
201 {
202     return _websiteDataStore->websiteDataStore().resourceLoadStatisticsEnabled();
203 }
204
205 - (void)_setResourceLoadStatisticsEnabled:(BOOL)enabled
206 {
207     _websiteDataStore->websiteDataStore().setResourceLoadStatisticsEnabled(enabled);
208 }
209
210 - (void)_resourceLoadStatisticsSetLastSeen:(double)seconds forHost:(NSString *)host
211 {
212     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
213     if (!store)
214         return;
215     
216     store->setLastSeen(URL(URL(), host), Seconds { seconds });
217 }
218
219 - (void)_resourceLoadStatisticsSetIsPrevalentResource:(BOOL)value forHost:(NSString *)host
220 {
221     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
222     if (!store)
223         return;
224
225     if (value)
226         store->setPrevalentResource(URL(URL(), host));
227     else
228         store->clearPrevalentResource(URL(URL(), host));
229 }
230
231 - (void)_resourceLoadStatisticsIsPrevalentResource:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
232 {
233     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
234     if (!store) {
235         completionHandler(NO);
236         return;
237     }
238
239     auto completionHandlerCopy = makeBlockPtr(completionHandler);
240     store->isPrevalentResource(URL(URL(), host), [completionHandlerCopy](bool isPrevalentResource) {
241         completionHandlerCopy(isPrevalentResource);
242     });
243 }
244
245 - (void)_resourceLoadStatisticsSetHadUserInteraction:(BOOL)value forHost:(NSString *)host
246 {
247     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
248     if (!store)
249         return;
250
251     if (value)
252         store->logUserInteraction(URL(URL(), host));
253     else
254         store->clearUserInteraction(URL(URL(), host));
255 }
256
257 - (void)_resourceLoadStatisticsHadUserInteraction:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
258 {
259     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
260     if (!store) {
261         completionHandler(NO);
262         return;
263     }
264
265     auto completionHandlerCopy = makeBlockPtr(completionHandler);
266     store->hasHadUserInteraction(URL(URL(), host), [completionHandlerCopy](bool hasHadUserInteraction) {
267         completionHandlerCopy(hasHadUserInteraction);
268     });
269 }
270
271 - (void)_resourceLoadStatisticsSetIsGrandfathered:(BOOL)value forHost:(NSString *)host
272 {
273     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
274     if (!store)
275         return;
276
277     store->setGrandfathered(URL(URL(), host), value);
278 }
279
280 - (void)_resourceLoadStatisticsIsGrandfathered:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
281 {
282     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
283     if (!store) {
284         completionHandler(NO);
285         return;
286     }
287
288     auto completionHandlerCopy = makeBlockPtr(completionHandler);
289     store->isGrandfathered(URL(URL(), host), [completionHandlerCopy](bool isGrandfathered) {
290         completionHandlerCopy(isGrandfathered);
291     });
292 }
293
294 - (void)_resourceLoadStatisticsSetSubframeUnderTopFrameOrigin:(NSString *)topFrameHostName forHost:(NSString *)host
295 {
296     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
297     if (!store)
298         return;
299
300     store->setSubframeUnderTopFrameOrigin(URL(URL(), host), URL(URL(), topFrameHostName));
301 }
302
303 - (void)_resourceLoadStatisticsSetSubresourceUnderTopFrameOrigin:(NSString *)topFrameHostName forHost:(NSString *)host
304 {
305     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
306     if (!store)
307         return;
308
309     store->setSubresourceUnderTopFrameOrigin(URL(URL(), host), URL(URL(), topFrameHostName));
310 }
311
312 - (void)_resourceLoadStatisticsSetSubresourceUniqueRedirectTo:(NSString *)hostNameRedirectedTo forHost:(NSString *)host
313 {
314     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
315     if (!store)
316         return;
317
318     store->setSubresourceUniqueRedirectTo(URL(URL(), host), URL(URL(), hostNameRedirectedTo));
319 }
320
321 - (void)_resourceLoadStatisticsSetTimeToLiveUserInteraction:(double)seconds
322 {
323     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
324     if (!store)
325         return;
326
327     store->setTimeToLiveUserInteraction(Seconds { seconds });
328 }
329
330 - (void)_resourceLoadStatisticsSetTimeToLiveCookiePartitionFree:(double)seconds
331 {
332     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
333     if (!store)
334         return;
335
336     store->setTimeToLiveCookiePartitionFree(Seconds { seconds });
337 }
338
339 - (void)_resourceLoadStatisticsSetMinimumTimeBetweenDataRecordsRemoval:(double)seconds
340 {
341     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
342     if (!store)
343         return;
344
345     store->setMinimumTimeBetweenDataRecordsRemoval(Seconds { seconds });
346 }
347
348 - (void)_resourceLoadStatisticsSetGrandfatheringTime:(double)seconds
349 {
350     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
351     if (!store)
352         return;
353
354     store->setGrandfatheringTime(Seconds {seconds });
355 }
356
357 - (void)_resourceLoadStatisticsSetMaxStatisticsEntries:(size_t)entries
358 {
359     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
360     if (!store)
361         return;
362
363     store->setMaxStatisticsEntries(entries);
364 }
365
366 - (void)_resourceLoadStatisticsSetPruneEntriesDownTo:(size_t)entries
367 {
368     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
369     if (!store)
370         return;
371
372     store->setPruneEntriesDownTo(entries);
373 }
374
375 - (void)_resourceLoadStatisticsProcessStatisticsAndDataRecords
376 {
377     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
378     if (!store)
379         return;
380
381     store->scheduleStatisticsAndDataRecordsProcessing();
382 }
383
384 - (void)_resourceLoadStatisticsUpdateCookiePartitioning
385 {
386     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
387     if (!store)
388         return;
389
390     store->scheduleCookiePartitioningUpdate();
391 }
392
393 - (void)_resourceLoadStatisticsSetShouldPartitionCookies:(BOOL)value forHost:(NSString *)host
394 {
395     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
396     if (!store)
397         return;
398
399     if (value)
400         store->scheduleCookiePartitioningUpdateForDomains({ }, { host }, WebKit::ShouldClearFirst::No);
401     else
402         store->scheduleCookiePartitioningUpdateForDomains({ host }, { }, WebKit::ShouldClearFirst::No);
403 }
404
405 - (void)_resourceLoadStatisticsSubmitTelemetry
406 {
407     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
408     if (!store)
409         return;
410
411     store->submitTelemetry();
412 }
413
414 - (void)_resourceLoadStatisticsSetNotifyPagesWhenDataRecordsWereScanned:(BOOL)value
415 {
416     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
417     if (!store)
418         return;
419
420     store->setNotifyPagesWhenDataRecordsWereScanned(value);
421 }
422
423 - (void)_resourceLoadStatisticsSetShouldClassifyResourcesBeforeDataRecordsRemoval:(BOOL)value
424 {
425     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
426     if (!store)
427         return;
428
429     store->setShouldClassifyResourcesBeforeDataRecordsRemoval(value);
430 }
431
432 - (void)_resourceLoadStatisticsSetNotifyPagesWhenTelemetryWasCaptured:(BOOL)value
433 {
434     WebKit::WebResourceLoadStatisticsTelemetry::setNotifyPagesWhenTelemetryWasCaptured(value);
435 }
436
437 - (void)_resourceLoadStatisticsSetShouldSubmitTelemetry:(BOOL)value
438 {
439     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
440     if (!store)
441         return;
442
443     store->setShouldSubmitTelemetry(value);
444 }
445
446 - (void)_resourceLoadStatisticsClearInMemoryAndPersistentStore
447 {
448     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
449     if (!store)
450         return;
451
452     store->scheduleClearInMemoryAndPersistent();
453 }
454
455 - (void)_resourceLoadStatisticsClearInMemoryAndPersistentStoreModifiedSinceHours:(unsigned)hours
456 {
457     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
458     if (!store)
459         return;
460
461     store->scheduleClearInMemoryAndPersistent(std::chrono::system_clock::now() - std::chrono::hours(hours));
462 }
463
464 - (void)_resourceLoadStatisticsResetToConsistentState
465 {
466     WebKit::WebResourceLoadStatisticsTelemetry::setNotifyPagesWhenTelemetryWasCaptured(false);
467
468     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
469     if (!store)
470         return;
471
472     store->resetParametersToDefaultValues();
473     store->scheduleClearInMemory();
474 }
475
476 @end
477
478 #endif // WK_API_ENABLED