Bug 1867190 - Initialise the PHC allocate delay later r=glandium
[gecko.git] / dom / fetch / FetchStreamUtils.cpp
blobd9cb762526676735f08a65d9fb43c63afc1f071f
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 {
18 namespace {
20 RefPtr<RemoteLazyInputStreamStorage> GetRemoteLazyInputStreamStorage() {
21 auto storageOrErr = RemoteLazyInputStreamStorage::Get();
22 MOZ_ASSERT(storageOrErr.isOk());
23 return storageOrErr.unwrap();
26 } // namespace
28 NotNull<nsCOMPtr<nsIInputStream>> ToInputStream(
29 const ParentToParentStream& aStream) {
30 MOZ_ASSERT(XRE_IsParentProcess());
31 return WrapNotNull(
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();
41 } else {
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());
54 return stream;
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());
66 } else {
67 nsCOMPtr<nsIInputStream> stream(aStream.get());
68 mozilla::ipc::IPCStream ipcStream;
69 Unused << NS_WARN_IF(
70 !mozilla::ipc::SerializeIPCStream(stream.forget(), ipcStream, false));
71 result = ipcStream;
73 return result;
76 ParentToChildStream ToParentToChildStream(
77 const ParentToParentStream& aStream, int64_t aStreamSize,
78 NotNull<mozilla::ipc::PBackgroundParent*> aBackgroundParent) {
79 return ToParentToChildStream(ToInputStream(aStream), aStreamSize,
80 aBackgroundParent);
83 } // namespace mozilla::dom