Title: [250786] trunk
Revision
250786
Author
[email protected]
Date
2019-10-07 13:41:05 -0700 (Mon, 07 Oct 2019)

Log Message

DOMCache should not prevent pages from entering the back/forward cache
https://bugs.webkit.org/show_bug.cgi?id=202590

Reviewed by Youenn Fablet.

Source/WebCore:

Allow DOMCache to suspend in the back/forward cache unconditionally, by
appending all promise resolutions to a SuspendableTaskQueue. This guarantees
that any asynchronous promise resolution is delayed while in the page cache,
until the page gets resumed.

Test: http/tests/navigation/page-cache-domcache-pending-promise.html

* Modules/cache/DOMCache.cpp:
(WebCore::DOMCache::DOMCache):
(WebCore::DOMCache::match):
(WebCore::DOMCache::matchAll):
(WebCore::DOMCache::addAll):
(WebCore::DOMCache::putWithResponseData):
(WebCore::DOMCache::put):
(WebCore::DOMCache::remove):
(WebCore::DOMCache::keys):
(WebCore::DOMCache::canSuspendForDocumentSuspension const):
* Modules/cache/DOMCache.h:

LayoutTests:

Add layout test coverage.

* TestExpectations:
* http/tests/navigation/page-cache-domcache-pending-promise-expected.txt: Added.
* http/tests/navigation/page-cache-domcache-pending-promise.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (250785 => 250786)


--- trunk/LayoutTests/ChangeLog	2019-10-07 20:31:01 UTC (rev 250785)
+++ trunk/LayoutTests/ChangeLog	2019-10-07 20:41:05 UTC (rev 250786)
@@ -1,3 +1,16 @@
+2019-10-07  Chris Dumez  <[email protected]>
+
+        DOMCache should not prevent pages from entering the back/forward cache
+        https://bugs.webkit.org/show_bug.cgi?id=202590
+
+        Reviewed by Youenn Fablet.
+
+        Add layout test coverage.
+
+        * TestExpectations:
+        * http/tests/navigation/page-cache-domcache-pending-promise-expected.txt: Added.
+        * http/tests/navigation/page-cache-domcache-pending-promise.html: Added.
+
 2019-10-07  Alexey Shvayka  <[email protected]>
 
         Sync web-platform-tests/css/css-overflow from upstream

Modified: trunk/LayoutTests/TestExpectations (250785 => 250786)


--- trunk/LayoutTests/TestExpectations	2019-10-07 20:31:01 UTC (rev 250785)
+++ trunk/LayoutTests/TestExpectations	2019-10-07 20:41:05 UTC (rev 250786)
@@ -454,6 +454,8 @@
 imported/w3c/web-platform-tests/workers/constructors/SharedWorker [ Skip ]
 imported/w3c/web-platform-tests/workers/SharedWorker_blobUrl.html [ Skip ]
 
+http/tests/navigation/page-cache-domcache-pending-promise.html [ DumpJSConsoleLogInStdErr ]
+
 imported/w3c/web-platform-tests/beacon/beacon-basic-blob.html [ DumpJSConsoleLogInStdErr ]
 imported/w3c/web-platform-tests/fetch/api/cors/cors-preflight-redirect.any.worker.html [ DumpJSConsoleLogInStdErr ]
 imported/w3c/web-platform-tests/xhr/access-control-and-redirects.htm [ DumpJSConsoleLogInStdErr ]

Added: trunk/LayoutTests/http/tests/navigation/page-cache-domcache-pending-promise-expected.txt (0 => 250786)


--- trunk/LayoutTests/http/tests/navigation/page-cache-domcache-pending-promise-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/http/tests/navigation/page-cache-domcache-pending-promise-expected.txt	2019-10-07 20:41:05 UTC (rev 250786)
@@ -0,0 +1,15 @@
+Tests that a page with pending DOMCache activity goes into the page cache.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+pageshow - not from cache
+pagehide - entering cache
+pageshow - from cache
+PASS Page was restored from Page Cache
+PASS Cache.add() succeeded
+PASS !!restoredFromPageCache is true
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/http/tests/navigation/page-cache-domcache-pending-promise.html (0 => 250786)


