Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / animation / DocumentTimeline.h
1 /*
2  * Copyright (C) 2013 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef DocumentTimeline_h
32 #define DocumentTimeline_h
33
34 #include "core/animation/AnimationEffect.h"
35 #include "core/animation/AnimationPlayer.h"
36 #include "core/dom/Element.h"
37 #include "core/events/Event.h"
38 #include "platform/Timer.h"
39 #include "wtf/RefCounted.h"
40 #include "wtf/RefPtr.h"
41 #include "wtf/Vector.h"
42
43 namespace WebCore {
44
45 class Document;
46 class TimedItem;
47
48 // DocumentTimeline is constructed and owned by Document, and tied to its lifecycle.
49 class DocumentTimeline : public RefCounted<DocumentTimeline> {
50 public:
51     class PlatformTiming {
52
53     public:
54         // Calls DocumentTimeline's wake() method after duration seconds.
55         virtual void wakeAfter(double duration) = 0;
56         virtual void cancelWake() = 0;
57         virtual void serviceOnNextFrame() = 0;
58         virtual ~PlatformTiming() { }
59
60     };
61
62     static PassRefPtr<DocumentTimeline> create(Document*, PassOwnPtr<PlatformTiming> = nullptr);
63     ~DocumentTimeline();
64
65     void serviceAnimations();
66
67     // Creates a player attached to this timeline, but without a start time.
68     AnimationPlayer* createAnimationPlayer(TimedItem*);
69     AnimationPlayer* play(TimedItem*);
70
71     void playerDestroyed(AnimationPlayer* player)
72     {
73         ASSERT(m_players.contains(player));
74         m_players.remove(player);
75     }
76
77     // Called from setReadyState() in Document.cpp to set m_zeroTime to
78     // performance.timing.domInteractive
79     void setZeroTime(double);
80     bool hasStarted() const { return !isNull(m_zeroTime); }
81     bool hasPendingUpdates() const { return !m_playersNeedingUpdate.isEmpty(); }
82     double zeroTime() const { return m_zeroTime; }
83     double currentTime();
84     double effectiveTime();
85     void pauseAnimationsForTesting(double);
86     size_t numberOfActiveAnimationsForTesting() const;
87
88     void setOutdatedAnimationPlayer(AnimationPlayer*);
89     bool hasOutdatedAnimationPlayer() const { return m_hasOutdatedAnimationPlayer; }
90
91     Document* document() { return m_document; }
92     void detachFromDocument();
93
94 protected:
95     DocumentTimeline(Document*, PassOwnPtr<PlatformTiming>);
96
97 private:
98     double m_zeroTime;
99     Document* m_document;
100     // AnimationPlayers which will be updated on the next frame
101     // i.e. current, in effect, or had timing changed
102     HashSet<RefPtr<AnimationPlayer> > m_playersNeedingUpdate;
103     HashSet<AnimationPlayer*> m_players;
104     bool m_hasOutdatedAnimationPlayer;
105
106     void wake();
107
108     friend class SMILTimeContainer;
109     static const double s_minimumDelay;
110
111     OwnPtr<PlatformTiming> m_timing;
112
113     class DocumentTimelineTiming FINAL : public PlatformTiming {
114     public:
115         DocumentTimelineTiming(DocumentTimeline* documentTimeline)
116             : m_timeline(documentTimeline)
117             , m_timer(this, &DocumentTimelineTiming::timerFired)
118         {
119             ASSERT(m_timeline);
120         }
121
122         virtual void wakeAfter(double duration) OVERRIDE;
123         virtual void cancelWake() OVERRIDE;
124         virtual void serviceOnNextFrame() OVERRIDE;
125
126         void timerFired(Timer<DocumentTimelineTiming>*) { m_timeline->wake(); }
127
128     private:
129         DocumentTimeline* m_timeline;
130         Timer<DocumentTimelineTiming> m_timer;
131
132     };
133
134     friend class AnimationDocumentTimelineTest;
135 };
136
137 } // namespace
138
139 #endif