Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / StringKeyframe.cpp
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/animation/StringKeyframe.h"
7
8 #include "core/animation/Interpolation.h"
9 #include "core/animation/css/CSSAnimations.h"
10 #include "core/css/resolver/StyleResolver.h"
11 #include "core/rendering/style/RenderStyle.h"
12
13 namespace WebCore {
14
15 StringKeyframe::StringKeyframe(const StringKeyframe& copyFrom)
16     : Keyframe(copyFrom.m_offset, copyFrom.m_composite, copyFrom.m_easing)
17     , m_propertySet(copyFrom.m_propertySet->mutableCopy())
18 {
19 }
20
21 void StringKeyframe::setPropertyValue(CSSPropertyID property, const String& value, StyleSheetContents* styleSheetContents)
22 {
23     ASSERT(property != CSSPropertyInvalid);
24     if (CSSAnimations::isAllowedAnimation(property))
25         m_propertySet->setProperty(property, value, false, styleSheetContents);
26 }
27
28 PropertySet StringKeyframe::properties() const
29 {
30     // This is not used in time-critical code, so we probably don't need to
31     // worry about caching this result.
32     PropertySet properties;
33     for (unsigned i = 0; i < m_propertySet->propertyCount(); ++i)
34         properties.add(m_propertySet->propertyAt(i).id());
35     return properties;
36 }
37
38 PassRefPtrWillBeRawPtr<Keyframe> StringKeyframe::clone() const
39 {
40     return adoptRefWillBeNoop(new StringKeyframe(*this));
41 }
42 PassOwnPtrWillBeRawPtr<Keyframe::PropertySpecificKeyframe> StringKeyframe::createPropertySpecificKeyframe(CSSPropertyID property) const
43 {
44     return adoptPtrWillBeNoop(new PropertySpecificKeyframe(offset(), easing(), propertyValue(property), composite()));
45 }
46
47 void StringKeyframe::trace(Visitor* visitor)
48 {
49     visitor->trace(m_propertySet);
50     Keyframe::trace(visitor);
51 }
52
53 StringKeyframe::PropertySpecificKeyframe::PropertySpecificKeyframe(double offset, PassRefPtr<TimingFunction> easing, CSSValue* value, AnimationEffect::CompositeOperation op)
54     : Keyframe::PropertySpecificKeyframe(offset, easing, op)
55     , m_value(value)
56 { }
57
58 StringKeyframe::PropertySpecificKeyframe::PropertySpecificKeyframe(double offset, PassRefPtr<TimingFunction> easing, CSSValue* value)
59     : Keyframe::PropertySpecificKeyframe(offset, easing, AnimationEffect::CompositeReplace)
60     , m_value(value)
61 {
62     ASSERT(!isNull(m_offset));
63 }
64
65 PassRefPtrWillBeRawPtr<Interpolation> StringKeyframe::PropertySpecificKeyframe::createInterpolation(CSSPropertyID property, Keyframe::PropertySpecificKeyframe* end, Element* element) const
66 {
67     CSSValue* fromCSSValue = m_value.get();
68     CSSValue* toCSSValue = toStringPropertySpecificKeyframe(end)->value();
69
70     if (!CSSAnimations::isAnimatableProperty(property))
71         return DefaultStyleInterpolation::create(fromCSSValue, toCSSValue, property);
72
73     switch (property) {
74     case CSSPropertyLeft:
75     case CSSPropertyRight:
76     case CSSPropertyWidth:
77     case CSSPropertyHeight:
78         if (LengthStyleInterpolation::canCreateFrom(*fromCSSValue) && LengthStyleInterpolation::canCreateFrom(*toCSSValue))
79             return LengthStyleInterpolation::create(fromCSSValue, toCSSValue, property);
80         break;
81     default:
82         break;
83     }
84
85     // FIXME: Remove the use of AnimatableValues, RenderStyles and Elements here.
86     // FIXME: Remove this cache
87     if (!m_animatableValueCache)
88         m_animatableValueCache = StyleResolver::createAnimatableValueSnapshot(*element, property, fromCSSValue);
89
90     RefPtrWillBeRawPtr<AnimatableValue> to = StyleResolver::createAnimatableValueSnapshot(*element, property, toCSSValue);
91     toStringPropertySpecificKeyframe(end)->m_animatableValueCache = to;
92
93     return LegacyStyleInterpolation::create(m_animatableValueCache.get(), to.release(), property);
94 }
95
96 PassOwnPtrWillBeRawPtr<Keyframe::PropertySpecificKeyframe> StringKeyframe::PropertySpecificKeyframe::neutralKeyframe(double offset, PassRefPtr<TimingFunction> easing) const
97 {
98     return adoptPtrWillBeNoop(new PropertySpecificKeyframe(offset, easing, 0, AnimationEffect::CompositeAdd));
99 }
100
101 PassOwnPtrWillBeRawPtr<Keyframe::PropertySpecificKeyframe> StringKeyframe::PropertySpecificKeyframe::cloneWithOffset(double offset) const
102 {
103     Keyframe::PropertySpecificKeyframe* theClone = new PropertySpecificKeyframe(offset, m_easing, m_value.get());
104     toStringPropertySpecificKeyframe(theClone)->m_animatableValueCache = m_animatableValueCache;
105     return adoptPtrWillBeNoop(theClone);
106 }
107
108 void StringKeyframe::PropertySpecificKeyframe::trace(Visitor* visitor)
109 {
110     visitor->trace(m_value);
111     visitor->trace(m_animatableValueCache);
112     Keyframe::PropertySpecificKeyframe::trace(visitor);
113 }
114
115 }