Source code

Revision control

Other Tools

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/. */
6
7
#include "nsDocShellEnumerator.h"
8
9
#include "nsDocShell.h"
10
11
nsDocShellEnumerator::nsDocShellEnumerator(
12
nsDocShellEnumerator::EnumerationDirection aDirection,
13
int32_t aDocShellType, nsDocShell& aRootItem)
14
: mRootItem(&aRootItem),
15
mDocShellType(aDocShellType),
16
mDirection(aDirection) {}
17
18
nsresult nsDocShellEnumerator::BuildDocShellArray(
19
nsTArray<RefPtr<nsIDocShell>>& aItemArray) {
20
MOZ_ASSERT(mRootItem);
21
22
aItemArray.Clear();
23
24
if (mDirection == EnumerationDirection::Forwards) {
25
return BuildArrayRecursiveForwards(mRootItem, aItemArray);
26
}
27
MOZ_ASSERT(mDirection == EnumerationDirection::Backwards);
28
return BuildArrayRecursiveBackwards(mRootItem, aItemArray);
29
}
30
31
nsresult nsDocShellEnumerator::BuildArrayRecursiveForwards(
32
nsDocShell* aItem, nsTArray<RefPtr<nsIDocShell>>& aItemArray) {
33
nsresult rv;
34
35
// add this item to the array
36
if (mDocShellType == nsIDocShellTreeItem::typeAll ||
37
aItem->ItemType() == mDocShellType) {
38
if (!aItemArray.AppendElement(aItem, fallible)) {
39
return NS_ERROR_OUT_OF_MEMORY;
40
}
41
}
42
43
int32_t numChildren = aItem->ChildCount();
44
45
for (int32_t i = 0; i < numChildren; ++i) {
46
RefPtr<nsDocShell> curChild = aItem->GetInProcessChildAt(i);
47
MOZ_ASSERT(curChild);
48
49
rv = BuildArrayRecursiveForwards(curChild, aItemArray);
50
if (NS_FAILED(rv)) {
51
return rv;
52
}
53
}
54
55
return NS_OK;
56
}
57
58
nsresult nsDocShellEnumerator::BuildArrayRecursiveBackwards(
59
nsDocShell* aItem, nsTArray<RefPtr<nsIDocShell>>& aItemArray) {
60
nsresult rv;
61
62
uint32_t numChildren = aItem->ChildCount();
63
64
for (int32_t i = numChildren - 1; i >= 0; --i) {
65
RefPtr<nsDocShell> curChild = aItem->GetInProcessChildAt(i);
66
MOZ_ASSERT(curChild);
67
68
rv = BuildArrayRecursiveBackwards(curChild, aItemArray);
69
if (NS_FAILED(rv)) {
70
return rv;
71
}
72
}
73
74
// add this item to the array
75
if (mDocShellType == nsIDocShellTreeItem::typeAll ||
76
aItem->ItemType() == mDocShellType) {
77
if (!aItemArray.AppendElement(aItem)) {
78
return NS_ERROR_OUT_OF_MEMORY;
79
}
80
}
81
82
return NS_OK;
83
}