content/media/mediasource/SourceBufferResource.cpp

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.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "SourceBufferResource.h"
michael@0 8
michael@0 9 #include <string.h>
michael@0 10 #include <algorithm>
michael@0 11
michael@0 12 #include "nsISeekableStream.h"
michael@0 13 #include "nsISupportsImpl.h"
michael@0 14 #include "prenv.h"
michael@0 15 #include "prlog.h"
michael@0 16
michael@0 17 #ifdef PR_LOGGING
michael@0 18 extern PRLogModuleInfo* gMediaSourceLog;
michael@0 19 #define MSE_DEBUG(...) PR_LOG(gMediaSourceLog, PR_LOG_DEBUG, (__VA_ARGS__))
michael@0 20 #else
michael@0 21 #define MSE_DEBUG(...)
michael@0 22 #endif
michael@0 23
michael@0 24 namespace mozilla {
michael@0 25
michael@0 26 namespace dom {
michael@0 27
michael@0 28 class SourceBuffer;
michael@0 29
michael@0 30 } // namespace dom
michael@0 31
michael@0 32 nsresult
michael@0 33 SourceBufferResource::Close()
michael@0 34 {
michael@0 35 ReentrantMonitorAutoEnter mon(mMonitor);
michael@0 36 MSE_DEBUG("%p SBR::Close", this);
michael@0 37 //MOZ_ASSERT(!mClosed);
michael@0 38 mClosed = true;
michael@0 39 mon.NotifyAll();
michael@0 40 return NS_OK;
michael@0 41 }
michael@0 42
michael@0 43 nsresult
michael@0 44 SourceBufferResource::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes)
michael@0 45 {
michael@0 46 ReentrantMonitorAutoEnter mon(mMonitor);
michael@0 47 bool blockingRead = !!aBytes;
michael@0 48
michael@0 49 while (blockingRead &&
michael@0 50 !mEnded &&
michael@0 51 mOffset + aCount > static_cast<uint64_t>(GetLength())) {
michael@0 52 MSE_DEBUG("%p SBR::Read waiting for data", this);
michael@0 53 mon.Wait();
michael@0 54 }
michael@0 55
michael@0 56 uint32_t available = GetLength() - mOffset;
michael@0 57 uint32_t count = std::min(aCount, available);
michael@0 58 if (!PR_GetEnv("MOZ_QUIET")) {
michael@0 59 MSE_DEBUG("%p SBR::Read aCount=%u length=%u offset=%u "
michael@0 60 "available=%u count=%u, blocking=%d bufComplete=%d",
michael@0 61 this, aCount, GetLength(), mOffset, available, count,
michael@0 62 blockingRead, mEnded);
michael@0 63 }
michael@0 64 if (available == 0) {
michael@0 65 MSE_DEBUG("%p SBR::Read EOF", this);
michael@0 66 *aBytes = 0;
michael@0 67 return NS_OK;
michael@0 68 }
michael@0 69
michael@0 70 mInputBuffer.CopyData(mOffset, count, aBuffer);
michael@0 71 *aBytes = count;
michael@0 72 mOffset += count;
michael@0 73 return NS_OK;
michael@0 74 }
michael@0 75
michael@0 76 nsresult
michael@0 77 SourceBufferResource::ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes)
michael@0 78 {
michael@0 79 ReentrantMonitorAutoEnter mon(mMonitor);
michael@0 80 nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
michael@0 81 if (NS_FAILED(rv)) {
michael@0 82 return rv;
michael@0 83 }
michael@0 84 return Read(aBuffer, aCount, aBytes);
michael@0 85 }
michael@0 86
michael@0 87 nsresult
michael@0 88 SourceBufferResource::Seek(int32_t aWhence, int64_t aOffset)
michael@0 89 {
michael@0 90 ReentrantMonitorAutoEnter mon(mMonitor);
michael@0 91 if (mClosed) {
michael@0 92 return NS_ERROR_FAILURE;
michael@0 93 }
michael@0 94
michael@0 95 int64_t newOffset = mOffset;
michael@0 96 switch (aWhence) {
michael@0 97 case nsISeekableStream::NS_SEEK_END:
michael@0 98 newOffset = GetLength() - aOffset;
michael@0 99 break;
michael@0 100 case nsISeekableStream::NS_SEEK_CUR:
michael@0 101 newOffset += aOffset;
michael@0 102 break;
michael@0 103 case nsISeekableStream::NS_SEEK_SET:
michael@0 104 newOffset = aOffset;
michael@0 105 break;
michael@0 106 }
michael@0 107
michael@0 108 if (newOffset < 0 || uint64_t(newOffset) < mInputBuffer.GetOffset() || newOffset > GetLength()) {
michael@0 109 return NS_ERROR_FAILURE;
michael@0 110 }
michael@0 111
michael@0 112 mOffset = newOffset;
michael@0 113 mon.NotifyAll();
michael@0 114
michael@0 115 return NS_OK;
michael@0 116 }
michael@0 117
michael@0 118 nsresult
michael@0 119 SourceBufferResource::ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount)
michael@0 120 {
michael@0 121 ReentrantMonitorAutoEnter mon(mMonitor);
michael@0 122 nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
michael@0 123 if (NS_FAILED(rv)) {
michael@0 124 return rv;
michael@0 125 }
michael@0 126 return Read(aBuffer, aCount, nullptr);
michael@0 127 }
michael@0 128
michael@0 129 bool
michael@0 130 SourceBufferResource::EvictData(uint32_t aThreshold)
michael@0 131 {
michael@0 132 return mInputBuffer.Evict(mOffset, aThreshold);
michael@0 133 }
michael@0 134
michael@0 135 void
michael@0 136 SourceBufferResource::EvictBefore(uint64_t aOffset)
michael@0 137 {
michael@0 138 // If aOffset is past the current playback offset we don't evict.
michael@0 139 if (aOffset < mOffset) {
michael@0 140 mInputBuffer.Evict(aOffset, 0);
michael@0 141 }
michael@0 142 }
michael@0 143
michael@0 144 void
michael@0 145 SourceBufferResource::AppendData(const uint8_t* aData, uint32_t aLength)
michael@0 146 {
michael@0 147 ReentrantMonitorAutoEnter mon(mMonitor);
michael@0 148 mInputBuffer.PushBack(new ResourceItem(aData, aLength));
michael@0 149 mon.NotifyAll();
michael@0 150 }
michael@0 151
michael@0 152 void
michael@0 153 SourceBufferResource::Ended()
michael@0 154 {
michael@0 155 ReentrantMonitorAutoEnter mon(mMonitor);
michael@0 156 mEnded = true;
michael@0 157 mon.NotifyAll();
michael@0 158 }
michael@0 159
michael@0 160 SourceBufferResource::~SourceBufferResource()
michael@0 161 {
michael@0 162 MOZ_COUNT_DTOR(SourceBufferResource);
michael@0 163 MSE_DEBUG("%p SBR::~SBR", this);
michael@0 164 }
michael@0 165
michael@0 166 SourceBufferResource::SourceBufferResource(nsIPrincipal* aPrincipal,
michael@0 167 const nsACString& aType)
michael@0 168 : mPrincipal(aPrincipal)
michael@0 169 , mType(aType)
michael@0 170 , mMonitor("mozilla::SourceBufferResource::mMonitor")
michael@0 171 , mOffset(0)
michael@0 172 , mClosed(false)
michael@0 173 , mEnded(false)
michael@0 174 {
michael@0 175 MOZ_COUNT_CTOR(SourceBufferResource);
michael@0 176 MSE_DEBUG("%p SBR::SBR()", this);
michael@0 177 }
michael@0 178
michael@0 179 } // namespace mozilla

mercurial