Bug 1839170 - Refactor Snap pulling, Add Firefox Snap Core22 and GNOME 42 SDK symbols...
[gecko.git] / ipc / glue / RandomAccessStreamUtils.cpp
blobd5e66daae6255e48eadd0e46734dc4699cd95f4f
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);
29 return streamParams;
32 Maybe<RandomAccessStreamParams> SerializeRandomAccessStream(
33 nsCOMPtr<nsIRandomAccessStream> aStream,
34 nsIInterfaceRequestor* aCallbacks) {
35 if (!aStream) {
36 return Nothing();
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));
51 break;
53 case RandomAccessStreamParams::TLimitingFileRandomAccessStreamParams:
54 stream = new dom::quota::FileRandomAccessStream();
55 break;
57 default:
58 MOZ_ASSERT_UNREACHABLE("Unknown params!");
59 return Err(false);
62 MOZ_ASSERT(stream);
64 if (!stream->Deserialize(aStreamParams)) {
65 MOZ_ASSERT_UNREACHABLE("Deserialize failed!");
66 return Err(false);
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());
79 if (res.isErr()) {
80 return res.propagateErr();
83 MovingNotNull<nsCOMPtr<nsIRandomAccessStream>> stream = res.unwrap();
85 return std::move(stream).unwrapBasePtr();
88 } // namespace mozilla::ipc