summaryrefslogtreecommitdiffstats
path: root/embedding/components/webbrowserpersist/WebBrowserPersistSerializeParent.cpp
blob: 2f76ffb332a89cdbe6aba0c565946b1411d6209a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* -*- 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()
{
}

bool
WebBrowserPersistSerializeParent::RecvWriteData(nsTArray<uint8_t>&& aData)
{
    if (NS_FAILED(mOutputError)) {
        return true;
    }

    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 true;
        }
        written += writeReturn;
    }
    return true;
}

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

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>
            (mFinish, &nsIWebBrowserPersistWriteCompletion::OnFinish,
             mDocument, mStream, EmptyCString(), NS_ERROR_FAILURE);
        NS_DispatchToCurrentThread(errorLater);
        mFinish = nullptr;
    }
}

} // namespace mozilla