content/media/webaudio/AudioBuffer.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef AudioBuffer_h_
     8 #define AudioBuffer_h_
    10 #include "nsWrapperCache.h"
    11 #include "nsCycleCollectionParticipant.h"
    12 #include "mozilla/Attributes.h"
    13 #include "nsAutoPtr.h"
    14 #include "nsTArray.h"
    15 #include "AudioContext.h"
    16 #include "js/TypeDecls.h"
    17 #include "mozilla/MemoryReporting.h"
    19 namespace mozilla {
    21 class ErrorResult;
    22 class ThreadSharedFloatArrayBufferList;
    24 namespace dom {
    26 class AudioContext;
    28 /**
    29  * An AudioBuffer keeps its data either in the mJSChannels objects, which
    30  * are Float32Arrays, or in mSharedChannels if the mJSChannels objects have
    31  * been neutered.
    32  */
    33 class AudioBuffer MOZ_FINAL : public nsWrapperCache
    34 {
    35 public:
    36   static already_AddRefed<AudioBuffer>
    37   Create(AudioContext* aContext, uint32_t aNumberOfChannels,
    38          uint32_t aLength, float aSampleRate,
    39          JSContext* aJSContext, ErrorResult& aRv);
    41   size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
    43   NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(AudioBuffer)
    44   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(AudioBuffer)
    46   AudioContext* GetParentObject() const
    47   {
    48     return mContext;
    49   }
    51   virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
    53   float SampleRate() const
    54   {
    55     return mSampleRate;
    56   }
    58   int32_t Length() const
    59   {
    60     return mLength;
    61   }
    63   double Duration() const
    64   {
    65     return mLength / static_cast<double> (mSampleRate);
    66   }
    68   uint32_t NumberOfChannels() const
    69   {
    70     return mJSChannels.Length();
    71   }
    73   /**
    74    * If mSharedChannels is non-null, copies its contents to
    75    * new Float32Arrays in mJSChannels. Returns a Float32Array.
    76    */
    77   void GetChannelData(JSContext* aJSContext, uint32_t aChannel,
    78                       JS::MutableHandle<JSObject*> aRetval,
    79                       ErrorResult& aRv);
    81   void CopyFromChannel(const Float32Array& aDestination, uint32_t aChannelNumber,
    82                        uint32_t aStartInChannel, ErrorResult& aRv);
    83   void CopyToChannel(JSContext* aJSContext, const Float32Array& aSource,
    84                      uint32_t aChannelNumber, uint32_t aStartInChannel,
    85                      ErrorResult& aRv);
    87   /**
    88    * Returns a ThreadSharedFloatArrayBufferList containing the sample data.
    89    * Can return null if there is no data.
    90    */
    91   ThreadSharedFloatArrayBufferList* GetThreadSharedChannelsForRate(JSContext* aContext);
    93   // This replaces the contents of the JS array for the given channel.
    94   // This function needs to be called on an AudioBuffer which has not been
    95   // handed off to the content yet, and right after the object has been
    96   // initialized.
    97   void SetRawChannelContents(JSContext* aJSContext,
    98                              uint32_t aChannel,
    99                              float* aContents);
   101 protected:
   102   AudioBuffer(AudioContext* aContext, uint32_t aNumberOfChannels,
   103               uint32_t aLength, float aSampleRate);
   104   ~AudioBuffer();
   106   bool RestoreJSChannelData(JSContext* aJSContext);
   107   void ClearJSChannels();
   109   nsRefPtr<AudioContext> mContext;
   110   // Float32Arrays
   111   nsAutoTArray<JS::Heap<JSObject*>, 2> mJSChannels;
   113   // mSharedChannels aggregates the data from mJSChannels. This is non-null
   114   // if and only if the mJSChannels are neutered.
   115   nsRefPtr<ThreadSharedFloatArrayBufferList> mSharedChannels;
   117   uint32_t mLength;
   118   float mSampleRate;
   119 };
   121 }
   122 }
   124 #endif

mercurial