Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / base / DOMStringList.h
blob89bb9fc418cce101ad2eb98c430980316a93f52e
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/. */
7 #ifndef mozilla_dom_DOMStringList_h
8 #define mozilla_dom_DOMStringList_h
10 #include "nsISupports.h"
11 #include "nsTArray.h"
12 #include "nsWrapperCache.h"
13 #include "nsString.h"
15 namespace mozilla::dom {
17 class DOMStringList : public nsISupports, public nsWrapperCache {
18 protected:
19 virtual ~DOMStringList();
21 public:
22 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
23 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(DOMStringList)
25 virtual JSObject* WrapObject(JSContext* aCx,
26 JS::Handle<JSObject*> aGivenProto) override;
27 nsISupports* GetParentObject() { return nullptr; }
29 void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aResult) {
30 EnsureFresh();
31 if (aIndex < mNames.Length()) {
32 aFound = true;
33 aResult = mNames[aIndex];
34 } else {
35 aFound = false;
39 void Item(uint32_t aIndex, nsAString& aResult) {
40 EnsureFresh();
41 if (aIndex < mNames.Length()) {
42 aResult = mNames[aIndex];
43 } else {
44 aResult.SetIsVoid(true);
48 uint32_t Length() {
49 EnsureFresh();
50 return mNames.Length();
53 bool Contains(const nsAString& aString) {
54 EnsureFresh();
55 return mNames.Contains(aString);
58 bool Add(const nsAString& aName) {
59 // XXXbz(Bug 1631374) mNames should really be a fallible array; otherwise
60 // this return value is meaningless. return mNames.AppendElement(aName) !=
61 // nullptr;
62 mNames.AppendElement(aName);
63 return true;
66 void Clear() { mNames.Clear(); }
68 nsTArray<nsString>& StringArray() { return mNames; }
70 void CopyList(nsTArray<nsString>& aNames) { aNames = mNames.Clone(); }
72 protected:
73 // A method that subclasses can override to modify mNames as needed
74 // before we index into it or return its length or whatnot.
75 virtual void EnsureFresh() {}
77 // XXXbz we really want this to be a fallible array, but we end up passing it
78 // to consumers who declare themselves as taking and nsTArray. :(
79 nsTArray<nsString> mNames;
82 } // namespace mozilla::dom
84 #endif /* mozilla_dom_DOMStringList_h */