summaryrefslogtreecommitdiffstats
path: root/dom/file/ipc/RemoteLazyInputStreamUtils.cpp
blob: 33fce73e14cdc9fb6f1a26f4b4098ff9736b5891 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "RemoteLazyInputStreamUtils.h"
#include "RemoteLazyInputStream.h"
#include "RemoteLazyInputStreamChild.h"
#include "RemoteLazyInputStreamParent.h"
#include "mozilla/ipc/BackgroundParent.h"
#include "mozilla/ipc/PBackgroundParent.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/ipc/IPCStreamUtils.h"
#include "RemoteLazyInputStreamStorage.h"
#include "StreamBlobImpl.h"

namespace mozilla {

namespace {

template <typename M>
nsresult SerializeInputStreamParent(nsIInputStream* aInputStream,
                                    uint64_t aSize, uint64_t aChildID,
                                    PRemoteLazyInputStreamParent*& aActorParent,
                                    M* aManager) {
  // Parent to Child we always send a RemoteLazyInputStream.
  MOZ_ASSERT(XRE_IsParentProcess());

  nsCOMPtr<nsIInputStream> stream = aInputStream;

  // In case this is a RemoteLazyInputStream, we don't want to create a loop:
  // RemoteLazyInputStreamParent -> RemoteLazyInputStream ->
  // RemoteLazyInputStreamParent. Let's use the underlying inputStream instead.
  nsCOMPtr<mozIRemoteLazyInputStream> remoteLazyInputStream =
      do_QueryInterface(aInputStream);
  if (remoteLazyInputStream) {
    stream = remoteLazyInputStream->GetInternalStream();
    // If we don't have an underlying stream, it's better to terminate here
    // instead of sending an 'empty' RemoteLazyInputStream actor on the other
    // side, unable to be used.
    if (NS_WARN_IF(!stream)) {
      return NS_ERROR_FAILURE;
    }
  }

  nsresult rv;
  RefPtr<RemoteLazyInputStreamParent> parentActor =
      RemoteLazyInputStreamParent::Create(stream, aSize, aChildID, &rv,
                                          aManager);
  if (!parentActor) {
    return rv;
  }

  if (!aManager->SendPRemoteLazyInputStreamConstructor(
          parentActor, parentActor->ID(), parentActor->Size())) {
    return NS_ERROR_FAILURE;
  }

  aActorParent = parentActor;
  return NS_OK;
}

}  // anonymous namespace

// static
nsresult RemoteLazyInputStreamUtils::SerializeInputStream(
    nsIInputStream* aInputStream, uint64_t aSize, RemoteLazyStream& aOutStream,
    dom::ContentParent* aManager) {
  PRemoteLazyInputStreamParent* actor = nullptr;
  nsresult rv = SerializeInputStreamParent(
      aInputStream, aSize, aManager->ChildID(), actor, aManager);
  NS_ENSURE_SUCCESS(rv, rv);

  aOutStream = actor;
  return NS_OK;
}

// static
nsresult RemoteLazyInputStreamUtils::SerializeInputStream(
    nsIInputStream* aInputStream, uint64_t aSize, RemoteLazyStream& aOutStream,
    mozilla::ipc::PBackgroundParent* aManager) {
  PRemoteLazyInputStreamParent* actor = nullptr;
  nsresult rv = SerializeInputStreamParent(
      aInputStream, aSize, mozilla::ipc::BackgroundParent::GetChildID(aManager),
      actor, aManager);
  NS_ENSURE_SUCCESS(rv, rv);

  aOutStream = actor;
  return NS_OK;
}

// static
nsresult RemoteLazyInputStreamUtils::SerializeInputStream(
    nsIInputStream* aInputStream, uint64_t aSize, RemoteLazyStream& aOutStream,
    dom::ContentChild* aManager) {
  mozilla::ipc::AutoIPCStream ipcStream(true /* delayed start */);
  if (!ipcStream.Serialize(aInputStream, aManager)) {
    return NS_ERROR_FAILURE;
  }

  aOutStream = ipcStream.TakeValue();
  return NS_OK;
}

// static
nsresult RemoteLazyInputStreamUtils::SerializeInputStream(
    nsIInputStream* aInputStream, uint64_t aSize, RemoteLazyStream& aOutStream,
    mozilla::ipc::PBackgroundChild* aManager) {
  mozilla::ipc::AutoIPCStream ipcStream(true /* delayed start */);
  if (!ipcStream.Serialize(aInputStream, aManager)) {
    return NS_ERROR_FAILURE;
  }

  aOutStream = ipcStream.TakeValue();
  return NS_OK;
}

}  // namespace mozilla