Bug 1449631 part 4. Remove nsIDOMEventTarget::GetContextForEventHandlers. r=smaug
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 05 Apr 2018 13:42:40 -0400
changeset 778074 1f824c936c25ac047e2a79aecb737f7095026d73
parent 778073 059872bf21beb529dc3552b3953474d007e76423
child 778075 d05e92371389bf5992a4c96a243b4e3d26fe7e1f
push id105383
push userbmo:gl@mozilla.com
push dateThu, 05 Apr 2018 20:22:58 +0000
reviewerssmaug
bugs1449631
milestone61.0a1
Bug 1449631 part 4. Remove nsIDOMEventTarget::GetContextForEventHandlers. r=smaug MozReview-Commit-ID: ID0FDvp28HY
dom/base/nsContentUtils.cpp
dom/base/nsContentUtils.h
dom/base/nsGlobalWindowInner.cpp
dom/base/nsGlobalWindowOuter.cpp
dom/base/nsINode.cpp
dom/base/nsWindowRoot.cpp
dom/events/DOMEventTargetHelper.cpp
dom/events/DataTransferItem.cpp
dom/events/EventDispatcher.cpp
dom/interfaces/events/nsIDOMEventTarget.idl
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -6054,45 +6054,16 @@ nsContentUtils::URIIsLocalFile(nsIURI *a
   // Important: we do NOT test the entire URI chain here!
   return util && NS_SUCCEEDED(util->ProtocolHasFlags(aURI,
                                 nsIProtocolHandler::URI_IS_LOCAL_FILE,
                                 &isFile)) &&
          isFile;
 }
 
 /* static */
