ResourceLoadStatistics grandfathering happens much too often.
[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 + (NSSet<NSString *> *)_allWebsiteDataTypesIncludingPrivate
161 {
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
168 #endif
169         ];
170
171         allWebsiteDataTypes = [[[self allWebsiteDataTypes] setByAddingObjectsFromArray:privateTypes] retain];
172     });
173
174     return allWebsiteDataTypes;
175 }
176
177 - (instancetype)_initWithConfiguration:(_WKWebsiteDataStoreConfiguration *)configuration
178 {
179     if (!(self = [super init]))
180         return nil;
181
182     auto config = API::WebsiteDataStore::defaultDataStoreConfiguration();
183
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
193     API::Object::constructInWrapper<API::WebsiteDataStore>(self, config, WebCore::SessionID::generatePersistentSessionID());
194
195     return self;
196 }
197
198 - (void)_fetchDataRecordsOfTypes:(NSSet<NSString *> *)dataTypes withOptions:(_WKWebsiteDataStoreFetchOptions)options completionHandler:(void (^)(NSArray<WKWebsiteDataRecord *> *))completionHandler
199 {
200     auto completionHandlerCopy = makeBlockPtr(completionHandler);
201
202     OptionSet<WebKit::WebsiteDataFetchOption> fetchOptions;
203     if (options & _WKWebsiteDataStoreFetchOptionComputeSizes)
204         fetchOptions |= WebKit::WebsiteDataFetchOption::ComputeSizes;
205
206     _websiteDataStore->websiteDataStore().fetchData(WebKit::toWebsiteDataTypes(dataTypes), fetchOptions, [completionHandlerCopy = WTFMove(completionHandlerCopy)](auto websiteDataRecords) {
207         Vector<RefPtr<API::Object>> elements;
208         elements.reserveInitialCapacity(websiteDataRecords.size());
209
210         for (auto& websiteDataRecord : websiteDataRecords)
211             elements.uncheckedAppend(API::WebsiteDataRecord::create(WTFMove(websiteDataRecord)));
212
213         completionHandlerCopy(wrapper(API::Array::create(WTFMove(elements))));
214     });
215 }
216
217 - (BOOL)_resourceLoadStatisticsEnabled
218 {
219     return _websiteDataStore->websiteDataStore().resourceLoadStatisticsEnabled();
220 }
221
222 - (void)_setResourceLoadStatisticsEnabled:(BOOL)enabled
223 {
224     _websiteDataStore->websiteDataStore().setResourceLoadStatisticsEnabled(enabled);
225 }
226
227 - (void)_resourceLoadStatisticsSetLastSeen:(double)seconds forHost:(NSString *)host
228 {
229     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
230     if (!store)
231         return;
232     
233     store->setLastSeen(URL(URL(), host), Seconds { seconds });
234 }
235
236 - (void)_resourceLoadStatisticsSetIsPrevalentResource:(BOOL)value forHost:(NSString *)host
237 {
238     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
239     if (!store)
240         return;
241
242     if (value)
243         store->setPrevalentResource(URL(URL(), host));
244     else
245         store->clearPrevalentResource(URL(URL(), host));
246 }
247
248 - (void)_resourceLoadStatisticsIsPrevalentResource:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
249 {
250     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
251     if (!store) {
252         completionHandler(NO);
253         return;
254     }
255
256     auto completionHandlerCopy = makeBlockPtr(completionHandler);
257     store->isPrevalentResource(URL(URL(), host), [completionHandlerCopy](bool isPrevalentResource) {
258         completionHandlerCopy(isPrevalentResource);
259     });
260 }
261
262 - (void)_resourceLoadStatisticsSetHadUserInteraction:(BOOL)value forHost:(NSString *)host
263 {
264     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
265     if (!store)
266         return;
267
268     if (value)
269         store->logUserInteraction(URL(URL(), host));
270     else
271         store->clearUserInteraction(URL(URL(), host));
272 }
273
274 - (void)_resourceLoadStatisticsHadUserInteraction:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
275 {
276     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
277     if (!store) {
278         completionHandler(NO);
279         return;
280     }
281
282     auto completionHandlerCopy = makeBlockPtr(completionHandler);
283     store->hasHadUserInteraction(URL(URL(), host), [completionHandlerCopy](bool hasHadUserInteraction) {
284         completionHandlerCopy(hasHadUserInteraction);
285     });
286 }
287
288 - (void)_resourceLoadStatisticsSetIsGrandfathered:(BOOL)value forHost:(NSString *)host
289 {
290     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
291     if (!store)
292         return;
293
294     store->setGrandfathered(URL(URL(), host), value);
295 }
296
297 - (void)_resourceLoadStatisticsIsGrandfathered:(NSString *)host completionHandler:(void (^)(BOOL))completionHandler
298 {
299     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
300     if (!store) {
301         completionHandler(NO);
302         return;
303     }
304
305     auto completionHandlerCopy = makeBlockPtr(completionHandler);
306     store->isGrandfathered(URL(URL(), host), [completionHandlerCopy](bool isGrandfathered) {
307         completionHandlerCopy(isGrandfathered);
308     });
309 }
310
311 - (void)_resourceLoadStatisticsSetSubframeUnderTopFrameOrigin:(NSString *)topFrameHostName forHost:(NSString *)host
312 {
313     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
314     if (!store)
315         return;
316
317     store->setSubframeUnderTopFrameOrigin(URL(URL(), host), URL(URL(), topFrameHostName));
318 }
319
320 - (void)_resourceLoadStatisticsSetSubresourceUnderTopFrameOrigin:(NSString *)topFrameHostName forHost:(NSString *)host
321 {
322     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
323     if (!store)
324         return;
325
326     store->setSubresourceUnderTopFrameOrigin(URL(URL(), host), URL(URL(), topFrameHostName));
327 }
328
329 - (void)_resourceLoadStatisticsSetSubresourceUniqueRedirectTo:(NSString *)hostNameRedirectedTo forHost:(NSString *)host
330 {
331     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
332     if (!store)
333         return;
334
335     store->setSubresourceUniqueRedirectTo(URL(URL(), host), URL(URL(), hostNameRedirectedTo));
336 }
337
338 - (void)_resourceLoadStatisticsSetTimeToLiveUserInteraction:(double)seconds
339 {
340     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
341     if (!store)
342         return;
343
344     store->setTimeToLiveUserInteraction(Seconds { seconds });
345 }
346
347 - (void)_resourceLoadStatisticsSetTimeToLiveCookiePartitionFree:(double)seconds
348 {
349     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
350     if (!store)
351         return;
352
353     store->setTimeToLiveCookiePartitionFree(Seconds { seconds });
354 }
355
356 - (void)_resourceLoadStatisticsSetMinimumTimeBetweenDataRecordsRemoval:(double)seconds
357 {
358     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
359     if (!store)
360         return;
361
362     store->setMinimumTimeBetweenDataRecordsRemoval(Seconds { seconds });
363 }
364
365 - (void)_resourceLoadStatisticsSetGrandfatheringTime:(double)seconds
366 {
367     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
368     if (!store)
369         return;
370
371     store->setGrandfatheringTime(Seconds {seconds });
372 }
373
374 - (void)_resourceLoadStatisticsSetMaxStatisticsEntries:(size_t)entries
375 {
376     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
377     if (!store)
378         return;
379
380     store->setMaxStatisticsEntries(entries);
381 }
382
383 - (void)_resourceLoadStatisticsSetPruneEntriesDownTo:(size_t)entries
384 {
385     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
386     if (!store)
387         return;
388
389     store->setPruneEntriesDownTo(entries);
390 }
391
392 - (void)_resourceLoadStatisticsProcessStatisticsAndDataRecords
393 {
394     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
395     if (!store)
396         return;
397
398     store->scheduleStatisticsAndDataRecordsProcessing();
399 }
400
401 - (void)_resourceLoadStatisticsUpdateCookiePartitioning
402 {
403     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
404     if (!store)
405         return;
406
407     store->scheduleCookiePartitioningUpdate();
408 }
409
410 - (void)_resourceLoadStatisticsSetShouldPartitionCookies:(BOOL)value forHost:(NSString *)host
411 {
412     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
413     if (!store)
414         return;
415
416     if (value)
417         store->scheduleCookiePartitioningUpdateForDomains({ }, { host }, WebKit::ShouldClearFirst::No);
418     else
419         store->scheduleCookiePartitioningUpdateForDomains({ host }, { }, WebKit::ShouldClearFirst::No);
420 }
421
422 - (void)_resourceLoadStatisticsSubmitTelemetry
423 {
424     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
425     if (!store)
426         return;
427
428     store->submitTelemetry();
429 }
430
431 - (void)_resourceLoadStatisticsSetNotifyPagesWhenDataRecordsWereScanned:(BOOL)value
432 {
433     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
434     if (!store)
435         return;
436
437     store->setNotifyPagesWhenDataRecordsWereScanned(value);
438 }
439
440 - (void)_resourceLoadStatisticsSetShouldClassifyResourcesBeforeDataRecordsRemoval:(BOOL)value
441 {
442     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
443     if (!store)
444         return;
445
446     store->setShouldClassifyResourcesBeforeDataRecordsRemoval(value);
447 }
448
449 - (void)_resourceLoadStatisticsSetNotifyPagesWhenTelemetryWasCaptured:(BOOL)value
450 {
451     WebKit::WebResourceLoadStatisticsTelemetry::setNotifyPagesWhenTelemetryWasCaptured(value);
452 }
453
454 - (void)_resourceLoadStatisticsSetShouldSubmitTelemetry:(BOOL)value
455 {
456     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
457     if (!store)
458         return;
459
460     store->setShouldSubmitTelemetry(value);
461 }
462
463 - (void)_resourceLoadStatisticsClearInMemoryAndPersistentStore
464 {
465     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
466     if (!store)
467         return;
468
469     store->scheduleClearInMemoryAndPersistent(WebKit::WebResourceLoadStatisticsStore::ShouldGrandfather::Yes);
470 }
471
472 - (void)_resourceLoadStatisticsClearInMemoryAndPersistentStoreModifiedSinceHours:(unsigned)hours
473 {
474     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
475     if (!store)
476         return;
477
478     store->scheduleClearInMemoryAndPersistent(std::chrono::system_clock::now() - std::chrono::hours(hours), WebKit::WebResourceLoadStatisticsStore::ShouldGrandfather::Yes);
479 }
480
481 - (void)_resourceLoadStatisticsResetToConsistentState
482 {
483     WebKit::WebResourceLoadStatisticsTelemetry::setNotifyPagesWhenTelemetryWasCaptured(false);
484
485     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
486     if (!store)
487         return;
488
489     store->resetParametersToDefaultValues();
490     store->scheduleClearInMemory();
491 }
492
493 - (void)_setResourceLoadStatisticsTestingCallback:(void (^)(WKWebsiteDataStore *, NSString *))callback
494 {
495     if (callback) {
496         _websiteDataStore->websiteDataStore().enableResourceLoadStatisticsAndSetTestingCallback([callback = makeBlockPtr(callback), self](const String& event) {
497             callback(self, (NSString *)event);
498         });
499         return;
500     }
501
502     auto* store = _websiteDataStore->websiteDataStore().resourceLoadStatistics();
503     if (!store)
504         return;
505
506     store->setStatisticsTestingCallback(nullptr);
507 }
508
509 @end
510
511 #endif // WK_API_ENABLED