Bug 624187 - r=smaug a=blocking-beta
authorMounir Lamouri <mounir.lamouri@gmail.com>
Fri, 14 Jan 2011 16:34:50 +0100
changeset 60517 e23b4fe4e09ce67c38d940af2dfc77f4aba16ae8
parent 60516 b023a8c82f97b1fcd0f0880bfca19c1f0effa0d8
child 60518 2e68d97b6932d46477ba3b81eec490f1c9a22d05
push idunknown
push userunknown
push dateunknown
reviewerssmaug, blocking-beta
bugs624187
milestone2.0b10pre
Bug 624187 - r=smaug a=blocking-beta
docshell/base/nsDocShellEnumerator.cpp
docshell/base/nsDocShellEnumerator.h
--- a/docshell/base/nsDocShellEnumerator.cpp
+++ b/docshell/base/nsDocShellEnumerator.cpp
@@ -65,19 +65,21 @@ NS_IMETHODIMP nsDocShellEnumerator::GetN
   *outCurItem = nsnull;
   
   nsresult rv = EnsureDocShellArray();
   if (NS_FAILED(rv)) return rv;
   
   if (mCurIndex >= mItemArray.Length()) {
     return NS_ERROR_FAILURE;
   }
-  
+
   // post-increment is important here
-  return CallQueryInterface(mItemArray[mCurIndex++], outCurItem);
+  nsCOMPtr<nsISupports> item = do_QueryReferent(mItemArray[mCurIndex++], &rv);
+  item.forget(outCurItem);
+  return rv;
 }
 
 /* boolean hasMoreElements (); */
 NS_IMETHODIMP nsDocShellEnumerator::HasMoreElements(PRBool *outHasMore)
 {
   NS_ENSURE_ARG_POINTER(outHasMore);
   *outHasMore = PR_FALSE;
 
@@ -86,24 +88,24 @@ NS_IMETHODIMP nsDocShellEnumerator::HasM
 
   *outHasMore = (mCurIndex < mItemArray.Length());
   return NS_OK;
 }
 
 nsresult nsDocShellEnumerator::GetEnumerationRootItem(nsIDocShellTreeItem * *aEnumerationRootItem)
 {
   NS_ENSURE_ARG_POINTER(aEnumerationRootItem);
-  *aEnumerationRootItem = mRootItem;
-  NS_IF_ADDREF(*aEnumerationRootItem);
+  nsCOMPtr<nsIDocShellTreeItem> item = do_QueryReferent(mRootItem);
+  item.forget(aEnumerationRootItem);
   return NS_OK;
 }
 
 nsresult nsDocShellEnumerator::SetEnumerationRootItem(nsIDocShellTreeItem * aEnumerationRootItem)
 {
-  mRootItem = aEnumerationRootItem;
+  mRootItem = do_GetWeakReference(aEnumerationRootItem);
   ClearState();
   return NS_OK;
 }
 
 nsresult nsDocShellEnumerator::GetEnumDocShellType(PRInt32 *aEnumerationItemType)
 {
   NS_ENSURE_ARG_POINTER(aEnumerationItemType);
   *aEnumerationItemType = mDocShellType;
@@ -137,35 +139,36 @@ nsresult nsDocShellEnumerator::EnsureDoc
 nsresult nsDocShellEnumerator::ClearState()
 {
   mItemArray.Clear();
   mArrayValid = PR_FALSE;
   mCurIndex = 0;
   return NS_OK;
 }
 
-nsresult nsDocShellEnumerator::BuildDocShellArray(nsTArray<nsIDocShellTreeItem*>& inItemArray)
+nsresult nsDocShellEnumerator::BuildDocShellArray(nsTArray<nsWeakPtr>& inItemArray)
 {
   NS_ENSURE_TRUE(mRootItem, NS_ERROR_NOT_INITIALIZED);
   inItemArray.Clear();
-  return BuildArrayRecursive(mRootItem, inItemArray);
+  nsCOMPtr<nsIDocShellTreeItem> item = do_QueryReferent(mRootItem);
+  return BuildArrayRecursive(item, inItemArray);
 }
 
-nsresult nsDocShellForwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsIDocShellTreeItem*>& inItemArray)
+nsresult nsDocShellForwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray)
 {
   nsresult rv;
   nsCOMPtr<nsIDocShellTreeNode> itemAsNode = do_QueryInterface(inItem, &rv);
   if (NS_FAILED(rv)) return rv;
 
   PRInt32   itemType;
   // add this item to the array
   if ((mDocShellType == nsIDocShellTreeItem::typeAll) ||
       (NS_SUCCEEDED(inItem->GetItemType(&itemType)) && (itemType == mDocShellType)))
   {
-    if (!inItemArray.AppendElement(inItem))
+    if (!inItemArray.AppendElement(do_GetWeakReference(inItem)))
       return NS_ERROR_OUT_OF_MEMORY;
   }
 
   PRInt32   numChildren;
   rv = itemAsNode->GetChildCount(&numChildren);
   if (NS_FAILED(rv)) return rv;
   
   for (PRInt32 i = 0; i < numChildren; ++i)
@@ -177,17 +180,17 @@ nsresult nsDocShellForwardsEnumerator::B
     rv = BuildArrayRecursive(curChild, inItemArray);
     if (NS_FAILED(rv)) return rv;
   }
 
   return NS_OK;
 }
 
 
