Lazily construct Navigator serviceWorker
authoryouenn@apple.com <youenn@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 8 Apr 2019 20:51:20 +0000 (20:51 +0000)
committeryouenn@apple.com <youenn@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 8 Apr 2019 20:51:20 +0000 (20:51 +0000)
https://bugs.webkit.org/show_bug.cgi?id=196692

Reviewed by Chris Dumez.

Make NavigatorBase a ContextDestructionObserver.
This allows to get the navigator script execution context.
Use it when creating the navigator ServiceWorkerContainer object.
For GC, introduce serviceWorkerIfAny which returns the container if created.
No JS observable change of behavior.
Covered by existing tests.

* bindings/js/JSNavigatorCustom.cpp:
(WebCore::JSNavigator::visitAdditionalChildren):
* bindings/js/JSWorkerNavigatorCustom.cpp:
(WebCore::JSWorkerNavigator::visitAdditionalChildren):
* page/NavigatorBase.cpp:
(WebCore::NavigatorBase::NavigatorBase):
(WebCore::NavigatorBase::serviceWorkerIfAny):
(WebCore::NavigatorBase::serviceWorker):
* page/NavigatorBase.h:

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

Source/WebCore/ChangeLog
Source/WebCore/bindings/js/JSNavigatorCustom.cpp
Source/WebCore/bindings/js/JSWorkerNavigatorCustom.cpp
Source/WebCore/page/NavigatorBase.cpp
Source/WebCore/page/NavigatorBase.h

index cfda389..0179b10 100644 (file)
@@ -1,3 +1,27 @@
+2019-04-08  Youenn Fablet  <youenn@apple.com>
+
+        Lazily construct Navigator serviceWorker
+        https://bugs.webkit.org/show_bug.cgi?id=196692
+
+        Reviewed by Chris Dumez.
+
+        Make NavigatorBase a ContextDestructionObserver.
+        This allows to get the navigator script execution context.
+        Use it when creating the navigator ServiceWorkerContainer object.
+        For GC, introduce serviceWorkerIfAny which returns the container if created.
+        No JS observable change of behavior.
+        Covered by existing tests.
+
+        * bindings/js/JSNavigatorCustom.cpp:
+        (WebCore::JSNavigator::visitAdditionalChildren):
+        * bindings/js/JSWorkerNavigatorCustom.cpp:
+        (WebCore::JSWorkerNavigator::visitAdditionalChildren):
+        * page/NavigatorBase.cpp:
+        (WebCore::NavigatorBase::NavigatorBase):
+        (WebCore::NavigatorBase::serviceWorkerIfAny):
+        (WebCore::NavigatorBase::serviceWorker):
+        * page/NavigatorBase.h:
+
 2019-04-08  Antti Koivisto  <antti@apple.com>
 
         Update touch-action region on property changes
index 2169900..f3ceee5 100644 (file)
@@ -31,7 +31,7 @@ namespace WebCore {
 void JSNavigator::visitAdditionalChildren(JSC::SlotVisitor& visitor)
 {
 #if ENABLE(SERVICE_WORKER)
-    visitor.addOpaqueRoot(&wrapped().serviceWorker());
+    visitor.addOpaqueRoot(wrapped().serviceWorkerIfExists());
 #else
     UNUSED_PARAM(visitor);
 #endif
index 0a1363c..fc963e0 100644 (file)
@@ -31,7 +31,7 @@ namespace WebCore {
 void JSWorkerNavigator::visitAdditionalChildren(JSC::SlotVisitor& visitor)
 {
 #if ENABLE(SERVICE_WORKER)
-    visitor.addOpaqueRoot(&wrapped().serviceWorker());
+    visitor.addOpaqueRoot(wrapped().serviceWorkerIfExists());
 #else
     UNUSED_PARAM(visitor);
 #endif
index da8794d..c013685 100644 (file)
@@ -28,6 +28,7 @@
 #include "NavigatorBase.h"
 
 #include "Document.h"
+#include "RuntimeEnabledFeatures.h"
 #include "ServiceWorkerContainer.h"
 #include <mutex>
 #include <wtf/Language.h>
 namespace WebCore {
 
 NavigatorBase::NavigatorBase(ScriptExecutionContext* context)
-#if ENABLE(SERVICE_WORKER)
-    : m_serviceWorkerContainer(makeUniqueRef<ServiceWorkerContainer>(context, *this))
-#endif
+    : ContextDestructionObserver(context)
 {
-#if !ENABLE(SERVICE_WORKER)
-    UNUSED_PARAM(context);
-#endif
 }
 
 NavigatorBase::~NavigatorBase() = default;
@@ -151,16 +147,24 @@ Vector<String> NavigatorBase::languages()
 }
 
 #if ENABLE(SERVICE_WORKER)
+ServiceWorkerContainer* NavigatorBase::serviceWorkerIfExists()
+{
+    return m_serviceWorkerContainer.get();
+}
+
 ServiceWorkerContainer& NavigatorBase::serviceWorker()
 {
-    return m_serviceWorkerContainer;
+    ASSERT(RuntimeEnabledFeatures::sharedFeatures().serviceWorkerEnabled());
+    if (!m_serviceWorkerContainer)
+        m_serviceWorkerContainer = std::make_unique<ServiceWorkerContainer>(scriptExecutionContext(), *this);
+    return *m_serviceWorkerContainer;
 }
 
 ExceptionOr<ServiceWorkerContainer&> NavigatorBase::serviceWorker(ScriptExecutionContext& context)
 {
     if (is<Document>(context) && downcast<Document>(context).isSandboxed(SandboxOrigin))
         return Exception { SecurityError, "Service Worker is disabled because the context is sandboxed and lacks the 'allow-same-origin' flag" };
-    return m_serviceWorkerContainer.get();
+    return serviceWorker();
 }
 #endif
 
index 5af5881..1a06c4e 100644 (file)
@@ -25,6 +25,7 @@
 
 #pragma once
 
+#include "ContextDestructionObserver.h"
 #include "ExceptionOr.h"
 #include <wtf/Forward.h>
 #include <wtf/RefCounted.h>
@@ -36,7 +37,7 @@ namespace WebCore {
 class ScriptExecutionContext;
 class ServiceWorkerContainer;
 
-class NavigatorBase : public RefCounted<NavigatorBase> {
+class NavigatorBase : public RefCounted<NavigatorBase>, public ContextDestructionObserver {
 public:
     virtual ~NavigatorBase();
 
@@ -61,11 +62,12 @@ protected:
 
 #if ENABLE(SERVICE_WORKER)
 public:
+    ServiceWorkerContainer* serviceWorkerIfExists();
     ServiceWorkerContainer& serviceWorker();
     ExceptionOr<ServiceWorkerContainer&> serviceWorker(ScriptExecutionContext&);
 
 private:
-    UniqueRef<ServiceWorkerContainer> m_serviceWorkerContainer;
+    std::unique_ptr<ServiceWorkerContainer> m_serviceWorkerContainer;
 #endif
 };