1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsDocShellEnumerator.h"
9 #include "nsDocShell.h"
11 using namespace mozilla
;
13 nsDocShellEnumerator::nsDocShellEnumerator(
14 nsDocShellEnumerator::EnumerationDirection aDirection
,
15 int32_t aDocShellType
, nsDocShell
& aRootItem
)
16 : mRootItem(&aRootItem
),
17 mDocShellType(aDocShellType
),
18 mDirection(aDirection
) {}
20 nsresult
nsDocShellEnumerator::BuildDocShellArray(
21 nsTArray
<RefPtr
<nsIDocShell
>>& aItemArray
) {
22 MOZ_ASSERT(mRootItem
);
26 if (mDirection
== EnumerationDirection::Forwards
) {
27 return BuildArrayRecursiveForwards(mRootItem
, aItemArray
);
29 MOZ_ASSERT(mDirection
== EnumerationDirection::Backwards
);
30 return BuildArrayRecursiveBackwards(mRootItem
, aItemArray
);
33 nsresult
nsDocShellEnumerator::BuildArrayRecursiveForwards(
34 nsDocShell
* aItem
, nsTArray
<RefPtr
<nsIDocShell
>>& aItemArray
) {
37 // add this item to the array
38 if (mDocShellType
== nsIDocShellTreeItem::typeAll
||
39 aItem
->ItemType() == mDocShellType
) {
40 if (!aItemArray
.AppendElement(aItem
, fallible
)) {
41 return NS_ERROR_OUT_OF_MEMORY
;
45 int32_t numChildren
= aItem
->ChildCount();
47 for (int32_t i
= 0; i
< numChildren
; ++i
) {
48 RefPtr
<nsDocShell
> curChild
= aItem
->GetInProcessChildAt(i
);
51 rv
= BuildArrayRecursiveForwards(curChild
, aItemArray
);
60 nsresult
nsDocShellEnumerator::BuildArrayRecursiveBackwards(
61 nsDocShell
* aItem
, nsTArray
<RefPtr
<nsIDocShell
>>& aItemArray
) {
64 uint32_t numChildren
= aItem
->ChildCount();
66 for (int32_t i
= numChildren
- 1; i
>= 0; --i
) {
67 RefPtr
<nsDocShell
> curChild
= aItem
->GetInProcessChildAt(i
);
70 rv
= BuildArrayRecursiveBackwards(curChild
, aItemArray
);
76 // add this item to the array
77 if (mDocShellType
== nsIDocShellTreeItem::typeAll
||
78 aItem
->ItemType() == mDocShellType
) {
79 if (!aItemArray
.AppendElement(aItem
, fallible
)) {
80 return NS_ERROR_OUT_OF_MEMORY
;