DOMStringList.h (2377B)
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 "nsString.h" 12 #include "nsTArray.h" 13 #include "nsWrapperCache.h" 14 15 namespace mozilla::dom { 16 17 class DOMStringList : public nsISupports, public nsWrapperCache { 18 protected: 19 virtual ~DOMStringList(); 20 21 public: 22 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 23 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(DOMStringList) 24 25 virtual JSObject* WrapObject(JSContext* aCx, 26 JS::Handle<JSObject*> aGivenProto) override; 27 nsISupports* GetParentObject() { return nullptr; } 28 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; 36 } 37 } 38 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); 45 } 46 } 47 48 uint32_t Length() { 49 EnsureFresh(); 50 return mNames.Length(); 51 } 52 53 bool Contains(const nsAString& aString) { 54 EnsureFresh(); 55 return mNames.Contains(aString); 56 } 57 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; 64 } 65 66 void Clear() { mNames.Clear(); } 67 68 nsTArray<nsString>& StringArray() { return mNames; } 69 70 void CopyList(nsTArray<nsString>& aNames) { aNames = mNames.Clone(); } 71 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() {} 76 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; 80 }; 81 82 } // namespace mozilla::dom 83 84 #endif /* mozilla_dom_DOMStringList_h */