1/*
2 * Copyright (C) 2017-2018 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 "ActiveDOMObject.h"
29#include "DOMPromiseProxy.h"
30#include "EventTarget.h"
31#include "ExceptionOr.h"
32#include "WebAnimationUtilities.h"
33#include <wtf/Markable.h>
34#include <wtf/RefCounted.h>
35#include <wtf/Seconds.h>
36#include <wtf/UniqueRef.h>
37#include <wtf/WeakPtr.h>
38
39namespace WebCore {
40
41class AnimationEffect;
42class AnimationPlaybackEvent;
43class AnimationTimeline;
44class Document;
45class Element;
46class RenderStyle;
47
48class WebAnimation : public RefCounted<WebAnimation>, public CanMakeWeakPtr<WebAnimation>, public EventTargetWithInlineData, public ActiveDOMObject {
49 WTF_MAKE_ISO_ALLOCATED(WebAnimation);
50public:
51 static Ref<WebAnimation> create(Document&, AnimationEffect*);
52 static Ref<WebAnimation> create(Document&, AnimationEffect*, AnimationTimeline*);
53 ~WebAnimation();
54
55 virtual bool isDeclarativeAnimation() const { return false; }
56 virtual bool isCSSAnimation() const { return false; }
57 virtual bool isCSSTransition() const { return false; }
58
59 const String& id() const { return m_id; }
60 void setId(const String& id) { m_id = id; }
61
62 AnimationEffect* effect() const { return m_effect.get(); }
63 void setEffect(RefPtr<AnimationEffect>&&);
64 AnimationTimeline* timeline() const { return m_timeline.get(); }
65 virtual void setTimeline(RefPtr<AnimationTimeline>&&);
66
67 Optional<Seconds> currentTime() const;
68 ExceptionOr<void> setCurrentTime(Optional<Seconds>);
69
70 enum class Silently : uint8_t { Yes, No };
71 double playbackRate() const { return m_playbackRate + 0; }
72 void setPlaybackRate(double);
73
74 enum class PlayState : uint8_t { Idle, Running, Paused, Finished };
75 PlayState playState() const;
76
77 bool pending() const { return hasPendingPauseTask() || hasPendingPlayTask(); }
78
79 using ReadyPromise = DOMPromiseProxyWithResolveCallback<IDLInterface<WebAnimation>>;
80 ReadyPromise& ready() { return m_readyPromise.get(); }
81
82 using FinishedPromise = DOMPromiseProxyWithResolveCallback<IDLInterface<WebAnimation>>;
83 FinishedPromise& finished() { return m_finishedPromise.get(); }
84
85 virtual void cancel();
86 void cancel(Silently);
87 ExceptionOr<void> finish();
88 ExceptionOr<void> play();
89 void updatePlaybackRate(double);
90 ExceptionOr<void> pause();
91 ExceptionOr<void> reverse();
92
93 virtual Optional<double> startTime() const;
94 virtual void setStartTime(Optional<double>);
95 virtual Optional<double> bindingsCurrentTime() const;
96 virtual ExceptionOr<void> setBindingsCurrentTime(Optional<double>);
97 virtual PlayState bindingsPlayState() const { return playState(); }
98 virtual bool bindingsPending() const { return pending(); }
99 virtual ReadyPromise& bindingsReady() { return ready(); }
100 virtual FinishedPromise& bindingsFinished() { return finished(); }
101 virtual ExceptionOr<void> bindingsPlay() { return play(); }
102 virtual ExceptionOr<void> bindingsPause() { return pause(); }
103
104 virtual bool needsTick() const;
105 virtual void tick();
106 Seconds timeToNextTick() const;
107 virtual void resolve(RenderStyle&);
108 void effectTargetDidChange(Element* previousTarget, Element* newTarget);
109 void acceleratedStateDidChange();
110 void applyPendingAcceleratedActions();
111
112 bool isRunningAccelerated() const;
113 bool isRelevant() const { return m_isRelevant; }
114 void effectTimingDidChange();
115 void suspendEffectInvalidation();
116 void unsuspendEffectInvalidation();
117 void setSuspended(bool);
118 bool isSuspended() const { return m_isSuspended; }
119 virtual void remove();
120
121 bool hasPendingActivity() const final;
122
123 using RefCounted::ref;
124 using RefCounted::deref;
125
126protected:
127 explicit WebAnimation(Document&);
128
129 void stop() override;
130
131private:
132 enum class DidSeek : uint8_t { Yes, No };
133 enum class SynchronouslyNotify : uint8_t { Yes, No };
134 enum class RespectHoldTime : uint8_t { Yes, No };
135 enum class AutoRewind : uint8_t { Yes, No };
136 enum class TimeToRunPendingTask : uint8_t { NotScheduled, ASAP, WhenReady };
137
138 void timingDidChange(DidSeek, SynchronouslyNotify);
139 void updateFinishedState(DidSeek, SynchronouslyNotify);
140 void enqueueAnimationPlaybackEvent(const AtomString&, Optional<Seconds>, Optional<Seconds>);
141 Seconds effectEndTime() const;
142 WebAnimation& readyPromiseResolve();
143 WebAnimation& finishedPromiseResolve();
144 Optional<Seconds> currentTime(RespectHoldTime) const;
145 ExceptionOr<void> silentlySetCurrentTime(Optional<Seconds>);
146 void finishNotificationSteps();
147 bool hasPendingPauseTask() const { return m_timeToRunPendingPauseTask != TimeToRunPendingTask::NotScheduled; }
148 bool hasPendingPlayTask() const { return m_timeToRunPendingPlayTask != TimeToRunPendingTask::NotScheduled; }
149 ExceptionOr<void> play(AutoRewind);
150 void runPendingPauseTask();
151 void runPendingPlayTask();
152 void resetPendingTasks(Silently = Silently::No);
153 void setEffectInternal(RefPtr<AnimationEffect>&&, bool = false);
154 void setTimelineInternal(RefPtr<AnimationTimeline>&&);
155 bool isEffectInvalidationSuspended() { return m_suspendCount; }
156 bool computeRelevance();
157 void updateRelevance();
158 void invalidateEffect();
159 double effectivePlaybackRate() const;
160 void applyPendingPlaybackRate();
161
162 RefPtr<AnimationEffect> m_effect;
163 RefPtr<AnimationTimeline> m_timeline;
164 UniqueRef<ReadyPromise> m_readyPromise;
165 UniqueRef<FinishedPromise> m_finishedPromise;
166 Markable<Seconds, Seconds::MarkableTraits> m_previousCurrentTime;
167 Markable<Seconds, Seconds::MarkableTraits> m_startTime;
168 Markable<Seconds, Seconds::MarkableTraits> m_holdTime;
169 MarkableDouble m_pendingPlaybackRate;
170 double m_playbackRate { 1 };
171 String m_id;
172
173 int m_suspendCount { 0 };
174
175 bool m_isStopped { false };
176 bool m_isSuspended { false };
177 bool m_finishNotificationStepsMicrotaskPending;
178 bool m_isRelevant;
179 bool m_shouldSkipUpdatingFinishedStateWhenResolving;
180 TimeToRunPendingTask m_timeToRunPendingPlayTask { TimeToRunPendingTask::NotScheduled };
181 TimeToRunPendingTask m_timeToRunPendingPauseTask { TimeToRunPendingTask::NotScheduled };
182
183 // ActiveDOMObject.
184 const char* activeDOMObjectName() const final;
185 bool canSuspendForDocumentSuspension() const final;
186
187 // EventTarget
188 EventTargetInterface eventTargetInterface() const final { return WebAnimationEventTargetInterfaceType; }
189 void refEventTarget() final { ref(); }
190 void derefEventTarget() final { deref(); }
191 ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
192};
193
194} // namespace WebCore
195
196#define SPECIALIZE_TYPE_TRAITS_WEB_ANIMATION(ToValueTypeName, predicate) \
197SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::ToValueTypeName) \
198static bool isType(const WebCore::WebAnimation& value) { return value.predicate; } \
199SPECIALIZE_TYPE_TRAITS_END()
200