SVGAnimatedString.h (4641B)
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_SVGANIMATEDSTRING_H_ 8 #define DOM_SVG_SVGANIMATEDSTRING_H_ 9 10 #include "mozilla/Attributes.h" 11 #include "mozilla/SMILAttr.h" 12 #include "mozilla/SVGAnimatedClassOrString.h" 13 #include "mozilla/UniquePtr.h" 14 #include "mozilla/dom/SVGElement.h" 15 #include "nsError.h" 16 17 namespace mozilla { 18 19 class ErrorResult; 20 class SMILValue; 21 22 namespace dom { 23 class SVGElement; 24 class OwningTrustedScriptURLOrString; 25 class TrustedScriptURLOrString; 26 } // namespace dom 27 28 class SVGAnimatedString : public SVGAnimatedClassOrString { 29 public: 30 using SVGElement = dom::SVGElement; 31 using OwningTrustedScriptURLOrString = dom::OwningTrustedScriptURLOrString; 32 using TrustedScriptURLOrString = dom::TrustedScriptURLOrString; 33 34 void Init(uint8_t aAttrEnum) { 35 mAnimVal = nullptr; 36 mAttrEnum = aAttrEnum; 37 mIsBaseSet = false; 38 } 39 40 void SetBaseValue(const nsAString& aValue, SVGElement* aSVGElement, 41 bool aDoSetAttr) override; 42 MOZ_CAN_RUN_SCRIPT void SetBaseValue(const TrustedScriptURLOrString& aValue, 43 SVGElement* aSVGElement, bool aDoSetAttr, 44 nsIPrincipal* aSubjectPrincipal, 45 ErrorResult& aRv) override { 46 SVGAnimatedClassOrString::SetBaseValue(aValue, aSVGElement, aDoSetAttr, 47 aSubjectPrincipal, aRv); 48 } 49 void GetBaseValue(nsAString& aValue, 50 const SVGElement* aSVGElement) const override { 51 aSVGElement->GetStringBaseValue(mAttrEnum, aValue); 52 } 53 54 void SetAnimValue(const nsAString& aValue, SVGElement* aSVGElement); 55 void GetAnimValue(nsAString& aResult, 56 const SVGElement* aSVGElement) const override; 57 58 // Returns true if the animated value of this string has been explicitly 59 // set (either by animation, or by taking on the base value which has been 60 // explicitly set by markup or a DOM call), false otherwise. 61 // If this returns false, the animated value is still valid, that is, 62 // usable, and represents the default base value of the attribute. 63 bool IsExplicitlySet() const { return !!mAnimVal || mIsBaseSet; } 64 65 UniquePtr<SMILAttr> ToSMILAttr(SVGElement* aSVGElement); 66 67 SVGAnimatedString() = default; 68 69 SVGAnimatedString& operator=(const SVGAnimatedString& aOther) { 70 mAttrEnum = aOther.mAttrEnum; 71 mIsBaseSet = aOther.mIsBaseSet; 72 if (aOther.mAnimVal) { 73 mAnimVal = MakeUnique<nsString>(*aOther.mAnimVal); 74 } 75 return *this; 76 } 77 78 SVGAnimatedString(const SVGAnimatedString& aOther) : SVGAnimatedString() { 79 *this = aOther; 80 } 81 82 private: 83 // FIXME: Should probably use void string rather than UniquePtr<nsString>. 84 UniquePtr<nsString> mAnimVal; 85 uint8_t mAttrEnum = 0; // element specified tracking for attribute 86 bool mIsBaseSet = false; 87 88 public: 89 struct SMILString : public SMILAttr { 90 public: 91 SMILString(SVGAnimatedString* aVal, SVGElement* aSVGElement) 92 : mVal(aVal), mSVGElement(aSVGElement) {} 93 94 // These will stay alive because a SMILAttr only lives as long 95 // as the Compositing step, and DOM elements don't get a chance to 96 // die during that. 97 SVGAnimatedString* mVal; 98 SVGElement* mSVGElement; 99 100 // SMILAttr methods 101 nsresult ValueFromString(const nsAString& aStr, 102 const dom::SVGAnimationElement* aSrcElement, 103 SMILValue& aValue, 104 bool& aPreventCachingOfSandwich) const override; 105 SMILValue GetBaseValue() const override; 106 void ClearAnimValue() override; 107 nsresult SetAnimValue(const SMILValue& aValue) override; 108 }; 109 }; 110 111 class SVGAnimatedScriptHrefString final : public SVGAnimatedString { 112 public: 113 using SVGElement = dom::SVGElement; 114 using OwningTrustedScriptURLOrString = dom::OwningTrustedScriptURLOrString; 115 using TrustedScriptURLOrString = dom::TrustedScriptURLOrString; 116 MOZ_CAN_RUN_SCRIPT void SetBaseValue(const TrustedScriptURLOrString& aValue, 117 SVGElement* aSVGElement, bool aDoSetAttr, 118 nsIPrincipal* aSubjectPrincipal, 119 ErrorResult& aRv) override; 120 SVGAnimatedScriptHrefString() = default; 121 }; 122 123 } // namespace mozilla 124 125 #endif // DOM_SVG_SVGANIMATEDSTRING_H_