2014-05-05 21:30:43 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-11-16 22:14:08 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The storage stream provides an internal buffer that can be filled by a
|
|
|
|
* client using a single output stream. One or more independent input streams
|
|
|
|
* can be created to read the data out non-destructively. The implementation
|
|
|
|
* uses a segmented buffer internally to avoid realloc'ing of large buffers,
|
|
|
|
* with the attendant performance loss and heap fragmentation.
|
|
|
|
*/
|
|
|
|
|
2011-06-02 16:56:50 +04:00
|
|
|
#include "nsAlgorithm.h"
|
1999-11-16 22:14:08 +03:00
|
|
|
#include "nsStorageStream.h"
|
|
|
|
#include "nsSegmentedBuffer.h"
|
2006-01-02 05:30:32 +03:00
|
|
|
#include "nsStreamUtils.h"
|
1999-11-16 22:14:08 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2015-02-11 07:55:43 +03:00
|
|
|
#include "nsICloneableInputStream.h"
|
2000-08-22 11:03:33 +04:00
|
|
|
#include "nsIInputStream.h"
|
2014-06-17 14:52:59 +04:00
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
2002-01-25 05:13:00 +03:00
|
|
|
#include "nsISeekableStream.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-10-26 17:32:10 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2013-11-13 04:22:38 +04:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2014-06-17 14:52:59 +04:00
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
|
|
|
|
|
|
|
using mozilla::ipc::InputStreamParams;
|
|
|
|
using mozilla::ipc::StringInputStreamParams;
|
2016-09-29 07:20:00 +03:00
|
|
|
using mozilla::Maybe;
|
|
|
|
using mozilla::Some;
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2000-01-22 02:16:35 +03:00
|
|
|
//
|
|
|
|
// Log module for StorageStream logging...
|
|
|
|
//
|
|
|
|
// To enable logging (see prlog.h for full details):
|
|
|
|
//
|
2016-05-26 01:26:10 +03:00
|
|
|
// set MOZ_LOG=StorageStreamLog:5
|
|
|
|
// set MOZ_LOG_FILE=storage.log
|
2000-01-22 02:16:35 +03:00
|
|
|
//
|
2016-05-26 01:26:10 +03:00
|
|
|
// This enables LogLevel::Debug level information and places all output in
|
|
|
|
// the file storage.log.
|
2000-01-22 02:16:35 +03:00
|
|
|
//
|
2015-10-19 22:50:14 +03:00
|
|
|
static LazyLogModule sStorageStreamLog("nsStorageStream");
|
2013-11-19 06:34:00 +04:00
|
|
|
#ifdef LOG
|
|
|
|
#undef LOG
|
|
|
|
#endif
|
2015-10-19 22:50:14 +03:00
|
|
|
#define LOG(args) MOZ_LOG(sStorageStreamLog, mozilla::LogLevel::Debug, args)
|
2000-01-22 02:16:35 +03:00
|
|
|
|
1999-11-16 22:14:08 +03:00
|
|
|
nsStorageStream::nsStorageStream()
|
2014-05-05 21:30:43 +04:00
|
|
|
: mSegmentedBuffer(0), mSegmentSize(0), mWriteInProgress(false),
|
|
|
|
mLastSegmentNum(-1), mWriteCursor(0), mSegmentEnd(0), mLogicalLength(0)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
LOG(("Creating nsStorageStream [%p].\n", this));
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsStorageStream::~nsStorageStream()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
delete mSegmentedBuffer;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsStorageStream,
|
|
|
|
nsIStorageStream,
|
|
|
|
nsIOutputStream)
|
1999-11-16 22:14:08 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageStream::Init(uint32_t aSegmentSize, uint32_t aMaxSize)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
mSegmentedBuffer = new nsSegmentedBuffer();
|
2014-05-15 00:15:46 +04:00
|
|
|
mSegmentSize = aSegmentSize;
|
|
|
|
mSegmentSizeLog2 = mozilla::FloorLog2(aSegmentSize);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// Segment size must be a power of two
|
2014-05-15 00:15:46 +04:00
|
|
|
if (mSegmentSize != ((uint32_t)1 << mSegmentSizeLog2)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
return mSegmentedBuffer->Init(aSegmentSize, aMaxSize);
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-05 21:30:43 +04:00
|
|
|
nsStorageStream::GetOutputStream(int32_t aStartingOffset,
|
2014-05-15 00:15:46 +04:00
|
|
|
nsIOutputStream** aOutputStream)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(!aOutputStream)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!mSegmentedBuffer)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (mWriteInProgress) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
nsresult rv = Seek(aStartingOffset);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// Enlarge the last segment in the buffer so that it is the same size as
|
|
|
|
// all the other segments in the buffer. (It may have been realloc'ed
|
|
|
|
// smaller in the Close() method.)
|
|
|
|
if (mLastSegmentNum >= 0)
|
|
|
|
if (mSegmentedBuffer->ReallocLastSegment(mSegmentSize)) {
|
|
|
|
// Need to re-Seek, since realloc changed segment base pointer
|
|
|
|
rv = Seek(aStartingOffset);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(this);
|
|
|
|
*aOutputStream = static_cast<nsIOutputStream*>(this);
|
|
|
|
mWriteInProgress = true;
|
|
|
|
return NS_OK;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStorageStream::Close()
|
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(!mSegmentedBuffer)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
1999-11-16 22:14:08 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
mWriteInProgress = false;
|
2000-01-22 02:16:35 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
int32_t segmentOffset = SegOffset(mLogicalLength);
|
|
|
|
|
|
|
|
// Shrink the final segment in the segmented buffer to the minimum size
|
|
|
|
// needed to contain the data, so as to conserve memory.
|
2014-05-15 00:15:46 +04:00
|
|
|
if (segmentOffset) {
|
2014-05-05 21:30:43 +04:00
|
|
|
mSegmentedBuffer->ReallocLastSegment(segmentOffset);
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
mWriteCursor = 0;
|
|
|
|
mSegmentEnd = 0;
|
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsStorageStream [%p] Close mWriteCursor=%p mSegmentEnd=%p\n",
|
2014-05-05 21:30:43 +04:00
|
|
|
this, mWriteCursor, mSegmentEnd));
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStorageStream::Flush()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageStream::Write(const char* aBuffer, uint32_t aCount,
|
|
|
|
uint32_t* aNumWritten)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(!aNumWritten) || NS_WARN_IF(!aBuffer)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!mSegmentedBuffer)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
const char* readCursor;
|
|
|
|
uint32_t count, availableInSegment, remaining;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsStorageStream [%p] Write mWriteCursor=%p mSegmentEnd=%p aCount=%d\n",
|
2014-05-05 21:30:43 +04:00
|
|
|
this, mWriteCursor, mSegmentEnd, aCount));
|
|
|
|
|
|
|
|
remaining = aCount;
|
|
|
|
readCursor = aBuffer;
|
|
|
|
// If no segments have been created yet, create one even if we don't have
|
|
|
|
// to write any data; this enables creating an input stream which reads from
|
|
|
|
// the very end of the data for any amount of data in the stream (i.e.
|
|
|
|
// this stream contains N bytes of data and newInputStream(N) is called),
|
|
|
|
// even for N=0 (with the caveat that we require .write("", 0) be called to
|
|
|
|
// initialize internal buffers).
|
|
|
|
bool firstTime = mSegmentedBuffer->GetSegmentCount() == 0;
|
|
|
|
while (remaining || MOZ_UNLIKELY(firstTime)) {
|
|
|
|
firstTime = false;
|
|
|
|
availableInSegment = mSegmentEnd - mWriteCursor;
|
|
|
|
if (!availableInSegment) {
|
|
|
|
mWriteCursor = mSegmentedBuffer->AppendNewSegment();
|
|
|
|
if (!mWriteCursor) {
|
|
|
|
mSegmentEnd = 0;
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
mLastSegmentNum++;
|
|
|
|
mSegmentEnd = mWriteCursor + mSegmentSize;
|
|
|
|
availableInSegment = mSegmentEnd - mWriteCursor;
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsStorageStream [%p] Write (new seg) mWriteCursor=%p mSegmentEnd=%p\n",
|
2014-05-05 21:30:43 +04:00
|
|
|
this, mWriteCursor, mSegmentEnd));
|
|
|
|
}
|
|
|
|
|
|
|
|
count = XPCOM_MIN(availableInSegment, remaining);
|
|
|
|
memcpy(mWriteCursor, readCursor, count);
|
|
|
|
remaining -= count;
|
|
|
|
readCursor += count;
|
|
|
|
mWriteCursor += count;
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsStorageStream [%p] Writing mWriteCursor=%p mSegmentEnd=%p count=%d\n",
|
2014-05-05 21:30:43 +04:00
|
|
|
this, mWriteCursor, mSegmentEnd, count));
|
2016-01-22 18:58:49 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
out:
|
|
|
|
*aNumWritten = aCount - remaining;
|
|
|
|
mLogicalLength += *aNumWritten;
|
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsStorageStream [%p] Wrote mWriteCursor=%p mSegmentEnd=%p numWritten=%d\n",
|
2014-05-05 21:30:43 +04:00
|
|
|
this, mWriteCursor, mSegmentEnd, *aNumWritten));
|
|
|
|
return rv;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageStream::WriteFrom(nsIInputStream* aInStr, uint32_t aCount,
|
|
|
|
uint32_t* aResult)
|
2000-08-22 11:03:33 +04:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2000-08-22 11:03:33 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageStream::WriteSegments(nsReadSegmentFun aReader, void* aClosure,
|
|
|
|
uint32_t aCount, uint32_t* aResult)
|
2000-08-22 11:03:33 +04:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2000-08-22 11:03:33 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageStream::IsNonBlocking(bool* aNonBlocking)
|
2000-08-22 11:03:33 +04:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
*aNonBlocking = false;
|
|
|
|
return NS_OK;
|
2000-08-22 11:03:33 +04:00
|
|
|
}
|
|
|
|
|
1999-11-16 22:14:08 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageStream::GetLength(uint32_t* aLength)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
*aLength = mLogicalLength;
|
|
|
|
return NS_OK;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate the buffer by deleting the end segments
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsStorageStream::SetLength(uint32_t aLength)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(!mSegmentedBuffer)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
1999-11-16 22:14:08 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (mWriteInProgress) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aLength > mLogicalLength) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
int32_t newLastSegmentNum = SegNum(aLength);
|
|
|
|
int32_t segmentOffset = SegOffset(aLength);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (segmentOffset == 0) {
|
2014-05-05 21:30:43 +04:00
|
|
|
newLastSegmentNum--;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
while (newLastSegmentNum < mLastSegmentNum) {
|
|
|
|
mSegmentedBuffer->DeleteLastSegment();
|
|
|
|
mLastSegmentNum--;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLogicalLength = aLength;
|
|
|
|
return NS_OK;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageStream::GetWriteInProgress(bool* aWriteInProgress)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
*aWriteInProgress = mWriteInProgress;
|
|
|
|
return NS_OK;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
2016-08-15 07:30:32 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsStorageStream::Seek(int32_t aPosition)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(!mSegmentedBuffer)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
1999-11-16 22:14:08 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
// An argument of -1 means "seek to end of stream"
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aPosition == -1) {
|
2014-05-05 21:30:43 +04:00
|
|
|
aPosition = mLogicalLength;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// Seeking beyond the buffer end is illegal
|
2014-05-15 00:15:46 +04:00
|
|
|
if ((uint32_t)aPosition > mLogicalLength) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// Seeking backwards in the write stream results in truncation
|
|
|
|
SetLength(aPosition);
|
|
|
|
|
|
|
|
// Special handling for seek to start-of-buffer
|
|
|
|
if (aPosition == 0) {
|
|
|
|
mWriteCursor = 0;
|
|
|
|
mSegmentEnd = 0;
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsStorageStream [%p] Seek mWriteCursor=%p mSegmentEnd=%p\n",
|
2014-05-05 21:30:43 +04:00
|
|
|
this, mWriteCursor, mSegmentEnd));
|
1999-11-16 22:14:08 +03:00
|
|
|
return NS_OK;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Segment may have changed, so reset pointers
|
|
|
|
mWriteCursor = mSegmentedBuffer->GetSegment(mLastSegmentNum);
|
|
|
|
NS_ASSERTION(mWriteCursor, "null mWriteCursor");
|
|
|
|
mSegmentEnd = mWriteCursor + mSegmentSize;
|
|
|
|
|
|
|
|
// Adjust write cursor for current segment offset. This test is necessary
|
|
|
|
// because SegNum may reference the next-to-be-allocated segment, in which
|
|
|
|
// case we need to be pointing at the end of the last segment.
|
|
|
|
int32_t segmentOffset = SegOffset(aPosition);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (segmentOffset == 0 && (SegNum(aPosition) > (uint32_t) mLastSegmentNum)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
mWriteCursor = mSegmentEnd;
|
2014-05-15 00:15:46 +04:00
|
|
|
} else {
|
2014-05-05 21:30:43 +04:00
|
|
|
mWriteCursor += segmentOffset;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("nsStorageStream [%p] Seek mWriteCursor=%p mSegmentEnd=%p\n",
|
2014-05-05 21:30:43 +04:00
|
|
|
this, mWriteCursor, mSegmentEnd));
|
|
|
|
return NS_OK;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
2000-01-22 02:16:35 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-11-16 22:14:08 +03:00
|
|
|
// There can be many nsStorageInputStreams for a single nsStorageStream
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsStorageInputStream final
|
2014-05-15 00:15:46 +04:00
|
|
|
: public nsIInputStream
|
|
|
|
, public nsISeekableStream
|
2014-06-17 14:52:59 +04:00
|
|
|
, public nsIIPCSerializableInputStream
|
2015-02-11 07:55:43 +03:00
|
|
|
, public nsICloneableInputStream
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
|
|
|
public:
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageInputStream(nsStorageStream* aStorageStream, uint32_t aSegmentSize)
|
2014-05-05 21:30:43 +04:00
|
|
|
: mStorageStream(aStorageStream), mReadCursor(0),
|
|
|
|
mSegmentEnd(0), mSegmentNum(0),
|
|
|
|
mSegmentSize(aSegmentSize), mLogicalCursor(0),
|
|
|
|
mStatus(NS_OK)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
2014-06-17 14:52:59 +04:00
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
2015-02-11 07:55:43 +03:00
|
|
|
NS_DECL_NSICLONEABLEINPUTSTREAM
|
1999-11-16 22:14:08 +03:00
|
|
|
|
2004-01-15 09:14:18 +03:00
|
|
|
private:
|
2014-05-05 21:30:43 +04:00
|
|
|
~nsStorageInputStream()
|
|
|
|
{
|
|
|
|
}
|
2004-01-15 09:14:18 +03:00
|
|
|
|
1999-11-16 22:14:08 +03:00
|
|
|
protected:
|
2016-08-15 07:30:32 +03:00
|
|
|
nsresult Seek(uint32_t aPosition);
|
1999-11-16 22:14:08 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
friend class nsStorageStream;
|
1999-11-16 22:14:08 +03:00
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsStorageStream> mStorageStream;
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t mReadCursor; // Next memory location to read byte, or 0
|
|
|
|
uint32_t mSegmentEnd; // One byte past end of current buffer segment
|
|
|
|
uint32_t mSegmentNum; // Segment number containing read cursor
|
|
|
|
uint32_t mSegmentSize; // All segments, except the last, are of this size
|
|
|
|
uint32_t mLogicalCursor; // Logical offset into stream
|
|
|
|
nsresult mStatus;
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
uint32_t SegNum(uint32_t aPosition)
|
|
|
|
{
|
|
|
|
return aPosition >> mStorageStream->mSegmentSizeLog2;
|
|
|
|
}
|
|
|
|
uint32_t SegOffset(uint32_t aPosition)
|
|
|
|
{
|
|
|
|
return aPosition & (mSegmentSize - 1);
|
|
|
|
}
|
1999-11-16 22:14:08 +03:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsStorageInputStream,
|
|
|
|
nsIInputStream,
|
2014-06-17 14:52:59 +04:00
|
|
|
nsISeekableStream,
|
2015-02-11 07:55:43 +03:00
|
|
|
nsIIPCSerializableInputStream,
|
|
|
|
nsICloneableInputStream)
|
1999-11-16 22:14:08 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageStream::NewInputStream(int32_t aStartingOffset,
|
|
|
|
nsIInputStream** aInputStream)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(!mSegmentedBuffer)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2007-03-09 15:28:55 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsStorageInputStream> inputStream =
|
2014-05-15 00:15:46 +04:00
|
|
|
new nsStorageInputStream(this, mSegmentSize);
|
1999-11-16 22:14:08 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult rv = inputStream->Seek(aStartingOffset);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
1999-11-16 22:14:08 +03:00
|
|
|
|
2015-07-01 19:27:43 +03:00
|
|
|
inputStream.forget(aInputStream);
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStorageInputStream::Close()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
mStatus = NS_BASE_STREAM_CLOSED;
|
|
|
|
return NS_OK;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageInputStream::Available(uint64_t* aAvailable)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(mStatus)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return mStatus;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
*aAvailable = mStorageStream->mLogicalLength - mLogicalCursor;
|
|
|
|
return NS_OK;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageInputStream::Read(char* aBuffer, uint32_t aCount, uint32_t* aNumRead)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, aBuffer, aCount, aNumRead);
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageInputStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
|
|
|
uint32_t aCount, uint32_t* aNumRead)
|
2000-08-22 11:03:33 +04:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
*aNumRead = 0;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (mStatus == NS_BASE_STREAM_CLOSED) {
|
2006-01-02 05:30:32 +03:00
|
|
|
return NS_OK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
if (NS_FAILED(mStatus)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return mStatus;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
uint32_t count, availableInSegment, remainingCapacity, bytesConsumed;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
remainingCapacity = aCount;
|
|
|
|
while (remainingCapacity) {
|
|
|
|
availableInSegment = mSegmentEnd - mReadCursor;
|
|
|
|
if (!availableInSegment) {
|
|
|
|
uint32_t available = mStorageStream->mLogicalLength - mLogicalCursor;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (!available) {
|
2014-05-05 21:30:43 +04:00
|
|
|
goto out;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2015-09-15 20:15:45 +03:00
|
|
|
// We have data in the stream, but if mSegmentEnd is zero, then we
|
|
|
|
// were likely constructed prior to any data being written into
|
|
|
|
// the stream. Therefore, if mSegmentEnd is non-zero, we should
|
|
|
|
// move into the next segment; otherwise, we should stay in this
|
|
|
|
// segment so our input state can be updated and we can properly
|
|
|
|
// perform the initial read.
|
|
|
|
if (mSegmentEnd > 0) {
|
|
|
|
mSegmentNum++;
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
mReadCursor = 0;
|
|
|
|
mSegmentEnd = XPCOM_MIN(mSegmentSize, available);
|
|
|
|
availableInSegment = mSegmentEnd;
|
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
const char* cur = mStorageStream->mSegmentedBuffer->GetSegment(mSegmentNum);
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
count = XPCOM_MIN(availableInSegment, remainingCapacity);
|
2014-05-15 00:15:46 +04:00
|
|
|
rv = aWriter(this, aClosure, cur + mReadCursor, aCount - remainingCapacity,
|
2014-08-25 23:17:15 +04:00
|
|
|
count, &bytesConsumed);
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(rv) || (bytesConsumed == 0)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
remainingCapacity -= bytesConsumed;
|
|
|
|
mReadCursor += bytesConsumed;
|
|
|
|
mLogicalCursor += bytesConsumed;
|
2016-01-22 18:58:49 +03:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
out:
|
|
|
|
*aNumRead = aCount - remainingCapacity;
|
|
|
|
|
|
|
|
bool isWriteInProgress = false;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(mStorageStream->GetWriteInProgress(&isWriteInProgress))) {
|
2014-05-05 21:30:43 +04:00
|
|
|
isWriteInProgress = false;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (*aNumRead == 0 && isWriteInProgress) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2000-08-22 11:03:33 +04:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageInputStream::IsNonBlocking(bool* aNonBlocking)
|
2000-08-22 11:03:33 +04:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
// TODO: This class should implement nsIAsyncInputStream so that callers
|
|
|
|
// have some way of dealing with NS_BASE_STREAM_WOULD_BLOCK errors.
|
|
|
|
|
|
|
|
*aNonBlocking = true;
|
|
|
|
return NS_OK;
|
2000-08-22 11:03:33 +04:00
|
|
|
}
|
|
|
|
|
2002-01-25 05:13:00 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsStorageInputStream::Seek(int32_t aWhence, int64_t aOffset)
|
2002-01-25 05:13:00 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(mStatus)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return mStatus;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
int64_t pos = aOffset;
|
2002-01-25 05:13:00 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
switch (aWhence) {
|
2002-01-25 05:13:00 +03:00
|
|
|
case NS_SEEK_SET:
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2002-01-25 05:13:00 +03:00
|
|
|
case NS_SEEK_CUR:
|
2014-05-05 21:30:43 +04:00
|
|
|
pos += mLogicalCursor;
|
|
|
|
break;
|
2002-01-25 05:13:00 +03:00
|
|
|
case NS_SEEK_END:
|
2014-05-05 21:30:43 +04:00
|
|
|
pos += mStorageStream->mLogicalLength;
|
|
|
|
break;
|
2002-01-25 13:47:24 +03:00
|
|
|
default:
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_NOTREACHED("unexpected whence value");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2014-05-15 00:15:46 +04:00
|
|
|
if (pos == int64_t(mLogicalCursor)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-01-18 05:15:14 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return Seek(pos);
|
2002-01-25 05:13:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStorageInputStream::Tell(int64_t* aResult)
|
2002-01-25 05:13:00 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_FAILED(mStatus)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return mStatus;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
*aResult = mLogicalCursor;
|
|
|
|
return NS_OK;
|
2002-01-25 05:13:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStorageInputStream::SetEOF()
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_NOTREACHED("nsStorageInputStream::SetEOF");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2002-01-25 05:13:00 +03:00
|
|
|
}
|
|
|
|
|
2016-08-15 07:30:32 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsStorageInputStream::Seek(uint32_t aPosition)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
uint32_t length = mStorageStream->mLogicalLength;
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aPosition > length) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (length == 0) {
|
1999-11-16 22:14:08 +03:00
|
|
|
return NS_OK;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
mSegmentNum = SegNum(aPosition);
|
|
|
|
mReadCursor = SegOffset(aPosition);
|
|
|
|
uint32_t available = length - aPosition;
|
|
|
|
mSegmentEnd = mReadCursor + XPCOM_MIN(mSegmentSize - mReadCursor, available);
|
|
|
|
mLogicalCursor = aPosition;
|
|
|
|
return NS_OK;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
|
|
|
|
2014-06-17 14:52:59 +04:00
|
|
|
void
|
|
|
|
nsStorageInputStream::Serialize(InputStreamParams& aParams, FileDescriptorArray&)
|
|
|
|
{
|
|
|
|
nsCString combined;
|
|
|
|
int64_t offset;
|
|
|
|
mozilla::DebugOnly<nsresult> rv = Tell(&offset);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
uint64_t remaining;
|
|
|
|
rv = Available(&remaining);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
combined.SetCapacity(remaining);
|
|
|
|
uint32_t numRead = 0;
|
|
|
|
|
|
|
|
rv = Read(combined.BeginWriting(), remaining, &numRead);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
MOZ_ASSERT(numRead == remaining);
|
|
|
|
combined.SetLength(numRead);
|
|
|
|
|
|
|
|
rv = Seek(NS_SEEK_SET, offset);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
StringInputStreamParams params;
|
|
|
|
params.data() = combined;
|
|
|
|
aParams = params;
|
|
|
|
}
|
|
|
|
|
2016-09-29 07:20:00 +03:00
|
|
|
Maybe<uint64_t>
|
|
|
|
nsStorageInputStream::ExpectedSerializedLength()
|
|
|
|
{
|
|
|
|
uint64_t remaining = 0;
|
|
|
|
DebugOnly<nsresult> rv = Available(&remaining);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
return Some(remaining);
|
|
|
|
}
|
|
|
|
|
2014-06-17 14:52:59 +04:00
|
|
|
bool
|
2014-08-25 23:17:15 +04:00
|
|
|
nsStorageInputStream::Deserialize(const InputStreamParams& aParams,
|
|
|
|
const FileDescriptorArray&)
|
2014-06-17 14:52:59 +04:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("We should never attempt to deserialize a storage input stream.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStorageInputStream::GetCloneable(bool* aCloneableOut)
|
|
|
|
{
|
|
|
|
*aCloneableOut = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStorageInputStream::Clone(nsIInputStream** aCloneOut)
|
|
|
|
{
|
|
|
|
return mStorageStream->NewInputStream(mLogicalCursor, aCloneOut);
|
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_NewStorageStream(uint32_t aSegmentSize, uint32_t aMaxSize,
|
|
|
|
nsIStorageStream** aResult)
|
1999-11-16 22:14:08 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsStorageStream> storageStream = new nsStorageStream();
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult rv = storageStream->Init(aSegmentSize, aMaxSize);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-07-01 19:27:43 +03:00
|
|
|
storageStream.forget(aResult);
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
1999-11-16 22:14:08 +03:00
|
|
|
}
|
2013-11-26 21:42:58 +04:00
|
|
|
|
|
|
|
// Undefine LOG, so that other .cpp files (or their includes) won't complain
|
|
|
|
// about it already being defined, when we build in unified mode.
|
|
|
|
#undef LOG
|