bug 1191648 don't keep ScriptProcessorNode alive when it has no audioprocess listener r=padenot
authorKarl Tomlinson <karlt+@karlt.net>
Thu, 06 Aug 2015 19:28:10 +1200
changeset 277814 131c6f3f88d11d0c302b250389b42a7371c059c2
parent 277813 5e3e18ec3cae1963406a9c9ef1fbc0d8d24a56ce
child 277815 da775fa176393fcfbfd29d789fc70d4160418e17
push id8456
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:31:52 +0000
treeherdermozilla-aurora@7f2f0fb041b1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot
bugs1191648
milestone43.0a1
bug 1191648 don't keep ScriptProcessorNode alive when it has no audioprocess listener r=padenot
dom/media/webaudio/ScriptProcessorNode.cpp
dom/media/webaudio/ScriptProcessorNode.h
--- a/dom/media/webaudio/ScriptProcessorNode.cpp
+++ b/dom/media/webaudio/ScriptProcessorNode.cpp
@@ -504,17 +504,46 @@ ScriptProcessorNode::SizeOfExcludingThis
 }
 
 size_t
 ScriptProcessorNode::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
 {
   return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
 }
 
+void
+ScriptProcessorNode::EventListenerAdded(nsIAtom* aType)
+{
+  AudioNode::EventListenerAdded(aType);
+  if (aType == nsGkAtoms::onaudioprocess) {
+    UpdateConnectedStatus();
+  }
+}
+
+void
+ScriptProcessorNode::EventListenerRemoved(nsIAtom* aType)
+{
+  AudioNode::EventListenerRemoved(aType);
+  if (aType == nsGkAtoms::onaudioprocess) {
+    UpdateConnectedStatus();
+  }
+}
+
 JSObject*
 ScriptProcessorNode::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
   return ScriptProcessorNodeBinding::Wrap(aCx, this, aGivenProto);
 }
 
+void
+ScriptProcessorNode::UpdateConnectedStatus()
+{
+  bool isConnected = !(OutputNodes().IsEmpty() && OutputParams().IsEmpty());
+  if (isConnected && HasListenersFor(nsGkAtoms::onaudioprocess)) {
+    MarkActive();
+  } else {
+    MarkInactive();
+  }
+}
+
 } // namespace dom
 } // namespace mozilla
 
--- a/dom/media/webaudio/ScriptProcessorNode.h
+++ b/dom/media/webaudio/ScriptProcessorNode.h
@@ -23,41 +23,44 @@ public:
                       uint32_t aBufferSize,
                       uint32_t aNumberOfInputChannels,
                       uint32_t aNumberOfOutputChannels);
 
   NS_DECL_ISUPPORTS_INHERITED
 
   IMPL_EVENT_HANDLER(audioprocess)
 
+  virtual void EventListenerAdded(nsIAtom* aType) override;
+  virtual void EventListenerRemoved(nsIAtom* aType) override;
+
   virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
 
   virtual void Connect(AudioNode& aDestination, uint32_t aOutput,
                        uint32_t aInput, ErrorResult& aRv) override
   {
     AudioNode::Connect(aDestination, aOutput, aInput, aRv);
     if (!aRv.Failed()) {
-      MarkActive();
+      UpdateConnectedStatus();
     }
   }
 
   virtual void Connect(AudioParam& aDestination, uint32_t aOutput,
                        ErrorResult& aRv) override
   {
     AudioNode::Connect(aDestination, aOutput, aRv);
     if (!aRv.Failed()) {
-      MarkActive();
+      UpdateConnectedStatus();
     }
   }
 
   virtual void Disconnect(uint32_t aOutput, ErrorResult& aRv) override
   {
     AudioNode::Disconnect(aOutput, aRv);
-    if (!aRv.Failed() && OutputNodes().IsEmpty() && OutputParams().IsEmpty()) {
-      MarkInactive();
+    if (!aRv.Failed()) {
+      UpdateConnectedStatus();
     }
   }
 
   virtual void SetChannelCount(uint32_t aChannelCount, ErrorResult& aRv) override
   {
     if (aChannelCount != ChannelCount()) {
       aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
     }
@@ -86,20 +89,21 @@ public:
   virtual const char* NodeType() const override
   {
     return "ScriptProcessorNode";
   }
 
   virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override;
   virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override;
 
-protected:
+private:
   virtual ~ScriptProcessorNode();
 
-private:
+  void UpdateConnectedStatus();
+
   const uint32_t mBufferSize;
   const uint32_t mNumberOfOutputChannels;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif