Bug 792169 - introduce better, safer constructors for nsArray; r=bsmedberg

This commit is contained in:
Nathan Froyd 2012-09-21 12:12:32 -04:00
parent 91f17d9b55
commit 54c3f613a8
5 changed files with 33 additions and 22 deletions

View File

@ -18,7 +18,7 @@
COMPONENT(PERSISTENTPROPERTIES, nsPersistentProperties::Create)
COMPONENT(SUPPORTSARRAY, nsSupportsArray::Create)
COMPONENT(ARRAY, nsArrayConstructor)
COMPONENT(ARRAY, nsArray::XPCOMConstructor)
COMPONENT(CONSOLESERVICE, nsConsoleServiceConstructor)
COMPONENT(EXCEPTIONSERVICE, nsExceptionServiceConstructor)
COMPONENT(ATOMSERVICE, nsAtomServiceConstructor)

View File

@ -59,6 +59,7 @@ EXPORTS_mozilla = \
$(NULL)
EXPORTS = \
nsArray.h \
nsAtomService.h \
nsCheapSets.h \
nsCppSharedAllocator.h \

View File

@ -195,14 +195,18 @@ FindElementCallback(void *aElement, void* aClosure)
}
nsresult
nsArrayConstructor(nsISupports *aOuter, const nsIID& aIID, void **aResult)
nsArray::XPCOMConstructor(nsISupports *aOuter, const nsIID& aIID, void **aResult)
{
if (aOuter)
return NS_ERROR_NO_AGGREGATION;
nsCOMPtr<nsIArray> inst = NS_IsMainThread() ? new nsArrayCC : new nsArray;
if (!inst)
return NS_ERROR_OUT_OF_MEMORY;
nsCOMPtr<nsIMutableArray> inst = Create();
return inst->QueryInterface(aIID, aResult);
}
already_AddRefed<nsIMutableArray>
nsArray::Create()
{
nsCOMPtr<nsIMutableArray> inst = NS_IsMainThread() ? new nsArrayCC : new nsArray;
return inst.forget();
}

View File

@ -20,23 +20,27 @@
{ 0x35c66fd1, 0x95e9, 0x4e0a, \
{ 0x80, 0xc5, 0xc3, 0xbd, 0x2b, 0x37, 0x54, 0x81 } }
// adapter class to map nsIArray->nsCOMArray
// do NOT declare this as a stack or member variable, use
// nsCOMArray instead!
// if you need to convert a nsCOMArray->nsIArray, see NS_NewArray above
class nsArray : public nsIMutableArray
{
public:
nsArray() { }
nsArray(const nsCOMArray_base& aBaseArray) : mArray(aBaseArray)
{ }
NS_DECL_ISUPPORTS
NS_DECL_NSIARRAY
NS_DECL_NSIMUTABLEARRAY
/* Both of these factory functions create a cycle-collectable array
on the main thread and a non-cycle-collectable array on other
threads. */
static already_AddRefed<nsIMutableArray> Create();
/* Only for the benefit of the XPCOM module system, use Create()
instead. */
static nsresult XPCOMConstructor(nsISupports* aOuter, const nsIID& aIID,
void** aResult);
protected:
nsArray() { }
nsArray(const nsArray& other);
nsArray(const nsCOMArray_base& aBaseArray) : mArray(aBaseArray)
{ }
virtual ~nsArray(); // nsArrayCC inherits from this
nsCOMArray_base mArray;
@ -44,15 +48,17 @@ protected:
class nsArrayCC MOZ_FINAL : public nsArray
{
friend class nsArray;
public:
nsArrayCC() : nsArray() { }
nsArrayCC(const nsCOMArray_base& aBaseArray) : nsArray(aBaseArray)
{ }
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(nsArrayCC)
private:
nsArrayCC() : nsArray() { }
nsArrayCC(const nsArrayCC& other);
nsArrayCC(const nsCOMArray_base& aBaseArray) : nsArray(aBaseArray)
{ }
};
nsresult nsArrayConstructor(nsISupports *aOuter, const nsIID& aIID, void **aResult);
#endif

View File

@ -159,7 +159,7 @@ PropertyHashToArrayFunc (const nsAString &aKey,
NS_IMETHODIMP
nsHashPropertyBag::GetEnumerator(nsISimpleEnumerator* *_retval)
{
nsCOMPtr<nsIMutableArray> propertyArray = new nsArray();
nsCOMPtr<nsIMutableArray> propertyArray = nsArray::Create();
if (!propertyArray)
return NS_ERROR_OUT_OF_MEMORY;