1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is the Mozilla browser.
17 : *
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.
22 : *
23 : * Contributor(s):
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 :
40 : #include "nsDocShellEnumerator.h"
41 :
42 : #include "nsIDocShellTreeItem.h"
43 : #include "nsIDocShellTreeNode.h"
44 :
45 0 : nsDocShellEnumerator::nsDocShellEnumerator(PRInt32 inEnumerationDirection)
46 : : mRootItem(nsnull)
47 : , mCurIndex(0)
48 : , mDocShellType(nsIDocShellTreeItem::typeAll)
49 : , mArrayValid(false)
50 0 : , mEnumerationDirection(inEnumerationDirection)
51 : {
52 0 : }
53 :
54 0 : nsDocShellEnumerator::~nsDocShellEnumerator()
55 : {
56 0 : }
57 :
58 0 : NS_IMPL_ISUPPORTS1(nsDocShellEnumerator, nsISimpleEnumerator)
59 :
60 :
61 : /* nsISupports getNext (); */
62 0 : NS_IMETHODIMP nsDocShellEnumerator::GetNext(nsISupports **outCurItem)
63 : {
64 0 : NS_ENSURE_ARG_POINTER(outCurItem);
65 0 : *outCurItem = nsnull;
66 :
67 0 : nsresult rv = EnsureDocShellArray();
68 0 : if (NS_FAILED(rv)) return rv;
69 :
70 0 : if (mCurIndex >= mItemArray.Length()) {
71 0 : return NS_ERROR_FAILURE;
72 : }
73 :
74 : // post-increment is important here
75 0 : nsCOMPtr<nsISupports> item = do_QueryReferent(mItemArray[mCurIndex++], &rv);
76 0 : item.forget(outCurItem);
77 0 : return rv;
78 : }
79 :
80 : /* boolean hasMoreElements (); */
81 0 : NS_IMETHODIMP nsDocShellEnumerator::HasMoreElements(bool *outHasMore)
82 : {
83 0 : NS_ENSURE_ARG_POINTER(outHasMore);
84 0 : *outHasMore = false;
85 :
86 0 : nsresult rv = EnsureDocShellArray();
87 0 : if (NS_FAILED(rv)) return rv;
88 :
89 0 : *outHasMore = (mCurIndex < mItemArray.Length());
90 0 : return NS_OK;
91 : }
92 :
93 0 : nsresult nsDocShellEnumerator::GetEnumerationRootItem(nsIDocShellTreeItem * *aEnumerationRootItem)
94 : {
95 0 : NS_ENSURE_ARG_POINTER(aEnumerationRootItem);
96 0 : nsCOMPtr<nsIDocShellTreeItem> item = do_QueryReferent(mRootItem);
97 0 : item.forget(aEnumerationRootItem);
98 0 : return NS_OK;
99 : }
100 :
101 0 : nsresult nsDocShellEnumerator::SetEnumerationRootItem(nsIDocShellTreeItem * aEnumerationRootItem)
102 : {
103 0 : mRootItem = do_GetWeakReference(aEnumerationRootItem);
104 0 : ClearState();
105 0 : return NS_OK;
106 : }
107 :
108 0 : nsresult nsDocShellEnumerator::GetEnumDocShellType(PRInt32 *aEnumerationItemType)
109 : {
110 0 : NS_ENSURE_ARG_POINTER(aEnumerationItemType);
111 0 : *aEnumerationItemType = mDocShellType;
112 0 : return NS_OK;
113 : }
114 :
115 0 : nsresult nsDocShellEnumerator::SetEnumDocShellType(PRInt32 aEnumerationItemType)
116 : {
117 0 : mDocShellType = aEnumerationItemType;
118 0 : ClearState();
119 0 : return NS_OK;
120 : }
121 :
122 0 : nsresult nsDocShellEnumerator::First()
123 : {
124 0 : mCurIndex = 0;
125 0 : return EnsureDocShellArray();
126 : }
127 :
128 0 : nsresult nsDocShellEnumerator::EnsureDocShellArray()
129 : {
130 0 : if (!mArrayValid)
131 : {
132 0 : mArrayValid = true;
133 0 : return BuildDocShellArray(mItemArray);
134 : }
135 :
136 0 : return NS_OK;
137 : }
138 :
139 0 : nsresult nsDocShellEnumerator::ClearState()
140 : {
141 0 : mItemArray.Clear();
142 0 : mArrayValid = false;
143 0 : mCurIndex = 0;
144 0 : return NS_OK;
145 : }
146 :
147 0 : nsresult nsDocShellEnumerator::BuildDocShellArray(nsTArray<nsWeakPtr>& inItemArray)
148 : {
149 0 : NS_ENSURE_TRUE(mRootItem, NS_ERROR_NOT_INITIALIZED);
150 0 : inItemArray.Clear();
151 0 : nsCOMPtr<nsIDocShellTreeItem> item = do_QueryReferent(mRootItem);
152 0 : return BuildArrayRecursive(item, inItemArray);
153 : }
154 :
155 0 : nsresult nsDocShellForwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray)
156 : {
157 : nsresult rv;
158 0 : nsCOMPtr<nsIDocShellTreeNode> itemAsNode = do_QueryInterface(inItem, &rv);
159 0 : if (NS_FAILED(rv)) return rv;
160 :
161 : PRInt32 itemType;
162 : // add this item to the array
163 0 : if ((mDocShellType == nsIDocShellTreeItem::typeAll) ||
164 0 : (NS_SUCCEEDED(inItem->GetItemType(&itemType)) && (itemType == mDocShellType)))
165 : {
166 0 : if (!inItemArray.AppendElement(do_GetWeakReference(inItem)))
167 0 : return NS_ERROR_OUT_OF_MEMORY;
168 : }
169 :
170 : PRInt32 numChildren;
171 0 : rv = itemAsNode->GetChildCount(&numChildren);
172 0 : if (NS_FAILED(rv)) return rv;
173 :
174 0 : for (PRInt32 i = 0; i < numChildren; ++i)
175 : {
176 0 : nsCOMPtr<nsIDocShellTreeItem> curChild;
177 0 : rv = itemAsNode->GetChildAt(i, getter_AddRefs(curChild));
178 0 : if (NS_FAILED(rv)) return rv;
179 :
180 0 : rv = BuildArrayRecursive(curChild, inItemArray);
181 0 : if (NS_FAILED(rv)) return rv;
182 : }
183 :
184 0 : return NS_OK;
185 : }
186 :
187 :
188 0 : nsresult nsDocShellBackwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray)
189 : {
190 : nsresult rv;
191 0 : nsCOMPtr<nsIDocShellTreeNode> itemAsNode = do_QueryInterface(inItem, &rv);
192 0 : if (NS_FAILED(rv)) return rv;
193 :
194 : PRInt32 numChildren;
195 0 : rv = itemAsNode->GetChildCount(&numChildren);
196 0 : if (NS_FAILED(rv)) return rv;
197 :
198 0 : for (PRInt32 i = numChildren - 1; i >= 0; --i)
199 : {
200 0 : nsCOMPtr<nsIDocShellTreeItem> curChild;
201 0 : rv = itemAsNode->GetChildAt(i, getter_AddRefs(curChild));
202 0 : if (NS_FAILED(rv)) return rv;
203 :
204 0 : rv = BuildArrayRecursive(curChild, inItemArray);
205 0 : if (NS_FAILED(rv)) return rv;
206 : }
207 :
208 : PRInt32 itemType;
209 : // add this item to the array
210 0 : if ((mDocShellType == nsIDocShellTreeItem::typeAll) ||
211 0 : (NS_SUCCEEDED(inItem->GetItemType(&itemType)) && (itemType == mDocShellType)))
212 : {
213 0 : if (!inItemArray.AppendElement(do_GetWeakReference(inItem)))
214 0 : return NS_ERROR_OUT_OF_MEMORY;
215 : }
216 :
217 :
218 0 : return NS_OK;
219 : }
220 :
221 :
|