|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "mozilla/dom/TextTrackList.h" |
|
7 #include "mozilla/dom/TextTrackListBinding.h" |
|
8 #include "mozilla/dom/TrackEvent.h" |
|
9 #include "nsThreadUtils.h" |
|
10 #include "mozilla/dom/TextTrackCue.h" |
|
11 #include "mozilla/dom/TextTrackManager.h" |
|
12 |
|
13 namespace mozilla { |
|
14 namespace dom { |
|
15 |
|
16 NS_IMPL_CYCLE_COLLECTION_INHERITED(TextTrackList, |
|
17 DOMEventTargetHelper, |
|
18 mTextTracks, |
|
19 mTextTrackManager) |
|
20 |
|
21 NS_IMPL_ADDREF_INHERITED(TextTrackList, DOMEventTargetHelper) |
|
22 NS_IMPL_RELEASE_INHERITED(TextTrackList, DOMEventTargetHelper) |
|
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TextTrackList) |
|
24 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) |
|
25 |
|
26 TextTrackList::TextTrackList(nsPIDOMWindow* aOwnerWindow) |
|
27 : DOMEventTargetHelper(aOwnerWindow) |
|
28 { |
|
29 } |
|
30 |
|
31 TextTrackList::TextTrackList(nsPIDOMWindow* aOwnerWindow, |
|
32 TextTrackManager* aTextTrackManager) |
|
33 : DOMEventTargetHelper(aOwnerWindow) |
|
34 , mTextTrackManager(aTextTrackManager) |
|
35 { |
|
36 } |
|
37 |
|
38 void |
|
39 TextTrackList::UpdateAndGetShowingCues(nsTArray<nsRefPtr<TextTrackCue> >& aCues) |
|
40 { |
|
41 nsTArray< nsRefPtr<TextTrackCue> > cues; |
|
42 for (uint32_t i = 0; i < Length(); i++) { |
|
43 TextTrackMode mode = mTextTracks[i]->Mode(); |
|
44 // If the mode is hidden then we just need to update the active cue list, |
|
45 // we don't need to show it on the video. |
|
46 if (mode == TextTrackMode::Hidden) { |
|
47 mTextTracks[i]->UpdateActiveCueList(); |
|
48 } else if (mode == TextTrackMode::Showing) { |
|
49 // If the mode is showing then we need to update the cue list and show it |
|
50 // on the video. GetActiveCueArray() calls UpdateActiveCueList() so we |
|
51 // don't need to call it explicitly. |
|
52 mTextTracks[i]->GetActiveCueArray(cues); |
|
53 aCues.AppendElements(cues); |
|
54 } |
|
55 } |
|
56 } |
|
57 |
|
58 JSObject* |
|
59 TextTrackList::WrapObject(JSContext* aCx) |
|
60 { |
|
61 return TextTrackListBinding::Wrap(aCx, this); |
|
62 } |
|
63 |
|
64 TextTrack* |
|
65 TextTrackList::IndexedGetter(uint32_t aIndex, bool& aFound) |
|
66 { |
|
67 aFound = aIndex < mTextTracks.Length(); |
|
68 return aFound ? mTextTracks[aIndex] : nullptr; |
|
69 } |
|
70 |
|
71 TextTrack* |
|
72 TextTrackList::operator[](uint32_t aIndex) |
|
73 { |
|
74 return mTextTracks.SafeElementAt(aIndex, nullptr); |
|
75 } |
|
76 |
|
77 already_AddRefed<TextTrack> |
|
78 TextTrackList::AddTextTrack(TextTrackKind aKind, |
|
79 const nsAString& aLabel, |
|
80 const nsAString& aLanguage, |
|
81 TextTrackMode aMode, |
|
82 TextTrackReadyState aReadyState, |
|
83 TextTrackSource aTextTrackSource, |
|
84 const CompareTextTracks& aCompareTT) |
|
85 { |
|
86 nsRefPtr<TextTrack> track = new TextTrack(GetOwner(), this, aKind, aLabel, |
|
87 aLanguage, aMode, aReadyState, |
|
88 aTextTrackSource); |
|
89 AddTextTrack(track, aCompareTT); |
|
90 return track.forget(); |
|
91 } |
|
92 |
|
93 void |
|
94 TextTrackList::AddTextTrack(TextTrack* aTextTrack, |
|
95 const CompareTextTracks& aCompareTT) |
|
96 { |
|
97 if (mTextTracks.InsertElementSorted(aTextTrack, aCompareTT)) { |
|
98 aTextTrack->SetTextTrackList(this); |
|
99 CreateAndDispatchTrackEventRunner(aTextTrack, NS_LITERAL_STRING("addtrack")); |
|
100 } |
|
101 } |
|
102 |
|
103 TextTrack* |
|
104 TextTrackList::GetTrackById(const nsAString& aId) |
|
105 { |
|
106 nsAutoString id; |
|
107 for (uint32_t i = 0; i < Length(); i++) { |
|
108 mTextTracks[i]->GetId(id); |
|
109 if (aId.Equals(id)) { |
|
110 return mTextTracks[i]; |
|
111 } |
|
112 } |
|
113 return nullptr; |
|
114 } |
|
115 |
|
116 void |
|
117 TextTrackList::RemoveTextTrack(TextTrack* aTrack) |
|
118 { |
|
119 if (mTextTracks.RemoveElement(aTrack)) { |
|
120 CreateAndDispatchTrackEventRunner(aTrack, NS_LITERAL_STRING("removetrack")); |
|
121 } |
|
122 } |
|
123 |
|
124 void |
|
125 TextTrackList::DidSeek() |
|
126 { |
|
127 for (uint32_t i = 0; i < mTextTracks.Length(); i++) { |
|
128 mTextTracks[i]->SetDirty(); |
|
129 } |
|
130 } |
|
131 |
|
132 class TrackEventRunner MOZ_FINAL: public nsRunnable |
|
133 { |
|
134 public: |
|
135 TrackEventRunner(TextTrackList* aList, nsIDOMEvent* aEvent) |
|
136 : mList(aList) |
|
137 , mEvent(aEvent) |
|
138 {} |
|
139 |
|
140 NS_IMETHOD Run() MOZ_OVERRIDE |
|
141 { |
|
142 return mList->DispatchTrackEvent(mEvent); |
|
143 } |
|
144 |
|
145 private: |
|
146 nsRefPtr<TextTrackList> mList; |
|
147 nsRefPtr<nsIDOMEvent> mEvent; |
|
148 }; |
|
149 |
|
150 nsresult |
|
151 TextTrackList::DispatchTrackEvent(nsIDOMEvent* aEvent) |
|
152 { |
|
153 return DispatchTrustedEvent(aEvent); |
|
154 } |
|
155 |
|
156 void |
|
157 TextTrackList::CreateAndDispatchChangeEvent() |
|
158 { |
|
159 nsCOMPtr<nsIDOMEvent> event; |
|
160 nsresult rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr); |
|
161 if (NS_FAILED(rv)) { |
|
162 NS_WARNING("Failed to create the error event!"); |
|
163 return; |
|
164 } |
|
165 |
|
166 rv = event->InitEvent(NS_LITERAL_STRING("change"), false, false); |
|
167 if (NS_FAILED(rv)) { |
|
168 NS_WARNING("Failed to init the change event!"); |
|
169 return; |
|
170 } |
|
171 |
|
172 event->SetTrusted(true); |
|
173 |
|
174 nsCOMPtr<nsIRunnable> eventRunner = new TrackEventRunner(this, event); |
|
175 NS_DispatchToMainThread(eventRunner, NS_DISPATCH_NORMAL); |
|
176 } |
|
177 |
|
178 void |
|
179 TextTrackList::CreateAndDispatchTrackEventRunner(TextTrack* aTrack, |
|
180 const nsAString& aEventName) |
|
181 { |
|
182 TrackEventInit eventInit; |
|
183 eventInit.mBubbles = false; |
|
184 eventInit.mCancelable = false; |
|
185 eventInit.mTrack = aTrack; |
|
186 nsRefPtr<TrackEvent> event = |
|
187 TrackEvent::Constructor(this, aEventName, eventInit); |
|
188 |
|
189 // Dispatch the TrackEvent asynchronously. |
|
190 nsCOMPtr<nsIRunnable> eventRunner = new TrackEventRunner(this, event); |
|
191 NS_DispatchToMainThread(eventRunner, NS_DISPATCH_NORMAL); |
|
192 } |
|
193 |
|
194 HTMLMediaElement* |
|
195 TextTrackList::GetMediaElement() |
|
196 { |
|
197 if (mTextTrackManager) { |
|
198 return mTextTrackManager->mMediaElement; |
|
199 } |
|
200 return nullptr; |
|
201 } |
|
202 |
|
203 void |
|
204 TextTrackList::SetTextTrackManager(TextTrackManager* aTextTrackManager) |
|
205 { |
|
206 mTextTrackManager = aTextTrackManager; |
|
207 } |
|
208 |
|
209 } // namespace dom |
|
210 } // namespace mozilla |