summaryrefslogtreecommitdiff
path: root/ipc/glue/SendStreamChild.cpp
blob: 02e8726e89eb44bb0b9eeb3e530678fd154dea86 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* -*- 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 "mozilla/ipc/SendStream.h"

#include "mozilla/Unused.h"
#include "mozilla/dom/nsIContentChild.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/workers/bindings/WorkerHolder.h"
#include "mozilla/ipc/PBackgroundChild.h"
#include "nsIAsyncInputStream.h"
#include "nsICancelableRunnable.h"
#include "nsIRunnable.h"
#include "nsIThread.h"
#include "nsStreamUtils.h"

namespace mozilla {
namespace ipc {

using mozilla::dom::nsIContentChild;
using mozilla::dom::workers::Canceling;
using mozilla::dom::workers::GetCurrentThreadWorkerPrivate;
using mozilla::dom::workers::Status;
using mozilla::dom::workers::WorkerHolder;
using mozilla::dom::workers::WorkerPrivate;

namespace {

class SendStreamChildImpl final : public SendStreamChild
                                , public WorkerHolder
{
public:
  explicit SendStreamChildImpl(nsIAsyncInputStream* aStream);
  ~SendStreamChildImpl();

  void Start() override;
  void StartDestroy() override;

  bool
  AddAsWorkerHolder(dom::workers::WorkerPrivate* aWorkerPrivate);

private:
  class Callback;

  // PSendStreamChild methods
  virtual void
  ActorDestroy(ActorDestroyReason aReason) override;

  virtual bool
  RecvRequestClose(const nsresult& aRv) override;

  // WorkerHolder methods
  virtual bool
  Notify(Status aStatus) override;

  void DoRead();

  void Wait();

  void OnStreamReady(Callback* aCallback);

  void OnEnd(nsresult aRv);

  nsCOMPtr<nsIAsyncInputStream> mStream;
  RefPtr<Callback> mCallback;
  WorkerPrivate* mWorkerPrivate;
  bool mClosed;

  NS_DECL_OWNINGTHREAD
};

class SendStreamChildImpl::Callback final : public nsIInputStreamCallback
                                          , public nsIRunnable
                                          , public nsICancelableRunnable
{
public:
  explicit Callback(SendStreamChildImpl* aActor)
    : mActor(aActor)
    , mOwningThread(NS_GetCurrentThread())
  {
    MOZ_ASSERT(mActor);
  }

  NS_IMETHOD
  OnInputStreamReady(nsIAsyncInputStream* aStream) override
  {
    // any thread
    if (mOwningThread == NS_GetCurrentThread()) {
      return Run();
    }

    // If this fails, then it means the owning thread is a Worker that has
    // been shutdown.  Its ok to lose the event in this case because the
    // SendStreamChild listens for this event through the WorkerHolder.
    nsresult rv = mOwningThread->Dispatch(this, nsIThread::DISPATCH_NORMAL);
    if (NS_FAILED(rv)) {
      NS_WARNING("Failed to dispatch stream readable event to owning thread");
    }

    return NS_OK;
  }

  NS_IMETHOD
  Run() override
  {
    MOZ_ASSERT(mOwningThread == NS_GetCurrentThread());
    if (mActor) {
      mActor->OnStreamReady(this);
    }
    return NS_OK;
  }

  nsresult
  Cancel() override
  {
    // Cancel() gets called when the Worker thread is being shutdown.  We have
    // nothing to do here because SendStreamChild handles this case via
    // the WorkerHolder.
    return NS_OK;
  }

  void
  ClearActor()
  {
    MOZ_ASSERT(mOwningThread == NS_GetCurrentThread());
    MOZ_ASSERT(mActor);
    mActor = nullptr;
  }

private:
  ~Callback()
  {
    // called on any thread

    // ClearActor() should be called before the Callback is destroyed
    MOZ_ASSERT(!mActor);
  }

  SendStreamChildImpl* mActor;
  nsCOMPtr<nsIThread> mOwningThread;

  NS_DECL_THREADSAFE_ISUPPORTS
};

NS_IMPL_ISUPPORTS(SendStreamChildImpl::Callback, nsIInputStreamCallback,
                                                 nsIRunnable,
                                                 nsICancelableRunnable);

SendStreamChildImpl::SendStreamChildImpl(nsIAsyncInputStream* aStream)
  : mStream(aStream)
  , mWorkerPrivate(nullptr)
  , mClosed(false)
{
  MOZ_ASSERT(mStream);
}

SendStreamChildImpl::~SendStreamChildImpl()
{
  NS_ASSERT_OWNINGTHREAD(SendStreamChild);
  MOZ_ASSERT(mClosed);
  MOZ_ASSERT(!mCallback);
  MOZ_ASSERT(!mWorkerPrivate);
}

void
SendStreamChildImpl::Start()
{
  NS_ASSERT_OWNINGTHREAD(SendStreamChild);
  MOZ_ASSERT_IF(!NS_IsMainThread(), mWorkerPrivate);
  DoRead();
}

void
SendStreamChildImpl::StartDestroy()
{
  NS_ASSERT_OWNINGTHREAD(SendStreamChild);
  OnEnd(NS_ERROR_ABORT);
}

bool
SendStreamChildImpl::AddAsWorkerHolder(WorkerPrivate* aWorkerPrivate)
{
  NS_ASSERT_OWNINGTHREAD(SendStreamChild);
  MOZ_ASSERT(aWorkerPrivate);
  bool result = HoldWorker(aWorkerPrivate, Canceling);
  if (result) {
    mWorkerPrivate = aWorkerPrivate;
  }
  return result;
}

void
SendStreamChildImpl::ActorDestroy(ActorDestroyReason aReason)
{
  NS_ASSERT_OWNINGTHREAD(SendStreamChild);

  // If the parent side runs into a problem it will ask the child to
  // close the connection via RequestClose().  Therefore OnEnd() should
  // always run before the actor is destroyed.
  MOZ_ASSERT(mClosed);

  if (mCallback) {
    mCallback->ClearActor();
    mCallback = nullptr;
  }

  if (mWorkerPrivate) {
    ReleaseWorker();
    mWorkerPrivate = nullptr;
  }
}

bool
SendStreamChildImpl::RecvRequestClose(const nsresult& aRv)
{
  NS_ASSERT_OWNINGTHREAD(SendStreamChild);
  OnEnd(aRv);
  return true;
}

bool
SendStreamChildImpl::Notify(Status aStatus)
{
  NS_ASSERT_OWNINGTHREAD(SendStreamChild);

  // Keep the worker thread alive until the stream is finished.
  return true;
}

void
SendStreamChildImpl::DoRead()
{
  NS_ASSERT_OWNINGTHREAD(SendStreamChild);
  MOZ_ASSERT(!mClosed);
  MOZ_ASSERT(!mCallback);

  // The input stream (likely a pipe) probably uses a segment size of
  // 4kb.  If there is data already buffered it would be nice to aggregate
  // multiple segments into a single IPC call.  Conversely, don't send too
  // too large of a buffer in a single call to avoid spiking memory.
  static const uint64_t kMaxBytesPerMessage = 32 * 1024;
  static_assert(kMaxBytesPerMessage <= static_cast<uint64_t>(UINT32_MAX),
                "kMaxBytesPerMessage must cleanly cast to uint32_t");

  while (true) {
    // It should not be possible to transition to closed state without
    // this loop terminating via a return.
    MOZ_ASSERT(!mClosed);

    // Use non-auto here as we're unlikely to hit stack storage with the
    // sizes we are sending.  Also, it would be nice to avoid another copy
    // to the IPC layer which we avoid if we use COW strings.  Unfortunately
    // IPC does not seem to support passing dependent storage types.
    nsCString buffer;

    uint64_t available = 0;
    nsresult rv = mStream->Available(&available);
    if (NS_FAILED(rv)) {
      OnEnd(rv);
      return;
    }

    if (available == 0) {
      Wait();
      return;
    }

    uint32_t expectedBytes =
      static_cast<uint32_t>(std::min(available, kMaxBytesPerMessage));

    buffer.SetLength(expectedBytes);

    uint32_t bytesRead = 0;
    rv = mStream->Read(buffer.BeginWriting(), buffer.Length(), &bytesRead);
    MOZ_ASSERT_IF(NS_FAILED(rv), bytesRead == 0);
    buffer.SetLength(bytesRead);

    // If we read any data from the stream, send it across.
    if (!buffer.IsEmpty()) {
      Unused << SendBuffer(buffer);
    }

    if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
      Wait();
      return;
    }

    // Any other error or zero-byte read indicates end-of-stream
    if (NS_FAILED(rv) || buffer.IsEmpty()) {
      OnEnd(rv);
      return;
    }
  }
}

void
SendStreamChildImpl::Wait()
{
  NS_ASSERT_OWNINGTHREAD(SendStreamChild);
  MOZ_ASSERT(!mClosed);
  MOZ_ASSERT(!mCallback);

  // Set mCallback immediately instead of waiting for success.  Its possible
  // AsyncWait() will callback synchronously.
  mCallback = new Callback(this);
  nsresult rv = mStream->AsyncWait(mCallback, 0, 0, nullptr);
  if (NS_FAILED(rv)) {
    OnEnd(rv);
    return;
  }
}

void
SendStreamChildImpl::OnStreamReady(Callback* aCallback)
{
  NS_ASSERT_OWNINGTHREAD(SendStreamChild);
  MOZ_ASSERT(mCallback);
  MOZ_ASSERT(aCallback == mCallback);
  mCallback->ClearActor();
  mCallback = nullptr;
  DoRead();
}

void
SendStreamChildImpl::OnEnd(nsresult aRv)
{
  NS_ASSERT_OWNINGTHREAD(SendStreamChild);
  MOZ_ASSERT(aRv != NS_BASE_STREAM_WOULD_BLOCK);

  if (mClosed) {
    return;
  }

  mClosed = true;

  mStream->CloseWithStatus(aRv);

  if (aRv == NS_BASE_STREAM_CLOSED) {
    aRv = NS_OK;
  }

  // This will trigger an ActorDestroy() from the parent side
  Unused << SendClose(aRv);
}

bool
IsBlocking(nsIAsyncInputStream* aInputStream)
{
  bool nonBlocking = false;
  MOZ_ALWAYS_TRUE(NS_SUCCEEDED(aInputStream->IsNonBlocking(&nonBlocking)));
  return !nonBlocking;
}

} // anonymous namespace

// static
SendStreamChild*
SendStreamChild::Create(nsIAsyncInputStream* aInputStream,
                        nsIContentChild* aManager)
{
  MOZ_ASSERT(aInputStream);
  MOZ_ASSERT(aManager);

  // PContent can only be used on the main thread
  MOZ_ASSERT(NS_IsMainThread());

  // SendStreamChild reads in the current thread, so it is only supported
  // on non-blocking, async channels
  if (NS_WARN_IF(IsBlocking(aInputStream))) {
    return nullptr;
  }

  SendStreamChild* actor = new SendStreamChildImpl(aInputStream);
  aManager->SendPSendStreamConstructor(actor);

  return actor;
}

// static
SendStreamChild*
SendStreamChild::Create(nsIAsyncInputStream* aInputStream,
                        PBackgroundChild* aManager)
{
  MOZ_ASSERT(aInputStream);
  MOZ_ASSERT(aManager);

  // PBackground can be used on any thread, but we only support SendStream on
  // main thread and Worker threads right now.  This is due to the requirement
  // that the thread be guaranteed to live long enough to receive messages
  // sent from parent to child.  We can enforce this guarantee with a feature
  // on worker threads, but not other threads.
  WorkerPrivate* workerPrivate = nullptr;
  if (!NS_IsMainThread()) {
    workerPrivate = GetCurrentThreadWorkerPrivate();
    MOZ_ASSERT(workerPrivate);
  }

  // SendStreamChild reads in the current thread, so it is only supported
  // on non-blocking, async channels
  if (NS_WARN_IF(IsBlocking(aInputStream))) {
    return nullptr;
  }

  SendStreamChildImpl* actor = new SendStreamChildImpl(aInputStream);

  if (workerPrivate && !actor->AddAsWorkerHolder(workerPrivate)) {
    delete actor;
    return nullptr;
  }

  aManager->SendPSendStreamConstructor(actor);
  return actor;
}

SendStreamChild::~SendStreamChild()
{
}

void
DeallocPSendStreamChild(PSendStreamChild* aActor)
{
  delete aActor;
}

} // namespace ipc
} // namespace mozilla