Bug 1449631 part 11. Remove nsIDOMEventTarget::AddSystemEventListener. r=smaug
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 05 Apr 2018 13:42:42 -0400
changeset 468099 6cf7b2b773dbefb08a1ca8f0a63ad5d8194b2f94
parent 468098 a73566a5d08ec5568f1f65cdf322d0fe93f99e81
child 468100 27805d201b9009bf17a508dff2c840a2e919907f
push id1728
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:12:27 +0000
treeherdermozilla-release@c296fde26f5f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1449631
milestone61.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 1449631 part 11. Remove nsIDOMEventTarget::AddSystemEventListener. r=smaug The security checks outer window did here don't seem right, because the whole point is that this method is only called by C++ code for its own purposes. We're not adding random untrusted listeners via addSystemEventListener! MozReview-Commit-ID: JdS5gTESclu
dom/base/nsGlobalWindowInner.cpp
dom/base/nsGlobalWindowOuter.cpp
dom/base/nsINode.cpp
dom/base/nsINode.h
dom/base/nsWindowRoot.cpp
dom/events/DOMEventTargetHelper.cpp
dom/events/EventListenerManager.h
dom/events/EventTarget.cpp
dom/events/EventTarget.h
dom/interfaces/events/nsIDOMEventTarget.idl
--- a/dom/base/nsGlobalWindowInner.cpp
+++ b/dom/base/nsGlobalWindowInner.cpp
@@ -4498,37 +4498,16 @@ nsGlobalWindowInner::DispatchEvent(Event
 }
 
 bool
 nsGlobalWindowInner::ComputeDefaultWantsUntrusted(ErrorResult& aRv)
 {
   return !nsContentUtils::IsChromeDoc(mDoc);
 }
 
-NS_IMETHODIMP
-nsGlobalWindowInner::AddSystemEventListener(const nsAString& aType,
-                                            nsIDOMEventListener *aListener,
-                                            bool aUseCapture,
-                                            bool aWantsUntrusted,
-                                            uint8_t aOptionalArgc)
-{
-  NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
-               "Won't check if this is chrome, you want to set "
-               "aWantsUntrusted to false or make the aWantsUntrusted "
-               "explicit by making optional_argc non-zero.");
-
-  if (!aWantsUntrusted &&
-      (aOptionalArgc < 2 && !nsContentUtils::IsChromeDoc(mDoc))) {
-    aWantsUntrusted = true;
-  }
-
-  return NS_AddSystemEventListener(this, aType, aListener, aUseCapture,
-                                   aWantsUntrusted);
-}
-
 EventListenerManager*
 nsGlobalWindowInner::GetOrCreateListenerManager()
 {
   if (!mListenerManager) {
     mListenerManager =
       new EventListenerManager(static_cast<EventTarget*>(this));
   }
 
--- a/dom/base/nsGlobalWindowOuter.cpp
+++ b/dom/base/nsGlobalWindowOuter.cpp
@@ -6470,43 +6470,16 @@ bool
 nsGlobalWindowOuter::ComputeDefaultWantsUntrusted(ErrorResult& aRv)
 {
   // It's OK that we just return false here on failure to create an
   // inner.  GetOrCreateListenerManager() will likewise fail, and then
   // we won't be adding any listeners anyway.
   FORWARD_TO_INNER_CREATE(ComputeDefaultWantsUntrusted, (aRv), false);
 }
 
-NS_IMETHODIMP
-nsGlobalWindowOuter::AddSystemEventListener(const nsAString& aType,
-                                            nsIDOMEventListener *aListener,
-                                            bool aUseCapture,
-                                            bool aWantsUntrusted,
-                                            uint8_t aOptionalArgc)
-{
-  NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
-               "Won't check if this is chrome, you want to set "
-               "aWantsUntrusted to false or make the aWantsUntrusted "
-               "explicit by making optional_argc non-zero.");
-
-  if (mInnerWindow &&
-      !nsContentUtils::LegacyIsCallerNativeCode() &&
-      !nsContentUtils::CanCallerAccess(mInnerWindow)) {
-    return NS_ERROR_DOM_SECURITY_ERR;
-  }
-
-  if (!aWantsUntrusted &&
-      (aOptionalArgc < 2 && !nsContentUtils::IsChromeDoc(mDoc))) {
-    aWantsUntrusted = true;
-  }
-
-  return NS_AddSystemEventListener(this, aType, aListener, aUseCapture,
-                                   aWantsUntrusted);
-}
-
 EventListenerManager*
 nsGlobalWindowOuter::GetOrCreateListenerManager()
 {
   FORWARD_TO_INNER_CREATE(GetOrCreateListenerManager, (), nullptr);
 }
 
 EventListenerManager*
 nsGlobalWindowOuter::GetExistingListenerManager() const
