1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 "RandomAccessStreamUtils.h"
9 #include "mozilla/NotNull.h"
10 #include "mozilla/Result.h"
11 #include "mozilla/ResultVariant.h"
12 #include "mozilla/dom/quota/FileStreams.h"
13 #include "mozilla/ipc/RandomAccessStreamParams.h"
14 #include "nsFileStreams.h"
15 #include "nsIInterfaceRequestor.h"
16 #include "nsIRandomAccessStream.h"
18 namespace mozilla::ipc
{
20 RandomAccessStreamParams
SerializeRandomAccessStream(
21 MovingNotNull
<nsCOMPtr
<nsIRandomAccessStream
>> aStream
,
22 nsIInterfaceRequestor
* aCallbacks
) {
23 NotNull
<nsCOMPtr
<nsIRandomAccessStream
>> stream
= std::move(aStream
);
25 RandomAccessStreamParams streamParams
= stream
->Serialize(aCallbacks
);
27 MOZ_ASSERT(streamParams
.type() != RandomAccessStreamParams::T__None
);
32 Maybe
<RandomAccessStreamParams
> SerializeRandomAccessStream(
33 nsCOMPtr
<nsIRandomAccessStream
> aStream
,
34 nsIInterfaceRequestor
* aCallbacks
) {
39 return Some(SerializeRandomAccessStream(
40 WrapMovingNotNullUnchecked(std::move(aStream
)), aCallbacks
));
43 Result
<MovingNotNull
<nsCOMPtr
<nsIRandomAccessStream
>>, bool>
44 DeserializeRandomAccessStream(RandomAccessStreamParams
& aStreamParams
) {
45 nsCOMPtr
<nsIRandomAccessStream
> stream
;
47 switch (aStreamParams
.type()) {
48 case RandomAccessStreamParams::TFileRandomAccessStreamParams
:
49 nsFileRandomAccessStream::Create(NS_GET_IID(nsIFileRandomAccessStream
),
50 getter_AddRefs(stream
));
53 case RandomAccessStreamParams::TLimitingFileRandomAccessStreamParams
:
54 stream
= new dom::quota::FileRandomAccessStream();
58 MOZ_ASSERT_UNREACHABLE("Unknown params!");
64 if (!stream
->Deserialize(aStreamParams
)) {
65 MOZ_ASSERT_UNREACHABLE("Deserialize failed!");
69 return WrapMovingNotNullUnchecked(std::move(stream
));
72 Result
<nsCOMPtr
<nsIRandomAccessStream
>, bool> DeserializeRandomAccessStream(
73 Maybe
<RandomAccessStreamParams
>& aStreamParams
) {
74 if (aStreamParams
.isNothing()) {
75 return nsCOMPtr
<nsIRandomAccessStream
>();
78 auto res
= DeserializeRandomAccessStream(aStreamParams
.ref());
80 return res
.propagateErr();
83 MovingNotNull
<nsCOMPtr
<nsIRandomAccessStream
>> stream
= res
.unwrap();
85 return std::move(stream
).unwrapBasePtr();
88 } // namespace mozilla::ipc