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