From b4232a0b60dbb8aceeab0f928c6e3944a1d9472c Mon Sep 17 00:00:00 2001 From: Kershaw Chang Date: Tue, 13 Jun 2017 20:14:00 -0400 Subject: [PATCH] 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. --- .../websocket/WebSocketChannelChild.cpp | 40 +++++++++++++++---- .../websocket/WebSocketChannelChild.h | 9 ++++- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/netwerk/protocol/websocket/WebSocketChannelChild.cpp b/netwerk/protocol/websocket/WebSocketChannelChild.cpp index 23be4284defa..127325e3b2c9 100644 --- a/netwerk/protocol/websocket/WebSocketChannelChild.cpp +++ b/netwerk/protocol/websocket/WebSocketChannelChild.cpp @@ -9,6 +9,7 @@ #include "mozilla/dom/TabChild.h" #include "mozilla/net/NeckoChild.h" #include "WebSocketChannelChild.h" +#include "nsContentUtils.h" #include "nsITabChild.h" #include "nsNetUtil.h" #include "mozilla/ipc/IPCStreamUtils.h" @@ -51,7 +52,8 @@ NS_INTERFACE_MAP_BEGIN(WebSocketChannelChild) NS_INTERFACE_MAP_END WebSocketChannelChild::WebSocketChannelChild(bool aEncrypted) - : mIPCState(Closed) + : NeckoTargetHolder(nullptr) + , mIPCState(Closed) , mMutex("WebSocketChannelChild::mMutex") { MOZ_ASSERT(NS_IsMainThread(), "not main thread"); @@ -107,9 +109,11 @@ WebSocketChannelChild::MaybeReleaseIPCObject() } if (!NS_IsMainThread()) { + nsCOMPtr target = GetNeckoTarget(); MOZ_ALWAYS_SUCCEEDS( - NS_DispatchToMainThread(NewRunnableMethod(this, - &WebSocketChannelChild::MaybeReleaseIPCObject))); + target->Dispatch(NewRunnableMethod(this, + &WebSocketChannelChild::MaybeReleaseIPCObject), + NS_DISPATCH_NORMAL)); return; } @@ -496,6 +500,17 @@ WebSocketChannelChild::OnServerClose(const uint16_t& aCode, } } +void +WebSocketChannelChild::SetupNeckoTarget() +{ + mNeckoTarget = nsContentUtils::GetEventTargetByLoadInfo(mLoadInfo, TaskCategory::Network); + if (!mNeckoTarget) { + return; + } + + gNeckoChild->SetEventTargetForActor(this, mNeckoTarget); +} + NS_IMETHODIMP WebSocketChannelChild::AsyncOpen(nsIURI *aURI, const nsACString &aOrigin, @@ -554,6 +569,9 @@ WebSocketChannelChild::AsyncOpen(nsIURI *aURI, transportProvider = ipcChild; } + // This must be called before sending constructor message. + SetupNeckoTarget(); + gNeckoChild->SendPWebSocketConstructor(this, tabChild, IPC::SerializedLoadContext(this), mSerial); @@ -607,7 +625,9 @@ WebSocketChannelChild::Close(uint16_t code, const nsACString & reason) { if (!NS_IsMainThread()) { MOZ_RELEASE_ASSERT(mTargetThread->IsOnCurrentThread()); - return NS_DispatchToMainThread(new CloseEvent(this, code, reason)); + nsCOMPtr target = GetNeckoTarget(); + return target->Dispatch(new CloseEvent(this, code, reason), + NS_DISPATCH_NORMAL); } LOG(("WebSocketChannelChild::Close() %p\n", this)); @@ -659,7 +679,9 @@ WebSocketChannelChild::SendMsg(const nsACString &aMsg) { if (!NS_IsMainThread()) { MOZ_RELEASE_ASSERT(IsOnTargetThread()); - return NS_DispatchToMainThread(new MsgEvent(this, aMsg, false)); + nsCOMPtr target = GetNeckoTarget(); + return target->Dispatch(new MsgEvent(this, aMsg, false), + NS_DISPATCH_NORMAL); } LOG(("WebSocketChannelChild::SendMsg() %p\n", this)); @@ -682,7 +704,9 @@ WebSocketChannelChild::SendBinaryMsg(const nsACString &aMsg) { if (!NS_IsMainThread()) { MOZ_RELEASE_ASSERT(IsOnTargetThread()); - return NS_DispatchToMainThread(new MsgEvent(this, aMsg, true)); + nsCOMPtr target = GetNeckoTarget(); + return target->Dispatch(new MsgEvent(this, aMsg, true), + NS_DISPATCH_NORMAL); } LOG(("WebSocketChannelChild::SendBinaryMsg() %p\n", this)); @@ -735,7 +759,9 @@ WebSocketChannelChild::SendBinaryStream(nsIInputStream *aStream, { if (!NS_IsMainThread()) { MOZ_RELEASE_ASSERT(mTargetThread->IsOnCurrentThread()); - return NS_DispatchToMainThread(new BinaryStreamEvent(this, aStream, aLength)); + nsCOMPtr target = GetNeckoTarget(); + return target->Dispatch(new BinaryStreamEvent(this, aStream, aLength), + NS_DISPATCH_NORMAL); } LOG(("WebSocketChannelChild::SendBinaryStream() %p\n", this)); diff --git a/netwerk/protocol/websocket/WebSocketChannelChild.h b/netwerk/protocol/websocket/WebSocketChannelChild.h index 6c58fbf903b3..8f6751bcf6d9 100644 --- a/netwerk/protocol/websocket/WebSocketChannelChild.h +++ b/netwerk/protocol/websocket/WebSocketChannelChild.h @@ -7,6 +7,7 @@ #ifndef mozilla_net_WebSocketChannelChild_h #define mozilla_net_WebSocketChannelChild_h +#include "mozilla/net/NeckoTargetHolder.h" #include "mozilla/net/PWebSocketChild.h" #include "mozilla/net/BaseWebSocketChannel.h" #include "nsString.h" @@ -19,7 +20,8 @@ class ChannelEvent; class ChannelEventQueue; class WebSocketChannelChild final : public BaseWebSocketChannel, - public PWebSocketChild + public PWebSocketChild, + public NeckoTargetHolder { public: explicit WebSocketChannelChild(bool aSecure); @@ -63,13 +65,16 @@ class WebSocketChannelChild final : public BaseWebSocketChannel, void OnBinaryMessageAvailable(const nsCString& aMsg); void OnAcknowledge(const uint32_t& aSize); void OnServerClose(const uint16_t& aCode, const nsCString& aReason); - void AsyncOpenFailed(); + void AsyncOpenFailed(); void DispatchToTargetThread(ChannelEvent *aChannelEvent); bool IsOnTargetThread(); void MaybeReleaseIPCObject(); + // This function tries to get a labeled event target for |mNeckoTarget|. + void SetupNeckoTarget(); + RefPtr mEventQ; nsString mEffectiveURL;