2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:set ts=4 sts=4 sw=4 cin et: */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Based on original code from nsIStringStream.cpp
|
|
|
|
*/
|
|
|
|
|
2012-08-28 05:41:04 -07:00
|
|
|
#include "ipc/IPCMessageUtils.h"
|
2010-10-21 11:36:13 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsStringStream.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "prerror.h"
|
|
|
|
#include "plstr.h"
|
|
|
|
#include "nsIClassInfoImpl.h"
|
2012-06-05 16:51:58 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-08-23 12:33:46 -07:00
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
2012-08-15 21:02:32 -07:00
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIStringInputStream implementation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-06-05 16:51:58 -07:00
|
|
|
class nsStringInputStream MOZ_FINAL : public nsIStringInputStream
|
|
|
|
, public nsISeekableStream
|
|
|
|
, public nsISupportsCString
|
2012-08-15 21:02:32 -07:00
|
|
|
, public nsIIPCSerializableInputStream
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2013-07-18 19:31:26 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSISTRINGINPUTSTREAM
|
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
|
|
|
NS_DECL_NSISUPPORTSPRIMITIVE
|
|
|
|
NS_DECL_NSISUPPORTSCSTRING
|
2012-08-15 21:02:32 -07:00
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsStringInputStream()
|
2011-11-01 01:28:31 -07:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
~nsStringInputStream()
|
2011-11-01 01:28:31 -07:00
|
|
|
{}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t Length() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-11-01 01:28:31 -07:00
|
|
|
return mData.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t LengthRemaining() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-11-01 01:28:31 -07:00
|
|
|
return Length() - mOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
2011-11-01 01:28:31 -07:00
|
|
|
mData.SetIsVoid(true);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-11-01 01:28:31 -07:00
|
|
|
bool Closed()
|
|
|
|
{
|
|
|
|
return mData.IsVoid();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-11-01 01:28:31 -07:00
|
|
|
nsDependentCSubstring mData;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// This class needs to support threadsafe refcounting since people often
|
|
|
|
// allocate a string stream, and then read it from a background thread.
|
2013-07-18 19:31:26 -07:00
|
|
|
NS_IMPL_ADDREF(nsStringInputStream)
|
|
|
|
NS_IMPL_RELEASE(nsStringInputStream)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-22 09:59:57 -07:00
|
|
|
NS_IMPL_CLASSINFO(nsStringInputStream, NULL, nsIClassInfo::THREADSAFE,
|
|
|
|
NS_STRINGINPUTSTREAM_CID)
|
2012-08-23 12:33:46 -07:00
|
|
|
NS_IMPL_QUERY_INTERFACE5_CI(nsStringInputStream,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIStringInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsISupportsCString,
|
2010-10-21 11:36:13 -07:00
|
|
|
nsISeekableStream,
|
2012-08-15 21:02:32 -07:00
|
|
|
nsIIPCSerializableInputStream)
|
|
|
|
NS_IMPL_CI_INTERFACE_GETTER4(nsStringInputStream,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIStringInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsISupportsCString,
|
2012-08-15 21:02:32 -07:00
|
|
|
nsISeekableStream)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/////////
|
|
|
|
// nsISupportsCString implementation
|
|
|
|
/////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsStringInputStream::GetType(uint16_t *type)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*type = TYPE_CSTRING;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::GetData(nsACString &data)
|
|
|
|
{
|
|
|
|
// The stream doesn't have any data when it is closed. We could fake it
|
|
|
|
// and return an empty string here, but it seems better to keep this return
|
|
|
|
// value consistent with the behavior of the other 'getter' methods.
|
2011-11-01 01:28:31 -07:00
|
|
|
NS_ENSURE_TRUE(!Closed(), NS_BASE_STREAM_CLOSED);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-11-01 01:28:31 -07:00
|
|
|
data.Assign(mData);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::SetData(const nsACString &data)
|
|
|
|
{
|
2011-11-01 01:28:31 -07:00
|
|
|
mData.Assign(data);
|
|
|
|
mOffset = 0;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::ToString(char **result)
|
|
|
|
{
|
|
|
|
// NOTE: This method may result in data loss, so we do not implement it.
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////
|
|
|
|
// nsIStringInputStream implementation
|
|
|
|
/////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsStringInputStream::SetData(const char *data, int32_t dataLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(data);
|
2011-11-01 01:28:31 -07:00
|
|
|
mData.Assign(data, dataLen);
|
|
|
|
mOffset = 0;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsStringInputStream::AdoptData(char *data, int32_t dataLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(data);
|
2011-11-01 01:28:31 -07:00
|
|
|
mData.Adopt(data, dataLen);
|
|
|
|
mOffset = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsStringInputStream::ShareData(const char *data, int32_t dataLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(data);
|
|
|
|
|
|
|
|
if (dataLen < 0)
|
|
|
|
dataLen = strlen(data);
|
|
|
|
|
2011-12-09 00:35:41 -08:00
|
|
|
mData.Rebind(data, dataLen);
|
2011-11-01 01:28:31 -07:00
|
|
|
mOffset = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////
|
|
|
|
// nsIInputStream implementation
|
|
|
|
/////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::Close()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsStringInputStream::Available(uint64_t *aLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aLength, "null ptr");
|
|
|
|
|
2011-11-01 01:28:31 -07:00
|
|
|
if (Closed())
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
|
|
|
*aLength = LengthRemaining();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsStringInputStream::Read(char* aBuf, uint32_t aCount, uint32_t *aReadCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aBuf, "null ptr");
|
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, aReadCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::ReadSegments(nsWriteSegmentFun writer, void *closure,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t aCount, uint32_t *result)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(result, "null ptr");
|
2011-11-01 01:28:31 -07:00
|
|
|
NS_ASSERTION(Length() >= mOffset, "bad stream state");
|
|
|
|
|
|
|
|
if (Closed())
|
|
|
|
return NS_BASE_STREAM_CLOSED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// We may be at end-of-file
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t maxCount = LengthRemaining();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (maxCount == 0) {
|
|
|
|
*result = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aCount > maxCount)
|
|
|
|
aCount = maxCount;
|
2011-11-01 01:28:31 -07:00
|
|
|
nsresult rv = writer(this, closure, mData.BeginReading() + mOffset, 0, aCount, result);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
NS_ASSERTION(*result <= aCount,
|
|
|
|
"writer should not write more than we asked it to write");
|
|
|
|
mOffset += *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// errors returned from the writer end here!
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsStringInputStream::IsNonBlocking(bool *aNonBlocking)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
*aNonBlocking = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////
|
|
|
|
// nsISeekableStream implementation
|
|
|
|
/////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsStringInputStream::Seek(int32_t whence, int64_t offset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-11-01 01:28:31 -07:00
|
|
|
if (Closed())
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
|
|
|
// Compute new stream position. The given offset may be a negative value.
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t newPos = offset;
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (whence) {
|
|
|
|
case NS_SEEK_SET:
|
|
|
|
break;
|
|
|
|
case NS_SEEK_CUR:
|
2011-11-01 01:28:31 -07:00
|
|
|
newPos += mOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
case NS_SEEK_END:
|
2011-11-01 01:28:31 -07:00
|
|
|
newPos += Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("invalid whence");
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_ARG(newPos >= 0);
|
2011-11-01 01:28:31 -07:00
|
|
|
NS_ENSURE_ARG(newPos <= Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
mOffset = (uint32_t)newPos;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsStringInputStream::Tell(int64_t* outWhere)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-11-01 01:28:31 -07:00
|
|
|
if (Closed())
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
|
|
|
*outWhere = mOffset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::SetEOF()
|
|
|
|
{
|
2011-11-01 01:28:31 -07:00
|
|
|
if (Closed())
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
|
|
|
|
2011-11-01 01:28:31 -07:00
|
|
|
mOffset = Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-15 21:02:32 -07:00
|
|
|
void
|
|
|
|
nsStringInputStream::Serialize(InputStreamParams& aParams)
|
|
|
|
{
|
|
|
|
StringInputStreamParams params;
|
|
|
|
params.data() = PromiseFlatCString(mData);
|
|
|
|
aParams = params;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsStringInputStream::Deserialize(const InputStreamParams& aParams)
|
|
|
|
{
|
|
|
|
if (aParams.type() != InputStreamParams::TStringInputStreamParams) {
|
|
|
|
NS_ERROR("Received unknown parameters from the other process!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const StringInputStreamParams& params =
|
|
|
|
aParams.get_StringInputStreamParams();
|
|
|
|
|
|
|
|
if (NS_FAILED(SetData(params.data()))) {
|
|
|
|
NS_WARNING("SetData failed!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NewByteInputStream(nsIInputStream** aStreamResult,
|
2012-08-22 08:56:38 -07:00
|
|
|
const char* aStringToRead, int32_t aLength,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsAssignmentType aAssignment)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aStreamResult, "null out ptr");
|
|
|
|
|
|
|
|
nsStringInputStream* stream = new nsStringInputStream();
|
|
|
|
if (! stream)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(stream);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
switch (aAssignment) {
|
|
|
|
case NS_ASSIGNMENT_COPY:
|
|
|
|
rv = stream->SetData(aStringToRead, aLength);
|
|
|
|
break;
|
|
|
|
case NS_ASSIGNMENT_DEPEND:
|
|
|
|
rv = stream->ShareData(aStringToRead, aLength);
|
|
|
|
break;
|
|
|
|
case NS_ASSIGNMENT_ADOPT:
|
2007-07-08 00:08:04 -07:00
|
|
|
rv = stream->AdoptData(const_cast<char*>(aStringToRead), aLength);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("invalid assignment type");
|
|
|
|
rv = NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(stream);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aStreamResult = stream;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NewStringInputStream(nsIInputStream** aStreamResult,
|
|
|
|
const nsAString& aStringToRead)
|
|
|
|
{
|
2011-11-01 01:28:31 -07:00
|
|
|
NS_LossyConvertUTF16toASCII data(aStringToRead); // truncates high-order bytes
|
|
|
|
return NS_NewCStringInputStream(aStreamResult, data);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-08-18 06:46:39 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_NewCStringInputStream(nsIInputStream** aStreamResult,
|
|
|
|
const nsACString& aStringToRead)
|
|
|
|
{
|
2011-11-01 01:28:31 -07:00
|
|
|
NS_PRECONDITION(aStreamResult, "null out ptr");
|
|
|
|
|
|
|
|
nsStringInputStream* stream = new nsStringInputStream();
|
|
|
|
if (! stream)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-11-01 01:28:31 -07:00
|
|
|
NS_ADDREF(stream);
|
|
|
|
|
|
|
|
stream->SetData(aStringToRead);
|
|
|
|
|
|
|
|
*aStreamResult = stream;
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// factory method for constructing a nsStringInputStream object
|
2010-06-10 11:11:11 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
nsStringInputStreamConstructor(nsISupports *outer, REFNSIID iid, void **result)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*result = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(!outer, NS_ERROR_NO_AGGREGATION);
|
|
|
|
|
|
|
|
nsStringInputStream *inst = new nsStringInputStream();
|
|
|
|
if (!inst)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(inst);
|
|
|
|
nsresult rv = inst->QueryInterface(iid, result);
|
|
|
|
NS_RELEASE(inst);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|