2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
#include "nsDocShellEnumerator.h"
|
|
|
|
|
2007-05-06 14:06:28 -07:00
|
|
|
#include "nsIDocShellTreeItem.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDocShellEnumerator::nsDocShellEnumerator(int32_t inEnumerationDirection)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mRootItem(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mCurIndex(0)
|
|
|
|
, mDocShellType(nsIDocShellTreeItem::typeAll)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mArrayValid(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mEnumerationDirection(inEnumerationDirection)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDocShellEnumerator::~nsDocShellEnumerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsDocShellEnumerator, nsISimpleEnumerator)
|
|
|
|
|
|
|
|
|
|
|
|
/* nsISupports getNext (); */
|
|
|
|
NS_IMETHODIMP nsDocShellEnumerator::GetNext(nsISupports **outCurItem)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(outCurItem);
|
2012-07-30 07:20:58 -07:00
|
|
|
*outCurItem = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = EnsureDocShellArray();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2009-04-03 14:21:40 -07:00
|
|
|
if (mCurIndex >= mItemArray.Length()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-04-03 14:21:40 -07:00
|
|
|
}
|
2011-01-14 07:34:50 -08:00
|
|
|
|
2009-04-03 14:21:40 -07:00
|
|
|
// post-increment is important here
|
2011-01-14 07:34:50 -08:00
|
|
|
nsCOMPtr<nsISupports> item = do_QueryReferent(mItemArray[mCurIndex++], &rv);
|
|
|
|
item.forget(outCurItem);
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* boolean hasMoreElements (); */
|
2011-09-28 23:19:26 -07:00
|
|
|
NS_IMETHODIMP nsDocShellEnumerator::HasMoreElements(bool *outHasMore)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(outHasMore);
|
2011-10-17 07:59:28 -07:00
|
|
|
*outHasMore = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = EnsureDocShellArray();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2009-04-03 14:21:40 -07:00
|
|
|
*outHasMore = (mCurIndex < mItemArray.Length());
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDocShellEnumerator::GetEnumerationRootItem(nsIDocShellTreeItem * *aEnumerationRootItem)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aEnumerationRootItem);
|
2011-01-14 07:34:50 -08:00
|
|
|
nsCOMPtr<nsIDocShellTreeItem> item = do_QueryReferent(mRootItem);
|
|
|
|
item.forget(aEnumerationRootItem);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDocShellEnumerator::SetEnumerationRootItem(nsIDocShellTreeItem * aEnumerationRootItem)
|
|
|
|
{
|
2011-01-14 07:34:50 -08:00
|
|
|
mRootItem = do_GetWeakReference(aEnumerationRootItem);
|
2007-03-22 10:30:00 -07:00
|
|
|
ClearState();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult nsDocShellEnumerator::GetEnumDocShellType(int32_t *aEnumerationItemType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aEnumerationItemType);
|
|
|
|
*aEnumerationItemType = mDocShellType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult nsDocShellEnumerator::SetEnumDocShellType(int32_t aEnumerationItemType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mDocShellType = aEnumerationItemType;
|
|
|
|
ClearState();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDocShellEnumerator::First()
|
|
|
|
{
|
|
|
|
mCurIndex = 0;
|
|
|
|
return EnsureDocShellArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDocShellEnumerator::EnsureDocShellArray()
|
|
|
|
{
|
2009-04-03 14:21:40 -07:00
|
|
|
if (!mArrayValid)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mArrayValid = true;
|
2009-04-03 14:21:40 -07:00
|
|
|
return BuildDocShellArray(mItemArray);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDocShellEnumerator::ClearState()
|
|
|
|
{
|
2009-04-03 14:21:40 -07:00
|
|
|
mItemArray.Clear();
|
2011-10-17 07:59:28 -07:00
|
|
|
mArrayValid = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
mCurIndex = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-01-14 07:34:50 -08:00
|
|
|
nsresult nsDocShellEnumerator::BuildDocShellArray(nsTArray<nsWeakPtr>& inItemArray)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mRootItem, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
inItemArray.Clear();
|
2011-01-14 07:34:50 -08:00
|
|
|
nsCOMPtr<nsIDocShellTreeItem> item = do_QueryReferent(mRootItem);
|
|
|
|
return BuildArrayRecursive(item, inItemArray);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-01-14 07:34:50 -08:00
|
|
|
nsresult nsDocShellForwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t itemType;
|
2007-03-22 10:30:00 -07:00
|
|
|
// add this item to the array
|
|
|
|
if ((mDocShellType == nsIDocShellTreeItem::typeAll) ||
|
|
|
|
(NS_SUCCEEDED(inItem->GetItemType(&itemType)) && (itemType == mDocShellType)))
|
|
|
|
{
|
2011-01-14 07:34:50 -08:00
|
|
|
if (!inItemArray.AppendElement(do_GetWeakReference(inItem)))
|
2009-04-03 14:21:40 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t numChildren;
|
2014-01-06 14:34:15 -08:00
|
|
|
rv = inItem->GetChildCount(&numChildren);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < numChildren; ++i)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> curChild;
|
2014-01-06 14:34:15 -08:00
|
|
|
rv = inItem->GetChildAt(i, getter_AddRefs(curChild));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = BuildArrayRecursive(curChild, inItemArray);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-14 07:34:50 -08:00
|
|
|
nsresult nsDocShellBackwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t numChildren;
|
2014-01-06 14:34:15 -08:00
|
|
|
rv = inItem->GetChildCount(&numChildren);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = numChildren - 1; i >= 0; --i)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> curChild;
|
2014-01-06 14:34:15 -08:00
|
|
|
rv = inItem->GetChildAt(i, getter_AddRefs(curChild));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = BuildArrayRecursive(curChild, inItemArray);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t itemType;
|
2007-03-22 10:30:00 -07:00
|
|
|
// add this item to the array
|
|
|
|
if ((mDocShellType == nsIDocShellTreeItem::typeAll) ||
|
|
|
|
(NS_SUCCEEDED(inItem->GetItemType(&itemType)) && (itemType == mDocShellType)))
|
|
|
|
{
|
2011-01-14 07:34:50 -08:00
|
|
|
if (!inItemArray.AppendElement(do_GetWeakReference(inItem)))
|
2009-04-03 14:21:40 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|