Changeset 252381 in webkit


Ignore:
Timestamp:
Nov 12, 2019 5:24:28 PM (4 years ago)
Author:
sihui_liu@apple.com
Message:

Add size file for CacheStorage
https://bugs.webkit.org/show_bug.cgi?id=204027
<rdar://problem/57100861>

Reviewed by Youenn Fablet.

Keep the CacheStorage size in a separate file so that we can get that value without waiting for Engine to
initialize and read caches from disk.

No behavior change as the file is not in use now.

  • NetworkProcess/cache/CacheStorageEngine.cpp:

(WebKit::CacheStorage::Engine::writeSizeFile):
(WebKit::CacheStorage::Engine::readSizeFile):
(WebKit::CacheStorage::Engine::clearAllCachesFromDisk):
(WebKit::CacheStorage::Engine::deleteDirectoryRecursivelyOnBackgroundThread):

  • NetworkProcess/cache/CacheStorageEngine.h:
  • NetworkProcess/cache/CacheStorageEngineCaches.cpp:

(WebKit::CacheStorage::Caches::cachesSizeFilename):
(WebKit::CacheStorage::Caches::updateSizeFile):
(WebKit::CacheStorage::Caches::initializeSize):
(WebKit::CacheStorage::Caches::writeRecord):
(WebKit::CacheStorage::Caches::removeRecord):
(WebKit::CacheStorage::Caches::resetSpaceUsed):

  • NetworkProcess/cache/CacheStorageEngineCaches.h:
