Title: [250379] trunk
Revision
250379
Author
[email protected]
Date
2019-09-25 22:32:00 -0700 (Wed, 25 Sep 2019)

Log Message

Add very basic PageCache support for RTCPeerConnection
https://bugs.webkit.org/show_bug.cgi?id=202244

Reviewed by Geoffrey Garen.

Source/WebCore:

RTCPeerConnection::canSuspendForDocumentSuspension() returns true if
RTCPeerConnection::hasPendingActivity() return false. Previously, hasPendingActivity()
would return true unless the JS has called close() on the RTCPeerConnection
object.

On weather.com, an ad would construct an RTCPeerConnection just to do some feature
checking and then let the variable go out of scope (without calling close()). Because
of the previous implementation of RTCPeerConnection::hasPendingActivity(), this wrapper
and its implementation object would stay alive forever AND prevent the page from
entering PageCache on navigation.

To improve this, the implementation of hadPendingActivity() has been updated so that
it keeps returning false if close() has been called, but will also return false if
there are no pending Promises to be resolved and no event listeners.

Test: fast/mediastream/RTCPeerConnection-page-cache.html

* Modules/mediastream/RTCPeerConnection.cpp:
WebCore::RTCPeerConnection::RTCPeerConnection:
Stop taking a pending activity in the constructor, this would be NOT keep the wrapper
alive since RTCPeerConnection::hasPendingActivity() was not checking
ActiveDOMObject::hasPendingActivity().

(WebCore::RTCPeerConnection::canSuspendForDocumentSuspension const):
(WebCore::RTCPeerConnection::hasPendingActivity const):

* Modules/mediastream/RTCPeerConnection.h:

* bindings/js/JSDOMPromiseDeferred.h:
(WebCore::DOMPromiseDeferredBase::whenSettled):

LayoutTests:

Add layout test coverage.

* fast/mediastream/RTCPeerConnection-page-cache-expected.txt: Added.
* fast/mediastream/RTCPeerConnection-page-cache.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (250378 => 250379)


--- trunk/LayoutTests/ChangeLog	2019-09-26 04:30:39 UTC (rev 250378)
+++ trunk/LayoutTests/ChangeLog	2019-09-26 05:32:00 UTC (rev 250379)
@@ -1,5 +1,17 @@
 2019-09-25  Chris Dumez  <[email protected]>
 
+        Add very basic PageCache support for RTCPeerConnection
+        https://bugs.webkit.org/show_bug.cgi?id=202244
+
+        Reviewed by Geoffrey Garen.
+
+        Add layout test coverage.
+
+        * fast/mediastream/RTCPeerConnection-page-cache-expected.txt: Added.
+        * fast/mediastream/RTCPeerConnection-page-cache.html: Added.
+
+2019-09-25  Chris Dumez  <[email protected]>
+
         Improve Service worker support for Page Caching
         https://bugs.webkit.org/show_bug.cgi?id=202221
 

Added: trunk/LayoutTests/fast/mediastream/RTCPeerConnection-page-cache-expected.txt (0 => 250379)


--- trunk/LayoutTests/fast/mediastream/RTCPeerConnection-page-cache-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/mediastream/RTCPeerConnection-page-cache-expected.txt	2019-09-26 05:32:00 UTC (rev 250379)
@@ -0,0 +1,13 @@
+Basic testing for RTCPeerConnection Page Caching.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+pageshow - not from page cache
+pagehide - entering page cache
+pageshow - from page cache
+PASS Page entered page cache
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/fast/mediastream/RTCPeerConnection-page-cache.html (0 => 250379)


--- trunk/LayoutTests/fast/mediastream/RTCPeerConnection-page-cache.html	                        (rev 0)
+++ trunk/LayoutTests/fast/mediastream/RTCPeerConnection-page-cache.html	2019-09-26 05:32:00 UTC (rev 250379)
@@ -0,0 +1,34 @@
+<!-- webkit-test-runner [ enablePageCache=true ] -->
+<!DOCTYPE html>
+<html>
+<body>
+<script src=""
+<script>
+description("Basic testing for RTCPeerConnection Page Caching.");
+jsTestIsAsync = true;
+
+window.addEventListener("pageshow", function(event) {
+  debug("pageshow - " + (event.persisted ? "" : "not ") + "from page cache");
+  if (event.persisted) {
+      testPassed("Page entered page cache");
+      finishJSTest();
+  }
+});
+
+window.addEventListener("pagehide", function(event) {
+    debug("pagehide - " + (event.persisted ? "" : "not ") + "entering page cache");
+    if (!event.persisted) {
+        testFailed("Page failed to enter page cache");
+        finishJSTest();
+    }
+});
+
+_onload_ = () => {
+    connection = new RTCPeerConnection();
+    setTimeout(() => {
+        window.location = "../history/resources/page-cache-helper.html";
+    }, 0);
+};
+</script>
+</body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (250378 => 250379)


