Home | History | Annotate | Download | only in vtt
      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 VTTRegion_h
     32 #define VTTRegion_h
     33 
     34 #include "core/dom/ContextLifecycleObserver.h"
     35 #include "core/html/track/TextTrack.h"
     36 #include "platform/Timer.h"
     37 #include "platform/geometry/FloatPoint.h"
     38 #include "platform/heap/Handle.h"
     39 #include "wtf/PassOwnPtr.h"
     40 #include "wtf/RefCounted.h"
     41 
     42 namespace WebCore {
     43 
     44 class ExceptionState;
     45 class HTMLDivElement;
     46 class VTTCueBox;
     47 class VTTScanner;
     48 
     49 class VTTRegion FINAL : public RefCountedWillBeGarbageCollectedFinalized<VTTRegion> {
     50 public:
     51     static PassRefPtrWillBeRawPtr<VTTRegion> create()
     52     {
     53         return adoptRefWillBeNoop(new VTTRegion());
     54     }
     55 
     56     virtual ~VTTRegion();
     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(VTTRegion*);
     86 
     87     const String& regionSettings() const { return m_settings; }
     88     void setRegionSettings(const String&);
     89 
     90     bool isScrollingRegion() { return m_scroll; }
     91 
     92     PassRefPtrWillBeRawPtr<HTMLDivElement> getDisplayTree(Document&);
     93 
     94     void appendVTTCueBox(PassRefPtrWillBeRawPtr<VTTCueBox>);
     95     void displayLastVTTCueBox();
     96     void willRemoveVTTCueBox(VTTCueBox*);
     97 
     98     void trace(Visitor*);
     99 
    100 private:
    101     VTTRegion();
    102 
    103     void prepareRegionDisplayTree();
    104 
    105     // The timer is needed to continue processing when cue scrolling ended.
    106     void startTimer();
    107     void stopTimer();
    108     void scrollTimerFired(Timer<VTTRegion>*);
    109 
    110     enum RegionSetting {
    111         None,
    112         Id,
    113         Width,
    114         Height,
    115         RegionAnchor,
    116         ViewportAnchor,
    117         Scroll
    118     };
    119     RegionSetting scanSettingName(VTTScanner&);
    120     void parseSettingValue(RegionSetting, VTTScanner&);
    121 
    122     static const AtomicString& textTrackCueContainerShadowPseudoId();
    123     static const AtomicString& textTrackCueContainerScrollingClass();
    124     static const AtomicString& textTrackRegionShadowPseudoId();
    125 
    126     String m_id;
    127     String m_settings;
    128     double m_width;
    129     unsigned m_heightInLines;
    130     FloatPoint m_regionAnchor;
    131     FloatPoint m_viewportAnchor;
    132     bool m_scroll;
    133 
    134     // The cue container is the container that is scrolled up to obtain the
    135     // effect of scrolling cues when this is enabled for the regions.
    136     RefPtrWillBeMember<HTMLDivElement> m_cueContainer;
    137     RefPtrWillBeMember<HTMLDivElement> m_regionDisplayTree;
    138 
    139     // The member variable track can be a raw pointer as it will never
    140     // reference a destroyed TextTrack, as this member variable
    141     // is cleared in the TextTrack destructor and it is generally
    142     // set/reset within the addRegion and removeRegion methods.
    143     RawPtrWillBeMember<TextTrack> m_track;
    144 
    145     // Keep track of the current numeric value of the css "top" property.
    146     double m_currentTop;
    147 
    148     // The timer is used to display the next cue line after the current one has
    149     // been displayed. It's main use is for scrolling regions and it triggers as
    150     // soon as the animation for rolling out one line has finished, but
    151     // currently it is used also for non-scrolling regions to use a single
    152     // code path.
    153     Timer<VTTRegion> m_scrollTimer;
    154 };
    155 
    156 } // namespace WebCore
    157 #endif
    158