|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim:set ts=2 sw=2 et tw=78: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef mozilla_dom_TextTrackCue_h |
|
8 #define mozilla_dom_TextTrackCue_h |
|
9 |
|
10 #include "mozilla/DOMEventTargetHelper.h" |
|
11 #include "mozilla/dom/DocumentFragment.h" |
|
12 #include "mozilla/dom/VTTCueBinding.h" |
|
13 #include "nsCycleCollectionParticipant.h" |
|
14 #include "nsIWebVTTParserWrapper.h" |
|
15 #include "mozilla/StaticPtr.h" |
|
16 #include "nsIDocument.h" |
|
17 #include "mozilla/dom/HTMLDivElement.h" |
|
18 #include "mozilla/dom/UnionTypes.h" |
|
19 #include "mozilla/dom/TextTrack.h" |
|
20 |
|
21 namespace mozilla { |
|
22 namespace dom { |
|
23 |
|
24 class HTMLTrackElement; |
|
25 class TextTrackRegion; |
|
26 |
|
27 class TextTrackCue MOZ_FINAL : public DOMEventTargetHelper |
|
28 { |
|
29 public: |
|
30 NS_DECL_ISUPPORTS_INHERITED |
|
31 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(TextTrackCue, DOMEventTargetHelper) |
|
32 |
|
33 // TextTrackCue WebIDL |
|
34 // See bug 868509 about splitting out the WebVTT-specific interfaces. |
|
35 static already_AddRefed<TextTrackCue> |
|
36 Constructor(GlobalObject& aGlobal, |
|
37 double aStartTime, |
|
38 double aEndTime, |
|
39 const nsAString& aText, |
|
40 ErrorResult& aRv) |
|
41 { |
|
42 nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports()); |
|
43 nsRefPtr<TextTrackCue> ttcue = new TextTrackCue(window, aStartTime, |
|
44 aEndTime, aText, aRv); |
|
45 return ttcue.forget(); |
|
46 } |
|
47 TextTrackCue(nsPIDOMWindow* aGlobal, double aStartTime, double aEndTime, |
|
48 const nsAString& aText, ErrorResult& aRv); |
|
49 |
|
50 TextTrackCue(nsPIDOMWindow* aGlobal, double aStartTime, double aEndTime, |
|
51 const nsAString& aText, HTMLTrackElement* aTrackElement, |
|
52 ErrorResult& aRv); |
|
53 |
|
54 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
55 |
|
56 TextTrack* GetTrack() const |
|
57 { |
|
58 return mTrack; |
|
59 } |
|
60 |
|
61 void GetId(nsAString& aId) const |
|
62 { |
|
63 aId = mId; |
|
64 } |
|
65 |
|
66 void SetId(const nsAString& aId) |
|
67 { |
|
68 if (mId == aId) { |
|
69 return; |
|
70 } |
|
71 |
|
72 mId = aId; |
|
73 } |
|
74 |
|
75 double StartTime() const |
|
76 { |
|
77 return mStartTime; |
|
78 } |
|
79 |
|
80 void SetStartTime(double aStartTime) |
|
81 { |
|
82 if (mStartTime == aStartTime) { |
|
83 return; |
|
84 } |
|
85 |
|
86 mStartTime = aStartTime; |
|
87 mReset = true; |
|
88 } |
|
89 |
|
90 double EndTime() const |
|
91 { |
|
92 return mEndTime; |
|
93 } |
|
94 |
|
95 void SetEndTime(double aEndTime) |
|
96 { |
|
97 if (mEndTime == aEndTime) { |
|
98 return; |
|
99 } |
|
100 |
|
101 mEndTime = aEndTime; |
|
102 mReset = true; |
|
103 } |
|
104 |
|
105 bool PauseOnExit() |
|
106 { |
|
107 return mPauseOnExit; |
|
108 } |
|
109 |
|
110 void SetPauseOnExit(bool aPauseOnExit) |
|
111 { |
|
112 if (mPauseOnExit == aPauseOnExit) { |
|
113 return; |
|
114 } |
|
115 |
|
116 mPauseOnExit = aPauseOnExit; |
|
117 } |
|
118 |
|
119 TextTrackRegion* GetRegion(); |
|
120 void SetRegion(TextTrackRegion* aRegion); |
|
121 |
|
122 DirectionSetting Vertical() const |
|
123 { |
|
124 return mVertical; |
|
125 } |
|
126 |
|
127 void SetVertical(const DirectionSetting& aVertical) |
|
128 { |
|
129 if (mVertical == aVertical) { |
|
130 return; |
|
131 } |
|
132 |
|
133 mReset = true; |
|
134 mVertical = aVertical; |
|
135 } |
|
136 |
|
137 bool SnapToLines() |
|
138 { |
|
139 return mSnapToLines; |
|
140 } |
|
141 |
|
142 void SetSnapToLines(bool aSnapToLines) |
|
143 { |
|
144 if (mSnapToLines == aSnapToLines) { |
|
145 return; |
|
146 } |
|
147 |
|
148 mReset = true; |
|
149 mSnapToLines = aSnapToLines; |
|
150 } |
|
151 |
|
152 void GetLine(OwningLongOrAutoKeyword& aLine) const |
|
153 { |
|
154 if (mLineIsAutoKeyword) { |
|
155 aLine.SetAsAutoKeyword() = AutoKeyword::Auto; |
|
156 return; |
|
157 } |
|
158 aLine.SetAsLong() = mLineLong; |
|
159 } |
|
160 |
|
161 void SetLine(const LongOrAutoKeyword& aLine) |
|
162 { |
|
163 if (aLine.IsLong() && |
|
164 (mLineIsAutoKeyword || (aLine.GetAsLong() != mLineLong))) { |
|
165 mLineIsAutoKeyword = false; |
|
166 mLineLong = aLine.GetAsLong(); |
|
167 mReset = true; |
|
168 return; |
|
169 } |
|
170 if (aLine.IsAutoKeyword() && !mLineIsAutoKeyword) { |
|
171 mLineIsAutoKeyword = true; |
|
172 mReset = true; |
|
173 } |
|
174 } |
|
175 |
|
176 AlignSetting LineAlign() const |
|
177 { |
|
178 return mLineAlign; |
|
179 } |
|
180 |
|
181 void SetLineAlign(AlignSetting& aLineAlign, ErrorResult& aRv) |
|
182 { |
|
183 if (mLineAlign == aLineAlign) |
|
184 return; |
|
185 |
|
186 if (aLineAlign == AlignSetting::Left || |
|
187 aLineAlign == AlignSetting::Right) { |
|
188 return aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); |
|
189 } |
|
190 |
|
191 mReset = true; |
|
192 mLineAlign = aLineAlign; |
|
193 } |
|
194 |
|
195 int32_t Position() const |
|
196 { |
|
197 return mPosition; |
|
198 } |
|
199 |
|
200 void SetPosition(int32_t aPosition, ErrorResult& aRv) |
|
201 { |
|
202 if (mPosition == aPosition) { |
|
203 return; |
|
204 } |
|
205 |
|
206 if (aPosition > 100 || aPosition < 0){ |
|
207 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); |
|
208 return; |
|
209 } |
|
210 |
|
211 mReset = true; |
|
212 mPosition = aPosition; |
|
213 } |
|
214 |
|
215 AlignSetting PositionAlign() const |
|
216 { |
|
217 return mPositionAlign; |
|
218 } |
|
219 |
|
220 void SetPositionAlign(AlignSetting aPositionAlign, ErrorResult& aRv) |
|
221 { |
|
222 if (mPositionAlign == aPositionAlign) |
|
223 return; |
|
224 |
|
225 if (aPositionAlign == AlignSetting::Left || |
|
226 aPositionAlign == AlignSetting::Right) { |
|
227 return aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); |
|
228 } |
|
229 |
|
230 mReset = true; |
|
231 mPositionAlign = aPositionAlign; |
|
232 } |
|
233 |
|
234 int32_t Size() const |
|
235 { |
|
236 return mSize; |
|
237 } |
|
238 |
|
239 void SetSize(int32_t aSize, ErrorResult& aRv) |
|
240 { |
|
241 if (mSize == aSize) { |
|
242 return; |
|
243 } |
|
244 |
|
245 if (aSize < 0 || aSize > 100) { |
|
246 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); |
|
247 return; |
|
248 } |
|
249 |
|
250 mReset = true; |
|
251 mSize = aSize; |
|
252 } |
|
253 |
|
254 AlignSetting Align() const |
|
255 { |
|
256 return mAlign; |
|
257 } |
|
258 |
|
259 void SetAlign(AlignSetting& aAlign) |
|
260 { |
|
261 if (mAlign == aAlign) { |
|
262 return; |
|
263 } |
|
264 |
|
265 mReset = true; |
|
266 mAlign = aAlign; |
|
267 } |
|
268 |
|
269 void GetText(nsAString& aText) const |
|
270 { |
|
271 aText = mText; |
|
272 } |
|
273 |
|
274 void SetText(const nsAString& aText) |
|
275 { |
|
276 if (mText == aText) { |
|
277 return; |
|
278 } |
|
279 |
|
280 mReset = true; |
|
281 mText = aText; |
|
282 } |
|
283 |
|
284 IMPL_EVENT_HANDLER(enter) |
|
285 IMPL_EVENT_HANDLER(exit) |
|
286 |
|
287 HTMLDivElement* GetDisplayState() |
|
288 { |
|
289 return static_cast<HTMLDivElement*>(mDisplayState.get()); |
|
290 } |
|
291 |
|
292 void SetDisplayState(HTMLDivElement* aDisplayState) |
|
293 { |
|
294 mDisplayState = aDisplayState; |
|
295 mReset = false; |
|
296 } |
|
297 |
|
298 void Reset() |
|
299 { |
|
300 mReset = true; |
|
301 } |
|
302 |
|
303 bool HasBeenReset() |
|
304 { |
|
305 return mReset; |
|
306 } |
|
307 |
|
308 // Helper functions for implementation. |
|
309 bool |
|
310 operator==(const TextTrackCue& rhs) const |
|
311 { |
|
312 return mId.Equals(rhs.mId); |
|
313 } |
|
314 |
|
315 const nsAString& Id() const |
|
316 { |
|
317 return mId; |
|
318 } |
|
319 |
|
320 void SetTrack(TextTrack* aTextTrack) |
|
321 { |
|
322 mTrack = aTextTrack; |
|
323 } |
|
324 |
|
325 /** |
|
326 * Produces a tree of anonymous content based on the tree of the processed |
|
327 * cue text. |
|
328 * |
|
329 * Returns a DocumentFragment that is the head of the tree of anonymous |
|
330 * content. |
|
331 */ |
|
332 already_AddRefed<DocumentFragment> GetCueAsHTML(); |
|
333 |
|
334 void SetTrackElement(HTMLTrackElement* aTrackElement); |
|
335 |
|
336 private: |
|
337 void SetDefaultCueSettings(); |
|
338 nsresult StashDocument(); |
|
339 |
|
340 nsRefPtr<nsIDocument> mDocument; |
|
341 nsString mText; |
|
342 double mStartTime; |
|
343 double mEndTime; |
|
344 |
|
345 nsRefPtr<TextTrack> mTrack; |
|
346 nsRefPtr<HTMLTrackElement> mTrackElement; |
|
347 nsString mId; |
|
348 int32_t mPosition; |
|
349 AlignSetting mPositionAlign; |
|
350 int32_t mSize; |
|
351 bool mPauseOnExit; |
|
352 bool mSnapToLines; |
|
353 nsRefPtr<TextTrackRegion> mRegion; |
|
354 DirectionSetting mVertical; |
|
355 bool mLineIsAutoKeyword; |
|
356 long mLineLong; |
|
357 AlignSetting mAlign; |
|
358 AlignSetting mLineAlign; |
|
359 |
|
360 // Holds the computed DOM elements that represent the parsed cue text. |
|
361 // http://www.whatwg.org/specs/web-apps/current-work/#text-track-cue-display-state |
|
362 nsRefPtr<nsGenericHTMLElement> mDisplayState; |
|
363 // Tells whether or not we need to recompute mDisplayState. This is set |
|
364 // anytime a property that relates to the display of the TextTrackCue is |
|
365 // changed. |
|
366 bool mReset; |
|
367 |
|
368 static StaticRefPtr<nsIWebVTTParserWrapper> sParserWrapper; |
|
369 }; |
|
370 |
|
371 } // namespace dom |
|
372 } // namespace mozilla |
|
373 |
|
374 #endif // mozilla_dom_TextTrackCue_h |