1/*
2 * Copyright (C) 2009 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 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef AnimationControllerPrivate_h
30#define AnimationControllerPrivate_h
31
32#include "AnimationBase.h"
33#include "CSSPropertyNames.h"
34#include "Timer.h"
35#include <wtf/HashMap.h>
36#include <wtf/HashSet.h>
37#include <wtf/PassRefPtr.h>
38#include <wtf/RefPtr.h>
39#include <wtf/Vector.h>
40#include <wtf/text/AtomicString.h>
41#include <wtf/text/WTFString.h>
42
43namespace WebCore {
44
45class AnimationBase;
46class CompositeAnimation;
47class Document;
48class Element;
49class Frame;
50class RenderElement;
51class RenderStyle;
52
53enum SetChanged {
54    DoNotCallSetChanged = 0,
55    CallSetChanged = 1
56};
57
58class AnimationControllerPrivate {
59    WTF_MAKE_NONCOPYABLE(AnimationControllerPrivate); WTF_MAKE_FAST_ALLOCATED;
60public:
61    explicit AnimationControllerPrivate(Frame&);
62    ~AnimationControllerPrivate();
63
64    // Returns the time until the next animation needs to be serviced, or -1 if there are none.
65    double updateAnimations(SetChanged callSetChanged = DoNotCallSetChanged);
66    void updateAnimationTimer(SetChanged callSetChanged = DoNotCallSetChanged);
67
68    CompositeAnimation& ensureCompositeAnimation(RenderElement*);
69    bool clear(RenderElement*);
70
71    void updateStyleIfNeededDispatcherFired(Timer<AnimationControllerPrivate>&);
72    void startUpdateStyleIfNeededDispatcher();
73    void addEventToDispatch(PassRefPtr<Element> element, const AtomicString& eventType, const String& name, double elapsedTime);
74    void addElementChangeToDispatch(PassRef<Element>);
75
76    bool hasAnimations() const { return !m_compositeAnimations.isEmpty(); }
77
78    bool isSuspended() const { return m_isSuspended; }
79    void suspendAnimations();
80    void resumeAnimations();
81#if ENABLE(REQUEST_ANIMATION_FRAME)
82    void animationFrameCallbackFired();
83#endif
84
85    void suspendAnimationsForDocument(Document*);
86    void resumeAnimationsForDocument(Document*);
87    void startAnimationsIfNotSuspended(Document*);
88
89    bool isRunningAnimationOnRenderer(RenderElement*, CSSPropertyID, AnimationBase::RunningState) const;
90    bool isRunningAcceleratedAnimationOnRenderer(RenderElement*, CSSPropertyID, AnimationBase::RunningState) const;
91
92    bool pauseAnimationAtTime(RenderElement*, const AtomicString& name, double t);
93    bool pauseTransitionAtTime(RenderElement*, const String& property, double t);
94    unsigned numberOfActiveAnimations(Document*) const;
95
96    PassRefPtr<RenderStyle> getAnimatedStyleForRenderer(RenderElement* renderer);
97
98    double beginAnimationUpdateTime();
99    void setBeginAnimationUpdateTime(double t) { m_beginAnimationUpdateTime = t; }
100    void endAnimationUpdate();
101    void receivedStartTimeResponse(double);
102
103    void addToAnimationsWaitingForStyle(AnimationBase*);
104    void removeFromAnimationsWaitingForStyle(AnimationBase*);
105
106    void addToAnimationsWaitingForStartTimeResponse(AnimationBase*, bool willGetResponse);
107    void removeFromAnimationsWaitingForStartTimeResponse(AnimationBase*);
108
109    void animationWillBeRemoved(AnimationBase*);
110
111    void updateAnimationTimerForRenderer(RenderElement*);
112
113    bool allowsNewAnimationsWhileSuspended() const { return m_allowsNewAnimationsWhileSuspended; }
114    void setAllowsNewAnimationsWhileSuspended(bool);
115
116private:
117    void animationTimerFired(Timer<AnimationControllerPrivate>&);
118
119    void styleAvailable();
120    void fireEventsAndUpdateStyle();
121    void startTimeResponse(double t);
122
123    HashMap<RenderElement*, RefPtr<CompositeAnimation>> m_compositeAnimations;
124    Timer<AnimationControllerPrivate> m_animationTimer;
125    Timer<AnimationControllerPrivate> m_updateStyleIfNeededDispatcher;
126    Frame& m_frame;
127
128    class EventToDispatch {
129    public:
130        RefPtr<Element> element;
131        AtomicString eventType;
132        String name;
133        double elapsedTime;
134    };
135
136    Vector<EventToDispatch> m_eventsToDispatch;
137    Vector<Ref<Element>> m_elementChangesToDispatch;
138
139    double m_beginAnimationUpdateTime;
140
141    typedef HashSet<RefPtr<AnimationBase>> WaitingAnimationsSet;
142    WaitingAnimationsSet m_animationsWaitingForStyle;
143    WaitingAnimationsSet m_animationsWaitingForStartTimeResponse;
144    bool m_waitingForAsyncStartNotification;
145    bool m_isSuspended;
146
147    // Used to flag whether we should revert to previous buggy
148    // behavior of allowing new transitions and animations to
149    // run even when this object is suspended.
150    bool m_allowsNewAnimationsWhileSuspended;
151};
152
153} // namespace WebCore
154
155#endif // AnimationControllerPrivate_h
156