Bug 1492014 add AudioWorkletImpl/PaintWorkletImpl subclasses r=baku
authorKarl Tomlinson <karlt+@karlt.net>
Thu, 20 Sep 2018 11:34:01 +0000
changeset 494595 e545854fd235c9080f7b004e13b044bc7bb6d27e
parent 494594 66eb950c3f8298ffea27a91c6ac925c333a1b4b5
child 494596 cb3fa704e1751459bd314a3387beaf8e83ce28e0
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1492014
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1492014 add AudioWorkletImpl/PaintWorkletImpl subclasses r=baku This will be useful for AudioWorklet-specific storage and behavior. PaintWorkletImpl is in layout/style, because it will be referenced from CSS.cpp in the same directory. Depends on D6108 Differential Revision: https://phabricator.services.mozilla.com/D6109
dom/base/nsGlobalWindowInner.cpp
dom/media/webaudio/AudioContext.cpp
dom/media/webaudio/AudioWorkletImpl.cpp
dom/media/webaudio/AudioWorkletImpl.h
dom/media/webaudio/moz.build
dom/worklet/WorkletImpl.cpp
dom/worklet/WorkletImpl.h
layout/style/PaintWorkletImpl.cpp
layout/style/PaintWorkletImpl.h
layout/style/moz.build
--- a/dom/base/nsGlobalWindowInner.cpp
+++ b/dom/base/nsGlobalWindowInner.cpp
@@ -97,16 +97,17 @@
 #include "mozilla/ThrottledEventQueue.h"
 #include "AudioChannelService.h"
 #include "nsAboutProtocolUtils.h"
 #include "nsCharTraits.h" // NS_IS_HIGH/LOW_SURROGATE
 #include "PostMessageEvent.h"
 #include "mozilla/dom/DocGroup.h"
 #include "mozilla/dom/TabGroup.h"
 #include "mozilla/StaticPrefs.h"
+#include "PaintWorkletImpl.h"
 
 // Interfaces Needed
 #include "nsIFrame.h"
 #include "nsCanvasFrame.h"
 #include "nsIWidget.h"
 #include "nsIWidgetListener.h"
 #include "nsIBaseWindow.h"
 #include "nsIDeviceSensors.h"
@@ -236,17 +237,16 @@
 #include "mozilla/dom/ImageBitmapBinding.h"
 #include "mozilla/dom/InstallTriggerBinding.h"
 #include "mozilla/dom/ServiceWorker.h"
 #include "mozilla/dom/ServiceWorkerRegistration.h"
 #include "mozilla/dom/ServiceWorkerRegistrationDescriptor.h"
 #include "mozilla/dom/U2F.h"
 #include "mozilla/dom/WebIDLGlobalNameHash.h"
 #include "mozilla/dom/Worklet.h"
-#include "mozilla/dom/WorkletImpl.h"
 #ifdef HAVE_SIDEBAR
 #include "mozilla/dom/ExternalBinding.h"
 #endif
 
 #ifdef MOZ_WEBSPEECH
 #include "mozilla/dom/SpeechSynthesis.h"
 #endif
 
