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 TextTrackRegion_h 32 #define TextTrackRegion_h 33 34 #if ENABLE(WEBVTT_REGIONS) 35 36 #include "core/dom/ContextLifecycleObserver.h" 37 #include "core/dom/Document.h" 38 #include "core/html/track/TextTrack.h" 39 #include "core/platform/graphics/FloatPoint.h" 40 #include "wtf/PassOwnPtr.h" 41 #include "wtf/RefCounted.h" 42 43 namespace WebCore { 44 45 class ExceptionState; 46 class HTMLDivElement; 47 class TextTrackCueBox; 48 49 class TextTrackRegion : public RefCounted<TextTrackRegion>, public ContextLifecycleObserver { 50 public: 51 static PassRefPtr<TextTrackRegion> create(ScriptExecutionContext* context) 52 { 53 return adoptRef(new TextTrackRegion(context)); 54 } 55 56 virtual ~TextTrackRegion(); 57 58 TextTrack* track() const { return m_track; } 59 void setTrack(TextTrack*); 60 61 const String& id() const { return m_id; } 62 void setId(const String&); 63 64 double width() const { return m_width; } 65 void setWidth(double, ExceptionState&); 66 67 long height() const { return m_heightInLines; } 68 void setHeight(long, ExceptionState&); 69 70 double regionAnchorX() const { return m_regionAnchor.x(); } 71 void setRegionAnchorX(double, ExceptionState&); 72 73 double regionAnchorY() const { return m_regionAnchor.y(); } 74 void setRegionAnchorY(double, ExceptionState&); 75 76 double viewportAnchorX() const { return m_viewportAnchor.x(); } 77 void setViewportAnchorX(double, ExceptionState&); 78 79 double viewportAnchorY() const { return m_viewportAnchor.y(); } 80 void setViewportAnchorY(double, ExceptionState&); 81 82 const AtomicString scroll() const; 83 void setScroll(const AtomicString&, ExceptionState&); 84 85 void updateParametersFromRegion(TextTrackRegion*); 86 87 const String& regionSettings() const { return m_settings; } 88 void setRegionSettings(const String&); 89 90 bool isScrollingRegion() { return m_scroll; } 91 92 PassRefPtr<HTMLDivElement> getDisplayTree(); 93 94 void appendTextTrackCueBox(PassRefPtr<TextTrackCueBox>); 95 void displayLastTextTrackCueBox(); 96 void willRemoveTextTrackCueBox(TextTrackCueBox*); 97 98 private: 99 TextTrackRegion(ScriptExecutionContext*); 100 Document* ownerDocument() { return toDocument(m_scriptExecutionContext); } 101 102 void prepareRegionDisplayTree(); 103 104 // The timer is needed to continue processing when cue scrolling ended. 105 void startTimer(); 106 void stopTimer(); 107 void scrollTimerFired(Timer<TextTrackRegion>*); 108 109 enum RegionSetting { 110 None, 111 Id, 112 Width, 113 Height, 114 RegionAnchor, 115 ViewportAnchor, 116 Scroll 117 }; 118 119 RegionSetting getSettingFromString(const String&); 120 121 void parseSettingValue(RegionSetting, const String&); 122 void parseSetting(const String&, unsigned*); 123 124 static const AtomicString& textTrackCueContainerShadowPseudoId(); 125 static const AtomicString& textTrackCueContainerScrollingClass(); 126 static const AtomicString& textTrackRegionShadowPseudoId(); 127 128 String m_id; 129 String m_settings; 130 131 double m_width; 132 unsigned m_heightInLines; 133 134 FloatPoint m_regionAnchor; 135 FloatPoint m_viewportAnchor; 136 137 bool m_scroll; 138 139 RefPtr<HTMLDivElement> m_regionDisplayTree; 140 141 // The cue container is the container that is scrolled up to obtain the 142 // effect of scrolling cues when this is enabled for the regions. 143 RefPtr<HTMLDivElement> m_cueContainer; 144 145 // The member variable track can be a raw pointer as it will never 146 // reference a destroyed TextTrack, as this member variable 147 // is cleared in the TextTrack destructor and it is generally 148 // set/reset within the addRegion and removeRegion methods. 149 TextTrack* m_track; 150 151 // Keep track of the current numeric value of the css "top" property. 152 double m_currentTop; 153 154 // The timer is used to display the next cue line after the current one has 155 // been displayed. It's main use is for scrolling regions and it triggers as 156 // soon as the animation for rolling out one line has finished, but 157 // currently it is used also for non-scrolling regions to use a single 158 // code path. 159 Timer<TextTrackRegion> m_scrollTimer; 160 }; 161 162 } // namespace WebCore 163 164 #endif 165 #endif 166