tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SourceBufferResource.h (4479B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      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/. */
      6 
      7 #ifndef MOZILLA_SOURCEBUFFERRESOURCE_H_
      8 #define MOZILLA_SOURCEBUFFERRESOURCE_H_
      9 
     10 #include "MediaResource.h"
     11 #include "ResourceQueue.h"
     12 #include "mozilla/AbstractThread.h"
     13 #include "mozilla/Logging.h"
     14 
     15 #define UNIMPLEMENTED()                               \
     16  { /* Logging this is too spammy to do by default */ \
     17  }
     18 
     19 namespace mozilla {
     20 
     21 class MediaByteBuffer;
     22 class AbstractThread;
     23 
     24 namespace dom {
     25 
     26 class SourceBuffer;
     27 
     28 }  // namespace dom
     29 
     30 DDLoggedTypeDeclNameAndBase(SourceBufferResource, MediaResource);
     31 
     32 // SourceBufferResource is not thread safe.
     33 class SourceBufferResource final
     34    : public MediaResource,
     35      public DecoderDoctorLifeLogger<SourceBufferResource> {
     36 public:
     37  SourceBufferResource();
     38  RefPtr<GenericPromise> Close() override;
     39  nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount,
     40                  uint32_t* aBytes) override;
     41  // Memory-based and no locks, caching discouraged.
     42  bool ShouldCacheReads() override { return false; }
     43  void Pin() override { UNIMPLEMENTED(); }
     44  void Unpin() override { UNIMPLEMENTED(); }
     45  int64_t GetLength() override {
     46    if (mEnded) {
     47      return mInputBuffer.GetLength();
     48    } else {
     49      return -1;
     50    }
     51  }
     52  int64_t GetNextCachedData(int64_t aOffset) override {
     53    MOZ_ASSERT(OnThread());
     54    MOZ_ASSERT(aOffset >= 0);
     55    if (uint64_t(aOffset) < mInputBuffer.GetOffset()) {
     56      return mInputBuffer.GetOffset();
     57    } else if (static_cast<uint64_t>(aOffset) == mInputBuffer.GetLength()) {
     58      return -1;
     59    }
     60    return aOffset;
     61  }
     62  int64_t GetCachedDataEnd(int64_t aOffset) override {
     63    MOZ_ASSERT(OnThread());
     64    MOZ_ASSERT(aOffset >= 0);
     65    if (uint64_t(aOffset) < mInputBuffer.GetOffset() ||
     66        static_cast<uint64_t>(aOffset) >= mInputBuffer.GetLength()) {
     67      // aOffset is outside of the buffered range.
     68      return aOffset;
     69    }
     70    return mInputBuffer.GetLength();
     71  }
     72  bool IsDataCachedToEndOfResource(int64_t aOffset) override { return false; }
     73  nsresult ReadFromCache(char* aBuffer, int64_t aOffset,
     74                         uint32_t aCount) override;
     75 
     76  nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override {
     77    MOZ_ASSERT(OnThread());
     78    if (mInputBuffer.GetLength()) {
     79      aRanges +=
     80          MediaByteRange(mInputBuffer.GetOffset(), mInputBuffer.GetLength());
     81    }
     82    return NS_OK;
     83  }
     84 
     85  size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
     86    MOZ_ASSERT(OnThread());
     87    return mInputBuffer.SizeOfExcludingThis(aMallocSizeOf);
     88  }
     89 
     90  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
     91    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
     92  }
     93 
     94  // Used by SourceBuffer.
     95  void AppendData(MediaByteBuffer* aData);
     96  void AppendData(const MediaSpan& aData);
     97  void Ended();
     98  bool IsEnded() {
     99    MOZ_ASSERT(OnThread());
    100    return mEnded;
    101  }
    102  // Remove data from resource if it holds more than the threshold reduced by
    103  // the given number of bytes. Returns amount evicted.
    104  uint32_t EvictData(uint64_t aPlaybackOffset, int64_t aThresholdReduct);
    105 
    106  // Remove data from resource before the given offset.
    107  void EvictBefore(uint64_t aOffset);
    108 
    109  // Remove all data from the resource
    110  uint32_t EvictAll();
    111 
    112  // Returns the amount of data currently retained by this resource.
    113  int64_t GetSize() {
    114    MOZ_ASSERT(OnThread());
    115    return mInputBuffer.GetLength() - mInputBuffer.GetOffset();
    116  }
    117 
    118  const uint8_t* GetContiguousAccess(int64_t aOffset, size_t aSize) {
    119    return mInputBuffer.GetContiguousAccess(aOffset, aSize);
    120  }
    121 
    122 #if defined(DEBUG)
    123  void Dump(const char* aPath) { mInputBuffer.Dump(aPath); }
    124 #endif
    125 
    126 private:
    127  virtual ~SourceBufferResource();
    128  nsresult ReadAtInternal(int64_t aOffset, char* aBuffer, uint32_t aCount,
    129                          uint32_t* aBytes);
    130 
    131 #if defined(DEBUG)
    132  const RefPtr<AbstractThread> mThread;
    133  // TaskQueue methods and objects.
    134  const AbstractThread* GetThread() const;
    135  bool OnThread() const;
    136 #endif
    137 
    138  // The buffer holding resource data.
    139  ResourceQueue mInputBuffer;
    140 
    141  bool mClosed = false;
    142  bool mEnded = false;
    143 };
    144 
    145 }  // namespace mozilla
    146 
    147 #undef UNIMPLEMENTED
    148 
    149 #endif /* MOZILLA_SOURCEBUFFERRESOURCE_H_ */