bool nsBufferedInputStream::Deserialize(const InputStreamParams& aParams, const FileDescriptorArray& aFileDescriptors) { if (aParams.type() != InputStreamParams::TBufferedInputStreamParams) { NS_ERROR("Received unknown parameters from the other process!"); return false; } const BufferedInputStreamParams& params = aParams.get_BufferedInputStreamParams(); const OptionalInputStreamParams& wrappedParams = params.optionalStream(); nsCOMPtr<nsIInputStream> stream; if (wrappedParams.type() == OptionalInputStreamParams::TInputStreamParams) { stream = DeserializeInputStream(wrappedParams.get_InputStreamParams(), aFileDescriptors); if (!stream) { NS_WARNING("Failed to deserialize wrapped stream!"); return false; } } else { NS_ASSERTION(wrappedParams.type() == OptionalInputStreamParams::Tvoid_t, "Unknown type for OptionalInputStreamParams!"); } nsresult rv = Init(stream, params.bufferSize()); NS_ENSURE_SUCCESS(rv, false); return true; }
void nsMIMEInputStream::Serialize(InputStreamParams& aParams, FileDescriptorArray& aFileDescriptors) { MIMEInputStreamParams params; if (mData) { nsCOMPtr<nsIInputStream> stream = do_QueryInterface(mData); MOZ_ASSERT(stream); InputStreamParams wrappedParams; SerializeInputStream(stream, wrappedParams, aFileDescriptors); NS_ASSERTION(wrappedParams.type() != InputStreamParams::T__None, "Wrapped stream failed to serialize!"); params.optionalStream() = wrappedParams; } else { params.optionalStream() = mozilla::void_t(); } params.headers() = mHeaders; params.contentLength() = mContentLength; params.startedReading() = mStartedReading; params.addContentLength() = mAddContentLength; aParams = params; }
void nsBufferedInputStream::Serialize(InputStreamParams& aParams) { BufferedInputStreamParams params; if (mStream) { nsCOMPtr<nsIIPCSerializableInputStream> stream = do_QueryInterface(mStream); NS_ASSERTION(stream, "Wrapped stream is not serializable!"); InputStreamParams wrappedParams; stream->Serialize(wrappedParams); NS_ASSERTION(wrappedParams.type() != InputStreamParams::T__None, "Wrapped stream failed to serialize!"); params.optionalStream() = wrappedParams; } else { params.optionalStream() = mozilla::void_t(); } params.bufferSize() = mBufferSize; aParams = params; }
bool nsFileInputStream::Deserialize(const InputStreamParams& aParams) { NS_ASSERTION(!mFD, "Already have a file descriptor?!"); NS_ASSERTION(!mDeferredOpen, "Deferring open?!"); NS_ASSERTION(!mFile, "Should never have a file here!"); NS_ASSERTION(!mPerm, "This should always be 0!"); if (aParams.type() != InputStreamParams::TFileInputStreamParams) { NS_WARNING("Received unknown parameters from the other process!"); return false; } const FileInputStreamParams& params = aParams.get_FileInputStreamParams(); const FileDescriptor& fd = params.file(); NS_WARN_IF_FALSE(fd.IsValid(), "Received an invalid file descriptor!"); if (fd.IsValid()) { PRFileDesc* fileDesc = PR_ImportFile(PROsfd(fd.PlatformHandle())); if (!fileDesc) { NS_WARNING("Failed to import file handle!"); return false; } mFD = fileDesc; } mBehaviorFlags = params.behaviorFlags(); mIOFlags = params.ioFlags(); return true; }
void nsMultiplexInputStream::Serialize(InputStreamParams& aParams) { MultiplexInputStreamParams params; uint32_t streamCount = mStreams.Length(); if (streamCount) { InfallibleTArray<InputStreamParams>& streams = params.streams(); streams.SetCapacity(streamCount); for (uint32_t index = 0; index < streamCount; index++) { nsCOMPtr<nsIIPCSerializableInputStream> serializable = do_QueryInterface(mStreams[index]); NS_ASSERTION(serializable, "Child stream isn't serializable!"); if (serializable) { InputStreamParams childStreamParams; serializable->Serialize(childStreamParams); NS_ASSERTION(childStreamParams.type() != InputStreamParams::T__None, "Serialize failed!"); streams.AppendElement(childStreamParams); } } } params.currentStream() = mCurrentStream; params.status() = mStatus; params.startedReadingCurrent() = mStartedReadingCurrent; aParams = params; }
bool nsMultiplexInputStream::Deserialize(const InputStreamParams& aParams) { if (aParams.type() != InputStreamParams::TMultiplexInputStreamParams) { NS_ERROR("Received unknown parameters from the other process!"); return false; } const MultiplexInputStreamParams& params = aParams.get_MultiplexInputStreamParams(); const InfallibleTArray<InputStreamParams>& streams = params.streams(); uint32_t streamCount = streams.Length(); for (uint32_t index = 0; index < streamCount; index++) { nsCOMPtr<nsIInputStream> stream = DeserializeInputStream(streams[index]); if (!stream) { NS_WARNING("Deserialize failed!"); return false; } if (NS_FAILED(AppendStream(stream))) { NS_WARNING("AppendStream failed!"); return false; } } mCurrentStream = params.currentStream(); mStatus = params.status(); mStartedReadingCurrent = params.startedReadingCurrent(); return true; }
bool nsFileInputStream::Deserialize(const InputStreamParams& aParams, const FileDescriptorArray& aFileDescriptors) { NS_ASSERTION(!mFD, "Already have a file descriptor?!"); NS_ASSERTION(!mDeferredOpen, "Deferring open?!"); NS_ASSERTION(!mFile, "Should never have a file here!"); NS_ASSERTION(!mPerm, "This should always be 0!"); if (aParams.type() != InputStreamParams::TFileInputStreamParams) { NS_WARNING("Received unknown parameters from the other process!"); return false; } const FileInputStreamParams& params = aParams.get_FileInputStreamParams(); uint32_t fileDescriptorIndex = params.fileDescriptorIndex(); FileDescriptor fd; if (fileDescriptorIndex < aFileDescriptors.Length()) { fd = aFileDescriptors[fileDescriptorIndex]; NS_WARNING_ASSERTION(fd.IsValid(), "Received an invalid file descriptor!"); } else { NS_WARNING("Received a bad file descriptor index!"); } if (fd.IsValid()) { auto rawFD = fd.ClonePlatformHandle(); PRFileDesc* fileDesc = PR_ImportFile(PROsfd(rawFD.release())); if (!fileDesc) { NS_WARNING("Failed to import file handle!"); return false; } mFD = fileDesc; } mBehaviorFlags = params.behaviorFlags(); if (!XRE_IsParentProcess()) { // A child process shouldn't close when it reads the end because it will // not be able to reopen the file later. mBehaviorFlags &= ~nsIFileInputStream::CLOSE_ON_EOF; // A child process will not be able to reopen the file so this flag is // meaningless. mBehaviorFlags &= ~nsIFileInputStream::REOPEN_ON_REWIND; } mIOFlags = params.ioFlags(); return true; }
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 OptionalInputStreamParams& wrappedParams = params.optionalStream(); mHeaders = params.headers(); mContentLength = params.contentLength(); mStartedReading = params.startedReading(); // nsMIMEInputStream::Init() already appended mHeaderStream & mCLStream mHeaderStream->ShareData(mHeaders.get(), mStartedReading ? mHeaders.Length() : 0); mCLStream->ShareData(mContentLength.get(), mStartedReading ? mContentLength.Length() : 0); nsCOMPtr<nsIInputStream> stream; if (wrappedParams.type() == OptionalInputStreamParams::TInputStreamParams) { stream = DeserializeInputStream(wrappedParams.get_InputStreamParams(), aFileDescriptors); if (!stream) { NS_WARNING("Failed to deserialize wrapped stream!"); return false; } mData = stream; if (NS_FAILED(mStream->AppendStream(mData))) { NS_WARNING("Failed to append stream!"); return false; } } else { NS_ASSERTION(wrappedParams.type() == OptionalInputStreamParams::Tvoid_t, "Unknown type for OptionalInputStreamParams!"); } mAddContentLength = params.addContentLength(); return true; }
bool nsTemporaryFileInputStream::Deserialize(const InputStreamParams& aParams, const FileDescriptorArray& aFileDescriptors) { const TemporaryFileInputStreamParams& params = aParams.get_TemporaryFileInputStreamParams(); uint32_t fileDescriptorIndex = params.fileDescriptorIndex(); FileDescriptor fd; if (fileDescriptorIndex < aFileDescriptors.Length()) { fd = aFileDescriptors[fileDescriptorIndex]; NS_WARN_IF_FALSE(fd.IsValid(), "Received an invalid file descriptor!"); } else { NS_WARNING("Received a bad file descriptor index!"); } if (fd.IsValid()) { auto rawFD = fd.ClonePlatformHandle(); PRFileDesc* fileDesc = PR_ImportFile(PROsfd(rawFD.release())); if (!fileDesc) { NS_WARNING("Failed to import file handle!"); return false; } mFileDescOwner = new FileDescOwner(fileDesc); } else { mClosed = true; } mStartPos = mCurPos = params.startPos(); mEndPos = params.endPos(); return true; }
void nsPartialFileInputStream::Serialize(InputStreamParams& aParams, FileDescriptorArray& aFileDescriptors) { // Serialize the base class first. InputStreamParams fileParams; nsFileInputStream::Serialize(fileParams, aFileDescriptors); PartialFileInputStreamParams params; params.fileStreamParams() = fileParams.get_FileInputStreamParams(); params.begin() = mStart; params.length() = mLength; 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; }
void nsPartialFileInputStream::Serialize(InputStreamParams& aParams) { // Serialize the base class first. InputStreamParams fileParams; nsFileInputStream::Serialize(fileParams); if (fileParams.type() != InputStreamParams::TFileInputStreamParams) { NS_ERROR("Base class serialize failed!"); return; } PartialFileInputStreamParams params; params.fileStreamParams() = fileParams.get_FileInputStreamParams(); params.begin() = mStart; params.length() = mLength; aParams = params; }
bool nsPartialFileInputStream::Deserialize( const InputStreamParams& aParams, const FileDescriptorArray& aFileDescriptors) { NS_ASSERTION(!mFD, "Already have a file descriptor?!"); NS_ASSERTION(!mStart, "Already have a start?!"); NS_ASSERTION(!mLength, "Already have a length?!"); NS_ASSERTION(!mPosition, "Already have a position?!"); if (aParams.type() != InputStreamParams::TPartialFileInputStreamParams) { NS_WARNING("Received unknown parameters from the other process!"); return false; } const PartialFileInputStreamParams& params = aParams.get_PartialFileInputStreamParams(); // Deserialize the base class first. InputStreamParams fileParams(params.fileStreamParams()); if (!nsFileInputStream::Deserialize(fileParams, aFileDescriptors)) { NS_WARNING("Base class deserialize failed!"); return false; } NS_ASSERTION(mFD, "Must have a file descriptor now!"); mStart = params.begin(); mLength = params.length(); mPosition = 0; if (!mStart) { return true; } // XXX This is so broken. Main thread IO alert. return NS_SUCCEEDED(nsFileInputStream::Seek(NS_SEEK_SET, mStart)); }
already_AddRefed<nsIInputStream> DeserializeInputStream(const InputStreamParams& aParams) { MOZ_ASSERT(NS_IsMainThread()); nsCOMPtr<nsIIPCSerializableInputStream> serializable; switch (aParams.type()) { case InputStreamParams::TStringInputStreamParams: serializable = do_CreateInstance(kStringInputStreamCID); break; case InputStreamParams::TFileInputStreamParams: serializable = do_CreateInstance(kFileInputStreamCID); break; case InputStreamParams::TPartialFileInputStreamParams: serializable = do_CreateInstance(kPartialFileInputStreamCID); break; case InputStreamParams::TBufferedInputStreamParams: serializable = do_CreateInstance(kBufferedInputStreamCID); break; case InputStreamParams::TMIMEInputStreamParams: serializable = do_CreateInstance(kMIMEInputStreamCID); break; case InputStreamParams::TMultiplexInputStreamParams: serializable = do_CreateInstance(kMultiplexInputStreamCID); break; default: MOZ_ASSERT(false, "Unknown params!"); return nullptr; } MOZ_ASSERT(serializable); if (!serializable->Deserialize(aParams)) { MOZ_ASSERT(false, "Deserialize failed!"); return nullptr; } nsCOMPtr<nsIInputStream> stream = do_QueryInterface(serializable); MOZ_ASSERT(stream); return stream.forget(); }
void SerializeInputStream(nsIInputStream* aInputStream, InputStreamParams& aParams, nsTArray<FileDescriptor>& aFileDescriptors) { MOZ_ASSERT(aInputStream); nsCOMPtr<nsIIPCSerializableInputStream> serializable = do_QueryInterface(aInputStream); if (!serializable) { MOZ_CRASH("Input stream is not serializable!"); } serializable->Serialize(aParams, aFileDescriptors); if (aParams.type() == InputStreamParams::T__None) { MOZ_CRASH("Serialize failed!"); } }
void SerializeInputStream(nsIInputStream* aInputStream, InputStreamParams& aParams) { MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(aInputStream); nsCOMPtr<nsIIPCSerializableInputStream> serializable = do_QueryInterface(aInputStream); if (!serializable) { MOZ_NOT_REACHED("Input stream is not serializable!"); } serializable->Serialize(aParams); if (aParams.type() == InputStreamParams::T__None) { MOZ_NOT_REACHED("Serialize failed!"); } }
already_AddRefed<nsIInputStream> DeserializeInputStream(const InputStreamParams& aParams, const nsTArray<FileDescriptor>& aFileDescriptors) { nsCOMPtr<nsIInputStream> stream; nsCOMPtr<nsIIPCSerializableInputStream> serializable; switch (aParams.type()) { case InputStreamParams::TStringInputStreamParams: serializable = do_CreateInstance(kStringInputStreamCID); break; case InputStreamParams::TFileInputStreamParams: serializable = do_CreateInstance(kFileInputStreamCID); break; case InputStreamParams::TPartialFileInputStreamParams: serializable = do_CreateInstance(kPartialFileInputStreamCID); break; case InputStreamParams::TBufferedInputStreamParams: serializable = do_CreateInstance(kBufferedInputStreamCID); break; case InputStreamParams::TMIMEInputStreamParams: serializable = do_CreateInstance(kMIMEInputStreamCID); break; case InputStreamParams::TMultiplexInputStreamParams: serializable = do_CreateInstance(kMultiplexInputStreamCID); break; // When the input stream already exists in this process, all we need to do // is retrieve the original instead of sending any data over the wire. case InputStreamParams::TRemoteInputStreamParams: { if (NS_WARN_IF(XRE_GetProcessType() != GoannaProcessType_Default)) { return nullptr; } const nsID& id = aParams.get_RemoteInputStreamParams().id(); nsRefPtr<FileImpl> blobImpl = BlobParent::GetBlobImplForID(id); MOZ_ASSERT(blobImpl, "Invalid blob contents"); // If fetching the internal stream fails, we ignore it and return a // null stream. nsCOMPtr<nsIInputStream> stream; nsresult rv = blobImpl->GetInternalStream(getter_AddRefs(stream)); if (NS_FAILED(rv) || !stream) { NS_WARNING("Couldn't obtain a valid stream from the blob"); } return stream.forget(); } case InputStreamParams::TSameProcessInputStreamParams: { MOZ_ASSERT(aFileDescriptors.IsEmpty()); const SameProcessInputStreamParams& params = aParams.get_SameProcessInputStreamParams(); stream = dont_AddRef( reinterpret_cast<nsIInputStream*>(params.addRefedInputStream())); MOZ_ASSERT(stream); return stream.forget(); } default: MOZ_ASSERT(false, "Unknown params!"); return nullptr; } MOZ_ASSERT(serializable); if (!serializable->Deserialize(aParams, aFileDescriptors)) { MOZ_ASSERT(false, "Deserialize failed!"); return nullptr; } stream = do_QueryInterface(serializable); MOZ_ASSERT(stream); return stream.forget(); }
already_AddRefed<nsIInputStream> DeserializeInputStream(const InputStreamParams& aParams, const nsTArray<FileDescriptor>& aFileDescriptors) { MOZ_ASSERT(NS_IsMainThread()); nsCOMPtr<nsIIPCSerializableInputStream> serializable; switch (aParams.type()) { case InputStreamParams::TStringInputStreamParams: serializable = do_CreateInstance(kStringInputStreamCID); break; case InputStreamParams::TFileInputStreamParams: serializable = do_CreateInstance(kFileInputStreamCID); break; case InputStreamParams::TPartialFileInputStreamParams: serializable = do_CreateInstance(kPartialFileInputStreamCID); break; case InputStreamParams::TBufferedInputStreamParams: serializable = do_CreateInstance(kBufferedInputStreamCID); break; case InputStreamParams::TMIMEInputStreamParams: serializable = do_CreateInstance(kMIMEInputStreamCID); break; case InputStreamParams::TMultiplexInputStreamParams: serializable = do_CreateInstance(kMultiplexInputStreamCID); break; // When the input stream already exists in this process, all we need to do // is retrieve the original instead of sending any data over the wire. case InputStreamParams::TRemoteInputStreamParams: { nsCOMPtr<nsIDOMBlob> domBlob; const RemoteInputStreamParams& params = aParams.get_RemoteInputStreamParams(); domBlob = params.remoteBlobParent() ? static_cast<BlobParent*>(params.remoteBlobParent())->GetBlob() : static_cast<BlobChild*>(params.remoteBlobChild())->GetBlob(); MOZ_ASSERT(domBlob, "Invalid blob contents"); // If fetching the internal stream fails, we ignore it and return a // null stream. nsCOMPtr<nsIInputStream> stream; nsresult rv = domBlob->GetInternalStream(getter_AddRefs(stream)); if (NS_FAILED(rv) || !stream) { NS_WARNING("Couldn't obtain a valid stream from the blob"); } return stream.forget(); } default: MOZ_ASSERT(false, "Unknown params!"); return nullptr; } MOZ_ASSERT(serializable); if (!serializable->Deserialize(aParams, aFileDescriptors)) { MOZ_ASSERT(false, "Deserialize failed!"); return nullptr; } nsCOMPtr<nsIInputStream> stream = do_QueryInterface(serializable); MOZ_ASSERT(stream); return stream.forget(); }