Domain relationships in the ITP Database should be inserted in a single query and...
[WebKit-https.git] / Source / WebKit / UIProcess / API / C / WKWebsiteDataStoreRef.cpp
1 /*
2  * Copyright (C) 2015-2019 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 "APIHTTPCookieStore.h"
31 #include "MockWebAuthenticationConfiguration.h"
32 #include "ShouldGrandfatherStatistics.h"
33 #include "WKAPICast.h"
34 #include "WKDictionary.h"
35 #include "WKNumber.h"
36 #include "WKRetainPtr.h"
37 #include "WKSecurityOriginRef.h"
38 #include "WKString.h"
39 #include "WebDeviceOrientationAndMotionAccessController.h"
40 #include "WebResourceLoadStatisticsStore.h"
41 #include "WebsiteData.h"
42 #include "WebsiteDataFetchOption.h"
43 #include "WebsiteDataRecord.h"
44 #include "WebsiteDataStore.h"
45 #include "WebsiteDataType.h"
46 #include <wtf/CallbackAggregator.h>
47 #include <wtf/URL.h>
48
49 WKTypeID WKWebsiteDataStoreGetTypeID()
50 {
51     return WebKit::toAPI(WebKit::WebsiteDataStore::APIType);
52 }
53
54 WKWebsiteDataStoreRef WKWebsiteDataStoreGetDefaultDataStore()
55 {
56     return WebKit::toAPI(WebKit::WebsiteDataStore::defaultDataStore().ptr());
57 }
58
59 WKWebsiteDataStoreRef WKWebsiteDataStoreCreateNonPersistentDataStore()
60 {
61     return WebKit::toAPI(&WebKit::WebsiteDataStore::createNonPersistent().leakRef());
62 }
63
64 WKWebsiteDataStoreRef WKWebsiteDataStoreCreateWithConfiguration(WKWebsiteDataStoreConfigurationRef configuration)
65 {
66     auto sessionID = WebKit::toImpl(configuration)->isPersistent() ? PAL::SessionID::generatePersistentSessionID() : PAL::SessionID::generateEphemeralSessionID();
67     return WebKit::toAPI(&WebKit::WebsiteDataStore::create(*WebKit::toImpl(configuration), sessionID).leakRef());
68 }
69
70 void WKWebsiteDataStoreSetResourceLoadStatisticsEnabled(WKWebsiteDataStoreRef dataStoreRef, bool enable)
71 {
72     WebKit::toImpl(dataStoreRef)->setResourceLoadStatisticsEnabled(enable);
73 }
74
75 bool WKWebsiteDataStoreGetResourceLoadStatisticsEnabled(WKWebsiteDataStoreRef dataStoreRef)
76 {
77     return WebKit::toImpl(dataStoreRef)->resourceLoadStatisticsEnabled();
78 }
79
80 void WKWebsiteDataStoreSetResourceLoadStatisticsDebugMode(WKWebsiteDataStoreRef dataStoreRef, bool enable)
81 {
82     WebKit::toImpl(dataStoreRef)->setResourceLoadStatisticsDebugMode(enable);
83 }
84
85 WKHTTPCookieStoreRef WKWebsiteDataStoreGetHTTPCookieStore(WKWebsiteDataStoreRef dataStoreRef)
86 {
87     return WebKit::toAPI(&WebKit::toImpl(dataStoreRef)->cookieStore());
88 }
89
90 void WKWebsiteDataStoreSetResourceLoadStatisticsDebugModeWithCompletionHandler(WKWebsiteDataStoreRef dataStoreRef, bool enable, void* context, WKWebsiteDataStoreStatisticsDebugModeFunction completionHandler)
91 {
92 #if ENABLE(RESOURCE_LOAD_STATISTICS)
93     WebKit::toImpl(dataStoreRef)->setResourceLoadStatisticsDebugMode(enable, [context, completionHandler] {
94         completionHandler(context);
95     });
96 #else
97     completionHandler(context);
98 #endif
99 }
100
101 void WKWebsiteDataStoreSetResourceLoadStatisticsPrevalentResourceForDebugMode(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreStatisticsDebugModeFunction completionHandler)
102 {
103 #if ENABLE(RESOURCE_LOAD_STATISTICS)
104     WebKit::toImpl(dataStoreRef)->setPrevalentResourceForDebugMode(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
105         completionHandler(context);
106     });
107 #else
108     completionHandler(context);
109 #endif
110 }
111 void WKWebsiteDataStoreSetStatisticsLastSeen(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, double seconds, void* context, WKWebsiteDataStoreStatisticsLastSeenFunction completionHandler)
112 {
113 #if ENABLE(RESOURCE_LOAD_STATISTICS)
114     WebKit::toImpl(dataStoreRef)->setLastSeen(URL(URL(), WebKit::toImpl(host)->string()), Seconds { seconds }, [context, completionHandler] {
115         completionHandler(context);
116     });
117 #else
118     completionHandler(context);
119 #endif
120 }
121
122 void WKWebsiteDataStoreSetStatisticsMergeStatistic(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef topFrameDomain1, WKStringRef topFrameDomain2, double lastSeen, bool hadUserInteraction, double mostRecentUserInteraction, bool isGrandfathered, bool isPrevalent, bool isVeryPrevalent, unsigned dataRecordsRemoved, void* context, WKWebsiteDataStoreStatisticsMergeStatisticFunction completionHandler)
123 {
124 #if ENABLE(RESOURCE_LOAD_STATISTICS)
125     WebKit::toImpl(dataStoreRef)->mergeStatisticForTesting(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(topFrameDomain1)->string()), URL(URL(), WebKit::toImpl(topFrameDomain2)->string()), Seconds { lastSeen }, hadUserInteraction, Seconds { mostRecentUserInteraction }, isGrandfathered, isPrevalent, isVeryPrevalent, dataRecordsRemoved, [context, completionHandler] {
126         completionHandler(context);
127     });
128 #else
129     completionHandler(context);
130 #endif
131 }
132
133 void WKWebsiteDataStoreSetStatisticsPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, bool value, void* context, WKWebsiteDataStoreStatisticsPrevalentResourceFunction completionHandler)
134 {
135 #if ENABLE(RESOURCE_LOAD_STATISTICS)
136     auto& websiteDataStore = *WebKit::toImpl(dataStoreRef);
137
138     if (value)
139         websiteDataStore.setPrevalentResource(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
140             completionHandler(context);
141         });
142     else
143         websiteDataStore.clearPrevalentResource(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
144             completionHandler(context);
145         });
146 #else
147     completionHandler(context);
148 #endif
149 }
150
151 void WKWebsiteDataStoreSetStatisticsVeryPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, bool value, void* context, WKWebsiteDataStoreStatisticsVeryPrevalentResourceFunction completionHandler)
152 {
153 #if ENABLE(RESOURCE_LOAD_STATISTICS)
154     auto& websiteDataStore = *WebKit::toImpl(dataStoreRef);
155
156     if (value)
157         websiteDataStore.setVeryPrevalentResource(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
158             completionHandler(context);
159         });
160     else
161         websiteDataStore.clearPrevalentResource(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
162             completionHandler(context);
163         });
164 #else
165     completionHandler(context);
166 #endif
167 }
168
169 void WKWebsiteDataStoreDumpResourceLoadStatistics(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreDumpResourceLoadStatisticsFunction callback)
170 {
171 #if ENABLE(RESOURCE_LOAD_STATISTICS)
172     WebKit::toImpl(dataStoreRef)->dumpResourceLoadStatistics([context, callback] (const String& resourceLoadStatistics) {
173         callback(WebKit::toAPI(resourceLoadStatistics.impl()), context);
174     });
175 #else
176     callback(WebKit::toAPI(emptyString().impl()), context);
177 #endif
178 }
179
180 void WKWebsiteDataStoreIsStatisticsPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreIsStatisticsPrevalentResourceFunction callback)
181 {
182 #if ENABLE(RESOURCE_LOAD_STATISTICS)
183     WebKit::toImpl(dataStoreRef)->isPrevalentResource(URL(URL(), WebKit::toImpl(host)->string()), [context, callback](bool isPrevalentResource) {
184         callback(isPrevalentResource, context);
185     });
186 #else
187     callback(false, context);
188 #endif
189 }
190
191 void WKWebsiteDataStoreIsStatisticsVeryPrevalentResource(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreIsStatisticsPrevalentResourceFunction callback)
192 {
193 #if ENABLE(RESOURCE_LOAD_STATISTICS)
194     WebKit::toImpl(dataStoreRef)->isVeryPrevalentResource(URL(URL(), WebKit::toImpl(host)->string()), [context, callback](bool isVeryPrevalentResource) {
195         callback(isVeryPrevalentResource, context);
196     });
197 #else
198     callback(false, context);
199 #endif
200 }
201
202 void WKWebsiteDataStoreIsStatisticsRegisteredAsSubresourceUnder(WKWebsiteDataStoreRef dataStoreRef, WKStringRef subresourceHost, WKStringRef topFrameHost, void* context, WKWebsiteDataStoreIsStatisticsRegisteredAsSubresourceUnderFunction callback)
203 {
204 #if ENABLE(RESOURCE_LOAD_STATISTICS)
205     WebKit::toImpl(dataStoreRef)->isRegisteredAsSubresourceUnder(URL(URL(), WebKit::toImpl(subresourceHost)->string()), URL(URL(), WebKit::toImpl(topFrameHost)->string()), [context, callback](bool isRegisteredAsSubresourceUnder) {
206         callback(isRegisteredAsSubresourceUnder, context);
207     });
208 #else
209     callback(false, context);
210 #endif
211 }
212
213 void WKWebsiteDataStoreIsStatisticsRegisteredAsSubFrameUnder(WKWebsiteDataStoreRef dataStoreRef, WKStringRef subFrameHost, WKStringRef topFrameHost, void* context, WKWebsiteDataStoreIsStatisticsRegisteredAsSubFrameUnderFunction callback)
214 {
215 #if ENABLE(RESOURCE_LOAD_STATISTICS)
216     WebKit::toImpl(dataStoreRef)->isRegisteredAsSubFrameUnder(URL(URL(), WebKit::toImpl(subFrameHost)->string()), URL(URL(), WebKit::toImpl(topFrameHost)->string()), [context, callback](bool isRegisteredAsSubFrameUnder) {
217         callback(isRegisteredAsSubFrameUnder, context);
218     });
219 #else
220     callback(false, context);
221 #endif
222 }
223
224 void WKWebsiteDataStoreIsStatisticsRegisteredAsRedirectingTo(WKWebsiteDataStoreRef dataStoreRef, WKStringRef hostRedirectedFrom, WKStringRef hostRedirectedTo, void* context, WKWebsiteDataStoreIsStatisticsRegisteredAsRedirectingToFunction callback)
225 {
226 #if ENABLE(RESOURCE_LOAD_STATISTICS)
227     WebKit::toImpl(dataStoreRef)->isRegisteredAsRedirectingTo(URL(URL(), WebKit::toImpl(hostRedirectedFrom)->string()), URL(URL(), WebKit::toImpl(hostRedirectedTo)->string()), [context, callback](bool isRegisteredAsRedirectingTo) {
228         callback(isRegisteredAsRedirectingTo, context);
229     });
230 #else
231     callback(false, context);
232 #endif
233 }
234
235 void WKWebsiteDataStoreSetStatisticsHasHadUserInteraction(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, bool value, void* context, WKWebsiteDataStoreStatisticsHasHadUserInteractionFunction completionHandler)
236 {
237 #if ENABLE(RESOURCE_LOAD_STATISTICS)
238     auto& dataStore = *WebKit::toImpl(dataStoreRef);
239
240     if (value)
241         dataStore.logUserInteraction(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
242             completionHandler(context);
243         });
244     else
245         dataStore.clearUserInteraction(URL(URL(), WebKit::toImpl(host)->string()), [context, completionHandler] {
246             completionHandler(context);
247         });
248 #else
249     completionHandler(context);
250 #endif
251 }
252
253 void WKWebsiteDataStoreIsStatisticsHasHadUserInteraction(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreIsStatisticsHasHadUserInteractionFunction callback)
254 {
255 #if ENABLE(RESOURCE_LOAD_STATISTICS)
256     WebKit::toImpl(dataStoreRef)->hasHadUserInteraction(URL(URL(), WebKit::toImpl(host)->string()), [context, callback](bool hasHadUserInteraction) {
257         callback(hasHadUserInteraction, context);
258     });
259 #else
260     callback(false, context);
261 #endif
262 }
263
264 void WKWebsiteDataStoreIsStatisticsOnlyInDatabaseOnce(WKWebsiteDataStoreRef dataStoreRef, WKStringRef subHost, WKStringRef topHost, void* context, WKWebsiteDataStoreIsStatisticsOnlyInDatabaseOnceFunction callback)
265 {
266 #if ENABLE(RESOURCE_LOAD_STATISTICS)
267     WebKit::toImpl(dataStoreRef)->isRelationshipOnlyInDatabaseOnce(URL(URL(), WebKit::toImpl(subHost)->string()), URL(URL(), WebKit::toImpl(topHost)->string()), [context, callback](bool onlyInDatabaseOnce) {
268         callback(onlyInDatabaseOnce, context);
269     });
270 #else
271     callback(false, context);
272 #endif
273 }
274
275 void WKWebsiteDataStoreSetStatisticsGrandfathered(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, bool value)
276 {
277 #if ENABLE(RESOURCE_LOAD_STATISTICS)
278     WebKit::toImpl(dataStoreRef)->setGrandfathered(URL(URL(), WebKit::toImpl(host)->string()), value, [] { });
279 #endif
280 }
281
282 void WKWebsiteDataStoreIsStatisticsGrandfathered(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreIsStatisticsGrandfatheredFunction callback)
283 {
284 #if ENABLE(RESOURCE_LOAD_STATISTICS)
285     WebKit::toImpl(dataStoreRef)->isGrandfathered(URL(URL(), WebKit::toImpl(host)->string()), [context, callback](bool isGrandfathered) {
286         callback(isGrandfathered, context);
287     });
288 #else
289     callback(false, context);
290 #endif
291 }
292
293 void WKWebsiteDataStoreSetUseITPDatabase(WKWebsiteDataStoreRef dataStoreRef, bool value)
294 {
295 #if ENABLE(RESOURCE_LOAD_STATISTICS)
296     WebKit::toImpl(dataStoreRef)->setUseITPDatabase(value);
297 #endif
298 }
299
300 void WKWebsiteDataStoreSetStatisticsSubframeUnderTopFrameOrigin(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef topFrameHost)
301 {
302 #if ENABLE(RESOURCE_LOAD_STATISTICS)
303     WebKit::toImpl(dataStoreRef)->setSubframeUnderTopFrameDomain(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(topFrameHost)->string()), [] { });
304 #endif
305 }
306
307 void WKWebsiteDataStoreSetStatisticsSubresourceUnderTopFrameOrigin(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef topFrameHost)
308 {
309 #if ENABLE(RESOURCE_LOAD_STATISTICS)
310     WebKit::toImpl(dataStoreRef)->setSubresourceUnderTopFrameDomain(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(topFrameHost)->string()), [] { });
311 #endif
312 }
313
314 void WKWebsiteDataStoreSetStatisticsSubresourceUniqueRedirectTo(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef hostRedirectedTo)
315 {
316 #if ENABLE(RESOURCE_LOAD_STATISTICS)
317     WebKit::toImpl(dataStoreRef)->setSubresourceUniqueRedirectTo(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(hostRedirectedTo)->string()), [] { });
318 #endif
319 }
320
321 void WKWebsiteDataStoreSetStatisticsSubresourceUniqueRedirectFrom(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef hostRedirectedFrom)
322 {
323 #if ENABLE(RESOURCE_LOAD_STATISTICS)
324     WebKit::toImpl(dataStoreRef)->setSubresourceUniqueRedirectFrom(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(hostRedirectedFrom)->string()), [] { });
325 #endif
326 }
327
328 void WKWebsiteDataStoreSetStatisticsTopFrameUniqueRedirectTo(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef hostRedirectedTo)
329 {
330 #if ENABLE(RESOURCE_LOAD_STATISTICS)
331     WebKit::toImpl(dataStoreRef)->setTopFrameUniqueRedirectTo(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(hostRedirectedTo)->string()), [] { });
332 #endif
333 }
334
335 void WKWebsiteDataStoreSetStatisticsTopFrameUniqueRedirectFrom(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, WKStringRef hostRedirectedFrom)
336 {
337 #if ENABLE(RESOURCE_LOAD_STATISTICS)
338     WebKit::toImpl(dataStoreRef)->setTopFrameUniqueRedirectFrom(URL(URL(), WebKit::toImpl(host)->string()), URL(URL(), WebKit::toImpl(hostRedirectedFrom)->string()), [] { });
339 #endif
340 }
341
342 void WKWebsiteDataStoreSetStatisticsCrossSiteLoadWithLinkDecoration(WKWebsiteDataStoreRef dataStoreRef, WKStringRef fromHost, WKStringRef toHost, void* context, WKWebsiteDataStoreSetStatisticsCrossSiteLoadWithLinkDecorationFunction callback)
343 {
344 #if ENABLE(RESOURCE_LOAD_STATISTICS)
345     WebKit::toImpl(dataStoreRef)->setCrossSiteLoadWithLinkDecorationForTesting(URL(URL(), WebKit::toImpl(fromHost)->string()), URL(URL(), WebKit::toImpl(toHost)->string()), [context, callback] {
346         callback(context);
347     });
348 #else
349     callback(context);
350 #endif
351 }
352
353 void WKWebsiteDataStoreSetStatisticsTimeToLiveUserInteraction(WKWebsiteDataStoreRef dataStoreRef, double seconds, void* context, WKWebsiteDataStoreSetStatisticsTimeToLiveUserInteractionFunction callback)
354 {
355 #if ENABLE(RESOURCE_LOAD_STATISTICS)
356     WebKit::toImpl(dataStoreRef)->setTimeToLiveUserInteraction(Seconds { seconds }, [context, callback] {
357         callback(context);
358     });
359 #else
360     callback(context);
361 #endif
362 }
363
364 void WKWebsiteDataStoreStatisticsProcessStatisticsAndDataRecords(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreStatisticsProcessStatisticsAndDataRecordsFunction callback)
365 {
366 #if ENABLE(RESOURCE_LOAD_STATISTICS)
367     WebKit::toImpl(dataStoreRef)->scheduleStatisticsAndDataRecordsProcessing([context, callback] {
368         callback(context);
369     });
370 #else
371     callback(context);
372 #endif
373 }
374
375 void WKWebsiteDataStoreStatisticsUpdateCookieBlocking(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreStatisticsUpdateCookieBlockingFunction completionHandler)
376 {
377 #if ENABLE(RESOURCE_LOAD_STATISTICS)
378     WebKit::toImpl(dataStoreRef)->scheduleCookieBlockingUpdate([context, completionHandler]() {
379         completionHandler(context);
380     });
381 #else
382     completionHandler(context);
383 #endif
384 }
385
386 void WKWebsiteDataStoreStatisticsSubmitTelemetry(WKWebsiteDataStoreRef dataStoreRef)
387 {
388 #if ENABLE(RESOURCE_LOAD_STATISTICS)
389     WebKit::toImpl(dataStoreRef)->submitTelemetry();
390 #endif
391 }
392
393 void WKWebsiteDataStoreSetStatisticsNotifyPagesWhenDataRecordsWereScanned(WKWebsiteDataStoreRef dataStoreRef, bool value)
394 {
395 #if ENABLE(RESOURCE_LOAD_STATISTICS)
396     WebKit::toImpl(dataStoreRef)->setNotifyPagesWhenDataRecordsWereScanned(value, [] { });
397 #endif
398 }
399
400 void WKWebsiteDataStoreSetStatisticsIsRunningTest(WKWebsiteDataStoreRef dataStoreRef, bool value, void* context, WKWebsiteDataStoreSetStatisticsIsRunningTestFunction callback)
401 {
402 #if ENABLE(RESOURCE_LOAD_STATISTICS)
403     WebKit::toImpl(dataStoreRef)->setIsRunningResourceLoadStatisticsTest(value, [context, callback] {
404         callback(context);
405     });
406 #else
407     callback(context);
408 #endif
409 }
410
411 void WKWebsiteDataStoreSetStatisticsShouldClassifyResourcesBeforeDataRecordsRemoval(WKWebsiteDataStoreRef dataStoreRef, bool value)
412 {
413 #if ENABLE(RESOURCE_LOAD_STATISTICS)
414     WebKit::toImpl(dataStoreRef)->setShouldClassifyResourcesBeforeDataRecordsRemoval(value, []() { });
415 #endif
416 }
417
418 void WKWebsiteDataStoreSetStatisticsNotifyPagesWhenTelemetryWasCaptured(WKWebsiteDataStoreRef dataStoreRef, bool value)
419 {
420 #if ENABLE(RESOURCE_LOAD_STATISTICS)
421     WebKit::toImpl(dataStoreRef)->setNotifyPagesWhenTelemetryWasCaptured(value, []() { });
422 #endif
423 }
424
425 void WKWebsiteDataStoreSetStatisticsMinimumTimeBetweenDataRecordsRemoval(WKWebsiteDataStoreRef dataStoreRef, double seconds)
426 {
427 #if ENABLE(RESOURCE_LOAD_STATISTICS)
428     WebKit::toImpl(dataStoreRef)->setMinimumTimeBetweenDataRecordsRemoval(Seconds { seconds }, []() { });
429 #endif
430 }
431
432 void WKWebsiteDataStoreSetStatisticsGrandfatheringTime(WKWebsiteDataStoreRef dataStoreRef, double seconds)
433 {
434 #if ENABLE(RESOURCE_LOAD_STATISTICS)
435     WebKit::toImpl(dataStoreRef)->setGrandfatheringTime(Seconds { seconds }, []() { });
436 #endif
437 }
438
439 void WKWebsiteDataStoreSetStatisticsMaxStatisticsEntries(WKWebsiteDataStoreRef dataStoreRef, unsigned entries)
440 {
441 #if ENABLE(RESOURCE_LOAD_STATISTICS)
442     WebKit::toImpl(dataStoreRef)->setMaxStatisticsEntries(entries, []() { });
443 #endif
444 }
445
446 void WKWebsiteDataStoreSetStatisticsPruneEntriesDownTo(WKWebsiteDataStoreRef dataStoreRef, unsigned entries)
447 {
448 #if ENABLE(RESOURCE_LOAD_STATISTICS)
449     WebKit::toImpl(dataStoreRef)->setPruneEntriesDownTo(entries, []() { });
450 #endif
451 }
452
453 void WKWebsiteDataStoreStatisticsClearInMemoryAndPersistentStore(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreStatisticsClearInMemoryAndPersistentStoreFunction callback)
454 {
455 #if ENABLE(RESOURCE_LOAD_STATISTICS)
456     WebKit::toImpl(dataStoreRef)->scheduleClearInMemoryAndPersistent(WebKit::ShouldGrandfatherStatistics::Yes, [context, callback]() {
457         callback(context);
458     });
459 #else
460     callback(context);
461 #endif
462 }
463
464 void WKWebsiteDataStoreStatisticsClearInMemoryAndPersistentStoreModifiedSinceHours(WKWebsiteDataStoreRef dataStoreRef, unsigned hours, void* context, WKWebsiteDataStoreStatisticsClearInMemoryAndPersistentStoreModifiedSinceHoursFunction callback)
465 {
466 #if ENABLE(RESOURCE_LOAD_STATISTICS)
467     WebKit::toImpl(dataStoreRef)->scheduleClearInMemoryAndPersistent(WallTime::now() - Seconds::fromHours(hours), WebKit::ShouldGrandfatherStatistics::Yes, [context, callback]() {
468         callback(context);
469     });
470 #else
471     callback(context);
472 #endif
473 }
474
475 void WKWebsiteDataStoreStatisticsClearThroughWebsiteDataRemoval(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreStatisticsClearThroughWebsiteDataRemovalFunction callback)
476 {
477     OptionSet<WebKit::WebsiteDataType> dataTypes = WebKit::WebsiteDataType::ResourceLoadStatistics;
478     WebKit::toImpl(dataStoreRef)->removeData(dataTypes, WallTime::fromRawSeconds(0), [context, callback] {
479         callback(context);
480     });
481 }
482
483 void WKWebsiteDataStoreStatisticsDeleteCookiesForTesting(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, bool includeHttpOnlyCookies, void* context, WKWebsiteDataStoreStatisticsDeleteCookiesForTestingFunction callback)
484 {
485 #if ENABLE(RESOURCE_LOAD_STATISTICS)
486     WebKit::toImpl(dataStoreRef)->deleteCookiesForTesting(URL(URL(), WebKit::toImpl(host)->string()), includeHttpOnlyCookies, [context, callback] {
487         callback(context);
488     });
489 #else
490     callback(context);
491 #endif
492 }
493
494 void WKWebsiteDataStoreStatisticsHasLocalStorage(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreStatisticsHasLocalStorageFunction callback)
495 {
496 #if ENABLE(RESOURCE_LOAD_STATISTICS)
497     WebKit::toImpl(dataStoreRef)->hasLocalStorageForTesting(URL(URL(), WebKit::toImpl(host)->string()), [context, callback](bool hasLocalStorage) {
498         callback(hasLocalStorage, context);
499     });
500 #else
501     callback(false, context);
502 #endif
503 }
504
505 void WKWebsiteDataStoreSetStatisticsCacheMaxAgeCap(WKWebsiteDataStoreRef dataStoreRef, double seconds, void* context, WKWebsiteDataStoreSetStatisticsCacheMaxAgeCapFunction callback)
506 {
507 #if ENABLE(RESOURCE_LOAD_STATISTICS)
508     WebKit::toImpl(dataStoreRef)->setCacheMaxAgeCapForPrevalentResources(Seconds { seconds }, [context, callback] {
509         callback(context);
510     });
511 #else
512     callback(context);
513 #endif
514 }
515
516 void WKWebsiteDataStoreStatisticsHasIsolatedSession(WKWebsiteDataStoreRef dataStoreRef, WKStringRef host, void* context, WKWebsiteDataStoreStatisticsHasIsolatedSessionFunction callback)
517 {
518 #if ENABLE(RESOURCE_LOAD_STATISTICS)
519     WebKit::toImpl(dataStoreRef)->hasIsolatedSessionForTesting(URL(URL(), WebKit::toImpl(host)->string()), [context, callback](bool hasIsolatedSession) {
520         callback(hasIsolatedSession, context);
521     });
522 #else
523     callback(false, context);
524 #endif
525 }
526
527 void WKWebsiteDataStoreSetResourceLoadStatisticsShouldDowngradeReferrerForTesting(WKWebsiteDataStoreRef dataStoreRef, bool enabled, void* context, WKWebsiteDataStoreSetResourceLoadStatisticsShouldDowngradeReferrerForTestingFunction completionHandler)
528 {
529 #if ENABLE(RESOURCE_LOAD_STATISTICS)
530     WebKit::toImpl(dataStoreRef)->setResourceLoadStatisticsShouldDowngradeReferrerForTesting(enabled, [context, completionHandler] {
531         completionHandler(context);
532     });
533 #else
534     completionHandler(context);
535 #endif
536 }
537
538 void WKWebsiteDataStoreStatisticsResetToConsistentState(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreStatisticsResetToConsistentStateFunction completionHandler)
539 {
540 #if ENABLE(RESOURCE_LOAD_STATISTICS)
541     auto callbackAggregator = CallbackAggregator::create([context, completionHandler]() {
542         completionHandler(context);
543     });
544
545     auto& store = *WebKit::toImpl(dataStoreRef);
546     store.clearResourceLoadStatisticsInWebProcesses([callbackAggregator = callbackAggregator.copyRef()] { });
547     store.resetCacheMaxAgeCapForPrevalentResources([callbackAggregator = callbackAggregator.copyRef()] { });
548     store.resetCrossSiteLoadsWithLinkDecorationForTesting([callbackAggregator = callbackAggregator.copyRef()] { });
549     store.setResourceLoadStatisticsShouldDowngradeReferrerForTesting(true, [callbackAggregator = callbackAggregator.copyRef()] { });
550     store.resetParametersToDefaultValues([callbackAggregator = callbackAggregator.copyRef()] { });
551     store.scheduleClearInMemoryAndPersistent(WebKit::ShouldGrandfatherStatistics::No, [callbackAggregator = callbackAggregator.copyRef()] { });
552 #else
553     UNUSED_PARAM(dataStoreRef);
554     completionHandler(context);
555 #endif
556 }
557
558 void WKWebsiteDataStoreRemoveAllFetchCaches(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreRemoveFetchCacheRemovalFunction callback)
559 {
560     OptionSet<WebKit::WebsiteDataType> dataTypes = WebKit::WebsiteDataType::DOMCache;
561     WebKit::toImpl(dataStoreRef)->removeData(dataTypes, -WallTime::infinity(), [context, callback] {
562         callback(context);
563     });
564 }
565
566 void WKWebsiteDataStoreRemoveFetchCacheForOrigin(WKWebsiteDataStoreRef dataStoreRef, WKSecurityOriginRef origin, void* context, WKWebsiteDataStoreRemoveFetchCacheRemovalFunction callback)
567 {
568     WebKit::WebsiteDataRecord dataRecord;
569     dataRecord.add(WebKit::WebsiteDataType::DOMCache, WebKit::toImpl(origin)->securityOrigin().data());
570     Vector<WebKit::WebsiteDataRecord> dataRecords = { WTFMove(dataRecord) };
571
572     OptionSet<WebKit::WebsiteDataType> dataTypes = WebKit::WebsiteDataType::DOMCache;
573     WebKit::toImpl(dataStoreRef)->removeData(dataTypes, dataRecords, [context, callback] {
574         callback(context);
575     });
576 }
577
578 void WKWebsiteDataStoreRemoveAllIndexedDatabases(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreRemoveAllIndexedDatabasesCallback callback)
579 {
580     OptionSet<WebKit::WebsiteDataType> dataTypes = WebKit::WebsiteDataType::IndexedDBDatabases;
581     WebKit::toImpl(dataStoreRef)->removeData(dataTypes, -WallTime::infinity(), [context, callback] {
582     if (callback)
583         callback(context);
584     });
585 }
586
587 void WKWebsiteDataStoreRemoveLocalStorage(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreRemoveLocalStorageCallback callback)
588 {
589     OptionSet<WebKit::WebsiteDataType> dataTypes = WebKit::WebsiteDataType::LocalStorage;
590     WebKit::toImpl(dataStoreRef)->removeData(dataTypes, -WallTime::infinity(), [context, callback] {
591         if (callback)
592             callback(context);
593     });
594 }
595
596 void WKWebsiteDataStoreRemoveAllServiceWorkerRegistrations(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreRemoveAllServiceWorkerRegistrationsCallback callback)
597 {
598 #if ENABLE(SERVICE_WORKER)
599     OptionSet<WebKit::WebsiteDataType> dataTypes = WebKit::WebsiteDataType::ServiceWorkerRegistrations;
600     WebKit::toImpl(dataStoreRef)->removeData(dataTypes, -WallTime::infinity(), [context, callback] {
601         callback(context);
602     });
603 #else
604     UNUSED_PARAM(dataStoreRef);
605     callback(context);
606 #endif
607 }
608
609 void WKWebsiteDataStoreGetFetchCacheOrigins(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreGetFetchCacheOriginsFunction callback)
610 {
611     WebKit::toImpl(dataStoreRef)->fetchData(WebKit::WebsiteDataType::DOMCache, { }, [context, callback] (auto dataRecords) {
612         Vector<RefPtr<API::Object>> securityOrigins;
613         for (const auto& dataRecord : dataRecords) {
614             for (const auto& origin : dataRecord.origins)
615                 securityOrigins.append(API::SecurityOrigin::create(origin.securityOrigin()));
616         }
617         callback(WebKit::toAPI(API::Array::create(WTFMove(securityOrigins)).ptr()), context);
618     });
619 }
620
621 void WKWebsiteDataStoreGetFetchCacheSizeForOrigin(WKWebsiteDataStoreRef dataStoreRef, WKStringRef origin, void* context, WKWebsiteDataStoreGetFetchCacheSizeForOriginFunction callback)
622 {
623     OptionSet<WebKit::WebsiteDataFetchOption> fetchOptions = WebKit::WebsiteDataFetchOption::ComputeSizes;
624
625     WebKit::toImpl(dataStoreRef)->fetchData(WebKit::WebsiteDataType::DOMCache, fetchOptions, [origin, context, callback] (auto dataRecords) {
626         auto originData = WebCore::SecurityOrigin::createFromString(WebKit::toImpl(origin)->string())->data();
627         for (auto& dataRecord : dataRecords) {
628             for (const auto& recordOrigin : dataRecord.origins) {
629                 if (originData == recordOrigin) {
630                     callback(dataRecord.size ? dataRecord.size->totalSize : 0, context);
631                     return;
632                 }
633
634             }
635         }
636         callback(0, context);
637     });
638 }
639
640 WKStringRef WKWebsiteDataStoreCopyServiceWorkerRegistrationDirectory(WKWebsiteDataStoreRef dataStoreRef)
641 {
642     return WebKit::toCopiedAPI(WebKit::toImpl(dataStoreRef)->serviceWorkerRegistrationDirectory());
643 }
644
645 void WKWebsiteDataStoreSetServiceWorkerRegistrationDirectory(WKWebsiteDataStoreRef dataStoreRef, WKStringRef serviceWorkerRegistrationDirectory)
646 {
647     WebKit::toImpl(dataStoreRef)->setServiceWorkerRegistrationDirectory(WebKit::toImpl(serviceWorkerRegistrationDirectory)->string());
648 }
649
650 void WKWebsiteDataStoreSetPerOriginStorageQuota(WKWebsiteDataStoreRef, uint64_t)
651 {
652 }
653
654 void WKWebsiteDataStoreClearAllDeviceOrientationPermissions(WKWebsiteDataStoreRef dataStoreRef)
655 {
656 #if ENABLE(DEVICE_ORIENTATION)
657     WebKit::toImpl(dataStoreRef)->deviceOrientationAndMotionAccessController().clearPermissions();
658 #endif
659 }
660
661 void WKWebsiteDataStoreSetWebAuthenticationMockConfiguration(WKWebsiteDataStoreRef dataStoreRef, WKDictionaryRef configurationRef)
662 {
663 #if ENABLE(WEB_AUTHN)
664     WebKit::MockWebAuthenticationConfiguration configuration;
665
666     if (auto silentFailureRef = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(configurationRef, adoptWK(WKStringCreateWithUTF8CString("SilentFailure")).get())))
667         configuration.silentFailure = WKBooleanGetValue(silentFailureRef);
668
669     if (auto localRef = static_cast<WKDictionaryRef>(WKDictionaryGetItemForKey(configurationRef, adoptWK(WKStringCreateWithUTF8CString("Local")).get()))) {
670         WebKit::MockWebAuthenticationConfiguration::Local local;
671         local.acceptAuthentication = WKBooleanGetValue(static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(localRef, adoptWK(WKStringCreateWithUTF8CString("AcceptAuthentication")).get())));
672         local.acceptAttestation = WKBooleanGetValue(static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(localRef, adoptWK(WKStringCreateWithUTF8CString("AcceptAttestation")).get())));
673         if (local.acceptAttestation) {
674             local.privateKeyBase64 = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(localRef, adoptWK(WKStringCreateWithUTF8CString("PrivateKeyBase64")).get())))->string();
675             local.userCertificateBase64 = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(localRef, adoptWK(WKStringCreateWithUTF8CString("UserCertificateBase64")).get())))->string();
676             local.intermediateCACertificateBase64 = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(localRef, adoptWK(WKStringCreateWithUTF8CString("IntermediateCACertificateBase64")).get())))->string();
677         }
678         if (auto preferredUserhandleBase64Ref = static_cast<WKStringRef>(WKDictionaryGetItemForKey(localRef, adoptWK(WKStringCreateWithUTF8CString("PreferredUserhandleBase64")).get())))
679             local.preferredUserhandleBase64 = WebKit::toImpl(preferredUserhandleBase64Ref)->string();
680         configuration.local = WTFMove(local);
681     }
682
683     if (auto hidRef = static_cast<WKDictionaryRef>(WKDictionaryGetItemForKey(configurationRef, adoptWK(WKStringCreateWithUTF8CString("Hid")).get()))) {
684         WebKit::MockWebAuthenticationConfiguration::Hid hid;
685
686         auto stage = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("Stage")).get())))->string();
687         if (stage == "info")
688             hid.stage = WebKit::MockWebAuthenticationConfiguration::Hid::Stage::Info;
689         else if (stage == "request")
690             hid.stage = WebKit::MockWebAuthenticationConfiguration::Hid::Stage::Request;
691
692         auto subStage = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("SubStage")).get())))->string();
693         if (subStage == "init")
694             hid.subStage = WebKit::MockWebAuthenticationConfiguration::Hid::SubStage::Init;
695         else if (subStage == "msg")
696             hid.subStage = WebKit::MockWebAuthenticationConfiguration::Hid::SubStage::Msg;
697
698         auto error = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("Error")).get())))->string();
699         if (error == "success")
700             hid.error = WebKit::MockWebAuthenticationConfiguration::Hid::Error::Success;
701         else if (error == "data-not-sent")
702             hid.error = WebKit::MockWebAuthenticationConfiguration::Hid::Error::DataNotSent;
703         else if (error == "empty-report")
704             hid.error = WebKit::MockWebAuthenticationConfiguration::Hid::Error::EmptyReport;
705         else if (error == "wrong-channel-id")
706             hid.error = WebKit::MockWebAuthenticationConfiguration::Hid::Error::WrongChannelId;
707         else if (error == "malicious-payload")
708             hid.error = WebKit::MockWebAuthenticationConfiguration::Hid::Error::MaliciousPayload;
709         else if (error == "unsupported-options")
710             hid.error = WebKit::MockWebAuthenticationConfiguration::Hid::Error::UnsupportedOptions;
711         else if (error == "wrong-nonce")
712             hid.error = WebKit::MockWebAuthenticationConfiguration::Hid::Error::WrongNonce;
713
714         if (auto payloadBase64 = static_cast<WKArrayRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("PayloadBase64")).get())))
715             hid.payloadBase64 = WebKit::toImpl(payloadBase64)->toStringVector();
716
717         if (auto isU2f = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("IsU2f")).get())))
718             hid.isU2f = WKBooleanGetValue(isU2f);
719
720         if (auto keepAlive = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("KeepAlive")).get())))
721             hid.keepAlive = WKBooleanGetValue(keepAlive);
722
723         if (auto fastDataArrival = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("FastDataArrival")).get())))
724             hid.fastDataArrival = WKBooleanGetValue(fastDataArrival);
725
726         if (auto continueAfterErrorData = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("ContinueAfterErrorData")).get())))
727             hid.continueAfterErrorData = WKBooleanGetValue(continueAfterErrorData);
728
729         if (auto canDowngrade = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(hidRef, adoptWK(WKStringCreateWithUTF8CString("CanDowngrade")).get())))
730             hid.canDowngrade = WKBooleanGetValue(canDowngrade);
731
732         configuration.hid = WTFMove(hid);
733     }
734
735     if (auto nfcRef = static_cast<WKDictionaryRef>(WKDictionaryGetItemForKey(configurationRef, adoptWK(WKStringCreateWithUTF8CString("Nfc")).get()))) {
736         WebKit::MockWebAuthenticationConfiguration::Nfc nfc;
737
738         auto error = WebKit::toImpl(static_cast<WKStringRef>(WKDictionaryGetItemForKey(nfcRef, adoptWK(WKStringCreateWithUTF8CString("Error")).get())))->string();
739         if (error == "success")
740             nfc.error = WebKit::MockWebAuthenticationConfiguration::Nfc::Error::Success;
741         else if (error == "no-tags")
742             nfc.error = WebKit::MockWebAuthenticationConfiguration::Nfc::Error::NoTags;
743         else if (error == "wrong-tag-type")
744             nfc.error = WebKit::MockWebAuthenticationConfiguration::Nfc::Error::WrongTagType;
745         else if (error == "no-connections")
746             nfc.error = WebKit::MockWebAuthenticationConfiguration::Nfc::Error::NoConnections;
747         else if (error == "malicious-payload")
748             nfc.error = WebKit::MockWebAuthenticationConfiguration::Nfc::Error::MaliciousPayload;
749
750         if (auto payloadBase64 = static_cast<WKArrayRef>(WKDictionaryGetItemForKey(nfcRef, adoptWK(WKStringCreateWithUTF8CString("PayloadBase64")).get())))
751             nfc.payloadBase64 = WebKit::toImpl(payloadBase64)->toStringVector();
752
753         if (auto multipleTags = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(nfcRef, adoptWK(WKStringCreateWithUTF8CString("MultipleTags")).get())))
754             nfc.multipleTags = WKBooleanGetValue(multipleTags);
755
756         configuration.nfc = WTFMove(nfc);
757     }
758
759     WebKit::toImpl(dataStoreRef)->setMockWebAuthenticationConfiguration(WTFMove(configuration));
760 #endif
761 }
762
763 void WKWebsiteDataStoreClearAdClickAttributionsThroughWebsiteDataRemoval(WKWebsiteDataStoreRef dataStoreRef, void* context, WKWebsiteDataStoreClearAdClickAttributionsThroughWebsiteDataRemovalFunction callback)
764 {
765     OptionSet<WebKit::WebsiteDataType> dataTypes = WebKit::WebsiteDataType::AdClickAttributions;
766     WebKit::toImpl(dataStoreRef)->removeData(dataTypes, WallTime::fromRawSeconds(0), [context, callback] {
767         callback(context);
768     });
769 }
770