DOMSVGTransformList.h (7171B)
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_DOMSVGTRANSFORMLIST_H_ 8 #define DOM_SVG_DOMSVGTRANSFORMLIST_H_ 9 10 #include "DOMSVGAnimatedTransformList.h" 11 #include "SVGTransformList.h" 12 #include "mozAutoDocUpdate.h" 13 #include "mozilla/Attributes.h" 14 #include "nsCycleCollectionParticipant.h" 15 #include "nsDebug.h" 16 #include "nsTArray.h" 17 18 namespace mozilla { 19 class ErrorResult; 20 21 namespace dom { 22 struct DOMMatrix2DInit; 23 class DOMSVGTransform; 24 class SVGElement; 25 class SVGMatrix; 26 27 //---------------------------------------------------------------------- 28 // Helper class: AutoChangeTransformListNotifier 29 // Stack-based helper class to pair calls to WillChangeTransformList and 30 // DidChangeTransformList. Used by DOMSVGTransform and DOMSVGTransformList. 31 template <class T> 32 class MOZ_RAII AutoChangeTransformListNotifier { 33 public: 34 explicit AutoChangeTransformListNotifier(T* aValue) : mValue(aValue) { 35 MOZ_ASSERT(mValue, "Expecting non-null value"); 36 // If we don't have an owner then changes don't affect anything else. 37 if (mValue->HasOwner()) { 38 mUpdateBatch.emplace(mValue->Element()->GetComposedDoc(), true); 39 mValue->Element()->WillChangeTransformList(mUpdateBatch.ref()); 40 } 41 } 42 43 ~AutoChangeTransformListNotifier() { 44 if (mValue->HasOwner()) { 45 mValue->Element()->DidChangeTransformList(mUpdateBatch.ref()); 46 if (mValue->IsAnimating()) { 47 mValue->Element()->AnimationNeedsResample(); 48 } 49 } 50 } 51 52 private: 53 T* const mValue; 54 Maybe<mozAutoDocUpdate> mUpdateBatch; 55 }; 56 57 /** 58 * Class DOMSVGTransformList 59 * 60 * This class is used to create the DOM tearoff objects that wrap internal 61 * SVGTransformList objects. 62 * 63 * See the architecture comment in DOMSVGAnimatedTransformList.h. 64 */ 65 class DOMSVGTransformList final : public nsISupports, public nsWrapperCache { 66 template <class T> 67 friend class AutoChangeTransformListNotifier; 68 friend class dom::DOMSVGTransform; 69 70 ~DOMSVGTransformList() { 71 // Our mAList's weak ref to us must be nulled out when we die. If GC has 72 // unlinked us using the cycle collector code, then that has already 73 // happened, and mAList is null. 74 if (mAList) { 75 (IsAnimValList() ? mAList->mAnimVal : mAList->mBaseVal) = nullptr; 76 } 77 } 78 79 public: 80 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 81 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMSVGTransformList) 82 83 DOMSVGTransformList(dom::DOMSVGAnimatedTransformList* aAList, 84 const SVGTransformList& aInternalList) 85 : mAList(aAList) { 86 // aInternalList must be passed in explicitly because we can't use 87 // InternalList() here. (Because it depends on IsAnimValList, which depends 88 // on this object having been assigned to aAList's mBaseVal or mAnimVal, 89 // which hasn't happened yet.) 90 91 InternalListLengthWillChange(aInternalList.Length()); // Sync mItems 92 } 93 94 JSObject* WrapObject(JSContext* cx, 95 JS::Handle<JSObject*> aGivenProto) override; 96 97 nsISupports* GetParentObject() { return static_cast<nsIContent*>(Element()); } 98 99 /** 100 * This will normally be the same as InternalList().Length(), except if we've 101 * hit OOM in which case our length will be zero. 102 */ 103 uint32_t LengthNoFlush() const { 104 MOZ_ASSERT(mItems.IsEmpty() || mItems.Length() == InternalList().Length(), 105 "DOM wrapper's list length is out of sync"); 106 return mItems.Length(); 107 } 108 109 /// Called to notify us to synchronize our length and detach excess items. 110 void InternalListLengthWillChange(uint32_t aNewLength); 111 112 /* 113 * List classes always have an owner. We need this so that templates that work 114 * on lists and elements can check ownership where elements may be unowned. 115 */ 116 bool HasOwner() const { return true; } 117 118 /** 119 * Returns true if our attribute is animating (in which case our animVal is 120 * not simply a mirror of our baseVal). 121 */ 122 bool IsAnimating() const { return mAList->IsAnimating(); } 123 /** 124 * Returns true if there is an animated list mirroring the base list. 125 */ 126 bool AnimListMirrorsBaseList() const { 127 return mAList->mAnimVal && !mAList->IsAnimating(); 128 } 129 130 uint32_t NumberOfItems() const { 131 if (IsAnimValList()) { 132 Element()->FlushAnimations(); 133 } 134 return LengthNoFlush(); 135 } 136 void Clear(ErrorResult& error); 137 already_AddRefed<dom::DOMSVGTransform> Initialize( 138 dom::DOMSVGTransform& newItem, ErrorResult& error); 139 already_AddRefed<dom::DOMSVGTransform> GetItem(uint32_t index, 140 ErrorResult& error); 141 already_AddRefed<dom::DOMSVGTransform> IndexedGetter(uint32_t index, 142 bool& found, 143 ErrorResult& error); 144 already_AddRefed<dom::DOMSVGTransform> InsertItemBefore( 145 dom::DOMSVGTransform& newItem, uint32_t index, ErrorResult& error); 146 already_AddRefed<dom::DOMSVGTransform> ReplaceItem( 147 dom::DOMSVGTransform& newItem, uint32_t index, ErrorResult& error); 148 already_AddRefed<dom::DOMSVGTransform> RemoveItem(uint32_t index, 149 ErrorResult& error); 150 already_AddRefed<dom::DOMSVGTransform> AppendItem( 151 dom::DOMSVGTransform& newItem, ErrorResult& error) { 152 return InsertItemBefore(newItem, LengthNoFlush(), error); 153 } 154 already_AddRefed<dom::DOMSVGTransform> CreateSVGTransformFromMatrix( 155 const DOMMatrix2DInit& aMatrix, ErrorResult& aRv); 156 already_AddRefed<dom::DOMSVGTransform> Consolidate(ErrorResult& error); 157 uint32_t Length() const { return NumberOfItems(); } 158 159 private: 160 dom::SVGElement* Element() const { return mAList->mElement; } 161 162 /// Used to determine if this list is the baseVal or animVal list. 163 bool IsAnimValList() const { 164 MOZ_ASSERT(this == mAList->mBaseVal || this == mAList->mAnimVal, 165 "Calling IsAnimValList() too early?!"); 166 return this == mAList->mAnimVal; 167 } 168 169 /** 170 * Get a reference to this object's corresponding internal SVGTransformList. 171 * 172 * To simplify the code we just have this one method for obtaining both 173 * baseVal and animVal internal lists. This means that animVal lists don't 174 * get const protection, but our setter methods guard against changing 175 * animVal lists. 176 */ 177 SVGTransformList& InternalList() const; 178 179 /// Returns the DOMSVGTransform at aIndex, creating it if necessary. 180 already_AddRefed<dom::DOMSVGTransform> GetItemAt(uint32_t aIndex); 181 182 void MaybeInsertNullInAnimValListAt(uint32_t aIndex); 183 void MaybeRemoveItemFromAnimValListAt(uint32_t aIndex); 184 185 // Weak refs to our DOMSVGTransform items. The items are friends and take care 186 // of clearing our pointer to them when they die. 187 FallibleTArray<dom::DOMSVGTransform*> mItems; 188 189 RefPtr<dom::DOMSVGAnimatedTransformList> mAList; 190 }; 191 192 } // namespace dom 193 } // namespace mozilla 194 195 #endif // DOM_SVG_DOMSVGTRANSFORMLIST_H_