Remove std::chrono completely
[WebKit.git] / Source / WebKit / NetworkProcess / cache / NetworkCache.cpp
1 /*
2  * Copyright (C) 2014-2017 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 "NetworkCache.h"
28
29 #include "Logging.h"
30 #include "NetworkCacheSpeculativeLoadManager.h"
31 #include "NetworkCacheStatistics.h"
32 #include "NetworkCacheStorage.h"
33 #include <WebCore/CacheValidation.h>
34 #include <WebCore/FileSystem.h>
35 #include <WebCore/HTTPHeaderNames.h>
36 #include <WebCore/LowPowerModeNotifier.h>
37 #include <WebCore/NetworkStorageSession.h>
38 #include <WebCore/PlatformCookieJar.h>
39 #include <WebCore/ResourceRequest.h>
40 #include <WebCore/ResourceResponse.h>
41 #include <WebCore/SharedBuffer.h>
42 #include <wtf/MainThread.h>
43 #include <wtf/NeverDestroyed.h>
44 #include <wtf/RunLoop.h>
45 #include <wtf/text/StringBuilder.h>
46
47 #if PLATFORM(COCOA)
48 #include <notify.h>
49 #endif
50
51 using namespace WebCore::FileSystem;
52
53 namespace WebKit {
54 namespace NetworkCache {
55
56 static const AtomicString& resourceType()
57 {
58     ASSERT(WTF::RunLoop::isMain());
59     static NeverDestroyed<const AtomicString> resource("Resource", AtomicString::ConstructFromLiteral);
60     return resource;
61 }
62
63 RefPtr<Cache> Cache::open(const String& cachePath, OptionSet<Option> options)
64 {
65     auto storage = Storage::open(cachePath, options.contains(Option::TestingMode) ? Storage::Mode::Testing : Storage::Mode::Normal);
66
67     LOG(NetworkCache, "(NetworkProcess) opened cache storage, success %d", !!storage);
68
69     if (!storage)
70         return nullptr;
71
72     return adoptRef(*new Cache(storage.releaseNonNull(), options));
73 }
74
75 #if PLATFORM(GTK)
76 static void dumpFileChanged(Cache* cache)
77 {
78     cache->dumpContentsToFile();
79 }
80 #endif
81
82 Cache::Cache(Ref<Storage>&& storage, OptionSet<Option> options)
83     : m_storage(WTFMove(storage))
84 {
85 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
86     if (options.contains(Option::SpeculativeRevalidation)) {
87         m_lowPowerModeNotifier = std::make_unique<WebCore::LowPowerModeNotifier>([this](bool isLowPowerModeEnabled) {
88             ASSERT(WTF::RunLoop::isMain());
89             if (isLowPowerModeEnabled)
90                 m_speculativeLoadManager = nullptr;
91             else {
92                 ASSERT(!m_speculativeLoadManager);
93                 m_speculativeLoadManager = std::make_unique<SpeculativeLoadManager>(*this, m_storage.get());
94             }
95         });
96         if (!m_lowPowerModeNotifier->isLowPowerModeEnabled())
97             m_speculativeLoadManager = std::make_unique<SpeculativeLoadManager>(*this, m_storage.get());
98     }
99 #endif
100
101     if (options.contains(Option::EfficacyLogging))
102         m_statistics = Statistics::open(*this, m_storage->basePath());
103
104     if (options.contains(Option::RegisterNotify)) {
105 #if PLATFORM(COCOA)
106         // Triggers with "notifyutil -p com.apple.WebKit.Cache.dump".
107         int token;
108         notify_register_dispatch("com.apple.WebKit.Cache.dump", &token, dispatch_get_main_queue(), ^(int) {
109             dumpContentsToFile();
110         });
111 #endif
112 #if PLATFORM(GTK)
113         // Triggers with "touch $cachePath/dump".
114         CString dumpFilePath = fileSystemRepresentation(pathByAppendingComponent(m_storage->basePath(), "dump"));
115         GRefPtr<GFile> dumpFile = adoptGRef(g_file_new_for_path(dumpFilePath.data()));
116         GFileMonitor* monitor = g_file_monitor_file(dumpFile.get(), G_FILE_MONITOR_NONE, nullptr, nullptr);
117         g_signal_connect_swapped(monitor, "changed", G_CALLBACK(dumpFileChanged), this);
118 #endif
119     }
120 }
121
122 Cache::~Cache()
123 {
124 }
125
126 void Cache::setCapacity(size_t maximumSize)
127 {
128     m_storage->setCapacity(maximumSize);
129 }
130
131 Key Cache::makeCacheKey(const WebCore::ResourceRequest& request)
132 {
133     // FIXME: This implements minimal Range header disk cache support. We don't parse
134     // ranges so only the same exact range request will be served from the cache.
135     String range = request.httpHeaderField(WebCore::HTTPHeaderName::Range);
136     return { request.cachePartition(), resourceType(), range, request.url().string(), m_storage->salt() };
137 }
138
139 static bool cachePolicyAllowsExpired(WebCore::ResourceRequestCachePolicy policy)
140 {
141     switch (policy) {
142     case WebCore::ReturnCacheDataElseLoad:
143     case WebCore::ReturnCacheDataDontLoad:
144         return true;
145     case WebCore::UseProtocolCachePolicy:
146     case WebCore::ReloadIgnoringCacheData:
147     case WebCore::RefreshAnyCacheData:
148         return false;
149     case WebCore::DoNotUseAnyCache:
150         ASSERT_NOT_REACHED();
151         return false;
152     }
153     return false;
154 }
155
156 static bool responseHasExpired(const WebCore::ResourceResponse& response, WallTime timestamp, std::optional<Seconds> maxStale)
157 {
158     if (response.cacheControlContainsNoCache())
159         return true;
160
161     auto age = WebCore::computeCurrentAge(response, timestamp);
162     auto lifetime = WebCore::computeFreshnessLifetimeForHTTPFamily(response, timestamp);
163
164     auto maximumStaleness = maxStale ? maxStale.value() : 0_ms;
165     bool hasExpired = age - lifetime > maximumStaleness;
166
167 #ifndef LOG_DISABLED
168     if (hasExpired)
169         LOG(NetworkCache, "(NetworkProcess) needsRevalidation hasExpired age=%f lifetime=%f max-stale=%g", age, lifetime, maxStale);
170 #endif
171
172     return hasExpired;
173 }
174
175 static bool responseNeedsRevalidation(const WebCore::ResourceResponse& response, const WebCore::ResourceRequest& request, WallTime timestamp)
176 {
177     auto requestDirectives = WebCore::parseCacheControlDirectives(request.httpHeaderFields());
178     if (requestDirectives.noCache)
179         return true;
180     // For requests we ignore max-age values other than zero.
181     if (requestDirectives.maxAge && requestDirectives.maxAge.value() == 0_ms)
182         return true;
183
184     return responseHasExpired(response, timestamp, requestDirectives.maxStale);
185 }
186
187 static UseDecision makeUseDecision(const Entry& entry, const WebCore::ResourceRequest& request)
188 {
189     // The request is conditional so we force revalidation from the network. We merely check the disk cache
190     // so we can update the cache entry.
191     if (request.isConditional() && !entry.redirectRequest())
192         return UseDecision::Validate;
193
194     if (!WebCore::verifyVaryingRequestHeaders(entry.varyingRequestHeaders(), request))
195         return UseDecision::NoDueToVaryingHeaderMismatch;
196
197     // We never revalidate in the case of a history navigation.
198     if (cachePolicyAllowsExpired(request.cachePolicy()))
199         return UseDecision::Use;
200
201     if (!responseNeedsRevalidation(entry.response(), request, entry.timeStamp()))
202         return UseDecision::Use;
203
204     if (!entry.response().hasCacheValidatorFields())
205         return UseDecision::NoDueToMissingValidatorFields;
206
207     return entry.redirectRequest() ? UseDecision::NoDueToExpiredRedirect : UseDecision::Validate;
208 }
209
210 static RetrieveDecision makeRetrieveDecision(const WebCore::ResourceRequest& request)
211 {
212     ASSERT(request.cachePolicy() != WebCore::DoNotUseAnyCache);
213
214     // FIXME: Support HEAD requests.
215     if (request.httpMethod() != "GET")
216         return RetrieveDecision::NoDueToHTTPMethod;
217     if (request.cachePolicy() == WebCore::ReloadIgnoringCacheData && !request.isConditional())
218         return RetrieveDecision::NoDueToReloadIgnoringCache;
219
220     return RetrieveDecision::Yes;
221 }
222
223 static bool isMediaMIMEType(const String& type)
224 {
225     return startsWithLettersIgnoringASCIICase(type, "video/") || startsWithLettersIgnoringASCIICase(type, "audio/");
226 }
227
228 static std::optional<size_t> expectedTotalResourceSizeFromContentRange(const WebCore::ResourceResponse& response)
229 {
230     ASSERT(response.httpStatusCode() == 206);
231
232     auto contentRange = response.httpHeaderField(WebCore::HTTPHeaderName::ContentRange);
233     if (contentRange.isNull())
234         return { };
235
236     if (!contentRange.startsWith("bytes "))
237         return { };
238
239     auto slashPosition = contentRange.find('/');
240     if (slashPosition == notFound)
241         return { };
242
243     auto sizeStringLength = contentRange.length() - slashPosition - 1;
244     if (!sizeStringLength)
245         return { };
246
247     bool isValid;
248     auto size = StringView(contentRange).right(sizeStringLength).toIntStrict(isValid);
249     if (!isValid)
250         return { };
251
252     return size;
253 }
254
255 static StoreDecision makeStoreDecision(const WebCore::ResourceRequest& originalRequest, const WebCore::ResourceResponse& response, size_t bodySize)
256 {
257     if (!originalRequest.url().protocolIsInHTTPFamily() || !response.isHTTP())
258         return StoreDecision::NoDueToProtocol;
259
260     if (originalRequest.httpMethod() != "GET")
261         return StoreDecision::NoDueToHTTPMethod;
262
263     auto requestDirectives = WebCore::parseCacheControlDirectives(originalRequest.httpHeaderFields());
264     if (requestDirectives.noStore)
265         return StoreDecision::NoDueToNoStoreRequest;
266
267     if (response.cacheControlContainsNoStore())
268         return StoreDecision::NoDueToNoStoreResponse;
269
270     if (!WebCore::isStatusCodeCacheableByDefault(response.httpStatusCode())) {
271         // http://tools.ietf.org/html/rfc7234#section-4.3.2
272         bool hasExpirationHeaders = response.expires() || response.cacheControlMaxAge();
273         bool expirationHeadersAllowCaching = WebCore::isStatusCodePotentiallyCacheable(response.httpStatusCode()) && hasExpirationHeaders;
274         if (!expirationHeadersAllowCaching)
275             return StoreDecision::NoDueToHTTPStatusCode;
276     }
277
278     bool isMainResource = originalRequest.requester() == WebCore::ResourceRequest::Requester::Main;
279     bool storeUnconditionallyForHistoryNavigation = isMainResource || originalRequest.priority() == WebCore::ResourceLoadPriority::VeryHigh;
280     if (!storeUnconditionallyForHistoryNavigation) {
281         auto now = WallTime::now();
282         bool hasNonZeroLifetime = !response.cacheControlContainsNoCache() && WebCore::computeFreshnessLifetimeForHTTPFamily(response, now) > 0_ms;
283
284         bool possiblyReusable = response.hasCacheValidatorFields() || hasNonZeroLifetime;
285         if (!possiblyReusable)
286             return StoreDecision::NoDueToUnlikelyToReuse;
287     }
288
289     // Streaming media fills the cache quickly and is unlikely to be reused.
290     // FIXME: We should introduce a separate media cache partition that doesn't affect other resources.
291     // FIXME: We should also make sure make the MSE paths are copy-free so we can use mapped buffers from disk effectively.
292     auto requester = originalRequest.requester();
293     bool isDefinitelyMedia = requester == WebCore::ResourceRequest::Requester::Media;
294     if (isDefinitelyMedia) {
295         // Allow caching of smaller media files if we know the total size.
296         const size_t maximumCacheableMediaSize = 5 * 1024 * 1024;
297         auto totalSize = response.httpStatusCode() == 206 ? expectedTotalResourceSizeFromContentRange(response) : bodySize;
298         if (!totalSize || *totalSize > maximumCacheableMediaSize)
299             return StoreDecision::NoDueToStreamingMedia;
300     }
301
302     bool isLikelyStreamingMedia = requester == WebCore::ResourceRequest::Requester::XHR && isMediaMIMEType(response.mimeType());
303     if (isLikelyStreamingMedia) {
304         // Media loaded via XHR is likely being used for MSE streaming (YouTube and Netflix for example).
305         // We have no way of knowing the total media size so disallow caching.
306         return StoreDecision::NoDueToStreamingMedia;
307     }
308
309     return StoreDecision::Yes;
310 }
311
312 void Cache::retrieve(const WebCore::ResourceRequest& request, const GlobalFrameID& frameID, Function<void (std::unique_ptr<Entry>)>&& completionHandler)
313 {
314     ASSERT(request.url().protocolIsInHTTPFamily());
315
316     LOG(NetworkCache, "(NetworkProcess) retrieving %s priority %d", request.url().string().ascii().data(), static_cast<int>(request.priority()));
317
318     if (m_statistics)
319         m_statistics->recordRetrievalRequest(frameID.first);
320
321     Key storageKey = makeCacheKey(request);
322
323 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
324     bool canUseSpeculativeRevalidation = m_speculativeLoadManager && !request.isConditional();
325     if (canUseSpeculativeRevalidation)
326         m_speculativeLoadManager->registerLoad(frameID, request, storageKey);
327 #endif
328
329     auto retrieveDecision = makeRetrieveDecision(request);
330     if (retrieveDecision != RetrieveDecision::Yes) {
331         if (m_statistics)
332             m_statistics->recordNotUsingCacheForRequest(frameID.first, storageKey, request, retrieveDecision);
333
334         completionHandler(nullptr);
335         return;
336     }
337
338 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
339     if (canUseSpeculativeRevalidation && m_speculativeLoadManager->canRetrieve(storageKey, request, frameID)) {
340         m_speculativeLoadManager->retrieve(storageKey, [request, completionHandler = WTFMove(completionHandler)](std::unique_ptr<Entry> entry) {
341             if (entry && WebCore::verifyVaryingRequestHeaders(entry->varyingRequestHeaders(), request))
342                 completionHandler(WTFMove(entry));
343             else
344                 completionHandler(nullptr);
345         });
346         return;
347     }
348 #endif
349
350     auto startTime = WallTime::now();
351     auto priority = static_cast<unsigned>(request.priority());
352
353     m_storage->retrieve(storageKey, priority, [this, protectedThis = makeRef(*this), request, completionHandler = WTFMove(completionHandler), startTime, storageKey, frameID](auto record) {
354         if (!record) {
355             LOG(NetworkCache, "(NetworkProcess) not found in storage");
356
357             if (m_statistics)
358                 m_statistics->recordRetrievalFailure(frameID.first, storageKey, request);
359
360             completionHandler(nullptr);
361             return false;
362         }
363
364         ASSERT(record->key == storageKey);
365
366         auto entry = Entry::decodeStorageRecord(*record);
367
368         auto useDecision = entry ? makeUseDecision(*entry, request) : UseDecision::NoDueToDecodeFailure;
369         switch (useDecision) {
370         case UseDecision::Use:
371             break;
372         case UseDecision::Validate:
373             entry->setNeedsValidation(true);
374             break;
375         default:
376             entry = nullptr;
377         };
378
379 #if !LOG_DISABLED
380         auto elapsed = WallTime::now() - startTime;
381         LOG(NetworkCache, "(NetworkProcess) retrieve complete useDecision=%d priority=%d time=%" PRIi64 "ms", static_cast<int>(useDecision), static_cast<int>(request.priority()), elapsed.millisecondsAs<int64_t>());
382 #else
383         UNUSED_PARAM(startTime);
384 #endif
385         completionHandler(WTFMove(entry));
386
387         if (m_statistics)
388             m_statistics->recordRetrievedCachedEntry(frameID.first, storageKey, request, useDecision);
389         return useDecision != UseDecision::NoDueToDecodeFailure;
390     });
391 }
392
393     
394 std::unique_ptr<Entry> Cache::makeEntry(const WebCore::ResourceRequest& request, const WebCore::ResourceResponse& response, RefPtr<WebCore::SharedBuffer>&& responseData)
395 {
396     return std::make_unique<Entry>(makeCacheKey(request), response, WTFMove(responseData), WebCore::collectVaryingRequestHeaders(request, response));
397 }
398
399 std::unique_ptr<Entry> Cache::makeRedirectEntry(const WebCore::ResourceRequest& request, const WebCore::ResourceResponse& response, const WebCore::ResourceRequest& redirectRequest)
400 {
401     return std::make_unique<Entry>(makeCacheKey(request), response, redirectRequest, WebCore::collectVaryingRequestHeaders(request, response));
402 }
403
404 std::unique_ptr<Entry> Cache::store(const WebCore::ResourceRequest& request, const WebCore::ResourceResponse& response, RefPtr<WebCore::SharedBuffer>&& responseData, Function<void (MappedBody&)>&& completionHandler)
405 {
406     ASSERT(responseData);
407
408     LOG(NetworkCache, "(NetworkProcess) storing %s, partition %s", request.url().string().latin1().data(), makeCacheKey(request).partition().latin1().data());
409
410     StoreDecision storeDecision = makeStoreDecision(request, response, responseData ? responseData->size() : 0);
411     if (storeDecision != StoreDecision::Yes) {
412         LOG(NetworkCache, "(NetworkProcess) didn't store, storeDecision=%d", static_cast<int>(storeDecision));
413         auto key = makeCacheKey(request);
414
415         auto isSuccessfulRevalidation = response.httpStatusCode() == 304;
416         if (!isSuccessfulRevalidation) {
417             // Make sure we don't keep a stale entry in the cache.
418             remove(key);
419         }
420
421         if (m_statistics)
422             m_statistics->recordNotCachingResponse(key, storeDecision);
423
424         return nullptr;
425     }
426
427     auto cacheEntry = makeEntry(request, response, WTFMove(responseData));
428     auto record = cacheEntry->encodeAsStorageRecord();
429
430     m_storage->store(record, [this, protectedThis = makeRef(*this), completionHandler = WTFMove(completionHandler)](const Data& bodyData) {
431         MappedBody mappedBody;
432 #if ENABLE(SHAREABLE_RESOURCE)
433         if (canUseSharedMemoryForBodyData()) {
434             if (auto sharedMemory = bodyData.tryCreateSharedMemory()) {
435                 mappedBody.shareableResource = ShareableResource::create(sharedMemory.releaseNonNull(), 0, bodyData.size());
436                 ASSERT(mappedBody.shareableResource);
437                 mappedBody.shareableResource->createHandle(mappedBody.shareableResourceHandle);
438             }
439         }
440 #endif
441         completionHandler(mappedBody);
442         LOG(NetworkCache, "(NetworkProcess) stored");
443     });
444
445     return cacheEntry;
446 }
447
448 std::unique_ptr<Entry> Cache::storeRedirect(const WebCore::ResourceRequest& request, const WebCore::ResourceResponse& response, const WebCore::ResourceRequest& redirectRequest)
449 {
450     LOG(NetworkCache, "(NetworkProcess) storing redirect %s -> %s", request.url().string().latin1().data(), redirectRequest.url().string().latin1().data());
451
452     StoreDecision storeDecision = makeStoreDecision(request, response, 0);
453     if (storeDecision != StoreDecision::Yes) {
454         LOG(NetworkCache, "(NetworkProcess) didn't store redirect, storeDecision=%d", static_cast<int>(storeDecision));
455         auto key = makeCacheKey(request);
456         if (m_statistics)
457             m_statistics->recordNotCachingResponse(key, storeDecision);
458
459         return nullptr;
460     }
461
462     auto cacheEntry = makeRedirectEntry(request, response, redirectRequest);
463     auto record = cacheEntry->encodeAsStorageRecord();
464
465     m_storage->store(record, nullptr);
466     
467     return cacheEntry;
468 }
469
470 std::unique_ptr<Entry> Cache::update(const WebCore::ResourceRequest& originalRequest, const GlobalFrameID& frameID, const Entry& existingEntry, const WebCore::ResourceResponse& validatingResponse)
471 {
472     LOG(NetworkCache, "(NetworkProcess) updating %s", originalRequest.url().string().latin1().data());
473
474     WebCore::ResourceResponse response = existingEntry.response();
475     WebCore::updateResponseHeadersAfterRevalidation(response, validatingResponse);
476
477     auto updateEntry = std::make_unique<Entry>(existingEntry.key(), response, existingEntry.buffer(), WebCore::collectVaryingRequestHeaders(originalRequest, response));
478     auto updateRecord = updateEntry->encodeAsStorageRecord();
479
480     m_storage->store(updateRecord, { });
481
482     if (m_statistics)
483         m_statistics->recordRevalidationSuccess(frameID.first, existingEntry.key(), originalRequest);
484
485     return updateEntry;
486 }
487
488 void Cache::remove(const Key& key)
489 {
490     m_storage->remove(key);
491 }
492
493 void Cache::remove(const WebCore::ResourceRequest& request)
494 {
495     remove(makeCacheKey(request));
496 }
497
498 void Cache::remove(const Vector<Key>& keys, Function<void ()>&& completionHandler)
499 {
500     m_storage->remove(keys, WTFMove(completionHandler));
501 }
502
503 void Cache::traverse(Function<void (const TraversalEntry*)>&& traverseHandler)
504 {
505     // Protect against clients making excessive traversal requests.
506     const unsigned maximumTraverseCount = 3;
507     if (m_traverseCount >= maximumTraverseCount) {
508         WTFLogAlways("Maximum parallel cache traverse count exceeded. Ignoring traversal request.");
509
510         RunLoop::main().dispatch([traverseHandler = WTFMove(traverseHandler)] {
511             traverseHandler(nullptr);
512         });
513         return;
514     }
515
516     ++m_traverseCount;
517
518     m_storage->traverse(resourceType(), 0, [this, protectedThis = makeRef(*this), traverseHandler = WTFMove(traverseHandler)](const Storage::Record* record, const Storage::RecordInfo& recordInfo) {
519         if (!record) {
520             --m_traverseCount;
521             traverseHandler(nullptr);
522             return;
523         }
524
525         auto entry = Entry::decodeStorageRecord(*record);
526         if (!entry)
527             return;
528
529         TraversalEntry traversalEntry { *entry, recordInfo };
530         traverseHandler(&traversalEntry);
531     });
532 }
533
534 String Cache::dumpFilePath() const
535 {
536     return pathByAppendingComponent(m_storage->versionPath(), "dump.json");
537 }
538
539 void Cache::dumpContentsToFile()
540 {
541     auto fd = openFile(dumpFilePath(), FileOpenMode::Write);
542     if (!isHandleValid(fd))
543         return;
544     auto prologue = String("{\n\"entries\": [\n").utf8();
545     writeToFile(fd, prologue.data(), prologue.length());
546
547     struct Totals {
548         unsigned count { 0 };
549         double worth { 0 };
550         size_t bodySize { 0 };
551     };
552     Totals totals;
553     auto flags = Storage::TraverseFlag::ComputeWorth | Storage::TraverseFlag::ShareCount;
554     size_t capacity = m_storage->capacity();
555     m_storage->traverse(resourceType(), flags, [fd, totals, capacity](const Storage::Record* record, const Storage::RecordInfo& info) mutable {
556         if (!record) {
557             StringBuilder epilogue;
558             epilogue.appendLiteral("{}\n],\n");
559             epilogue.appendLiteral("\"totals\": {\n");
560             epilogue.appendLiteral("\"capacity\": ");
561             epilogue.appendNumber(capacity);
562             epilogue.appendLiteral(",\n");
563             epilogue.appendLiteral("\"count\": ");
564             epilogue.appendNumber(totals.count);
565             epilogue.appendLiteral(",\n");
566             epilogue.appendLiteral("\"bodySize\": ");
567             epilogue.appendNumber(totals.bodySize);
568             epilogue.appendLiteral(",\n");
569             epilogue.appendLiteral("\"averageWorth\": ");
570             epilogue.appendNumber(totals.count ? totals.worth / totals.count : 0);
571             epilogue.appendLiteral("\n");
572             epilogue.appendLiteral("}\n}\n");
573             auto writeData = epilogue.toString().utf8();
574             writeToFile(fd, writeData.data(), writeData.length());
575             closeFile(fd);
576             return;
577         }
578         auto entry = Entry::decodeStorageRecord(*record);
579         if (!entry)
580             return;
581         ++totals.count;
582         totals.worth += info.worth;
583         totals.bodySize += info.bodySize;
584
585         StringBuilder json;
586         entry->asJSON(json, info);
587         json.appendLiteral(",\n");
588         auto writeData = json.toString().utf8();
589         writeToFile(fd, writeData.data(), writeData.length());
590     });
591 }
592
593 void Cache::deleteDumpFile()
594 {
595     WorkQueue::create("com.apple.WebKit.Cache.delete")->dispatch([path = dumpFilePath().isolatedCopy()] {
596         deleteFile(path);
597     });
598 }
599
600 void Cache::clear(WallTime modifiedSince, Function<void ()>&& completionHandler)
601 {
602     LOG(NetworkCache, "(NetworkProcess) clearing cache");
603
604     if (m_statistics)
605         m_statistics->clear();
606
607     String anyType;
608     m_storage->clear(anyType, modifiedSince, WTFMove(completionHandler));
609
610     deleteDumpFile();
611 }
612
613 void Cache::clear()
614 {
615     clear(-WallTime::infinity(), nullptr);
616 }
617
618 String Cache::recordsPath() const
619 {
620     return m_storage->recordsPath();
621 }
622
623 void Cache::retrieveData(const DataKey& dataKey, Function<void (const uint8_t* data, size_t size)> completionHandler)
624 {
625     Key key { dataKey, m_storage->salt() };
626     m_storage->retrieve(key, 4, [completionHandler = WTFMove(completionHandler)] (auto record) {
627         if (!record || !record->body.size()) {
628             completionHandler(nullptr, 0);
629             return true;
630         }
631         completionHandler(record->body.data(), record->body.size());
632         return true;
633     });
634 }
635
636 void Cache::storeData(const DataKey& dataKey, const uint8_t* data, size_t size)
637 {
638     Key key { dataKey, m_storage->salt() };
639     Storage::Record record { key, WallTime::now(), { }, Data { data, size }, { } };
640     m_storage->store(record, { });
641 }
642
643 }
644 }