nsDocShellEnumerator.cpp 4.4 KB

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