Bug 504553 - patch 5 - AutoJSAPI in websockets in workers, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Fri, 10 Oct 2014 17:58:19 +0100
changeset 209873 e289b886cccb57acfb9717b6d46126f6ded12948
parent 209872 8edaa32e2278b7f1a8835a4f85d9a7b0d4eb5b53
child 209874 dc05ab9ff12cb81d7e2bd88494062fe9ad1c464d
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerssmaug
bugs504553
milestone35.0a1
Bug 504553 - patch 5 - AutoJSAPI in websockets in workers, r=smaug
content/base/src/WebSocket.cpp
--- a/content/base/src/WebSocket.cpp
+++ b/content/base/src/WebSocket.cpp
@@ -12,16 +12,17 @@
 #include "jsfriendapi.h"
 #include "js/OldDebugAPI.h"
 #include "mozilla/DOMEventTargetHelper.h"
 #include "mozilla/net/WebSocketChannel.h"
 #include "mozilla/dom/File.h"
 #include "mozilla/dom/ScriptSettings.h"
 #include "mozilla/dom/WorkerPrivate.h"
 #include "mozilla/dom/WorkerRunnable.h"
+#include "mozilla/dom/WorkerScope.h"
 #include "nsIScriptGlobalObject.h"
 #include "nsIDOMWindow.h"
 #include "nsIDocument.h"
 #include "nsXPCOM.h"
 #include "nsIXPConnect.h"
 #include "nsContentUtils.h"
 #include "nsError.h"
 #include "nsIScriptObjectPrincipal.h"
@@ -1538,28 +1539,30 @@ WebSocket::CreateAndDispatchSimpleEvent(
 
 nsresult
 WebSocket::CreateAndDispatchMessageEvent(const nsACString& aData,
                                          bool aIsBinary)
 {
   MOZ_ASSERT(mImpl);
   mImpl->AssertIsOnTargetThread();
 
+  AutoJSAPI jsapi;
+
   if (NS_IsMainThread()) {
-    AutoJSAPI jsapi;
     if (NS_WARN_IF(!jsapi.Init(GetOwner()))) {
       return NS_ERROR_FAILURE;
     }
-
-    return CreateAndDispatchMessageEvent(jsapi.cx(), aData, aIsBinary);
+  } else {
+    MOZ_ASSERT(mWorkerPrivate);
+    if (NS_WARN_IF(!jsapi.Init(mWorkerPrivate->GlobalScope()))) {
+      return NS_ERROR_FAILURE;
+    }
   }
 
-  MOZ_ASSERT(mWorkerPrivate);
-  return CreateAndDispatchMessageEvent(mWorkerPrivate->GetJSContext(), aData,
-                                       aIsBinary);
+  return CreateAndDispatchMessageEvent(jsapi.cx(), aData, aIsBinary);
 }
 
 nsresult
 WebSocket::CreateAndDispatchMessageEvent(JSContext* aCx,
                                          const nsACString& aData,
                                          bool aIsBinary)
 {
   MOZ_ASSERT(mImpl);