1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "FetchStreamUtils.h"
7 #include "mozilla/NotNull.h"
8 #include "mozilla/RemoteLazyInputStreamChild.h"
9 #include "mozilla/RemoteLazyInputStreamStorage.h"
10 #include "mozilla/dom/FetchTypes.h"
11 #include "mozilla/dom/IPCBlob.h"
12 #include "mozilla/ipc/IPCStreamUtils.h"
13 #include "nsContentUtils.h"
14 #include "nsXULAppAPI.h"
16 namespace mozilla::dom
{
20 RefPtr
<RemoteLazyInputStreamStorage
> GetRemoteLazyInputStreamStorage() {
21 auto storageOrErr
= RemoteLazyInputStreamStorage::Get();
22 MOZ_ASSERT(storageOrErr
.isOk());
23 return storageOrErr
.unwrap();
28 NotNull
<nsCOMPtr
<nsIInputStream
>> ToInputStream(
29 const ParentToParentStream
& aStream
) {
30 MOZ_ASSERT(XRE_IsParentProcess());
32 GetRemoteLazyInputStreamStorage()->ForgetStream(aStream
.uuid()));
35 NotNull
<nsCOMPtr
<nsIInputStream
>> ToInputStream(
36 const ParentToChildStream
& aStream
) {
37 MOZ_ASSERT(XRE_IsContentProcess());
38 nsCOMPtr
<nsIInputStream
> result
;
39 if (aStream
.type() == ParentToChildStream::TRemoteLazyInputStream
) {
40 result
= aStream
.get_RemoteLazyInputStream();
42 result
= DeserializeIPCStream(aStream
.get_IPCStream());
44 return WrapNotNull(result
);
47 ParentToParentStream
ToParentToParentStream(
48 const NotNull
<nsCOMPtr
<nsIInputStream
>>& aStream
, int64_t aStreamSize
) {
49 MOZ_ASSERT(XRE_IsParentProcess());
51 ParentToParentStream stream
;
52 stream
.uuid() = nsID::GenerateUUID();
53 GetRemoteLazyInputStreamStorage()->AddStream(aStream
.get(), stream
.uuid());
57 ParentToChildStream
ToParentToChildStream(
58 const NotNull
<nsCOMPtr
<nsIInputStream
>>& aStream
, int64_t aStreamSize
,
59 NotNull
<mozilla::ipc::PBackgroundParent
*> aBackgroundParent
,
60 bool aSerializeAsLazy
) {
61 MOZ_ASSERT(XRE_IsParentProcess());
63 ParentToChildStream result
;
64 if (aSerializeAsLazy
) {
65 result
= RemoteLazyInputStream::WrapStream(aStream
.get());
67 nsCOMPtr
<nsIInputStream
> stream(aStream
.get());
68 mozilla::ipc::IPCStream ipcStream
;
70 !mozilla::ipc::SerializeIPCStream(stream
.forget(), ipcStream
, false));
76 ParentToChildStream
ToParentToChildStream(
77 const ParentToParentStream
& aStream
, int64_t aStreamSize
,
78 NotNull
<mozilla::ipc::PBackgroundParent
*> aBackgroundParent
) {
79 return ToParentToChildStream(ToInputStream(aStream
), aStreamSize
,
83 } // namespace mozilla::dom