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/. */
|
2001-11-22 02:55:10 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Based on original code from nsIStringStream.cpp
|
|
|
|
*/
|
|
|
|
|
2012-08-28 16:41:04 +04:00
|
|
|
#include "ipc/IPCMessageUtils.h"
|
2010-10-21 22:36:13 +04:00
|
|
|
|
2001-11-22 02:55:10 +03:00
|
|
|
#include "nsStringStream.h"
|
2006-01-02 05:30:32 +03:00
|
|
|
#include "nsStreamUtils.h"
|
2001-11-22 02:55:10 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2015-02-11 07:55:43 +03:00
|
|
|
#include "nsICloneableInputStream.h"
|
2003-03-15 04:04:32 +03:00
|
|
|
#include "nsISeekableStream.h"
|
2006-01-20 22:17:58 +03:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "prerror.h"
|
|
|
|
#include "plstr.h"
|
2006-03-15 07:59:42 +03:00
|
|
|
#include "nsIClassInfoImpl.h"
|
2012-06-06 03:51:58 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-08-23 23:33:46 +04:00
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
2012-08-16 08:02:32 +04:00
|
|
|
#include "nsIIPCSerializableInputStream.h"
|
|
|
|
|
|
|
|
using namespace mozilla::ipc;
|
2003-03-15 04:04:32 +03:00
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// nsIStringInputStream implementation
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
class nsStringInputStream MOZ_FINAL
|
|
|
|
: public nsIStringInputStream
|
|
|
|
, public nsISeekableStream
|
|
|
|
, public nsISupportsCString
|
|
|
|
, public nsIIPCSerializableInputStream
|
2015-02-11 07:55:43 +03:00
|
|
|
, public nsICloneableInputStream
|
2003-07-12 03:10:27 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
NS_DECL_NSISTRINGINPUTSTREAM
|
|
|
|
NS_DECL_NSISEEKABLESTREAM
|
|
|
|
NS_DECL_NSISUPPORTSPRIMITIVE
|
|
|
|
NS_DECL_NSISUPPORTSCSTRING
|
|
|
|
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
|
2015-02-11 07:55:43 +03:00
|
|
|
NS_DECL_NSICLONEABLEINPUTSTREAM
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
nsStringInputStream()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
explicit nsStringInputStream(const nsStringInputStream& aOther)
|
|
|
|
: mOffset(aOther.mOffset)
|
|
|
|
{
|
|
|
|
// Use Assign() here because we don't want the life of the clone to be
|
|
|
|
// dependent on the life of the original stream.
|
|
|
|
mData.Assign(aOther.mData);
|
|
|
|
}
|
|
|
|
|
2004-02-11 09:19:03 +03:00
|
|
|
private:
|
2014-05-05 21:30:43 +04:00
|
|
|
~nsStringInputStream()
|
2014-05-15 00:15:46 +04:00
|
|
|
{
|
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
uint32_t Length() const
|
|
|
|
{
|
|
|
|
return mData.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t LengthRemaining() const
|
|
|
|
{
|
|
|
|
return Length() - mOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
mData.SetIsVoid(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Closed()
|
|
|
|
{
|
|
|
|
return mData.IsVoid();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentCSubstring mData;
|
|
|
|
uint32_t mOffset;
|
2003-07-12 03:10:27 +04:00
|
|
|
};
|
|
|
|
|
2006-01-02 05:30:32 +03: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-19 06:31:26 +04:00
|
|
|
NS_IMPL_ADDREF(nsStringInputStream)
|
|
|
|
NS_IMPL_RELEASE(nsStringInputStream)
|
2006-01-20 22:17:58 +03:00
|
|
|
|
2013-10-11 00:41:39 +04:00
|
|
|
NS_IMPL_CLASSINFO(nsStringInputStream, nullptr, nsIClassInfo::THREADSAFE,
|
2010-06-22 20:59:57 +04:00
|
|
|
NS_STRINGINPUTSTREAM_CID)
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_QUERY_INTERFACE_CI(nsStringInputStream,
|
|
|
|
nsIStringInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsISupportsCString,
|
|
|
|
nsISeekableStream,
|
2015-02-11 07:55:43 +03:00
|
|
|
nsIIPCSerializableInputStream,
|
|
|
|
nsICloneableInputStream)
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_CI_INTERFACE_GETTER(nsStringInputStream,
|
2006-01-20 22:17:58 +03:00
|
|
|
nsIStringInputStream,
|
|
|
|
nsIInputStream,
|
|
|
|
nsISupportsCString,
|
2015-02-11 07:55:43 +03:00
|
|
|
nsISeekableStream,
|
|
|
|
nsICloneableInputStream)
|
2003-07-12 03:10:27 +04:00
|
|
|
|
|
|
|
/////////
|
2006-01-20 22:17:58 +03:00
|
|
|
// nsISupportsCString implementation
|
2003-07-12 03:10:27 +04:00
|
|
|
/////////
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2006-01-20 22:17:58 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::GetType(uint16_t* aType)
|
2006-01-20 22:17:58 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
*aType = TYPE_CSTRING;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2006-01-20 22:17:58 +03:00
|
|
|
}
|
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::GetData(nsACString& data)
|
2006-01-02 05:30:32 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
// 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.
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(Closed())) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
data.Assign(mData);
|
|
|
|
return NS_OK;
|
2006-01-02 05:30:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::SetData(const nsACString& aData)
|
2006-01-02 05:30:32 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
mData.Assign(aData);
|
2014-05-05 21:30:43 +04:00
|
|
|
mOffset = 0;
|
|
|
|
return NS_OK;
|
2006-01-02 05:30:32 +03:00
|
|
|
}
|
|
|
|
|
2006-01-20 22:17:58 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::ToString(char** aResult)
|
2006-01-20 22:17:58 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
// NOTE: This method may result in data loss, so we do not implement it.
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2006-01-20 22:17:58 +03:00
|
|
|
}
|
|
|
|
|
2006-01-10 04:56:00 +03:00
|
|
|
/////////
|
|
|
|
// nsIStringInputStream implementation
|
|
|
|
/////////
|
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::SetData(const char* aData, int32_t aDataLen)
|
2003-07-12 03:10:27 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(!aData)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
mData.Assign(aData, aDataLen);
|
2014-05-05 21:30:43 +04:00
|
|
|
mOffset = 0;
|
|
|
|
return NS_OK;
|
2003-07-12 03:10:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::AdoptData(char* aData, int32_t aDataLen)
|
2003-07-12 03:10:27 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(!aData)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
|
|
|
mData.Adopt(aData, aDataLen);
|
2014-05-05 21:30:43 +04:00
|
|
|
mOffset = 0;
|
|
|
|
return NS_OK;
|
2003-07-12 03:10:27 +04:00
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::ShareData(const char* aData, int32_t aDataLen)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(!aData)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2003-07-12 03:10:27 +04:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aDataLen < 0) {
|
|
|
|
aDataLen = strlen(aData);
|
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
mData.Rebind(aData, aDataLen);
|
2014-05-05 21:30:43 +04:00
|
|
|
mOffset = 0;
|
|
|
|
return NS_OK;
|
2003-07-12 03:10:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////
|
|
|
|
// nsIInputStream implementation
|
|
|
|
/////////
|
2006-01-02 05:30:32 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::Close()
|
2003-07-12 03:10:27 +04:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
Clear();
|
|
|
|
return NS_OK;
|
2003-07-12 03:10:27 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::Available(uint64_t* aLength)
|
2003-07-12 03:10:27 +04:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(aLength, "null ptr");
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (Closed()) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
*aLength = LengthRemaining();
|
|
|
|
return NS_OK;
|
2003-07-12 03:10:27 +04:00
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::Read(char* aBuf, uint32_t aCount, uint32_t* aReadCount)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(aBuf, "null ptr");
|
|
|
|
return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, aReadCount);
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
|
|
|
|
uint32_t aCount, uint32_t* aResult)
|
2003-07-12 03:10:27 +04:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(aResult, "null ptr");
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ASSERTION(Length() >= mOffset, "bad stream state");
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (Closed()) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// We may be at end-of-file
|
|
|
|
uint32_t maxCount = LengthRemaining();
|
|
|
|
if (maxCount == 0) {
|
2014-05-15 00:15:46 +04:00
|
|
|
*aResult = 0;
|
2003-07-12 03:10:27 +04:00
|
|
|
return NS_OK;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (aCount > maxCount) {
|
2014-05-05 21:30:43 +04:00
|
|
|
aCount = maxCount;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-08-25 23:17:15 +04:00
|
|
|
nsresult rv = aWriter(this, aClosure, mData.BeginReading() + mOffset, 0,
|
|
|
|
aCount, aResult);
|
2014-05-05 21:30:43 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ASSERTION(*aResult <= aCount,
|
2014-05-05 21:30:43 +04:00
|
|
|
"writer should not write more than we asked it to write");
|
2014-05-15 00:15:46 +04:00
|
|
|
mOffset += *aResult;
|
2014-05-05 21:30:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// errors returned from the writer end here!
|
|
|
|
return NS_OK;
|
2003-07-12 03:10:27 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::IsNonBlocking(bool* aNonBlocking)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
*aNonBlocking = true;
|
|
|
|
return NS_OK;
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|
|
|
|
|
2003-07-12 03:10:27 +04:00
|
|
|
/////////
|
|
|
|
// nsISeekableStream implementation
|
|
|
|
/////////
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::Seek(int32_t aWhence, int64_t aOffset)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (Closed()) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2014-05-05 21:30:43 +04:00
|
|
|
|
|
|
|
// Compute new stream position. The given offset may be a negative value.
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
int64_t newPos = aOffset;
|
|
|
|
switch (aWhence) {
|
2006-01-02 05:30:32 +03:00
|
|
|
case NS_SEEK_SET:
|
2014-05-05 21:30:43 +04:00
|
|
|
break;
|
2006-01-02 05:30:32 +03:00
|
|
|
case NS_SEEK_CUR:
|
2014-05-05 21:30:43 +04:00
|
|
|
newPos += mOffset;
|
|
|
|
break;
|
2006-01-02 05:30:32 +03:00
|
|
|
case NS_SEEK_END:
|
2014-05-05 21:30:43 +04:00
|
|
|
newPos += Length();
|
|
|
|
break;
|
2006-01-02 05:30:32 +03:00
|
|
|
default:
|
2014-05-15 00:15:46 +04:00
|
|
|
NS_ERROR("invalid aWhence");
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(newPos < 0) || NS_WARN_IF(newPos > Length())) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
mOffset = (uint32_t)newPos;
|
|
|
|
return NS_OK;
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_IMETHODIMP
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream::Tell(int64_t* aOutWhere)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (Closed()) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
*aOutWhere = mOffset;
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_OK;
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|
|
|
|
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::SetEOF()
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
if (Closed()) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_BASE_STREAM_CLOSED;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
mOffset = Length();
|
|
|
|
return NS_OK;
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
/////////
|
|
|
|
// nsIIPCSerializableInputStream implementation
|
|
|
|
/////////
|
|
|
|
|
2012-08-16 08:02:32 +04:00
|
|
|
void
|
2014-03-25 22:37:13 +04:00
|
|
|
nsStringInputStream::Serialize(InputStreamParams& aParams,
|
|
|
|
FileDescriptorArray& /* aFDs */)
|
2012-08-16 08:02:32 +04:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
StringInputStreamParams params;
|
|
|
|
params.data() = PromiseFlatCString(mData);
|
|
|
|
aParams = params;
|
2012-08-16 08:02:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-03-25 22:37:13 +04:00
|
|
|
nsStringInputStream::Deserialize(const InputStreamParams& aParams,
|
|
|
|
const FileDescriptorArray& /* aFDs */)
|
2012-08-16 08:02:32 +04:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
if (aParams.type() != InputStreamParams::TStringInputStreamParams) {
|
|
|
|
NS_ERROR("Received unknown parameters from the other process!");
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
const StringInputStreamParams& params =
|
|
|
|
aParams.get_StringInputStreamParams();
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
if (NS_FAILED(SetData(params.data()))) {
|
|
|
|
NS_WARNING("SetData failed!");
|
|
|
|
return false;
|
|
|
|
}
|
2012-08-16 08:02:32 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return true;
|
2012-08-16 08:02:32 +04:00
|
|
|
}
|
|
|
|
|
2015-02-11 07:55:43 +03:00
|
|
|
/////////
|
|
|
|
// nsICloneableInputStream implementation
|
|
|
|
/////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::GetCloneable(bool* aCloneableOut)
|
|
|
|
{
|
|
|
|
*aCloneableOut = true;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStringInputStream::Clone(nsIInputStream** aCloneOut)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsIInputStream> ref = new nsStringInputStream(*this);
|
|
|
|
ref.forget(aCloneOut);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_NewByteInputStream(nsIInputStream** aStreamResult,
|
2012-08-22 19:56:38 +04:00
|
|
|
const char* aStringToRead, int32_t aLength,
|
2006-01-02 05:30:32 +03:00
|
|
|
nsAssignmentType aAssignment)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_PRECONDITION(aStreamResult, "null out ptr");
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsStringInputStream* stream = new nsStringInputStream();
|
2014-08-25 23:17:15 +04:00
|
|
|
if (!stream) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ADDREF(stream);
|
2004-02-11 09:19:03 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsresult rv;
|
|
|
|
switch (aAssignment) {
|
2006-01-02 05:30:32 +03:00
|
|
|
case NS_ASSIGNMENT_COPY:
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = stream->SetData(aStringToRead, aLength);
|
|
|
|
break;
|
2006-01-02 05:30:32 +03:00
|
|
|
case NS_ASSIGNMENT_DEPEND:
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = stream->ShareData(aStringToRead, aLength);
|
|
|
|
break;
|
2006-01-02 05:30:32 +03:00
|
|
|
case NS_ASSIGNMENT_ADOPT:
|
2014-05-05 21:30:43 +04:00
|
|
|
rv = stream->AdoptData(const_cast<char*>(aStringToRead), aLength);
|
|
|
|
break;
|
2006-01-02 05:30:32 +03:00
|
|
|
default:
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ERROR("invalid assignment type");
|
|
|
|
rv = NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(stream);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aStreamResult = stream;
|
|
|
|
return NS_OK;
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_NewStringInputStream(nsIInputStream** aStreamResult,
|
|
|
|
const nsAString& aStringToRead)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_LossyConvertUTF16toASCII data(aStringToRead); // truncates high-order bytes
|
|
|
|
return NS_NewCStringInputStream(aStreamResult, data);
|
2006-01-02 05:30:32 +03:00
|
|
|
}
|
2004-02-11 09:19:03 +03:00
|
|
|
|
2011-08-18 17:46:39 +04:00
|
|
|
nsresult
|
2006-01-02 05:30:32 +03:00
|
|
|
NS_NewCStringInputStream(nsIInputStream** aStreamResult,
|
|
|
|
const nsACString& aStringToRead)
|
|
|
|
{
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_PRECONDITION(aStreamResult, "null out ptr");
|
2011-11-01 12:28:31 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
nsStringInputStream* stream = new nsStringInputStream();
|
2014-08-25 23:17:15 +04:00
|
|
|
if (!stream) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2006-01-02 05:30:32 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ADDREF(stream);
|
2011-11-01 12:28:31 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
stream->SetData(aStringToRead);
|
2011-11-01 12:28:31 +04:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
*aStreamResult = stream;
|
|
|
|
return NS_OK;
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// factory method for constructing a nsStringInputStream object
|
2010-06-10 22:11:11 +04:00
|
|
|
nsresult
|
2014-08-25 23:17:15 +04:00
|
|
|
nsStringInputStreamConstructor(nsISupports* aOuter, REFNSIID aIID,
|
|
|
|
void** aResult)
|
2001-11-22 02:55:10 +03:00
|
|
|
{
|
2014-05-15 00:15:46 +04:00
|
|
|
*aResult = nullptr;
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
if (NS_WARN_IF(aOuter)) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2014-05-15 00:15:46 +04:00
|
|
|
nsStringInputStream* inst = new nsStringInputStream();
|
|
|
|
if (!inst) {
|
2014-05-05 21:30:43 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2014-05-15 00:15:46 +04:00
|
|
|
}
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_ADDREF(inst);
|
2014-05-15 00:15:46 +04:00
|
|
|
nsresult rv = inst->QueryInterface(aIID, aResult);
|
2014-05-05 21:30:43 +04:00
|
|
|
NS_RELEASE(inst);
|
2001-11-22 02:55:10 +03:00
|
|
|
|
2014-05-05 21:30:43 +04:00
|
|
|
return rv;
|
2001-11-22 02:55:10 +03:00
|
|
|
}
|