Files
palemoon27/xpcom/io/nsStringStream.cpp
T
roytam1 16aec83f00 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1175600. Add getRelativePath/setRelativePath to nsIFile. r=froydnj (93515cbda)
- bug 1171124 - Swap some XP_MACOSX for XP_DARWIN in nsLocalFileUnix. r=froydnj (1928918ac)
- Bug 1192230 - clean up reference-counting in xpcom/; r=erahm (11528cd9f)
- missing bit of Bug 1138293 - Use malloc/free/realloc/calloc (365fb9991)
- Bug 1175601. Make nsIFile.getRelativeDescriptor work with paths with aths with arbitrary numbers of segments, not just 32. r=froydnj (a4b5ea11d)
- Bug 1175600 followup. Add getRelativePath to ye othere nsIFile implementatione so as to de-bust ye olde CLOSED TREE (b23dfee9e)
- Bug 1159604 - Use fallible allocations in nsLinebreakConverter.cpp (as the code expects). r=smaug (498f30023)
- Bug 1139547 - Fix unsequenced variable modmification/access error. r=botond,bsmedberg (a8460ecff)
- Bug 1167380 - Mark (Read/Write)SegmentsState::m(Source/Sink) as MOZ_NON_OWNING_REF and mark as MOZ_STACK_CLASS. r=froydnj (a1f2dc2ae)
- Bug 1137464 - add missing include for nsICloneableInputStream.h to nsStreamUtils.cpp; r=bkelly (dddd1831c)
- Bug 1026761 - CID 749761: nsAStreamCopier::Process can use sourceCondition, sinkCondition uninitialized. r=froydnj (a728f2b3e)
- Bug 1189423 - part 1 - Add MOZ_COUNT_CTOR/DTOR for nsXPTCStubBase. r=froydnj (208b578bb)
- Bug 1189423 - part 2 - Remove superfluous |new| result check. r=froydnj (b58d8b1fd)
- Bug 1151506 - Null-check the count parameter in GetConstantCount. r=froydnj (6e89f6635)
- Bug 1171603 - Better size check in nsTSubstring::ReplacePrep. r=ehsan (f17bb9c07)
- Bug 603201 - Enable primitive receivers in [[Get]]. r=jorendorff (24e6b04e2)
- revert/align malloc functions to bug 1156317 21 April 2015 (89de8cbad)
- Bug 1081260 - Update the malloc counters if we successfully recover from OOM; r=jonco (c3a03f041)
- Bug 1196210 - Fix an incomplete assertion in the nursery; r=jonco (84140599a)
- Bug 1198826 - Increment (plain) gcNumber on all GCs, r=terrence (a45b9d65c)
- Bug 1184578 - Fix the nursery profiling print statement; r=sfink (f03accbe4)
- Bug 1188878 - Ensure RematerializedFrames are cleared from Debugger instances on Ion bailout failure. (r=jandem) (0cad2e549)
- pointer style (96793b3b3)
- some leftover (7685205e9)
- pointer style (5dc480bd4)
- Bug 1145781 - Unlazify functions when getting their debug scopes. (r=jimb) (f86c2a1b6)
- Bug 1148388 - Handle lost accesses in missing Debugger scopes for block objects. (r=jimb) (9c1734834)
- Bug 1118865 - Relax assertion in DebugScopeProxy::isMagicMissingArgumentsValue. (r=nbp) (7b46b3929)
- Bug 1193046 - Clear prevUpToDate on younger frames when toggling frame debuggeeness off->on. (r=jimb) (f93459582)
- Bug 1188334 - Fix this one weird case with creating debug block scopes of 0-variable block scopes that come from deprecated let exprs inside generators. (r=jimb) (69cc9afe9)
- code style (dc626020e)
- Bug 1135703 - Ensure that lastProfilingFrame gets set appropriately on ALL JitActivations when profiling is turned on or off. r=shu (64665fe96)
- missing init, taken from TFF (dc9544999)
- pointer style (84478dba2)
- cleanup (6e2dd6ae3)
- reposition due to misspatch (738769d65)
- Bug 1191758 - Rework JS::FormatStackDump() to fix OOM handling r=terrence (680e5f8d2)
- Bug 1195545 - Add instruction reordering pass to IonMonkey, r=sunfish. (ba1441975)
2022-01-12 10:00:34 +08:00

