Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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/. */
6 :
7 : #include "WebBrowserPersistSerializeParent.h"
8 :
9 : #include "nsReadableUtils.h"
10 : #include "nsThreadUtils.h"
11 :
12 : namespace mozilla {
13 :
14 0 : WebBrowserPersistSerializeParent::WebBrowserPersistSerializeParent(
15 : nsIWebBrowserPersistDocument* aDocument,
16 : nsIOutputStream* aStream,
17 0 : nsIWebBrowserPersistWriteCompletion* aFinish)
18 : : mDocument(aDocument)
19 : , mStream(aStream)
20 : , mFinish(aFinish)
21 0 : , mOutputError(NS_OK)
22 : {
23 0 : MOZ_ASSERT(aDocument);
24 0 : MOZ_ASSERT(aStream);
25 0 : MOZ_ASSERT(aFinish);
26 0 : }
27 :
28 : WebBrowserPersistSerializeParent::~WebBrowserPersistSerializeParent() = default;
29 :
30 : mozilla::ipc::IPCResult
31 0 : WebBrowserPersistSerializeParent::RecvWriteData(nsTArray<uint8_t>&& aData)
32 : {
33 0 : if (NS_FAILED(mOutputError)) {
34 0 : return IPC_OK();
35 : }
36 :
37 0 : uint32_t written = 0;
38 : static_assert(sizeof(char) == sizeof(uint8_t),
39 : "char must be (at least?) 8 bits");
40 0 : const char* data = reinterpret_cast<const char*>(aData.Elements());
41 : // nsIOutputStream::Write is allowed to return short writes.
42 0 : while (written < aData.Length()) {
43 : uint32_t writeReturn;
44 0 : nsresult rv = mStream->Write(data + written,
45 0 : aData.Length() - written,
46 0 : &writeReturn);
47 0 : if (NS_FAILED(rv)) {
48 0 : mOutputError = rv;
49 0 : return IPC_OK();
50 : }
51 0 : written += writeReturn;
52 : }
53 0 : return IPC_OK();
54 : }
55 :
56 : mozilla::ipc::IPCResult
57 0 : WebBrowserPersistSerializeParent::Recv__delete__(const nsCString& aContentType,
58 : const nsresult& aStatus)
59 : {
60 0 : if (NS_SUCCEEDED(mOutputError)) {
61 0 : mOutputError = aStatus;
62 : }
63 0 : mFinish->OnFinish(mDocument,
64 : mStream,
65 : aContentType,
66 0 : mOutputError);
67 0 : mFinish = nullptr;
68 0 : return IPC_OK();
69 : }
70 :
71 : void
72 0 : WebBrowserPersistSerializeParent::ActorDestroy(ActorDestroyReason aWhy)
73 : {
74 0 : if (mFinish) {
75 0 : MOZ_ASSERT(aWhy != Deletion);
76 : // See comment in WebBrowserPersistDocumentParent::ActorDestroy
77 : // (or bug 1202887) for why this is deferred.
78 : nsCOMPtr<nsIRunnable> errorLater =
79 : NewRunnableMethod<nsCOMPtr<nsIWebBrowserPersistDocument>,
80 : nsCOMPtr<nsIOutputStream>,
81 : nsCString,
82 0 : nsresult>(
83 : "nsIWebBrowserPersistWriteCompletion::OnFinish",
84 : mFinish,
85 : &nsIWebBrowserPersistWriteCompletion::OnFinish,
86 : mDocument,
87 : mStream,
88 : EmptyCString(),
89 0 : NS_ERROR_FAILURE);
90 0 : NS_DispatchToCurrentThread(errorLater);
91 0 : mFinish = nullptr;
92 : }
93 0 : }
94 :
95 : } // namespace mozilla
|