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
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.
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
)
49 , mDocShellType(nsIDocShellTreeItem::typeAll
)
50 , mEnumerationDirection(inEnumerationDirection
)
54 nsDocShellEnumerator::~nsDocShellEnumerator()
59 NS_IMPL_ISUPPORTS1(nsDocShellEnumerator
, nsISimpleEnumerator
)
62 /* nsISupports getNext (); */
63 NS_IMETHODIMP
nsDocShellEnumerator::GetNext(nsISupports
**outCurItem
)
65 NS_ENSURE_ARG_POINTER(outCurItem
);
68 nsresult rv
= EnsureDocShellArray();
69 if (NS_FAILED(rv
)) return rv
;
71 if (mCurIndex
>= 0 && mCurIndex
< mItemArray
->Count())
73 nsIDocShellTreeItem
* thisItem
= reinterpret_cast<nsIDocShellTreeItem
*>(mItemArray
->ElementAt(mCurIndex
));
74 rv
= thisItem
->QueryInterface(NS_GET_IID(nsISupports
), (void **)outCurItem
);
75 if (NS_FAILED(rv
)) return rv
;
78 return NS_ERROR_FAILURE
;
85 /* boolean hasMoreElements (); */
86 NS_IMETHODIMP
nsDocShellEnumerator::HasMoreElements(PRBool
*outHasMore
)
88 NS_ENSURE_ARG_POINTER(outHasMore
);
89 *outHasMore
= PR_FALSE
;
91 nsresult rv
= EnsureDocShellArray();
92 if (NS_FAILED(rv
)) return rv
;
94 *outHasMore
= (mCurIndex
< mItemArray
->Count());
98 nsresult
nsDocShellEnumerator::GetEnumerationRootItem(nsIDocShellTreeItem
* *aEnumerationRootItem
)
100 NS_ENSURE_ARG_POINTER(aEnumerationRootItem
);
101 *aEnumerationRootItem
= mRootItem
;
102 NS_IF_ADDREF(*aEnumerationRootItem
);
106 nsresult
nsDocShellEnumerator::SetEnumerationRootItem(nsIDocShellTreeItem
* aEnumerationRootItem
)
108 mRootItem
= aEnumerationRootItem
;
113 nsresult
nsDocShellEnumerator::GetEnumDocShellType(PRInt32
*aEnumerationItemType
)
115 NS_ENSURE_ARG_POINTER(aEnumerationItemType
);
116 *aEnumerationItemType
= mDocShellType
;
120 nsresult
nsDocShellEnumerator::SetEnumDocShellType(PRInt32 aEnumerationItemType
)
122 mDocShellType
= aEnumerationItemType
;
127 nsresult
nsDocShellEnumerator::First()
130 return EnsureDocShellArray();
133 nsresult
nsDocShellEnumerator::EnsureDocShellArray()
137 mItemArray
= new nsVoidArray
;
138 if (!mItemArray
) return NS_ERROR_OUT_OF_MEMORY
;
140 return BuildDocShellArray(*mItemArray
);
146 nsresult
nsDocShellEnumerator::ClearState()
155 nsresult
nsDocShellEnumerator::BuildDocShellArray(nsVoidArray
& inItemArray
)
157 NS_ENSURE_TRUE(mRootItem
, NS_ERROR_NOT_INITIALIZED
);
159 return BuildArrayRecursive(mRootItem
, inItemArray
);
162 nsresult
nsDocShellForwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem
* inItem
, nsVoidArray
& inItemArray
)
165 nsCOMPtr
<nsIDocShellTreeNode
> itemAsNode
= do_QueryInterface(inItem
, &rv
);
166 if (NS_FAILED(rv
)) return rv
;
169 // add this item to the array
170 if ((mDocShellType
== nsIDocShellTreeItem::typeAll
) ||
171 (NS_SUCCEEDED(inItem
->GetItemType(&itemType
)) && (itemType
== mDocShellType
)))
173 rv
= inItemArray
.AppendElement((void *)inItem
);
174 if (NS_FAILED(rv
)) return rv
;
178 rv
= itemAsNode
->GetChildCount(&numChildren
);
179 if (NS_FAILED(rv
)) return rv
;
181 for (PRInt32 i
= 0; i
< numChildren
; ++i
)
183 nsCOMPtr
<nsIDocShellTreeItem
> curChild
;
184 rv
= itemAsNode
->GetChildAt(i
, getter_AddRefs(curChild
));
185 if (NS_FAILED(rv
)) return rv
;
187 rv
= BuildArrayRecursive(curChild
, inItemArray
);
188 if (NS_FAILED(rv
)) return rv
;
195 nsresult
nsDocShellBackwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem
* inItem
, nsVoidArray
& inItemArray
)
198 nsCOMPtr
<nsIDocShellTreeNode
> itemAsNode
= do_QueryInterface(inItem
, &rv
);
199 if (NS_FAILED(rv
)) return rv
;
202 rv
= itemAsNode
->GetChildCount(&numChildren
);
203 if (NS_FAILED(rv
)) return rv
;
205 for (PRInt32 i
= numChildren
- 1; i
>= 0; --i
)
207 nsCOMPtr
<nsIDocShellTreeItem
> curChild
;
208 rv
= itemAsNode
->GetChildAt(i
, getter_AddRefs(curChild
));
209 if (NS_FAILED(rv
)) return rv
;
211 rv
= BuildArrayRecursive(curChild
, inItemArray
);
212 if (NS_FAILED(rv
)) return rv
;
216 // add this item to the array
217 if ((mDocShellType
== nsIDocShellTreeItem::typeAll
) ||
218 (NS_SUCCEEDED(inItem
->GetItemType(&itemType
)) && (itemType
== mDocShellType
)))
220 rv
= inItemArray
.AppendElement((void *)inItem
);
221 if (NS_FAILED(rv
)) return rv
;