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