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.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7
michael@0 8 #include "nsDocShellEnumerator.h"
michael@0 9
michael@0 10 #include "nsIDocShellTreeItem.h"
michael@0 11
michael@0 12 nsDocShellEnumerator::nsDocShellEnumerator(int32_t inEnumerationDirection)
michael@0 13 : mRootItem(nullptr)
michael@0 14 , mCurIndex(0)
michael@0 15 , mDocShellType(nsIDocShellTreeItem::typeAll)
michael@0 16 , mArrayValid(false)
michael@0 17 , mEnumerationDirection(inEnumerationDirection)
michael@0 18 {
michael@0 19 }
michael@0 20
michael@0 21 nsDocShellEnumerator::~nsDocShellEnumerator()
michael@0 22 {
michael@0 23 }
michael@0 24
michael@0 25 NS_IMPL_ISUPPORTS(nsDocShellEnumerator, nsISimpleEnumerator)
michael@0 26
michael@0 27
michael@0 28 /* nsISupports getNext (); */
michael@0 29 NS_IMETHODIMP nsDocShellEnumerator::GetNext(nsISupports **outCurItem)
michael@0 30 {
michael@0 31 NS_ENSURE_ARG_POINTER(outCurItem);
michael@0 32 *outCurItem = nullptr;
michael@0 33
michael@0 34 nsresult rv = EnsureDocShellArray();
michael@0 35 if (NS_FAILED(rv)) return rv;
michael@0 36
michael@0 37 if (mCurIndex >= mItemArray.Length()) {
michael@0 38 return NS_ERROR_FAILURE;
michael@0 39 }
michael@0 40
michael@0 41 // post-increment is important here
michael@0 42 nsCOMPtr<nsISupports> item = do_QueryReferent(mItemArray[mCurIndex++], &rv);
michael@0 43 item.forget(outCurItem);
michael@0 44 return rv;
michael@0 45 }
michael@0 46
michael@0 47 /* boolean hasMoreElements (); */
michael@0 48 NS_IMETHODIMP nsDocShellEnumerator::HasMoreElements(bool *outHasMore)
michael@0 49 {
michael@0 50 NS_ENSURE_ARG_POINTER(outHasMore);
michael@0 51 *outHasMore = false;
michael@0 52
michael@0 53 nsresult rv = EnsureDocShellArray();
michael@0 54 if (NS_FAILED(rv)) return rv;
michael@0 55
michael@0 56 *outHasMore = (mCurIndex < mItemArray.Length());
michael@0 57 return NS_OK;
michael@0 58 }
michael@0 59
michael@0 60 nsresult nsDocShellEnumerator::GetEnumerationRootItem(nsIDocShellTreeItem * *aEnumerationRootItem)
michael@0 61 {
michael@0 62 NS_ENSURE_ARG_POINTER(aEnumerationRootItem);
michael@0 63 nsCOMPtr<nsIDocShellTreeItem> item = do_QueryReferent(mRootItem);
michael@0 64 item.forget(aEnumerationRootItem);
michael@0 65 return NS_OK;
michael@0 66 }
michael@0 67
michael@0 68 nsresult nsDocShellEnumerator::SetEnumerationRootItem(nsIDocShellTreeItem * aEnumerationRootItem)
michael@0 69 {
michael@0 70 mRootItem = do_GetWeakReference(aEnumerationRootItem);
michael@0 71 ClearState();
michael@0 72 return NS_OK;
michael@0 73 }
michael@0 74
michael@0 75 nsresult nsDocShellEnumerator::GetEnumDocShellType(int32_t *aEnumerationItemType)
michael@0 76 {
michael@0 77 NS_ENSURE_ARG_POINTER(aEnumerationItemType);
michael@0 78 *aEnumerationItemType = mDocShellType;
michael@0 79 return NS_OK;
michael@0 80 }
michael@0 81
michael@0 82 nsresult nsDocShellEnumerator::SetEnumDocShellType(int32_t aEnumerationItemType)
michael@0 83 {
michael@0 84 mDocShellType = aEnumerationItemType;
michael@0 85 ClearState();
michael@0 86 return NS_OK;
michael@0 87 }
michael@0 88
michael@0 89 nsresult nsDocShellEnumerator::First()
michael@0 90 {
michael@0 91 mCurIndex = 0;
michael@0 92 return EnsureDocShellArray();
michael@0 93 }
michael@0 94
michael@0 95 nsresult nsDocShellEnumerator::EnsureDocShellArray()
michael@0 96 {
michael@0 97 if (!mArrayValid)
michael@0 98 {
michael@0 99 mArrayValid = true;
michael@0 100 return BuildDocShellArray(mItemArray);
michael@0 101 }
michael@0 102
michael@0 103 return NS_OK;
michael@0 104 }
michael@0 105
michael@0 106 nsresult nsDocShellEnumerator::ClearState()
michael@0 107 {
michael@0 108 mItemArray.Clear();
michael@0 109 mArrayValid = false;
michael@0 110 mCurIndex = 0;
michael@0 111 return NS_OK;
michael@0 112 }
michael@0 113
michael@0 114 nsresult nsDocShellEnumerator::BuildDocShellArray(nsTArray<nsWeakPtr>& inItemArray)
michael@0 115 {
michael@0 116 NS_ENSURE_TRUE(mRootItem, NS_ERROR_NOT_INITIALIZED);
michael@0 117 inItemArray.Clear();
michael@0 118 nsCOMPtr<nsIDocShellTreeItem> item = do_QueryReferent(mRootItem);
michael@0 119 return BuildArrayRecursive(item, inItemArray);
michael@0 120 }
michael@0 121
michael@0 122 nsresult nsDocShellForwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray)
michael@0 123 {
michael@0 124 nsresult rv;
michael@0 125
michael@0 126 // add this item to the array
michael@0 127 if (mDocShellType == nsIDocShellTreeItem::typeAll ||
michael@0 128 inItem->ItemType() == mDocShellType) {
michael@0 129 nsWeakPtr weakItem = do_GetWeakReference(inItem);
michael@0 130 if (!inItemArray.AppendElement(weakItem))
michael@0 131 return NS_ERROR_OUT_OF_MEMORY;
michael@0 132 }
michael@0 133
michael@0 134 int32_t numChildren;
michael@0 135 rv = inItem->GetChildCount(&numChildren);
michael@0 136 if (NS_FAILED(rv)) return rv;
michael@0 137
michael@0 138 for (int32_t i = 0; i < numChildren; ++i)
michael@0 139 {
michael@0 140 nsCOMPtr<nsIDocShellTreeItem> curChild;
michael@0 141 rv = inItem->GetChildAt(i, getter_AddRefs(curChild));
michael@0 142 if (NS_FAILED(rv)) return rv;
michael@0 143
michael@0 144 rv = BuildArrayRecursive(curChild, inItemArray);
michael@0 145 if (NS_FAILED(rv)) return rv;
michael@0 146 }
michael@0 147
michael@0 148 return NS_OK;
michael@0 149 }
michael@0 150
michael@0 151
michael@0 152 nsresult nsDocShellBackwardsEnumerator::BuildArrayRecursive(nsIDocShellTreeItem* inItem, nsTArray<nsWeakPtr>& inItemArray)
michael@0 153 {
michael@0 154 nsresult rv;
michael@0 155
michael@0 156 int32_t numChildren;
michael@0 157 rv = inItem->GetChildCount(&numChildren);
michael@0 158 if (NS_FAILED(rv)) return rv;
michael@0 159
michael@0 160 for (int32_t i = numChildren - 1; i >= 0; --i)
michael@0 161 {
michael@0 162 nsCOMPtr<nsIDocShellTreeItem> curChild;
michael@0 163 rv = inItem->GetChildAt(i, getter_AddRefs(curChild));
michael@0 164 if (NS_FAILED(rv)) return rv;
michael@0 165
michael@0 166 rv = BuildArrayRecursive(curChild, inItemArray);
michael@0 167 if (NS_FAILED(rv)) return rv;
michael@0 168 }
michael@0 169
michael@0 170 // add this item to the array
michael@0 171 if (mDocShellType == nsIDocShellTreeItem::typeAll ||
michael@0 172 inItem->ItemType() == mDocShellType) {
michael@0 173 nsWeakPtr weakItem = do_GetWeakReference(inItem);
michael@0 174 if (!inItemArray.AppendElement(weakItem))
michael@0 175 return NS_ERROR_OUT_OF_MEMORY;
michael@0 176 }
michael@0 177
michael@0 178 return NS_OK;
michael@0 179 }

mercurial