Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebKit / UIProcess / API / C / WKWebsiteDataStoreRef.cpp
1 /*
2  * Copyright (C) 2015 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 #include "config.h"
27 #include "WKWebsiteDataStoreRef.h"
28
29 #include "APIArray.h"
30 #include "APIWebsiteDataStore.h"
31 #include "MockWebAuthenticationConfiguration.h"
32 #include "WKAPICast.h"
33 #include "WKDictionary.h"
34 #include "WKNumber.h"
35 #include "WKRetainPtr.h"
36 #include "WKSecurityOriginRef.h"
37 #include "WKString.h"
38 #include "WebResourceLoadStatisticsStore.h"
39 #include "WebResourceLoadStatisticsTelemetry.h"
40 #include "WebsiteData.h"
41 #include "WebsiteDataFetchOption.h"
42 #include "WebsiteDataRecord.h"
43 #include "WebsiteDataType.h"
44 #include <wtf/CallbackAggregator.h>
45 #include <wtf/URL.h>
46
47 WKTypeID WKWebsiteDataStoreGetTypeID()
48 {
49     return WebKit::toAPI(API::WebsiteDataStore::APIType);
50 }
51
52 WKWebsiteDataStoreRef WKWebsiteDataStoreGetDefaultDataStore()
53 {
54     return WebKit::toAPI(API::WebsiteDataStore::defaultDataStore().ptr());
55 }
56
57 WKWebsiteDataStoreRef WKWebsiteDataStoreCreateNonPersistentDataStore()
58 {
59     return WebKit::toAPI(&API::WebsiteDataStore::createNonPersistentDataStore().leakRef());
60 }
61
62 void WKWebsiteDataStoreSetResourceLoadStatisticsEnabled(WKWebsiteDataStoreRef dataStoreRef, bool enable)
63 {
64     WebKit::toImpl(dataStoreRef)->setResourceLoadStatisticsEnabled(enable);
65 }
66
67 bool WKWebsiteDataStoreGetResourceLoadStatisticsEnabled(WKWebsiteDataStoreRef dataStoreRef)
68 {
69     return WebKit::toImpl(dataStoreRef)->resourceLoadStatisticsEnabled();
70 }
71
72 void WKWebsiteDataStoreSetResourceLoadStatisticsDebugMode(WKWebsiteDataStoreRef dataStoreRef, bool enable)
73 {
74     WebKit::toImpl(dataStoreRef)->setResourceLoadStatisticsDebugMode(enable);
75 }
76
77 void WKWebsiteDataStoreSetResourceLoadStatisticsDebugModeWithCompletionHandler(WKWebsiteDataStoreRef dataStoreRef, bool enable, void* context, WKWebsiteDataStoreStatisticsDebugModeFunction completionHandler)
78 {
79     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
80     if (!store) {
81         completionHandler(context);
82         return;
83     }
84     
85     store->setResourceLoadStatisticsDebugMode(enable, [context, completionHandler] {
86         completionHandler(context);
87     });
88 }
89
90 void WKWebsiteDataStoreSetResourceLoadStatisticsPrevalentResourceForDebugMode(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreStatisticsDebugModeFunction completionHandler)
91 {
92     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
93     if (!store) {
94         completionHandler(context);
95         return;
96     }
97     
98     store->setPrevalentResourceForDebugMode(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
99         completionHandler(context);
100     });
101 }
102 void WKWebsiteDataStoreSetStatisticsLastSeen(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, double seconds, void* context, WKWebsiteDataStoreStatisticsLastSeenFunction completionHandler)
103 {
104     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
105     if (!store) {
106         completionHandler(context);
107         return;
108     }
109
110     store->setLastSeen(URL(URL(), WebKit::toImpl(host)->string()), Seconds { seconds }, [context, completionHandler] {
111         completionHandler(context);
112     });
113 }
114
115 void WKWebsiteDataStoreSetStatisticsPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, bool value, void* context, WKWebsiteDataStoreStatisticsPrevalentResourceFunction completionHandler)
116 {
117     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
118     if (!store) {
119         completionHandler(context);
120         return;
121     }
122
123     if (value)
124         store->setPrevalentResource(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
125             completionHandler(context);
126         });
127     else
128         store->clearPrevalentResource(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
129             completionHandler(context);
130         });
131 }
132
133 void WKWebsiteDataStoreSetStatisticsVeryPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, bool value, void* context, WKWebsiteDataStoreStatisticsVeryPrevalentResourceFunction completionHandler)
134 {
135     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
136     if (!store) {
137         completionHandler(context);
138         return;
139     }
140
141     if (value)
142         store->setVeryPrevalentResource(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
143             completionHandler(context);
144         });
145     else
146         store->clearPrevalentResource(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
147             completionHandler(context);
148         });
149 }
150
151 void WKWebsiteDataStoreDumpResourceLoadStatistics(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreDumpResourceLoadStatisticsFunction callback)
152 {
153     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
154     if (!store) {
155         callback(WebKit::toAPI(emptyString().impl()), context);
156         return;
157     }
158
159     store->dumpResourceLoadStatistics([context, callback] (const String& resourceLoadStatistics) {
160         callback(WebKit::toAPI(resourceLoadStatistics.impl()), context);
161     });
162 }
163
164 void WKWebsiteDataStoreIsStatisticsPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreIsStatisticsPrevalentResourceFunction callback)
165 {
166     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
167     if (!store) {
168         callback(false, context);
169         return;
170     }
171
172     store->isPrevalentResource(URL(URL(), WebKit::toImpl(host)->string()), [context, callback](bool isPrevalentResource) {
173         callback(isPrevalentResource, context);
174     });
175 }
176
177 void WKWebsiteDataStoreIsStatisticsVeryPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreIsStatisticsPrevalentResourceFunction callback)
178 {
179     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
180     if (!store) {
181         callback(false, context);
182         return;
183     }
184     
185     store->isVeryPrevalentResource(URL(URL(), WebKit::toImpl(host)->string()), [context, callback](bool isVeryPrevalentResource) {
186         callback(isVeryPrevalentResource, context);
187     });
188 }
189
190 void WKWebsiteDataStoreIsStatisticsRegisteredAsSubresourceUnder(WKWebsiteDataStoreRef dataStoreRef, WKStringRef subresourceHost, WKStringRef topFrameHost, void* context, WKWebsiteDataStoreIsStatisticsRegisteredAsSubresourceUnderFunction callback)
191 {
192     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
193     if (!store) {
194         callback(false, context);
195         return;
196     }
197     
198     store->isRegisteredAsSubresourceUnder(URL(URL(), WebKit::toImpl(subresourceHost)->string()), URL(URL(), WebKit::toImpl(topFrameHost)->string()), [context, callback](bool isRegisteredAsSubresourceUnder) {
199         callback(isRegisteredAsSubresourceUnder, context);
200     });
201 }
202
203 void WKWebsiteDataStoreIsStatisticsRegisteredAsSubFrameUnder(WKWebsiteDataStoreRef dataStoreRef, WKStringRef subFrameHost, WKStringRef topFrameHost, void* context, WKWebsiteDataStoreIsStatisticsRegisteredAsSubFrameUnderFunction callback)
204 {
205     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
206     if (!store) {
207         callback(false, context);
208         return;
209     }
210
211     store->isRegisteredAsSubFrameUnder(URL(URL(), WebKit::toImpl(subFrameHost)->string()), URL(URL(), WebKit::toImpl(topFrameHost)->string()), [context, callback](bool isRegisteredAsSubFrameUnder) {
212         callback(isRegisteredAsSubFrameUnder, context);
213     });
214 }
215
216 void WKWebsiteDataStoreIsStatisticsRegisteredAsRedirectingTo(WKWebsiteDataStoreRef dataStoreRef, WKStringRef hostRedirectedFrom, WKStringRef hostRedirectedTo, void* context, WKWebsiteDataStoreIsStatisticsRegisteredAsRedirectingToFunction callback)
217 {
218     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
219     if (!store) {
220         callback(false, context);
221         return;
222     }
223
224     store->isRegisteredAsRedirectingTo(URL(URL(), WebKit::toImpl(hostRedirectedFrom)->string()), URL(URL(), WebKit::toImpl(hostRedirectedTo)->string()), [context, callback](bool isRegisteredAsRedirectingTo) {
225         callback(isRegisteredAsRedirectingTo, context);
226     });
227 }
228
229 void WKWebsiteDataStoreSetStatisticsHasHadUserInteraction(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, bool value, void* context, WKWebsiteDataStoreStatisticsHasHadUserInteractionFunction completionHandler)
230 {
231     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
232     if (!store) {
233         completionHandler(context);
234         return;
235     }
236
237     if (value)
238         store->logUserInteraction(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
239             completionHandler(context);
240         });
241     else
242         store->clearUserInteraction(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
243             completionHandler(context);
244         });
245 }
246
247 void WKWebsiteDataStoreIsStatisticsHasHadUserInteraction(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreIsStatisticsHasHadUserInteractionFunction callback)
248 {
249     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
250     if (!store) {
251         callback(false, context);
252         return;
253     }
254
255     store->hasHadUserInteraction(URL(URL(), WebKit::toImpl(host)->string()), [context, callback](bool hasHadUserInteraction) {
256         callback(hasHadUserInteraction, context);
257     });
258 }
259
260 void WKWebsiteDataStoreSetStatisticsGrandfathered(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, bool value)
261 {
262     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
263     if (!store)
264         return;
265
266     store->setGrandfathered(URL(URL(), WebKit::toImpl(host)->string()), value);
267 }
268
269 void WKWebsiteDataStoreIsStatisticsGrandfathered(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreIsStatisticsGrandfatheredFunction callback)
270 {
271     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
272     if (!store) {
273         callback(false, context);
274         return;
275     }
276
277     store->hasHadUserInteraction(URL(URL(), WebKit::toImpl(host)->string()), [context, callback](bool isGrandfathered) {
278         callback(isGrandfathered, context);
279     });
280 }
281
282 void WKWebsiteDataStoreSetStatisticsSubframeUnderTopFrameOrigin(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef topFrameHost)
283 {
284     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
285     if (!store)
286         return;
287
288     store->setSubframeUnderTopFrameOrigin(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(topFrameHost)->string()));
289 }
290
291 void WKWebsiteDataStoreSetStatisticsSubresourceUnderTopFrameOrigin(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef topFrameHost)
292 {
293     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
294     if (!store)
295         return;
296
297     store->setSubresourceUnderTopFrameOrigin(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(topFrameHost)->string()));
298 }
299
300 void WKWebsiteDataStoreSetStatisticsSubresourceUniqueRedirectTo(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef hostRedirectedTo)
301 {
302     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
303     if (!store)
304         return;
305
306     store->setSubresourceUniqueRedirectTo(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(hostRedirectedTo)->string()));
307 }
308
309 void WKWebsiteDataStoreSetStatisticsSubresourceUniqueRedirectFrom(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef hostRedirectedFrom)
310 {
311     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
312     if (!store)
313         return;
314     
315     store->setSubresourceUniqueRedirectFrom(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(hostRedirectedFrom)->string()));
316 }
317
318 void WKWebsiteDataStoreSetStatisticsTopFrameUniqueRedirectTo(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef hostRedirectedTo)
319 {
320     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
321     if (!store)
322         return;
323     
324     store->setTopFrameUniqueRedirectTo(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(hostRedirectedTo)->string()));
325 }
326
327 void WKWebsiteDataStoreSetStatisticsTopFrameUniqueRedirectFrom(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef hostRedirectedFrom)
328 {
329     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
330     if (!store)
331         return;
332     
333     store->setTopFrameUniqueRedirectFrom(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(hostRedirectedFrom)->string()));
334 }
335
336 void WKWebsiteDataStoreSetStatisticsTimeToLiveUserInteraction(WKWebsiteDataStoreRef dataStoreRef, double seconds)
337 {
338     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
339     if (!store)
340         return;
341
342     store->setTimeToLiveUserInteraction(Seconds { seconds });
343 }
344
345 void WKWebsiteDataStoreStatisticsProcessStatisticsAndDataRecords(WKWebsiteDataStoreRef dataStoreRef)
346 {
347     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
348     if (!store)
349         return;
350
351     store->scheduleStatisticsAndDataRecordsProcessing();
352 }
353
354 void WKWebsiteDataStoreStatisticsUpdateCookieBlocking(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreStatisticsUpdateCookieBlockingFunction completionHandler)
355 {
356     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
357     if (!store) {
358         completionHandler(context);
359         return;
360     }
361
362     store->scheduleCookieBlockingUpdate([context, completionHandler]() {
363         completionHandler(context);
364     });
365 }
366
367 void WKWebsiteDataStoreStatisticsSubmitTelemetry(WKWebsiteDataStoreRef dataStoreRef)
368 {
369     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
370     if (!store)
371         return;
372
373     store->submitTelemetry();
374 }
375
376 void WKWebsiteDataStoreSetStatisticsNotifyPagesWhenDataRecordsWereScanned(WKWebsiteDataStoreRef dataStoreRef, bool value)
377 {
378     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
379     if (!store)
380         return;
381
382     store->setNotifyPagesWhenDataRecordsWereScanned(value);
383 }
384
385 void WKWebsiteDataStoreSetStatisticsShouldClassifyResourcesBeforeDataRecordsRemoval(WKWebsiteDataStoreRef dataStoreRef, bool value)
386 {
387     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
388     if (!store)
389         return;
390
391     store->setShouldClassifyResourcesBeforeDataRecordsRemoval(value);
392 }
393
394 void WKWebsiteDataStoreSetStatisticsNotifyPagesWhenTelemetryWasCaptured(WKWebsiteDataStoreRef, bool value)
395 {
396     WebKit::WebResourceLoadStatisticsTelemetry::setNotifyPagesWhenTelemetryWasCaptured(value);
397 }
398
399 void WKWebsiteDataStoreSetStatisticsMinimumTimeBetweenDataRecordsRemoval(WKWebsiteDataStoreRef dataStoreRef, double seconds)
400 {
401     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
402     if (!store)
403         return;
404
405     store->setMinimumTimeBetweenDataRecordsRemoval(Seconds { seconds });
406 }
407
408 void WKWebsiteDataStoreSetStatisticsGrandfatheringTime(WKWebsiteDataStoreRef dataStoreRef, double seconds)
409 {
410     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
411     if (!store)
412         return;
413
414     store->setGrandfatheringTime(Seconds {seconds });
415 }
416
417 void WKWebsiteDataStoreSetStatisticsMaxStatisticsEntries(WKWebsiteDataStoreRef dataStoreRef, unsigned entries)
418 {
419     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
420     if (!store)
421         return;
422
423     store->setMaxStatisticsEntries(entries);
424 }
425
426 void WKWebsiteDataStoreSetStatisticsPruneEntriesDownTo(WKWebsiteDataStoreRef dataStoreRef, unsigned entries)
427 {
428     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
429     if (!store)
430         return;
431
432     store->setPruneEntriesDownTo(entries);
433 }
434
435 void WKWebsiteDataStoreStatisticsClearInMemoryAndPersistentStore(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreStatisticsClearInMemoryAndPersistentStoreFunction callback)
436 {
437     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
438     if (!store) {
439         callback(context);
440         return;
441     }
442
443     store->scheduleClearInMemoryAndPersistent(WebKit::WebResourceLoadStatisticsStore::ShouldGrandfather::Yes, [context, callback]() {
444         callback(context);
445     });
446 }
447
448 void WKWebsiteDataStoreStatisticsClearInMemoryAndPersistentStoreModifiedSinceHours(WKWebsiteDataStoreRef dataStoreRef, unsigned hours, void* context, WKWebsiteDataStoreStatisticsClearInMemoryAndPersistentStoreModifiedSinceHoursFunction callback)
449 {
450     auto* store = WebKit::toImpl(dataStoreRef)->websiteDataStore().resourceLoadStatistics();
451     if (!store) {
452         callback(context);
453         return;
454     }
455
456     store->scheduleClearInMemoryAndPersistent(WallTime::now() - Seconds::fromHours(hours), WebKit::WebResourceLoadStatisticsStore::ShouldGrandfather::Yes, [context, callback]() {
457         callback(context);
458     });
459 }
460
461 void WKWebsiteDataStoreStatisticsClearThroughWebsiteDataRemoval(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreStatisticsClearThroughWebsiteDataRemovalFunction callback)
462 {
463     OptionSet<WebKit::WebsiteDataType> dataTypes = WebKit::WebsiteDataType::ResourceLoadStatistics;
464     WebKit::toImpl(dataStoreRef)->websiteDataStore().removeData(dataTypes, WallTime::fromRawSeconds(0), [context, callback] {
465         callback(context);
466     });
467 }
468
469 void WKWebsiteDataStoreSetStatisticsCacheMaxAgeCap(WKWebsiteDataStoreRef dataStoreRef, double seconds, void* context, WKWebsiteDataStoreSetStatisticsCacheMaxAgeCapFunction callback)
470 {
471     WebKit::toImpl(dataStoreRef)->websiteDataStore().setCacheMaxAgeCapForPrevalentResources(Seconds { seconds }, [context, callback] {
472         callback(context);
473     });
474 }
475
476 void WKWebsiteDataStoreStatisticsResetToConsistentState(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreStatisticsResetToConsistentStateFunction completionHandler)
477 {
478     auto callbackAggregator = CallbackAggregator::create([context, completionHandler]() {
479         completionHandler(context);
480     });
481
482     auto& store = WebKit::toImpl(dataStoreRef)->websiteDataStore();
483     store.clearResourceLoadStatisticsInWebProcesses([callbackAggregator = callbackAggregator.copyRef()] { });
484     store.resetCacheMaxAgeCapForPrevalentResources([callbackAggregator = callbackAggregator.copyRef()] { });
485
486     auto* statisticsStore = store.resourceLoadStatistics();
487     if (!statisticsStore)
488         return;
489
490     statisticsStore->resetParametersToDefaultValues([callbackAggregator = callbackAggregator.copyRef()] { });
491     statisticsStore->scheduleClearInMemoryAndPersistent(WebKit::WebResourceLoadStatisticsStore::ShouldGrandfather::No, [callbackAggregator = callbackAggregator.copyRef()] { });
492 }
493
494 void WKWebsiteDataStoreRemoveAllFetchCaches(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreRemoveFetchCacheRemovalFunction callback)
495 {
496     OptionSet<WebKit::WebsiteDataType> dataTypes = WebKit::WebsiteDataType::DOMCache;
497     WebKit::toImpl(dataStoreRef)->websiteDataStore().removeData(dataTypes, -WallTime::infinity(), [context, callback] {
498         callback(context);
499     });
500 }
501
502 void WKWebsiteDataStoreRemoveFetchCacheForOrigin(WKWebsiteDataStoreRef dataStoreRef, WKSecurityOriginRef origin, void* context, WKWebsiteDataStoreRemoveFetchCacheRemovalFunction callback)
503 {
504     WebKit::WebsiteDataRecord dataRecord;
505     dataRecord.add(WebKit::WebsiteDataType::DOMCache, WebKit::toImpl(origin)->securityOrigin().data());
506     Vector<WebKit::WebsiteDataRecord> dataRecords = { WTFMove(dataRecord) };
507
508     OptionSet<WebKit::WebsiteDataType> dataTypes = WebKit::WebsiteDataType::DOMCache;
509     WebKit::toImpl(dataStoreRef)->websiteDataStore().removeData(dataTypes, dataRecords, [context, callback] {
510         callback(context);
511     });
512 }
513
514 void WKWebsiteDataStoreRemoveAllIndexedDatabases(WKWebsiteDataStoreRef dataStoreRef)
515 {
516     OptionSet<WebKit::WebsiteDataType> dataTypes = WebKit::WebsiteDataType::IndexedDBDatabases;
517     WebKit::toImpl(dataStoreRef)->websiteDataStore().removeData(dataTypes, -WallTime::infinity(), [] { });
518 }
519
520 void WKWebsiteDataStoreRemoveAllServiceWorkerRegistrations(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreRemoveAllServiceWorkerRegistrationsCallback callback)
521 {
522 #if ENABLE(SERVICE_WORKER)
523     OptionSet<WebKit::WebsiteDataType> dataTypes = WebKit::WebsiteDataType::ServiceWorkerRegistrations;
524     WebKit::toImpl(dataStoreRef)->websiteDataStore().removeData(dataTypes, -WallTime::infinity(), [context, callback] {
525         callback(context);
526     });
527 #else
528     UNUSED_PARAM(dataStoreRef);
529     callback(context);
530 #endif
531 }
532
533 void WKWebsiteDataStoreGetFetchCacheOrigins(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreGetFetchCacheOriginsFunction callback)
534 {
535     WebKit::toImpl(dataStoreRef)->websiteDataStore().fetchData(WebKit::WebsiteDataType::DOMCache, { }, [context, callback] (auto dataRecords) {
536         Vector<RefPtr<API::Object>> securityOrigins;
537         for (const auto& dataRecord : dataRecords) {
538             for (const auto& origin : dataRecord.origins)
539                 securityOrigins.append(API::SecurityOrigin::create(origin.securityOrigin()));
540         }
541         callback(WebKit::toAPI(API::Array::create(WTFMove(securityOrigins)).ptr()), context);
542     });
543 }
544
545 void WKWebsiteDataStoreGetFetchCacheSizeForOrigin(WKWebsiteDataStoreRef dataStoreRef, WKStringRef origin, void* context, WKWebsiteDataStoreGetFetchCacheSizeForOriginFunction callback)
546 {
547     OptionSet<WebKit::WebsiteDataFetchOption> fetchOptions = WebKit::WebsiteDataFetchOption::ComputeSizes;
548
549     WebKit::toImpl(dataStoreRef)->websiteDataStore().fetchData(WebKit::WebsiteDataType::DOMCache, fetchOptions, [origin, context, callback] (auto dataRecords) {
550         auto originData = WebCore::SecurityOrigin::createFromString(WebKit::toImpl(origin)->string())->data();
551         for (auto& dataRecord : dataRecords) {
552             for (const auto& recordOrigin : dataRecord.origins) {
553                 if (originData == recordOrigin) {
554                     callback(dataRecord.size ? dataRecord.size->totalSize : 0, context);
555                     return;
556                 }
557
558             }
559         }
560         callback(0, context);
561     });
562 }
563
564 WKStringRef WKWebsiteDataStoreCopyServiceWorkerRegistrationDirectory(WKWebsiteDataStoreRef dataStoreRef)
565 {
566     return WebKit::toCopiedAPI(WebKit::toImpl(dataStoreRef)->websiteDataStore().serviceWorkerRegistrationDirectory());
567 }
568
569 void WKWebsiteDataStoreSetServiceWorkerRegistrationDirectory(WKWebsiteDataStoreRef dataStoreRef, WKStringRef serviceWorkerRegistrationDirectory)
570 {
571     WebKit::toImpl(dataStoreRef)->websiteDataStore().setServiceWorkerRegistrationDirectory(WebKit::toImpl(serviceWorkerRegistrationDirectory)->string());
572 }
573
574 void WKWebsiteDataStoreSetCacheStoragePerOriginQuota(WKWebsiteDataStoreRef dataStoreRef, uint64_t quota)
575 {
576     WebKit::toImpl(dataStoreRef)->websiteDataStore().setCacheStoragePerOriginQuota(quota); 
577 }
578
579 void WKWebsiteDataStoreSetWebAuthenticationMockConfiguration(WKWebsiteDataStoreRef dataStoreRef, WKDictionaryRef configurationRef)
580 {
581 #if ENABLE(WEB_AUTHN)
582     MockWebAuthenticationConfiguration configuration;
583
584     if (auto silentFailureRef = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(configurationRef, adoptWK(WKStringCreateWithUTF8CString("SilentFailure")).get())))
585         configuration.silentFailure = WKBooleanGetValue(silentFailureRef);
586
587     if (auto localRef = static_cast<WKDictionaryRef>(WKDictionaryGetItemForKey(configurationRef, adoptWK(WKStringCreateWithUTF8CString("Local")).get()))) {
588         MockWebAuthenticationConfiguration::Local local;
589         local.acceptAuthentication = WKBooleanGetValue(static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(localRef, adoptWK(WKStringCreateWithUTF8CString("AcceptAuthentication")).get())));
590         local.acceptAttestation = WKBooleanGetValue(static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(localRef, adoptWK(WKStringCreateWithUTF8CString("AcceptAttestation")).get())));
591         if (local.acceptAttestation) {
592             local.privateKeyBase64 = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(localRef, adoptWK(WKStringCreateWithUTF8CString("PrivateKeyBase64")).get())))->string();
593             local.userCertificateBase64 = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(localRef, adoptWK(WKStringCreateWithUTF8CString("UserCertificateBase64")).get())))->string();
594             local.intermediateCACertificateBase64 = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(localRef, adoptWK(WKStringCreateWithUTF8CString("IntermediateCACertificateBase64")).get())))->string();
595         }
596         configuration.local = WTFMove(local);
597     }
598
599     if (auto hidRef = static_cast<WKDictionaryRef>(WKDictionaryGetItemForKey(configurationRef, adoptWK(WKStringCreateWithUTF8CString("Hid")).get()))) {
600         MockWebAuthenticationConfiguration::Hid hid;
601
602         auto stage = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("Stage")).get())))->string();
603         if (stage == "info")
604             hid.stage = MockWebAuthenticationConfiguration::Hid::Stage::Info;
605         if (stage == "request")
606             hid.stage = MockWebAuthenticationConfiguration::Hid::Stage::Request;
607
608         auto subStage = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("SubStage")).get())))->string();
609         if (subStage == "init")
610             hid.subStage = MockWebAuthenticationConfiguration::Hid::SubStage::Init;
611         if (subStage == "msg")
612             hid.subStage = MockWebAuthenticationConfiguration::Hid::SubStage::Msg;
613
614         auto error = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("Error")).get())))->string();
615         if (error == "success")
616             hid.error = MockWebAuthenticationConfiguration::Hid::Error::Success;
617         if (error == "data-not-sent")
618             hid.error = MockWebAuthenticationConfiguration::Hid::Error::DataNotSent;
619         if (error == "empty-report")
620             hid.error = MockWebAuthenticationConfiguration::Hid::Error::EmptyReport;
621         if (error == "wrong-channel-id")
622             hid.error = MockWebAuthenticationConfiguration::Hid::Error::WrongChannelId;
623         if (error == "malicious-payload")
624             hid.error = MockWebAuthenticationConfiguration::Hid::Error::MaliciousPayload;
625         if (error == "unsupported-options")
626             hid.error = MockWebAuthenticationConfiguration::Hid::Error::UnsupportedOptions;
627         if (error == "wrong-nonce")
628             hid.error = MockWebAuthenticationConfiguration::Hid::Error::WrongNonce;
629
630         if (auto payloadBase64 = static_cast<WKStringRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("PayloadBase64")).get())))
631             hid.payloadBase64 = WebKit::toImpl(payloadBase64)->string();
632
633         if (auto keepAlive = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("KeepAlive")).get())))
634             hid.keepAlive = WKBooleanGetValue(keepAlive);
635
636         if (auto fastDataArrival = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("FastDataArrival")).get())))
637             hid.fastDataArrival = WKBooleanGetValue(fastDataArrival);
638
639         if (auto continueAfterErrorData = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("ContinueAfterErrorData")).get())))
640             hid.continueAfterErrorData = WKBooleanGetValue(continueAfterErrorData);
641
642         configuration.hid = WTFMove(hid);
643     }
644
645     WebKit::toImpl(dataStoreRef)->websiteDataStore().setMockWebAuthenticationConfiguration(WTFMove(configuration));
646 #endif
647 }