Bug 1539532 - EventSource must bind itself to valid nsIGlobalObject in workers, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 28 Mar 2019 22:06:29 +0000
changeset 466710 c45308b644b0bf71af22215a8e581e7ef9ddc10d
parent 466709 0e92cb061bb104f9448d0638bdf561f7e50c7ba4
child 466711 005d447749ecce7321818b2aa04a296cccfbb1ac
push id35780
push useropoprus@mozilla.com
push dateFri, 29 Mar 2019 21:53:01 +0000
treeherdermozilla-central@414f37afbe07 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1539532
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1539532 - EventSource must bind itself to valid nsIGlobalObject in workers, r=smaug Differential Revision: https://phabricator.services.mozilla.com/D25092
dom/base/EventSource.cpp
dom/base/EventSource.h
--- a/dom/base/EventSource.cpp
+++ b/dom/base/EventSource.cpp
@@ -1371,26 +1371,20 @@ void EventSourceImpl::DispatchAllMessage
   }
 
   nsresult rv = mEventSource->CheckInnerWindowCorrectness();
   if (NS_FAILED(rv)) {
     return;
   }
 
   AutoJSAPI jsapi;
-  if (mIsMainThread) {
-    if (NS_WARN_IF(!jsapi.Init(mEventSource->GetOwner()))) {
-      return;
-    }
-  } else {
-    MOZ_ASSERT(mWorkerRef);
-    if (NS_WARN_IF(!jsapi.Init(mWorkerRef->Private()->GlobalScope()))) {
-      return;
-    }
+  if (NS_WARN_IF(!jsapi.Init(mEventSource->GetOwnerGlobal()))) {
+    return;
   }
+
   JSContext* cx = jsapi.cx();
 
   while (mMessagesToDispatch.GetSize() > 0) {
     UniquePtr<Message> message(
         static_cast<Message*>(mMessagesToDispatch.PopFront()));
     // Now we can turn our string into a jsval
     JS::Rooted<JS::Value> jsData(cx);
     {
@@ -1778,23 +1772,24 @@ NS_IMETHODIMP
 EventSourceImpl::CheckListenerChain() {
   MOZ_ASSERT(NS_IsMainThread(), "Should be on the main thread!");
   return NS_OK;
 }
 ////////////////////////////////////////////////////////////////////////////////
 // EventSource
 ////////////////////////////////////////////////////////////////////////////////
 
-EventSource::EventSource(nsPIDOMWindowInner* aOwnerWindow,
+EventSource::EventSource(nsIGlobalObject* aGlobal,
                          nsICookieSettings* aCookieSettings,
                          bool aWithCredentials)
-    : DOMEventTargetHelper(aOwnerWindow),
+    : DOMEventTargetHelper(aGlobal),
       mWithCredentials(aWithCredentials),
       mIsMainThread(true),
       mKeepingAlive(false) {
+  MOZ_ASSERT(aGlobal);
   MOZ_ASSERT(aCookieSettings);
   mImpl = new EventSourceImpl(this, aCookieSettings);
 }
 
 EventSource::~EventSource() {}
 
 nsresult EventSource::CreateAndDispatchSimpleEvent(const nsAString& aName) {
   RefPtr<Event> event = NS_NewDOMEvent(this, nullptr, nullptr);
@@ -1805,39 +1800,41 @@ nsresult EventSource::CreateAndDispatchS
   DispatchEvent(*event, rv);
   return rv.StealNSResult();
 }
 
 /* static */
 already_AddRefed<EventSource> EventSource::Constructor(
     const GlobalObject& aGlobal, const nsAString& aURL,
     const EventSourceInit& aEventSourceInitDict, ErrorResult& aRv) {
-  nsCOMPtr<nsPIDOMWindowInner> ownerWindow =
-      do_QueryInterface(aGlobal.GetAsSupports());
-
-  MOZ_ASSERT(!NS_IsMainThread() || ownerWindow);
+  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
+  if (NS_WARN_IF(!global)) {
+    aRv.Throw(NS_ERROR_FAILURE);
+    return nullptr;
+  }
 
   nsCOMPtr<nsICookieSettings> cookieSettings;
+  nsCOMPtr<nsPIDOMWindowInner> ownerWindow = do_QueryInterface(global);
   if (ownerWindow) {
     Document* doc = ownerWindow->GetExtantDoc();
     if (NS_WARN_IF(!doc)) {
       aRv.Throw(NS_ERROR_FAILURE);
       return nullptr;
     }
 
     cookieSettings = doc->CookieSettings();
   } else {
     // Worker side.
     WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
     MOZ_ASSERT(workerPrivate);
     cookieSettings = workerPrivate->CookieSettings();
   }
 
   RefPtr<EventSource> eventSource = new EventSource(
-      ownerWindow, cookieSettings, aEventSourceInitDict.mWithCredentials);
+      global, cookieSettings, aEventSourceInitDict.mWithCredentials);
   RefPtr<EventSourceImpl> eventSourceImp = eventSource->mImpl;
 
   if (NS_IsMainThread()) {
     // Get principal from document and init EventSourceImpl
     nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal =
         do_QueryInterface(aGlobal.GetAsSupports());
     if (!scriptPrincipal) {
       aRv.Throw(NS_ERROR_FAILURE);
--- a/dom/base/EventSource.h
+++ b/dom/base/EventSource.h
@@ -19,17 +19,17 @@
 #include "nsIObserver.h"
 #include "nsIStreamListener.h"
 #include "nsIChannelEventSink.h"
 #include "nsIInterfaceRequestor.h"
 #include "nsITimer.h"
 #include "nsIHttpChannel.h"
 #include "nsDeque.h"
 
-class nsPIDOMWindowInner;
+class nsIGlobalObject;
 class nsICookieSettings;
 
 namespace mozilla {
 
 class ErrorResult;
 
 namespace dom {
 
@@ -76,18 +76,18 @@ class EventSource final : public DOMEven
 
   IMPL_EVENT_HANDLER(open)
   IMPL_EVENT_HANDLER(message)
   IMPL_EVENT_HANDLER(error)
 
   void Close();
 
  private:
-  EventSource(nsPIDOMWindowInner* aOwnerWindow,
-              nsICookieSettings* aCookieSettings, bool aWithCredentials);
+  EventSource(nsIGlobalObject* aGlobal, nsICookieSettings* aCookieSettings,
+              bool aWithCredentials);
   virtual ~EventSource();
   // prevent bad usage
   EventSource(const EventSource& x) = delete;
   EventSource& operator=(const EventSource& x) = delete;
 
   void AssertIsOnTargetThread() const {
     MOZ_ASSERT(NS_IsMainThread() == mIsMainThread);
   }