Diff
Modified: trunk/LayoutTests/ChangeLog (250692 => 250693)
--- trunk/LayoutTests/ChangeLog 2019-10-03 23:08:07 UTC (rev 250692)
+++ trunk/LayoutTests/ChangeLog 2019-10-04 00:37:54 UTC (rev 250693)
@@ -1,5 +1,18 @@
2019-10-03 Chris Dumez <[email protected]>
+ Allow pages using FontFaceSet to enter the back/forward cache
+ https://bugs.webkit.org/show_bug.cgi?id=202476
+
+ Reviewed by Myles C. Maxfield.
+
+ Add layout test coverage.
+
+ * http/tests/navigation/page-cache-fontfaceset-expected.txt: Added.
+ * http/tests/navigation/page-cache-fontfaceset.html: Added.
+ * http/tests/navigation/resources/page-cache-fontfaceset-iframe.html: Added.
+
+2019-10-03 Chris Dumez <[email protected]>
+
Provisional / scheduled loads in subframes should not prevent a page from entering the back/forward cache
https://bugs.webkit.org/show_bug.cgi?id=202474
Added: trunk/LayoutTests/http/tests/navigation/page-cache-fontfaceset-expected.txt (0 => 250693)
--- trunk/LayoutTests/http/tests/navigation/page-cache-fontfaceset-expected.txt (rev 0)
+++ trunk/LayoutTests/http/tests/navigation/page-cache-fontfaceset-expected.txt 2019-10-04 00:37:54 UTC (rev 250693)
@@ -0,0 +1,13 @@
+Tests that a page with pending CSSFontFaceSet 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 successfullyParsed is true
+
+TEST COMPLETE
+
Added: trunk/LayoutTests/http/tests/navigation/page-cache-fontfaceset.html (0 => 250693)
--- trunk/LayoutTests/http/tests/navigation/page-cache-fontfaceset.html (rev 0)
+++ trunk/LayoutTests/http/tests/navigation/page-cache-fontfaceset.html 2019-10-04 00:37:54 UTC (rev 250693)
@@ -0,0 +1,43 @@
+<!-- webkit-test-runner [ enablePageCache=true ] -->
+<!DOCTYPE html>
+<html>
+<head>
+<script src=""
+</head>
+<body>
+<script>
+description('Tests that a page with pending CSSFontFaceSet activity goes into the page cache.');
+jsTestIsAsync = true;
+
+window.addEventListener("pageshow", function(event) {
+ debug("pageshow - " + (event.persisted ? "" : "not ") + "from cache");
+ if (event.persisted) {
+ testPassed("Page was restored from Page Cache");
+ finishJSTest();
+ }
+});
+
+window.addEventListener("pagehide", function(event) {
+ debug("pagehide - " + (event.persisted ? "" : "not ") + "entering cache");
+ if (!event.persisted) {
+ testFailed("Page failed to enter the Page Cache");
+ finishJSTest();
+ }
+});
+
+_onload_ = () => {
+ setTimeout(() => {
+ let iframe = document.createElement("iframe");
+ iframe.src = ""
+ iframe._onload_ = () => {
+ setTimeout(() => {
+ testLink.click();
+ }, 100);
+ };
+ document.body.appendChild(iframe);
+ }, 0);
+}
+</script>
+<a id="testLink" href="" style="display: none">Link</a>
+</body>
+</html>
Added: trunk/LayoutTests/http/tests/navigation/resources/page-cache-fontfaceset-iframe.html (0 => 250693)
--- trunk/LayoutTests/http/tests/navigation/resources/page-cache-fontfaceset-iframe.html (rev 0)
+++ trunk/LayoutTests/http/tests/navigation/resources/page-cache-fontfaceset-iframe.html 2019-10-04 00:37:54 UTC (rev 250693)
@@ -0,0 +1,129 @@
+<!DOCTYPE html>
+<html>
+<head>
+<style>
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 400;
+ src: local('Roboto Regular'), local('Roboto-Regular'), url(/resources/Ahem.woff?h) format('woff');
+ unicode-range: U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 400;
+ src: local('Roboto Regular'), local('Roboto-Regular'), url(/resources/Ahem.woff?i) format('woff');
+ unicode-range: U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 400;
+ src: local('Roboto Regular'), local('Roboto-Regular'), url(/resources/Ahem.woff?j) format('woff');
+ unicode-range: U+1F00-1FFF;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 400;
+ src: local('Roboto Regular'), local('Roboto-Regular'), url(/resources/Ahem.woff?k) format('woff');
+ unicode-range: U+0370-03FF;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 400;
+ src: local('Roboto Regular'), local('Roboto-Regular'), url(/resources/Ahem.woff?l) format('woff');
+ unicode-range: U+0102-0103, U+0110-0111, U+1EA0-1EF9, U+20AB;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 400;
+ src: local('Roboto Regular'), local('Roboto-Regular'), url(/resources/Ahem.woff?m) format('woff');
+ unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 400;
+ src: local('Roboto Regular'), local('Roboto-Regular'), url(/resources/Ahem.woff?n) format('woff');
+ unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 500;
+ src: local('Roboto Medium'), local('Roboto-Medium'), url(/resources/Ahem.woff?o) format('woff');
+ unicode-range: U+0460-052F, U+1C80-1C88, U+20B4, U+2DE0-2DFF, U+A640-A69F, U+FE2E-FE2F;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 500;
+ src: local('Roboto Medium'), local('Roboto-Medium'), url(/resources/Ahem.woff?p) format('woff');
+ unicode-range: U+0400-045F, U+0490-0491, U+04B0-04B1, U+2116;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 500;
+ src: local('Roboto Medium'), local('Roboto-Medium'), url(/resources/Ahem.woff?q) format('woff');
+ unicode-range: U+1F00-1FFF;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 500;
+ src: local('Roboto Medium'), local('Roboto-Medium'), url(/resources/Ahem.woff?r) format('woff');
+ unicode-range: U+0370-03FF;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 500;
+ src: local('Roboto Medium'), local('Roboto-Medium'), url(/resources/Ahem.woff?s) format('woff');
+ unicode-range: U+0102-0103, U+0110-0111, U+1EA0-1EF9, U+20AB;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 500;
+ src: local('Roboto Medium'), local('Roboto-Medium'), url(/resources/Ahem.woff?t) format('woff');
+ unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
+}
+
+@font-face {
+ font-family: 'Roboto';
+ font-style: normal;
+ font-weight: 500;
+ src: local('Roboto Medium'), local('Roboto-Medium'), url(/resources/Ahem.woff?u) format('woff');
+ unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
+}
+
+body {
+ font-family: "Roboto", arial, sans-serif;
+ font-weight: 500;
+}
+</style>
+<script>
+fontFaceSet = document.fonts;
+</script>
+</head>
+<body>
+FOO
+</body>
+</html>
Modified: trunk/Source/WebCore/ChangeLog (250692 => 250693)
--- trunk/Source/WebCore/ChangeLog 2019-10-03 23:08:07 UTC (rev 250692)
+++ trunk/Source/WebCore/ChangeLog 2019-10-04 00:37:54 UTC (rev 250693)
@@ -1,5 +1,50 @@
2019-10-03 Chris Dumez <[email protected]>
+ Allow pages using FontFaceSet to enter the back/forward cache
+ https://bugs.webkit.org/show_bug.cgi?id=202476
+
+ Reviewed by Myles C. Maxfield.
+
+ Allow pages using FontFaceSet to enter the back/forward cache by updating its
+ implementation to queue all its tasks that run script to a SuspendableTaskQueue.
+ The SuspendableTaskQueue makes sure to properly suspend while in PageCache and
+ run the tasks when resuming from PageCache.
+
+ Test: http/tests/navigation/page-cache-fontfaceset.html
+
+ * PlatformWin.cmake:
+ * Sources.txt:
+ * WebCore.xcodeproj/project.pbxproj:
+ * css/CSSFontFace.cpp:
+ (WebCore::CSSFontFace::fontLoadEventOccurred):
+ * css/FontFaceSet.cpp:
+ (WebCore::FontFaceSet::FontFaceSet):
+ (WebCore::FontFaceSet::canSuspendForDocumentSuspension const):
+ (WebCore::FontFaceSet::didFirstLayout):
+ (WebCore::FontFaceSet::completedLoading):
+ (WebCore::FontFaceSet::faceFinished):
+ * css/FontFaceSet.h:
+
+ * platform/SuspendableTaskQueue.cpp: Added.
+ (WebCore::SuspendableTaskQueue::SuspendableTaskQueue):
+ (WebCore::SuspendableTaskQueue::close):
+ (WebCore::SuspendableTaskQueue::cancelAllTasks):
+ (WebCore::SuspendableTaskQueue::enqueueTask):
+ (WebCore::SuspendableTaskQueue::runOneTask):
+ (WebCore::SuspendableTaskQueue::activeDOMObjectName const):
+ (WebCore::SuspendableTaskQueue::canSuspendForDocumentSuspension const):
+ (WebCore::SuspendableTaskQueue::stop):
+ (WebCore::SuspendableTaskQueue::suspend):
+ (WebCore::SuspendableTaskQueue::resume):
+ * platform/SuspendableTaskQueue.h: Added.
+ (WebCore::SuspendableTaskQueue::create):
+ (WebCore::SuspendableTaskQueue::isClosed const):
+ (WebCore::SuspendableTaskQueue::hasPendingTasks const):
+ Add a SuspendableTaskQueue class which is Page-Cache aware and can be used to queue
+ generic tasks as WTF::Function<void<>> lambdas.
+
+2019-10-03 Chris Dumez <[email protected]>
+
Provisional / scheduled loads in subframes should not prevent a page from entering the back/forward cache
https://bugs.webkit.org/show_bug.cgi?id=202474
Modified: trunk/Source/WebCore/PlatformWin.cmake (250692 => 250693)
--- trunk/Source/WebCore/PlatformWin.cmake 2019-10-03 23:08:07 UTC (rev 250692)
+++ trunk/Source/WebCore/PlatformWin.cmake 2019-10-04 00:37:54 UTC (rev 250693)
@@ -34,6 +34,7 @@
platform/Cursor.cpp
platform/LocalizedStrings.cpp
platform/StaticPasteboard.cpp
+ platform/SuspendableTaskQueue.cpp
platform/audio/PlatformMediaSessionManager.cpp
Modified: trunk/Source/WebCore/Sources.txt (250692 => 250693)
--- trunk/Source/WebCore/Sources.txt 2019-10-03 23:08:07 UTC (rev 250692)
+++ trunk/Source/WebCore/Sources.txt 2019-10-04 00:37:54 UTC (rev 250693)
@@ -1734,6 +1734,7 @@
platform/SharedBufferChunkReader.cpp
platform/SharedStringHash.cpp
platform/StaticPasteboard.cpp
+platform/SuspendableTaskQueue.cpp
platform/Theme.cpp
platform/ThreadGlobalData.cpp
platform/ThreadTimers.cpp
Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (250692 => 250693)
--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj 2019-10-03 23:08:07 UTC (rev 250692)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj 2019-10-04 00:37:54 UTC (rev 250693)
@@ -1228,6 +1228,7 @@
46C696CC1E7205FC00597937 /* CPUMonitor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46C696CA1E7205E400597937 /* CPUMonitor.cpp */; };
46C83EFE1A9BBE2900A79A41 /* GeoNotifier.h in Headers */ = {isa = PBXBuildFile; fileRef = 46C83EFC1A9BBE2900A79A41 /* GeoNotifier.h */; settings = {ATTRIBUTES = (Private, ); }; };
46CA9C441F97BBE9004CFC3A /* VisibilityState.h in Headers */ = {isa = PBXBuildFile; fileRef = 46CA9C411F97BBE7004CFC3A /* VisibilityState.h */; settings = {ATTRIBUTES = (Private, ); }; };
+ 46CDC1F023466B2300D2E70E /* SuspendableTaskQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = 466907F82346686200EFEFF0 /* SuspendableTaskQueue.h */; settings = {ATTRIBUTES = (Private, ); }; };
46DBB6501AB8C96F00D9A813 /* PowerObserverMac.h in Headers */ = {isa = PBXBuildFile; fileRef = 46DBB64E1AB8C96F00D9A813 /* PowerObserverMac.h */; };
46DFF4981DC2603100B80B48 /* ShadowRootMode.h in Headers */ = {isa = PBXBuildFile; fileRef = 46DFF4961DC2601300B80B48 /* ShadowRootMode.h */; settings = {ATTRIBUTES = (Private, ); }; };
46DFF49C1DC2620B00B80B48 /* JSShadowRootMode.h in Headers */ = {isa = PBXBuildFile; fileRef = 46DFF49A1DC261F900B80B48 /* JSShadowRootMode.h */; };
@@ -7602,6 +7603,8 @@
465EDD9D222F4EC300B46E16 /* DeviceOrientationOrMotionEvent.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = DeviceOrientationOrMotionEvent.idl; sourceTree = "<group>"; };
465EDD9F222F4EC300B46E16 /* DeviceOrientationOrMotionPermissionState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DeviceOrientationOrMotionPermissionState.h; sourceTree = "<group>"; };
465EDDA0222F4EC400B46E16 /* DeviceOrientationOrMotionPermissionState.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = DeviceOrientationOrMotionPermissionState.idl; sourceTree = "<group>"; };
+ 466907F62346686200EFEFF0 /* SuspendableTaskQueue.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = SuspendableTaskQueue.cpp; sourceTree = "<group>"; };
+ 466907F82346686200EFEFF0 /* SuspendableTaskQueue.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SuspendableTaskQueue.h; sourceTree = "<group>"; };
466DC6AB1EDE021D00746224 /* JSDOMRectList.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSDOMRectList.cpp; sourceTree = "<group>"; };
466ED8D21EDE0135005E43F6 /* JSDOMRectList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSDOMRectList.h; sourceTree = "<group>"; };
4671E0631D67A57B00C6B497 /* CanvasPath.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CanvasPath.cpp; sourceTree = "<group>"; };
@@ -25595,6 +25598,8 @@
F433E9021DBBDBA200EF0D14 /* StaticPasteboard.h */,
93B2D8150F9920D2006AE6B2 /* SuddenTermination.h */,
97627B9714FB5424002CDCA1 /* Supplementable.h */,
+ 466907F62346686200EFEFF0 /* SuspendableTaskQueue.cpp */,
+ 466907F82346686200EFEFF0 /* SuspendableTaskQueue.h */,
7CC564B618BABEA6001B9652 /* TelephoneNumberDetector.h */,
BCE65D310EAD1211007E4533 /* Theme.cpp */,
BCE658FE0EA9248A007E4533 /* Theme.h */,
@@ -31850,6 +31855,7 @@
97C078501165D5BE003A32EF /* SuffixTree.h in Headers */,
97627B9814FB5424002CDCA1 /* Supplementable.h in Headers */,
087558C613B4A57D00F49307 /* SurrogatePairAwareTextIterator.h in Headers */,
+ 46CDC1F023466B2300D2E70E /* SuspendableTaskQueue.h in Headers */,
62C1217D11AB9E77003C462C /* SuspendableTimer.h in Headers */,
B22279740D00BF220071B782 /* SVGAElement.h in Headers */,
24D912B113CA9A1F00D21915 /* SVGAltGlyphDefElement.h in Headers */,
Modified: trunk/Source/WebCore/css/FontFaceSet.cpp (250692 => 250693)
--- trunk/Source/WebCore/css/FontFaceSet.cpp 2019-10-03 23:08:07 UTC (rev 250692)
+++ trunk/Source/WebCore/css/FontFaceSet.cpp 2019-10-04 00:37:54 UTC (rev 250693)
@@ -56,6 +56,7 @@
: ActiveDOMObject(document)
, m_backing(CSSFontFaceSet::create())
, m_readyPromise(*this, &FontFaceSet::readyPromiseResolve)
+ , m_taskQueue(SuspendableTaskQueue::create(&document))
{
m_backing->addClient(*this);
for (auto& face : initialFaces)
@@ -66,6 +67,7 @@
: ActiveDOMObject(document)
, m_backing(backing)
, m_readyPromise(*this, &FontFaceSet::readyPromiseResolve)
+ , m_taskQueue(SuspendableTaskQueue::create(&document))
{
if (document.frame())
m_isFirstLayoutDone = document.frame()->loader().stateMachine().firstLayoutDone();
@@ -190,7 +192,7 @@
bool FontFaceSet::canSuspendForDocumentSuspension() const
{
- return m_backing->status() == CSSFontFaceSet::Status::Loaded;
+ return true;
}
void FontFaceSet::startedLoading()
@@ -201,14 +203,20 @@
void FontFaceSet::didFirstLayout()
{
m_isFirstLayoutDone = true;
- if (!m_backing->hasActiveFontFaces() && !m_readyPromise.isFulfilled())
- m_readyPromise.resolve(*this);
+ if (!m_backing->hasActiveFontFaces() && !m_readyPromise.isFulfilled()) {
+ m_taskQueue->enqueueTask([this] {
+ m_readyPromise.resolve(*this);
+ });
+ }
}
void FontFaceSet::completedLoading()
{
- if (m_isFirstLayoutDone && !m_readyPromise.isFulfilled())
- m_readyPromise.resolve(*this);
+ if (m_isFirstLayoutDone && !m_readyPromise.isFulfilled()) {
+ m_taskQueue->enqueueTask([this] {
+ m_readyPromise.resolve(*this);
+ });
+ }
}
void FontFaceSet::faceFinished(CSSFontFace& face, CSSFontFace::Status newStatus)
@@ -216,26 +224,26 @@
if (!face.existingWrapper())
return;
- auto iterator = m_pendingPromises.find(face.existingWrapper());
- if (iterator == m_pendingPromises.end())
+ auto pendingPromises = m_pendingPromises.take(face.existingWrapper());
+ if (pendingPromises.isEmpty())
return;
- for (auto& pendingPromise : iterator->value) {
- if (pendingPromise->hasReachedTerminalState)
- continue;
- if (newStatus == CSSFontFace::Status::Success) {
- if (pendingPromise->hasOneRef()) {
- pendingPromise->promise.resolve(pendingPromise->faces);
+ m_taskQueue->enqueueTask([pendingPromises = WTFMove(pendingPromises), newStatus] {
+ for (auto& pendingPromise : pendingPromises) {
+ if (pendingPromise->hasReachedTerminalState)
+ continue;
+ if (newStatus == CSSFontFace::Status::Success) {
+ if (pendingPromise->hasOneRef()) {
+ pendingPromise->promise.resolve(pendingPromise->faces);
+ pendingPromise->hasReachedTerminalState = true;
+ }
+ } else {
+ ASSERT(newStatus == CSSFontFace::Status::Failure);
+ pendingPromise->promise.reject(NetworkError);
pendingPromise->hasReachedTerminalState = true;
}
- } else {
- ASSERT(newStatus == CSSFontFace::Status::Failure);
- pendingPromise->promise.reject(NetworkError);
- pendingPromise->hasReachedTerminalState = true;
}
- }
-
- m_pendingPromises.remove(iterator);
+ });
}
FontFaceSet& FontFaceSet::readyPromiseResolve()
Modified: trunk/Source/WebCore/css/FontFaceSet.h (250692 => 250693)
--- trunk/Source/WebCore/css/FontFaceSet.h 2019-10-03 23:08:07 UTC (rev 250692)
+++ trunk/Source/WebCore/css/FontFaceSet.h 2019-10-04 00:37:54 UTC (rev 250693)
@@ -30,6 +30,7 @@
#include "DOMPromiseProxy.h"
#include "EventTarget.h"
#include "JSDOMPromiseDeferred.h"
+#include "SuspendableTaskQueue.h"
namespace WebCore {
@@ -116,6 +117,7 @@
Ref<CSSFontFaceSet> m_backing;
HashMap<RefPtr<FontFace>, Vector<Ref<PendingPromise>>> m_pendingPromises;
ReadyPromise m_readyPromise;
+ UniqueRef<SuspendableTaskQueue> m_taskQueue;
bool m_isFirstLayoutDone { true };
};
Added: trunk/Source/WebCore/platform/SuspendableTaskQueue.cpp (0 => 250693)
--- trunk/Source/WebCore/platform/SuspendableTaskQueue.cpp (rev 0)
+++ trunk/Source/WebCore/platform/SuspendableTaskQueue.cpp 2019-10-04 00:37:54 UTC (rev 250693)
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "SuspendableTaskQueue.h"
+
+namespace WebCore {
+
+SuspendableTaskQueue::SuspendableTaskQueue(ScriptExecutionContext* context)
+ : ActiveDOMObject(context)
+ , m_taskQueue(context)
+ , m_isClosed(!context)
+{
+}
+
+void SuspendableTaskQueue::close()
+{
+ m_isClosed = true;
+ cancelAllTasks();
+}
+
+void SuspendableTaskQueue::cancelAllTasks()
+{
+ m_taskQueue.cancelAllTasks();
+ m_pendingTasks.clear();
+}
+
+void SuspendableTaskQueue::enqueueTask(TaskFunction&& task)
+{
+ if (m_isClosed)
+ return;
+
+ m_pendingTasks.append(WTFMove(task));
+
+ if (m_isSuspended)
+ return;
+
+ m_taskQueue.enqueueTask(std::bind(&SuspendableTaskQueue::runOneTask, this));
+}
+
+void SuspendableTaskQueue::runOneTask()
+{
+ ASSERT(!m_pendingTasks.isEmpty());
+ auto task = m_pendingTasks.takeFirst();
+ task();
+}
+
+const char* SuspendableTaskQueue::activeDOMObjectName() const
+{
+ return "SuspendableTaskQueue";
+}
+
+bool SuspendableTaskQueue::canSuspendForDocumentSuspension() const
+{
+ return true;
+}
+
+void SuspendableTaskQueue::stop()
+{
+ close();
+}
+
+void SuspendableTaskQueue::suspend(ReasonForSuspension)
+{
+ m_isSuspended = true;
+ m_taskQueue.cancelAllTasks();
+}
+
+void SuspendableTaskQueue::resume()
+{
+ m_isSuspended = false;
+ for (size_t i = 0; i < m_pendingTasks.size(); ++i)
+ m_taskQueue.enqueueTask(std::bind(&SuspendableTaskQueue::runOneTask, this));
+}
+
+} // namespace WebCore
Added: trunk/Source/WebCore/platform/SuspendableTaskQueue.h (0 => 250693)
--- trunk/Source/WebCore/platform/SuspendableTaskQueue.h (rev 0)
+++ trunk/Source/WebCore/platform/SuspendableTaskQueue.h 2019-10-04 00:37:54 UTC (rev 250693)
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2019 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include "ActiveDOMObject.h"
+#include "GenericTaskQueue.h"
+#include <wtf/UniqueRef.h>
+
+namespace WebCore {
+
+class SuspendableTaskQueue : public ActiveDOMObject {
+ WTF_MAKE_FAST_ALLOCATED;
+public:
+ static UniqueRef<SuspendableTaskQueue> create(ScriptExecutionContext* context)
+ {
+ auto taskQueue = makeUniqueRef<SuspendableTaskQueue>(context);
+ taskQueue->suspendIfNeeded();
+ return taskQueue;
+ }
+
+ using TaskFunction = WTF::Function<void()>;
+ void enqueueTask(TaskFunction&&);
+ void cancelAllTasks();
+
+ void close();
+ bool isClosed() const { return m_isClosed; }
+
+ bool hasPendingTasks() const { return m_pendingTasks.isEmpty(); }
+
+private:
+ friend UniqueRef<SuspendableTaskQueue> WTF::makeUniqueRefWithoutFastMallocCheck<SuspendableTaskQueue, WebCore::ScriptExecutionContext*&>(WebCore::ScriptExecutionContext*&);
+ explicit SuspendableTaskQueue(ScriptExecutionContext*);
+
+ void runOneTask();
+
+ // ActiveDOMObject.
+ const char* activeDOMObjectName() const final;
+ bool canSuspendForDocumentSuspension() const final;
+ void stop() final;
+ void suspend(ReasonForSuspension) final;
+ void resume() final;
+
+ GenericTaskQueue<ScriptExecutionContext> m_taskQueue;
+ Deque<TaskFunction> m_pendingTasks;
+ bool m_isClosed { false };
+ bool m_isSuspended { false };
+};
+
+} // namespace WebCore