445 lines
10 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
/**
* Based on original code from nsIStringStream.cpp
*/
#include "ipc/IPCMessageUtils.h"
#include "nsStringStream.h"
#include "nsStreamUtils.h"
#include "nsReadableUtils.h"
#include "nsICloneableInputStream.h"
#include "nsISeekableStream.h"
#include "nsISupportsPrimitives.h"
#include "nsCRT.h"
#include "prerror.h"
#include "plstr.h"
#include "nsIClassInfoImpl.h"
#include "mozilla/Attributes.h"
#include "mozilla/ipc/InputStreamUtils.h"
#include "nsIIPCSerializableInputStream.h"
using namespace mozilla::ipc;
//-----------------------------------------------------------------------------
// nsIStringInputStream implementation
//-----------------------------------------------------------------------------
class nsStringInputStream final
: public nsIStringInputStream
, public nsISeekableStream
, public nsISupportsCString
, public nsIIPCSerializableInputStream
, public nsICloneableInputStream
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIINPUTSTREAM
NS_DECL_NSISTRINGINPUTSTREAM
NS_DECL_NSISEEKABLESTREAM
NS_DECL_NSISUPPORTSPRIMITIVE
NS_DECL_NSISUPPORTSCSTRING
NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM
NS_DECL_NSICLONEABLEINPUTSTREAM
nsStringInputStream()
{
Clear();
}
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);
}
private:
~nsStringInputStream()
{
}
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;
};
// This class needs to support threadsafe refcounting since people often
// allocate a string stream, and then read it from a background thread.
NS_IMPL_ADDREF(nsStringInputStream)
NS_IMPL_RELEASE(nsStringInputStream)
NS_IMPL_CLASSINFO(nsStringInputStream, nullptr, nsIClassInfo::THREADSAFE,
NS_STRINGINPUTSTREAM_CID)
NS_IMPL_QUERY_INTERFACE_CI(nsStringInputStream,
nsIStringInputStream,
nsIInputStream,
nsISupportsCString,
nsISeekableStream,
nsIIPCSerializableInputStream,
nsICloneableInputStream)
NS_IMPL_CI_INTERFACE_GETTER(nsStringInputStream,
nsIStringInputStream,
nsIInputStream,
nsISupportsCString,
nsISeekableStream,
nsICloneableInputStream)
/////////
// nsISupportsCString implementation
/////////
NS_IMETHODIMP
nsStringInputStream::GetType(uint16_t* aType)
{
*aType = 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.
if (NS_WARN_IF(Closed())) {
return NS_BASE_STREAM_CLOSED;
}
data.Assign(mData);
return NS_OK;
}
NS_IMETHODIMP
nsStringInputStream::SetData(const nsACString& aData)
{
mData.Assign(aData);
mOffset = 0;
return NS_OK;
}
NS_IMETHODIMP
nsStringInputStream::ToString(char** aResult)
{
// NOTE: This method may result in data loss, so we do not implement it.
return NS_ERROR_NOT_IMPLEMENTED;
}
/////////
// nsIStringInputStream implementation
/////////
NS_IMETHODIMP
nsStringInputStream::SetData(const char* aData, int32_t aDataLen)
{
if (NS_WARN_IF(!aData)) {
return NS_ERROR_INVALID_ARG;
}
mData.Assign(aData, aDataLen);
mOffset = 0;
return NS_OK;
}
NS_IMETHODIMP
nsStringInputStream::AdoptData(char* aData, int32_t aDataLen)
{
if (NS_WARN_IF(!aData)) {
return NS_ERROR_INVALID_ARG;
}
mData.Adopt(aData, aDataLen);
mOffset = 0;
return NS_OK;
}
NS_IMETHODIMP
nsStringInputStream::ShareData(const char* aData, int32_t aDataLen)
{
if (NS_WARN_IF(!aData)) {
return NS_ERROR_INVALID_ARG;
}
if (aDataLen < 0) {
aDataLen = strlen(aData);
}
mData.Rebind(aData, aDataLen);
mOffset = 0;
return NS_OK;
}
/////////
// nsIInputStream implementation
/////////
NS_IMETHODIMP
nsStringInputStream::Close()
{
Clear();
return NS_OK;
}
NS_IMETHODIMP
nsStringInputStream::Available(uint64_t* aLength)
{
NS_ASSERTION(aLength, "null ptr");
if (Closed()) {
return NS_BASE_STREAM_CLOSED;
}
*aLength = LengthRemaining();
return NS_OK;
}
NS_IMETHODIMP
nsStringInputStream::Read(char* aBuf, uint32_t aCount, uint32_t* aReadCount)
{
NS_ASSERTION(aBuf, "null ptr");
return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, aReadCount);
}
NS_IMETHODIMP
nsStringInputStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure,
uint32_t aCount, uint32_t* aResult)
{
NS_ASSERTION(aResult, "null ptr");
NS_ASSERTION(Length() >= mOffset, "bad stream state");
if (Closed()) {
return NS_BASE_STREAM_CLOSED;
}
// We may be at end-of-file
uint32_t maxCount = LengthRemaining();
if (maxCount == 0) {
*aResult = 0;
return NS_OK;
}
if (aCount > maxCount) {
aCount = maxCount;
}
nsresult rv = aWriter(this, aClosure, mData.BeginReading() + mOffset, 0,
aCount, aResult);
if (NS_SUCCEEDED(rv)) {
NS_ASSERTION(*aResult <= aCount,
"writer should not write more than we asked it to write");
mOffset += *aResult;
}
// errors returned from the writer end here!
return NS_OK;
}
NS_IMETHODIMP
nsStringInputStream::IsNonBlocking(bool* aNonBlocking)
{
*aNonBlocking = true;
return NS_OK;
}
/////////
// nsISeekableStream implementation
/////////
NS_IMETHODIMP
nsStringInputStream::Seek(int32_t aWhence, int64_t aOffset)
{
if (Closed()) {
return NS_BASE_STREAM_CLOSED;
}
// Compute new stream position. The given offset may be a negative value.
int64_t newPos = aOffset;
switch (aWhence) {
case NS_SEEK_SET:
break;
case NS_SEEK_CUR:
newPos += mOffset;
break;
case NS_SEEK_END:
newPos += Length();
break;
default:
NS_ERROR("invalid aWhence");
return NS_ERROR_INVALID_ARG;
}
if (NS_WARN_IF(newPos < 0) || NS_WARN_IF(newPos > Length())) {
return NS_ERROR_INVALID_ARG;
}
mOffset = (uint32_t)newPos;
return NS_OK;
}
NS_IMETHODIMP
nsStringInputStream::Tell(int64_t* aOutWhere)
{
if (Closed()) {
return NS_BASE_STREAM_CLOSED;
}
*aOutWhere = mOffset;
return NS_OK;
}
NS_IMETHODIMP
nsStringInputStream::SetEOF()
{
if (Closed()) {
return NS_BASE_STREAM_CLOSED;
}
mOffset = Length();
return NS_OK;
}
/////////
// nsIIPCSerializableInputStream implementation
/////////
void
nsStringInputStream::Serialize(InputStreamParams& aParams,
FileDescriptorArray& /* aFDs */)
{
StringInputStreamParams params;
params.data() = PromiseFlatCString(mData);
aParams = params;
}
bool
nsStringInputStream::Deserialize(const InputStreamParams& aParams,
const FileDescriptorArray& /* aFDs */)
{
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;
}
/////////
// 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;
}
nsresult
NS_NewByteInputStream(nsIInputStream** aStreamResult,
const char* aStringToRead, int32_t aLength,
nsAssignmentType aAssignment)
{
NS_PRECONDITION(aStreamResult, "null out ptr");
nsRefPtr<nsStringInputStream> stream = new nsStringInputStream();
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:
rv = stream->AdoptData(const_cast<char*>(aStringToRead), aLength);
break;
default:
NS_ERROR("invalid assignment type");
rv = NS_ERROR_INVALID_ARG;
}
if (NS_FAILED(rv)) {
return rv;
}
stream.forget(aStreamResult);
return NS_OK;
}
nsresult
NS_NewStringInputStream(nsIInputStream** aStreamResult,
const nsAString& aStringToRead)
{
NS_LossyConvertUTF16toASCII data(aStringToRead); // truncates high-order bytes
return NS_NewCStringInputStream(aStreamResult, data);
}
nsresult
NS_NewCStringInputStream(nsIInputStream** aStreamResult,
const nsACString& aStringToRead)
{
NS_PRECONDITION(aStreamResult, "null out ptr");
nsRefPtr<nsStringInputStream> stream = new nsStringInputStream();
stream->SetData(aStringToRead);
stream.forget(aStreamResult);
return NS_OK;
}
// factory method for constructing a nsStringInputStream object
nsresult
nsStringInputStreamConstructor(nsISupports* aOuter, REFNSIID aIID,
void** aResult)
{
*aResult = nullptr;
if (NS_WARN_IF(aOuter)) {
return NS_ERROR_NO_AGGREGATION;
}
nsRefPtr<nsStringInputStream> inst = new nsStringInputStream();
return inst->QueryInterface(aIID, aResult);
}