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
430
431
432
433
434
435
436
437
438
439
440
|
/* -*- 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 "RemoteLazyInputStreamChild.h"
#include "RemoteLazyInputStreamThread.h"
#include "mozilla/ipc/IPCStreamUtils.h"
#include "mozilla/dom/WorkerCommon.h"
#include "mozilla/dom/WorkerRef.h"
namespace mozilla {
using namespace dom;
namespace {
// This runnable is used in case the last stream is forgotten on the 'wrong'
// thread.
class ShutdownRunnable final : public DiscardableRunnable {
public:
explicit ShutdownRunnable(RemoteLazyInputStreamChild* aActor)
: DiscardableRunnable("dom::ShutdownRunnable"), mActor(aActor) {}
NS_IMETHOD
Run() override {
mActor->Shutdown();
return NS_OK;
}
private:
RefPtr<RemoteLazyInputStreamChild> mActor;
};
// This runnable is used in case StreamNeeded() has been called on a non-owning
// thread.
class StreamNeededRunnable final : public DiscardableRunnable {
public:
explicit StreamNeededRunnable(RemoteLazyInputStreamChild* aActor)
: DiscardableRunnable("dom::StreamNeededRunnable"), mActor(aActor) {}
NS_IMETHOD
Run() override {
MOZ_ASSERT(
mActor->State() != RemoteLazyInputStreamChild::eActiveMigrating &&
mActor->State() != RemoteLazyInputStreamChild::eInactiveMigrating);
if (mActor->State() == RemoteLazyInputStreamChild::eActive) {
mActor->SendStreamNeeded();
}
return NS_OK;
}
private:
RefPtr<RemoteLazyInputStreamChild> mActor;
};
// When the stream has been received from the parent, we inform the
// RemoteLazyInputStream.
class StreamReadyRunnable final : public DiscardableRunnable {
public:
StreamReadyRunnable(RemoteLazyInputStream* aDestinationStream,
already_AddRefed<nsIInputStream> aCreatedStream)
: DiscardableRunnable("dom::StreamReadyRunnable"),
mDestinationStream(aDestinationStream),
mCreatedStream(std::move(aCreatedStream)) {
MOZ_ASSERT(mDestinationStream);
// mCreatedStream can be null.
}
NS_IMETHOD
Run() override {
mDestinationStream->StreamReady(mCreatedStream.forget());
return NS_OK;
}
private:
RefPtr<RemoteLazyInputStream> mDestinationStream;
nsCOMPtr<nsIInputStream> mCreatedStream;
};
// This runnable is used in case LengthNeeded() has been called on a non-owning
// thread.
class LengthNeededRunnable final : public DiscardableRunnable {
public:
explicit LengthNeededRunnable(RemoteLazyInputStreamChild* aActor)
: DiscardableRunnable("dom::LengthNeededRunnable"), mActor(aActor) {}
NS_IMETHOD
Run() override {
MOZ_ASSERT(
mActor->State() != RemoteLazyInputStreamChild::eActiveMigrating &&
mActor->State() != RemoteLazyInputStreamChild::eInactiveMigrating);
if (mActor->State() == RemoteLazyInputStreamChild::eActive) {
mActor->SendLengthNeeded();
}
return NS_OK;
}
private:
RefPtr<RemoteLazyInputStreamChild> mActor;
};
// When the stream has been received from the parent, we inform the
// RemoteLazyInputStream.
class LengthReadyRunnable final : public DiscardableRunnable {
public:
LengthReadyRunnable(RemoteLazyInputStream* aDestinationStream, int64_t aSize)
: DiscardableRunnable("dom::LengthReadyRunnable"),
mDestinationStream(aDestinationStream),
mSize(aSize) {
MOZ_ASSERT(mDestinationStream);
}
NS_IMETHOD
Run() override {
mDestinationStream->LengthReady(mSize);
return NS_OK;
}
private:
RefPtr<RemoteLazyInputStream> mDestinationStream;
int64_t mSize;
};
} // namespace
RemoteLazyInputStreamChild::RemoteLazyInputStreamChild(const nsID& aID,
uint64_t aSize)
: mMutex("RemoteLazyInputStreamChild::mMutex"),
mID(aID),
mSize(aSize),
mState(eActive),
mOwningEventTarget(GetCurrentSerialEventTarget()) {
// If we are running in a worker, we need to send a Close() to the parent side
// before the thread is released.
if (!NS_IsMainThread()) {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
if (!workerPrivate) {
return;
}
RefPtr<StrongWorkerRef> workerRef =
StrongWorkerRef::Create(workerPrivate, "RemoteLazyInputStreamChild");
if (!workerRef) {
return;
}
// We must keep the worker alive until the migration is completed.
mWorkerRef = new ThreadSafeWorkerRef(workerRef);
}
}
RemoteLazyInputStreamChild::~RemoteLazyInputStreamChild() = default;
void RemoteLazyInputStreamChild::Shutdown() {
MutexAutoLock lock(mMutex);
RefPtr<RemoteLazyInputStreamChild> kungFuDeathGrip = this;
mWorkerRef = nullptr;
mPendingOperations.Clear();
if (mState == eActive) {
SendClose();
mState = eInactive;
}
}
void RemoteLazyInputStreamChild::ActorDestroy(
IProtocol::ActorDestroyReason aReason) {
bool migrating = false;
{
MutexAutoLock lock(mMutex);
migrating = mState == eActiveMigrating;
mState = migrating ? eInactiveMigrating : eInactive;
}
if (!migrating) {
// Let's cleanup the workerRef and the pending operation queue.
Shutdown();
return;
}
}
RemoteLazyInputStreamChild::ActorState RemoteLazyInputStreamChild::State() {
MutexAutoLock lock(mMutex);
return mState;
}
already_AddRefed<RemoteLazyInputStream>
RemoteLazyInputStreamChild::CreateStream() {
bool shouldMigrate = false;
RefPtr<RemoteLazyInputStream> stream;
{
MutexAutoLock lock(mMutex);
if (mState == eInactive) {
return nullptr;
}
// The stream is active but maybe it is not running in the DOM-File thread.
// We should migrate it there.
if (mState == eActive &&
!RemoteLazyInputStreamThread::IsOnFileEventTarget(mOwningEventTarget)) {
MOZ_ASSERT(mStreams.IsEmpty());
shouldMigrate = true;
mState = eActiveMigrating;
RefPtr<RemoteLazyInputStreamThread> thread =
RemoteLazyInputStreamThread::GetOrCreate();
MOZ_ASSERT(thread, "We cannot continue without DOMFile thread.");
// Create a new actor object to connect to the target thread.
RefPtr<RemoteLazyInputStreamChild> newActor =
new RemoteLazyInputStreamChild(mID, mSize);
{
MutexAutoLock newActorLock(newActor->mMutex);
// Move over our local state onto the new actor object.
newActor->mWorkerRef = mWorkerRef;
newActor->mState = eInactiveMigrating;
newActor->mPendingOperations = std::move(mPendingOperations);
// Create the actual stream object.
stream = new RemoteLazyInputStream(newActor);
newActor->mStreams.AppendElement(stream);
}
// Perform the actual migration.
thread->MigrateActor(newActor);
} else {
stream = new RemoteLazyInputStream(this);
mStreams.AppendElement(stream);
}
}
// Send__delete__ will call ActorDestroy(). mMutex cannot be locked at this
// time.
if (shouldMigrate) {
Send__delete__(this);
}
return stream.forget();
}
void RemoteLazyInputStreamChild::ForgetStream(RemoteLazyInputStream* aStream) {
MOZ_ASSERT(aStream);
RefPtr<RemoteLazyInputStreamChild> kungFuDeathGrip = this;
{
MutexAutoLock lock(mMutex);
mStreams.RemoveElement(aStream);
if (!mStreams.IsEmpty() || mState != eActive) {
return;
}
}
if (mOwningEventTarget->IsOnCurrentThread()) {
Shutdown();
return;
}
RefPtr<ShutdownRunnable> runnable = new ShutdownRunnable(this);
mOwningEventTarget->Dispatch(runnable, NS_DISPATCH_NORMAL);
}
void RemoteLazyInputStreamChild::StreamNeeded(RemoteLazyInputStream* aStream,
nsIEventTarget* aEventTarget) {
MutexAutoLock lock(mMutex);
if (mState == eInactive) {
return;
}
MOZ_ASSERT(mStreams.Contains(aStream));
PendingOperation* opt = mPendingOperations.AppendElement();
opt->mStream = aStream;
opt->mEventTarget = aEventTarget;
opt->mOp = PendingOperation::eStreamNeeded;
if (mState == eActiveMigrating || mState == eInactiveMigrating) {
// This operation will be continued when the migration is completed.
return;
}
MOZ_ASSERT(mState == eActive);
if (mOwningEventTarget->IsOnCurrentThread()) {
SendStreamNeeded();
return;
}
RefPtr<StreamNeededRunnable> runnable = new StreamNeededRunnable(this);
mOwningEventTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
}
mozilla::ipc::IPCResult RemoteLazyInputStreamChild::RecvStreamReady(
const Maybe<IPCStream>& aStream) {
nsCOMPtr<nsIInputStream> stream = mozilla::ipc::DeserializeIPCStream(aStream);
RefPtr<RemoteLazyInputStream> pendingStream;
nsCOMPtr<nsIEventTarget> eventTarget;
{
MutexAutoLock lock(mMutex);
// We have been shutdown in the meantime.
if (mState == eInactive) {
return IPC_OK();
}
MOZ_ASSERT(!mPendingOperations.IsEmpty());
MOZ_ASSERT(mState == eActive);
pendingStream = mPendingOperations[0].mStream;
eventTarget = mPendingOperations[0].mEventTarget;
MOZ_ASSERT(mPendingOperations[0].mOp == PendingOperation::eStreamNeeded);
mPendingOperations.RemoveElementAt(0);
}
RefPtr<StreamReadyRunnable> runnable =
new StreamReadyRunnable(pendingStream, stream.forget());
// If RemoteLazyInputStream::AsyncWait() has been executed without passing an
// event target, we run the callback synchronous because any thread could be
// result to be the wrong one. See more in nsIAsyncInputStream::asyncWait
// documentation.
if (eventTarget) {
eventTarget->Dispatch(runnable, NS_DISPATCH_NORMAL);
} else {
runnable->Run();
}
return IPC_OK();
}
void RemoteLazyInputStreamChild::LengthNeeded(RemoteLazyInputStream* aStream,
nsIEventTarget* aEventTarget) {
MutexAutoLock lock(mMutex);
if (mState == eInactive) {
return;
}
MOZ_ASSERT(mStreams.Contains(aStream));
PendingOperation* opt = mPendingOperations.AppendElement();
opt->mStream = aStream;
opt->mEventTarget = aEventTarget;
opt->mOp = PendingOperation::eLengthNeeded;
if (mState == eActiveMigrating || mState == eInactiveMigrating) {
// This operation will be continued when the migration is completed.
return;
}
MOZ_ASSERT(mState == eActive);
if (mOwningEventTarget->IsOnCurrentThread()) {
SendLengthNeeded();
return;
}
RefPtr<LengthNeededRunnable> runnable = new LengthNeededRunnable(this);
mOwningEventTarget->Dispatch(runnable.forget(), NS_DISPATCH_NORMAL);
}
mozilla::ipc::IPCResult RemoteLazyInputStreamChild::RecvLengthReady(
const int64_t& aLength) {
RefPtr<RemoteLazyInputStream> pendingStream;
nsCOMPtr<nsIEventTarget> eventTarget;
{
MutexAutoLock lock(mMutex);
// We have been shutdown in the meantime.
if (mState == eInactive) {
return IPC_OK();
}
MOZ_ASSERT(!mPendingOperations.IsEmpty());
MOZ_ASSERT(mState == eActive);
pendingStream = mPendingOperations[0].mStream;
eventTarget = mPendingOperations[0].mEventTarget;
MOZ_ASSERT(mPendingOperations[0].mOp == PendingOperation::eLengthNeeded);
mPendingOperations.RemoveElementAt(0);
}
RefPtr<LengthReadyRunnable> runnable =
new LengthReadyRunnable(pendingStream, aLength);
MOZ_ASSERT(eventTarget);
eventTarget->Dispatch(runnable, NS_DISPATCH_NORMAL);
return IPC_OK();
}
void RemoteLazyInputStreamChild::Migrated() {
MutexAutoLock lock(mMutex);
MOZ_ASSERT(mState == eInactiveMigrating);
mWorkerRef = nullptr;
mOwningEventTarget = GetCurrentSerialEventTarget();
MOZ_ASSERT(
RemoteLazyInputStreamThread::IsOnFileEventTarget(mOwningEventTarget));
// Maybe we have no reasons to keep this actor alive.
if (mStreams.IsEmpty()) {
mState = eInactive;
SendClose();
return;
}
mState = eActive;
// Let's processing the pending operations. We need a stream for each pending
// operation.
for (uint32_t i = 0; i < mPendingOperations.Length(); ++i) {
if (mPendingOperations[i].mOp == PendingOperation::eStreamNeeded) {
SendStreamNeeded();
} else {
MOZ_ASSERT(mPendingOperations[i].mOp == PendingOperation::eLengthNeeded);
SendLengthNeeded();
}
}
}
} // namespace mozilla
|