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;
}
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;
}
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;
}
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;
}