Bug 1469914 - Prevent the HAL from registering duplicate observers. r=froydnj, a=IanN CLOSED TREE DONTBUILD SEAMONKEY_2_49_ESR_RELBRANCH
authorGabriele Svelto <gsvelto@mozilla.com>
Fri, 22 Jun 2018 00:35:08 +0200
branchSEAMONKEY_2_49_ESR_RELBRANCH
changeset 357520 0df9f3e46a2ac3e642450e61665232c8b881b5fa
parent 357519 bc62db49bca39c347ffa4662ae3cb08b53857d8d
child 357521 d9462dfc7e56f3ce8cfe9f0171e09b575e141a79
push id7834
push userfrgrahl@gmx.net
push dateSun, 13 Jan 2019 12:17:02 +0000
treeherdermozilla-esr52@6e4ad8a8f2e8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj, IanN
bugs1469914
milestone52.9.1
Bug 1469914 - Prevent the HAL from registering duplicate observers. r=froydnj, a=IanN CLOSED TREE DONTBUILD mozilla-esr52 SEAMONKEY_2_49_ESR_RELBRANCH This also replaces the custom logic in ObserverList with an nsTObserverArray which has all the necessary logic for stable iteration over a potentially changing list of items. Unused dependencies were also removed.
hal/Hal.cpp
hal/Hal.h
xpcom/glue/Observer.h
--- a/hal/Hal.cpp
+++ b/hal/Hal.cpp
@@ -16,20 +16,17 @@
 #include "nsITabChild.h"
 #include "nsIWebNavigation.h"
 #include "nsThreadUtils.h"
 #include "nsXULAppAPI.h"
 #include "nsPIDOMWindow.h"
 #include "nsJSUtils.h"
 #include "mozilla/ClearOnShutdown.h"
 #include "mozilla/Observer.h"
-#include "mozilla/Services.h"
-#include "mozilla/StaticPtr.h"
 #include "mozilla/dom/ContentChild.h"
-#include "mozilla/dom/ContentParent.h"
 #include "mozilla/dom/ScreenOrientation.h"
 #include "WindowIdentifier.h"
 
 #ifdef XP_WIN
 #include <process.h>
 #define getpid _getpid
 #endif
 
--- a/hal/Hal.h
+++ b/hal/Hal.h
@@ -13,17 +13,16 @@
 #include "mozilla/dom/battery/Types.h"
 #include "mozilla/dom/MozPowerManagerBinding.h"
 #include "mozilla/dom/network/Types.h"
 #include "mozilla/dom/power/Types.h"
 #include "mozilla/dom/ScreenOrientation.h"
 #include "mozilla/hal_sandbox/PHal.h"
 #include "mozilla/HalScreenConfiguration.h"
 #include "mozilla/HalTypes.h"
-#include "mozilla/Observer.h"
 #include "mozilla/Types.h"
 
 /*
  * Hal.h contains the public Hal API.
  *
  * By default, this file defines its functions in the hal namespace, but if
  * MOZ_HAL_NAMESPACE is defined, we'll define our functions in that namespace.
  *
--- a/xpcom/glue/Observer.h
+++ b/xpcom/glue/Observer.h
@@ -2,17 +2,17 @@
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* 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_Observer_h
 #define mozilla_Observer_h
 
-#include "nsTArray.h"
+#include "nsTObserverArray.h"
 
 namespace mozilla {
 
 /**
  * Observer<T> provides a way for a class to observe something.
  * When an event has to be broadcasted to all Observer<T>, Notify() method
  * is called.
  * T represents the type of the object passed in argument to Notify().
@@ -43,17 +43,17 @@ public:
    * Note: When calling AddObserver, it's up to the caller to make sure the
    * object isn't going to be release as long as RemoveObserver hasn't been
    * called.
    *
    * @see RemoveObserver()
    */
   void AddObserver(Observer<T>* aObserver)
   {
-    mObservers.AppendElement(aObserver);
+    mObservers.AppendElementUnlessExists(aObserver);
   }
 
   /**
    * Remove the observer from the observer list.
    * @return Whether the observer has been found in the list.
    */
   bool RemoveObserver(Observer<T>* aObserver)
   {
@@ -62,22 +62,22 @@ public:
 
   uint32_t Length()
   {
     return mObservers.Length();
   }
 
   void Broadcast(const T& aParam)
   {
-    nsTArray<Observer<T>*> observersCopy(mObservers);
-    uint32_t size = observersCopy.Length();
-    for (uint32_t i = 0; i < size; ++i) {
-      observersCopy[i]->Notify(aParam);
+    typename nsTObserverArray<Observer<T>*>::ForwardIterator iter(mObservers);
+    while (iter.HasMore()) {
+      Observer<T>* obs = iter.GetNext();
+      obs->Notify(aParam);
     }
   }
 
 protected:
-  nsTArray<Observer<T>*> mObservers;
+  nsTObserverArray<Observer<T>*> mObservers;
 };
 
 } // namespace mozilla
 
 #endif // mozilla_Observer_h