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