--- trunk/LayoutTests/http/tests/navigation/page-cache-domcache-pending-promise.html	                        (rev 0)
+++ trunk/LayoutTests/http/tests/navigation/page-cache-domcache-pending-promise.html	2019-10-07 20:41:05 UTC (rev 250786)
@@ -0,0 +1,53 @@
+<!-- webkit-test-runner [ enablePageCache=true ] -->
+<!DOCTYPE html>
+<html>
+<head>
+<script src=""
+</head>
+<body>
+<script>
+description('Tests that a page with pending DOMCache activity goes into the page cache.');
+jsTestIsAsync = true;
+let restoredFromPageCache = false;
+
+window.addEventListener("pageshow", function(event) {
+    debug("pageshow - " + (event.persisted ? "" : "not ") + "from cache");
+    if (event.persisted) {
+        testPassed("Page was restored from Page Cache");
+        restoredFromPageCache = true;
+    }
+});
+
+function tryCache()
+{
+    cache.add('resources/blank.txt').then(() => {
+        testPassed("Cache.add() succeeded");
+        shouldBeTrue("!!restoredFromPageCache");
+        finishJSTest();
+    }, (e) => {
+        tryCache();
+    });
+}
+
+window.addEventListener("pagehide", function(event) {
+    debug("pagehide - " + (event.persisted ? "" : "not ") + "entering cache");
+    if (!event.persisted) {
+        testFailed("Page failed to enter the Page Cache");
+        finishJSTest();
+    }
+
+    tryCache();
+});
+
+_onload_ = () => {
+    setTimeout(() => {
+        caches.open('test').then((_cache) => {
+            cache = _cache;
+            testLink.click();
+        });
+    }, 0);
+}
+</script>
+<a id="testLink" href="" style="display: none">Link</a>
+</body>
+</html>

Modified: trunk/LayoutTests/platform/ios-wk1/TestExpectations (250785 => 250786)


--- trunk/LayoutTests/platform/ios-wk1/TestExpectations	2019-10-07 20:31:01 UTC (rev 250785)
+++ trunk/LayoutTests/platform/ios-wk1/TestExpectations	2019-10-07 20:41:05 UTC (rev 250786)
@@ -11,6 +11,7 @@
 http/wpt/service-workers [ Skip ]
 http/wpt/cache-storage [ Skip ]
 http/tests/cache-storage [ Skip ]
+http/tests/navigation/page-cache-domcache-pending-promise.html [ Skip ]
 imported/w3c/web-platform-tests/fetch/api/request/destination [ Skip ]
 imported/w3c/web-platform-tests/fetch/cross-origin-resource-policy [ Skip ]
 

Modified: trunk/LayoutTests/platform/mac-wk1/TestExpectations (250785 => 250786)


--- trunk/LayoutTests/platform/mac-wk1/TestExpectations	2019-10-07 20:31:01 UTC (rev 250785)
+++ trunk/LayoutTests/platform/mac-wk1/TestExpectations	2019-10-07 20:41:05 UTC (rev 250786)
@@ -267,6 +267,7 @@
 # No service worker implementation for WK1
 http/tests/appcache/main-resource-redirect-with-sw.html [ Skip ]
 http/tests/cache-storage [ Skip ]
+http/tests/navigation/page-cache-domcache-pending-promise.html [ Skip ]
 http/tests/cookies/same-site/fetch-in-cross-origin-service-worker.html [ Skip ]
 http/tests/cookies/same-site/fetch-in-same-origin-service-worker.html [ Skip ]
 http/wpt/cache-storage [ Skip ]

Modified: trunk/LayoutTests/platform/win/TestExpectations (250785 => 250786)


--- trunk/LayoutTests/platform/win/TestExpectations	2019-10-07 20:31:01 UTC (rev 250785)
+++ trunk/LayoutTests/platform/win/TestExpectations	2019-10-07 20:41:05 UTC (rev 250786)
@@ -3798,6 +3798,7 @@
 # No service worker implementation for WK1
 http/tests/appcache/main-resource-redirect-with-sw.html [ Skip ]
 http/tests/cache-storage [ Skip ]
+http/tests/navigation/page-cache-domcache-pending-promise.html [ Skip ]
 http/tests/inspector/network/resource-response-service-worker.html [ Skip ]
 http/tests/workers/service [ Skip ]
 http/wpt/cache-storage [ Skip ]

Modified: trunk/Source/WebCore/ChangeLog (250785 => 250786)


--- trunk/Source/WebCore/ChangeLog	2019-10-07 20:31:01 UTC (rev 250785)
+++ trunk/Source/WebCore/ChangeLog	2019-10-07 20:41:05 UTC (rev 250786)
@@ -1,5 +1,31 @@
 2019-10-07  Chris Dumez  <[email protected]>
 
