docshell/base/nsDocShellEnumerator.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     2  *
     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/. */
     8 #include "nsDocShellEnumerator.h"
    10 #include "nsIDocShellTreeItem.h"
    12 nsDocShellEnumerator::nsDocShellEnumerator(int32_t inEnumerationDirection)
    13 : mRootItem(nullptr)
    14 , mCurIndex(0)
    15 , mDocShellType(nsIDocShellTreeItem::typeAll)
    16 , mArrayValid(false)
    17 , mEnumerationDirection(inEnumerationDirection)
    18 {
    19 }
    21 nsDocShellEnumerator::~nsDocShellEnumerator()
    22 {
    23 }
    25 NS_IMPL_ISUPPORTS(nsDocShellEnumerator, nsISimpleEnumerator)
    28 /* nsISupports getNext (); */
    29 NS_IMETHODIMP nsDocShellEnumerator::GetNext(nsISupports **outCurItem)
    30 {
    31   NS_ENSURE_ARG_POINTER(outCurItem);
    32   *outCurItem = nullptr;
    34   nsresult rv = EnsureDocShellArray();
    35   if (NS_FAILED(rv)) return rv;
    37   if (mCurIndex >= mItemArray.Length()) {
    38     return NS_ERROR_FAILURE;
    39   }
    41   // post-increment is important here
    42   nsCOMPtr<nsISupports> item = do_QueryReferent(mItemArray[mCurIndex++], &rv);
    43   item.forget(outCurItem);
    44   return rv;
    45 }
    47 /* boolean hasMoreElements (); */
    48 NS_IMETHODIMP nsDocShellEnumerator::HasMoreElements(bool *outHasMore)
    49 {
    50   NS_ENSURE_ARG_POINTER(outHasMore);
    51   *outHasMore = false;
    53   nsresult rv = EnsureDocShellArray();
    54   if (NS_FAILED(rv)) return rv;
    56   *outHasMore = (mCurIndex < mItemArray.Length());
    57   return NS_OK;
    58 }
    60 nsresult nsDocShellEnumerator::GetEnumerationRootItem(nsIDocShellTreeItem * *aEnumerationRootItem)
    61 {
    62   NS_ENSURE_ARG_POINTER(aEnumerationRootItem);
    63   nsCOMPtr<nsIDocShellTreeItem> item = do_QueryReferent(mRootItem);
    64   item.forget(aEnumerationRootItem);
    65   return NS_OK;
    66 }
    68 nsresult nsDocShellEnumerator::SetEnumerationRootItem(nsIDocShellTreeItem * aEnumerationRootItem)
    69 {
    70   mRootItem = do_GetWeakReference(aEnumerationRootItem);
    71   ClearState();
    72   return NS_OK;
    73 }
    75 nsresult nsDocShellEnumerator::GetEnumDocShellType(int32_t *aEnumerationItemType)
    76 {
    77   NS_ENSURE_ARG_POINTER(aEnumerationItemType);
    78   *aEnumerationItemType = mDocShellType;
    79   return NS_OK;
    80 }
    82 nsresult nsDocShellEnumerator::SetEnumDocShellType(int32_t aEnumerationItemType)
    83 {
    84   mDocShellType = aEnumerationItemType;
    85   ClearState();
    86   return NS_OK;
    87 }
    89 nsresult nsDocShellEnumerator::First()
    90 {
    91   mCurIndex = 0;
    92   return EnsureDocShellArray();
    93 }
    95 nsresult nsDocShellEnumerator::EnsureDocShellArray()
    96 {
    97   if (!mArrayValid)
    98   {
    99     mArrayValid = true;
   100     return BuildDocShellArray(mItemArray);
   101   }
   103   return NS_OK;
   104 }
   106 nsresult nsDocShellEnumerator::ClearState()
   107 {
   108   mItemArray.Clear();
   109   mArrayValid = false;
   110   mCurIndex = 0;
   111   return NS_OK;
   112 }
   114 nsresult nsDocShellEnumerator::BuildDocShellArray(nsTArray<nsWeakPtr>& inItemArray)
   115 {
   116   NS_ENSURE_TRUE(mRootItem, NS_ERROR_NOT_INITIALIZED);
   117   inItemArray.Clear();
   118   nsCOMPtr<nsIDocShellTreeItem> item = do_QueryReferent(mRootItem);
   119   return BuildArrayRecursive(item, inItemArray);
   120 }
   122 nsresult nsDocShellForwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray)
   123 {
   124   nsresult rv;
   126   // add this item to the array
   127   if (mDocShellType == nsIDocShellTreeItem::typeAll ||
   128       inItem->ItemType() == mDocShellType) {
   129     nsWeakPtr weakItem = do_GetWeakReference(inItem);
   130     if (!inItemArray.AppendElement(weakItem))
   131       return NS_ERROR_OUT_OF_MEMORY;
   132   }
   134   int32_t   numChildren;
   135   rv = inItem->GetChildCount(&numChildren);
   136   if (NS_FAILED(rv)) return rv;
   138   for (int32_t i = 0; i < numChildren; ++i)
   139   {
   140     nsCOMPtr<nsIDocShellTreeItem> curChild;
   141     rv = inItem->GetChildAt(i, getter_AddRefs(curChild));
   142     if (NS_FAILED(rv)) return rv;
   144     rv = BuildArrayRecursive(curChild, inItemArray);
   145     if (NS_FAILED(rv)) return rv;
   146   }
   148   return NS_OK;
   149 }
   152 nsresult nsDocShellBackwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray)
   153 {
   154   nsresult rv;
   156   int32_t   numChildren;
   157   rv = inItem->GetChildCount(&numChildren);
   158   if (NS_FAILED(rv)) return rv;
   160   for (int32_t i = numChildren - 1; i >= 0; --i)
   161   {
   162     nsCOMPtr<nsIDocShellTreeItem> curChild;
   163     rv = inItem->GetChildAt(i, getter_AddRefs(curChild));
   164     if (NS_FAILED(rv)) return rv;
   166     rv = BuildArrayRecursive(curChild, inItemArray);
   167     if (NS_FAILED(rv)) return rv;
   168   }
   170   // add this item to the array
   171   if (mDocShellType == nsIDocShellTreeItem::typeAll ||
   172       inItem->ItemType() == mDocShellType) {
   173     nsWeakPtr weakItem = do_GetWeakReference(inItem);
   174     if (!inItemArray.AppendElement(weakItem))
   175       return NS_ERROR_OUT_OF_MEMORY;
   176   }
   178   return NS_OK;
   179 }

mercurial