Skip to content

Commit b4232a0

Browse files
author
Kershaw Chang
committed
Bug 1343747 - Part 1: Label runnables in WebSocketChannelChild. r=mayhemer
Use nsContentUtils::GetEventTargetByLoadInfo to get a labeled event target and use it to dispatch runnables.
1 parent 8195d16 commit b4232a0

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

netwerk/protocol/websocket/WebSocketChannelChild.cpp

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "mozilla/dom/TabChild.h"
1010
#include "mozilla/net/NeckoChild.h"
1111
#include "WebSocketChannelChild.h"
12+
#include "nsContentUtils.h"
1213
#include "nsITabChild.h"
1314
#include "nsNetUtil.h"
1415
#include "mozilla/ipc/IPCStreamUtils.h"
@@ -51,7 +52,8 @@ NS_INTERFACE_MAP_BEGIN(WebSocketChannelChild)
5152
NS_INTERFACE_MAP_END
5253

5354
WebSocketChannelChild::WebSocketChannelChild(bool aEncrypted)
54-
: mIPCState(Closed)
55+
: NeckoTargetHolder(nullptr)
56+
, mIPCState(Closed)
5557
, mMutex("WebSocketChannelChild::mMutex")
5658
{
5759
MOZ_ASSERT(NS_IsMainThread(), "not main thread");
@@ -107,9 +109,11 @@ WebSocketChannelChild::MaybeReleaseIPCObject()
107109
}
108110

109111
if (!NS_IsMainThread()) {
112+
nsCOMPtr<nsIEventTarget> target = GetNeckoTarget();
110113
MOZ_ALWAYS_SUCCEEDS(
111-
NS_DispatchToMainThread(NewRunnableMethod(this,
112-
&WebSocketChannelChild::MaybeReleaseIPCObject)));
114+
target->Dispatch(NewRunnableMethod(this,
115+
&WebSocketChannelChild::MaybeReleaseIPCObject),
116+
NS_DISPATCH_NORMAL));
113117
return;
114118
}
115119

@@ -496,6 +500,17 @@ WebSocketChannelChild::OnServerClose(const uint16_t& aCode,
496500
}
497501
}
498502

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+
499514
NS_IMETHODIMP
500515
WebSocketChannelChild::AsyncOpen(nsIURI *aURI,
501516
const nsACString &aOrigin,
@@ -554,6 +569,9 @@ WebSocketChannelChild::AsyncOpen(nsIURI *aURI,
554569
transportProvider = ipcChild;
555570
}
556571

572+
// This must be called before sending constructor message.
573+
SetupNeckoTarget();
574+
557575
gNeckoChild->SendPWebSocketConstructor(this, tabChild,
558576
IPC::SerializedLoadContext(this),
559577
mSerial);
@@ -607,7 +625,9 @@ WebSocketChannelChild::Close(uint16_t code, const nsACString & reason)
607625
{
608626
if (!NS_IsMainThread()) {
609627
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);
611631
}
612632
LOG(("WebSocketChannelChild::Close() %p\n", this));
613633

@@ -659,7 +679,9 @@ WebSocketChannelChild::SendMsg(const nsACString &aMsg)
659679
{
660680
if (!NS_IsMainThread()) {
661681
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);
663685
}
664686
LOG(("WebSocketChannelChild::SendMsg() %p\n", this));
665687

@@ -682,7 +704,9 @@ WebSocketChannelChild::SendBinaryMsg(const nsACString &aMsg)
682704
{
683705
if (!NS_IsMainThread()) {
684706
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);
686710
}
687711
LOG(("WebSocketChannelChild::SendBinaryMsg() %p\n", this));
688712

@@ -735,7 +759,9 @@ WebSocketChannelChild::SendBinaryStream(nsIInputStream *aStream,
735759
{
736760
if (!NS_IsMainThread()) {
737761
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);
739765
}
740766

741767
LOG(("WebSocketChannelChild::SendBinaryStream() %p\n", this));

netwerk/protocol/websocket/WebSocketChannelChild.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#ifndef mozilla_net_WebSocketChannelChild_h
88
#define mozilla_net_WebSocketChannelChild_h
99

10+
#include "mozilla/net/NeckoTargetHolder.h"
1011
#include "mozilla/net/PWebSocketChild.h"
1112
#include "mozilla/net/BaseWebSocketChannel.h"
1213
#include "nsString.h"
@@ -19,7 +20,8 @@ class ChannelEvent;
1920
class ChannelEventQueue;
2021

2122
class WebSocketChannelChild final : public BaseWebSocketChannel,
22-
public PWebSocketChild
23+
public PWebSocketChild,
24+
public NeckoTargetHolder
2325
{
2426
public:
2527
explicit WebSocketChannelChild(bool aSecure);
@@ -63,13 +65,16 @@ class WebSocketChannelChild final : public BaseWebSocketChannel,
6365
void OnBinaryMessageAvailable(const nsCString& aMsg);
6466
void OnAcknowledge(const uint32_t& aSize);
6567
void OnServerClose(const uint16_t& aCode, const nsCString& aReason);
66-
void AsyncOpenFailed();
68+
void AsyncOpenFailed();
6769

6870
void DispatchToTargetThread(ChannelEvent *aChannelEvent);
6971
bool IsOnTargetThread();
7072

7173
void MaybeReleaseIPCObject();
7274

75+
// This function tries to get a labeled event target for |mNeckoTarget|.
76+
void SetupNeckoTarget();
77+
7378
RefPtr<ChannelEventQueue> mEventQ;
7479
nsString mEffectiveURL;
7580

0 commit comments

Comments
 (0)