Bug 1861709 replace AudioCallbackDriver::ThreadRunning() assertions that mean to...
[gecko.git] / docshell / base / nsDocShellEnumerator.cpp
blob5ad0ad35e6eea54c3c318c6cd5e8c76098c9b64c
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);
24 aItemArray.Clear();
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) {
35 nsresult rv;
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);
49 MOZ_ASSERT(curChild);
51 rv = BuildArrayRecursiveForwards(curChild, aItemArray);
52 if (NS_FAILED(rv)) {
53 return rv;
57 return NS_OK;
60 nsresult nsDocShellEnumerator::BuildArrayRecursiveBackwards(
61 nsDocShell* aItem, nsTArray<RefPtr<nsIDocShell>>& aItemArray) {
62 nsresult rv;
64 uint32_t numChildren = aItem->ChildCount();
66 for (int32_t i = numChildren - 1; i >= 0; --i) {
67 RefPtr<nsDocShell> curChild = aItem->GetInProcessChildAt(i);
68 MOZ_ASSERT(curChild);
70 rv = BuildArrayRecursiveBackwards(curChild, aItemArray);
71 if (NS_FAILED(rv)) {
72 return rv;
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;
84 return NS_OK;