+        DOMCache should not prevent pages from entering the back/forward cache
+        https://bugs.webkit.org/show_bug.cgi?id=202590
+
+        Reviewed by Youenn Fablet.
+
+        Allow DOMCache to suspend in the back/forward cache unconditionally, by
+        appending all promise resolutions to a SuspendableTaskQueue. This guarantees
+        that any asynchronous promise resolution is delayed while in the page cache,
+        until the page gets resumed.
+
+        Test: http/tests/navigation/page-cache-domcache-pending-promise.html
+
+        * Modules/cache/DOMCache.cpp:
+        (WebCore::DOMCache::DOMCache):
+        (WebCore::DOMCache::match):
+        (WebCore::DOMCache::matchAll):
+        (WebCore::DOMCache::addAll):
+        (WebCore::DOMCache::putWithResponseData):
+        (WebCore::DOMCache::put):
+        (WebCore::DOMCache::remove):
+        (WebCore::DOMCache::keys):
+        (WebCore::DOMCache::canSuspendForDocumentSuspension const):
+        * Modules/cache/DOMCache.h:
+
+2019-10-07  Chris Dumez  <[email protected]>
+
         PendingImageBitmap should not prevent entering the back/forward cache
         https://bugs.webkit.org/show_bug.cgi?id=202585
 

Modified: trunk/Source/WebCore/Modules/cache/DOMCache.cpp (250785 => 250786)


--- trunk/Source/WebCore/Modules/cache/DOMCache.cpp	2019-10-07 20:31:01 UTC (rev 250785)
+++ trunk/Source/WebCore/Modules/cache/DOMCache.cpp	2019-10-07 20:41:05 UTC (rev 250786)
@@ -33,6 +33,7 @@
 #include "JSFetchResponse.h"
 #include "ReadableStreamChunk.h"
 #include "ScriptExecutionContext.h"
+#include "SuspendableTaskQueue.h"
 #include <wtf/CompletionHandler.h>
 #include <wtf/URL.h>
 
@@ -44,6 +45,7 @@
     , m_name(WTFMove(name))
     , m_identifier(identifier)
     , m_connection(WTFMove(connection))
+    , m_taskQueue(SuspendableTaskQueue::create(&context))
 {
     suspendIfNeeded();
     m_connection->reference(m_identifier);
@@ -57,16 +59,18 @@
 
 void DOMCache::match(RequestInfo&& info, CacheQueryOptions&& options, Ref<DeferredPromise>&& promise)
 {
-    doMatch(WTFMove(info), WTFMove(options), [promise = WTFMove(promise)](ExceptionOr<FetchResponse*>&& result) mutable {
-        if (result.hasException()) {
-            promise->reject(result.releaseException());
-            return;
-        }
-        if (!result.returnValue()) {
-            promise->resolve();
-            return;
-        }
-        promise->resolve<IDLInterface<FetchResponse>>(*result.returnValue());
+    doMatch(WTFMove(info), WTFMove(options), [this, protectedThis = makeRef(*this), promise = WTFMove(promise)](ExceptionOr<RefPtr<FetchResponse>>&& result) mutable {
+        m_taskQueue->enqueueTask([promise = WTFMove(promise), result = WTFMove(result)]() mutable {
+            if (result.hasException()) {
+                promise->reject(result.releaseException());
+                return;
+            }
+            if (!result.returnValue()) {
+                promise->resolve();
+                return;
+            }
+            promise->resolve<IDLInterface<FetchResponse>>(*result.returnValue());
+        });
     });
 }
 
@@ -91,7 +95,7 @@
             callback(nullptr);
             return;
         }
-        callback(result.returnValue()[0].response->clone(*scriptExecutionContext()).releaseReturnValue().ptr());
+        callback(RefPtr<FetchResponse>(result.returnValue()[0].response->clone(*scriptExecutionContext()).releaseReturnValue()));
     });
 }
 
@@ -120,20 +124,24 @@
 
     if (!request) {
         retrieveRecords(URL { }, [this, promise = WTFMove(promise)](Optional<Exception>&& exception) mutable {
-            if (exception) {
-                promise.reject(WTFMove(exception.value()));
-                return;
-            }
-            promise.resolve(cloneResponses(m_records));
+            m_taskQueue->enqueueTask([this, promise = WTFMove(promise), exception = WTFMove(exception)]() mutable {
+                if (exception) {
+                    promise.reject(WTFMove(exception.value()));
+                    return;
+                }
+                promise.resolve(cloneResponses(m_records));
+            });
         });
         return;
     }
     queryCache(request.releaseNonNull(), WTFMove(options), [this, promise = WTFMove(promise)](ExceptionOr<Vector<CacheStorageRecord>>&& result) mutable {
-        if (result.hasException()) {
-            promise.reject(result.releaseException());
-            return;
-        }
-        promise.resolve(cloneResponses(result.releaseReturnValue()));
+        m_taskQueue->enqueueTask([this, promise = WTFMove(promise), result = WTFMove(result)]() mutable {
+            if (result.hasException()) {
+                promise.reject(result.releaseException());
+                return;
+            }
+            promise.resolve(cloneResponses(result.releaseReturnValue()));
+        });
     });
 }
 
