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