@@ -7815,18 +7815,17 @@ nsGlobalWindowInner::GetPaintWorklet(Err
 {
   if (!mPaintWorklet) {
     nsIPrincipal* principal = GetPrincipal();
     if (!principal) {
       aRv.Throw(NS_ERROR_FAILURE);
       return nullptr;
     }
 
-    mPaintWorklet =
-      WorkletImpl::CreateWorklet(this, principal, WorkletImpl::ePaintWorklet);
+    mPaintWorklet = PaintWorkletImpl::CreateWorklet(this, principal);
   }
 
   return mPaintWorklet;
 }
 
 void
 nsGlobalWindowInner::GetRegionalPrefsLocales(nsTArray<nsString>& aLocales)
 {
--- a/dom/media/webaudio/AudioContext.cpp
+++ b/dom/media/webaudio/AudioContext.cpp
@@ -35,41 +35,40 @@
 #include "mozilla/dom/OfflineAudioContextBinding.h"
 #include "mozilla/dom/OscillatorNodeBinding.h"
 #include "mozilla/dom/PannerNodeBinding.h"
 #include "mozilla/dom/PeriodicWaveBinding.h"
 #include "mozilla/dom/Promise.h"
 #include "mozilla/dom/StereoPannerNodeBinding.h"
 #include "mozilla/dom/WaveShaperNodeBinding.h"
 #include "mozilla/dom/Worklet.h"
-#include "mozilla/dom/WorkletImpl.h"
 
 #include "AudioBuffer.h"
 #include "AudioBufferSourceNode.h"
 #include "AudioChannelService.h"
 #include "AudioDestinationNode.h"
 #include "AudioListener.h"
 #include "AudioNodeStream.h"
 #include "AudioStream.h"
+#include "AudioWorkletImpl.h"
 #include "AutoplayPolicy.h"
 #include "BiquadFilterNode.h"
 #include "ChannelMergerNode.h"
 #include "ChannelSplitterNode.h"
 #include "ConstantSourceNode.h"
 #include "ConvolverNode.h"
 #include "DelayNode.h"
 #include "DynamicsCompressorNode.h"
 #include "GainNode.h"
 #include "IIRFilterNode.h"
 #include "MediaElementAudioSourceNode.h"
 #include "MediaStreamAudioDestinationNode.h"
 #include "MediaStreamAudioSourceNode.h"
 #include "MediaStreamGraph.h"
 #include "nsContentUtils.h"
-#include "nsGlobalWindowInner.h"
 #include "nsNetCID.h"
 #include "nsNetUtil.h"
 #include "nsPIDOMWindow.h"
 #include "nsPrintfCString.h"
 #include "nsRFPService.h"
 #include "OscillatorNode.h"
 #include "PannerNode.h"
 #include "PeriodicWave.h"
@@ -587,30 +586,17 @@ AudioContext::Listener()
   }
   return mListener;
 }
 
 Worklet*
 AudioContext::GetAudioWorklet(ErrorResult& aRv)
 {
   if (!mWorklet) {
-    nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
-    if (NS_WARN_IF(!window)) {
-      aRv.Throw(NS_ERROR_FAILURE);
-      return nullptr;
-    }
-    nsCOMPtr<nsIPrincipal> principal =
-        nsGlobalWindowInner::Cast(window)->GetPrincipal();
-    if (NS_WARN_IF(!principal)) {
-      aRv.Throw(NS_ERROR_FAILURE);
-      return nullptr;
-    }
-
-    mWorklet =
-      WorkletImpl::CreateWorklet(window, principal, WorkletImpl::eAudioWorklet);
+    mWorklet = AudioWorkletImpl::CreateWorklet(this, aRv);
   }
 
   return mWorklet;
 }
 
 bool
 AudioContext::IsRunning() const
 {
new file mode 100644
--- /dev/null
+++ b/dom/media/webaudio/AudioWorkletImpl.cpp
@@ -0,0 +1,63 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+#include "AudioWorkletImpl.h"
+
+#include "AudioContext.h"
+#include "mozilla/dom/AudioWorkletBinding.h"
+#include "mozilla/dom/AudioWorkletGlobalScope.h"
+#include "mozilla/dom/Worklet.h"
+#include "mozilla/dom/WorkletThread.h"
+#include "nsGlobalWindowInner.h"
+
+namespace mozilla {
+
+/* static */ already_AddRefed<dom::Worklet>
+AudioWorkletImpl::CreateWorklet(dom::AudioContext* aContext, ErrorResult& aRv)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+
+  nsCOMPtr<nsPIDOMWindowInner> window = aContext->GetOwner();
+  if (NS_WARN_IF(!window)) {
+    aRv.Throw(NS_ERROR_FAILURE);
+    return nullptr;
+  }
+  nsCOMPtr<nsIPrincipal> principal =
+    nsGlobalWindowInner::Cast(window)->GetPrincipal();
+  if (NS_WARN_IF(!principal)) {
+    aRv.Throw(NS_ERROR_FAILURE);
+    return nullptr;
+  }
+
+  RefPtr<AudioWorkletImpl> impl = new AudioWorkletImpl(window, principal);
+  return MakeAndAddRef<dom::Worklet>(window, std::move(impl));
+}
+
+AudioWorkletImpl::AudioWorkletImpl(nsPIDOMWindowInner* aWindow,
+                                   nsIPrincipal* aPrincipal)
+  : WorkletImpl(aWindow, aPrincipal)
+{
+}
+
+AudioWorkletImpl::~AudioWorkletImpl() = default;
+
+JSObject*
+AudioWorkletImpl::WrapWorklet(JSContext* aCx, dom::Worklet* aWorklet,
+                              JS::Handle<JSObject*> aGivenProto)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  return dom::AudioWorklet_Binding::Wrap(aCx, aWorklet, aGivenProto);
+}
+
+already_AddRefed<dom::WorkletGlobalScope>
+AudioWorkletImpl::ConstructGlobalScope()
+{
+  dom::WorkletThread::AssertIsOnWorkletThread();
+
+  return MakeAndAddRef<dom::AudioWorkletGlobalScope>(this);
+}
+
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/media/webaudio/AudioWorkletImpl.h
@@ -0,0 +1,41 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+#ifndef AudioWorkletImpl_h
+#define AudioWorkletImpl_h
+
+#include "mozilla/dom/WorkletImpl.h"
+
+namespace mozilla {
+
+namespace dom {
+  class AudioContext;
+}
+
+class AudioWorkletImpl final : public WorkletImpl
+{
+public:
+  // Methods for parent thread only:
+
+  static already_AddRefed<dom::Worklet>
+  CreateWorklet(dom::AudioContext* aContext, ErrorResult& aRv);
+
+  JSObject*
+  WrapWorklet(JSContext* aCx, dom::Worklet* aWorklet,
+              JS::Handle<JSObject*> aGivenProto) override;
+
+protected:
+  // Execution thread only.
+  already_AddRefed<dom::WorkletGlobalScope> ConstructGlobalScope() override;
+
+private:
+  AudioWorkletImpl(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal);
+  ~AudioWorkletImpl();
+};
+
+} // namespace mozilla
+
+#endif // AudioWorkletImpl_h
--- a/dom/media/webaudio/moz.build
+++ b/dom/media/webaudio/moz.build
@@ -84,16 +84,17 @@ UNIFIED_SOURCES += [
     'AudioNode.cpp',
     'AudioNodeEngine.cpp',
     'AudioNodeExternalInputStream.cpp',
     'AudioNodeStream.cpp',
     'AudioParam.cpp',
     'AudioParamMap.cpp',
     'AudioProcessingEvent.cpp',
     'AudioScheduledSourceNode.cpp',
+    'AudioWorkletImpl.cpp',
     'AudioWorkletNode.cpp',
     'AudioWorkletProcessor.cpp',
     'BiquadFilterNode.cpp',
     'ChannelMergerNode.cpp',
     'ChannelSplitterNode.cpp',
     'ConstantSourceNode.cpp',
     'ConvolverNode.cpp',
     'DelayBuffer.cpp',
--- a/dom/worklet/WorkletImpl.cpp
+++ b/dom/worklet/WorkletImpl.cpp
@@ -1,23 +1,20 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
 
 #include "WorkletImpl.h"
 
-#include "AudioWorkletGlobalScope.h"
-#include "PaintWorkletGlobalScope.h"
 #include "Worklet.h"
 #include "WorkletThread.h"
 
 #include "mozilla/BasePrincipal.h"
-#include "mozilla/dom/AudioWorkletBinding.h"
 #include "mozilla/dom/DOMPrefs.h"
 #include "mozilla/dom/RegisterWorkletBindings.h"
 #include "mozilla/dom/WorkletBinding.h"
 
 namespace mozilla {
 
 // ---------------------------------------------------------------------------
 // WorkletLoadInfo
@@ -40,64 +37,38 @@ WorkletLoadInfo::WorkletLoadInfo(nsPIDOM
 WorkletLoadInfo::~WorkletLoadInfo()
 {
   MOZ_ASSERT(!mPrincipal || NS_IsMainThread());
 }
 
 // ---------------------------------------------------------------------------
 // WorkletImpl
 
-/* static */ already_AddRefed<dom::Worklet>
-WorkletImpl::CreateWorklet(nsPIDOMWindowInner* aWindow,
-                           nsIPrincipal* aPrincipal,
-                           WorkletType aWorkletType)
-{
-  MOZ_ASSERT(NS_IsMainThread());
-
-  RefPtr<WorkletImpl> impl = new WorkletImpl(aWindow, aPrincipal, aWorkletType);
-  return MakeAndAddRef<dom::Worklet>(aWindow, std::move(impl));
-}
-
 WorkletImpl::WorkletImpl(nsPIDOMWindowInner* aWindow,
-                         nsIPrincipal* aPrincipal,
-                         WorkletType aWorkletType)
+                         nsIPrincipal* aPrincipal)
   : mWorkletLoadInfo(aWindow, aPrincipal)
-  , mWorkletType(aWorkletType)
 {
 }
 
 WorkletImpl::~WorkletImpl() = default;
 
 JSObject*
 WorkletImpl::WrapWorklet(JSContext* aCx, dom::Worklet* aWorklet,
                          JS::Handle<JSObject*> aGivenProto)
 {
   MOZ_ASSERT(NS_IsMainThread());
-  if (mWorkletType == eAudioWorklet) {
-    return dom::AudioWorklet_Binding::Wrap(aCx, aWorklet, aGivenProto);
-  } else {
-    return dom::Worklet_Binding::Wrap(aCx, aWorklet, aGivenProto);
-  }
+  return dom::Worklet_Binding::Wrap(aCx, aWorklet, aGivenProto);
 }
 
 already_AddRefed<dom::WorkletGlobalScope>
 WorkletImpl::CreateGlobalScope(JSContext* aCx)
 {
   dom::WorkletThread::AssertIsOnWorkletThread();
 
-  RefPtr<dom::WorkletGlobalScope> scope;
-
-  switch (mWorkletType) {
-    case eAudioWorklet:
-      scope = new dom::AudioWorkletGlobalScope(this);
-      break;
-    case ePaintWorklet:
-      scope = new dom::PaintWorkletGlobalScope(this);
-      break;
-  }
+  RefPtr<dom::WorkletGlobalScope> scope = ConstructGlobalScope();
 
   JS::Rooted<JSObject*> global(aCx);
   NS_ENSURE_TRUE(scope->WrapGlobalObject(aCx, &global), nullptr);
 
   JSAutoRealm ar(aCx, global);
 
   // Init Web IDL bindings
   if (!dom::RegisterWorkletBindings(aCx, global)) {
--- a/dom/worklet/WorkletImpl.h
+++ b/dom/worklet/WorkletImpl.h
@@ -65,53 +65,44 @@ private:
  * are owned indefinitely by WorkletImpl because WorkletImpl is not cycle
  * collected.
  */
 class WorkletImpl
 {
 public:
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WorkletImpl);
 
-  enum WorkletType {
-    eAudioWorklet,
-    ePaintWorklet,
-  };
-
   // Methods for parent thread only:
 
-  static already_AddRefed<dom::Worklet>
-  CreateWorklet(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
-                WorkletType aWorkletType);
-
-  JSObject*
+  virtual JSObject*
   WrapWorklet(JSContext* aCx, dom::Worklet* aWorklet,
               JS::Handle<JSObject*> aGivenProto);
 
   dom::WorkletThread* GetOrCreateThread();
   void TerminateThread();
 
   // Execution thread only.
   already_AddRefed<dom::WorkletGlobalScope> CreateGlobalScope(JSContext* aCx);
 
   // Any thread.
 
   const WorkletLoadInfo& LoadInfo() const { return mWorkletLoadInfo; }
 
   // Use DispatchRunnable only when the thread is known to already exist.
   nsresult DispatchRunnable(already_AddRefed<nsIRunnable> aRunnable);
 
-private:
-  WorkletImpl(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal,
-              WorkletType aWorkletType);
-  ~WorkletImpl();
+protected:
+  WorkletImpl(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal);
+  virtual ~WorkletImpl();
+
+  virtual already_AddRefed<dom::WorkletGlobalScope> ConstructGlobalScope() = 0;
 
   // The only WorkletLoadInfo member modified is mPrincipal which is accessed
   // on only the parent thread.
   WorkletLoadInfo mWorkletLoadInfo;
-  const WorkletType mWorkletType;
 
   // Parent thread only.
   RefPtr<dom::WorkletThread> mWorkletThread;
 };
 
 } // namespace mozilla
 
 #endif // mozilla_dom_worklet_WorkletImpl_h
new file mode 100644
--- /dev/null
+++ b/layout/style/PaintWorkletImpl.cpp
@@ -0,0 +1,40 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+#include "PaintWorkletImpl.h"
+
+#include "mozilla/dom/PaintWorkletGlobalScope.h"
+#include "mozilla/dom/Worklet.h"
+#include "mozilla/dom/WorkletThread.h"
+
+namespace mozilla {
+
+/* static */ already_AddRefed<dom::Worklet>
+PaintWorkletImpl::CreateWorklet(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+
+  RefPtr<PaintWorkletImpl> impl = new PaintWorkletImpl(aWindow, aPrincipal);
+  return MakeAndAddRef<dom::Worklet>(aWindow, std::move(impl));
+}
+
+PaintWorkletImpl::PaintWorkletImpl(nsPIDOMWindowInner* aWindow,
+                                   nsIPrincipal* aPrincipal)
+  : WorkletImpl(aWindow, aPrincipal)
+{
+}
+
+PaintWorkletImpl::~PaintWorkletImpl() = default;
+
+already_AddRefed<dom::WorkletGlobalScope>
+PaintWorkletImpl::ConstructGlobalScope()
+{
+  dom::WorkletThread::AssertIsOnWorkletThread();
+
+  return MakeAndAddRef<dom::PaintWorkletGlobalScope>(this);
+}
+
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/layout/style/PaintWorkletImpl.h
@@ -0,0 +1,33 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
+
+#ifndef PaintWorkletImpl_h
+#define PaintWorkletImpl_h
+
+#include "mozilla/dom/WorkletImpl.h"
+
+namespace mozilla {
+
+class PaintWorkletImpl final : public WorkletImpl
+{
+public:
+  // Methods for parent thread only:
+
+  static already_AddRefed<dom::Worklet>
+  CreateWorklet(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal);
+
+protected:
+  // Execution thread only.
+  already_AddRefed<dom::WorkletGlobalScope> ConstructGlobalScope() override;
+
+private:
+  PaintWorkletImpl(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal);
+  ~PaintWorkletImpl();
+};
+
+} // namespace mozilla
+
+#endif // PaintWorkletImpl_h
--- a/layout/style/moz.build
+++ b/layout/style/moz.build
@@ -194,16 +194,17 @@ UNIFIED_SOURCES += [
     'nsICSSDeclaration.cpp',
     'nsMediaFeatures.cpp',
     'nsROCSSPrimitiveValue.cpp',
     'nsStyleCoord.cpp',
     'nsStyleStruct.cpp',
     'nsStyleTransformMatrix.cpp',
     'nsStyleUtil.cpp',
     'nsTransitionManager.cpp',
+    'PaintWorkletImpl.cpp',
     'PostTraversalTask.cpp',
     'PreloadedStyleSheet.cpp',
     'Rule.cpp',
     'ServoBindings.cpp',
     'ServoCSSParser.cpp',
     'ServoCSSRuleList.cpp',
     'ServoElementSnapshot.cpp',
     'ServoStyleSet.cpp',