content/media/EncodedBufferCache.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial