dom/webbrowserpersist/WebBrowserPersistSerializeParent.cpp
author Narcis Beleuzu <nbeleuzu@mozilla.com>
Sun, 29 Jul 2018 03:55:23 +0300
changeset 483986 aaee68beff800bcf076d9c49b5c1f6531b135579
parent 414833 312f7a5a2c08d394a2403c837e5ee546dd4103d7
child 505383 6f3709b3878117466168c40affa7bca0b60cf75b
permissions -rw-r--r--
Backed out 2 changesets (bug 1463016, bug 1463291) for geckoview failures Backed out changeset fcfb99baa0f0 (bug 1463291) Backed out changeset 0d69b4fb1ed4 (bug 1463016)

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * 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/. */

#include "WebBrowserPersistSerializeParent.h"

#include "nsReadableUtils.h"
#include "nsThreadUtils.h"

namespace mozilla {

WebBrowserPersistSerializeParent::WebBrowserPersistSerializeParent(
        nsIWebBrowserPersistDocument* aDocument,
        nsIOutputStream* aStream,
        nsIWebBrowserPersistWriteCompletion* aFinish)
: mDocument(aDocument)
, mStream(aStream)
, mFinish(aFinish)
, mOutputError(NS_OK)
{
    MOZ_ASSERT(aDocument);
    MOZ_ASSERT(aStream);
    MOZ_ASSERT(aFinish);
}

WebBrowserPersistSerializeParent::~WebBrowserPersistSerializeParent() = default;

mozilla::ipc::IPCResult
WebBrowserPersistSerializeParent::RecvWriteData(nsTArray<uint8_t>&& aData)
{
    if (NS_FAILED(mOutputError)) {
        return IPC_OK();
    }

    uint32_t written = 0;
    static_assert(sizeof(char) == sizeof(uint8_t),
                  "char must be (at least?) 8 bits");
    const char* data = reinterpret_cast<const char*>(aData.Elements());
    // nsIOutputStream::Write is allowed to return short writes.
    while (written < aData.Length()) {
        uint32_t writeReturn;
        nsresult rv = mStream->Write(data + written,
                                     aData.Length() - written,
                                     &writeReturn);
        if (NS_FAILED(rv)) {
            mOutputError = rv;
            return IPC_OK();
        }
        written += writeReturn;
    }
    return IPC_OK();
}

mozilla::ipc::IPCResult
WebBrowserPersistSerializeParent::Recv__delete__(const nsCString& aContentType,
                                                 const nsresult& aStatus)
{
    if (NS_SUCCEEDED(mOutputError)) {
        mOutputError = aStatus;
    }
    mFinish->OnFinish(mDocument,
                      mStream,
                      aContentType,
                      mOutputError);
    mFinish = nullptr;
    return IPC_OK();
}

void
WebBrowserPersistSerializeParent::ActorDestroy(ActorDestroyReason aWhy)
{
    if (mFinish) {
        MOZ_ASSERT(aWhy != Deletion);
        // See comment in WebBrowserPersistDocumentParent::ActorDestroy
        // (or bug 1202887) for why this is deferred.
        nsCOMPtr<nsIRunnable> errorLater =
          NewRunnableMethod<nsCOMPtr<nsIWebBrowserPersistDocument>,
                            nsCOMPtr<nsIOutputStream>,
                            nsCString,
                            nsresult>(
            "nsIWebBrowserPersistWriteCompletion::OnFinish",
            mFinish,
            &nsIWebBrowserPersistWriteCompletion::OnFinish,
            mDocument,
            mStream,
            EmptyCString(),
            NS_ERROR_FAILURE);
        NS_DispatchToCurrentThread(errorLater);
        mFinish = nullptr;
    }
}

} // namespace mozilla