summaryrefslogtreecommitdiff
path: root/ipc/glue/InputStreamUtils.cpp
blob: bbc863efd6bdf84fe79f32f3069ab167c4b4e146 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* -*- 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 "InputStreamUtils.h"

#include "nsIIPCSerializableInputStream.h"

#include "mozilla/Assertions.h"
#include "mozilla/dom/File.h"
#include "mozilla/dom/ipc/BlobChild.h"
#include "mozilla/dom/ipc/BlobParent.h"
#include "nsComponentManagerUtils.h"
#include "nsDebug.h"
#include "nsID.h"
#include "nsIXULRuntime.h"
#include "nsMIMEInputStream.h"
#include "nsMultiplexInputStream.h"
#include "nsNetCID.h"
#include "nsStringStream.h"
#include "nsXULAppAPI.h"

using namespace mozilla::dom;

namespace {

NS_DEFINE_CID(kStringInputStreamCID, NS_STRINGINPUTSTREAM_CID);
NS_DEFINE_CID(kFileInputStreamCID, NS_LOCALFILEINPUTSTREAM_CID);
NS_DEFINE_CID(kPartialFileInputStreamCID, NS_PARTIALLOCALFILEINPUTSTREAM_CID);
NS_DEFINE_CID(kBufferedInputStreamCID, NS_BUFFEREDINPUTSTREAM_CID);
NS_DEFINE_CID(kMIMEInputStreamCID, NS_MIMEINPUTSTREAM_CID);
NS_DEFINE_CID(kMultiplexInputStreamCID, NS_MULTIPLEXINPUTSTREAM_CID);

} // namespace

namespace mozilla {
namespace ipc {

void
SerializeInputStream(nsIInputStream* aInputStream,
                     InputStreamParams& aParams,
                     nsTArray<FileDescriptor>& aFileDescriptors)
{
  MOZ_ASSERT(aInputStream);

  nsCOMPtr<nsIIPCSerializableInputStream> serializable =
    do_QueryInterface(aInputStream);
  if (!serializable) {
    MOZ_CRASH("Input stream is not serializable!");
  }

  serializable->Serialize(aParams, aFileDescriptors);

  if (aParams.type() == InputStreamParams::T__None) {
    MOZ_CRASH("Serialize failed!");
  }
}

void
SerializeInputStream(nsIInputStream* aInputStream,
                     OptionalInputStreamParams& aParams,
                     nsTArray<FileDescriptor>& aFileDescriptors)
{
  if (aInputStream) {
    InputStreamParams params;
    SerializeInputStream(aInputStream, params, aFileDescriptors);
    aParams = params;
  }
  else {
    aParams = mozilla::void_t();
  }
}

already_AddRefed<nsIInputStream>
DeserializeInputStream(const InputStreamParams& aParams,
                       const nsTArray<FileDescriptor>& aFileDescriptors)
{
  nsCOMPtr<nsIInputStream> stream;
  nsCOMPtr<nsIIPCSerializableInputStream> serializable;

  switch (aParams.type()) {
    case InputStreamParams::TStringInputStreamParams:
      serializable = do_CreateInstance(kStringInputStreamCID);
      break;

    case InputStreamParams::TFileInputStreamParams:
      serializable = do_CreateInstance(kFileInputStreamCID);
      break;

    case InputStreamParams::TPartialFileInputStreamParams:
      serializable = do_CreateInstance(kPartialFileInputStreamCID);
      break;

    case InputStreamParams::TTemporaryFileInputStreamParams:
      serializable = new nsTemporaryFileInputStream();
      break;

    case InputStreamParams::TBufferedInputStreamParams:
      serializable = do_CreateInstance(kBufferedInputStreamCID);
      break;

    case InputStreamParams::TMIMEInputStreamParams:
      serializable = do_CreateInstance(kMIMEInputStreamCID);
      break;

    case InputStreamParams::TMultiplexInputStreamParams:
      serializable = do_CreateInstance(kMultiplexInputStreamCID);
      break;

    // When the input stream already exists in this process, all we need to do
    // is retrieve the original instead of sending any data over the wire.
    case InputStreamParams::TRemoteInputStreamParams: {
      if (NS_WARN_IF(!XRE_IsParentProcess())) {
        return nullptr;
      }

      const nsID& id = aParams.get_RemoteInputStreamParams().id();

      RefPtr<BlobImpl> blobImpl = BlobParent::GetBlobImplForID(id);

      MOZ_ASSERT(blobImpl, "Invalid blob contents");

      // If fetching the internal stream fails, we ignore it and return a
      // null stream.
      ErrorResult rv;
      nsCOMPtr<nsIInputStream> stream;
      blobImpl->GetInternalStream(getter_AddRefs(stream), rv);
      if (NS_WARN_IF(rv.Failed()) || !stream) {
        NS_WARNING("Couldn't obtain a valid stream from the blob");
        rv.SuppressException();
      }
      return stream.forget();
    }

    case InputStreamParams::TSameProcessInputStreamParams: {
      MOZ_ASSERT(aFileDescriptors.IsEmpty());

      const SameProcessInputStreamParams& params =
        aParams.get_SameProcessInputStreamParams();

      stream = dont_AddRef(
        reinterpret_cast<nsIInputStream*>(params.addRefedInputStream()));
      MOZ_ASSERT(stream);

      return stream.forget();
    }

    default:
      MOZ_ASSERT(false, "Unknown params!");
      return nullptr;
  }

  MOZ_ASSERT(serializable);

  if (!serializable->Deserialize(aParams, aFileDescriptors)) {
    MOZ_ASSERT(false, "Deserialize failed!");
    return nullptr;
  }

  stream = do_QueryInterface(serializable);
  MOZ_ASSERT(stream);

  return stream.forget();
}

already_AddRefed<nsIInputStream>
DeserializeInputStream(const OptionalInputStreamParams& aParams,
                       const nsTArray<FileDescriptor>& aFileDescriptors)
{
  nsCOMPtr<nsIInputStream> stream;

  switch (aParams.type()) {
    case OptionalInputStreamParams::Tvoid_t:
      // Leave stream null.
      break;

    case OptionalInputStreamParams::TInputStreamParams:
      stream = DeserializeInputStream(aParams.get_InputStreamParams(),
                                      aFileDescriptors);
      break;

    default:
      MOZ_ASSERT(false, "Unknown params!");
  }

  return stream.forget();
}

} // namespace ipc
} // namespace mozilla