|
1 /* -*- Mode: C++; tab-width: 2; 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 |
|
6 #ifndef mozilla_dom_DOMStringList_h |
|
7 #define mozilla_dom_DOMStringList_h |
|
8 |
|
9 #include "nsISupports.h" |
|
10 #include "nsTArray.h" |
|
11 #include "nsWrapperCache.h" |
|
12 #include "nsString.h" |
|
13 |
|
14 namespace mozilla { |
|
15 namespace dom { |
|
16 |
|
17 class DOMStringList : public nsISupports, |
|
18 public nsWrapperCache |
|
19 { |
|
20 public: |
|
21 DOMStringList() |
|
22 { |
|
23 SetIsDOMBinding(); |
|
24 } |
|
25 virtual ~DOMStringList(); |
|
26 |
|
27 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
28 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMStringList) |
|
29 |
|
30 virtual JSObject* WrapObject(JSContext* aCx); |
|
31 nsISupports* GetParentObject() |
|
32 { |
|
33 return nullptr; |
|
34 } |
|
35 |
|
36 void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aResult) |
|
37 { |
|
38 EnsureFresh(); |
|
39 if (aIndex < mNames.Length()) { |
|
40 aFound = true; |
|
41 aResult = mNames[aIndex]; |
|
42 } else { |
|
43 aFound = false; |
|
44 } |
|
45 } |
|
46 |
|
47 void Item(uint32_t aIndex, nsAString& aResult) |
|
48 { |
|
49 EnsureFresh(); |
|
50 if (aIndex < mNames.Length()) { |
|
51 aResult = mNames[aIndex]; |
|
52 } else { |
|
53 aResult.SetIsVoid(true); |
|
54 } |
|
55 } |
|
56 |
|
57 uint32_t Length() |
|
58 { |
|
59 EnsureFresh(); |
|
60 return mNames.Length(); |
|
61 } |
|
62 |
|
63 bool Contains(const nsAString& aString) |
|
64 { |
|
65 EnsureFresh(); |
|
66 return mNames.Contains(aString); |
|
67 } |
|
68 |
|
69 bool Add(const nsAString& aName) |
|
70 { |
|
71 // XXXbz mNames should really be a fallible array; otherwise this |
|
72 // return value is meaningless. |
|
73 return mNames.AppendElement(aName) != nullptr; |
|
74 } |
|
75 |
|
76 void Clear() |
|
77 { |
|
78 mNames.Clear(); |
|
79 } |
|
80 |
|
81 nsTArray<nsString>& StringArray() |
|
82 { |
|
83 return mNames; |
|
84 } |
|
85 |
|
86 void CopyList(nsTArray<nsString>& aNames) |
|
87 { |
|
88 aNames = mNames; |
|
89 } |
|
90 |
|
91 protected: |
|
92 // A method that subclasses can override to modify mNames as needed |
|
93 // before we index into it or return its length or whatnot. |
|
94 virtual void EnsureFresh() |
|
95 { |
|
96 } |
|
97 |
|
98 // XXXbz we really want this to be a fallible array, but we end up passing it |
|
99 // to consumers who declare themselves as taking and nsTArray. :( |
|
100 nsTArray<nsString> mNames; |
|
101 }; |
|
102 |
|
103 } // namespace dom |
|
104 } // namespace mozilla |
|
105 |
|
106 #endif /* mozilla_dom_DOMStringList_h */ |