--- trunk/Source/WebCore/ChangeLog	2019-09-26 04:30:39 UTC (rev 250378)
+++ trunk/Source/WebCore/ChangeLog	2019-09-26 05:32:00 UTC (rev 250379)
@@ -1,5 +1,43 @@
 2019-09-25  Chris Dumez  <[email protected]>
 
+        Add very basic PageCache support for RTCPeerConnection
+        https://bugs.webkit.org/show_bug.cgi?id=202244
+
+        Reviewed by Geoffrey Garen.
+
+        RTCPeerConnection::canSuspendForDocumentSuspension() returns true if
+        RTCPeerConnection::hasPendingActivity() return false. Previously, hasPendingActivity()
+        would return true unless the JS has called close() on the RTCPeerConnection
+        object.
+
+        On weather.com, an ad would construct an RTCPeerConnection just to do some feature
+        checking and then let the variable go out of scope (without calling close()). Because
+        of the previous implementation of RTCPeerConnection::hasPendingActivity(), this wrapper
+        and its implementation object would stay alive forever AND prevent the page from
+        entering PageCache on navigation.
+
+        To improve this, the implementation of hadPendingActivity() has been updated so that
+        it keeps returning false if close() has been called, but will also return false if
+        there are no pending Promises to be resolved and no event listeners.
+
+        Test: fast/mediastream/RTCPeerConnection-page-cache.html
+
+        * Modules/mediastream/RTCPeerConnection.cpp:
+        WebCore::RTCPeerConnection::RTCPeerConnection:
+        Stop taking a pending activity in the constructor, this would be NOT keep the wrapper
+        alive since RTCPeerConnection::hasPendingActivity() was not checking
+        ActiveDOMObject::hasPendingActivity().
+
+        (WebCore::RTCPeerConnection::canSuspendForDocumentSuspension const):
+        (WebCore::RTCPeerConnection::hasPendingActivity const):
+        
+        * Modules/mediastream/RTCPeerConnection.h:
+
+        * bindings/js/JSDOMPromiseDeferred.h:
+        (WebCore::DOMPromiseDeferredBase::whenSettled):
+
+2019-09-25  Chris Dumez  <[email protected]>
+
         Improve Service worker support for Page Caching
         https://bugs.webkit.org/show_bug.cgi?id=202221
 

Modified: trunk/Source/WebCore/Modules/mediastream/RTCPeerConnection.cpp (250378 => 250379)


--- trunk/Source/WebCore/Modules/mediastream/RTCPeerConnection.cpp	2019-09-26 04:30:39 UTC (rev 250378)
+++ trunk/Source/WebCore/Modules/mediastream/RTCPeerConnection.cpp	2019-09-26 05:32:00 UTC (rev 250379)
@@ -70,10 +70,7 @@
     auto& document = downcast<Document>(context);
     auto peerConnection = adoptRef(*new RTCPeerConnection(document));
     peerConnection->suspendIfNeeded();
-    // RTCPeerConnection may send events at about any time during its lifetime.
-    // Let's make it uncollectable until the pc is closed by JS or the page stops it.
     if (!peerConnection->isClosed()) {
-        peerConnection->m_pendingActivity = peerConnection->makePendingActivity(peerConnection.get());
         if (auto* page = document.page()) {
             peerConnection->registerToController(page->rtcController());
             page->libWebRTCProvider().setEnableLogging(!page->sessionID().isEphemeral());
@@ -197,6 +194,7 @@
         return;
     }
 
+    addPendingPromise(promise);
     m_backend->createOffer(WTFMove(options), WTFMove(promise));
 }
 
@@ -208,6 +206,7 @@
         return;
     }
 
+    addPendingPromise(promise);
     m_backend->createAnswer(WTFMove(options), WTFMove(promise));
 }
 
@@ -219,6 +218,7 @@
         return;
     }
 
+    addPendingPromise(promise);
     m_backend->setLocalDescription(description, WTFMove(promise));
 }
 
@@ -245,6 +245,7 @@
         promise.reject(InvalidStateError);
         return;
     }
+    addPendingPromise(promise);
     m_backend->setRemoteDescription(description, WTFMove(promise));
 }
 
@@ -272,6 +273,7 @@
         return;
     }
 
+    addPendingPromise(promise);
     m_backend->addIceCandidate(rtcCandidate, WTFMove(promise));
 }
 
@@ -399,6 +401,7 @@
             }
         }
     }
+    addPendingPromise(promise.get());
     m_backend->getStats(WTFMove(promise));
 }
 
@@ -474,7 +477,6 @@
     m_isStopped = true;
 
     m_backend->stop();
-    m_pendingActivity = nullptr;
 }
 
 void RTCPeerConnection::registerToController(RTCController& controller)
@@ -494,6 +496,7 @@
     return "RTCPeerConnection";
 }
 
