tor-browser

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

BufferMediaResource.h (2499B)


      1 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #if !defined(BufferMediaResource_h_)
      7 #  define BufferMediaResource_h_
      8 
      9 #  include <algorithm>
     10 
     11 #  include "MediaResource.h"
     12 #  include "nsISeekableStream.h"
     13 
     14 namespace mozilla {
     15 
     16 DDLoggedTypeDeclNameAndBase(BufferMediaResource, MediaResource);
     17 
     18 // A simple MediaResource based on an in memory buffer.  This class accepts
     19 // the address and the length of the buffer, and simulates a read/seek API
     20 // on top of it.  The Read implementation involves copying memory, which is
     21 // unfortunate, but the MediaResource interface mandates that.
     22 class BufferMediaResource
     23    : public MediaResource,
     24      public DecoderDoctorLifeLogger<BufferMediaResource> {
     25 public:
     26  BufferMediaResource(const uint8_t* aBuffer, uint32_t aLength)
     27      : mBuffer(aBuffer), mLength(aLength) {}
     28 
     29 protected:
     30  virtual ~BufferMediaResource() = default;
     31 
     32 private:
     33  // These methods are called off the main thread.
     34  nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount,
     35                  uint32_t* aBytes) override {
     36    if (aOffset < 0 || aOffset > mLength) {
     37      return NS_ERROR_FAILURE;
     38    }
     39    *aBytes = std::min(mLength - static_cast<uint32_t>(aOffset), aCount);
     40    memcpy(aBuffer, mBuffer + aOffset, *aBytes);
     41    return NS_OK;
     42  }
     43  // Memory-based and no locks, caching discouraged.
     44  bool ShouldCacheReads() override { return false; }
     45 
     46  void Pin() override {}
     47  void Unpin() override {}
     48  int64_t GetLength() override { return mLength; }
     49  int64_t GetNextCachedData(int64_t aOffset) override { return aOffset; }
     50  int64_t GetCachedDataEnd(int64_t aOffset) override {
     51    return std::max(aOffset, int64_t(mLength));
     52  }
     53  bool IsDataCachedToEndOfResource(int64_t aOffset) override { return true; }
     54  nsresult ReadFromCache(char* aBuffer, int64_t aOffset,
     55                         uint32_t aCount) override {
     56    if (aOffset < 0) {
     57      return NS_ERROR_FAILURE;
     58    }
     59 
     60    uint32_t bytes = std::min(mLength - static_cast<uint32_t>(aOffset), aCount);
     61    memcpy(aBuffer, mBuffer + aOffset, bytes);
     62    return NS_OK;
     63  }
     64 
     65  nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override {
     66    aRanges += MediaByteRange(0, int64_t(mLength));
     67    return NS_OK;
     68  }
     69 
     70 private:
     71  const uint8_t* mBuffer;
     72  uint32_t mLength;
     73 };
     74 
     75 }  // namespace mozilla
     76 
     77 #endif