/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* 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/. */ /** * The MIME stream separates headers and a datastream. It also allows * automatic creation of the content-length header. */ #include "nsMIMEInputStream.h" #include #include "ipc/IPCMessageUtils.h" #include "mozilla/Mutex.h" #include "mozilla/ipc/InputStreamUtils.h" #include "nsCOMPtr.h" #include "nsComponentManagerUtils.h" #include "nsIAsyncInputStream.h" #include "nsIClassInfoImpl.h" #include "nsIHttpHeaderVisitor.h" #include "nsIIPCSerializableInputStream.h" #include "nsIInputStreamLength.h" #include "nsIMIMEInputStream.h" #include "nsISeekableStream.h" #include "nsString.h" using namespace mozilla::ipc; using mozilla::Maybe; class nsMIMEInputStream : public nsIMIMEInputStream, public nsISeekableStream, public nsIIPCSerializableInputStream, public nsIAsyncInputStream, public nsIInputStreamCallback, public nsIInputStreamLength, public nsIAsyncInputStreamLength, public nsIInputStreamLengthCallback, public nsICloneableInputStream { virtual ~nsMIMEInputStream() = default; public: nsMIMEInputStream(); NS_DECL_THREADSAFE_ISUPPORTS NS_DECL_NSIINPUTSTREAM NS_DECL_NSIMIMEINPUTSTREAM NS_DECL_NSISEEKABLESTREAM NS_DECL_NSITELLABLESTREAM NS_DECL_NSIIPCSERIALIZABLEINPUTSTREAM NS_DECL_NSIASYNCINPUTSTREAM NS_DECL_NSIINPUTSTREAMCALLBACK NS_DECL_NSIINPUTSTREAMLENGTH NS_DECL_NSIASYNCINPUTSTREAMLENGTH NS_DECL_NSIINPUTSTREAMLENGTHCALLBACK NS_DECL_NSICLONEABLEINPUTSTREAM private: void InitStreams(); template void SerializeInternal(InputStreamParams& aParams, FileDescriptorArray& aFileDescriptors, bool aDelayedStart, uint32_t aMaxSize, uint32_t* aSizeUsed, M* aManager); struct MOZ_STACK_CLASS ReadSegmentsState { nsCOMPtr mThisStream; nsWriteSegmentFun mWriter; void* mClosure; }; static nsresult ReadSegCb(nsIInputStream* aIn, void* aClosure, const char* aFromRawSegment, uint32_t aToOffset, uint32_t aCount, uint32_t* aWriteCount); bool IsSeekableInputStream() const; bool IsAsyncInputStream() const; bool IsInputStreamLength() const; bool IsAsyncInputStreamLength() const; bool IsCloneableInputStream() const; nsTArray mHeaders; nsCOMPtr mStream; bool mStartedReading; mozilla::Mutex mMutex; // This is protected by mutex. nsCOMPtr mAsyncWaitCallback; // This is protected by mutex. nsCOMPtr mAsyncInputStreamLengthCallback; }; NS_IMPL_ADDREF(nsMIMEInputStream) NS_IMPL_RELEASE(nsMIMEInputStream) NS_IMPL_CLASSINFO(nsMIMEInputStream, nullptr, nsIClassInfo::THREADSAFE, NS_MIMEINPUTSTREAM_CID) NS_INTERFACE_MAP_BEGIN(nsMIMEInputStream) NS_INTERFACE_MAP_ENTRY(nsIMIMEInputStream) NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIInputStream, nsIMIMEInputStream) NS_INTERFACE_MAP_ENTRY(nsITellableStream) NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsISeekableStream, IsSeekableInputStream()) NS_INTERFACE_MAP_ENTRY(nsIIPCSerializableInputStream) NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStream, IsAsyncInputStream()) NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIInputStreamCallback, IsAsyncInputStream()) NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMIMEInputStream) NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIInputStreamLength, IsInputStreamLength()) NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIAsyncInputStreamLength, IsAsyncInputStreamLength()) NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIInputStreamLengthCallback, IsAsyncInputStreamLength()) NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICloneableInputStream, IsCloneableInputStream()) NS_IMPL_QUERY_CLASSINFO(nsMIMEInputStream) NS_INTERFACE_MAP_END NS_IMPL_CI_INTERFACE_GETTER(nsMIMEInputStream, nsIMIMEInputStream, nsIAsyncInputStream, nsIInputStream, nsISeekableStream, nsITellableStream) nsMIMEInputStream::nsMIMEInputStream() : mStartedReading(false), mMutex("nsMIMEInputStream::mMutex") {} NS_IMETHODIMP nsMIMEInputStream::AddHeader(const char* aName, const char* aValue) { NS_ENSURE_FALSE(mStartedReading, NS_ERROR_FAILURE); HeaderEntry* entry = mHeaders.AppendElement(); entry->name().Append(aName); entry->value().Append(aValue); return NS_OK; } NS_IMETHODIMP nsMIMEInputStream::VisitHeaders(nsIHttpHeaderVisitor* visitor) { nsresult rv; for (auto& header : mHeaders) { rv = visitor->VisitHeader(header.name(), header.value()); if (NS_FAILED(rv)) { return rv; } } return NS_OK; } NS_IMETHODIMP nsMIMEInputStream::SetData(nsIInputStream* aStream) { NS_ENSURE_FALSE(mStartedReading, NS_ERROR_FAILURE); nsCOMPtr seekable = do_QueryInterface(aStream); if (!seekable) { return NS_ERROR_INVALID_ARG; } mStream = aStream; return NS_OK; } NS_IMETHODIMP nsMIMEInputStream::GetData(nsIInputStream** aStream) { NS_ENSURE_ARG_POINTER(aStream); *aStream = mStream; NS_IF_ADDREF(*aStream); return NS_OK; } // set up the internal streams void nsMIMEInputStream::InitStreams() { NS_ASSERTION(!mStartedReading, "Don't call initStreams twice without rewinding"); mStartedReading = true; } #define INITSTREAMS \ if (!mStartedReading) { \ NS_ENSURE_TRUE(mStream, NS_ERROR_UNEXPECTED); \ InitStreams(); \ } // Reset mStartedReading when Seek-ing to start NS_IMETHODIMP nsMIMEInputStream::Seek(int32_t whence, int64_t offset) { NS_ENSURE_TRUE(mStream, NS_ERROR_UNEXPECTED); nsresult rv; nsCOMPtr stream = do_QueryInterface(mStream); if (whence == NS_SEEK_SET && offset == 0) { rv = stream->Seek(whence, offset); if (NS_SUCCEEDED(rv)) mStartedReading = false; } else { INITSTREAMS; rv = stream->Seek(whence, offset); } return rv; } // Proxy ReadSegments since we need to be a good little nsIInputStream NS_IMETHODIMP nsMIMEInputStream::ReadSegments(nsWriteSegmentFun aWriter, void* aClosure, uint32_t aCount, uint32_t* _retval) { INITSTREAMS; ReadSegmentsState state; // Disambiguate ambiguous nsIInputStream. state.mThisStream = static_cast(static_cast(this)); state.mWriter = aWriter; state.mClosure = aClosure; return mStream->ReadSegments(ReadSegCb, &state, aCount, _retval); } nsresult nsMIMEInputStream::ReadSegCb(nsIInputStream* aIn, void* aClosure, const char* aFromRawSegment, uint32_t aToOffset, uint32_t aCount, uint32_t* aWriteCount) { ReadSegmentsState* state = (ReadSegmentsState*)aClosure; return (state->mWriter)(state->mThisStream, state->mClosure, aFromRawSegment, aToOffset, aCount, aWriteCount); } /** * Forward everything else to the mStream after calling InitStreams() */ // nsIInputStream NS_IMETHODIMP nsMIMEInputStream::Close(void) { INITSTREAMS; return mStream->Close(); } NS_IMETHODIMP nsMIMEInputStream::Available(uint64_t* _retval) { INITSTREAMS; return mStream->Available(_retval); } NS_IMETHODIMP nsMIMEInputStream::Read(char* buf, uint32_t count, uint32_t* _retval) { INITSTREAMS; return mStream->Read(buf, count, _retval); } NS_IMETHODIMP nsMIMEInputStream::IsNonBlocking(bool* aNonBlocking) { INITSTREAMS; return mStream->IsNonBlocking(aNonBlocking); } // nsIAsyncInputStream NS_IMETHODIMP nsMIMEInputStream::CloseWithStatus(nsresult aStatus) { INITSTREAMS; nsCOMPtr asyncStream = do_QueryInterface(mStream); return asyncStream->CloseWithStatus(aStatus); } NS_IMETHODIMP nsMIMEInputStream::AsyncWait(nsIInputStreamCallback* aCallback, uint32_t aFlags, uint32_t aRequestedCount, nsIEventTarget* aEventTarget) { INITSTREAMS; nsCOMPtr asyncStream = do_QueryInterface(mStream); if (NS_WARN_IF(!asyncStream)) { return NS_ERROR_FAILURE; } nsCOMPtr callback = aCallback ? this : nullptr; { MutexAutoLock lock(mMutex); if (mAsyncWaitCallback && aCallback) { return NS_ERROR_FAILURE; } mAsyncWaitCallback = aCallback; } return asyncStream->AsyncWait(callback, aFlags, aRequestedCount, aEventTarget); } // nsIInputStreamCallback NS_IMETHODIMP nsMIMEInputStream::OnInputStreamReady(nsIAsyncInputStream* aStream) { nsCOMPtr callback; { MutexAutoLock lock(mMutex); // We have been canceled in the meanwhile. if (!mAsyncWaitCallback) { return NS_OK; } callback.swap(mAsyncWaitCallback); } MOZ_ASSERT(callback); return callback->OnInputStreamReady(this); } // nsITellableStream NS_IMETHODIMP nsMIMEInputStream::Tell(int64_t* _retval) { INITSTREAMS; nsCOMPtr stream = do_QueryInterface(mStream); return stream->Tell(_retval); } // nsISeekableStream NS_IMETHODIMP nsMIMEInputStream::SetEOF(void) { INITSTREAMS; nsCOMPtr stream = do_QueryInterface(mStream); return stream->SetEOF(); } /** * Factory method used by do_CreateInstance */ nsresult nsMIMEInputStreamConstructor(nsISupports* outer, REFNSIID iid, void** result) { *result = nullptr; if (outer) return NS_ERROR_NO_AGGREGATION; RefPtr inst = new nsMIMEInputStream(); if (!inst) return NS_ERROR_OUT_OF_MEMORY; return inst->QueryInterface(iid, result); } void nsMIMEInputStream::Serialize( InputStreamParams& aParams, FileDescriptorArray& aFileDescriptors, bool aDelayedStart, uint32_t aMaxSize, uint32_t* aSizeUsed, mozilla::ipc::ParentToChildStreamActorManager* aManager) { SerializeInternal(aParams, aFileDescriptors, aDelayedStart, aMaxSize, aSizeUsed, aManager); } void nsMIMEInputStream::Serialize( InputStreamParams& aParams, FileDescriptorArray& aFileDescriptors, bool aDelayedStart, uint32_t aMaxSize, uint32_t* aSizeUsed, mozilla::ipc::ChildToParentStreamActorManager* aManager) { SerializeInternal(aParams, aFileDescriptors, aDelayedStart, aMaxSize, aSizeUsed, aManager); } template void nsMIMEInputStream::SerializeInternal(InputStreamParams& aParams, FileDescriptorArray& aFileDescriptors, bool aDelayedStart, uint32_t aMaxSize, uint32_t* aSizeUsed, M* aManager) { MOZ_ASSERT(aSizeUsed); *aSizeUsed = 0; MIMEInputStreamParams params; params.headers() = mHeaders.Clone(); params.startedReading() = mStartedReading; if (!mStream) { aParams = params; return; } InputStreamParams wrappedParams; if (nsCOMPtr serializable = do_QueryInterface(mStream)) { InputStreamHelper::SerializeInputStream(mStream, wrappedParams, aFileDescriptors, aDelayedStart, aMaxSize, aSizeUsed, aManager); } else { // Falling back to sending the underlying stream over a pipe when // sending an nsMIMEInputStream over IPC is potentially wasteful // if it is sent several times. This can possibly happen with // fission. There are two ways to improve this, see bug 1648369 // and bug 1648370. InputStreamHelper::SerializeInputStreamAsPipe(mStream, wrappedParams, aDelayedStart, aManager); } NS_ASSERTION(wrappedParams.type() != InputStreamParams::T__None, "Wrapped stream failed to serialize!"); params.optionalStream().emplace(wrappedParams); aParams = params; } bool nsMIMEInputStream::Deserialize( const InputStreamParams& aParams, const FileDescriptorArray& aFileDescriptors) { if (aParams.type() != InputStreamParams::TMIMEInputStreamParams) { NS_ERROR("Received unknown parameters from the other process!"); return false; } const MIMEInputStreamParams& params = aParams.get_MIMEInputStreamParams(); const Maybe& wrappedParams = params.optionalStream(); mHeaders = params.headers().Clone(); mStartedReading = params.startedReading(); if (wrappedParams.isSome()) { nsCOMPtr stream; stream = InputStreamHelper::DeserializeInputStream(wrappedParams.ref(), aFileDescriptors); if (!stream) { NS_WARNING("Failed to deserialize wrapped stream!"); return false; } mStream = stream; } return true; } NS_IMETHODIMP nsMIMEInputStream::Length(int64_t* aLength) { nsCOMPtr stream = do_QueryInterface(mStream); if (NS_WARN_IF(!stream)) { return NS_ERROR_FAILURE; } return stream->Length(aLength); } NS_IMETHODIMP nsMIMEInputStream::AsyncLengthWait(nsIInputStreamLengthCallback* aCallback, nsIEventTarget* aEventTarget) { nsCOMPtr stream = do_QueryInterface(mStream); if (NS_WARN_IF(!stream)) { return NS_ERROR_FAILURE; } nsCOMPtr callback = aCallback ? this : nullptr; { MutexAutoLock lock(mMutex); mAsyncInputStreamLengthCallback = aCallback; } return stream->AsyncLengthWait(callback, aEventTarget); } NS_IMETHODIMP nsMIMEInputStream::OnInputStreamLengthReady(nsIAsyncInputStreamLength* aStream, int64_t aLength) { nsCOMPtr callback; { MutexAutoLock lock(mMutex); // We have been canceled in the meanwhile. if (!mAsyncInputStreamLengthCallback) { return NS_OK; } callback.swap(mAsyncInputStreamLengthCallback); } MOZ_ASSERT(callback); return callback->OnInputStreamLengthReady(this, aLength); } bool nsMIMEInputStream::IsSeekableInputStream() const { nsCOMPtr seekable = do_QueryInterface(mStream); return !!seekable; } bool nsMIMEInputStream::IsAsyncInputStream() const { nsCOMPtr asyncStream = do_QueryInterface(mStream); return !!asyncStream; } bool nsMIMEInputStream::IsInputStreamLength() const { nsCOMPtr stream = do_QueryInterface(mStream); return !!stream; } bool nsMIMEInputStream::IsAsyncInputStreamLength() const { nsCOMPtr stream = do_QueryInterface(mStream); return !!stream; } bool nsMIMEInputStream::IsCloneableInputStream() const { nsCOMPtr stream = do_QueryInterface(mStream); return !!stream; } // nsICloneableInputStream interface NS_IMETHODIMP nsMIMEInputStream::GetCloneable(bool* aCloneable) { nsCOMPtr stream = do_QueryInterface(mStream); if (!mStream) { return NS_ERROR_FAILURE; } return stream->GetCloneable(aCloneable); } NS_IMETHODIMP nsMIMEInputStream::Clone(nsIInputStream** aResult) { nsCOMPtr stream = do_QueryInterface(mStream); if (!mStream) { return NS_ERROR_FAILURE; } nsCOMPtr clonedStream; nsresult rv = stream->Clone(getter_AddRefs(clonedStream)); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } nsCOMPtr mimeStream = new nsMIMEInputStream(); rv = mimeStream->SetData(clonedStream); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } for (const HeaderEntry& entry : mHeaders) { rv = mimeStream->AddHeader(entry.name().get(), entry.value().get()); MOZ_ASSERT(NS_SUCCEEDED(rv)); } static_cast(mimeStream.get())->mStartedReading = mStartedReading; mimeStream.forget(aResult); return NS_OK; }