+    // FIXME: We should do better here, it is way too easy to prevent PageCache.
 bool RTCPeerConnection::canSuspendForDocumentSuspension() const
 {
     return !hasPendingActivity();
@@ -501,7 +504,15 @@
 
 bool RTCPeerConnection::hasPendingActivity() const
 {
-    return !m_isStopped;
+    if (m_isStopped)
+        return false;
+
+    // This returns true if we have pending promises to be resolved.
+    if (ActiveDOMObject::hasPendingActivity())
+        return true;
+
+    // As long as the connection is not stopped and it has event listeners, it may dispatch events.
+    return hasEventListeners();
 }
 
 void RTCPeerConnection::addTransceiver(Ref<RTCRtpTransceiver>&& transceiver)

Modified: trunk/Source/WebCore/Modules/mediastream/RTCPeerConnection.h (250378 => 250379)


--- trunk/Source/WebCore/Modules/mediastream/RTCPeerConnection.h	2019-09-26 04:30:39 UTC (rev 250378)
+++ trunk/Source/WebCore/Modules/mediastream/RTCPeerConnection.h	2019-09-26 05:32:00 UTC (rev 250379)
@@ -185,6 +185,11 @@
 #endif
 
 private:
+    template<typename PromiseType> void addPendingPromise(PromiseType& promise)
+    {
+        promise.whenSettled([pendingActivity = makePendingActivity(*this)] { });
+    }
+
     RTCPeerConnection(Document&);
 
     ExceptionOr<void> initializeConfiguration(RTCConfiguration&&);
@@ -230,7 +235,6 @@
     RTCConfiguration m_configuration;
     RTCController* m_controller { nullptr };
     Vector<RefPtr<RTCCertificate>> m_certificates;
-    RefPtr<PendingActivity<RTCPeerConnection>> m_pendingActivity;
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/bindings/js/JSDOMPromise.cpp (250378 => 250379)


--- trunk/Source/WebCore/bindings/js/JSDOMPromise.cpp	2019-09-26 04:30:39 UTC (rev 250378)
+++ trunk/Source/WebCore/bindings/js/JSDOMPromise.cpp	2019-09-26 05:32:00 UTC (rev 250379)
@@ -57,7 +57,7 @@
     whenPromiseIsSettled(globalObject(), promise(), WTFMove(callback));
 }
 
-void DOMPromise::whenPromiseIsSettled(JSDOMGlobalObject* globalObject, JSC::JSObject* promise, std::function<void()>&& callback)
+void DOMPromise::whenPromiseIsSettled(JSDOMGlobalObject* globalObject, JSC::JSObject* promise, Function<void()>&& callback)
 {
     auto& state = *globalObject->globalExec();
     auto& vm = state.vm();

Modified: trunk/Source/WebCore/bindings/js/JSDOMPromise.h (250378 => 250379)


--- trunk/Source/WebCore/bindings/js/JSDOMPromise.h	2019-09-26 04:30:39 UTC (rev 250378)
+++ trunk/Source/WebCore/bindings/js/JSDOMPromise.h	2019-09-26 05:32:00 UTC (rev 250379)
@@ -50,7 +50,7 @@
     enum class Status { Pending, Fulfilled, Rejected };
     Status status() const;
 
-    static void whenPromiseIsSettled(JSDOMGlobalObject*, JSC::JSObject* promise, std::function<void()>&&);
+    static void whenPromiseIsSettled(JSDOMGlobalObject*, JSC::JSObject* promise, Function<void()>&&);
 
 private:
     DOMPromise(JSDOMGlobalObject& globalObject, JSC::JSPromise& promise)

Modified: trunk/Source/WebCore/bindings/js/JSDOMPromiseDeferred.cpp (250378 => 250379)


--- trunk/Source/WebCore/bindings/js/JSDOMPromiseDeferred.cpp	2019-09-26 04:30:39 UTC (rev 250378)
+++ trunk/Source/WebCore/bindings/js/JSDOMPromiseDeferred.cpp	2019-09-26 05:32:00 UTC (rev 250379)
@@ -69,7 +69,7 @@
         clear();
 }
 
-void DeferredPromise::whenSettled(std::function<void()>&& callback)
+void DeferredPromise::whenSettled(Function<void()>&& callback)
 {
     DOMPromise::whenPromiseIsSettled(globalObject(), deferred()->promise(), WTFMove(callback));
 }

Modified: trunk/Source/WebCore/bindings/js/JSDOMPromiseDeferred.h (250378 => 250379)


--- trunk/Source/WebCore/bindings/js/JSDOMPromiseDeferred.h	2019-09-26 04:30:39 UTC (rev 250378)
+++ trunk/Source/WebCore/bindings/js/JSDOMPromiseDeferred.h	2019-09-26 05:32:00 UTC (rev 250379)
@@ -146,7 +146,7 @@
 
     JSC::JSValue promise() const;
 
-    void whenSettled(std::function<void()>&&);
+    void whenSettled(Function<void()>&&);
 
 private:
     DeferredPromise(JSDOMGlobalObject& globalObject, JSC::JSPromiseDeferred& deferred, Mode mode)
@@ -213,6 +213,11 @@
 
     JSC::JSValue promise() const { return m_promiseDeferred->promise(); };
 
+    void whenSettled(Function<void()>&& function)
+    {
+        m_promiseDeferred->whenSettled(WTFMove(function));
+    }
+
 protected:
     Ref<DeferredPromise> m_promiseDeferred;
 };
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to