Allow retrieving Cache Storage records for a given URL only
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 5 Sep 2017 17:52:51 +0000 (17:52 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 5 Sep 2017 17:52:51 +0000 (17:52 +0000)
https://bugs.webkit.org/show_bug.cgi?id=176202

Patch by Youenn Fablet <youenn@apple.com> on 2017-09-05
Reviewed by Alex Christensen.

Source/WebCore:

No observable change of behavior.

Adding a URL parameter to CacheStorageConnection::retrieveRecords.
If given URL is null, all records will be retrieved, which is useful for the keys method.
Other operations only need records that share the same URL (modulo query string and frag id).

* Modules/cache/Cache.cpp:
(WebCore::Cache::matchAll):
(WebCore::Cache::keys):
(WebCore::Cache::retrieveRecords):
(WebCore::Cache::queryCache):
* Modules/cache/Cache.h:
* Modules/cache/CacheStorageConnection.cpp:
(WebCore::CacheStorageConnection::retrieveRecords):
* Modules/cache/CacheStorageConnection.h:
(WebCore::CacheStorageConnection::doRetrieveRecords):
* Modules/cache/WorkerCacheStorageConnection.cpp:
(WebCore::WorkerCacheStorageConnection::doRetrieveRecords):
* Modules/cache/WorkerCacheStorageConnection.h:
* platform/URL.h:

Source/WebKit:

Update CacheStorageEngineCache to store records as a map URL -> Vector of Records.
This allows regular record retrieval to be faster.
Retrieval of all records is less efficient but this should happen less often.

* NetworkProcess/cache/CacheStorageEngine.cpp:
(WebKit::CacheStorage::Engine::retrieveRecords):
* NetworkProcess/cache/CacheStorageEngine.h:
* NetworkProcess/cache/CacheStorageEngineCache.cpp:
(WebKit::CacheStorage::queryCache):
(WebKit::CacheStorage::Cache::retrieveRecords const):
(WebKit::CacheStorage::computeKeyURL):
(WebKit::CacheStorage::Cache::addNewURLRecord):
(WebKit::CacheStorage::Cache::recordsFromURL):
(WebKit::CacheStorage::Cache::recordsFromURL const):
(WebKit::CacheStorage::Cache::put):
(WebKit::CacheStorage::Cache::remove):
* NetworkProcess/cache/CacheStorageEngineCache.h:
* NetworkProcess/cache/CacheStorageEngineConnection.cpp:
(WebKit::CacheStorageEngineConnection::retrieveRecords):
* NetworkProcess/cache/CacheStorageEngineConnection.h:
* NetworkProcess/cache/CacheStorageEngineConnection.messages.in:
* WebProcess/Cache/WebCacheStorageConnection.cpp:
(WebKit::WebCacheStorageConnection::doRetrieveRecords):
* WebProcess/Cache/WebCacheStorageConnection.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@221624 268f45cc-cd09-0410-ab3c-d52691b4dbfc

18 files changed:
Source/WebCore/ChangeLog
Source/WebCore/Modules/cache/CacheStorageConnection.cpp
Source/WebCore/Modules/cache/CacheStorageConnection.h
Source/WebCore/Modules/cache/DOMCache.cpp
Source/WebCore/Modules/cache/DOMCache.h
Source/WebCore/Modules/cache/WorkerCacheStorageConnection.cpp
Source/WebCore/Modules/cache/WorkerCacheStorageConnection.h
Source/WebCore/platform/URL.h
Source/WebKit/ChangeLog
Source/WebKit/NetworkProcess/cache/CacheStorageEngine.cpp
Source/WebKit/NetworkProcess/cache/CacheStorageEngine.h
Source/WebKit/NetworkProcess/cache/CacheStorageEngineCache.cpp
Source/WebKit/NetworkProcess/cache/CacheStorageEngineCache.h
Source/WebKit/NetworkProcess/cache/CacheStorageEngineConnection.cpp
Source/WebKit/NetworkProcess/cache/CacheStorageEngineConnection.h
Source/WebKit/NetworkProcess/cache/CacheStorageEngineConnection.messages.in
Source/WebKit/WebProcess/Cache/WebCacheStorageConnection.cpp
Source/WebKit/WebProcess/Cache/WebCacheStorageConnection.h

index 941f607..a7c5e72 100644 (file)
@@ -1,3 +1,31 @@
+2017-09-05  Youenn Fablet  <youenn@apple.com>
+
+        Allow retrieving Cache Storage records for a given URL only
+        https://bugs.webkit.org/show_bug.cgi?id=176202
+
+        Reviewed by Alex Christensen.
+
+        No observable change of behavior.
+
+        Adding a URL parameter to CacheStorageConnection::retrieveRecords.
+        If given URL is null, all records will be retrieved, which is useful for the keys method.
+        Other operations only need records that share the same URL (modulo query string and frag id).
+
+        * Modules/cache/Cache.cpp:
+        (WebCore::Cache::matchAll):
+        (WebCore::Cache::keys):
+        (WebCore::Cache::retrieveRecords):
+        (WebCore::Cache::queryCache):
+        * Modules/cache/Cache.h:
+        * Modules/cache/CacheStorageConnection.cpp:
+        (WebCore::CacheStorageConnection::retrieveRecords):
+        * Modules/cache/CacheStorageConnection.h:
+        (WebCore::CacheStorageConnection::doRetrieveRecords):
+        * Modules/cache/WorkerCacheStorageConnection.cpp:
+        (WebCore::WorkerCacheStorageConnection::doRetrieveRecords):
+        * Modules/cache/WorkerCacheStorageConnection.h:
+        * platform/URL.h:
+
 2017-09-05  Tim Horton  <timothy_horton@apple.com>
 
         Remove some unused bits of code and resources
index 73e62f4..c8d9d22 100644 (file)
@@ -55,12 +55,12 @@ void CacheStorageConnection::retrieveCaches(const String& origin, uint64_t updat
     doRetrieveCaches(requestIdentifier, origin, updateCounter);
 }
 
-void CacheStorageConnection::retrieveRecords(uint64_t cacheIdentifier, RecordsCallback&& callback)
+void CacheStorageConnection::retrieveRecords(uint64_t cacheIdentifier, const URL& url, RecordsCallback&& callback)
 {
     uint64_t requestIdentifier = ++m_lastRequestIdentifier;
     m_retrieveRecordsPendingRequests.add(requestIdentifier, WTFMove(callback));
 
-    doRetrieveRecords(requestIdentifier, cacheIdentifier);
+    doRetrieveRecords(requestIdentifier, cacheIdentifier, url);
 }
 
 void CacheStorageConnection::batchDeleteOperation(uint64_t cacheIdentifier, const ResourceRequest& request, CacheQueryOptions&& options, RecordIdentifiersCallback&& callback)
index 8717b90..f0451a3 100644 (file)
@@ -41,7 +41,7 @@ public:
     void remove(uint64_t cacheIdentifier, DOMCacheEngine::CacheIdentifierCallback&&);
     void retrieveCaches(const String& origin, uint64_t updateCounter, DOMCacheEngine::CacheInfosCallback&&);
 
-    void retrieveRecords(uint64_t cacheIdentifier, DOMCacheEngine::RecordsCallback&&);
+    void retrieveRecords(uint64_t cacheIdentifier, const URL&, DOMCacheEngine::RecordsCallback&&);
     void batchDeleteOperation(uint64_t cacheIdentifier, const ResourceRequest&, CacheQueryOptions&&, DOMCacheEngine::RecordIdentifiersCallback&&);
     void batchPutOperation(uint64_t cacheIdentifier, Vector<DOMCacheEngine::Record>&&, DOMCacheEngine::RecordIdentifiersCallback&&);
 
@@ -64,7 +64,7 @@ private:
     virtual void doRemove(uint64_t requestIdentifier, uint64_t /* cacheIdentifier */) { removeCompleted(requestIdentifier, makeUnexpected(DOMCacheEngine::Error::NotImplemented)); }
     virtual void doRetrieveCaches(uint64_t requestIdentifier, const String& /* origin */, uint64_t /* updateCounter */) { updateCaches(requestIdentifier, { }); }
 
-    virtual void doRetrieveRecords(uint64_t requestIdentifier, uint64_t /* cacheIdentifier */) { updateRecords(requestIdentifier, { }); }
+    virtual void doRetrieveRecords(uint64_t requestIdentifier, uint64_t /* cacheIdentifier */, const URL& /* url */) { updateRecords(requestIdentifier, { }); }
     virtual void doBatchDeleteOperation(uint64_t requestIdentifier, uint64_t /* cacheIdentifier */, const ResourceRequest&, CacheQueryOptions&&) { deleteRecordsCompleted(requestIdentifier, makeUnexpected(DOMCacheEngine::Error::NotImplemented)); }
     virtual void doBatchPutOperation(uint64_t requestIdentifier, uint64_t /* cacheIdentifier */, Vector<DOMCacheEngine::Record>&&) { putRecordsCompleted(requestIdentifier, makeUnexpected(DOMCacheEngine::Error::NotImplemented)); }
 
index 4bd7c51..acd339c 100644 (file)
@@ -109,7 +109,7 @@ void DOMCache::matchAll(std::optional<RequestInfo>&& info, CacheQueryOptions&& o
     }
 
     if (!request) {
-        retrieveRecords([this, promise = WTFMove(promise)](std::optional<Exception>&& exception) mutable {
+        retrieveRecords(URL { }, [this, promise = WTFMove(promise)](std::optional<Exception>&& exception) mutable {
             if (exception) {
                 promise.reject(WTFMove(exception.value()));
                 return;
@@ -378,7 +378,7 @@ void DOMCache::keys(std::optional<RequestInfo>&& info, CacheQueryOptions&& optio
     }
 
     if (!request) {
-        retrieveRecords([this, promise = WTFMove(promise)](std::optional<Exception>&& exception) mutable {
+        retrieveRecords(URL { }, [this, promise = WTFMove(promise)](std::optional<Exception>&& exception) mutable {
             if (exception) {
                 promise.reject(WTFMove(exception.value()));
                 return;
@@ -407,10 +407,16 @@ void DOMCache::keys(std::optional<RequestInfo>&& info, CacheQueryOptions&& optio
     });
 }
 
-void DOMCache::retrieveRecords(WTF::Function<void(std::optional<Exception>&&)>&& callback)
+void DOMCache::retrieveRecords(const URL& url, WTF::Function<void(std::optional<Exception>&&)>&& callback)
 {
     setPendingActivity(this);
-    m_connection->retrieveRecords(m_identifier, [this, callback = WTFMove(callback)](RecordsOrError&& result) {
+
+    URL retrieveURL = url;
+    if (retrieveURL.hasQuery())
+        retrieveURL.setQuery({ });
+    retrieveURL.removeFragmentIdentifier();
+
+    m_connection->retrieveRecords(m_identifier, retrieveURL, [this, callback = WTFMove(callback)](RecordsOrError&& result) {
         if (!m_isStopped) {
             if (!result.hasValue()) {
                 callback(DOMCacheEngine::errorToException(result.error()));
@@ -427,7 +433,8 @@ void DOMCache::retrieveRecords(WTF::Function<void(std::optional<Exception>&&)>&&
 
 void DOMCache::queryCache(Ref<FetchRequest>&& request, CacheQueryOptions&& options, WTF::Function<void(ExceptionOr<Vector<CacheStorageRecord>>&&)>&& callback)
 {
-    retrieveRecords([this, request = WTFMove(request), options = WTFMove(options), callback = WTFMove(callback)](std::optional<Exception>&& exception) mutable {
+    auto url = request->url();
+    retrieveRecords(url, [this, request = WTFMove(request), options = WTFMove(options), callback = WTFMove(callback)](std::optional<Exception>&& exception) mutable {
         if (exception) {
             callback(WTFMove(exception.value()));
             return;
index 13c7d60..1ad0019 100644 (file)
@@ -69,7 +69,7 @@ private:
     const char* activeDOMObjectName() const final;
     bool canSuspendForDocumentSuspension() const final;
 
-    void retrieveRecords(WTF::Function<void(std::optional<Exception>&&)>&&);
+    void retrieveRecords(const URL&, WTF::Function<void(std::optional<Exception>&&)>&&);
     Vector<CacheStorageRecord> queryCacheWithTargetStorage(const FetchRequest&, const CacheQueryOptions&, const Vector<CacheStorageRecord>&);
     void queryCache(Ref<FetchRequest>&&, CacheQueryOptions&&, WTF::Function<void(ExceptionOr<Vector<CacheStorageRecord>>&&)>&&);
     void batchDeleteOperation(const FetchRequest&, CacheQueryOptions&&, WTF::Function<void(ExceptionOr<bool>&&)>&&);
index 4700b1c..af955de 100644 (file)
@@ -196,13 +196,13 @@ static inline RecordsOrError recordsOrErrorFromRecordsData(Expected<Vector<Cross
     return recordsFromRecordsData(WTFMove(recordsData.value()));
 }
 
-void WorkerCacheStorageConnection::doRetrieveRecords(uint64_t requestIdentifier, uint64_t cacheIdentifier)
+void WorkerCacheStorageConnection::doRetrieveRecords(uint64_t requestIdentifier, uint64_t cacheIdentifier, const URL& url)
 {
-    m_proxy.postTaskToLoader([this, protectedThis = makeRef(*this), requestIdentifier, cacheIdentifier](ScriptExecutionContext&) mutable {
+    m_proxy.postTaskToLoader([this, protectedThis = makeRef(*this), requestIdentifier, cacheIdentifier, url = url.isolatedCopy()](ScriptExecutionContext&) mutable {
         ASSERT(isMainThread());
         ASSERT(m_mainThreadConnection);
 
-        m_mainThreadConnection->retrieveRecords(cacheIdentifier, [this, protectedThis = WTFMove(protectedThis), requestIdentifier](RecordsOrError&& result) mutable {
+        m_mainThreadConnection->retrieveRecords(cacheIdentifier, url, [this, protectedThis = WTFMove(protectedThis), requestIdentifier](RecordsOrError&& result) mutable {
             m_proxy.postTaskForModeToWorkerGlobalScope([this, protectedThis = WTFMove(protectedThis), result = recordsDataOrErrorFromRecords(result), requestIdentifier](ScriptExecutionContext& context) mutable {
                 ASSERT_UNUSED(context, context.isWorkerGlobalScope());
                 updateRecords(requestIdentifier, recordsOrErrorFromRecordsData(WTFMove(result)));
index 8c5b410..e34fa7b 100644 (file)
@@ -46,7 +46,7 @@ private:
     void doRemove(uint64_t requestIdentifier, uint64_t cacheIdentifier) final;
     void doRetrieveCaches(uint64_t requestIdentifier, const String& origin, uint64_t updateCounter) final;
 
-    void doRetrieveRecords(uint64_t requestIdentifier, uint64_t cacheIdentifier) final;
+    void doRetrieveRecords(uint64_t requestIdentifier, uint64_t cacheIdentifier, const URL&) final;
     void doBatchDeleteOperation(uint64_t requestIdentifier, uint64_t cacheIdentifier, const WebCore::ResourceRequest&, WebCore::CacheQueryOptions&&) final;
     void doBatchPutOperation(uint64_t requestIdentifier, uint64_t cacheIdentifier, Vector<DOMCacheEngine::Record>&&) final;
 
index 97dc882..8be0b16 100644 (file)
@@ -161,10 +161,10 @@ public:
     // The query may begin with a question mark, or, if not, one will be added
     // for you. Setting the query to the empty string will leave a "?" in the
     // URL (with nothing after it). To clear the query, pass a null string.
-    void setQuery(const String&);
+    WEBCORE_EXPORT void setQuery(const String&);
 
     void setFragmentIdentifier(StringView);
-    void removeFragmentIdentifier();
+    WEBCORE_EXPORT void removeFragmentIdentifier();
 
     WEBCORE_EXPORT friend bool equalIgnoringFragmentIdentifier(const URL&, const URL&);
 
index e32663f..ac520b4 100644 (file)
@@ -1,5 +1,37 @@
 2017-09-05  Youenn Fablet  <youenn@apple.com>
 
+        Allow retrieving Cache Storage records for a given URL only
+        https://bugs.webkit.org/show_bug.cgi?id=176202
+
+        Reviewed by Alex Christensen.
+
+        Update CacheStorageEngineCache to store records as a map URL -> Vector of Records.
+        This allows regular record retrieval to be faster.
+        Retrieval of all records is less efficient but this should happen less often.
+
+        * NetworkProcess/cache/CacheStorageEngine.cpp:
+        (WebKit::CacheStorage::Engine::retrieveRecords):
+        * NetworkProcess/cache/CacheStorageEngine.h:
+        * NetworkProcess/cache/CacheStorageEngineCache.cpp:
+        (WebKit::CacheStorage::queryCache):
+        (WebKit::CacheStorage::Cache::retrieveRecords const):
+        (WebKit::CacheStorage::computeKeyURL):
+        (WebKit::CacheStorage::Cache::addNewURLRecord):
+        (WebKit::CacheStorage::Cache::recordsFromURL):
+        (WebKit::CacheStorage::Cache::recordsFromURL const):
+        (WebKit::CacheStorage::Cache::put):
+        (WebKit::CacheStorage::Cache::remove):
+        * NetworkProcess/cache/CacheStorageEngineCache.h:
+        * NetworkProcess/cache/CacheStorageEngineConnection.cpp:
+        (WebKit::CacheStorageEngineConnection::retrieveRecords):
+        * NetworkProcess/cache/CacheStorageEngineConnection.h:
+        * NetworkProcess/cache/CacheStorageEngineConnection.messages.in:
+        * WebProcess/Cache/WebCacheStorageConnection.cpp:
+        (WebKit::WebCacheStorageConnection::doRetrieveRecords):
+        * WebProcess/Cache/WebCacheStorageConnection.h:
+
+2017-09-05  Youenn Fablet  <youenn@apple.com>
+
         WebKitTestRunner should set the cache storage directory path
         https://bugs.webkit.org/show_bug.cgi?id=176373
 
index 14f2967..cba7d51 100644 (file)
@@ -124,15 +124,15 @@ void Engine::retrieveCaches(const String& origin, uint64_t updateCounter, CacheI
     });
 }
 
-void Engine::retrieveRecords(uint64_t cacheIdentifier, RecordsCallback&& callback)
+void Engine::retrieveRecords(uint64_t cacheIdentifier, WebCore::URL&& url, RecordsCallback&& callback)
 {
-    readCache(cacheIdentifier, [callback = WTFMove(callback)](CacheOrError&& result) mutable {
+    readCache(cacheIdentifier, [url = WTFMove(url), callback = WTFMove(callback)](CacheOrError&& result) mutable {
         if (!result.hasValue()) {
             callback(makeUnexpected(result.error()));
             return;
         }
 
-        callback(result.value().get().records());
+        callback(result.value().get().retrieveRecords(url));
     });
 }
 
index 22e92f7..2af8815 100644 (file)
@@ -57,7 +57,7 @@ public:
     void remove(uint64_t cacheIdentifier, WebCore::DOMCacheEngine::CacheIdentifierCallback&&);
     void retrieveCaches(const String& origin, uint64_t updateCounter, WebCore::DOMCacheEngine::CacheInfosCallback&&);
 
-    void retrieveRecords(uint64_t cacheIdentifier, WebCore::DOMCacheEngine::RecordsCallback&&);
+    void retrieveRecords(uint64_t cacheIdentifier, WebCore::URL&&, WebCore::DOMCacheEngine::RecordsCallback&&);
     void putRecords(uint64_t cacheIdentifier, Vector<WebCore::DOMCacheEngine::Record>&&, WebCore::DOMCacheEngine::RecordIdentifiersCallback&&);
     void deleteMatchingRecords(uint64_t cacheIdentifier, WebCore::ResourceRequest&&, WebCore::CacheQueryOptions&&, WebCore::DOMCacheEngine::RecordIdentifiersCallback&&);
 
index 7b0677f..835d95b 100644 (file)
@@ -49,19 +49,87 @@ namespace WebKit {
 
 namespace CacheStorage {
 
+static inline Vector<uint64_t> queryCache(const Vector<Record>* records, const ResourceRequest& request, const CacheQueryOptions& options)
+{
+    if (!records)
+        return { };
+
+    if (!options.ignoreMethod && request.httpMethod() != "GET")
+        return { };
+
+    Vector<uint64_t> results;
+    for (const auto& record : *records) {
+        if (WebCore::DOMCacheEngine::queryCacheMatch(request, record.request, record.response, options))
+            results.append(record.identifier);
+    }
+    return results;
+}
+
 Cache::Cache(uint64_t identifier, String&& name)
     : m_identifier(identifier)
     , m_name(WTFMove(name))
 {
 }
 
-Vector<Record> Cache::records() const
+Vector<Record> Cache::retrieveRecords(const URL& url) const
+{
+    if (url.isNull()) {
+        Vector<Record> result;
+        for (auto& records : m_records.values()) {
+            for (auto& record : records)
+                result.append(record.copy());
+        }
+        std::sort(result.begin(), result.end(), [](const auto& a, const auto& b) {
+            return a.identifier < b.identifier;
+        });
+        return result;
+    }
+
+    const auto* records = recordsFromURL(url);
+    if (!records)
+        return { };
+
+    Vector<Record> result;
+    result.reserveInitialCapacity(records->size());
+    for (auto& record : *records)
+        result.uncheckedAppend(record.copy());
+    return result;
+}
+
+static inline String computeKeyURL(const URL& url)
+{
+    URL keyURL = url;
+    if (keyURL.hasQuery())
+        keyURL.setQuery({ });
+    keyURL.removeFragmentIdentifier();
+    return keyURL;
+}
+
+Record& Cache::addNewURLRecord(Record&& record)
+{
+    auto key = computeKeyURL(record.request.url());
+    ASSERT(!m_records.contains(key));
+
+    Vector<Record> newRecords;
+    newRecords.reserveInitialCapacity(1);
+    newRecords.uncheckedAppend(WTFMove(record));
+    return m_records.set(key, WTFMove(newRecords)).iterator->value.last();
+}
+
+Vector<Record>* Cache::recordsFromURL(const URL& url)
+{
+    auto iterator = m_records.find(computeKeyURL(url));
+    if (iterator == m_records.end())
+        return nullptr;
+    return &iterator->value;
+}
+
+const Vector<Record>* Cache::recordsFromURL(const URL& url) const
 {
-    Vector<Record> records;
-    records.reserveInitialCapacity(m_records.size());
-    for (auto& record : m_records)
-        records.uncheckedAppend(record.copy());
-    return records;
+    auto iterator = m_records.find(computeKeyURL(url));
+    if (iterator == m_records.end())
+        return nullptr;
+    return &iterator->value;
 }
 
 void Cache::put(Vector<Record>&& records, RecordIdentifiersCallback&& callback)
@@ -71,20 +139,27 @@ void Cache::put(Vector<Record>&& records, RecordIdentifiersCallback&& callback)
     Vector<uint64_t> recordIdentifiers;
     recordIdentifiers.reserveInitialCapacity(records.size());
     for (auto& record : records) {
-        auto matchingRecords = queryCache(record.request, options);
+        auto* sameURLRecords = recordsFromURL(record.request.url());
+
+        auto matchingRecords = queryCache(sameURLRecords, record.request, options);
         if (matchingRecords.isEmpty()) {
             record.identifier = ++m_nextRecordIdentifier;
             recordIdentifiers.uncheckedAppend(record.identifier);
-            m_records.append(WTFMove(record));
 
             shouldWriteRecordList = true;
-            writeRecordToDisk(m_records.last());
+            if (!sameURLRecords) {
+                auto& recordToWrite = addNewURLRecord(WTFMove(record));
+                writeRecordToDisk(recordToWrite);
+            } else {
+                sameURLRecords->append(WTFMove(record));
+                writeRecordToDisk(sameURLRecords->last());
+            }
         } else {
             auto identifier = matchingRecords[0];
-            auto position = m_records.findMatching([&](const auto& item) { return item.identifier == identifier; });
+            auto position = sameURLRecords->findMatching([&](const auto& item) { return item.identifier == identifier; });
             ASSERT(position != notFound);
             if (position != notFound) {
-                auto& existingRecord = m_records[position];
+                auto& existingRecord = (*sameURLRecords)[position];
                 recordIdentifiers.uncheckedAppend(identifier);
                 existingRecord.responseHeadersGuard = record.responseHeadersGuard;
                 existingRecord.response = WTFMove(record.response);
@@ -111,20 +186,19 @@ void Cache::put(Vector<Record>&& records, RecordIdentifiersCallback&& callback)
 
 void Cache::remove(WebCore::ResourceRequest&& request, WebCore::CacheQueryOptions&& options, RecordIdentifiersCallback&& callback)
 {
-    auto recordIdentifiers = queryCache(request, options);
+    auto* records = recordsFromURL(request.url());
+    auto recordIdentifiers = queryCache(records, request, options);
     if (recordIdentifiers.isEmpty()) {
         callback({ });
         return;
     }
 
-    Vector<Record> recordsToKeep;
-    for (auto& record : m_records) {
-        if (recordIdentifiers.findMatching([&](auto item) { return item == record.identifier; }) == notFound)
-            recordsToKeep.append(WTFMove(record));
-        else
-            removeRecordFromDisk(record);
-    }
-    m_records = WTFMove(recordsToKeep);
+    records->removeAllMatching([this, &recordIdentifiers](auto& item) {
+        bool shouldRemove = recordIdentifiers.findMatching([&item](auto identifier) { return identifier == item.identifier; }) != notFound;
+        if (shouldRemove)
+            this->removeRecordFromDisk(item);
+        return shouldRemove;
+    });
 
     writeRecordsList([callback = WTFMove(callback), recordIdentifiers = WTFMove(recordIdentifiers)](std::optional<Error>&& error) mutable {
         if (error) {
@@ -135,19 +209,6 @@ void Cache::remove(WebCore::ResourceRequest&& request, WebCore::CacheQueryOption
     });
 }
 
-Vector<uint64_t> Cache::queryCache(const ResourceRequest& request, const CacheQueryOptions& options)
-{
-    if (!options.ignoreMethod && request.httpMethod() != "GET")
-        return { };
-
-    Vector<uint64_t> results;
-    for (const auto& record : m_records) {
-        if (WebCore::DOMCacheEngine::queryCacheMatch(request, record.request, record.response, options))
-            results.append(record.identifier);
-    }
-    return results;
-}
-
 void Cache::writeRecordsList(CompletionCallback&& callback)
 {
     // FIXME: Implement this.
index def86f5..18d6976 100644 (file)
@@ -40,14 +40,16 @@ public:
     uint64_t identifier() const { return m_identifier; }
     const String& name() const { return m_name; }
 
-    Vector<WebCore::DOMCacheEngine::Record> records() const;
+    Vector<WebCore::DOMCacheEngine::Record> retrieveRecords(const WebCore::URL&) const;
     WebCore::DOMCacheEngine::CacheInfo info() const { return { m_identifier, m_name }; }
 
     void put(Vector<WebCore::DOMCacheEngine::Record>&&, WebCore::DOMCacheEngine::RecordIdentifiersCallback&&);
     void remove(WebCore::ResourceRequest&&, WebCore::CacheQueryOptions&&, WebCore::DOMCacheEngine::RecordIdentifiersCallback&&);
 
 private:
-    Vector<uint64_t> queryCache(const WebCore::ResourceRequest&, const WebCore::CacheQueryOptions&);
+    Vector<WebCore::DOMCacheEngine::Record>* recordsFromURL(const WebCore::URL&);
+    const Vector<WebCore::DOMCacheEngine::Record>* recordsFromURL(const WebCore::URL&) const;
+    WebCore::DOMCacheEngine::Record& addNewURLRecord(WebCore::DOMCacheEngine::Record&&);
 
     void writeRecordsList(WebCore::DOMCacheEngine::CompletionCallback&&);
     void writeRecordToDisk(WebCore::DOMCacheEngine::Record&);
@@ -55,7 +57,7 @@ private:
 
     uint64_t m_identifier { 0 };
     String m_name;
-    Vector<WebCore::DOMCacheEngine::Record> m_records;
+    HashMap<String, Vector<WebCore::DOMCacheEngine::Record>> m_records;
     uint64_t m_nextRecordIdentifier { 0 };
 };
 
index b73b155..cea2ebc 100644 (file)
@@ -68,9 +68,9 @@ void CacheStorageEngineConnection::clearMemoryRepresentation(PAL::SessionID sess
     Engine::from(sessionID).clearMemoryRepresentation(origin);
 }
 
-void CacheStorageEngineConnection::records(PAL::SessionID sessionID, uint64_t requestIdentifier, uint64_t cacheIdentifier)
+void CacheStorageEngineConnection::retrieveRecords(PAL::SessionID sessionID, uint64_t requestIdentifier, uint64_t cacheIdentifier, WebCore::URL&& url)
 {
-    Engine::from(sessionID).retrieveRecords(cacheIdentifier, [protectedThis = makeRef(*this), this, sessionID, requestIdentifier](RecordsOrError&& result) {
+    Engine::from(sessionID).retrieveRecords(cacheIdentifier, WTFMove(url), [protectedThis = makeRef(*this), this, sessionID, requestIdentifier](RecordsOrError&& result) {
         m_connection.connection().send(Messages::WebCacheStorageConnection::UpdateRecords(requestIdentifier, result), sessionID.sessionID());
     });
 }
index a33e147..adbf4ac 100644 (file)
@@ -56,7 +56,7 @@ private:
 
     void clearMemoryRepresentation(PAL::SessionID, const String& origin);
 
-    void records(PAL::SessionID, uint64_t requestIdentifier, uint64_t cacheIdentifier);
+    void retrieveRecords(PAL::SessionID, uint64_t requestIdentifier, uint64_t cacheIdentifier, WebCore::URL&&);
     void deleteMatchingRecords(PAL::SessionID, uint64_t requestIdentifier, uint64_t cacheIdentifier, WebCore::ResourceRequest&&, WebCore::CacheQueryOptions&&);
     void putRecords(PAL::SessionID, uint64_t requestIdentifier, uint64_t cacheIdentifier, Vector<WebCore::DOMCacheEngine::Record>&&);
 
index 077d93b..4f61b67 100644 (file)
@@ -27,7 +27,7 @@ messages -> CacheStorageEngineConnection {
 
     ClearMemoryRepresentation(PAL::SessionID sessionID, String origin);
 
-    Records(PAL::SessionID sessionID, uint64_t requestIdentifier, uint64_t cacheIdentifier);
+    RetrieveRecords(PAL::SessionID sessionID, uint64_t requestIdentifier, uint64_t cacheIdentifier, WebCore::URL url);
     DeleteMatchingRecords(PAL::SessionID sessionID, uint64_t requestIdentifier, uint64_t cacheIdentifier, WebCore::ResourceRequest request, struct WebCore::CacheQueryOptions options);
     PutRecords(PAL::SessionID sessionID, uint64_t requestIdentifier, uint64_t cacheIdentifier, Vector<WebCore::DOMCacheEngine::Record> record);
 }
index 461210d..818b9d0 100644 (file)
@@ -71,9 +71,9 @@ void WebCacheStorageConnection::doRetrieveCaches(uint64_t requestIdentifier, con
     connection().send(Messages::CacheStorageEngineConnection::Caches(m_sessionID, requestIdentifier, origin, updateCounter), 0);
 }
 
-void WebCacheStorageConnection::doRetrieveRecords(uint64_t requestIdentifier, uint64_t cacheIdentifier)
+void WebCacheStorageConnection::doRetrieveRecords(uint64_t requestIdentifier, uint64_t cacheIdentifier, const WebCore::URL& url)
 {
-    connection().send(Messages::CacheStorageEngineConnection::Records(m_sessionID, requestIdentifier, cacheIdentifier), 0);
+    connection().send(Messages::CacheStorageEngineConnection::RetrieveRecords(m_sessionID, requestIdentifier, cacheIdentifier, url), 0);
 }
 
 void WebCacheStorageConnection::doBatchDeleteOperation(uint64_t requestIdentifier, uint64_t cacheIdentifier, const WebCore::ResourceRequest& request, WebCore::CacheQueryOptions&& options)
index 6e937df..9f2aed2 100644 (file)
@@ -56,7 +56,7 @@ private:
     void doRemove(uint64_t requestIdentifier, uint64_t cacheIdentifier) final;
     void doRetrieveCaches(uint64_t requestIdentifier, const String& origin, uint64_t updateCounter) final;
 
-    void doRetrieveRecords(uint64_t requestIdentifier, uint64_t cacheIdentifier) final;
+    void doRetrieveRecords(uint64_t requestIdentifier, uint64_t cacheIdentifier, const WebCore::URL&) final;
     void doBatchDeleteOperation(uint64_t requestIdentifier, uint64_t cacheIdentifier, const WebCore::ResourceRequest&, WebCore::CacheQueryOptions&&) final;
     void doBatchPutOperation(uint64_t requestIdentifier, uint64_t cacheIdentifier, Vector<WebCore::DOMCacheEngine::Record>&&) final;