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