Line data Source code
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/. */
6 :
7 : #ifndef EncodedBufferCache_h_
8 : #define EncodedBufferCache_h_
9 :
10 : #include "nsCOMPtr.h"
11 : #include "nsTArray.h"
12 : #include "mozilla/Mutex.h"
13 :
14 : struct PRFileDesc;
15 :
16 : namespace mozilla {
17 :
18 : namespace dom {
19 : class Blob;
20 : } // namespace dom
21 :
22 : /**
23 : * Data is moved into a temporary file when it grows beyond
24 : * the maximal size passed in the Init function.
25 : * The AppendBuffer and ExtractBlob methods are thread-safe and can be called on
26 : * different threads at the same time.
27 : */
28 : class EncodedBufferCache
29 : {
30 : public:
31 0 : explicit EncodedBufferCache(uint32_t aMaxMemoryStorage)
32 0 : : mFD(nullptr),
33 : mMutex("EncodedBufferCache.Data.Mutex"),
34 : mDataSize(0),
35 : mMaxMemoryStorage(aMaxMemoryStorage),
36 0 : mTempFileEnabled(false) { }
37 0 : ~EncodedBufferCache()
38 0 : {
39 0 : }
40 : // Append buffers in cache, check if the queue is too large then switch to write buffer to file system
41 : // aBuf will append to mEncodedBuffers or temporary File, aBuf also be cleared
42 : void AppendBuffer(nsTArray<uint8_t> & aBuf);
43 : // Read all buffer from memory or file System, also Remove the temporary file or clean the buffers in memory.
44 : already_AddRefed<dom::Blob> ExtractBlob(nsISupports* aParent, const nsAString &aContentType);
45 :
46 : private:
47 : //array for storing the encoded data.
48 : nsTArray<nsTArray<uint8_t> > mEncodedBuffers;
49 : // File handle for the temporary file
50 : PRFileDesc* mFD;
51 : // Used to protect the mEncodedBuffer for avoiding AppendBuffer/Consume on different thread at the same time.
52 : Mutex mMutex;
53 : // the current buffer size can be read
54 : uint64_t mDataSize;
55 : // The maximal buffer allowed in memory
56 : uint32_t mMaxMemoryStorage;
57 : // indicate the buffer is stored on temporary file or not
58 : bool mTempFileEnabled;
59 : };
60 :
61 : } // namespace mozilla
62 :
63 : #endif
|