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/Player.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; ~PlatformTiming()58 virtual ~PlatformTiming() { } 59 60 }; 61 62 static PassRefPtr<DocumentTimeline> create(Document*, PassOwnPtr<PlatformTiming> = nullptr); 63 // Returns whether style recalc was triggered. 64 bool serviceAnimations(); 65 66 // Creates a player attached to this timeline, but without a start time. 67 Player* createPlayer(TimedItem*); 68 Player* play(TimedItem*); 69 70 // Called from setReadyState() in Document.cpp to set m_zeroTime to 71 // performance.timing.domInteractive 72 void setZeroTime(double); hasStarted()73 bool hasStarted() const { return !isNull(m_zeroTime); } zeroTime()74 double zeroTime() const { return m_zeroTime; } 75 double currentTime(); 76 void pauseAnimationsForTesting(double); 77 size_t numberOfActiveAnimationsForTesting() const; players()78 const Vector<RefPtr<Player> >& players() const { return m_players; } 79 addEventToDispatch(EventTarget * target,PassRefPtr<Event> event)80 void addEventToDispatch(EventTarget* target, PassRefPtr<Event> event) 81 { 82 m_events.append(EventToDispatch(target, event)); 83 } 84 85 void dispatchEvents(); 86 void dispatchEventsAsync(); 87 88 protected: 89 DocumentTimeline(Document*, PassOwnPtr<PlatformTiming>); 90 91 private: 92 double m_zeroTime; 93 Document* m_document; 94 Timer<DocumentTimeline> m_eventDistpachTimer; 95 Vector<RefPtr<Player> > m_players; 96 97 void eventDispatchTimerFired(Timer<DocumentTimeline>*); 98 void wake(); 99 100 struct EventToDispatch { EventToDispatchEventToDispatch101 EventToDispatch(EventTarget* target, PassRefPtr<Event> event) 102 : target(target) 103 , event(event) 104 { 105 } 106 RefPtr<EventTarget> target; 107 RefPtr<Event> event; 108 }; 109 Vector<EventToDispatch> m_events; 110 111 static const double s_minimumDelay; 112 113 OwnPtr<PlatformTiming> m_timing; 114 115 class DocumentTimelineTiming : public PlatformTiming { 116 public: DocumentTimelineTiming(DocumentTimeline * documentTimeline)117 DocumentTimelineTiming(DocumentTimeline* documentTimeline) 118 : m_timeline(documentTimeline) 119 , m_timer(this, &DocumentTimelineTiming::timerFired) 120 { 121 ASSERT(m_timeline); 122 } 123 124 virtual void wakeAfter(double duration) OVERRIDE; 125 virtual void cancelWake() OVERRIDE; 126 virtual void serviceOnNextFrame() OVERRIDE; 127 timerFired(Timer<DocumentTimelineTiming> *)128 void timerFired(Timer<DocumentTimelineTiming>*) { m_timeline->wake(); } 129 130 private: 131 DocumentTimeline* m_timeline; 132 Timer<DocumentTimelineTiming> m_timer; 133 134 }; 135 136 friend class AnimationDocumentTimelineTest; 137 }; 138 139 } // namespace 140 141 #endif 142