--- a/dom/base/nsINode.cpp
+++ b/dom/base/nsINode.cpp
@@ -1017,38 +1017,16 @@ nsINode::LookupNamespaceURI(const nsAStr
 }
 
 bool
 nsINode::ComputeDefaultWantsUntrusted(ErrorResult& aRv)
 {
   return !nsContentUtils::IsChromeDoc(OwnerDoc());
 }
 
-NS_IMETHODIMP
-nsINode::AddSystemEventListener(const nsAString& aType,
-                                nsIDOMEventListener *aListener,
-                                bool aUseCapture,
-                                bool aWantsUntrusted,
-                                uint8_t aOptionalArgc)
-{
-  NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
-               "Won't check if this is chrome, you want to set "
-               "aWantsUntrusted to false or make the aWantsUntrusted "
-               "explicit by making aOptionalArgc non-zero.");
-
-  if (!aWantsUntrusted &&
-      (aOptionalArgc < 2 &&
-       !nsContentUtils::IsChromeDoc(OwnerDoc()))) {
-    aWantsUntrusted = true;
-  }
-
-  return NS_AddSystemEventListener(this, aType, aListener, aUseCapture,
-                                   aWantsUntrusted);
-}
-
 void
 nsINode::GetBoxQuads(const BoxQuadOptions& aOptions,
                      nsTArray<RefPtr<DOMQuad> >& aResult,
                      CallerType aCallerType,
                      mozilla::ErrorResult& aRv)
 {
   mozilla::GetBoxQuads(this, aOptions, aResult, aCallerType, aRv);
 }
--- a/dom/base/nsINode.h
+++ b/dom/base/nsINode.h
@@ -1025,17 +1025,16 @@ public:
   NS_DECL_NSIDOMEVENTTARGET
 
   virtual mozilla::EventListenerManager*
     GetExistingListenerManager() const override;
   virtual mozilla::EventListenerManager*
     GetOrCreateListenerManager() override;
 
   bool ComputeDefaultWantsUntrusted(mozilla::ErrorResult& aRv) final;
-  using nsIDOMEventTarget::AddSystemEventListener;
 
   virtual bool IsApzAware() const override;
 
   virtual nsPIDOMWindowOuter* GetOwnerGlobalForBindings() override;
   virtual nsIGlobalObject* GetOwnerGlobal() const override;
 
   using mozilla::dom::EventTarget::DispatchEvent;
   bool DispatchEvent(mozilla::dom::Event& aEvent,
--- a/dom/base/nsWindowRoot.cpp
+++ b/dom/base/nsWindowRoot.cpp
@@ -85,32 +85,16 @@ nsWindowRoot::DispatchEvent(Event& aEven
 }
 
 bool
 nsWindowRoot::ComputeDefaultWantsUntrusted(ErrorResult& aRv)
 {
   return false;
 }
 
-NS_IMETHODIMP
-nsWindowRoot::AddSystemEventListener(const nsAString& aType,
-                                     nsIDOMEventListener *aListener,
-                                     bool aUseCapture,
-                                     bool aWantsUntrusted,
-                                     uint8_t aOptionalArgc)
-{
-  NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
-               "Won't check if this is chrome, you want to set "
-               "aWantsUntrusted to false or make the aWantsUntrusted "
-               "explicit by making optional_argc non-zero.");
-
-  return NS_AddSystemEventListener(this, aType, aListener, aUseCapture,
-                                   aWantsUntrusted);
-}
-
 EventListenerManager*
 nsWindowRoot::GetOrCreateListenerManager()
 {
   if (!mListenerManager) {
     mListenerManager =
       new EventListenerManager(static_cast<EventTarget*>(this));
   }
 
--- a/dom/events/DOMEventTargetHelper.cpp
+++ b/dom/events/DOMEventTargetHelper.cpp
@@ -180,37 +180,16 @@ DOMEventTargetHelper::ComputeDefaultWant
   nsresult rv = WantsUntrusted(&wantsUntrusted);
   if (NS_FAILED(rv)) {
     aRv.Throw(rv);
     return false;
   }
   return wantsUntrusted;
 }
 
