summaryrefslogtreecommitdiffstats
path: root/dom/fetch/FetchStreamUtils.cpp
blob: 96f4dc346e81f1f5c8f5e414ddc3d4f030a6ac34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FetchStreamUtils.h"

#include "mozilla/NotNull.h"
#include "mozilla/RemoteLazyInputStreamChild.h"
#include "mozilla/RemoteLazyInputStreamStorage.h"
#include "mozilla/dom/FetchTypes.h"
#include "mozilla/dom/IPCBlob.h"
#include "nsContentUtils.h"
#include "nsXULAppAPI.h"

namespace mozilla::dom {

namespace {

RefPtr<RemoteLazyInputStreamStorage> GetRemoteLazyInputStreamStorage() {
  auto storageOrErr = RemoteLazyInputStreamStorage::Get();
  MOZ_ASSERT(storageOrErr.isOk());
  return storageOrErr.unwrap();
}

}  // namespace

NotNull<nsCOMPtr<nsIInputStream>> ToInputStream(
    const ParentToParentStream& aStream) {
  MOZ_ASSERT(XRE_IsParentProcess());
  return WrapNotNull(
      GetRemoteLazyInputStreamStorage()->ForgetStream(aStream.uuid()));
}

NotNull<nsCOMPtr<nsIInputStream>> ToInputStream(
    const ParentToChildStream& aStream) {
  MOZ_ASSERT(XRE_IsContentProcess());
  nsCOMPtr<nsIInputStream> result = aStream.stream();
  return WrapNotNull(result);
}

ParentToParentStream ToParentToParentStream(
    const NotNull<nsCOMPtr<nsIInputStream>>& aStream, int64_t aStreamSize) {
  MOZ_ASSERT(XRE_IsParentProcess());

  ParentToParentStream stream;
  stream.uuid() = nsID::GenerateUUID();
  GetRemoteLazyInputStreamStorage()->AddStream(aStream.get(), stream.uuid());
  return stream;
}

ParentToChildStream ToParentToChildStream(
    const NotNull<nsCOMPtr<nsIInputStream>>& aStream, int64_t aStreamSize,
    NotNull<mozilla::ipc::PBackgroundParent*> aBackgroundParent) {
  MOZ_ASSERT(XRE_IsParentProcess());

  ParentToChildStream result;
  result.stream() = RemoteLazyInputStream::WrapStream(aStream.get());
  return result;
}

ParentToChildStream ToParentToChildStream(
    const ParentToParentStream& aStream, int64_t aStreamSize,
    NotNull<mozilla::ipc::PBackgroundParent*> aBackgroundParent) {
  return ToParentToChildStream(ToInputStream(aStream), aStreamSize,
                               aBackgroundParent);
}

}  // namespace mozilla::dom