forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Blob.cpp
1404 lines (1134 loc) · 34.2 KB
/
Blob.cpp
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set sw=4 ts=8 et 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/Assertions.h"
#include "mozilla/DebugOnly.h"
#include "base/basictypes.h"
#include "Blob.h"
#include "nsIDOMFile.h"
#include "nsIInputStream.h"
#include "nsIIPCSerializableInputStream.h"
#include "nsIRemoteBlob.h"
#include "nsISeekableStream.h"
#include "mozilla/Monitor.h"
#include "mozilla/unused.h"
#include "mozilla/ipc/InputStreamUtils.h"
#include "nsDOMFile.h"
#include "nsThreadUtils.h"
#include "ContentChild.h"
#include "ContentParent.h"
#define PRIVATE_REMOTE_INPUT_STREAM_IID \
{0x30c7699f, 0x51d2, 0x48c8, {0xad, 0x56, 0xc0, 0x16, 0xd7, 0x6f, 0x71, 0x27}}
using namespace mozilla::dom;
using namespace mozilla::dom::ipc;
using namespace mozilla::ipc;
namespace {
class NS_NO_VTABLE IPrivateRemoteInputStream : public nsISupports
{
public:
NS_DECLARE_STATIC_IID_ACCESSOR(PRIVATE_REMOTE_INPUT_STREAM_IID)
// This will return the underlying stream.
virtual nsIInputStream*
BlockAndGetInternalStream() = 0;
};
NS_DEFINE_STATIC_IID_ACCESSOR(IPrivateRemoteInputStream,
PRIVATE_REMOTE_INPUT_STREAM_IID)
class RemoteInputStream : public nsIInputStream,
public nsISeekableStream,
public nsIIPCSerializableInputStream,
public IPrivateRemoteInputStream
{
mozilla::Monitor mMonitor;
nsCOMPtr<nsIDOMBlob> mSourceBlob;
nsCOMPtr<nsIInputStream> mStream;
nsCOMPtr<nsISeekableStream> mSeekableStream;
ActorFlavorEnum mOrigin;
public:
NS_DECL_ISUPPORTS
RemoteInputStream(nsIDOMBlob* aSourceBlob, ActorFlavorEnum aOrigin)
: mMonitor("RemoteInputStream.mMonitor"), mSourceBlob(aSourceBlob),
mOrigin(aOrigin)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aSourceBlob);
}
void
Serialize(InputStreamParams& aParams)
{
nsCOMPtr<nsIRemoteBlob> remote = do_QueryInterface(mSourceBlob);
MOZ_ASSERT(remote);
if (mOrigin == Parent) {
aParams = RemoteInputStreamParams(
static_cast<PBlobParent*>(remote->GetPBlob()), nullptr);
} else {
aParams = RemoteInputStreamParams(
nullptr, static_cast<PBlobChild*>(remote->GetPBlob()));
}
}
bool
Deserialize(const InputStreamParams& aParams)
{
// See InputStreamUtils.cpp to see how deserialization of a
// RemoteInputStream is special-cased.
MOZ_NOT_REACHED("RemoteInputStream should never be deserialized");
return false;
}
void
SetStream(nsIInputStream* aStream)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aStream);
nsCOMPtr<nsIInputStream> stream = aStream;
nsCOMPtr<nsISeekableStream> seekableStream = do_QueryInterface(aStream);
{
mozilla::MonitorAutoLock lock(mMonitor);
MOZ_ASSERT(!mStream);
MOZ_ASSERT(!mSeekableStream);
mStream.swap(stream);
mSeekableStream.swap(seekableStream);
mMonitor.Notify();
}
}
NS_IMETHOD
Close() MOZ_OVERRIDE
{
nsresult rv = BlockAndWaitForStream();
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDOMBlob> sourceBlob;
mSourceBlob.swap(sourceBlob);
rv = mStream->Close();
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
NS_IMETHOD
Available(uint64_t* aAvailable) MOZ_OVERRIDE
{
// See large comment in FileInputStreamWrapper::Available.
if (NS_IsMainThread()) {
return NS_BASE_STREAM_CLOSED;
}
nsresult rv = BlockAndWaitForStream();
NS_ENSURE_SUCCESS(rv, rv);
rv = mStream->Available(aAvailable);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
NS_IMETHOD
Read(char* aBuffer, uint32_t aCount, uint32_t* aResult) MOZ_OVERRIDE
{
nsresult rv = BlockAndWaitForStream();
NS_ENSURE_SUCCESS(rv, rv);
rv = mStream->Read(aBuffer, aCount, aResult);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
NS_IMETHOD
ReadSegments(nsWriteSegmentFun aWriter, void* aClosure, uint32_t aCount,
uint32_t* aResult) MOZ_OVERRIDE
{
nsresult rv = BlockAndWaitForStream();
NS_ENSURE_SUCCESS(rv, rv);
rv = mStream->ReadSegments(aWriter, aClosure, aCount, aResult);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
NS_IMETHOD
IsNonBlocking(bool* aNonBlocking) MOZ_OVERRIDE
{
NS_ENSURE_ARG_POINTER(aNonBlocking);
*aNonBlocking = false;
return NS_OK;
}
NS_IMETHOD
Seek(int32_t aWhence, int64_t aOffset) MOZ_OVERRIDE
{
nsresult rv = BlockAndWaitForStream();
NS_ENSURE_SUCCESS(rv, rv);
if (!mSeekableStream) {
NS_WARNING("Underlying blob stream is not seekable!");
return NS_ERROR_NO_INTERFACE;
}
rv = mSeekableStream->Seek(aWhence, aOffset);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
NS_IMETHOD
Tell(int64_t* aResult)
{
// We can cheat here and assume that we're going to start at 0 if we don't
// yet have our stream. Though, really, this should abort since most input
// streams could block here.
if (NS_IsMainThread() && !mStream) {
*aResult = 0;
return NS_OK;
}
nsresult rv = BlockAndWaitForStream();
NS_ENSURE_SUCCESS(rv, rv);
if (!mSeekableStream) {
NS_WARNING("Underlying blob stream is not seekable!");
return NS_ERROR_NO_INTERFACE;
}
rv = mSeekableStream->Tell(aResult);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
NS_IMETHOD
SetEOF()
{
nsresult rv = BlockAndWaitForStream();
NS_ENSURE_SUCCESS(rv, rv);
if (!mSeekableStream) {
NS_WARNING("Underlying blob stream is not seekable!");
return NS_ERROR_NO_INTERFACE;
}
rv = mSeekableStream->SetEOF();
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
virtual nsIInputStream*
BlockAndGetInternalStream()
{
MOZ_ASSERT(!NS_IsMainThread());
nsresult rv = BlockAndWaitForStream();
NS_ENSURE_SUCCESS(rv, nullptr);
return mStream;
}
private:
virtual ~RemoteInputStream()
{ }
void
ReallyBlockAndWaitForStream()
{
mozilla::DebugOnly<bool> waited;
{
mozilla::MonitorAutoLock lock(mMonitor);
waited = !mStream;
while (!mStream) {
mMonitor.Wait();
}
}
MOZ_ASSERT(mStream);
#ifdef DEBUG
if (waited && mSeekableStream) {
int64_t position;
MOZ_ASSERT(NS_SUCCEEDED(mSeekableStream->Tell(&position)),
"Failed to determine initial stream position!");
MOZ_ASSERT(!position, "Stream not starting at 0!");
}
#endif
}
nsresult
BlockAndWaitForStream()
{
if (NS_IsMainThread()) {
NS_WARNING("Blocking the main thread is not supported!");
return NS_ERROR_FAILURE;
}
ReallyBlockAndWaitForStream();
return NS_OK;
}
bool
IsSeekableStream()
{
if (NS_IsMainThread()) {
if (!mStream) {
NS_WARNING("Don't know if this stream is seekable yet!");
return true;
}
}
else {
ReallyBlockAndWaitForStream();
}
return !!mSeekableStream;
}
};
NS_IMPL_THREADSAFE_ADDREF(RemoteInputStream)
NS_IMPL_THREADSAFE_RELEASE(RemoteInputStream)
NS_INTERFACE_MAP_BEGIN(RemoteInputStream)
NS_INTERFACE_MAP_ENTRY(nsIInputStream)
NS_INTERFACE_MAP_ENTRY(nsIIPCSerializableInputStream)
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsISeekableStream, IsSeekableStream())
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
NS_INTERFACE_MAP_ENTRY(IPrivateRemoteInputStream)
NS_INTERFACE_MAP_END
template <ActorFlavorEnum ActorFlavor>
class InputStreamActor : public BlobTraits<ActorFlavor>::StreamType
{
nsRefPtr<RemoteInputStream> mRemoteStream;
public:
InputStreamActor(RemoteInputStream* aRemoteStream)
: mRemoteStream(aRemoteStream)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aRemoteStream);
}
InputStreamActor()
{
MOZ_ASSERT(NS_IsMainThread());
}
private:
// This method is only called by the IPDL message machinery.
virtual bool
Recv__delete__(const InputStreamParams& aParams) MOZ_OVERRIDE
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mRemoteStream);
nsCOMPtr<nsIInputStream> stream = DeserializeInputStream(aParams);
if (!stream) {
return false;
}
mRemoteStream->SetStream(stream);
return true;
}
};
template <ActorFlavorEnum ActorFlavor>
inline
already_AddRefed<nsIDOMBlob>
GetBlobFromParams(const SlicedBlobConstructorParams& aParams)
{
MOZ_STATIC_ASSERT(ActorFlavor == mozilla::dom::ipc::Parent,
"No other flavor is supported here!");
BlobParent* actor =
const_cast<BlobParent*>(
static_cast<const BlobParent*>(aParams.sourceParent()));
MOZ_ASSERT(actor);
return actor->GetBlob();
}
template <>
inline
already_AddRefed<nsIDOMBlob>
GetBlobFromParams<Child>(const SlicedBlobConstructorParams& aParams)
{
BlobChild* actor =
const_cast<BlobChild*>(
static_cast<const BlobChild*>(aParams.sourceChild()));
MOZ_ASSERT(actor);
return actor->GetBlob();
}
inline
void
SetBlobOnParams(BlobChild* aActor, SlicedBlobConstructorParams& aParams)
{
aParams.sourceChild() = aActor;
}
inline
void
SetBlobOnParams(BlobParent* aActor, SlicedBlobConstructorParams& aParams)
{
aParams.sourceParent() = aActor;
}
inline
nsDOMFileBase*
ToConcreteBlob(nsIDOMBlob* aBlob)
{
// XXX This is only safe so long as all blob implementations in our tree
// inherit nsDOMFileBase. If that ever changes then this will need to grow
// a real interface or something.
return static_cast<nsDOMFileBase*>(aBlob);
}
} // anonymous namespace
namespace mozilla {
namespace dom {
namespace ipc {
// Each instance of this class will be dispatched to the network stream thread
// pool to run the first time where it will open the file input stream. It will
// then dispatch itself back to the main thread to send the child process its
// response (assuming that the child has not crashed). The runnable will then
// dispatch itself to the thread pool again in order to close the file input
// stream.
class BlobTraits<Parent>::BaseType::OpenStreamRunnable : public nsRunnable
{
friend class nsRevocableEventPtr<OpenStreamRunnable>;
typedef BlobTraits<Parent> TraitsType;
typedef TraitsType::BaseType BlobActorType;
typedef TraitsType::StreamType BlobStreamProtocolType;
// Only safe to access these pointers if mRevoked is false!
BlobActorType* mBlobActor;
BlobStreamProtocolType* mStreamActor;
nsCOMPtr<nsIInputStream> mStream;
nsCOMPtr<nsIIPCSerializableInputStream> mSerializable;
nsCOMPtr<nsIEventTarget> mTarget;
bool mRevoked;
bool mClosing;
public:
OpenStreamRunnable(BlobActorType* aBlobActor,
BlobStreamProtocolType* aStreamActor,
nsIInputStream* aStream,
nsIIPCSerializableInputStream* aSerializable,
nsIEventTarget* aTarget)
: mBlobActor(aBlobActor), mStreamActor(aStreamActor), mStream(aStream),
mSerializable(aSerializable), mTarget(aTarget), mRevoked(false),
mClosing(false)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aBlobActor);
MOZ_ASSERT(aStreamActor);
MOZ_ASSERT(aStream);
// aSerializable may be null.
MOZ_ASSERT(aTarget);
}
NS_IMETHOD
Run()
{
if (NS_IsMainThread()) {
return SendResponse();
}
if (!mClosing) {
return OpenStream();
}
return CloseStream();
}
nsresult
Dispatch()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mTarget);
nsresult rv = mTarget->Dispatch(this, NS_DISPATCH_NORMAL);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
private:
void
Revoke()
{
MOZ_ASSERT(NS_IsMainThread());
#ifdef DEBUG
mBlobActor = nullptr;
mStreamActor = nullptr;
#endif
mRevoked = true;
}
nsresult
OpenStream()
{
MOZ_ASSERT(!NS_IsMainThread());
MOZ_ASSERT(mStream);
if (!mSerializable) {
nsCOMPtr<IPrivateRemoteInputStream> remoteStream =
do_QueryInterface(mStream);
MOZ_ASSERT(remoteStream, "Must QI to IPrivateRemoteInputStream here!");
nsCOMPtr<nsIInputStream> realStream =
remoteStream->BlockAndGetInternalStream();
NS_ENSURE_TRUE(realStream, NS_ERROR_FAILURE);
mSerializable = do_QueryInterface(realStream);
if (!mSerializable) {
MOZ_ASSERT(false, "Must be serializable!");
return NS_ERROR_FAILURE;
}
mStream.swap(realStream);
}
// To force the stream open we call Available(). We don't actually care
// how much data is available.
uint64_t available;
if (NS_FAILED(mStream->Available(&available))) {
NS_WARNING("Available failed on this stream!");
}
nsresult rv = NS_DispatchToMainThread(this, NS_DISPATCH_NORMAL);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
nsresult
CloseStream()
{
MOZ_ASSERT(!NS_IsMainThread());
MOZ_ASSERT(mStream);
// Going to always release here.
nsCOMPtr<nsIInputStream> stream;
mStream.swap(stream);
nsresult rv = stream->Close();
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
nsresult
SendResponse()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mStream);
MOZ_ASSERT(mSerializable);
MOZ_ASSERT(mTarget);
MOZ_ASSERT(!mClosing);
nsCOMPtr<nsIIPCSerializableInputStream> serializable;
mSerializable.swap(serializable);
if (mRevoked) {
MOZ_ASSERT(!mBlobActor);
MOZ_ASSERT(!mStreamActor);
}
else {
MOZ_ASSERT(mBlobActor);
MOZ_ASSERT(mStreamActor);
InputStreamParams params;
serializable->Serialize(params);
MOZ_ASSERT(params.type() != InputStreamParams::T__None);
unused << mStreamActor->Send__delete__(mStreamActor, params);
mBlobActor->NoteRunnableCompleted(this);
#ifdef DEBUG
mBlobActor = nullptr;
mStreamActor = nullptr;
#endif
}
mClosing = true;
nsCOMPtr<nsIEventTarget> target;
mTarget.swap(target);
nsresult rv = target->Dispatch(this, NS_DISPATCH_NORMAL);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
};
BlobTraits<Parent>::BaseType::BaseType()
{
}
BlobTraits<Parent>::BaseType::~BaseType()
{
}
void
BlobTraits<Parent>::BaseType::NoteRunnableCompleted(
BlobTraits<Parent>::BaseType::OpenStreamRunnable* aRunnable)
{
MOZ_ASSERT(NS_IsMainThread());
for (uint32_t index = 0; index < mOpenStreamRunnables.Length(); index++) {
nsRevocableEventPtr<BaseType::OpenStreamRunnable>& runnable =
mOpenStreamRunnables[index];
if (runnable.get() == aRunnable) {
runnable.Forget();
mOpenStreamRunnables.RemoveElementAt(index);
return;
}
}
MOZ_NOT_REACHED("Runnable not in our array!");
}
template <ActorFlavorEnum ActorFlavor>
struct RemoteBlobBase;
template <>
struct RemoteBlobBase<Parent>
{
InputStreamParams mInputStreamParams;
};
template <>
struct RemoteBlobBase<Child>
{ };
template <ActorFlavorEnum ActorFlavor>
class RemoteBlob : public RemoteBlobBase<ActorFlavor>,
public nsDOMFile,
public nsIRemoteBlob
{
public:
typedef RemoteBlob<ActorFlavor> SelfType;
typedef Blob<ActorFlavor> ActorType;
typedef InputStreamActor<ActorFlavor> StreamActorType;
typedef typename ActorType::ConstructorParamsType ConstructorParamsType;
private:
ActorType* mActor;
class StreamHelper : public nsRunnable
{
typedef Blob<ActorFlavor> ActorType;
typedef InputStreamActor<ActorFlavor> StreamActorType;
mozilla::Monitor mMonitor;
ActorType* mActor;
nsCOMPtr<nsIDOMBlob> mSourceBlob;
nsRefPtr<RemoteInputStream> mInputStream;
bool mDone;
public:
StreamHelper(ActorType* aActor, nsIDOMBlob* aSourceBlob)
: mMonitor("RemoteBlob::StreamHelper::mMonitor"), mActor(aActor),
mSourceBlob(aSourceBlob), mDone(false)
{
// This may be created on any thread.
MOZ_ASSERT(aActor);
MOZ_ASSERT(aSourceBlob);
}
nsresult
GetStream(nsIInputStream** aInputStream)
{
// This may be called on any thread.
MOZ_ASSERT(aInputStream);
MOZ_ASSERT(mActor);
MOZ_ASSERT(!mInputStream);
MOZ_ASSERT(!mDone);
if (NS_IsMainThread()) {
RunInternal(false);
}
else {
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
NS_ENSURE_TRUE(mainThread, NS_ERROR_FAILURE);
nsresult rv = mainThread->Dispatch(this, NS_DISPATCH_NORMAL);
NS_ENSURE_SUCCESS(rv, rv);
{
MonitorAutoLock lock(mMonitor);
while (!mDone) {
lock.Wait();
}
}
}
MOZ_ASSERT(!mActor);
MOZ_ASSERT(mDone);
if (!mInputStream) {
return NS_ERROR_UNEXPECTED;
}
mInputStream.forget(aInputStream);
return NS_OK;
}
NS_IMETHOD
Run()
{
MOZ_ASSERT(NS_IsMainThread());
RunInternal(true);
return NS_OK;
}
private:
void
RunInternal(bool aNotify)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mActor);
MOZ_ASSERT(!mInputStream);
MOZ_ASSERT(!mDone);
nsRefPtr<RemoteInputStream> stream = new RemoteInputStream(mSourceBlob,
ActorFlavor);
StreamActorType* streamActor = new StreamActorType(stream);
if (mActor->SendPBlobStreamConstructor(streamActor)) {
stream.swap(mInputStream);
}
mActor = nullptr;
if (aNotify) {
MonitorAutoLock lock(mMonitor);
mDone = true;
lock.Notify();
}
else {
mDone = true;
}
}
};
class SliceHelper : public nsRunnable
{
typedef Blob<ActorFlavor> ActorType;
mozilla::Monitor mMonitor;
ActorType* mActor;
nsCOMPtr<nsIDOMBlob> mSlice;
uint64_t mStart;
uint64_t mLength;
nsString mContentType;
bool mDone;
public:
SliceHelper(ActorType* aActor)
: mMonitor("RemoteBlob::SliceHelper::mMonitor"), mActor(aActor), mStart(0),
mLength(0), mDone(false)
{
// This may be created on any thread.
MOZ_ASSERT(aActor);
}
nsresult
GetSlice(uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
nsIDOMBlob** aSlice)
{
// This may be called on any thread.
MOZ_ASSERT(aSlice);
MOZ_ASSERT(mActor);
MOZ_ASSERT(!mSlice);
MOZ_ASSERT(!mDone);
mStart = aStart;
mLength = aLength;
mContentType = aContentType;
if (NS_IsMainThread()) {
RunInternal(false);
}
else {
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
NS_ENSURE_TRUE(mainThread, NS_ERROR_FAILURE);
nsresult rv = mainThread->Dispatch(this, NS_DISPATCH_NORMAL);
NS_ENSURE_SUCCESS(rv, rv);
{
MonitorAutoLock lock(mMonitor);
while (!mDone) {
lock.Wait();
}
}
}
MOZ_ASSERT(!mActor);
MOZ_ASSERT(mDone);
if (!mSlice) {
return NS_ERROR_UNEXPECTED;
}
mSlice.forget(aSlice);
return NS_OK;
}
NS_IMETHOD
Run()
{
MOZ_ASSERT(NS_IsMainThread());
RunInternal(true);
return NS_OK;
}
private:
void
RunInternal(bool aNotify)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mActor);
MOZ_ASSERT(!mSlice);
MOZ_ASSERT(!mDone);
NormalBlobConstructorParams normalParams;
normalParams.contentType() = mContentType;
normalParams.length() = mLength;
typename ActorType::ConstructorParamsType params;
ActorType::BaseType::SetBlobConstructorParams(params, normalParams);
ActorType* newActor = ActorType::Create(params);
MOZ_ASSERT(newActor);
SlicedBlobConstructorParams slicedParams;
slicedParams.contentType() = mContentType;
slicedParams.begin() = mStart;
slicedParams.end() = mStart + mLength;
SetBlobOnParams(mActor, slicedParams);
typename ActorType::OtherSideConstructorParamsType otherSideParams;
ActorType::BaseType::SetBlobConstructorParams(otherSideParams,
slicedParams);
if (mActor->Manager()->SendPBlobConstructor(newActor, otherSideParams)) {
mSlice = newActor->GetBlob();
}
mActor = nullptr;
if (aNotify) {
MonitorAutoLock lock(mMonitor);
mDone = true;
lock.Notify();
}
else {
mDone = true;
}
}
};
public:
NS_DECL_ISUPPORTS_INHERITED
RemoteBlob(const nsAString& aName, const nsAString& aContentType,
uint64_t aLength, uint64_t aModDate)
: nsDOMFile(aName, aContentType, aLength, aModDate), mActor(nullptr)
{
mImmutable = true;
}
RemoteBlob(const nsAString& aContentType, uint64_t aLength)
: nsDOMFile(aContentType, aLength), mActor(nullptr)
{
mImmutable = true;
}
RemoteBlob()
: nsDOMFile(EmptyString(), EmptyString(), UINT64_MAX, UINT64_MAX)
, mActor(nullptr)
{
mImmutable = true;
}
virtual ~RemoteBlob()
{
if (mActor) {
mActor->NoteDyingRemoteBlob();
}
}
void
SetActor(ActorType* aActor)
{
MOZ_ASSERT(!aActor || !mActor);
mActor = aActor;
}
void
MaybeSetInputStream(const ConstructorParamsType& aParams);
virtual already_AddRefed<nsIDOMBlob>
CreateSlice(uint64_t aStart, uint64_t aLength, const nsAString& aContentType)
MOZ_OVERRIDE
{
if (!mActor) {
return nullptr;
}
nsRefPtr<SliceHelper> helper = new SliceHelper(mActor);
nsCOMPtr<nsIDOMBlob> slice;
nsresult rv =
helper->GetSlice(aStart, aLength, aContentType, getter_AddRefs(slice));
NS_ENSURE_SUCCESS(rv, nullptr);
return slice.forget();
}
NS_IMETHOD
GetInternalStream(nsIInputStream** aStream) MOZ_OVERRIDE;
virtual void*
GetPBlob() MOZ_OVERRIDE
{
return static_cast<typename ActorType::ProtocolType*>(mActor);
}
NS_IMETHOD
GetLastModifiedDate(JSContext* cx, JS::Value* aLastModifiedDate)
{
if (IsDateUnknown()) {
aLastModifiedDate->setNull();
} else {
JSObject* date = JS_NewDateObjectMsec(cx, mLastModificationDate);
if (!date) {
return NS_ERROR_OUT_OF_MEMORY;
}
aLastModifiedDate->setObject(*date);
}
return NS_OK;
}
};
template <>
void
RemoteBlob<Parent>::MaybeSetInputStream(const ConstructorParamsType& aParams)
{
if (aParams.optionalInputStreamParams().type() ==
OptionalInputStreamParams::TInputStreamParams) {
mInputStreamParams =
aParams.optionalInputStreamParams().get_InputStreamParams();
}
else {
MOZ_ASSERT(aParams.blobParams().type() ==
ChildBlobConstructorParams::TMysteryBlobConstructorParams);
}
}
template <>
void
RemoteBlob<Child>::MaybeSetInputStream(const ConstructorParamsType& aParams)
{
// Nothing needed on the child side!
}
template <>
NS_IMETHODIMP
RemoteBlob<Parent>::GetInternalStream(nsIInputStream** aStream)
{
if (mInputStreamParams.type() != InputStreamParams::T__None) {
nsCOMPtr<nsIInputStream> stream = DeserializeInputStream(mInputStreamParams);
if (!stream) {
NS_WARNING("Failed to deserialize stream!");
return NS_ERROR_UNEXPECTED;
}
stream.forget(aStream);
return NS_OK;
}
if (!mActor) {
return NS_ERROR_UNEXPECTED;
}
nsRefPtr<StreamHelper> helper = new StreamHelper(mActor, this);
return helper->GetStream(aStream);
}