-NS_IMETHODIMP
-DOMEventTargetHelper::AddSystemEventListener(const nsAString& aType,
-                                             nsIDOMEventListener* aListener,
-                                             bool aUseCapture,
-                                             bool aWantsUntrusted,
-                                             uint8_t aOptionalArgc)
-{
-  NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1,
-               "Won't check if this is chrome, you want to set "
-               "aWantsUntrusted to false or make the aWantsUntrusted "
-               "explicit by making aOptionalArgc non-zero.");
-
-  if (aOptionalArgc < 2) {
-    nsresult rv = WantsUntrusted(&aWantsUntrusted);
-    NS_ENSURE_SUCCESS(rv, rv);
-  }
-
-  return NS_AddSystemEventListener(this, aType, aListener, aUseCapture,
-                                   aWantsUntrusted);
-}
-
 bool
 DOMEventTargetHelper::DispatchEvent(Event& aEvent, CallerType aCallerType,
                                     ErrorResult& aRv)
 {
   nsEventStatus status = nsEventStatus_eIgnore;
   nsresult rv =
     EventDispatcher::DispatchDOMEvent(this, nullptr, &aEvent, nullptr, &status);
   bool retval = !aEvent.DefaultPrevented(aCallerType);
--- a/dom/events/EventListenerManager.h
+++ b/dom/events/EventListenerManager.h
@@ -636,31 +636,9 @@ protected:
   RefPtr<nsAtom> mNoListenerForEventAtom;
 
   friend class ELMCreationDetector;
   static uint32_t sMainThreadCreatedCount;
 };
 
 } // namespace mozilla
 
-/**
- * NS_AddSystemEventListener() is a helper function for implementing
- * EventTarget::AddSystemEventListener().
- */
-inline nsresult
-NS_AddSystemEventListener(mozilla::dom::EventTarget* aTarget,
-                          const nsAString& aType,
-                          nsIDOMEventListener *aListener,
-                          bool aUseCapture,
-                          bool aWantsUntrusted)
-{
-  mozilla::EventListenerManager* listenerManager =
-    aTarget->GetOrCreateListenerManager();
-  NS_ENSURE_STATE(listenerManager);
-  mozilla::EventListenerFlags flags;
-  flags.mInSystemGroup = true;
-  flags.mCapture = aUseCapture;
-  flags.mAllowUntrustedEvents = aWantsUntrusted;
-  listenerManager->AddEventListenerByType(aListener, aType, flags);
-  return NS_OK;
-}
-
 #endif // mozilla_EventListenerManager_h_
--- a/dom/events/EventTarget.cpp
+++ b/dom/events/EventTarget.cpp
@@ -100,16 +100,39 @@ EventTarget::RemoveEventListener(const n
                                  bool aUseCapture)
 {
   EventListenerManager* elm = GetExistingListenerManager();
   if (elm) {
     elm->RemoveEventListener(aType, aListener, aUseCapture);
   }
 }
 
