Bug 1469914 - Prevent the HAL from registering duplicate observers; r=froydnj
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.
This commit is contained in:
@@ -20,10 +20,7 @@
|
||||
#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"
|
||||
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
#include "mozilla/hal_sandbox/PHal.h"
|
||||
#include "mozilla/HalScreenConfiguration.h"
|
||||
#include "mozilla/HalTypes.h"
|
||||
#include "mozilla/Observer.h"
|
||||
#include "mozilla/Types.h"
|
||||
|
||||
/*
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#ifndef mozilla_Observer_h
|
||||
#define mozilla_Observer_h
|
||||
|
||||
#include "nsTArray.h"
|
||||
#include "nsTObserverArray.h"
|
||||
|
||||
namespace mozilla {
|
||||
|
||||
@@ -48,7 +48,7 @@ public:
|
||||
*/
|
||||
void AddObserver(Observer<T>* aObserver)
|
||||
{
|
||||
mObservers.AppendElement(aObserver);
|
||||
mObservers.AppendElementUnlessExists(aObserver);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,17 +57,7 @@ public:
|
||||
*/
|
||||
bool RemoveObserver(Observer<T>* aObserver)
|
||||
{
|
||||
if (mObservers.RemoveElement(aObserver)) {
|
||||
if (!mBroadcastCopy.IsEmpty()) {
|
||||
// Annoyingly, someone could RemoveObserver() an item on the list
|
||||
// while we're in a Broadcast()'s Notify() call.
|
||||
auto i = mBroadcastCopy.IndexOf(aObserver);
|
||||
MOZ_ASSERT(i != mBroadcastCopy.NoIndex);
|
||||
mBroadcastCopy[i] = nullptr;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return mObservers.RemoveElement(aObserver);
|
||||
}
|
||||
|
||||
uint32_t Length()
|
||||
@@ -77,25 +67,18 @@ public:
|
||||
|
||||
/**
|
||||
* Call Notify() on each item in the list.
|
||||
* Handles the case of Notify() calling RemoveObserver()
|
||||
*/
|
||||
void Broadcast(const T& aParam)
|
||||
{
|
||||
MOZ_ASSERT(mBroadcastCopy.IsEmpty());
|
||||
mBroadcastCopy = mObservers;
|
||||
uint32_t size = mBroadcastCopy.Length();
|
||||
for (uint32_t i = 0; i < size; ++i) {
|
||||
// nulled if Removed during Broadcast
|
||||
if (mBroadcastCopy[i]) {
|
||||
mBroadcastCopy[i]->Notify(aParam);
|
||||
typename nsTObserverArray<Observer<T>*>::ForwardIterator iter(mObservers);
|
||||
while (iter.HasMore()) {
|
||||
Observer<T>* obs = iter.GetNext();
|
||||
obs->Notify(aParam);
|
||||
}
|
||||
}
|
||||
mBroadcastCopy.Clear();
|
||||
}
|
||||
|
||||
protected:
|
||||
nsTArray<Observer<T>*> mObservers;
|
||||
nsTArray<Observer<T>*> mBroadcastCopy;
|
||||
nsTObserverArray<Observer<T>*> mObservers;
|
||||
};
|
||||
|
||||
} // namespace mozilla
|
||||
|
||||
Reference in New Issue
Block a user