2015-02-13 22:36:47 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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 04:32:32 +04:00
|
|
|
|
|
|
|
#include "nsDocShellEnumerator.h"
|
|
|
|
|
2007-05-07 01:06:28 +04:00
|
|
|
#include "nsIDocShellTreeItem.h"
|
2001-04-27 04:32:32 +04:00
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsDocShellEnumerator::nsDocShellEnumerator(int32_t aEnumerationDirection)
|
|
|
|
: mRootItem(nullptr)
|
|
|
|
, mCurIndex(0)
|
|
|
|
, mDocShellType(nsIDocShellTreeItem::typeAll)
|
|
|
|
, mArrayValid(false)
|
|
|
|
, mEnumerationDirection(aEnumerationDirection)
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDocShellEnumerator::~nsDocShellEnumerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsDocShellEnumerator, nsISimpleEnumerator)
|
2001-04-27 04:32:32 +04:00
|
|
|
|
|
|
|
/* nsISupports getNext (); */
|
2015-02-13 22:36:37 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocShellEnumerator::GetNext(nsISupports** aResult)
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
2015-02-13 22:36:37 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
*aResult = nullptr;
|
|
|
|
|
2001-04-27 04:32:32 +04:00
|
|
|
nsresult rv = EnsureDocShellArray();
|
2015-02-13 22:36:37 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-04-04 01:21:40 +04:00
|
|
|
if (mCurIndex >= mItemArray.Length()) {
|
2001-04-27 04:32:32 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2009-04-04 01:21:40 +04:00
|
|
|
}
|
2011-01-14 18:34:50 +03:00
|
|
|
|
2009-04-04 01:21:40 +04:00
|
|
|
// post-increment is important here
|
2011-01-14 18:34:50 +03:00
|
|
|
nsCOMPtr<nsISupports> item = do_QueryReferent(mItemArray[mCurIndex++], &rv);
|
2015-02-13 22:36:37 +03:00
|
|
|
item.forget(aResult);
|
2011-01-14 18:34:50 +03:00
|
|
|
return rv;
|
2001-04-27 04:32:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* boolean hasMoreElements (); */
|
2015-02-13 22:36:37 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDocShellEnumerator::HasMoreElements(bool* aResult)
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
2015-02-13 22:36:37 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
*aResult = false;
|
2001-04-27 04:32:32 +04:00
|
|
|
|
|
|
|
nsresult rv = EnsureDocShellArray();
|
2015-02-13 22:36:37 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-04-27 04:32:32 +04:00
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
*aResult = (mCurIndex < mItemArray.Length());
|
2001-04-27 04:32:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsresult
|
|
|
|
nsDocShellEnumerator::GetEnumerationRootItem(
|
|
|
|
nsIDocShellTreeItem** aEnumerationRootItem)
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aEnumerationRootItem);
|
2011-01-14 18:34:50 +03:00
|
|
|
nsCOMPtr<nsIDocShellTreeItem> item = do_QueryReferent(mRootItem);
|
|
|
|
item.forget(aEnumerationRootItem);
|
2001-04-27 04:32:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsresult
|
|
|
|
nsDocShellEnumerator::SetEnumerationRootItem(
|
|
|
|
nsIDocShellTreeItem* aEnumerationRootItem)
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
2011-01-14 18:34:50 +03:00
|
|
|
mRootItem = do_GetWeakReference(aEnumerationRootItem);
|
2001-04-27 04:32:32 +04:00
|
|
|
ClearState();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsresult
|
|
|
|
nsDocShellEnumerator::GetEnumDocShellType(int32_t* aEnumerationItemType)
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aEnumerationItemType);
|
|
|
|
*aEnumerationItemType = mDocShellType;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsresult
|
|
|
|
nsDocShellEnumerator::SetEnumDocShellType(int32_t aEnumerationItemType)
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
|
|
|
mDocShellType = aEnumerationItemType;
|
|
|
|
ClearState();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsresult
|
|
|
|
nsDocShellEnumerator::First()
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
|
|
|
mCurIndex = 0;
|
|
|
|
return EnsureDocShellArray();
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsresult
|
|
|
|
nsDocShellEnumerator::EnsureDocShellArray()
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
2015-02-13 22:36:37 +03:00
|
|
|
if (!mArrayValid) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mArrayValid = true;
|
2009-04-04 01:21:40 +04:00
|
|
|
return BuildDocShellArray(mItemArray);
|
2001-04-27 04:32:32 +04:00
|
|
|
}
|
2015-02-13 22:36:37 +03:00
|
|
|
|
2001-04-27 04:32:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsresult
|
|
|
|
nsDocShellEnumerator::ClearState()
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
2009-04-04 01:21:40 +04:00
|
|
|
mItemArray.Clear();
|
2011-10-17 18:59:28 +04:00
|
|
|
mArrayValid = false;
|
2001-04-27 04:32:32 +04:00
|
|
|
mCurIndex = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsresult
|
|
|
|
nsDocShellEnumerator::BuildDocShellArray(nsTArray<nsWeakPtr>& aItemArray)
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mRootItem, NS_ERROR_NOT_INITIALIZED);
|
2015-02-13 22:36:37 +03:00
|
|
|
aItemArray.Clear();
|
2011-01-14 18:34:50 +03:00
|
|
|
nsCOMPtr<nsIDocShellTreeItem> item = do_QueryReferent(mRootItem);
|
2015-02-13 22:36:37 +03:00
|
|
|
return BuildArrayRecursive(item, aItemArray);
|
2001-04-27 04:32:32 +04:00
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsresult
|
|
|
|
nsDocShellForwardsEnumerator::BuildArrayRecursive(
|
|
|
|
nsIDocShellTreeItem* aItem,
|
|
|
|
nsTArray<nsWeakPtr>& aItemArray)
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// add this item to the array
|
2014-01-20 11:58:26 +04:00
|
|
|
if (mDocShellType == nsIDocShellTreeItem::typeAll ||
|
2015-02-13 22:36:37 +03:00
|
|
|
aItem->ItemType() == mDocShellType) {
|
|
|
|
if (!aItemArray.AppendElement(do_GetWeakReference(aItem))) {
|
2009-04-04 01:21:40 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2015-02-13 22:36:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t numChildren;
|
|
|
|
rv = aItem->GetChildCount(&numChildren);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2001-04-27 04:32:32 +04:00
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
for (int32_t i = 0; i < numChildren; ++i) {
|
2001-04-27 04:32:32 +04:00
|
|
|
nsCOMPtr<nsIDocShellTreeItem> curChild;
|
2015-02-13 22:36:37 +03:00
|
|
|
rv = aItem->GetChildAt(i, getter_AddRefs(curChild));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = BuildArrayRecursive(curChild, aItemArray);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-04-27 04:32:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsresult
|
|
|
|
nsDocShellBackwardsEnumerator::BuildArrayRecursive(
|
|
|
|
nsIDocShellTreeItem* aItem,
|
|
|
|
nsTArray<nsWeakPtr>& aItemArray)
|
2001-04-27 04:32:32 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
int32_t numChildren;
|
|
|
|
rv = aItem->GetChildCount(&numChildren);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = numChildren - 1; i >= 0; --i) {
|
2001-04-27 04:32:32 +04:00
|
|
|
nsCOMPtr<nsIDocShellTreeItem> curChild;
|
2015-02-13 22:36:37 +03:00
|
|
|
rv = aItem->GetChildAt(i, getter_AddRefs(curChild));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = BuildArrayRecursive(curChild, aItemArray);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2001-04-27 04:32:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// add this item to the array
|
2014-01-20 11:58:26 +04:00
|
|
|
if (mDocShellType == nsIDocShellTreeItem::typeAll ||
|
2015-02-13 22:36:37 +03:00
|
|
|
aItem->ItemType() == mDocShellType) {
|
|
|
|
if (!aItemArray.AppendElement(do_GetWeakReference(aItem))) {
|
2009-04-04 01:21:40 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2015-02-13 22:36:37 +03:00
|
|
|
}
|
2001-04-27 04:32:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|