Changeset 180949 in webkit


Ignore:
Timestamp:
Mar 3, 2015 12:13:01 PM (9 years ago)
Author:
Antti Koivisto
Message:

Include key to NetworkCacheStorage::Entry
https://bugs.webkit.org/show_bug.cgi?id=142215

Reviewed by Chris Dumez.

This simplified code. The key is saved as part of the entry so it makes logical sense too.

  • NetworkProcess/cache/NetworkCache.cpp:

(WebKit::makeCacheKey):
(WebKit::encodeStorageEntry):
(WebKit::NetworkCache::retrieve):
(WebKit::NetworkCache::store):
(WebKit::NetworkCache::update):
(WebKit::NetworkCache::traverse):
(WebKit::entryAsJSON):
(WebKit::NetworkCache::dumpContentsToFile):

  • NetworkProcess/cache/NetworkCacheKey.cpp:

(WebKit::NetworkCacheKey::operator=):

  • NetworkProcess/cache/NetworkCacheKey.h:

(WebKit::NetworkCacheKey::isNull):

  • NetworkProcess/cache/NetworkCacheStorage.h:
  • NetworkProcess/cache/NetworkCacheStorageCocoa.mm:

(WebKit::decodeEntry):
(WebKit::encodeEntryHeader):
(WebKit::retrieveFromMemory):
(WebKit::NetworkCacheStorage::retrieve):
(WebKit::NetworkCacheStorage::store):
(WebKit::NetworkCacheStorage::update):
(WebKit::NetworkCacheStorage::traverse):
(WebKit::NetworkCacheStorage::dispatchPendingWriteOperations):
(WebKit::NetworkCacheStorage::dispatchFullWriteOperation):
(WebKit::NetworkCacheStorage::dispatchHeaderWriteOperation):

