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 #ifndef nsDocShellEnumerator_h___
8 #define nsDocShellEnumerator_h___
9 
10 #include "nsISimpleEnumerator.h"
11 #include "nsTArray.h"
12 #include "nsIWeakReferenceUtils.h"
13 
14 class nsIDocShellTreeItem;
15 
16 /*
17 // {13cbc281-35ae-11d5-be5b-bde0edece43c}
18 #define NS_DOCSHELL_FORWARDS_ENUMERATOR_CID  \
19 { 0x13cbc281, 0x35ae, 0x11d5, { 0xbe, 0x5b, 0xbd, 0xe0, 0xed, 0xec, 0xe4, 0x3c }
20 }
21 
22 #define NS_DOCSHELL_FORWARDS_ENUMERATOR_CONTRACTID \
23 "@mozilla.org/docshell/enumerator-forwards;1"
24 
25 // {13cbc282-35ae-11d5-be5b-bde0edece43c}
26 #define NS_DOCSHELL_BACKWARDS_ENUMERATOR_CID  \
27 { 0x13cbc282, 0x35ae, 0x11d5, { 0xbe, 0x5b, 0xbd, 0xe0, 0xed, 0xec, 0xe4, 0x3c }
28 }
29 
30 #define NS_DOCSHELL_BACKWARDS_ENUMERATOR_CONTRACTID \
31 "@mozilla.org/docshell/enumerator-backwards;1"
32 */
33 
34 class nsDocShellEnumerator : public nsISimpleEnumerator {
35  protected:
36   enum { enumerateForwards, enumerateBackwards };
37 
38   virtual ~nsDocShellEnumerator();
39 
40  public:
41   explicit nsDocShellEnumerator(int32_t aEnumerationDirection);
42 
43   // nsISupports
44   NS_DECL_ISUPPORTS
45 
46   // nsISimpleEnumerator
47   NS_DECL_NSISIMPLEENUMERATOR
48 
49  public:
50   nsresult GetEnumerationRootItem(nsIDocShellTreeItem** aEnumerationRootItem);
51   nsresult SetEnumerationRootItem(nsIDocShellTreeItem* aEnumerationRootItem);
52 
53   nsresult GetEnumDocShellType(int32_t* aEnumerationItemType);
54   nsresult SetEnumDocShellType(int32_t aEnumerationItemType);
55 
56   nsresult First();
57 
58  protected:
59   nsresult EnsureDocShellArray();
60   nsresult ClearState();
61 
62   nsresult BuildDocShellArray(nsTArray<nsWeakPtr>& aItemArray);
63   virtual nsresult BuildArrayRecursive(nsIDocShellTreeItem* aItem,
64                                        nsTArray<nsWeakPtr>& aItemArray) = 0;
65 
66  protected:
67   nsWeakPtr mRootItem;  // weak ref!
68 
69   nsTArray<nsWeakPtr> mItemArray;  // flattened list of items with matching type
70   uint32_t mCurIndex;
71 
72   int32_t mDocShellType;  // only want shells of this type
73   bool mArrayValid;       // is mItemArray up to date?
74 
75   const int8_t mEnumerationDirection;
76 };
77 
78 class nsDocShellForwardsEnumerator : public nsDocShellEnumerator {
79  public:
nsDocShellForwardsEnumerator()80   nsDocShellForwardsEnumerator() : nsDocShellEnumerator(enumerateForwards) {}
81 
82  protected:
83   virtual nsresult BuildArrayRecursive(
84       nsIDocShellTreeItem* aItem, nsTArray<nsWeakPtr>& aItemArray) override;
85 };
86 
87 class nsDocShellBackwardsEnumerator : public nsDocShellEnumerator {
88  public:
nsDocShellBackwardsEnumerator()89   nsDocShellBackwardsEnumerator() : nsDocShellEnumerator(enumerateBackwards) {}
90 
91  protected:
92   virtual nsresult BuildArrayRecursive(
93       nsIDocShellTreeItem* aItem, nsTArray<nsWeakPtr>& aItemArray) override;
94 };
95 
96 #endif  // nsDocShellEnumerator_h___
97