Bug 578272: Remove Algol-like display optimization. (r=brendan)
[mozilla-central.git] / docshell / base / nsDocShellEnumerator.cpp
blobbd30defbc543e641df1098a3096510930e5a0a83
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is the Mozilla browser.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications, Inc.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 #include "nsDocShellEnumerator.h"
42 #include "nsIDocShellTreeItem.h"
43 #include "nsIDocShellTreeNode.h"
45 nsDocShellEnumerator::nsDocShellEnumerator(PRInt32 inEnumerationDirection)
46 : mRootItem(nsnull)
47 , mCurIndex(0)
48 , mDocShellType(nsIDocShellTreeItem::typeAll)
49 , mArrayValid(PR_FALSE)
50 , mEnumerationDirection(inEnumerationDirection)
54 nsDocShellEnumerator::~nsDocShellEnumerator()
58 NS_IMPL_ISUPPORTS1(nsDocShellEnumerator, nsISimpleEnumerator)
61 /* nsISupports getNext (); */
62 NS_IMETHODIMP nsDocShellEnumerator::GetNext(nsISupports **outCurItem)
64 NS_ENSURE_ARG_POINTER(outCurItem);
65 *outCurItem = nsnull;
67 nsresult rv = EnsureDocShellArray();
68 if (NS_FAILED(rv)) return rv;
70 if (mCurIndex >= mItemArray.Length()) {
71 return NS_ERROR_FAILURE;
74 // post-increment is important here
75 return CallQueryInterface(mItemArray[mCurIndex++], outCurItem);
78 /* boolean hasMoreElements (); */
79 NS_IMETHODIMP nsDocShellEnumerator::HasMoreElements(PRBool *outHasMore)
81 NS_ENSURE_ARG_POINTER(outHasMore);
82 *outHasMore = PR_FALSE;
84 nsresult rv = EnsureDocShellArray();
85 if (NS_FAILED(rv)) return rv;
87 *outHasMore = (mCurIndex < mItemArray.Length());
88 return NS_OK;
91 nsresult nsDocShellEnumerator::GetEnumerationRootItem(nsIDocShellTreeItem * *aEnumerationRootItem)
93 NS_ENSURE_ARG_POINTER(aEnumerationRootItem);
94 *aEnumerationRootItem = mRootItem;
95 NS_IF_ADDREF(*aEnumerationRootItem);
96 return NS_OK;
99 nsresult nsDocShellEnumerator::SetEnumerationRootItem(nsIDocShellTreeItem * aEnumerationRootItem)
101 mRootItem = aEnumerationRootItem;
102 ClearState();
103 return NS_OK;
106 nsresult nsDocShellEnumerator::GetEnumDocShellType(PRInt32 *aEnumerationItemType)
108 NS_ENSURE_ARG_POINTER(aEnumerationItemType);
109 *aEnumerationItemType = mDocShellType;
110 return NS_OK;
113 nsresult nsDocShellEnumerator::SetEnumDocShellType(PRInt32 aEnumerationItemType)
115 mDocShellType = aEnumerationItemType;
116 ClearState();
117 return NS_OK;
120 nsresult nsDocShellEnumerator::First()
122 mCurIndex = 0;
123 return EnsureDocShellArray();
126 nsresult nsDocShellEnumerator::EnsureDocShellArray()
128 if (!mArrayValid)
130 mArrayValid = PR_TRUE;
131 return BuildDocShellArray(mItemArray);
134 return NS_OK;
137 nsresult nsDocShellEnumerator::ClearState()
139 mItemArray.Clear();
140 mArrayValid = PR_FALSE;
141 mCurIndex = 0;
142 return NS_OK;
145 nsresult nsDocShellEnumerator::BuildDocShellArray(nsTArray<nsIDocShellTreeItem*>& inItemArray)
147 NS_ENSURE_TRUE(mRootItem, NS_ERROR_NOT_INITIALIZED);
148 inItemArray.Clear();
149 return BuildArrayRecursive(mRootItem, inItemArray);
152 nsresult nsDocShellForwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsIDocShellTreeItem*>& inItemArray)
154 nsresult rv;
155 nsCOMPtr<nsIDocShellTreeNode> itemAsNode = do_QueryInterface(inItem, &rv);
156 if (NS_FAILED(rv)) return rv;
158 PRInt32 itemType;
159 // add this item to the array
160 if ((mDocShellType == nsIDocShellTreeItem::typeAll) ||
161 (NS_SUCCEEDED(inItem->GetItemType(&itemType)) && (itemType == mDocShellType)))
163 if (!inItemArray.AppendElement(inItem))
164 return NS_ERROR_OUT_OF_MEMORY;
167 PRInt32 numChildren;
168 rv = itemAsNode->GetChildCount(&numChildren);
169 if (NS_FAILED(rv)) return rv;
171 for (PRInt32 i = 0; i < numChildren; ++i)
173 nsCOMPtr<nsIDocShellTreeItem> curChild;
174 rv = itemAsNode->GetChildAt(i, getter_AddRefs(curChild));
175 if (NS_FAILED(rv)) return rv;
177 rv = BuildArrayRecursive(curChild, inItemArray);
178 if (NS_FAILED(rv)) return rv;
181 return NS_OK;
185 nsresult nsDocShellBackwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsIDocShellTreeItem*>& inItemArray)
187 nsresult rv;
188 nsCOMPtr<nsIDocShellTreeNode> itemAsNode = do_QueryInterface(inItem, &rv);
189 if (NS_FAILED(rv)) return rv;
191 PRInt32 numChildren;
192 rv = itemAsNode->GetChildCount(&numChildren);
193 if (NS_FAILED(rv)) return rv;
195 for (PRInt32 i = numChildren - 1; i >= 0; --i)
197 nsCOMPtr<nsIDocShellTreeItem> curChild;
198 rv = itemAsNode->GetChildAt(i, getter_AddRefs(curChild));
199 if (NS_FAILED(rv)) return rv;
201 rv = BuildArrayRecursive(curChild, inItemArray);
202 if (NS_FAILED(rv)) return rv;
205 PRInt32 itemType;
206 // add this item to the array
207 if ((mDocShellType == nsIDocShellTreeItem::typeAll) ||
208 (NS_SUCCEEDED(inItem->GetItemType(&itemType)) && (itemType == mDocShellType)))
210 if (!inItemArray.AppendElement(inItem))
211 return NS_ERROR_OUT_OF_MEMORY;
215 return NS_OK;