Location:
trunk/Source/WebKit2
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r180947 r180949  
     12015-03-03  Antti Koivisto  <antti@apple.com>
     2
     3        Include key to NetworkCacheStorage::Entry
     4        https://bugs.webkit.org/show_bug.cgi?id=142215
     5
     6        Reviewed by Chris Dumez.
     7
     8        This simplified code. The key is saved as part of the entry so it makes logical sense too.
     9
     10        * NetworkProcess/cache/NetworkCache.cpp:
     11        (WebKit::makeCacheKey):
     12        (WebKit::encodeStorageEntry):
     13        (WebKit::NetworkCache::retrieve):
     14        (WebKit::NetworkCache::store):
     15        (WebKit::NetworkCache::update):
     16        (WebKit::NetworkCache::traverse):
     17        (WebKit::entryAsJSON):
     18        (WebKit::NetworkCache::dumpContentsToFile):
     19        * NetworkProcess/cache/NetworkCacheKey.cpp:
     20        (WebKit::NetworkCacheKey::operator=):
     21        * NetworkProcess/cache/NetworkCacheKey.h:
     22        (WebKit::NetworkCacheKey::isNull):
     23        * NetworkProcess/cache/NetworkCacheStorage.h:
     24        * NetworkProcess/cache/NetworkCacheStorageCocoa.mm:
     25        (WebKit::decodeEntry):
     26        (WebKit::encodeEntryHeader):
     27        (WebKit::retrieveFromMemory):
     28        (WebKit::NetworkCacheStorage::retrieve):
     29        (WebKit::NetworkCacheStorage::store):
     30        (WebKit::NetworkCacheStorage::update):
     31        (WebKit::NetworkCacheStorage::traverse):
     32        (WebKit::NetworkCacheStorage::dispatchPendingWriteOperations):
     33        (WebKit::NetworkCacheStorage::dispatchFullWriteOperation):
     34        (WebKit::NetworkCacheStorage::dispatchHeaderWriteOperation):
     35
    1362015-03-03  Antti Koivisto  <antti@apple.com>
    237
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCache.cpp

    r180905 r180949  
    11/*
    2  * Copyright (C) 2014 Apple Inc. All rights reserved.
     2 * Copyright (C) 2014-2015 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    8585}
    8686
     87static NetworkCacheKey makeCacheKey(const WebCore::ResourceRequest& request)
     88{
     89#if ENABLE(CACHE_PARTITIONING)
     90    String partition = request.cachePartition();
     91#else
     92    String partition;
     93#endif
     94    if (partition.isEmpty())
     95        partition = ASCIILiteral("No partition");
     96    return NetworkCacheKey(request.httpMethod(), partition, request.url().string());
     97}
     98
    8799static NetworkCacheStorage::Entry encodeStorageEntry(const WebCore::ResourceRequest& request, const WebCore::ResourceResponse& response, PassRefPtr<WebCore::SharedBuffer> responseData)
    88100{
     
    114126        body = NetworkCacheStorage::Data(reinterpret_cast<const uint8_t*>(responseData->data()), responseData->size());
    115127
    116     return NetworkCacheStorage::Entry { timeStamp, header, body };
     128    return NetworkCacheStorage::Entry { makeCacheKey(request), timeStamp, header, body };
    117129}
    118130
     
    232244}
    233245
    234 static NetworkCacheKey makeCacheKey(const WebCore::ResourceRequest& request)
    235 {
    236 #if ENABLE(CACHE_PARTITIONING)
    237     String partition = request.cachePartition();
    238 #else
    239     String partition;
    240 #endif
    241     if (partition.isEmpty())
    242         partition = ASCIILiteral("No partition");
    243     return NetworkCacheKey(request.httpMethod(), partition, request.url().string());
    244 }
    245 
    246246void NetworkCache::retrieve(const WebCore::ResourceRequest& originalRequest, uint64_t webPageID, std::function<void (std::unique_ptr<Entry>)> completionHandler)
    247247{
     
    273273            return false;
    274274        }
     275        ASSERT(entry->key == storageKey);
     276
    275277        CachedEntryReuseFailure failure = CachedEntryReuseFailure::None;
    276278        auto decodedEntry = decodeStorageEntry(*entry, originalRequest, failure);
     
    330332    LOG(NetworkCache, "(NetworkProcess) storing %s, partition %s", originalRequest.url().string().latin1().data(), originalRequest.cachePartition().latin1().data());
    331333
    332     auto key = makeCacheKey(originalRequest);
    333334    StoreDecision storeDecision = canStore(originalRequest, response);
    334335    if (storeDecision != StoreDecision::Yes) {
    335336        LOG(NetworkCache, "(NetworkProcess) didn't store");
    336         if (m_statistics)
     337        if (m_statistics) {
     338            auto key = makeCacheKey(originalRequest);
    337339            m_statistics->recordNotCachingResponse(key, storeDecision);
     340        }
    338341        return;
    339342    }
     
    341344    auto storageEntry = encodeStorageEntry(originalRequest, response, WTF::move(responseData));
    342345
    343     m_storage->store(key, storageEntry, [completionHandler](bool success, const NetworkCacheStorage::Data& bodyData) {
     346    m_storage->store(storageEntry, [completionHandler](bool success, const NetworkCacheStorage::Data& bodyData) {
    344347        MappedBody mappedBody;
    345348#if ENABLE(SHAREABLE_RESOURCE)
     
    363366    WebCore::updateResponseHeadersAfterRevalidation(response, validatingResponse);
    364367
    365     auto key = makeCacheKey(originalRequest);
    366368    auto updateEntry = encodeStorageEntry(originalRequest, response, entry.buffer);
    367369
    368     m_storage->update(key, updateEntry, entry.storageEntry, [](bool success, const NetworkCacheStorage::Data&) {
     370    m_storage->update(updateEntry, entry.storageEntry, [](bool success, const NetworkCacheStorage::Data&) {
    369371        LOG(NetworkCache, "(NetworkProcess) updated, success=%d", success);
    370372    });
     
    375377    ASSERT(isEnabled());
    376378
    377     m_storage->traverse([traverseHandler](const NetworkCacheKey& key, const NetworkCacheStorage::Entry* entry) {
     379    m_storage->traverse([traverseHandler](const NetworkCacheStorage::Entry* entry) {
    378380        if (!entry) {
    379381            traverseHandler(nullptr);
     
    397399}
    398400
    399 static bool entryAsJSON(StringBuilder& json, const NetworkCacheKey& key, const NetworkCacheStorage::Entry& entry)
     401static bool entryAsJSON(StringBuilder& json, const NetworkCacheStorage::Entry& entry)
    400402{
    401403    NetworkCacheDecoder decoder(entry.header.data(), entry.header.size());
     
    405407    json.append("{\n");
    406408    json.append("\"hash\": ");
    407     JSC::appendQuotedJSONStringToBuilder(json, key.hashAsString());
     409    JSC::appendQuotedJSONStringToBuilder(json, entry.key.hashAsString());
    408410    json.append(",\n");
    409411    json.append("\"partition\": ");
    410     JSC::appendQuotedJSONStringToBuilder(json, key.partition());
     412    JSC::appendQuotedJSONStringToBuilder(json, entry.key.partition());
    411413    json.append(",\n");
    412414    json.append("\"timestamp\": ");
     
    440442        return;
    441443    WebCore::writeToFile(dumpFileHandle, "[\n", 2);
    442     m_storage->traverse([dumpFileHandle](const NetworkCacheKey& key, const NetworkCacheStorage::Entry* entry) {
     444    m_storage->traverse([dumpFileHandle](const NetworkCacheStorage::Entry* entry) {
    443445        if (!entry) {
    444446            WebCore::writeToFile(dumpFileHandle, "{}\n]\n", 5);
     
    448450        }
    449451        StringBuilder json;
    450         if (!entryAsJSON(json, key, *entry))
     452        if (!entryAsJSON(json, *entry))
    451453            return;
    452454        json.append(",\n");
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheKey.cpp

    r179782 r180949  
    5858    , m_hash(computeHash())
    5959{
     60}
     61
     62NetworkCacheKey& NetworkCacheKey::operator=(const NetworkCacheKey& other)
     63{
     64    m_method = other.m_method.isolatedCopy();
     65    m_partition = other.m_partition.isolatedCopy();
     66    m_identifier = other.m_identifier.isolatedCopy();
     67    m_hash = other.m_hash;
     68    return *this;
    6069}
    6170
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheKey.h

    r179781 r180949  
    4646    NetworkCacheKey(const String& method, const String& partition, const String& identifier);
    4747
     48    NetworkCacheKey& operator=(const NetworkCacheKey&);
     49
     50    bool isNull() const { return m_identifier.isNull(); }
     51
    4852    const String& method() const { return m_method; }
    4953    const String& partition() const { return m_partition; }
     
    6771private:
    6872    HashType computeHash() const;
    69     void operator=(const NetworkCacheKey&) = delete;
    7073
    7174    String m_method;
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorage.h

    r180894 r180949  
    137137
    138138    struct Entry {
     139        NetworkCacheKey key;
    139140        std::chrono::milliseconds timeStamp;
    140141        Data header;
     
    146147
    147148    typedef std::function<void (bool success, const Data& mappedBody)> StoreCompletionHandler;
    148     void store(const NetworkCacheKey&, const Entry&, StoreCompletionHandler&&);
    149     void update(const NetworkCacheKey&, const Entry& updateEntry, const Entry& existingEntry, StoreCompletionHandler&&);
     149    void store(const Entry&, StoreCompletionHandler&&);
     150    void update(const Entry& updateEntry, const Entry& existingEntry, StoreCompletionHandler&&);
    150151
    151152    // Null entry signals end.
    152     void traverse(std::function<void (const NetworkCacheKey&, const Entry*)>&&);
     153    void traverse(std::function<void (const Entry*)>&&);
    153154
    154155    void setMaximumSize(size_t);
     
    177178
    178179    struct WriteOperation {
    179         NetworkCacheKey key;
    180180        Entry entry;
    181181        Optional<Entry> existingEntry;
  • trunk/Source/WebKit2/NetworkProcess/cache/NetworkCacheStorageCocoa.mm

    r180947 r180949  
    302302    }
    303303
    304     return std::make_unique<NetworkCacheStorage::Entry>(NetworkCacheStorage::Entry {
     304    return std::unique_ptr<NetworkCacheStorage::Entry>(new NetworkCacheStorage::Entry {
     305        metaData.key,
    305306        metaData.timeStamp,
    306307        headerData,
     
    326327}
    327328
    328 static DispatchPtr<dispatch_data_t> encodeEntryHeader(const NetworkCacheKey& key, const NetworkCacheStorage::Entry& entry)
    329 {
    330     EntryMetaData metaData(key);
     329static DispatchPtr<dispatch_data_t> encodeEntryHeader(const NetworkCacheStorage::Entry& entry)
     330{
     331    EntryMetaData metaData(entry.key);
    331332    metaData.timeStamp = entry.timeStamp;
    332333    metaData.headerChecksum = hashData(entry.header.dispatchData());
     
    422423{
    423424    for (auto& operation : operations) {
    424         if (operation->key == key) {
     425        if (operation->entry.key == key) {
    425426            LOG(NetworkCacheStorage, "(NetworkProcess) found write operation in progress");
    426427            auto entry = operation->entry;
     
    438439    ASSERT(RunLoop::isMain());
    439440    ASSERT(priority <= maximumRetrievePriority);
     441    ASSERT(!key.isNull());
    440442
    441443    if (!m_maximumSize) {
     
    454456        return;
    455457
    456     m_pendingReadOperationsByPriority[priority].append(std::make_unique<ReadOperation>(ReadOperation { key, WTF::move(completionHandler) }));
     458    m_pendingReadOperationsByPriority[priority].append(new ReadOperation { key, WTF::move(completionHandler) });
    457459    dispatchPendingReadOperations();
    458460}
    459461
    460 void NetworkCacheStorage::store(const NetworkCacheKey& key, const Entry& entry, StoreCompletionHandler&& completionHandler)
    461 {
    462     ASSERT(RunLoop::isMain());
     462void NetworkCacheStorage::store(const Entry& entry, StoreCompletionHandler&& completionHandler)
     463{
     464    ASSERT(RunLoop::isMain());
     465    ASSERT(!entry.key.isNull());
    463466
    464467    if (!m_maximumSize) {
     
    467470    }
    468471
    469     auto writeOperation = std::make_unique<WriteOperation>(WriteOperation { key, entry, { }, WTF::move(completionHandler) });
    470     m_pendingWriteOperations.append(WTF::move(writeOperation));
     472    m_pendingWriteOperations.append(new WriteOperation { entry, { }, WTF::move(completionHandler) });
    471473
    472474    // Add key to the filter already here as we do lookups from the pending operations too.
    473     m_contentsFilter.add(key.shortHash());
     475    m_contentsFilter.add(entry.key.shortHash());
    474476
    475477    dispatchPendingWriteOperations();
    476478}
    477479
    478 void NetworkCacheStorage::update(const NetworkCacheKey& key, const Entry& updateEntry, const Entry& existingEntry, StoreCompletionHandler&& completionHandler)
    479 {
    480     ASSERT(RunLoop::isMain());
     480void NetworkCacheStorage::update(const Entry& updateEntry, const Entry& existingEntry, StoreCompletionHandler&& completionHandler)
     481{
     482    ASSERT(RunLoop::isMain());
     483    ASSERT(!existingEntry.key.isNull());
     484    ASSERT(existingEntry.key == updateEntry.key);
    481485
    482486    if (!m_maximumSize) {
     
    485489    }
    486490
    487     auto writeOperation = std::make_unique<WriteOperation>(WriteOperation { key, updateEntry, existingEntry, WTF::move(completionHandler) });
    488     m_pendingWriteOperations.append(WTF::move(writeOperation));
     491    m_pendingWriteOperations.append(new WriteOperation { updateEntry, existingEntry, WTF::move(completionHandler) });
    489492
    490493    dispatchPendingWriteOperations();
    491494}
    492495
    493 void NetworkCacheStorage::traverse(std::function<void (const NetworkCacheKey&, const Entry*)>&& traverseHandler)
     496void NetworkCacheStorage::traverse(std::function<void (const Entry*)>&& traverseHandler)
    494497{
    495498    StringCapture cachePathCapture(m_directoryPath);
     
    505508                NetworkCacheStorage::Data headerData;
    506509                if (decodeEntryHeader(fileData, metaData, headerData)) {
    507                     Entry entry { metaData.timeStamp, headerData, Data() };
    508                     traverseHandler(metaData.key, &entry);
     510                    Entry entry { metaData.key, metaData.timeStamp, headerData, Data() };
     511                    traverseHandler(&entry);
    509512                }
    510513                if (done)
     
    514517        });
    515518        dispatch_async(dispatch_get_main_queue(), [this, traverseHandler] {
    516             traverseHandler({ }, nullptr);
     519            traverseHandler(nullptr);
    517520        });
    518521    });
     
    534537        m_activeWriteOperations.add(WTF::move(writeOperation));
    535538
    536         if (write.existingEntry && m_contentsFilter.mayContain(write.key.shortHash())) {
     539        if (write.existingEntry && m_contentsFilter.mayContain(write.entry.key.shortHash())) {
    537540            dispatchHeaderWriteOperation(write);
    538541            continue;
     
    547550    ASSERT(m_activeWriteOperations.contains(&write));
    548551
    549     if (!m_contentsFilter.mayContain(write.key.shortHash()))
    550         m_contentsFilter.add(write.key.shortHash());
     552    if (!m_contentsFilter.mayContain(write.entry.key.shortHash()))
     553        m_contentsFilter.add(write.entry.key.shortHash());
    551554
    552555    StringCapture cachePathCapture(m_directoryPath);
    553556    dispatch_async(m_backgroundIOQueue.get(), [this, &write, cachePathCapture] {
    554         auto encodedHeader = encodeEntryHeader(write.key, write.entry);
     557        auto encodedHeader = encodeEntryHeader(write.entry);
    555558        auto writeData = adoptDispatch(dispatch_data_create_concat(encodedHeader.get(), write.entry.body.dispatchData()));
    556559
     
    558561
    559562        int fd;
    560         auto channel = openFileForKey(write.key, FileOpenType::Create, cachePathCapture.string(), fd);
     563        auto channel = openFileForKey(write.entry.key, FileOpenType::Create, cachePathCapture.string(), fd);
    561564        dispatch_io_write(channel.get(), 0, writeData.get(), dispatch_get_main_queue(), [this, &write, fd, bodyOffset](bool done, dispatch_data_t, int error) {
    562565            ASSERT_UNUSED(done, done);
    563566            LOG(NetworkCacheStorage, "(NetworkProcess) write complete error=%d", error);
    564567            if (error) {
    565                 if (m_contentsFilter.mayContain(write.key.shortHash()))
    566                     m_contentsFilter.remove(write.key.shortHash());
     568                if (m_contentsFilter.mayContain(write.entry.key.shortHash()))
     569                    m_contentsFilter.remove(write.entry.key.shortHash());
    567570            }
    568571            size_t bodySize = write.entry.body.size();
     
    591594    ASSERT(write.existingEntry);
    592595    ASSERT(m_activeWriteOperations.contains(&write));
    593     ASSERT(m_contentsFilter.mayContain(write.key.shortHash()));
     596    ASSERT(m_contentsFilter.mayContain(write.entry.key.shortHash()));
    594597
    595598    // Try to update the header of an existing entry.
    596599    StringCapture cachePathCapture(m_directoryPath);
    597600    dispatch_async(m_backgroundIOQueue.get(), [this, &write, cachePathCapture] {
    598         auto headerData = encodeEntryHeader(write.key, write.entry);
    599         auto existingHeaderData = encodeEntryHeader(write.key, write.existingEntry.value());
     601        auto headerData = encodeEntryHeader(write.entry);
     602        auto existingHeaderData = encodeEntryHeader(write.existingEntry.value());
    600603
    601604        bool pageRoundedHeaderSizeChanged = dispatch_data_get_size(headerData.get()) != dispatch_data_get_size(existingHeaderData.get());
     
    609612
    610613        int fd;
    611         auto channel = openFileForKey(write.key, FileOpenType::Write, cachePathCapture.string(), fd);
     614        auto channel = openFileForKey(write.entry.key, FileOpenType::Write, cachePathCapture.string(), fd);
    612615        dispatch_io_write(channel.get(), 0, headerData.get(), dispatch_get_main_queue(), [this, &write](bool done, dispatch_data_t, int error) {
    613616            ASSERT_UNUSED(done, done);
     
    615618
    616619            if (error)
    617                 removeEntry(write.key);
     620                removeEntry(write.entry.key);
    618621
    619622            write.completionHandler(!error, Data());
Note: See TracChangeset for help on using the changeset viewer.