Location:
trunk/Source/WebKit
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r252377 r252381  
     12019-11-12  Sihui Liu  <sihui_liu@apple.com>
     2
     3        Add size file for CacheStorage
     4        https://bugs.webkit.org/show_bug.cgi?id=204027
     5        <rdar://problem/57100861>
     6
     7        Reviewed by Youenn Fablet.
     8
     9        Keep the CacheStorage size in a separate file so that we can get that value without waiting for Engine to
     10        initialize and read caches from disk.
     11
     12        No behavior change as the file is not in use now.
     13
     14        * NetworkProcess/cache/CacheStorageEngine.cpp:
     15        (WebKit::CacheStorage::Engine::writeSizeFile):
     16        (WebKit::CacheStorage::Engine::readSizeFile):
     17        (WebKit::CacheStorage::Engine::clearAllCachesFromDisk):
     18        (WebKit::CacheStorage::Engine::deleteDirectoryRecursivelyOnBackgroundThread):
     19        * NetworkProcess/cache/CacheStorageEngine.h:
     20        * NetworkProcess/cache/CacheStorageEngineCaches.cpp:
     21        (WebKit::CacheStorage::Caches::cachesSizeFilename):
     22        (WebKit::CacheStorage::Caches::updateSizeFile):
     23        (WebKit::CacheStorage::Caches::initializeSize):
     24        (WebKit::CacheStorage::Caches::writeRecord):
     25        (WebKit::CacheStorage::Caches::removeRecord):
     26        (WebKit::CacheStorage::Caches::resetSpaceUsed):
     27        * NetworkProcess/cache/CacheStorageEngineCaches.h:
     28
    1292019-11-12  Wenson Hsieh  <wenson_hsieh@apple.com>
    230
  • trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngine.cpp

    r252369 r252381  
    3636#include <wtf/CallbackAggregator.h>
    3737#include <wtf/NeverDestroyed.h>
     38#include <wtf/Scope.h>
    3839#include <wtf/text/StringBuilder.h>
    3940#include <wtf/text/StringHash.h>
     
    4546using namespace WebCore::DOMCacheEngine;
    4647using namespace NetworkCache;
     48
     49static Lock globalSizeFileLock;
    4750
    4851String Engine::cachesRootPath(const WebCore::ClientOrigin& origin)
     
    462465}
    463466
     467void Engine::writeSizeFile(const String& path, uint64_t size)
     468{
     469    if (!shouldPersist())
     470        return;
     471
     472    m_ioQueue->dispatch([path = path.isolatedCopy(), size]() {
     473        LockHolder locker(globalSizeFileLock);
     474        auto fileHandle = FileSystem::openFile(path, FileSystem::FileOpenMode::Write);
     475        auto closeFileHandler = makeScopeExit([&] {
     476            FileSystem::closeFile(fileHandle);
     477        });
     478        if (!FileSystem::isHandleValid(fileHandle))
     479            return;
     480
     481        FileSystem::truncateFile(fileHandle, 0);
     482        FileSystem::writeToFile(fileHandle, String::number(size).utf8().data(), String::number(size).utf8().length());
     483    });
     484}
     485
     486Optional<uint64_t> Engine::readSizeFile(const String& path)
     487{
     488    ASSERT(!RunLoop::isMain());
     489
     490    LockHolder locker(globalSizeFileLock);
     491    auto fileHandle = FileSystem::openFile(path, FileSystem::FileOpenMode::Read);
     492    auto closeFileHandle = makeScopeExit([&] {
     493        FileSystem::closeFile(fileHandle);
     494    });
     495
     496    if (!FileSystem::isHandleValid(fileHandle))
     497        return WTF::nullopt;
     498
     499    long long fileSize = 0;
     500    if (!FileSystem::getFileSize(path, fileSize) || !fileSize)
     501        return WTF::nullopt;
     502
     503    size_t bytesToRead;
     504    if (!WTF::convertSafely(fileSize, bytesToRead))
     505        return WTF::nullopt;
     506
     507    Vector<char> buffer(bytesToRead);
     508    size_t totalBytesRead = FileSystem::readFromFile(fileHandle, buffer.data(), buffer.size());
     509    if (totalBytesRead != bytesToRead)
     510        return WTF::nullopt;
     511
     512    return String::fromUTF8(buffer.data()).toUInt64Strict();
     513}
     514
    464515class ReadOriginsTaskCounter : public RefCounted<ReadOriginsTaskCounter> {
    465516public:
     
    566617
    567618    m_ioQueue->dispatch([path = m_rootPath.isolatedCopy(), completionHandler = WTFMove(completionHandler)]() mutable {
     619        LockHolder locker(globalSizeFileLock);
    568620        for (auto& filename : FileSystem::listDirectory(path, "*")) {
    569621            if (FileSystem::fileIsDirectory(filename, FileSystem::ShouldFollowSymbolicLinks::No))
     
    624676
    625677    m_ioQueue->dispatch([path = path.isolatedCopy(), completionHandler = WTFMove(completionHandler)]() mutable {
     678        LockHolder locker(globalSizeFileLock);
    626679        deleteDirectoryRecursively(path);
    627680
  • trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngine.h

    r252369 r252381  
    8585    void readFile(const String& filename, CompletionHandler<void(const NetworkCache::Data&, int error)>&&);
    8686    void removeFile(const String& filename);
     87    void writeSizeFile(const String&, uint64_t size);
     88    static Optional<uint64_t> readSizeFile(const String&);
    8789
    8890    const String& rootPath() const { return m_rootPath; }
  • trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngineCaches.cpp

    r252369 r252381  
    5252}
    5353
     54String Caches::cachesSizeFilename(const String& cachesRootsPath)
     55{
     56    return FileSystem::pathByAppendingComponent(cachesRootsPath, "estimatedsize"_s);
     57}
     58
    5459Ref<Caches> Caches::create(Engine& engine, WebCore::ClientOrigin&& origin, String&& rootPath, WebCore::StorageQuotaManager& quotaManager)
    5560{
     
    204209}
    205210
     211void Caches::updateSizeFile()
     212{
     213    if (m_engine)
     214        m_engine->writeSizeFile(cachesSizeFilename(m_rootPath), m_size);
     215}
     216
    206217void Caches::initializeSize()
    207218{
     
    222233            }
    223234            m_size = size;
     235            updateSizeFile();
     236
    224237            m_isInitialized = true;
    225238            auto pendingCallbacks = WTFMove(m_pendingInitializationCallbacks);
     
    536549    m_size += recordInformation.size;
    537550    m_size -= previousRecordSize;
     551    updateSizeFile();
    538552
    539553    if (!shouldPersist()) {
     
    592606    ASSERT(m_size >= record.size);
    593607    m_size -= record.size;
     608    updateSizeFile();
     609
    594610    removeCacheEntry(record.key);
    595611}
     
    609625{
    610626    m_size = 0;
     627    updateSizeFile();
     628
    611629    if (m_quotaManager) {
    612630        m_quotaManager->removeUser(*this);
  • trunk/Source/WebKit/NetworkProcess/cache/CacheStorageEngineCaches.h

    r252369 r252381  
    4545class Caches final : public RefCounted<Caches>, private WebCore::StorageQuotaUser {
    4646public:
     47    static String cachesSizeFilename(const String&);
    4748    static Ref<Caches> create(Engine&, WebCore::ClientOrigin&&, String&& rootPath, WebCore::StorageQuotaManager&);
    4849    ~Caches();
     
    106107    bool hasActiveCache() const;
    107108
     109    void updateSizeFile();
     110
    108111    bool m_isInitialized { false };
    109112    Engine* m_engine { nullptr };
Note: See TracChangeset for help on using the changeset viewer.