dom/file/MemoryStreams.cpp

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=2 et sw=2 tw=80: */
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 file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "MemoryStreams.h"
michael@0 8
michael@0 9 #include "nsStreamUtils.h"
michael@0 10
michael@0 11 USING_FILE_NAMESPACE
michael@0 12
michael@0 13 // static
michael@0 14 already_AddRefed<MemoryOutputStream>
michael@0 15 MemoryOutputStream::Create(uint64_t aSize)
michael@0 16 {
michael@0 17 NS_ASSERTION(aSize, "Passed zero size!");
michael@0 18
michael@0 19 NS_ENSURE_TRUE(aSize <= UINT32_MAX, nullptr);
michael@0 20
michael@0 21 nsRefPtr<MemoryOutputStream> stream = new MemoryOutputStream();
michael@0 22
michael@0 23 char* dummy;
michael@0 24 uint32_t length = stream->mData.GetMutableData(&dummy, aSize, fallible_t());
michael@0 25 NS_ENSURE_TRUE(length == aSize, nullptr);
michael@0 26
michael@0 27 return stream.forget();
michael@0 28 }
michael@0 29
michael@0 30 NS_IMPL_ISUPPORTS(MemoryOutputStream, nsIOutputStream)
michael@0 31
michael@0 32 NS_IMETHODIMP
michael@0 33 MemoryOutputStream::Close()
michael@0 34 {
michael@0 35 mData.Truncate(mOffset);
michael@0 36 return NS_OK;
michael@0 37 }
michael@0 38
michael@0 39 NS_IMETHODIMP
michael@0 40 MemoryOutputStream::Write(const char* aBuf, uint32_t aCount, uint32_t* _retval)
michael@0 41 {
michael@0 42 return WriteSegments(NS_CopySegmentToBuffer, (char*)aBuf, aCount, _retval);
michael@0 43 }
michael@0 44
michael@0 45 NS_IMETHODIMP
michael@0 46 MemoryOutputStream::Flush()
michael@0 47 {
michael@0 48 return NS_OK;
michael@0 49 }
michael@0 50
michael@0 51 NS_IMETHODIMP
michael@0 52 MemoryOutputStream::WriteFrom(nsIInputStream* aFromStream, uint32_t aCount,
michael@0 53 uint32_t* _retval)
michael@0 54 {
michael@0 55 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 56 }
michael@0 57
michael@0 58 NS_IMETHODIMP
michael@0 59 MemoryOutputStream::WriteSegments(nsReadSegmentFun aReader, void* aClosure,
michael@0 60 uint32_t aCount, uint32_t* _retval)
michael@0 61 {
michael@0 62 NS_ASSERTION(mData.Length() >= mOffset, "Bad stream state!");
michael@0 63
michael@0 64 uint32_t maxCount = mData.Length() - mOffset;
michael@0 65 if (maxCount == 0) {
michael@0 66 *_retval = 0;
michael@0 67 return NS_OK;
michael@0 68 }
michael@0 69
michael@0 70 if (aCount > maxCount) {
michael@0 71 aCount = maxCount;
michael@0 72 }
michael@0 73
michael@0 74 nsresult rv = aReader(this, aClosure, mData.BeginWriting() + mOffset, 0,
michael@0 75 aCount, _retval);
michael@0 76 if (NS_SUCCEEDED(rv)) {
michael@0 77 NS_ASSERTION(*_retval <= aCount,
michael@0 78 "Reader should not read more than we asked it to read!");
michael@0 79 mOffset += *_retval;
michael@0 80 }
michael@0 81
michael@0 82 return NS_OK;
michael@0 83 }
michael@0 84
michael@0 85 NS_IMETHODIMP
michael@0 86 MemoryOutputStream::IsNonBlocking(bool* _retval)
michael@0 87 {
michael@0 88 *_retval = false;
michael@0 89 return NS_OK;
michael@0 90 }

mercurial