-nsresult nsDocShellBackwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsIDocShellTreeItem*>& inItemArray)
+nsresult nsDocShellBackwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray)
 {
   nsresult rv;
   nsCOMPtr<nsIDocShellTreeNode> itemAsNode = do_QueryInterface(inItem, &rv);
   if (NS_FAILED(rv)) return rv;
 
   PRInt32   numChildren;
   rv = itemAsNode->GetChildCount(&numChildren);
   if (NS_FAILED(rv)) return rv;
@@ -202,17 +205,17 @@ nsresult nsDocShellBackwardsEnumerator::
     if (NS_FAILED(rv)) return rv;
   }
 
   PRInt32   itemType;
   // add this item to the array
   if ((mDocShellType == nsIDocShellTreeItem::typeAll) ||
       (NS_SUCCEEDED(inItem->GetItemType(&itemType)) && (itemType == mDocShellType)))
   {
-    if (!inItemArray.AppendElement(inItem))
+    if (!inItemArray.AppendElement(do_GetWeakReference(inItem)))
       return NS_ERROR_OUT_OF_MEMORY;
   }
 
 
   return NS_OK;
 }
 
 
--- a/docshell/base/nsDocShellEnumerator.h
+++ b/docshell/base/nsDocShellEnumerator.h
@@ -38,16 +38,18 @@
  * ***** END LICENSE BLOCK ***** */
 
 
 
 #include "nsIEnumerator.h"
 
 #include "nsCOMPtr.h"
 #include "nsTArray.h"
+#include "nsIWeakReference.h"
+#include "nsIWeakReferenceUtils.h"
 
 class nsIDocShellTreeItem;
 
 
 /*
 // {13cbc281-35ae-11d5-be5b-bde0edece43c}
 #define NS_DOCSHELL_FORWARDS_ENUMERATOR_CID  \
 { 0x13cbc281, 0x35ae, 0x11d5, { 0xbe, 0x5b, 0xbd, 0xe0, 0xed, 0xec, 0xe4, 0x3c } }
@@ -93,24 +95,24 @@ public:
     
   nsresult                    First();
 
 protected:
 
   nsresult                    EnsureDocShellArray();
   nsresult                    ClearState();
   
-  nsresult                    BuildDocShellArray(nsTArray<nsIDocShellTreeItem*>& inItemArray);
-  virtual nsresult            BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsIDocShellTreeItem*>& inItemArray) = 0;
+  nsresult                    BuildDocShellArray(nsTArray<nsWeakPtr>& inItemArray);
+  virtual nsresult            BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray) = 0;
     
 protected:
 
-  nsIDocShellTreeItem*        mRootItem;      // weak ref!
+  nsWeakPtr                   mRootItem;      // weak ref!
   
-  nsTArray<nsIDocShellTreeItem*> mItemArray; // flattened list of items with matching type
+  nsTArray<nsWeakPtr>         mItemArray;     // flattened list of items with matching type
   PRUint32                    mCurIndex;
   
   PRInt32                     mDocShellType;  // only want shells of this type
   PRPackedBool                mArrayValid;    // is mItemArray up to date?
 
   const PRInt8                mEnumerationDirection;
 };
 
@@ -121,25 +123,25 @@ public:
 
                               nsDocShellForwardsEnumerator()
                               : nsDocShellEnumerator(enumerateForwards)
                               {                              
                               }
 
 protected:
 
-  virtual nsresult            BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsIDocShellTreeItem*>& inItemArray);
+  virtual nsresult            BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray);
 
 };
 
 class nsDocShellBackwardsEnumerator : public nsDocShellEnumerator
 {
 public:
 
                               nsDocShellBackwardsEnumerator()
                               : nsDocShellEnumerator(enumerateBackwards)
                               {                              
                               }
 protected:
 
-  virtual nsresult            BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsIDocShellTreeItem*>& inItemArray);
+  virtual nsresult            BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray);
 
 };