-nsIScriptContext*
-nsContentUtils::GetContextForEventHandlers(nsINode* aNode,
-                                           nsresult* aRv)
-{
-  *aRv = NS_OK;
-  bool hasHadScriptObject = true;
-  nsIScriptGlobalObject* sgo =
-    aNode->OwnerDoc()->GetScriptHandlingObject(hasHadScriptObject);
-  // It is bad if the document doesn't have event handling context,
-  // but it used to have one.
-  if (!sgo && hasHadScriptObject) {
-    *aRv = NS_ERROR_UNEXPECTED;
-    return nullptr;
-  }
-
-  if (sgo) {
-    nsIScriptContext* scx = sgo->GetContext();
-    // Bad, no context from script global object!
-    if (!scx) {
-      *aRv = NS_ERROR_UNEXPECTED;
-      return nullptr;
-    }
-    return scx;
-  }
-
-  return nullptr;
-}
-
-/* static */
 JSContext *
 nsContentUtils::GetCurrentJSContext()
 {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(IsInitialized());
   if (!IsJSAPIActive()) {
     return nullptr;
   }
--- a/dom/base/nsContentUtils.h
+++ b/dom/base/nsContentUtils.h
@@ -1985,16 +1985,18 @@ public:
 
   /**
    * Returns true if it's safe to execute content script and false otherwise.
    *
    * The only known case where this lies is mutation events. They run, and can
    * run anything else, when this function returns false, but this is ok.
    */
   static bool IsSafeToRunScript() {
+    MOZ_ASSERT(NS_IsMainThread(),
+               "This static variable only makes sense on the main thread!");
     return sScriptBlockerCount == 0;
   }
 
   // XXXcatalinb: workaround for weird include error when trying to reference
   // ipdl types in WindowWatcher.
   static nsIWindowProvider*
   GetWindowProviderForContentProcess();
 
@@ -2039,19 +2041,16 @@ public:
    * and zoom of a page on mobile devices. We stick the information in
    * the document header and use it later on after rendering.
    *
    * See Bug #436083
    */
   static nsresult ProcessViewportInfo(nsIDocument *aDocument,
                                       const nsAString &viewportInfo);
 
-  static nsIScriptContext* GetContextForEventHandlers(nsINode* aNode,
-                                                      nsresult* aRv);
-
   static JSContext *GetCurrentJSContext();
   static JSContext *GetCurrentJSContextForThread();
 
   /**
    * Case insensitive comparison between two strings. However it only ignores
    * case for ASCII characters a-z.
    */
   static bool EqualsIgnoreASCIICase(const nsAString& aStr1,
--- a/dom/base/nsGlobalWindowInner.cpp
+++ b/dom/base/nsGlobalWindowInner.cpp
@@ -4583,23 +4583,16 @@ nsGlobalWindowInner::GetOrCreateListener
 }
 
 EventListenerManager*
 nsGlobalWindowInner::GetExistingListenerManager() const
 {
   return mListenerManager;
 }
 
-nsIScriptContext*
-nsGlobalWindowInner::GetContextForEventHandlers(nsresult* aRv)
-{
-  *aRv = NS_ERROR_UNEXPECTED;
-  FORWARD_TO_OUTER(GetContextForEventHandlers, (aRv), nullptr);
-}
-
 //*****************************************************************************
 // nsGlobalWindowInner::nsPIDOMWindow
 //*****************************************************************************
 
 nsPIDOMWindowOuter*
 nsGlobalWindowInner::GetPrivateRoot()
 {
   nsGlobalWindowOuter* outer = GetOuterWindowInternal();
--- a/dom/base/nsGlobalWindowOuter.cpp
+++ b/dom/base/nsGlobalWindowOuter.cpp
@@ -6542,29 +6542,16 @@ nsGlobalWindowOuter::GetOrCreateListener
 }
 
 EventListenerManager*
 nsGlobalWindowOuter::GetExistingListenerManager() const
 {
   FORWARD_TO_INNER(GetExistingListenerManager, (), nullptr);
 }
 
-nsIScriptContext*
-nsGlobalWindowOuter::GetContextForEventHandlers(nsresult* aRv)
-{
-  *aRv = NS_ERROR_UNEXPECTED;
-
-  nsIScriptContext* scx;
-  if ((scx = GetContext())) {
-    *aRv = NS_OK;
-    return scx;
-  }
-  return nullptr;
-}
-
 //*****************************************************************************
 // nsGlobalWindowOuter::nsPIDOMWindow
 //*****************************************************************************
 
 nsPIDOMWindowOuter*
 nsGlobalWindowOuter::GetPrivateParent()
 {
   nsCOMPtr<nsPIDOMWindowOuter> parent = GetParent();
--- a/dom/base/nsINode.cpp
+++ b/dom/base/nsINode.cpp
@@ -1175,22 +1175,16 @@ nsINode::GetOrCreateListenerManager()
 }
 
 EventListenerManager*
 nsINode::GetExistingListenerManager() const
 {
   return nsContentUtils::GetExistingListenerManagerForNode(this);
 }
 
-nsIScriptContext*
-nsINode::GetContextForEventHandlers(nsresult* aRv)
-{
-  return nsContentUtils::GetContextForEventHandlers(this, aRv);
-}
-
 nsPIDOMWindowOuter*
 nsINode::GetOwnerGlobalForBindings()
 {
   bool dummy;
   auto* window = static_cast<nsGlobalWindowInner*>(OwnerDoc()->GetScriptHandlingObject(dummy));
   return window ? nsPIDOMWindowOuter::GetFromCurrentInner(window->AsInner()) : nullptr;
 }
 
--- a/dom/base/nsWindowRoot.cpp
+++ b/dom/base/nsWindowRoot.cpp
@@ -144,23 +144,16 @@ nsWindowRoot::GetOrCreateListenerManager
 }
 
 EventListenerManager*
 nsWindowRoot::GetExistingListenerManager() const
 {
   return mListenerManager;
 }
 
-nsIScriptContext*
-nsWindowRoot::GetContextForEventHandlers(nsresult* aRv)
-{
-  *aRv = NS_OK;
-  return nullptr;
-}
-
 nsresult
 nsWindowRoot::GetEventTargetParent(EventChainPreVisitor& aVisitor)
 {
   aVisitor.mCanHandle = true;
   aVisitor.mForceContentDispatch = true; //FIXME! Bug 329119
   // To keep mWindow alive
   aVisitor.mItemData = static_cast<nsISupports *>(mWindow);
   aVisitor.SetParentTarget(mParent, false);
--- a/dom/events/DOMEventTargetHelper.cpp
+++ b/dom/events/DOMEventTargetHelper.cpp
@@ -300,28 +300,16 @@ DOMEventTargetHelper::GetOrCreateListene
 }
 
 EventListenerManager*
 DOMEventTargetHelper::GetExistingListenerManager() const
 {
   return mListenerManager;
 }
 
