Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
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"
13 namespace mozilla {
14 namespace dom {
16 NS_IMPL_CYCLE_COLLECTION_INHERITED(TextTrackList,
17 DOMEventTargetHelper,
18 mTextTracks,
19 mTextTrackManager)
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)
26 TextTrackList::TextTrackList(nsPIDOMWindow* aOwnerWindow)
27 : DOMEventTargetHelper(aOwnerWindow)
28 {
29 }
31 TextTrackList::TextTrackList(nsPIDOMWindow* aOwnerWindow,
32 TextTrackManager* aTextTrackManager)
33 : DOMEventTargetHelper(aOwnerWindow)
34 , mTextTrackManager(aTextTrackManager)
35 {
36 }
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 }
58 JSObject*
59 TextTrackList::WrapObject(JSContext* aCx)
60 {
61 return TextTrackListBinding::Wrap(aCx, this);
62 }
64 TextTrack*
65 TextTrackList::IndexedGetter(uint32_t aIndex, bool& aFound)
66 {
67 aFound = aIndex < mTextTracks.Length();
68 return aFound ? mTextTracks[aIndex] : nullptr;
69 }
71 TextTrack*
72 TextTrackList::operator[](uint32_t aIndex)
73 {
74 return mTextTracks.SafeElementAt(aIndex, nullptr);
75 }
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 }
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 }
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 }
116 void
117 TextTrackList::RemoveTextTrack(TextTrack* aTrack)
118 {
119 if (mTextTracks.RemoveElement(aTrack)) {
120 CreateAndDispatchTrackEventRunner(aTrack, NS_LITERAL_STRING("removetrack"));
121 }
122 }
124 void
125 TextTrackList::DidSeek()
126 {
127 for (uint32_t i = 0; i < mTextTracks.Length(); i++) {
128 mTextTracks[i]->SetDirty();
129 }
130 }
132 class TrackEventRunner MOZ_FINAL: public nsRunnable
133 {
134 public:
135 TrackEventRunner(TextTrackList* aList, nsIDOMEvent* aEvent)
136 : mList(aList)
137 , mEvent(aEvent)
138 {}
140 NS_IMETHOD Run() MOZ_OVERRIDE
141 {
142 return mList->DispatchTrackEvent(mEvent);
143 }
145 private:
146 nsRefPtr<TextTrackList> mList;
147 nsRefPtr<nsIDOMEvent> mEvent;
148 };
150 nsresult
151 TextTrackList::DispatchTrackEvent(nsIDOMEvent* aEvent)
152 {
153 return DispatchTrustedEvent(aEvent);
154 }
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 }
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 }
172 event->SetTrusted(true);
174 nsCOMPtr<nsIRunnable> eventRunner = new TrackEventRunner(this, event);
175 NS_DispatchToMainThread(eventRunner, NS_DISPATCH_NORMAL);
176 }
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);
189 // Dispatch the TrackEvent asynchronously.
190 nsCOMPtr<nsIRunnable> eventRunner = new TrackEventRunner(this, event);
191 NS_DispatchToMainThread(eventRunner, NS_DISPATCH_NORMAL);
192 }
194 HTMLMediaElement*
195 TextTrackList::GetMediaElement()
196 {
197 if (mTextTrackManager) {
198 return mTextTrackManager->mMediaElement;
199 }
200 return nullptr;
201 }
203 void
204 TextTrackList::SetTextTrackManager(TextTrackManager* aTextTrackManager)
205 {
206 mTextTrackManager = aTextTrackManager;
207 }
209 } // namespace dom
210 } // namespace mozilla