/work/obj-fuzz/dist/include/mozilla/dom/TextTrackCue.h
Line | Count | Source (jump to first uncovered line) |
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/TextTrack.h" |
19 | | #include "mozilla/StateWatching.h" |
20 | | |
21 | | namespace mozilla { |
22 | | namespace dom { |
23 | | |
24 | | class HTMLTrackElement; |
25 | | class TextTrackRegion; |
26 | | |
27 | | class TextTrackCue 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 | 0 | { |
42 | 0 | nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal.GetAsSupports()); |
43 | 0 | RefPtr<TextTrackCue> ttcue = new TextTrackCue(window, aStartTime, |
44 | 0 | aEndTime, aText, aRv); |
45 | 0 | return ttcue.forget(); |
46 | 0 | } |
47 | | TextTrackCue(nsPIDOMWindowInner* aGlobal, double aStartTime, double aEndTime, |
48 | | const nsAString& aText, ErrorResult& aRv); |
49 | | |
50 | | TextTrackCue(nsPIDOMWindowInner* aGlobal, double aStartTime, double aEndTime, |
51 | | const nsAString& aText, HTMLTrackElement* aTrackElement, |
52 | | ErrorResult& aRv); |
53 | | |
54 | | JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; |
55 | | |
56 | | TextTrack* GetTrack() const |
57 | 0 | { |
58 | 0 | return mTrack; |
59 | 0 | } |
60 | | |
61 | | void GetId(nsAString& aId) const |
62 | 0 | { |
63 | 0 | aId = mId; |
64 | 0 | } |
65 | | |
66 | | void SetId(const nsAString& aId) |
67 | 0 | { |
68 | 0 | if (mId == aId) { |
69 | 0 | return; |
70 | 0 | } |
71 | 0 | |
72 | 0 | mId = aId; |
73 | 0 | } |
74 | | |
75 | | double StartTime() const |
76 | 0 | { |
77 | 0 | return mStartTime; |
78 | 0 | } |
79 | | |
80 | | void SetStartTime(double aStartTime) |
81 | 0 | { |
82 | 0 | if (mStartTime == aStartTime) { |
83 | 0 | return; |
84 | 0 | } |
85 | 0 | |
86 | 0 | mStartTime = aStartTime; |
87 | 0 | mReset = true; |
88 | 0 | NotifyCueUpdated(this); |
89 | 0 | } |
90 | | |
91 | | double EndTime() const |
92 | 0 | { |
93 | 0 | return mEndTime; |
94 | 0 | } |
95 | | |
96 | | void SetEndTime(double aEndTime) |
97 | 0 | { |
98 | 0 | if (mEndTime == aEndTime) { |
99 | 0 | return; |
100 | 0 | } |
101 | 0 | |
102 | 0 | mEndTime = aEndTime; |
103 | 0 | mReset = true; |
104 | 0 | NotifyCueUpdated(this); |
105 | 0 | } |
106 | | |
107 | | bool PauseOnExit() |
108 | 0 | { |
109 | 0 | return mPauseOnExit; |
110 | 0 | } |
111 | | |
112 | | void SetPauseOnExit(bool aPauseOnExit) |
113 | 0 | { |
114 | 0 | if (mPauseOnExit == aPauseOnExit) { |
115 | 0 | return; |
116 | 0 | } |
117 | 0 | |
118 | 0 | mPauseOnExit = aPauseOnExit; |
119 | 0 | NotifyCueUpdated(nullptr); |
120 | 0 | } |
121 | | |
122 | | TextTrackRegion* GetRegion(); |
123 | | void SetRegion(TextTrackRegion* aRegion); |
124 | | |
125 | | DirectionSetting Vertical() const |
126 | 0 | { |
127 | 0 | return mVertical; |
128 | 0 | } |
129 | | |
130 | | void SetVertical(const DirectionSetting& aVertical) |
131 | 0 | { |
132 | 0 | if (mVertical == aVertical) { |
133 | 0 | return; |
134 | 0 | } |
135 | 0 | |
136 | 0 | mReset = true; |
137 | 0 | mVertical = aVertical; |
138 | 0 | } |
139 | | |
140 | | bool SnapToLines() |
141 | 0 | { |
142 | 0 | return mSnapToLines; |
143 | 0 | } |
144 | | |
145 | | void SetSnapToLines(bool aSnapToLines) |
146 | 0 | { |
147 | 0 | if (mSnapToLines == aSnapToLines) { |
148 | 0 | return; |
149 | 0 | } |
150 | 0 | |
151 | 0 | mReset = true; |
152 | 0 | mSnapToLines = aSnapToLines; |
153 | 0 | } |
154 | | |
155 | | void GetLine(OwningDoubleOrAutoKeyword& aLine) const |
156 | 0 | { |
157 | 0 | if (mLineIsAutoKeyword) { |
158 | 0 | aLine.SetAsAutoKeyword() = AutoKeyword::Auto; |
159 | 0 | return; |
160 | 0 | } |
161 | 0 | aLine.SetAsDouble() = mLine; |
162 | 0 | } |
163 | | |
164 | | void SetLine(const DoubleOrAutoKeyword& aLine) |
165 | 0 | { |
166 | 0 | if (aLine.IsDouble() && |
167 | 0 | (mLineIsAutoKeyword || (aLine.GetAsDouble() != mLine))) { |
168 | 0 | mLineIsAutoKeyword = false; |
169 | 0 | mLine = aLine.GetAsDouble(); |
170 | 0 | mReset = true; |
171 | 0 | return; |
172 | 0 | } |
173 | 0 | if (aLine.IsAutoKeyword() && !mLineIsAutoKeyword) { |
174 | 0 | mLineIsAutoKeyword = true; |
175 | 0 | mReset = true; |
176 | 0 | } |
177 | 0 | } |
178 | | |
179 | | LineAlignSetting LineAlign() const |
180 | 0 | { |
181 | 0 | return mLineAlign; |
182 | 0 | } |
183 | | |
184 | | void SetLineAlign(LineAlignSetting& aLineAlign, ErrorResult& aRv) |
185 | 0 | { |
186 | 0 | if (mLineAlign == aLineAlign) { |
187 | 0 | return; |
188 | 0 | } |
189 | 0 | |
190 | 0 | mReset = true; |
191 | 0 | mLineAlign = aLineAlign; |
192 | 0 | } |
193 | | |
194 | | void GetPosition(OwningDoubleOrAutoKeyword& aPosition) const |
195 | 0 | { |
196 | 0 | if (mPositionIsAutoKeyword) { |
197 | 0 | aPosition.SetAsAutoKeyword() = AutoKeyword::Auto; |
198 | 0 | return; |
199 | 0 | } |
200 | 0 | aPosition.SetAsDouble() = mPosition; |
201 | 0 | } |
202 | | |
203 | | void SetPosition(const DoubleOrAutoKeyword& aPosition, ErrorResult& aRv) |
204 | 0 | { |
205 | 0 | if (!aPosition.IsAutoKeyword() && |
206 | 0 | (aPosition.GetAsDouble() > 100.0 || aPosition.GetAsDouble() < 0.0)){ |
207 | 0 | aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); |
208 | 0 | return; |
209 | 0 | } |
210 | 0 | |
211 | 0 | if (aPosition.IsDouble() && |
212 | 0 | (mPositionIsAutoKeyword || (aPosition.GetAsDouble() != mPosition))) { |
213 | 0 | mPositionIsAutoKeyword = false; |
214 | 0 | mPosition = aPosition.GetAsDouble(); |
215 | 0 | mReset = true; |
216 | 0 | return; |
217 | 0 | } |
218 | 0 | |
219 | 0 | if (aPosition.IsAutoKeyword() && !mPositionIsAutoKeyword) { |
220 | 0 | mPositionIsAutoKeyword = true; |
221 | 0 | mReset = true; |
222 | 0 | } |
223 | 0 | } |
224 | | |
225 | | PositionAlignSetting PositionAlign() const |
226 | 0 | { |
227 | 0 | return mPositionAlign; |
228 | 0 | } |
229 | | |
230 | | void SetPositionAlign(PositionAlignSetting aPositionAlign, ErrorResult& aRv) |
231 | 0 | { |
232 | 0 | if (mPositionAlign == aPositionAlign) { |
233 | 0 | return; |
234 | 0 | } |
235 | 0 | |
236 | 0 | mReset = true; |
237 | 0 | mPositionAlign = aPositionAlign; |
238 | 0 | } |
239 | | |
240 | | double Size() const |
241 | 0 | { |
242 | 0 | return mSize; |
243 | 0 | } |
244 | | |
245 | | void SetSize(double aSize, ErrorResult& aRv) |
246 | 0 | { |
247 | 0 | if (mSize == aSize) { |
248 | 0 | return; |
249 | 0 | } |
250 | 0 | |
251 | 0 | if (aSize < 0.0 || aSize > 100.0) { |
252 | 0 | aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); |
253 | 0 | return; |
254 | 0 | } |
255 | 0 | |
256 | 0 | mReset = true; |
257 | 0 | mSize = aSize; |
258 | 0 | } |
259 | | |
260 | | AlignSetting Align() const |
261 | 0 | { |
262 | 0 | return mAlign; |
263 | 0 | } |
264 | | |
265 | | void SetAlign(AlignSetting& aAlign) |
266 | 0 | { |
267 | 0 | if (mAlign == aAlign) { |
268 | 0 | return; |
269 | 0 | } |
270 | 0 | |
271 | 0 | mReset = true; |
272 | 0 | mAlign = aAlign; |
273 | 0 | } |
274 | | |
275 | | void GetText(nsAString& aText) const |
276 | 0 | { |
277 | 0 | aText = mText; |
278 | 0 | } |
279 | | |
280 | | void SetText(const nsAString& aText) |
281 | 0 | { |
282 | 0 | if (mText == aText) { |
283 | 0 | return; |
284 | 0 | } |
285 | 0 | |
286 | 0 | mReset = true; |
287 | 0 | mText = aText; |
288 | 0 | } |
289 | | |
290 | | IMPL_EVENT_HANDLER(enter) |
291 | | IMPL_EVENT_HANDLER(exit) |
292 | | |
293 | | HTMLDivElement* GetDisplayState() |
294 | 0 | { |
295 | 0 | return static_cast<HTMLDivElement*>(mDisplayState.get()); |
296 | 0 | } |
297 | | |
298 | | void SetDisplayState(HTMLDivElement* aDisplayState) |
299 | 0 | { |
300 | 0 | mDisplayState = aDisplayState; |
301 | 0 | mReset = false; |
302 | 0 | } |
303 | | |
304 | | void Reset() |
305 | 0 | { |
306 | 0 | mReset = true; |
307 | 0 | } |
308 | | |
309 | | bool HasBeenReset() |
310 | 0 | { |
311 | 0 | return mReset; |
312 | 0 | } |
313 | | |
314 | | double ComputedLine(); |
315 | | double ComputedPosition(); |
316 | | PositionAlignSetting ComputedPositionAlign(); |
317 | | |
318 | | // Helper functions for implementation. |
319 | | const nsAString& Id() const |
320 | 0 | { |
321 | 0 | return mId; |
322 | 0 | } |
323 | | |
324 | | void SetTrack(TextTrack* aTextTrack) |
325 | 0 | { |
326 | 0 | mTrack = aTextTrack; |
327 | 0 | if (!mHaveStartedWatcher && aTextTrack) { |
328 | 0 | mHaveStartedWatcher = true; |
329 | 0 | mWatchManager.Watch(mReset, &TextTrackCue::NotifyDisplayStatesChanged); |
330 | 0 | } else if (mHaveStartedWatcher && !aTextTrack) { |
331 | 0 | mHaveStartedWatcher = false; |
332 | 0 | mWatchManager.Unwatch(mReset, &TextTrackCue::NotifyDisplayStatesChanged); |
333 | 0 | } |
334 | 0 | } |
335 | | |
336 | | /** |
337 | | * Produces a tree of anonymous content based on the tree of the processed |
338 | | * cue text. |
339 | | * |
340 | | * Returns a DocumentFragment that is the head of the tree of anonymous |
341 | | * content. |
342 | | */ |
343 | | already_AddRefed<DocumentFragment> GetCueAsHTML(); |
344 | | |
345 | | void SetTrackElement(HTMLTrackElement* aTrackElement); |
346 | | |
347 | | void SetActive(bool aActive) |
348 | 0 | { |
349 | 0 | if (mActive == aActive) { |
350 | 0 | return; |
351 | 0 | } |
352 | 0 | |
353 | 0 | mActive = aActive; |
354 | 0 | mDisplayState = mActive ? mDisplayState : nullptr; |
355 | 0 | } |
356 | | |
357 | | bool GetActive() |
358 | 0 | { |
359 | 0 | return mActive; |
360 | 0 | } |
361 | | |
362 | | private: |
363 | | ~TextTrackCue(); |
364 | | |
365 | | void NotifyCueUpdated(TextTrackCue* aCue) |
366 | 0 | { |
367 | 0 | if (mTrack) { |
368 | 0 | mTrack->NotifyCueUpdated(aCue); |
369 | 0 | } |
370 | 0 | } |
371 | | |
372 | | void NotifyDisplayStatesChanged(); |
373 | | |
374 | | void SetDefaultCueSettings(); |
375 | | nsresult StashDocument(); |
376 | | |
377 | | RefPtr<nsIDocument> mDocument; |
378 | | nsString mText; |
379 | | double mStartTime; |
380 | | double mEndTime; |
381 | | |
382 | | RefPtr<TextTrack> mTrack; |
383 | | RefPtr<HTMLTrackElement> mTrackElement; |
384 | | nsString mId; |
385 | | double mPosition; |
386 | | bool mPositionIsAutoKeyword; |
387 | | PositionAlignSetting mPositionAlign; |
388 | | double mSize; |
389 | | bool mPauseOnExit; |
390 | | bool mSnapToLines; |
391 | | RefPtr<TextTrackRegion> mRegion; |
392 | | DirectionSetting mVertical; |
393 | | bool mLineIsAutoKeyword; |
394 | | double mLine; |
395 | | AlignSetting mAlign; |
396 | | LineAlignSetting mLineAlign; |
397 | | |
398 | | // Holds the computed DOM elements that represent the parsed cue text. |
399 | | // http://www.whatwg.org/specs/web-apps/current-work/#text-track-cue-display-state |
400 | | RefPtr<nsGenericHTMLElement> mDisplayState; |
401 | | // Tells whether or not we need to recompute mDisplayState. This is set |
402 | | // anytime a property that relates to the display of the TextTrackCue is |
403 | | // changed. |
404 | | Watchable<bool> mReset; |
405 | | |
406 | | bool mActive; |
407 | | |
408 | | static StaticRefPtr<nsIWebVTTParserWrapper> sParserWrapper; |
409 | | |
410 | | // Only start watcher after the cue has text track. |
411 | | bool mHaveStartedWatcher; |
412 | | WatchManager<TextTrackCue> mWatchManager; |
413 | | }; |
414 | | |
415 | | } // namespace dom |
416 | | } // namespace mozilla |
417 | | |
418 | | #endif // mozilla_dom_TextTrackCue_h |