DOMSVGStringList.h (4767B)
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 DOM_SVG_DOMSVGSTRINGLIST_H_ 8 #define DOM_SVG_DOMSVGSTRINGLIST_H_ 9 10 #include "SVGElement.h" 11 #include "mozilla/RefPtr.h" 12 #include "nsCycleCollectionParticipant.h" 13 14 namespace mozilla { 15 16 class ErrorResult; 17 class SVGStringList; 18 19 namespace dom { 20 21 /** 22 * Class DOMSVGStringList 23 * 24 * This class is used to create the DOM tearoff objects that wrap internal 25 * SVGPathData objects. 26 * 27 * See the architecture comment in DOMSVGAnimatedLengthList.h first (that's 28 * LENGTH list), then continue reading the remainder of this comment. 29 * 30 * The architecture of this class is similar to that of DOMSVGLengthList 31 * except for two important aspects: 32 * 33 * First, since there is no nsIDOMSVGAnimatedStringList interface in SVG, we 34 * have no parent DOMSVGAnimatedStringList (unlike DOMSVGLengthList which has 35 * a parent DOMSVGAnimatedLengthList class). As a consequence, much of the 36 * logic that would otherwise be in DOMSVGAnimatedStringList (and is in 37 * DOMSVGAnimatedLengthList) is contained in this class. 38 * 39 * Second, since there is no nsIDOMSVGString interface in SVG, we have no 40 * DOMSVGString items to maintain. As far as script is concerned, objects 41 * of this class contain a list of strings, not a list of mutable objects 42 * like the other SVG list types. As a result, unlike the other SVG list 43 * types, this class does not create its items lazily on demand and store 44 * them so it can return the same objects each time. It simply returns a new 45 * string each time any given item is requested. 46 */ 47 class DOMSVGStringList final : public nsISupports, public nsWrapperCache { 48 friend class AutoChangeStringListNotifier; 49 50 public: 51 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 52 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMSVGStringList) 53 54 dom::SVGElement* GetParentObject() const { return mElement; } 55 JSObject* WrapObject(JSContext* aCx, 56 JS::Handle<JSObject*> aGivenProto) override; 57 58 uint32_t NumberOfItems() const; 59 uint32_t Length() const; 60 void Clear(); 61 void Initialize(const nsAString& aNewItem, nsAString& aRetval, 62 ErrorResult& aRv); 63 void GetItem(uint32_t aIndex, nsAString& aRetval, ErrorResult& aRv); 64 void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aRetval); 65 void InsertItemBefore(const nsAString& aNewItem, uint32_t aIndex, 66 nsAString& aRetval, ErrorResult& aRv); 67 void ReplaceItem(const nsAString& aNewItem, uint32_t aIndex, 68 nsAString& aRetval, ErrorResult& aRv); 69 void RemoveItem(uint32_t aIndex, nsAString& aRetval, ErrorResult& aRv); 70 void AppendItem(const nsAString& aNewItem, nsAString& aRetval, 71 ErrorResult& aRv); 72 73 /** 74 * Factory method to create and return a DOMSVGStringList wrapper 75 * for a given internal SVGStringList object. The factory takes care 76 * of caching the object that it returns so that the same object can be 77 * returned for the given SVGStringList each time it is requested. 78 * The cached object is only removed from the cache when it is destroyed due 79 * to there being no more references to it. If that happens, any subsequent 80 * call requesting the DOM wrapper for the SVGStringList will naturally 81 * result in a new DOMSVGStringList being returned. 82 */ 83 static already_AddRefed<DOMSVGStringList> GetDOMWrapper( 84 SVGStringList* aList, dom::SVGElement* aElement, 85 bool aIsConditionalProcessingAttribute, uint8_t aAttrEnum); 86 87 private: 88 /** 89 * Only our static GetDOMWrapper() factory method may create objects of our 90 * type. 91 */ 92 DOMSVGStringList(dom::SVGElement* aElement, 93 bool aIsConditionalProcessingAttribute, uint8_t aAttrEnum) 94 : mElement(aElement), 95 mAttrEnum(aAttrEnum), 96 mIsConditionalProcessingAttribute(aIsConditionalProcessingAttribute) {} 97 98 ~DOMSVGStringList(); 99 100 SVGStringList& InternalList() const; 101 102 void RemoveFromTearoffTable(); 103 104 // Strong ref to our element to keep it alive. 105 RefPtr<dom::SVGElement> mElement; 106 107 uint8_t mAttrEnum; 108 109 bool mIsConditionalProcessingAttribute; 110 111 // Tracks whether we're in the tearoff table. Initialized to true, since all 112 // new instances are added to the table right after construction. Updated to 113 // false when we're removed from the table (at which point we're being 114 // destructed or soon-to-be destructed). 115 bool mIsInTearoffTable = true; 116 }; 117 118 } // namespace dom 119 } // namespace mozilla 120 121 #endif // DOM_SVG_DOMSVGSTRINGLIST_H_