summaryrefslogtreecommitdiffstats
path: root/dom/file/ipc/RemoteLazyInputStreamParent.cpp
blob: 7aced41ac46d6ce86b7eef5ba6ddbd6b2186dc87 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* -*- 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 "RemoteLazyInputStreamParent.h"
#include "mozilla/ipc/IPCStreamUtils.h"
#include "mozilla/InputStreamLengthHelper.h"
#include "nsContentUtils.h"
#include "RemoteLazyInputStreamStorage.h"

namespace mozilla {

template <typename M>
/* static */
already_AddRefed<RemoteLazyInputStreamParent>
RemoteLazyInputStreamParent::Create(nsIInputStream* aInputStream,
                                    uint64_t aSize, uint64_t aChildID,
                                    nsresult* aRv, M* aManager) {
  MOZ_ASSERT(aInputStream);
  MOZ_ASSERT(aRv);

  nsID id;
  *aRv = nsContentUtils::GenerateUUIDInPlace(id);
  if (NS_WARN_IF(NS_FAILED(*aRv))) {
    return nullptr;
  }

  auto storageOrErr = RemoteLazyInputStreamStorage::Get();

  if (NS_WARN_IF(storageOrErr.isErr())) {
    *aRv = storageOrErr.unwrapErr();
    return nullptr;
  }

  auto storage = storageOrErr.unwrap();
  storage->AddStream(aInputStream, id, aSize, aChildID);

  RefPtr<RemoteLazyInputStreamParent> parent =
      new RemoteLazyInputStreamParent(id, aSize, aManager);
  return parent.forget();
}

/* static */
already_AddRefed<RemoteLazyInputStreamParent>
RemoteLazyInputStreamParent::Create(const nsID& aID, uint64_t aSize,
                                    PBackgroundParent* aManager) {
  RefPtr<RemoteLazyInputStreamParent> actor =
      new RemoteLazyInputStreamParent(aID, aSize, aManager);

  auto storage = RemoteLazyInputStreamStorage::Get().unwrapOr(nullptr);

  if (storage) {
    actor->mCallback = storage->TakeCallback(aID);
    return actor.forget();
  }

  return nullptr;
}

template already_AddRefed<RemoteLazyInputStreamParent>
RemoteLazyInputStreamParent::Create<mozilla::ipc::PBackgroundParent>(
    nsIInputStream*, uint64_t, uint64_t, nsresult*,
    mozilla::ipc::PBackgroundParent*);

/* static */
already_AddRefed<RemoteLazyInputStreamParent>
RemoteLazyInputStreamParent::Create(const nsID& aID, uint64_t aSize,
                                    net::SocketProcessParent* aManager) {
  RefPtr<RemoteLazyInputStreamParent> actor =
      new RemoteLazyInputStreamParent(aID, aSize, aManager);

  auto storage = RemoteLazyInputStreamStorage::Get().unwrapOr(nullptr);

  if (storage) {
    actor->mCallback = storage->TakeCallback(aID);
    return actor.forget();
  }

  return nullptr;
}

template already_AddRefed<RemoteLazyInputStreamParent>
RemoteLazyInputStreamParent::Create<mozilla::net::SocketProcessParent>(
    nsIInputStream*, uint64_t, uint64_t, nsresult*,
    mozilla::net::SocketProcessParent*);

template already_AddRefed<RemoteLazyInputStreamParent>
RemoteLazyInputStreamParent::Create<dom::ContentParent>(nsIInputStream*,
                                                        uint64_t, uint64_t,
                                                        nsresult*,
                                                        dom::ContentParent*);

RemoteLazyInputStreamParent::RemoteLazyInputStreamParent(
    const nsID& aID, uint64_t aSize, dom::ContentParent* aManager)
    : mID(aID),
      mSize(aSize),
      mContentManager(aManager),
      mPBackgroundManager(nullptr),
      mSocketProcessManager(nullptr),
      mMigrating(false) {}

RemoteLazyInputStreamParent::RemoteLazyInputStreamParent(
    const nsID& aID, uint64_t aSize, PBackgroundParent* aManager)
    : mID(aID),
      mSize(aSize),
      mContentManager(nullptr),
      mPBackgroundManager(aManager),
      mSocketProcessManager(nullptr),
      mMigrating(false) {}

RemoteLazyInputStreamParent::RemoteLazyInputStreamParent(
    const nsID& aID, uint64_t aSize, net::SocketProcessParent* aManager)
    : mID(aID),
      mSize(aSize),
      mContentManager(nullptr),
      mPBackgroundManager(nullptr),
      mSocketProcessManager(aManager),
      mMigrating(false) {}

void RemoteLazyInputStreamParent::ActorDestroy(
    IProtocol::ActorDestroyReason aReason) {
  MOZ_ASSERT(mContentManager || mPBackgroundManager || mSocketProcessManager);

  mContentManager = nullptr;
  mPBackgroundManager = nullptr;
  mSocketProcessManager = nullptr;

  RefPtr<RemoteLazyInputStreamParentCallback> callback;
  mCallback.swap(callback);

  auto storage = RemoteLazyInputStreamStorage::Get().unwrapOr(nullptr);

  if (mMigrating) {
    if (callback && storage) {
      // We need to assign this callback to the next parent.
      storage->StoreCallback(mID, callback);
    }
    return;
  }

  if (storage) {
    storage->ForgetStream(mID);
  }

  if (callback) {
    callback->ActorDestroyed(mID);
  }
}

void RemoteLazyInputStreamParent::SetCallback(
    RemoteLazyInputStreamParentCallback* aCallback) {
  MOZ_ASSERT(aCallback);
  MOZ_ASSERT(!mCallback);

  mCallback = aCallback;
}

mozilla::ipc::IPCResult RemoteLazyInputStreamParent::RecvStreamNeeded() {
  MOZ_ASSERT(mContentManager || mPBackgroundManager || mSocketProcessManager);

  nsCOMPtr<nsIInputStream> stream;
  auto storage = RemoteLazyInputStreamStorage::Get().unwrapOr(nullptr);
  if (storage) {
    storage->GetStream(mID, 0, mSize, getter_AddRefs(stream));
  }

  if (!stream) {
    if (!SendStreamReady(Nothing())) {
      return IPC_FAIL(this, "SendStreamReady failed");
    }

    return IPC_OK();
  }

  mozilla::ipc::AutoIPCStream ipcStream;
  bool ok = false;

  if (mContentManager) {
    MOZ_ASSERT(NS_IsMainThread());
    ok = ipcStream.Serialize(stream, mContentManager);
  } else if (mPBackgroundManager) {
    ok = ipcStream.Serialize(stream, mPBackgroundManager);
  } else {
    MOZ_ASSERT(mSocketProcessManager);
    ok = ipcStream.Serialize(stream, mSocketProcessManager);
  }

  if (NS_WARN_IF(!ok)) {
    return IPC_FAIL(this, "SendStreamReady failed");
  }

  if (!SendStreamReady(Some(ipcStream.TakeValue()))) {
    return IPC_FAIL(this, "SendStreamReady failed");
  }

  return IPC_OK();
}

mozilla::ipc::IPCResult RemoteLazyInputStreamParent::RecvLengthNeeded() {
  MOZ_ASSERT(mContentManager || mPBackgroundManager || mSocketProcessManager);

  nsCOMPtr<nsIInputStream> stream;
  auto storage = RemoteLazyInputStreamStorage::Get().unwrapOr(nullptr);
  if (storage) {
    storage->GetStream(mID, 0, mSize, getter_AddRefs(stream));
  }

  if (!stream) {
    if (!SendLengthReady(-1)) {
      return IPC_FAIL(this, "SendLengthReady failed");
    }

    return IPC_OK();
  }

  int64_t length = -1;
  if (InputStreamLengthHelper::GetSyncLength(stream, &length)) {
    Unused << SendLengthReady(length);
    return IPC_OK();
  }

  RefPtr<RemoteLazyInputStreamParent> self = this;
  InputStreamLengthHelper::GetAsyncLength(stream, [self](int64_t aLength) {
    if (self->mContentManager || self->mPBackgroundManager ||
        self->mSocketProcessManager) {
      Unused << self->SendLengthReady(aLength);
    }
  });

  return IPC_OK();
}

mozilla::ipc::IPCResult RemoteLazyInputStreamParent::RecvClose() {
  MOZ_ASSERT(mContentManager || mPBackgroundManager || mSocketProcessManager);

  Unused << Send__delete__(this);
  return IPC_OK();
}

mozilla::ipc::IPCResult RemoteLazyInputStreamParent::Recv__delete__() {
  MOZ_ASSERT(mContentManager || mPBackgroundManager || mSocketProcessManager);
  mMigrating = true;
  return IPC_OK();
}

bool RemoteLazyInputStreamParent::HasValidStream() const {
  auto storage = RemoteLazyInputStreamStorage::Get().unwrapOr(nullptr);
  return storage ? storage->HasStream(mID) : false;
}

}  // namespace mozilla