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.
This commit is contained in:
Kershaw Chang 2017-06-13 20:14:00 -04:00
parent 8195d166cd
commit b4232a0b60
2 changed files with 40 additions and 9 deletions

View File

@ -9,6 +9,7 @@
#include "mozilla/dom/TabChild.h" #include "mozilla/dom/TabChild.h"
#include "mozilla/net/NeckoChild.h" #include "mozilla/net/NeckoChild.h"
#include "WebSocketChannelChild.h" #include "WebSocketChannelChild.h"
#include "nsContentUtils.h"
#include "nsITabChild.h" #include "nsITabChild.h"
#include "nsNetUtil.h" #include "nsNetUtil.h"
#include "mozilla/ipc/IPCStreamUtils.h" #include "mozilla/ipc/IPCStreamUtils.h"
@ -51,7 +52,8 @@ NS_INTERFACE_MAP_BEGIN(WebSocketChannelChild)
NS_INTERFACE_MAP_END NS_INTERFACE_MAP_END
WebSocketChannelChild::WebSocketChannelChild(bool aEncrypted) WebSocketChannelChild::WebSocketChannelChild(bool aEncrypted)
: mIPCState(Closed) : NeckoTargetHolder(nullptr)
, mIPCState(Closed)
, mMutex("WebSocketChannelChild::mMutex") , mMutex("WebSocketChannelChild::mMutex")
{ {
MOZ_ASSERT(NS_IsMainThread(), "not main thread"); MOZ_ASSERT(NS_IsMainThread(), "not main thread");
@ -107,9 +109,11 @@ WebSocketChannelChild::MaybeReleaseIPCObject()
} }
if (!NS_IsMainThread()) { if (!NS_IsMainThread()) {
nsCOMPtr<nsIEventTarget> target = GetNeckoTarget();
MOZ_ALWAYS_SUCCEEDS( MOZ_ALWAYS_SUCCEEDS(
NS_DispatchToMainThread(NewRunnableMethod(this, target->Dispatch(NewRunnableMethod(this,
&WebSocketChannelChild::MaybeReleaseIPCObject))); &WebSocketChannelChild::MaybeReleaseIPCObject),
NS_DISPATCH_NORMAL));
return; 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 NS_IMETHODIMP
WebSocketChannelChild::AsyncOpen(nsIURI *aURI, WebSocketChannelChild::AsyncOpen(nsIURI *aURI,
const nsACString &aOrigin, const nsACString &aOrigin,
@ -554,6 +569,9 @@ WebSocketChannelChild::AsyncOpen(nsIURI *aURI,
transportProvider = ipcChild; transportProvider = ipcChild;
} }
// This must be called before sending constructor message.
SetupNeckoTarget();
gNeckoChild->SendPWebSocketConstructor(this, tabChild, gNeckoChild->SendPWebSocketConstructor(this, tabChild,
IPC::SerializedLoadContext(this), IPC::SerializedLoadContext(this),
mSerial); mSerial);
@ -607,7 +625,9 @@ WebSocketChannelChild::Close(uint16_t code, const nsACString & reason)
{ {
if (!NS_IsMainThread()) { if (!NS_IsMainThread()) {
MOZ_RELEASE_ASSERT(mTargetThread->IsOnCurrentThread()); MOZ_RELEASE_ASSERT(mTargetThread->IsOnCurrentThread());
return NS_DispatchToMainThread(new CloseEvent(this, code, reason)); nsCOMPtr<nsIEventTarget> target = GetNeckoTarget();
return target->Dispatch(new CloseEvent(this, code, reason),
NS_DISPATCH_NORMAL);
} }
LOG(("WebSocketChannelChild::Close() %p\n", this)); LOG(("WebSocketChannelChild::Close() %p\n", this));
@ -659,7 +679,9 @@ WebSocketChannelChild::SendMsg(const nsACString &aMsg)
{ {
if (!NS_IsMainThread()) { if (!NS_IsMainThread()) {
MOZ_RELEASE_ASSERT(IsOnTargetThread()); MOZ_RELEASE_ASSERT(IsOnTargetThread());
return NS_DispatchToMainThread(new MsgEvent(this, aMsg, false)); nsCOMPtr<nsIEventTarget> target = GetNeckoTarget();
return target->Dispatch(new MsgEvent(this, aMsg, false),
NS_DISPATCH_NORMAL);
} }
LOG(("WebSocketChannelChild::SendMsg() %p\n", this)); LOG(("WebSocketChannelChild::SendMsg() %p\n", this));
@ -682,7 +704,9 @@ WebSocketChannelChild::SendBinaryMsg(const nsACString &aMsg)
{ {
if (!NS_IsMainThread()) { if (!NS_IsMainThread()) {
MOZ_RELEASE_ASSERT(IsOnTargetThread()); MOZ_RELEASE_ASSERT(IsOnTargetThread());
return NS_DispatchToMainThread(new MsgEvent(this, aMsg, true)); nsCOMPtr<nsIEventTarget> target = GetNeckoTarget();
return target->Dispatch(new MsgEvent(this, aMsg, true),
NS_DISPATCH_NORMAL);
} }
LOG(("WebSocketChannelChild::SendBinaryMsg() %p\n", this)); LOG(("WebSocketChannelChild::SendBinaryMsg() %p\n", this));
@ -735,7 +759,9 @@ WebSocketChannelChild::SendBinaryStream(nsIInputStream *aStream,
{ {
if (!NS_IsMainThread()) { if (!NS_IsMainThread()) {
MOZ_RELEASE_ASSERT(mTargetThread->IsOnCurrentThread()); MOZ_RELEASE_ASSERT(mTargetThread->IsOnCurrentThread());
return NS_DispatchToMainThread(new BinaryStreamEvent(this, aStream, aLength)); nsCOMPtr<nsIEventTarget> target = GetNeckoTarget();
return target->Dispatch(new BinaryStreamEvent(this, aStream, aLength),
NS_DISPATCH_NORMAL);
} }
LOG(("WebSocketChannelChild::SendBinaryStream() %p\n", this)); LOG(("WebSocketChannelChild::SendBinaryStream() %p\n", this));

View File

@ -7,6 +7,7 @@
#ifndef mozilla_net_WebSocketChannelChild_h #ifndef mozilla_net_WebSocketChannelChild_h
#define mozilla_net_WebSocketChannelChild_h #define mozilla_net_WebSocketChannelChild_h
#include "mozilla/net/NeckoTargetHolder.h"
#include "mozilla/net/PWebSocketChild.h" #include "mozilla/net/PWebSocketChild.h"
#include "mozilla/net/BaseWebSocketChannel.h" #include "mozilla/net/BaseWebSocketChannel.h"
#include "nsString.h" #include "nsString.h"
@ -19,7 +20,8 @@ class ChannelEvent;
class ChannelEventQueue; class ChannelEventQueue;
class WebSocketChannelChild final : public BaseWebSocketChannel, class WebSocketChannelChild final : public BaseWebSocketChannel,
public PWebSocketChild public PWebSocketChild,
public NeckoTargetHolder
{ {
public: public:
explicit WebSocketChannelChild(bool aSecure); explicit WebSocketChannelChild(bool aSecure);
@ -63,13 +65,16 @@ class WebSocketChannelChild final : public BaseWebSocketChannel,
void OnBinaryMessageAvailable(const nsCString& aMsg); void OnBinaryMessageAvailable(const nsCString& aMsg);
void OnAcknowledge(const uint32_t& aSize); void OnAcknowledge(const uint32_t& aSize);
void OnServerClose(const uint16_t& aCode, const nsCString& aReason); void OnServerClose(const uint16_t& aCode, const nsCString& aReason);
void AsyncOpenFailed(); void AsyncOpenFailed();
void DispatchToTargetThread(ChannelEvent *aChannelEvent); void DispatchToTargetThread(ChannelEvent *aChannelEvent);
bool IsOnTargetThread(); bool IsOnTargetThread();
void MaybeReleaseIPCObject(); void MaybeReleaseIPCObject();
// This function tries to get a labeled event target for |mNeckoTarget|.
void SetupNeckoTarget();
RefPtr<ChannelEventQueue> mEventQ; RefPtr<ChannelEventQueue> mEventQ;
nsString mEffectiveURL; nsString mEffectiveURL;