michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et cindent: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef EncodedBufferCache_h_ michael@0: #define EncodedBufferCache_h_ michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsTArray.h" michael@0: #include "mozilla/Mutex.h" michael@0: michael@0: struct PRFileDesc; michael@0: class nsIDOMBlob; michael@0: michael@0: namespace mozilla { michael@0: michael@0: class ReentrantMonitor; michael@0: /** michael@0: * Data is moved into a temporary file when it grows beyond michael@0: * the maximal size passed in the Init function. michael@0: * The AppendBuffer and ExtractBlob methods are thread-safe and can be called on michael@0: * different threads at the same time. michael@0: */ michael@0: class EncodedBufferCache michael@0: { michael@0: public: michael@0: EncodedBufferCache(uint32_t aMaxMemoryStorage) michael@0: : mFD(nullptr), michael@0: mMutex("EncodedBufferCache.Data.Mutex"), michael@0: mDataSize(0), michael@0: mMaxMemoryStorage(aMaxMemoryStorage), michael@0: mTempFileEnabled(false) { } michael@0: ~EncodedBufferCache() michael@0: { michael@0: } michael@0: // Append buffers in cache, check if the queue is too large then switch to write buffer to file system michael@0: // aBuf will append to mEncodedBuffers or temporary File, aBuf also be cleared michael@0: void AppendBuffer(nsTArray & aBuf); michael@0: // Read all buffer from memory or file System, also Remove the temporary file or clean the buffers in memory. michael@0: already_AddRefed ExtractBlob(const nsAString &aContentType); michael@0: michael@0: private: michael@0: //array for storing the encoded data. michael@0: nsTArray > mEncodedBuffers; michael@0: // File handle for the temporary file michael@0: PRFileDesc* mFD; michael@0: // Used to protect the mEncodedBuffer for avoiding AppendBuffer/Consume on different thread at the same time. michael@0: Mutex mMutex; michael@0: // the current buffer size can be read michael@0: uint64_t mDataSize; michael@0: // The maximal buffer allowed in memory michael@0: uint32_t mMaxMemoryStorage; michael@0: // indicate the buffer is stored on temporary file or not michael@0: bool mTempFileEnabled; michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif