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