Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / html / TextTrackManager.h
blob6ce19963d538e7e0c8240df837b42d0bc89994f8
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_TextTrackManager_h
8 #define mozilla_dom_TextTrackManager_h
10 #include "mozilla/dom/TextTrack.h"
11 #include "mozilla/dom/TextTrackList.h"
12 #include "mozilla/dom/TextTrackCueList.h"
13 #include "mozilla/StaticPtr.h"
14 #include "nsContentUtils.h"
15 #include "nsIDOMEventListener.h"
16 #include "TimeUnits.h"
18 class nsIWebVTTParserWrapper;
20 namespace mozilla {
21 template <typename T>
22 class Maybe;
23 namespace dom {
25 class HTMLMediaElement;
27 class CompareTextTracks {
28 private:
29 HTMLMediaElement* mMediaElement;
30 Maybe<uint32_t> TrackChildPosition(TextTrack* aTrack) const;
32 public:
33 explicit CompareTextTracks(HTMLMediaElement* aMediaElement);
34 bool Equals(TextTrack* aOne, TextTrack* aTwo) const;
35 bool LessThan(TextTrack* aOne, TextTrack* aTwo) const;
38 class TextTrack;
39 class TextTrackCue;
41 class TextTrackManager final : public nsIDOMEventListener {
42 ~TextTrackManager();
44 public:
45 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
46 NS_DECL_CYCLE_COLLECTION_CLASS(TextTrackManager)
48 NS_DECL_NSIDOMEVENTLISTENER
50 explicit TextTrackManager(HTMLMediaElement* aMediaElement);
52 TextTrackList* GetTextTracks() const;
53 already_AddRefed<TextTrack> AddTextTrack(TextTrackKind aKind,
54 const nsAString& aLabel,
55 const nsAString& aLanguage,
56 TextTrackMode aMode,
57 TextTrackReadyState aReadyState,
58 TextTrackSource aTextTrackSource);
59 void AddTextTrack(TextTrack* aTextTrack);
60 void RemoveTextTrack(TextTrack* aTextTrack, bool aPendingListOnly);
61 void DidSeek();
63 void NotifyCueAdded(TextTrackCue& aCue);
64 void AddCues(TextTrack* aTextTrack);
65 void NotifyCueRemoved(TextTrackCue& aCue);
66 /**
67 * Overview of WebVTT cuetext and anonymous content setup.
69 * WebVTT nodes are the parsed version of WebVTT cuetext. WebVTT cuetext is
70 * the portion of a WebVTT cue that specifies what the caption will actually
71 * show up as on screen.
73 * WebVTT cuetext can contain markup that loosely relates to HTML markup. It
74 * can contain tags like <b>, <u>, <i>, <c>, <v>, <ruby>, <rt>, <lang>,
75 * including timestamp tags.
77 * When the caption is ready to be displayed the WebVTT nodes are converted
78 * over to anonymous DOM content. <i>, <u>, <b>, <ruby>, and <rt> all become
79 * HTMLElements of their corresponding HTML markup tags. <c> and <v> are
80 * converted to <span> tags. Timestamp tags are converted to XML processing
81 * instructions. Additionally, all cuetext tags support specifying of classes.
82 * This takes the form of <foo.class.subclass>. These classes are then parsed
83 * and set as the anonymous content's class attribute.
85 * Rules on constructing DOM objects from WebVTT nodes can be found here
86 * http://dev.w3.org/html5/webvtt/#webvtt-cue-text-dom-construction-rules.
87 * Current rules are taken from revision on April 15, 2013.
90 void PopulatePendingList();
92 void AddListeners();
94 // The HTMLMediaElement that this TextTrackManager manages the TextTracks of.
95 RefPtr<HTMLMediaElement> mMediaElement;
97 void DispatchTimeMarchesOn();
98 void TimeMarchesOn();
99 void DispatchUpdateCueDisplay();
101 void NotifyShutdown() { mShutdown = true; }
103 void NotifyCueUpdated(TextTrackCue* aCue);
105 void NotifyReset();
107 bool IsLoaded();
109 private:
111 * Converts the TextTrackCue's cuetext into a tree of DOM objects
112 * and attaches it to a div on its owning TrackElement's
113 * MediaElement's caption overlay.
115 void UpdateCueDisplay();
117 // List of the TextTrackManager's owning HTMLMediaElement's TextTracks.
118 RefPtr<TextTrackList> mTextTracks;
119 // List of text track objects awaiting loading.
120 RefPtr<TextTrackList> mPendingTextTracks;
121 // List of newly introduced Text Track cues.
123 // Contain all cues for a MediaElement. Not sorted.
124 RefPtr<TextTrackCueList> mNewCues;
126 // True if the media player playback changed due to seeking prior to and
127 // during running the "Time Marches On" algorithm.
128 bool mHasSeeked;
129 // Playback position at the time of last "Time Marches On" call
130 media::TimeUnit mLastTimeMarchesOnCalled;
132 bool mTimeMarchesOnDispatched;
133 bool mUpdateCueDisplayDispatched;
135 static StaticRefPtr<nsIWebVTTParserWrapper> sParserWrapper;
137 bool performedTrackSelection;
139 // Runs the algorithm for performing automatic track selection.
140 void HonorUserPreferencesForTrackSelection();
141 // Performs track selection for a single TextTrackKind.
142 void PerformTrackSelection(TextTrackKind aTextTrackKind);
143 // Performs track selection for a set of TextTrackKinds, for example,
144 // 'subtitles' and 'captions' should be selected together.
145 void PerformTrackSelection(TextTrackKind aTextTrackKinds[], uint32_t size);
146 void GetTextTracksOfKinds(TextTrackKind aTextTrackKinds[], uint32_t size,
147 nsTArray<TextTrack*>& aTextTracks);
148 void GetTextTracksOfKind(TextTrackKind aTextTrackKind,
149 nsTArray<TextTrack*>& aTextTracks);
150 bool TrackIsDefault(TextTrack* aTextTrack);
152 bool IsShutdown() const;
154 // This function will check media element's show poster flag to decide whether
155 // we need to run `TimeMarchesOn`.
156 void MaybeRunTimeMarchesOn();
158 class ShutdownObserverProxy final : public nsIObserver {
159 NS_DECL_ISUPPORTS
161 public:
162 explicit ShutdownObserverProxy(TextTrackManager* aManager)
163 : mManager(aManager) {
164 nsContentUtils::RegisterShutdownObserver(this);
167 NS_IMETHODIMP Observe(nsISupports* aSubject, const char* aTopic,
168 const char16_t* aData) override {
169 MOZ_ASSERT(NS_IsMainThread());
170 if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
171 if (mManager) {
172 mManager->NotifyShutdown();
174 Unregister();
176 return NS_OK;
179 void Unregister();
181 private:
182 ~ShutdownObserverProxy() = default;
184 TextTrackManager* mManager;
187 RefPtr<ShutdownObserverProxy> mShutdownProxy;
188 bool mShutdown;
191 } // namespace dom
192 } // namespace mozilla
194 #endif // mozilla_dom_TextTrackManager_h