content/media/EncodedBufferCache.cpp

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

     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 #include "EncodedBufferCache.h"
     8 #include "nsAnonymousTemporaryFile.h"
     9 #include "nsDOMFile.h"
    10 #include "prio.h"
    12 namespace mozilla {
    14 void
    15 EncodedBufferCache::AppendBuffer(nsTArray<uint8_t> & aBuf)
    16 {
    17   MutexAutoLock lock(mMutex);
    18   mDataSize += aBuf.Length();
    20   mEncodedBuffers.AppendElement()->SwapElements(aBuf);
    22   if (!mTempFileEnabled && mDataSize > mMaxMemoryStorage) {
    23     nsresult rv = NS_OpenAnonymousTemporaryFile(&mFD);
    24     if (!NS_FAILED(rv)) {
    25       mTempFileEnabled = true;
    26     }
    27   }
    29   if (mTempFileEnabled) {
    30     // has created temporary file, write buffer in it
    31     for (uint32_t i = 0; i < mEncodedBuffers.Length(); i++) {
    32       int64_t amount = PR_Write(mFD, mEncodedBuffers.ElementAt(i).Elements(), mEncodedBuffers.ElementAt(i).Length());
    33       if (amount <  mEncodedBuffers.ElementAt(i).Length()) {
    34         NS_WARNING("Failed to write media cache block!");
    35       }
    36     }
    37     mEncodedBuffers.Clear();
    38   }
    40 }
    42 already_AddRefed<nsIDOMBlob>
    43 EncodedBufferCache::ExtractBlob(const nsAString &aContentType)
    44 {
    45   MutexAutoLock lock(mMutex);
    46   nsCOMPtr<nsIDOMBlob> blob;
    47   if (mTempFileEnabled) {
    48     // generate new temporary file to write
    49     blob = new nsDOMTemporaryFileBlob(mFD, 0, mDataSize, aContentType);
    50     // fallback to memory blob
    51     mTempFileEnabled = false;
    52     mDataSize = 0;
    53     mFD = nullptr;
    54   } else {
    55     void* blobData = moz_malloc(mDataSize);
    56     NS_ASSERTION(blobData, "out of memory!!");
    58     if (blobData) {
    59       for (uint32_t i = 0, offset = 0; i < mEncodedBuffers.Length(); i++) {
    60         memcpy((uint8_t*)blobData + offset, mEncodedBuffers.ElementAt(i).Elements(),
    61                mEncodedBuffers.ElementAt(i).Length());
    62         offset += mEncodedBuffers.ElementAt(i).Length();
    63       }
    64       blob = new nsDOMMemoryFile(blobData, mDataSize,
    65                                  aContentType);
    66       mEncodedBuffers.Clear();
    67     } else
    68       return nullptr;
    69   }
    70   mDataSize = 0;
    71   return blob.forget();
    72 }
    74 } //end namespace

mercurial