| 1 | /* |
| 2 | * Copyright (C) 2019 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "SVGAnimatedProperty.h" |
| 29 | |
| 30 | namespace WebCore { |
| 31 | |
| 32 | template<typename ListType> |
| 33 | class SVGAnimatedPropertyList : public SVGAnimatedProperty { |
| 34 | public: |
| 35 | template<typename... Arguments> |
| 36 | static Ref<SVGAnimatedPropertyList> create(SVGElement* contextElement, Arguments&&... arguments) |
| 37 | { |
| 38 | return adoptRef(*new SVGAnimatedPropertyList(contextElement, std::forward<Arguments>(arguments)...)); |
| 39 | } |
| 40 | |
| 41 | ~SVGAnimatedPropertyList() |
| 42 | { |
| 43 | m_baseVal->detach(); |
| 44 | if (m_animVal) |
| 45 | m_animVal->detach(); |
| 46 | } |
| 47 | |
| 48 | // Used by the DOM. |
| 49 | const Ref<ListType>& baseVal() const { return m_baseVal; } |
| 50 | |
| 51 | // Used by SVGElement::parseAttribute(). |
| 52 | Ref<ListType>& baseVal() { return m_baseVal; } |
| 53 | |
| 54 | // Used by the DOM. |
| 55 | const RefPtr<ListType>& animVal() const { return const_cast<SVGAnimatedPropertyList*>(this)->ensureAnimVal(); } |
| 56 | |
| 57 | // Called by SVGAnimatedPropertyAnimator to pass the animVal to the SVGAnimationFunction::progress. |
| 58 | RefPtr<ListType>& animVal() { return ensureAnimVal(); } |
| 59 | |
| 60 | // Used when committing a change from the SVGAnimatedProperty to the attribute. |
| 61 | String baseValAsString() const override { return m_baseVal->valueAsString(); } |
| 62 | |
| 63 | // Used to apply the SVGAnimator change to the target element. |
| 64 | String animValAsString() const override |
| 65 | { |
| 66 | ASSERT(isAnimating()); |
| 67 | return m_animVal->valueAsString(); |
| 68 | } |
| 69 | |
| 70 | // Managing the relationship with the owner. |
| 71 | void setDirty() override { m_baseVal->setDirty(); } |
| 72 | bool isDirty() const override { return m_baseVal->isDirty(); } |
| 73 | Optional<String> synchronize() override { return m_baseVal->synchronize(); } |
| 74 | |
| 75 | // Used by RenderSVGElements and DumpRenderTree. |
| 76 | const ListType& currentValue() const |
| 77 | { |
| 78 | ASSERT_IMPLIES(isAnimating(), m_animVal); |
| 79 | return isAnimating() ? *m_animVal : m_baseVal.get(); |
| 80 | } |
| 81 | |
| 82 | // Controlling the animation. |
| 83 | void startAnimation() override |
| 84 | { |
| 85 | if (m_animVal) |
| 86 | *m_animVal = m_baseVal; |
| 87 | else |
| 88 | ensureAnimVal(); |
| 89 | SVGAnimatedProperty::startAnimation(); |
| 90 | } |
| 91 | |
| 92 | void stopAnimation() override |
| 93 | { |
| 94 | if (m_animVal) |
| 95 | *m_animVal = m_baseVal; |
| 96 | SVGAnimatedProperty::stopAnimation(); |
| 97 | } |
| 98 | |
| 99 | // Controlling the instance animation. |
| 100 | void instanceStartAnimation(SVGAnimatedProperty& animated) override |
| 101 | { |
| 102 | m_animVal = static_cast<SVGAnimatedPropertyList&>(animated).animVal(); |
| 103 | SVGAnimatedProperty::instanceStartAnimation(animated); |
| 104 | } |
| 105 | |
| 106 | void instanceStopAnimation() override |
| 107 | { |
| 108 | m_animVal = nullptr; |
| 109 | SVGAnimatedProperty::instanceStopAnimation(); |
| 110 | } |
| 111 | |
| 112 | // Visual Studio doesn't seem to see these private constructors from subclasses. |
| 113 | // FIXME: See what it takes to remove this hack. |
| 114 | #if !COMPILER(MSVC) |
| 115 | protected: |
| 116 | #endif |
| 117 | template<typename... Arguments> |
| 118 | SVGAnimatedPropertyList(SVGElement* contextElement, Arguments&&... arguments) |
| 119 | : SVGAnimatedProperty(contextElement) |
| 120 | , m_baseVal(ListType::create(this, SVGPropertyAccess::ReadWrite, std::forward<Arguments>(arguments)...)) |
| 121 | { |
| 122 | } |
| 123 | |
| 124 | RefPtr<ListType>& ensureAnimVal() |
| 125 | { |
| 126 | if (!m_animVal) |
| 127 | m_animVal = ListType::create(m_baseVal, SVGPropertyAccess::ReadOnly); |
| 128 | return m_animVal; |
| 129 | } |
| 130 | |
| 131 | // Called when m_baseVal changes or an item in m_baseVal changes. |
| 132 | void commitPropertyChange(SVGProperty* property) override |
| 133 | { |
| 134 | if (m_animVal) |
| 135 | *m_animVal = m_baseVal; |
| 136 | SVGAnimatedProperty::commitPropertyChange(property); |
| 137 | } |
| 138 | |
| 139 | Ref<ListType> m_baseVal; |
| 140 | mutable RefPtr<ListType> m_animVal; |
| 141 | }; |
| 142 | |
| 143 | } |
| 144 | |