1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/EncodedBufferCache.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,61 @@ 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 +#ifndef EncodedBufferCache_h_ 1.11 +#define EncodedBufferCache_h_ 1.12 + 1.13 +#include "nsCOMPtr.h" 1.14 +#include "nsTArray.h" 1.15 +#include "mozilla/Mutex.h" 1.16 + 1.17 +struct PRFileDesc; 1.18 +class nsIDOMBlob; 1.19 + 1.20 +namespace mozilla { 1.21 + 1.22 +class ReentrantMonitor; 1.23 +/** 1.24 + * Data is moved into a temporary file when it grows beyond 1.25 + * the maximal size passed in the Init function. 1.26 + * The AppendBuffer and ExtractBlob methods are thread-safe and can be called on 1.27 + * different threads at the same time. 1.28 + */ 1.29 +class EncodedBufferCache 1.30 +{ 1.31 +public: 1.32 + EncodedBufferCache(uint32_t aMaxMemoryStorage) 1.33 + : mFD(nullptr), 1.34 + mMutex("EncodedBufferCache.Data.Mutex"), 1.35 + mDataSize(0), 1.36 + mMaxMemoryStorage(aMaxMemoryStorage), 1.37 + mTempFileEnabled(false) { } 1.38 + ~EncodedBufferCache() 1.39 + { 1.40 + } 1.41 + // Append buffers in cache, check if the queue is too large then switch to write buffer to file system 1.42 + // aBuf will append to mEncodedBuffers or temporary File, aBuf also be cleared 1.43 + void AppendBuffer(nsTArray<uint8_t> & aBuf); 1.44 + // Read all buffer from memory or file System, also Remove the temporary file or clean the buffers in memory. 1.45 + already_AddRefed<nsIDOMBlob> ExtractBlob(const nsAString &aContentType); 1.46 + 1.47 +private: 1.48 + //array for storing the encoded data. 1.49 + nsTArray<nsTArray<uint8_t> > mEncodedBuffers; 1.50 + // File handle for the temporary file 1.51 + PRFileDesc* mFD; 1.52 + // Used to protect the mEncodedBuffer for avoiding AppendBuffer/Consume on different thread at the same time. 1.53 + Mutex mMutex; 1.54 + // the current buffer size can be read 1.55 + uint64_t mDataSize; 1.56 + // The maximal buffer allowed in memory 1.57 + uint32_t mMaxMemoryStorage; 1.58 + // indicate the buffer is stored on temporary file or not 1.59 + bool mTempFileEnabled; 1.60 +}; 1.61 + 1.62 +} // namespace mozilla 1.63 + 1.64 +#endif