bug 819940 - remove nsISupportsArray::EnumerateForwards() r=ehsan

This commit is contained in:
Trevor Saunders 2012-12-10 05:27:14 -05:00
parent 7bd1a65ef4
commit be9baec5b3
6 changed files with 90 additions and 179 deletions

View File

@ -240,7 +240,7 @@ protected:
/**
* The builder observers.
*/
nsCOMPtr<nsISupportsArray> mObservers;
nsCOMArray<nsIXULTreeBuilderObserver> mObservers;
};
//----------------------------------------------------------------------
@ -282,22 +282,11 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsXULTreeBuilder, nsXULTemplateB
NS_IMPL_CYCLE_COLLECTION_UNLINK(mObservers)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
static bool TraverseObservers(nsISupports* aElement, void *aData)
{
nsCycleCollectionTraversalCallback *cb =
static_cast<nsCycleCollectionTraversalCallback*>(aData);
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*cb, "mObservers[i]");
cb->NoteXPCOMChild(aElement);
return true;
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsXULTreeBuilder, nsXULTemplateBuilder)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBoxObject)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSelection)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPersistStateStore)
if (tmp->mObservers) {
tmp->mObservers->EnumerateForwards(TraverseObservers, &cb);
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mObservers)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
DOMCI_DATA(XULTreeBuilder, nsXULTreeBuilder)
@ -361,20 +350,13 @@ nsXULTreeBuilder::GetIndexOfResource(nsIRDFResource* aResource, int32_t* aResult
NS_IMETHODIMP
nsXULTreeBuilder::AddObserver(nsIXULTreeBuilderObserver* aObserver)
{
nsresult rv;
if (!mObservers) {
rv = NS_NewISupportsArray(getter_AddRefs(mObservers));
if (NS_FAILED(rv))
return rv;
}
return mObservers->AppendElement(aObserver);
return mObservers.AppendObject(aObserver) ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsXULTreeBuilder::RemoveObserver(nsIXULTreeBuilderObserver* aObserver)
{
return mObservers ? mObservers->RemoveElement(aObserver) : NS_ERROR_FAILURE;
return mObservers.RemoveObject(aObserver) ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP
@ -855,14 +837,11 @@ nsXULTreeBuilder::ToggleOpenState(int32_t aIndex)
return rv;
}
if (mObservers) {
uint32_t count;
mObservers->Count(&count);
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = do_QueryElementAt(mObservers, i);
if (observer)
observer->OnToggleOpenState(aIndex);
}
uint32_t count = mObservers.Count();
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i);
if (observer)
observer->OnToggleOpenState(aIndex);
}
if (mPersistStateStore) {
@ -908,17 +887,14 @@ nsXULTreeBuilder::CycleHeader(nsITreeColumn* aCol)
nsCOMPtr<nsIDOMElement> element;
aCol->GetElement(getter_AddRefs(element));
if (mObservers) {
nsAutoString id;
aCol->GetId(id);
nsAutoString id;
aCol->GetId(id);
uint32_t count;
mObservers->Count(&count);
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = do_QueryElementAt(mObservers, i);
if (observer)
observer->OnCycleHeader(id.get(), element);
}
uint32_t count = mObservers.Count();
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i);
if (observer)
observer->OnCycleHeader(id.get(), element);
}
return Sort(element);
@ -927,14 +903,11 @@ nsXULTreeBuilder::CycleHeader(nsITreeColumn* aCol)
NS_IMETHODIMP
nsXULTreeBuilder::SelectionChanged()
{
if (mObservers) {
uint32_t count;
mObservers->Count(&count);
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = do_QueryElementAt(mObservers, i);
if (observer)
observer->OnSelectionChanged();
}
uint32_t count = mObservers.Count();
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i);
if (observer)
observer->OnSelectionChanged();
}
return NS_OK;
@ -944,17 +917,15 @@ NS_IMETHODIMP
nsXULTreeBuilder::CycleCell(int32_t aRow, nsITreeColumn* aCol)
{
NS_ENSURE_ARG_POINTER(aCol);
if (mObservers) {
nsAutoString id;
aCol->GetId(id);
uint32_t count;
mObservers->Count(&count);
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = do_QueryElementAt(mObservers, i);
if (observer)
observer->OnCycleCell(aRow, id.get());
}
nsAutoString id;
aCol->GetId(id);
uint32_t count = mObservers.Count();
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i);
if (observer)
observer->OnCycleCell(aRow, id.get());
}
return NS_OK;
@ -1029,14 +1000,11 @@ nsXULTreeBuilder::SetCellText(int32_t aRow, nsITreeColumn* aCol, const nsAString
NS_IMETHODIMP
nsXULTreeBuilder::PerformAction(const PRUnichar* aAction)
{
if (mObservers) {
uint32_t count;
mObservers->Count(&count);
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = do_QueryElementAt(mObservers, i);
if (observer)
observer->OnPerformAction(aAction);
}
uint32_t count = mObservers.Count();
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i);
if (observer)
observer->OnPerformAction(aAction);
}
return NS_OK;
@ -1045,14 +1013,11 @@ nsXULTreeBuilder::PerformAction(const PRUnichar* aAction)
NS_IMETHODIMP
nsXULTreeBuilder::PerformActionOnRow(const PRUnichar* aAction, int32_t aRow)
{
if (mObservers) {
uint32_t count;
mObservers->Count(&count);
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = do_QueryElementAt(mObservers, i);
if (observer)
observer->OnPerformActionOnRow(aAction, aRow);
}
uint32_t count = mObservers.Count();
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i);
if (observer)
observer->OnPerformActionOnRow(aAction, aRow);
}
return NS_OK;
@ -1062,17 +1027,14 @@ NS_IMETHODIMP
nsXULTreeBuilder::PerformActionOnCell(const PRUnichar* aAction, int32_t aRow, nsITreeColumn* aCol)
{
NS_ENSURE_ARG_POINTER(aCol);
if (mObservers) {
nsAutoString id;
aCol->GetId(id);
nsAutoString id;
aCol->GetId(id);
uint32_t count;
mObservers->Count(&count);
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = do_QueryElementAt(mObservers, i);
if (observer)
observer->OnPerformActionOnCell(aAction, aRow, id.get());
}
uint32_t count = mObservers.Count();
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i);
if (observer)
observer->OnPerformActionOnCell(aAction, aRow, id.get());
}
return NS_OK;
@ -1083,8 +1045,7 @@ void
nsXULTreeBuilder::NodeWillBeDestroyed(const nsINode* aNode)
{
nsCOMPtr<nsIMutationObserver> kungFuDeathGrip(this);
if (mObservers)
mObservers->Clear();
mObservers.Clear();
nsXULTemplateBuilder::NodeWillBeDestroyed(aNode);
}
@ -1918,16 +1879,13 @@ nsXULTreeBuilder::CanDrop(int32_t index, int32_t orientation,
nsIDOMDataTransfer* dataTransfer, bool *_retval)
{
*_retval = false;
if (mObservers) {
uint32_t count;
mObservers->Count(&count);
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = do_QueryElementAt(mObservers, i);
if (observer) {
observer->CanDrop(index, orientation, dataTransfer, _retval);
if (*_retval)
break;
}
uint32_t count = mObservers.Count();
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i);
if (observer) {
observer->CanDrop(index, orientation, dataTransfer, _retval);
if (*_retval)
break;
}
}
@ -1937,17 +1895,14 @@ nsXULTreeBuilder::CanDrop(int32_t index, int32_t orientation,
NS_IMETHODIMP
nsXULTreeBuilder::Drop(int32_t row, int32_t orient, nsIDOMDataTransfer* dataTransfer)
{
if (mObservers) {
uint32_t count;
mObservers->Count(&count);
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = do_QueryElementAt(mObservers, i);
if (observer) {
bool canDrop = false;
observer->CanDrop(row, orient, dataTransfer, &canDrop);
if (canDrop)
observer->OnDrop(row, orient, dataTransfer);
}
uint32_t count = mObservers.Count();
for (uint32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIXULTreeBuilderObserver> observer = mObservers.SafeObjectAt(i);
if (observer) {
bool canDrop = false;
observer->CanDrop(row, orient, dataTransfer, &canDrop);
if (canDrop)
observer->OnDrop(row, orient, dataTransfer);
}
}

View File

@ -15,8 +15,6 @@
* http://groups.google.com/groups?q=nsisupportsarray+group:netscape.public.mozilla.xpcom&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=al8412%245ab2%40ripley.netscape.com&rnum=8
*/
native nsISupportsArrayEnumFunc(nsISupportsArrayEnumFunc);
%{C++
class nsIBidirectionalEnumerator;
@ -32,15 +30,12 @@ class nsISupportsArray;
#define NS_SUPPORTSARRAY_CONTRACTID "@mozilla.org/supports-array;1"
#define NS_SUPPORTSARRAY_CLASSNAME "Supports Array"
// Enumerator callback function. Return PR_FALSE to stop
typedef bool (*nsISupportsArrayEnumFunc)(nsISupports* aElement, void *aData);
nsresult
NS_NewArrayEnumerator(nsISimpleEnumerator* *result,
nsISupportsArray* array);
%}
[scriptable, uuid(2d375c0f-8a34-4397-bea1-ab4d6beae0ab)]
[scriptable, uuid(241addc8-3608-4e73-8083-2fd6fa09eba2)]
interface nsISupportsArray : nsICollection {
[notxpcom] boolean Equals([const] in nsISupportsArray other);
@ -74,9 +69,6 @@ interface nsISupportsArray : nsICollection {
void Compact();
[notxpcom, noscript]
boolean EnumerateForwards(in nsISupportsArrayEnumFunc aFunc,
in voidPtr aData);
nsISupportsArray clone();

View File

@ -571,18 +571,6 @@ nsSupportsArray::SizeTo(int32_t aSize)
return true;
}
NS_IMETHODIMP_(bool)
nsSupportsArray::EnumerateForwards(nsISupportsArrayEnumFunc aFunc, void* aData)
{
int32_t aIndex = -1;
bool running = true;
while (running && (++aIndex < (int32_t)mCount)) {
running = (*aFunc)(mArray[aIndex], aData);
}
return running;
}
NS_IMETHODIMP
nsSupportsArray::Enumerate(nsIEnumerator* *result)
{
@ -594,15 +582,6 @@ nsSupportsArray::Enumerate(nsIEnumerator* *result)
return NS_OK;
}
static bool
CopyElement(nsISupports* aElement, void *aData)
{
nsresult rv;
nsISupportsArray* newArray = (nsISupportsArray*)aData;
rv = newArray->AppendElement(aElement);
return NS_SUCCEEDED(rv);
}
NS_IMETHODIMP
nsSupportsArray::Clone(nsISupportsArray** aResult)
{
@ -610,8 +589,13 @@ nsSupportsArray::Clone(nsISupportsArray** aResult)
nsresult rv = NS_NewISupportsArray(getter_AddRefs(newArray));
NS_ENSURE_SUCCESS(rv, rv);
bool ok = EnumerateForwards(CopyElement, newArray);
NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
uint32_t count = 0;
Count(&count);
for (uint32_t i = 0; i < count; i++) {
if (!newArray->InsertElementAt(mArray[i], i)) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
newArray.forget(aResult);
return NS_OK;

View File

@ -105,8 +105,6 @@ public:
NS_IMETHOD Compact(void);
NS_IMETHOD_(bool) EnumerateForwards(nsISupportsArrayEnumFunc aFunc, void* aData);
NS_IMETHOD Clone(nsISupportsArray **_retval);
NS_IMETHOD_(bool) InsertElementsAt(nsISupportsArray *aOther, uint32_t aIndex);

View File

@ -29,9 +29,9 @@ using namespace mozilla;
static nsresult GetDOMWindow(nsIXULWindow* inWindow,
nsCOMPtr< nsIDOMWindow>& outDOMWindow);
static bool notifyOpenWindow(nsISupports *aElement, void* aData);
static bool notifyCloseWindow(nsISupports *aElement, void* aData);
static bool notifyWindowTitleChange(nsISupports *aElement, void* aData);
static bool notifyOpenWindow(nsIWindowMediatorListener *aElement, void* aData);
static bool notifyCloseWindow(nsIWindowMediatorListener *aElement, void* aData);
static bool notifyWindowTitleChange(nsIWindowMediatorListener *aElement, void* aData);
// for notifyWindowTitleChange
struct WindowTitleData {
@ -91,10 +91,8 @@ NS_IMETHODIMP nsWindowMediator::RegisterWindow(nsIXULWindow* inWindow)
if (!windowInfo)
return NS_ERROR_OUT_OF_MEMORY;
if (mListeners) {
WindowTitleData winData = { inWindow, nullptr };
mListeners->EnumerateForwards(notifyOpenWindow, (void*)&winData);
}
WindowTitleData winData = { inWindow, nullptr };
mListeners.EnumerateForwards(notifyOpenWindow, &winData);
MutexAutoLock lock(mListLock);
if (mOldestWindow)
@ -126,10 +124,8 @@ nsWindowMediator::UnregisterWindow(nsWindowInfo *inInfo)
index++;
}
if (mListeners) {
WindowTitleData winData = { inInfo->mWindow.get(), nullptr };
mListeners->EnumerateForwards(notifyCloseWindow, (void*)&winData);
}
WindowTitleData winData = { inInfo->mWindow.get(), nullptr };
mListeners.EnumerateForwards(notifyCloseWindow, &winData);
// Remove from the lists and free up
if (inInfo == mOldestWindow)
@ -344,9 +340,9 @@ nsWindowMediator::UpdateWindowTitle(nsIXULWindow* inWindow,
{
NS_ENSURE_STATE(mReady);
MutexAutoLock lock(mListLock);
if (mListeners && GetInfoFor(inWindow)) {
if (GetInfoFor(inWindow)) {
WindowTitleData winData = { inWindow, inTitle };
mListeners->EnumerateForwards(notifyWindowTitleChange, (void*)&winData);
mListeners.EnumerateForwards(notifyWindowTitleChange, &winData);
}
return NS_OK;
@ -727,13 +723,7 @@ nsWindowMediator::AddListener(nsIWindowMediatorListener* aListener)
{
NS_ENSURE_ARG_POINTER(aListener);
nsresult rv;
if (!mListeners) {
rv = NS_NewISupportsArray(getter_AddRefs(mListeners));
if (NS_FAILED(rv)) return rv;
}
mListeners->AppendElement(aListener);
mListeners.AppendObject(aListener);
return NS_OK;
}
@ -743,10 +733,7 @@ nsWindowMediator::RemoveListener(nsIWindowMediatorListener* aListener)
{
NS_ENSURE_ARG_POINTER(aListener);
if (!mListeners)
return NS_OK;
mListeners->RemoveElement(aListener);
mListeners.RemoveObject(aListener);
return NS_OK;
}
@ -776,34 +763,28 @@ nsWindowMediator::Observe(nsISupports* aSubject,
}
bool
notifyOpenWindow(nsISupports *aElement, void* aData)
notifyOpenWindow(nsIWindowMediatorListener *aListener, void* aData)
{
nsIWindowMediatorListener* listener =
reinterpret_cast<nsIWindowMediatorListener*>(aElement);
WindowTitleData* winData = static_cast<WindowTitleData*>(aData);
listener->OnOpenWindow(winData->mWindow);
aListener->OnOpenWindow(winData->mWindow);
return true;
}
bool
notifyCloseWindow(nsISupports *aElement, void* aData)
notifyCloseWindow(nsIWindowMediatorListener *aListener, void* aData)
{
nsIWindowMediatorListener* listener =
reinterpret_cast<nsIWindowMediatorListener*>(aElement);
WindowTitleData* winData = static_cast<WindowTitleData*>(aData);
listener->OnCloseWindow(winData->mWindow);
aListener->OnCloseWindow(winData->mWindow);
return true;
}
bool
notifyWindowTitleChange(nsISupports *aElement, void* aData)
notifyWindowTitleChange(nsIWindowMediatorListener *aListener, void* aData)
{
nsIWindowMediatorListener* listener =
reinterpret_cast<nsIWindowMediatorListener*>(aElement);
WindowTitleData* titleData = reinterpret_cast<WindowTitleData*>(aData);
listener->OnWindowTitleChange(titleData->mWindow, titleData->mTitle);
aListener->OnWindowTitleChange(titleData->mWindow, titleData->mTitle);
return true;
}

View File

@ -9,12 +9,12 @@
#include "mozilla/Mutex.h"
#include "nsCOMPtr.h"
#include "nsIWindowMediator.h"
#include "nsISupportsArray.h"
#include "nsIObserver.h"
#include "nsTArray.h"
#include "nsXPIDLString.h"
#include "nsWeakReference.h"
#include "nsCRT.h"
#include "nsCOMArray.h"
class nsAppShellWindowEnumerator;
class nsASXULWindowEarlyToLateEnumerator;
@ -23,6 +23,7 @@ class nsASDOMWindowFrontToBackEnumerator;
class nsASXULWindowFrontToBackEnumerator;
class nsASDOMWindowBackToFrontEnumerator;
class nsASXULWindowBackToFrontEnumerator;
class nsIWindowMediatorListener;
struct nsWindowInfo;
struct PRLock;
@ -68,7 +69,7 @@ private:
bool mReady;
mozilla::Mutex mListLock;
nsCOMPtr<nsISupportsArray> mListeners;
nsCOMArray<nsIWindowMediatorListener> mListeners;
};
#endif