+nsresult
+EventTarget::AddSystemEventListener(const nsAString& aType,
+                                    nsIDOMEventListener* aListener,
+                                    bool aUseCapture,
+                                    const Nullable<bool>& aWantsUntrusted)
+{
+  ErrorResult rv;
+  bool wantsUntrusted = ComputeWantsUntrusted(aWantsUntrusted, rv);
+  if (rv.Failed()) {
+    return rv.StealNSResult();
+  }
+
+  EventListenerManager* elm = GetOrCreateListenerManager();
+  NS_ENSURE_STATE(elm);
+
+  EventListenerFlags flags;
+  flags.mInSystemGroup = true;
+  flags.mCapture = aUseCapture;
+  flags.mAllowUntrustedEvents = wantsUntrusted;
+  elm->AddEventListenerByType(aListener, aType, flags);
+  return NS_OK;
+}
+
 void
 EventTarget::RemoveSystemEventListener(const nsAString& aType,
                                        nsIDOMEventListener *aListener,
                                        bool aUseCapture)
 {
   EventListenerManager* elm = GetExistingListenerManager();
   if (elm) {
     EventListenerFlags flags;
--- a/dom/events/EventTarget.h
+++ b/dom/events/EventTarget.h
@@ -3,16 +3,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_EventTarget_h_
 #define mozilla_dom_EventTarget_h_
 
 #include "mozilla/dom/BindingDeclarations.h"
+#include "mozilla/dom/Nullable.h"
 #include "nsIDOMEventTarget.h"
 #include "nsWrapperCache.h"
 #include "nsAtom.h"
 
 class nsPIDOMWindowOuter;
 class nsIGlobalObject;
 
 namespace mozilla {
@@ -28,18 +29,16 @@ namespace dom {
 
 class AddEventListenerOptionsOrBoolean;
 class Event;
 class EventListener;
 class EventListenerOptionsOrBoolean;
 class EventHandlerNonNull;
 class GlobalObject;
 
-template <class T> struct Nullable;
-
 // IID for the dom::EventTarget interface
 #define NS_EVENTTARGET_IID \
 { 0xde651c36, 0x0053, 0x4c67, \
   { 0xb1, 0x3d, 0x67, 0xb9, 0x40, 0xfc, 0x82, 0xe4 } }
 
 class EventTarget : public nsIDOMEventTarget,
                     public nsWrapperCache
 {
@@ -108,16 +107,38 @@ public:
    * RemoveSystemEventListener() should be used if you have used
    * AddSystemEventListener().
    */
   void RemoveSystemEventListener(const nsAString& aType,
                                  nsIDOMEventListener* aListener,
                                  bool aUseCapture);
 
   /**
+   * Add a system event listener with the default wantsUntrusted value.
+   */
+  nsresult AddSystemEventListener(const nsAString& aType,
+                                  nsIDOMEventListener* aListener,
+                                  bool aUseCapture)
+  {
+    return AddSystemEventListener(aType, aListener, aUseCapture, Nullable<bool>());
+  }
+
+  /**
+   * Add a system event listener with the given wantsUntrusted value.
+   */
+  nsresult AddSystemEventListener(const nsAString& aType,
+                                  nsIDOMEventListener* aListener,
+                                  bool aUseCapture,
+                                  bool aWantsUntrusted)
+  {
+    return AddSystemEventListener(aType, aListener, aUseCapture,
+                                  Nullable<bool>(aWantsUntrusted));
+  }
+
+  /**
    * Returns the EventTarget object which should be used as the target
    * of DOMEvents.
    * Usually |this| is returned, but for example Window (inner windw) returns
    * the WindowProxy (outer window).
    */
   virtual EventTarget* GetTargetForDOMEvent()
   {
     return this;
@@ -282,16 +303,37 @@ protected:
   virtual bool ComputeDefaultWantsUntrusted(ErrorResult& aRv) = 0;
 
   /**
    * A method to compute the right wantsUntrusted value for AddEventListener.
    * This will call the above hook as needed.
    */
   bool ComputeWantsUntrusted(const Nullable<bool>& aWantsUntrusted,
                              ErrorResult& aRv);
+
+  /**
+   * addSystemEventListener() adds an event listener of aType to the system
+   * group.  Typically, core code should use the system group for listening to
+   * content (i.e., non-chrome) element's events.  If core code uses
+   * EventTarget::AddEventListener for a content node, it means
+   * that the listener cannot listen to the event when web content calls
+   * stopPropagation() of the event.
+   *
+   * @param aType            An event name you're going to handle.
+   * @param aListener        An event listener.
+   * @param aUseCapture      true if you want to listen the event in capturing
+   *                         phase.  Otherwise, false.
+   * @param aWantsUntrusted  true if you want to handle untrusted events.
+   *                         false if not.
+   *                         Null if you want the default behavior.
+   */
+  nsresult AddSystemEventListener(const nsAString& aType,
+                                  nsIDOMEventListener* aListener,
+                                  bool aUseCapture,
+                                  const Nullable<bool>& aWantsUntrusted);
 };
 
 NS_DEFINE_STATIC_IID_ACCESSOR(EventTarget, NS_EVENTTARGET_IID)
 
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_EventTarget_h_
--- a/dom/interfaces/events/nsIDOMEventTarget.idl
+++ b/dom/interfaces/events/nsIDOMEventTarget.idl
@@ -1,90 +1,16 @@
 /* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "domstubs.idl"
 
 %{C++
-#include "mozilla/EventForwards.h"
 #include "mozilla/dom/Nullable.h"
-#include "js/TypeDecls.h"
-
 using mozilla::dom::Nullable;
-
-namespace mozilla {
-class EventListenerManager;
-namespace dom {
-class EventTarget;
-} // namespace dom
-} // namespace mozilla
-
-class nsPresContext;
 %}
 
-/**
- * The nsIDOMEventTarget interface is the interface implemented by all
- * event targets in the Document Object Model.
- *
- * For more information on this interface please see 
- * http://www.w3.org/TR/DOM-Level-2-Events/
- */
-
-[ptr] native WidgetEventPtr(mozilla::WidgetEvent);
-[ptr] native nsPresContextPtr(nsPresContext);
-[ptr] native nsEventStatusPtr(nsEventStatus);
-[ptr] native JSContextPtr(JSContext);
-[ref] native EventChainPostVisitorRef(mozilla::EventChainPostVisitor);
-[ptr] native EventListenerManagerPtr(mozilla::EventListenerManager);
-[ptr] native EventTargetPtr(mozilla::dom::EventTarget);
-
-interface nsIScriptContext;
-interface nsIDOMEventListener;
-interface nsIDOMEvent;
-
 [uuid(9a78ac3c-9507-4d00-b2d6-10b508d2ec31)]
 interface nsIDOMEventTarget : nsISupports
 {
-
-  /**
-   * addSystemEventListener() adds an event listener of aType to the system
-   * group.  Typically, core code should use system group for listening to
-   * content (i.e., non-chrome) element's events.  If core code uses
-   * nsIDOMEventTarget::AddEventListener for a content node, it means
-   * that the listener cannot listen the event when web content calls
-   * stopPropagation() of the event.
-   *
-   * @param aType            An event name you're going to handle.
-   * @param aListener        An event listener.
-   * @param aUseCapture      TRUE if you want to listen the event in capturing
-   *                         phase.  Otherwise, FALSE.
-   * @param aWantsUntrusted  TRUE if you want to handle untrusted events.
-   *                         Otherwise, FALSE.
-   * @return                 NS_OK if succeed.  Otherwise, NS_ERROR_*.
-   */
-  [noscript, optional_argc] void addSystemEventListener(
-                                   in DOMString type,
-                                   in nsIDOMEventListener listener,
-                                   [optional] in boolean aUseCapture,
-                                   [optional] in boolean aWantsUntrusted);
-
-%{C++
-  // non-virtual so it won't affect the vtable
-  nsresult AddSystemEventListener(const nsAString& aType,
-                                  nsIDOMEventListener* aListener,
-                                  bool aUseCapture)
-  {
-    return AddSystemEventListener(aType, aListener, aUseCapture, false, 1);
-  }
-  // non-virtual so it won't affect the vtable
-  nsresult AddSystemEventListener(const nsAString& aType,
-                                  nsIDOMEventListener* aListener,
-                                  bool aUseCapture,
-                                  bool aWantsUntrusted)
-  {
-    return AddSystemEventListener(aType, aListener, aUseCapture,
-                                  aWantsUntrusted, 2);
-  }
-%}
-
 };