@@ -235,11 +243,15 @@
 
     auto taskHandler = FetchTasksHandler::create(*this, [this, promise = WTFMove(promise)](ExceptionOr<Vector<Record>>&& result) mutable {
         if (result.hasException()) {
-            promise.reject(result.releaseException());
+            m_taskQueue->enqueueTask([promise = WTFMove(promise), exception = result.releaseException()]() mutable {
+                promise.reject(WTFMove(exception));
+            });
             return;
         }
-        batchPutOperation(result.releaseReturnValue(), [promise = WTFMove(promise)](ExceptionOr<void>&& result) mutable {
-            promise.settle(WTFMove(result));
+        batchPutOperation(result.releaseReturnValue(), [this, protectedThis = makeRef(*this), promise = WTFMove(promise)](ExceptionOr<void>&& result) mutable {
+            m_taskQueue->enqueueTask([promise = WTFMove(promise), result = WTFMove(result)]() mutable {
+                promise.settle(WTFMove(result));
+            });
         });
     });
 
@@ -302,7 +314,9 @@
 void DOMCache::putWithResponseData(DOMPromiseDeferred<void>&& promise, Ref<FetchRequest>&& request, Ref<FetchResponse>&& response, ExceptionOr<RefPtr<SharedBuffer>>&& responseBody)
 {
     if (responseBody.hasException()) {
-        promise.reject(responseBody.releaseException());
+        m_taskQueue->enqueueTask([promise = WTFMove(promise), exception = responseBody.releaseException()]() mutable {
+            promise.reject(WTFMove(exception));
+        });
         return;
     }
 
@@ -309,8 +323,10 @@
     DOMCacheEngine::ResponseBody body;
     if (auto buffer = responseBody.releaseReturnValue())
         body = buffer.releaseNonNull();
-    batchPutOperation(request.get(), response.get(), WTFMove(body), [promise = WTFMove(promise)](ExceptionOr<void>&& result) mutable {
-        promise.settle(WTFMove(result));
+    batchPutOperation(request.get(), response.get(), WTFMove(body), [this, protectedThis = makeRef(*this), promise = WTFMove(promise)](ExceptionOr<void>&& result) mutable {
+        m_taskQueue->enqueueTask([promise = WTFMove(promise), result = WTFMove(result)]() mutable {
+            promise.settle(WTFMove(result));
+        });
     });
 }
 
@@ -373,8 +389,10 @@
         return;
     }
 
-    batchPutOperation(request.get(), response.get(), response->consumeBody(), [promise = WTFMove(promise)](ExceptionOr<void>&& result) mutable {
-        promise.settle(WTFMove(result));
+    batchPutOperation(request.get(), response.get(), response->consumeBody(), [this, protectedThis = makeRef(*this), promise = WTFMove(promise)](ExceptionOr<void>&& result) mutable {
+        m_taskQueue->enqueueTask([promise = WTFMove(promise), result = WTFMove(result)]() mutable {
+            promise.settle(WTFMove(result));
+        });
     });
 }
 
@@ -389,8 +407,10 @@
         return;
     }
 
-    batchDeleteOperation(requestOrException.releaseReturnValue(), WTFMove(options), [promise = WTFMove(promise)](ExceptionOr<bool>&& result) mutable {
-        promise.settle(WTFMove(result));
+    batchDeleteOperation(requestOrException.releaseReturnValue(), WTFMove(options), [this, protectedThis = makeRef(*this), promise = WTFMove(promise)](ExceptionOr<bool>&& result) mutable {
+        m_taskQueue->enqueueTask([promise = WTFMove(promise), result = WTFMove(result)]() mutable {
+            promise.settle(WTFMove(result));
+        });
     });
 }
 
@@ -416,22 +436,26 @@
 
     if (!request) {
         retrieveRecords(URL { }, [this, promise = WTFMove(promise)](Optional<Exception>&& exception) mutable {
-            if (exception) {
-                promise.reject(WTFMove(exception.value()));
-                return;
-            }
-            promise.resolve(WTF::map(m_records, copyRequestRef));
+            m_taskQueue->enqueueTask([this, promise = WTFMove(promise), exception = WTFMove(exception)]() mutable {
+                if (exception) {
+                    promise.reject(WTFMove(exception.value()));
+                    return;
+                }
+                promise.resolve(WTF::map(m_records, copyRequestRef));
+            });
         });
         return;
     }
 
-    queryCache(request.releaseNonNull(), WTFMove(options), [promise = WTFMove(promise)](ExceptionOr<Vector<CacheStorageRecord>>&& result) mutable {
-        if (result.hasException()) {
-            promise.reject(result.releaseException());
-            return;
-        }
+    queryCache(request.releaseNonNull(), WTFMove(options), [this, protectedThis = makeRef(*this), promise = WTFMove(promise)](auto&& result) mutable {
+        m_taskQueue->enqueueTask([promise = WTFMove(promise), result = WTFMove(result)]() mutable {
+            if (result.hasException()) {
+                promise.reject(result.releaseException());
+                return;
+            }
 
-        promise.resolve(WTF::map(result.releaseReturnValue(), copyRequestRef));
+            promise.resolve(WTF::map(result.releaseReturnValue(), copyRequestRef));
+        });
     });
 }
 
@@ -586,7 +610,7 @@
 
 bool DOMCache::canSuspendForDocumentSuspension() const
 {
-    return m_records.isEmpty() && !hasPendingActivity();
+    return true;
 }
 
 

Modified: trunk/Source/WebCore/Modules/cache/DOMCache.h (250785 => 250786)


--- trunk/Source/WebCore/Modules/cache/DOMCache.h	2019-10-07 20:31:01 UTC (rev 250785)
+++ trunk/Source/WebCore/Modules/cache/DOMCache.h	2019-10-07 20:41:05 UTC (rev 250786)
@@ -28,10 +28,12 @@
 #include "ActiveDOMObject.h"
 #include "CacheStorageConnection.h"
 #include "CacheStorageRecord.h"
+#include <wtf/UniqueRef.h>
 
 namespace WebCore {
 
 class ScriptExecutionContext;
+class SuspendableTaskQueue;
 
 class DOMCache final : public RefCounted<DOMCache>, public ActiveDOMObject {
 public:
@@ -56,7 +58,7 @@
     const String& name() const { return m_name; }
     uint64_t identifier() const { return m_identifier; }
 
-    using MatchCallback = WTF::Function<void(ExceptionOr<FetchResponse*>)>;
+    using MatchCallback = Function<void(ExceptionOr<RefPtr<FetchResponse>>)>;
     void doMatch(RequestInfo&&, CacheQueryOptions&&, MatchCallback&&);
 
     CacheStorageConnection& connection() { return m_connection.get(); }
@@ -90,6 +92,7 @@
 
     Vector<CacheStorageRecord> m_records;
     bool m_isStopped { false };
+    UniqueRef<SuspendableTaskQueue> m_taskQueue;
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/Modules/cache/DOMCacheStorage.cpp (250785 => 250786)


--- trunk/Source/WebCore/Modules/cache/DOMCacheStorage.cpp	2019-10-07 20:31:01 UTC (rev 250785)
+++ trunk/Source/WebCore/Modules/cache/DOMCacheStorage.cpp	2019-10-07 20:41:05 UTC (rev 250786)
@@ -60,13 +60,13 @@
     }
 
     auto& cache = caches[index].get();
-    cache.doMatch(WTFMove(info), WTFMove(options), [caches = WTFMove(caches), info, options, completionHandler = WTFMove(completionHandler), index](ExceptionOr<FetchResponse*>&& result) mutable {
+    cache.doMatch(WTFMove(info), WTFMove(options), [caches = WTFMove(caches), info, options, completionHandler = WTFMove(completionHandler), index](auto&& result) mutable {
         if (result.hasException()) {
             completionHandler(result.releaseException());
             return;
         }
         if (result.returnValue()) {
-            completionHandler(result.returnValue());
+            completionHandler(result.releaseReturnValue());
             return;
         }
         doSequentialMatch(++index, WTFMove(caches), WTFMove(info), WTFMove(options), WTFMove(completionHandler));
@@ -85,7 +85,7 @@
 
 void DOMCacheStorage::doSequentialMatch(DOMCache::RequestInfo&& info, CacheQueryOptions&& options, Ref<DeferredPromise>&& promise)
 {
-    startSequentialMatch(WTF::map(m_caches, copyCache), WTFMove(info), WTFMove(options), [this, pendingActivity = makePendingActivity(*this), promise = WTFMove(promise)](ExceptionOr<FetchResponse*>&& result) mutable {
+    startSequentialMatch(WTF::map(m_caches, copyCache), WTFMove(info), WTFMove(options), [this, pendingActivity = makePendingActivity(*this), promise = WTFMove(promise)](auto&& result) mutable {
         if (m_isStopped)
             return;
         if (result.hasException()) {
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to