1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "WebBrowserPersistSerializeParent.h"
9 #include "nsReadableUtils.h"
10 #include "nsThreadUtils.h"
14 WebBrowserPersistSerializeParent::WebBrowserPersistSerializeParent(
15 nsIWebBrowserPersistDocument
* aDocument
, nsIOutputStream
* aStream
,
16 nsIWebBrowserPersistWriteCompletion
* aFinish
)
17 : mDocument(aDocument
),
21 MOZ_ASSERT(aDocument
);
26 WebBrowserPersistSerializeParent::~WebBrowserPersistSerializeParent() = default;
28 mozilla::ipc::IPCResult
WebBrowserPersistSerializeParent::RecvWriteData(
29 nsTArray
<uint8_t>&& aData
) {
30 if (NS_FAILED(mOutputError
)) {
35 static_assert(sizeof(char) == sizeof(uint8_t),
36 "char must be (at least?) 8 bits");
37 const char* data
= reinterpret_cast<const char*>(aData
.Elements());
38 // nsIOutputStream::Write is allowed to return short writes.
39 while (written
< aData
.Length()) {
42 mStream
->Write(data
+ written
, aData
.Length() - written
, &writeReturn
);
47 written
+= writeReturn
;
52 mozilla::ipc::IPCResult
WebBrowserPersistSerializeParent::Recv__delete__(
53 const nsACString
& aContentType
, const nsresult
& aStatus
) {
54 if (NS_SUCCEEDED(mOutputError
)) {
55 mOutputError
= aStatus
;
57 mFinish
->OnFinish(mDocument
, mStream
, aContentType
, mOutputError
);
62 void WebBrowserPersistSerializeParent::ActorDestroy(ActorDestroyReason aWhy
) {
64 MOZ_ASSERT(aWhy
!= Deletion
);
65 // See comment in WebBrowserPersistDocumentParent::ActorDestroy
66 // (or bug 1202887) for why this is deferred.
67 nsCOMPtr
<nsIRunnable
> errorLater
=
68 NewRunnableMethod
<nsCOMPtr
<nsIWebBrowserPersistDocument
>,
69 nsCOMPtr
<nsIOutputStream
>, nsCString
, nsresult
>(
70 "nsIWebBrowserPersistWriteCompletion::OnFinish", mFinish
,
71 &nsIWebBrowserPersistWriteCompletion::OnFinish
, mDocument
, mStream
,
72 ""_ns
, NS_ERROR_FAILURE
);
73 NS_DispatchToCurrentThread(errorLater
);
78 } // namespace mozilla