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