Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 sts=2 et cindent: */ |
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 "EncodedBufferCache.h" |
michael@0 | 8 | #include "nsAnonymousTemporaryFile.h" |
michael@0 | 9 | #include "nsDOMFile.h" |
michael@0 | 10 | #include "prio.h" |
michael@0 | 11 | |
michael@0 | 12 | namespace mozilla { |
michael@0 | 13 | |
michael@0 | 14 | void |
michael@0 | 15 | EncodedBufferCache::AppendBuffer(nsTArray<uint8_t> & aBuf) |
michael@0 | 16 | { |
michael@0 | 17 | MutexAutoLock lock(mMutex); |
michael@0 | 18 | mDataSize += aBuf.Length(); |
michael@0 | 19 | |
michael@0 | 20 | mEncodedBuffers.AppendElement()->SwapElements(aBuf); |
michael@0 | 21 | |
michael@0 | 22 | if (!mTempFileEnabled && mDataSize > mMaxMemoryStorage) { |
michael@0 | 23 | nsresult rv = NS_OpenAnonymousTemporaryFile(&mFD); |
michael@0 | 24 | if (!NS_FAILED(rv)) { |
michael@0 | 25 | mTempFileEnabled = true; |
michael@0 | 26 | } |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | if (mTempFileEnabled) { |
michael@0 | 30 | // has created temporary file, write buffer in it |
michael@0 | 31 | for (uint32_t i = 0; i < mEncodedBuffers.Length(); i++) { |
michael@0 | 32 | int64_t amount = PR_Write(mFD, mEncodedBuffers.ElementAt(i).Elements(), mEncodedBuffers.ElementAt(i).Length()); |
michael@0 | 33 | if (amount < mEncodedBuffers.ElementAt(i).Length()) { |
michael@0 | 34 | NS_WARNING("Failed to write media cache block!"); |
michael@0 | 35 | } |
michael@0 | 36 | } |
michael@0 | 37 | mEncodedBuffers.Clear(); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | already_AddRefed<nsIDOMBlob> |
michael@0 | 43 | EncodedBufferCache::ExtractBlob(const nsAString &aContentType) |
michael@0 | 44 | { |
michael@0 | 45 | MutexAutoLock lock(mMutex); |
michael@0 | 46 | nsCOMPtr<nsIDOMBlob> blob; |
michael@0 | 47 | if (mTempFileEnabled) { |
michael@0 | 48 | // generate new temporary file to write |
michael@0 | 49 | blob = new nsDOMTemporaryFileBlob(mFD, 0, mDataSize, aContentType); |
michael@0 | 50 | // fallback to memory blob |
michael@0 | 51 | mTempFileEnabled = false; |
michael@0 | 52 | mDataSize = 0; |
michael@0 | 53 | mFD = nullptr; |
michael@0 | 54 | } else { |
michael@0 | 55 | void* blobData = moz_malloc(mDataSize); |
michael@0 | 56 | NS_ASSERTION(blobData, "out of memory!!"); |
michael@0 | 57 | |
michael@0 | 58 | if (blobData) { |
michael@0 | 59 | for (uint32_t i = 0, offset = 0; i < mEncodedBuffers.Length(); i++) { |
michael@0 | 60 | memcpy((uint8_t*)blobData + offset, mEncodedBuffers.ElementAt(i).Elements(), |
michael@0 | 61 | mEncodedBuffers.ElementAt(i).Length()); |
michael@0 | 62 | offset += mEncodedBuffers.ElementAt(i).Length(); |
michael@0 | 63 | } |
michael@0 | 64 | blob = new nsDOMMemoryFile(blobData, mDataSize, |
michael@0 | 65 | aContentType); |
michael@0 | 66 | mEncodedBuffers.Clear(); |
michael@0 | 67 | } else |
michael@0 | 68 | return nullptr; |
michael@0 | 69 | } |
michael@0 | 70 | mDataSize = 0; |
michael@0 | 71 | return blob.forget(); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | } //end namespace |