bug 1391482 move AudioBuffer parameter checking to constructor r?padenot draft
authorKarl Tomlinson <karlt+@karlt.net>
Wed, 16 Aug 2017 17:38:43 +1200
changeset 654728 399c117de835624c39b0d0009b07a76275a044bb
parent 654727 6eeb02b9289db7d4d9a661746ba672bc861fcb82
child 654729 899023dedd2e9568df8bdf250973b158644ed804
push id76658
push userktomlinson@mozilla.com
push dateTue, 29 Aug 2017 06:20:42 +0000
reviewerspadenot
bugs1391482
milestone57.0a1
bug 1391482 move AudioBuffer parameter checking to constructor r?padenot for sharing with a new factory method in a future patch. MozReview-Commit-ID: LAtbRVttMh8
dom/media/webaudio/AudioBuffer.cpp
dom/media/webaudio/AudioBuffer.h
--- a/dom/media/webaudio/AudioBuffer.cpp
+++ b/dom/media/webaudio/AudioBuffer.cpp
@@ -154,20 +154,32 @@ AudioBufferMemoryTracker::CollectReports
     "Memory used by AudioBuffer objects (Web Audio).");
 
   return NS_OK;
 }
 
 AudioBuffer::AudioBuffer(nsPIDOMWindowInner* aWindow,
                          uint32_t aNumberOfChannels,
                          uint32_t aLength,
-                         float aSampleRate)
+                         float aSampleRate,
+                         ErrorResult& aRv)
   : mOwnerWindow(do_GetWeakReference(aWindow)),
     mSampleRate(aSampleRate)
 {
+  // Note that a buffer with zero channels is permitted here for the sake of
+  // AudioProcessingEvent, where channel counts must match parameters passed
+  // to createScriptProcessor(), one of which may be zero.
+  if (aSampleRate < WebAudioUtils::MinSampleRate ||
+      aSampleRate > WebAudioUtils::MaxSampleRate ||
+      aNumberOfChannels > WebAudioUtils::MaxChannelCount ||
+      !aLength || aLength > INT32_MAX) {
+    aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
+    return;
+  }
+
   mSharedChannels.mDuration = aLength;
   mJSChannels.SetLength(aNumberOfChannels);
   mozilla::HoldJSObjects(this);
   AudioBufferMemoryTracker::RegisterAudioBuffer(this);
 }
 
 AudioBuffer::~AudioBuffer()
 {
@@ -217,31 +229,22 @@ AudioBuffer::SetSharedChannels(
 /* static */ already_AddRefed<AudioBuffer>
 AudioBuffer::Create(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels,
                     uint32_t aLength, float aSampleRate,
                     already_AddRefed<ThreadSharedFloatArrayBufferList>
                       aInitialContents,
                     ErrorResult& aRv)
 {
   RefPtr<ThreadSharedFloatArrayBufferList> initialContents = aInitialContents;
-
-  // Note that a buffer with zero channels is permitted here for the sake of
-  // AudioProcessingEvent, where channel counts must match parameters passed
-  // to createScriptProcessor(), one of which may be zero.
-  if (aSampleRate < WebAudioUtils::MinSampleRate ||
-      aSampleRate > WebAudioUtils::MaxSampleRate ||
-      aNumberOfChannels > WebAudioUtils::MaxChannelCount ||
-      !aLength || aLength > INT32_MAX) {
-    aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
+  RefPtr<AudioBuffer> buffer =
+    new AudioBuffer(aWindow, aNumberOfChannels, aLength, aSampleRate, aRv);
+  if (aRv.Failed()) {
     return nullptr;
   }
 
-  RefPtr<AudioBuffer> buffer =
-    new AudioBuffer(aWindow, aNumberOfChannels, aLength, aSampleRate);
-
   if (initialContents) {
     MOZ_ASSERT(initialContents->GetChannels() == aNumberOfChannels);
     buffer->SetSharedChannels(initialContents.forget());
   }
 
   return buffer.forget();
 }
 
--- a/dom/media/webaudio/AudioBuffer.h
+++ b/dom/media/webaudio/AudioBuffer.h
@@ -108,17 +108,17 @@ public:
   /**
    * Returns a reference to an AudioChunk containing the sample data.
    * The AudioChunk can have a null buffer if there is no data.
    */
   const AudioChunk& GetThreadSharedChannelsForRate(JSContext* aContext);
 
 protected:
   AudioBuffer(nsPIDOMWindowInner* aWindow, uint32_t aNumberOfChannels,
-              uint32_t aLength, float aSampleRate);
+              uint32_t aLength, float aSampleRate, ErrorResult& aRv);
   ~AudioBuffer();
 
   void
   SetSharedChannels(already_AddRefed<ThreadSharedFloatArrayBufferList> aBuffer);
 
   bool RestoreJSChannelData(JSContext* aJSContext);
 
   already_AddRefed<ThreadSharedFloatArrayBufferList>