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