tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsDocShellEnumerator.cpp (2453B)


      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 using namespace mozilla;
     12 
     13 nsDocShellEnumerator::nsDocShellEnumerator(
     14    nsDocShellEnumerator::EnumerationDirection aDirection,
     15    int32_t aDocShellType, nsDocShell& aRootItem)
     16    : mRootItem(&aRootItem),
     17      mDocShellType(aDocShellType),
     18      mDirection(aDirection) {}
     19 
     20 nsresult nsDocShellEnumerator::BuildDocShellArray(
     21    nsTArray<RefPtr<nsIDocShell>>& aItemArray) {
     22  MOZ_ASSERT(mRootItem);
     23 
     24  aItemArray.Clear();
     25 
     26  if (mDirection == EnumerationDirection::Forwards) {
     27    return BuildArrayRecursiveForwards(mRootItem, aItemArray);
     28  }
     29  MOZ_ASSERT(mDirection == EnumerationDirection::Backwards);
     30  return BuildArrayRecursiveBackwards(mRootItem, aItemArray);
     31 }
     32 
     33 nsresult nsDocShellEnumerator::BuildArrayRecursiveForwards(
     34    nsDocShell* aItem, nsTArray<RefPtr<nsIDocShell>>& aItemArray) {
     35  nsresult rv;
     36 
     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;
     42    }
     43  }
     44 
     45  int32_t numChildren = aItem->ChildCount();
     46 
     47  for (int32_t i = 0; i < numChildren; ++i) {
     48    RefPtr<nsDocShell> curChild = aItem->GetInProcessChildAt(i);
     49    MOZ_ASSERT(curChild);
     50 
     51    rv = BuildArrayRecursiveForwards(curChild, aItemArray);
     52    if (NS_FAILED(rv)) {
     53      return rv;
     54    }
     55  }
     56 
     57  return NS_OK;
     58 }
     59 
     60 nsresult nsDocShellEnumerator::BuildArrayRecursiveBackwards(
     61    nsDocShell* aItem, nsTArray<RefPtr<nsIDocShell>>& aItemArray) {
     62  nsresult rv;
     63 
     64  uint32_t numChildren = aItem->ChildCount();
     65 
     66  for (int32_t i = numChildren - 1; i >= 0; --i) {
     67    RefPtr<nsDocShell> curChild = aItem->GetInProcessChildAt(i);
     68    MOZ_ASSERT(curChild);
     69 
     70    rv = BuildArrayRecursiveBackwards(curChild, aItemArray);
     71    if (NS_FAILED(rv)) {
     72      return rv;
     73    }
     74  }
     75 
     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;
     81    }
     82  }
     83 
     84  return NS_OK;
     85 }