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
--- 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>