-nsIScriptContext*
-DOMEventTargetHelper::GetContextForEventHandlers(nsresult* aRv)
-{
-  *aRv = CheckInnerWindowCorrectness();
-  if (NS_FAILED(*aRv)) {
-    return nullptr;
-  }
-  nsPIDOMWindowInner* owner = GetOwner();
-  return owner ? nsGlobalWindowInner::Cast(owner)->GetContextInternal()
-               : nullptr;
-}
-
 nsresult
 DOMEventTargetHelper::WantsUntrusted(bool* aRetVal)
 {
   nsresult rv = CheckInnerWindowCorrectness();
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIDocument> doc = GetDocumentIfCurrent();
   // We can let listeners on workers to always handle all the events.
--- a/dom/events/DataTransferItem.cpp
+++ b/dom/events/DataTransferItem.cpp
@@ -543,22 +543,17 @@ DataTransferItem::Data(nsIPrincipal* aPr
     return nullptr;
   }
 
   nsCOMPtr<nsISupports> data;
   nsresult rv = variant->GetAsISupports(getter_AddRefs(data));
   if (NS_SUCCEEDED(rv) && data) {
     nsCOMPtr<EventTarget> pt = do_QueryInterface(data);
     if (pt) {
-      nsIScriptContext* c = pt->GetContextForEventHandlers(&rv);
-      if (NS_WARN_IF(NS_FAILED(rv) || !c)) {
-        return nullptr;
-      }
-
-      nsIGlobalObject* go = c->GetGlobalObject();
+      nsIGlobalObject* go = pt->GetOwnerGlobal();
       if (NS_WARN_IF(!go)) {
         return nullptr;
       }
 
       nsCOMPtr<nsIScriptObjectPrincipal> sp = do_QueryInterface(go);
       MOZ_ASSERT(sp, "This cannot fail on the main thread.");
 
       nsIPrincipal* dataPrincipal = sp->GetPrincipal();
--- a/dom/events/EventDispatcher.cpp
+++ b/dom/events/EventDispatcher.cpp
@@ -740,27 +740,24 @@ EventDispatcher::Dispatch(nsISupports* a
       // but use chrome event handler or TabChildGlobal for event target chain.
       target = piTarget;
     } else if (NS_WARN_IF(!doc)) {
       return NS_ERROR_UNEXPECTED;
     }
   }
 
 #ifdef DEBUG
-  if (aEvent->mMessage != eVoidEvent &&
+  if (NS_IsMainThread() &&
+      aEvent->mMessage != eVoidEvent &&
       !nsContentUtils::IsSafeToRunScript()) {
-    nsresult rv = NS_ERROR_FAILURE;
-    if (target->GetContextForEventHandlers(&rv) ||
-        NS_FAILED(rv)) {
-      nsCOMPtr<nsINode> node = do_QueryInterface(target);
-      if (node && nsContentUtils::IsChromeDoc(node->OwnerDoc())) {
-        NS_WARNING("Fix the caller!");
-      } else {
-        NS_ERROR("This is unsafe! Fix the caller!");
-      }
+    nsCOMPtr<nsINode> node = do_QueryInterface(target);
+    if (node && nsContentUtils::IsChromeDoc(node->OwnerDoc())) {
+      NS_WARNING("Fix the caller!");
+    } else {
+      NS_ERROR("This is unsafe! Fix the caller!");
     }
   }
 
   if (aDOMEvent) {
     WidgetEvent* innerEvent = aDOMEvent->WidgetEventPtr();
     NS_ASSERTION(innerEvent == aEvent,
                   "The inner event of aDOMEvent is not the same as aEvent!");
   }
--- a/dom/interfaces/events/nsIDOMEventTarget.idl
+++ b/dom/interfaces/events/nsIDOMEventTarget.idl
@@ -218,24 +218,16 @@ interface nsIDOMEventTarget : nsISupport
    * The default handling of the event should happen here.
    * @param aVisitor the visitor object which is used during post handling.
    *
    * @see EventDispatcher.h for documentation about aVisitor.
    * @note Only EventDispatcher should call this method.
    */
   [noscript, nostdcall]
   void PostHandleEvent(in EventChainPostVisitorRef aVisitor);
-
-  /**
-   * Get the script context in which the event handlers should be run.
-   * May return null.
-   * @note Caller *must* check the value of aRv.
-   */
-  [notxpcom, nostdcall]
-  nsIScriptContext GetContextForEventHandlers(out nsresult aRv);
 };
 
 %{C++
 
 #define NS_IMPL_DOMTARGET_DEFAULTS(_class) \
 mozilla::dom::EventTarget* _class::GetTargetForDOMEvent() { return this; } \
 mozilla::dom::EventTarget* _class::GetTargetForEventTargetChain() { return this; } \
 nsresult _class::WillHandleEvent(mozilla::EventChainPostVisitor& aVisitor) { return NS_OK; }