9
9
#include " mozilla/dom/TabChild.h"
10
10
#include " mozilla/net/NeckoChild.h"
11
11
#include " WebSocketChannelChild.h"
12
+ #include " nsContentUtils.h"
12
13
#include " nsITabChild.h"
13
14
#include " nsNetUtil.h"
14
15
#include " mozilla/ipc/IPCStreamUtils.h"
@@ -51,7 +52,8 @@ NS_INTERFACE_MAP_BEGIN(WebSocketChannelChild)
51
52
NS_INTERFACE_MAP_END
52
53
53
54
WebSocketChannelChild::WebSocketChannelChild (bool aEncrypted)
54
- : mIPCState (Closed)
55
+ : NeckoTargetHolder(nullptr )
56
+ , mIPCState (Closed)
55
57
, mMutex (" WebSocketChannelChild::mMutex" )
56
58
{
57
59
MOZ_ASSERT (NS_IsMainThread(), " not main thread" );
@@ -107,9 +109,11 @@ WebSocketChannelChild::MaybeReleaseIPCObject()
107
109
}
108
110
109
111
if (!NS_IsMainThread()) {
112
+ nsCOMPtr<nsIEventTarget> target = GetNeckoTarget ();
110
113
MOZ_ALWAYS_SUCCEEDS (
111
- NS_DispatchToMainThread (NewRunnableMethod(this ,
112
- &WebSocketChannelChild::MaybeReleaseIPCObject)));
114
+ target->Dispatch (NewRunnableMethod (this ,
115
+ &WebSocketChannelChild::MaybeReleaseIPCObject),
116
+ NS_DISPATCH_NORMAL));
113
117
return ;
114
118
}
115
119
@@ -496,6 +500,17 @@ WebSocketChannelChild::OnServerClose(const uint16_t& aCode,
496
500
}
497
501
}
498
502
503
+ void
504
+ WebSocketChannelChild::SetupNeckoTarget ()
505
+ {
506
+ mNeckoTarget = nsContentUtils::GetEventTargetByLoadInfo (mLoadInfo , TaskCategory::Network);
507
+ if (!mNeckoTarget ) {
508
+ return ;
509
+ }
510
+
511
+ gNeckoChild ->SetEventTargetForActor (this , mNeckoTarget );
512
+ }
513
+
499
514
NS_IMETHODIMP
500
515
WebSocketChannelChild::AsyncOpen (nsIURI *aURI,
501
516
const nsACString &aOrigin,
@@ -554,6 +569,9 @@ WebSocketChannelChild::AsyncOpen(nsIURI *aURI,
554
569
transportProvider = ipcChild;
555
570
}
556
571
572
+ // This must be called before sending constructor message.
573
+ SetupNeckoTarget ();
574
+
557
575
gNeckoChild ->SendPWebSocketConstructor (this , tabChild,
558
576
IPC::SerializedLoadContext (this ),
559
577
mSerial );
@@ -607,7 +625,9 @@ WebSocketChannelChild::Close(uint16_t code, const nsACString & reason)
607
625
{
608
626
if (!NS_IsMainThread()) {
609
627
MOZ_RELEASE_ASSERT (mTargetThread ->IsOnCurrentThread ());
610
- return NS_DispatchToMainThread(new CloseEvent (this , code, reason));
628
+ nsCOMPtr<nsIEventTarget> target = GetNeckoTarget ();
629
+ return target->Dispatch (new CloseEvent (this , code, reason),
630
+ NS_DISPATCH_NORMAL);
611
631
}
612
632
LOG ((" WebSocketChannelChild::Close() %p\n " , this ));
613
633
@@ -659,7 +679,9 @@ WebSocketChannelChild::SendMsg(const nsACString &aMsg)
659
679
{
660
680
if (!NS_IsMainThread()) {
661
681
MOZ_RELEASE_ASSERT (IsOnTargetThread ());
662
- return NS_DispatchToMainThread(new MsgEvent (this , aMsg, false ));
682
+ nsCOMPtr<nsIEventTarget> target = GetNeckoTarget ();
683
+ return target->Dispatch (new MsgEvent (this , aMsg, false ),
684
+ NS_DISPATCH_NORMAL);
663
685
}
664
686
LOG ((" WebSocketChannelChild::SendMsg() %p\n " , this ));
665
687
@@ -682,7 +704,9 @@ WebSocketChannelChild::SendBinaryMsg(const nsACString &aMsg)
682
704
{
683
705
if (!NS_IsMainThread()) {
684
706
MOZ_RELEASE_ASSERT (IsOnTargetThread ());
685
- return NS_DispatchToMainThread(new MsgEvent (this , aMsg, true ));
707
+ nsCOMPtr<nsIEventTarget> target = GetNeckoTarget ();
708
+ return target->Dispatch (new MsgEvent (this , aMsg, true ),
709
+ NS_DISPATCH_NORMAL);
686
710
}
687
711
LOG ((" WebSocketChannelChild::SendBinaryMsg() %p\n " , this ));
688
712
@@ -735,7 +759,9 @@ WebSocketChannelChild::SendBinaryStream(nsIInputStream *aStream,
735
759
{
736
760
if (!NS_IsMainThread()) {
737
761
MOZ_RELEASE_ASSERT (mTargetThread ->IsOnCurrentThread ());
738
- return NS_DispatchToMainThread(new BinaryStreamEvent (this , aStream, aLength));
762
+ nsCOMPtr<nsIEventTarget> target = GetNeckoTarget ();
763
+ return target->Dispatch (new BinaryStreamEvent (this , aStream, aLength),
764
+ NS_DISPATCH_NORMAL);
739
765
}
740
766
741
767
LOG ((" WebSocketChannelChild::SendBinaryStream() %p\n " , this ));
0 commit comments