summaryrefslogtreecommitdiffstats
path: root/ipc/glue/IPCStreamSource.cpp
blob: 3ddf0c9e52de205b74b6b44e5133dc9968d00825 (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
/* -*- 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 "IPCStreamSource.h"

#include "BackgroundParent.h"  // for AssertIsOnBackgroundThread
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/RemoteWorkerService.h"
#include "mozilla/dom/WorkerCommon.h"
#include "mozilla/webrender/WebRenderTypes.h"
#include "nsIAsyncInputStream.h"
#include "nsICancelableRunnable.h"
#include "nsIRunnable.h"
#include "nsISerialEventTarget.h"
#include "nsStreamUtils.h"
#include "nsThreadUtils.h"

using mozilla::wr::ByteBuffer;

namespace mozilla {
namespace ipc {

class IPCStreamSource::Callback final : public DiscardableRunnable,
                                        public nsIInputStreamCallback {
 public:
  explicit Callback(IPCStreamSource* aSource)
      : DiscardableRunnable("IPCStreamSource::Callback"),
        mSource(aSource),
        mOwningEventTarget(GetCurrentSerialEventTarget()) {
    MOZ_ASSERT(mSource);
  }

  NS_IMETHOD
  OnInputStreamReady(nsIAsyncInputStream* aStream) override {
    // any thread
    if (mOwningEventTarget->IsOnCurrentThread()) {
      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
    // IPCStreamChild listens for this event through the WorkerRef.
    nsresult rv =
        mOwningEventTarget->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(mOwningEventTarget->IsOnCurrentThread());
    if (mSource) {
      mSource->OnStreamReady(this);
    }
    return NS_OK;
  }

  // OnDiscard() gets called when the Worker thread is being shutdown.  We have
  // nothing to do here because IPCStreamChild handles this case via
  // the WorkerRef.

  void ClearSource() {
    MOZ_ASSERT(mOwningEventTarget->IsOnCurrentThread());
    MOZ_ASSERT(mSource);
    mSource = nullptr;
  }

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

    // ClearSource() should be called before the Callback is destroyed
    MOZ_ASSERT(!mSource);
  }

  // This is a raw pointer because the source keeps alive the callback and,
  // before beeing destroyed, it nullifies this pointer (this happens when
  // ActorDestroyed() is called).
  IPCStreamSource* mSource;

  nsCOMPtr<nsISerialEventTarget> mOwningEventTarget;

  NS_DECL_ISUPPORTS_INHERITED
};

NS_IMPL_ISUPPORTS_INHERITED(IPCStreamSource::Callback, DiscardableRunnable,
                            nsIInputStreamCallback);

IPCStreamSource::IPCStreamSource(nsIAsyncInputStream* aInputStream)
    : mStream(aInputStream), mState(ePending) {
  MOZ_ASSERT(aInputStream);
}

IPCStreamSource::~IPCStreamSource() {
  NS_ASSERT_OWNINGTHREAD(IPCStreamSource);
  MOZ_ASSERT(mState == eClosed);
  MOZ_ASSERT(!mCallback);
  MOZ_ASSERT(!mWorkerRef);
}

bool IPCStreamSource::Initialize() {
  bool nonBlocking = false;
  MOZ_ALWAYS_TRUE(NS_SUCCEEDED(mStream->IsNonBlocking(&nonBlocking)));
  // IPCStreamChild reads in the current thread, so it is only supported on
  // non-blocking, async channels
  if (!nonBlocking) {
    return false;
  }

  // A source can be used on any thread, but we only support IPCStream on
  // main thread, Workers, Worker Launcher, and PBackground thread right now.
  // This is due to the requirement  that the thread be guaranteed to live long
  // enough to receive messages. We can enforce this guarantee with a
  // StrongWorkerRef on worker threads, but not other threads. Main-thread,
  // PBackground, and Worker Launcher threads do not need anything special in
  // order to be kept alive.
  if (!NS_IsMainThread()) {
    if (const auto workerPrivate = dom::GetCurrentThreadWorkerPrivate()) {
      RefPtr<dom::StrongWorkerRef> workerRef =
          dom::StrongWorkerRef::CreateForcibly(workerPrivate,
                                               "IPCStreamSource");
      if (NS_WARN_IF(!workerRef)) {
        return false;
      }

      mWorkerRef = std::move(workerRef);
    } else {
      MOZ_DIAGNOSTIC_ASSERT(
          IsOnBackgroundThread() ||
          dom::RemoteWorkerService::Thread()->IsOnCurrentThread());
    }
  }

  return true;
}

void IPCStreamSource::ActorConstructed() {
  MOZ_ASSERT(mState == ePending);
  mState = eActorConstructed;
}

void IPCStreamSource::ActorDestroyed() {
  NS_ASSERT_OWNINGTHREAD(IPCStreamSource);

  mState = eClosed;

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

  mWorkerRef = nullptr;
}

void IPCStreamSource::Start() {
  NS_ASSERT_OWNINGTHREAD(IPCStreamSource);
  DoRead();
}

void IPCStreamSource::StartDestroy() {
  NS_ASSERT_OWNINGTHREAD(IPCStreamSource);
  OnEnd(NS_ERROR_ABORT);
}

void IPCStreamSource::DoRead() {
  NS_ASSERT_OWNINGTHREAD(IPCStreamSource);
  MOZ_ASSERT(mState == eActorConstructed);
  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");

  UniquePtr<char[]> buffer(new char[kMaxBytesPerMessage]);

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

    // See if the stream is closed by checking the return of Available.
    uint64_t dummy;
    nsresult rv = mStream->Available(&dummy);
    if (NS_FAILED(rv)) {
      OnEnd(rv);
      return;
    }

    uint32_t bytesRead = 0;
    rv = mStream->Read(buffer.get(), kMaxBytesPerMessage, &bytesRead);

    if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
      MOZ_ASSERT(bytesRead == 0);
      Wait();
      return;
    }

    if (NS_FAILED(rv)) {
      MOZ_ASSERT(bytesRead == 0);
      OnEnd(rv);
      return;
    }

    // Zero-byte read indicates end-of-stream.
    if (bytesRead == 0) {
      OnEnd(NS_BASE_STREAM_CLOSED);
      return;
    }

    // We read some data from the stream, send it across.
    SendData(ByteBuffer(bytesRead, reinterpret_cast<uint8_t*>(buffer.get())));
  }
}

void IPCStreamSource::Wait() {
  NS_ASSERT_OWNINGTHREAD(IPCStreamSource);
  MOZ_ASSERT(mState == eActorConstructed);
  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 IPCStreamSource::OnStreamReady(Callback* aCallback) {
  NS_ASSERT_OWNINGTHREAD(IPCStreamSource);
  MOZ_ASSERT(mCallback);
  MOZ_ASSERT(aCallback == mCallback);
  mCallback->ClearSource();
  mCallback = nullptr;

  // Possibly closed if this callback is (indirectly) called by
  // IPCStreamSourceParent::RecvRequestClose().
  if (mState == eClosed) {
    return;
  }

  DoRead();
}

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

  if (mState == eClosed) {
    return;
  }

  mState = eClosed;

  mStream->CloseWithStatus(aRv);

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

  // This will trigger an ActorDestroy() from the other side
  Close(aRv);
}

}  // namespace ipc
}  // namespace mozilla