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