1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/EncodedBufferCache.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,74 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "EncodedBufferCache.h" 1.11 +#include "nsAnonymousTemporaryFile.h" 1.12 +#include "nsDOMFile.h" 1.13 +#include "prio.h" 1.14 + 1.15 +namespace mozilla { 1.16 + 1.17 +void 1.18 +EncodedBufferCache::AppendBuffer(nsTArray<uint8_t> & aBuf) 1.19 +{ 1.20 + MutexAutoLock lock(mMutex); 1.21 + mDataSize += aBuf.Length(); 1.22 + 1.23 + mEncodedBuffers.AppendElement()->SwapElements(aBuf); 1.24 + 1.25 + if (!mTempFileEnabled && mDataSize > mMaxMemoryStorage) { 1.26 + nsresult rv = NS_OpenAnonymousTemporaryFile(&mFD); 1.27 + if (!NS_FAILED(rv)) { 1.28 + mTempFileEnabled = true; 1.29 + } 1.30 + } 1.31 + 1.32 + if (mTempFileEnabled) { 1.33 + // has created temporary file, write buffer in it 1.34 + for (uint32_t i = 0; i < mEncodedBuffers.Length(); i++) { 1.35 + int64_t amount = PR_Write(mFD, mEncodedBuffers.ElementAt(i).Elements(), mEncodedBuffers.ElementAt(i).Length()); 1.36 + if (amount < mEncodedBuffers.ElementAt(i).Length()) { 1.37 + NS_WARNING("Failed to write media cache block!"); 1.38 + } 1.39 + } 1.40 + mEncodedBuffers.Clear(); 1.41 + } 1.42 + 1.43 +} 1.44 + 1.45 +already_AddRefed<nsIDOMBlob> 1.46 +EncodedBufferCache::ExtractBlob(const nsAString &aContentType) 1.47 +{ 1.48 + MutexAutoLock lock(mMutex); 1.49 + nsCOMPtr<nsIDOMBlob> blob; 1.50 + if (mTempFileEnabled) { 1.51 + // generate new temporary file to write 1.52 + blob = new nsDOMTemporaryFileBlob(mFD, 0, mDataSize, aContentType); 1.53 + // fallback to memory blob 1.54 + mTempFileEnabled = false; 1.55 + mDataSize = 0; 1.56 + mFD = nullptr; 1.57 + } else { 1.58 + void* blobData = moz_malloc(mDataSize); 1.59 + NS_ASSERTION(blobData, "out of memory!!"); 1.60 + 1.61 + if (blobData) { 1.62 + for (uint32_t i = 0, offset = 0; i < mEncodedBuffers.Length(); i++) { 1.63 + memcpy((uint8_t*)blobData + offset, mEncodedBuffers.ElementAt(i).Elements(), 1.64 + mEncodedBuffers.ElementAt(i).Length()); 1.65 + offset += mEncodedBuffers.ElementAt(i).Length(); 1.66 + } 1.67 + blob = new nsDOMMemoryFile(blobData, mDataSize, 1.68 + aContentType); 1.69 + mEncodedBuffers.Clear(); 1.70 + } else 1.71 + return nullptr; 1.72 + } 1.73 + mDataSize = 0; 1.74 + return blob.forget(); 1.75 +} 1.76 + 1.77 +} //end namespace