Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim:set ts=2 sw=2 et tw=78: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "mozilla/dom/TextTrack.h" |
michael@0 | 8 | #include "mozilla/dom/TextTrackBinding.h" |
michael@0 | 9 | #include "mozilla/dom/TextTrackList.h" |
michael@0 | 10 | #include "mozilla/dom/TextTrackCue.h" |
michael@0 | 11 | #include "mozilla/dom/TextTrackCueList.h" |
michael@0 | 12 | #include "mozilla/dom/TextTrackRegion.h" |
michael@0 | 13 | #include "mozilla/dom/HTMLMediaElement.h" |
michael@0 | 14 | #include "mozilla/dom/HTMLTrackElement.h" |
michael@0 | 15 | |
michael@0 | 16 | namespace mozilla { |
michael@0 | 17 | namespace dom { |
michael@0 | 18 | |
michael@0 | 19 | NS_IMPL_CYCLE_COLLECTION_INHERITED(TextTrack, |
michael@0 | 20 | DOMEventTargetHelper, |
michael@0 | 21 | mCueList, |
michael@0 | 22 | mActiveCueList, |
michael@0 | 23 | mTextTrackList, |
michael@0 | 24 | mTrackElement) |
michael@0 | 25 | |
michael@0 | 26 | NS_IMPL_ADDREF_INHERITED(TextTrack, DOMEventTargetHelper) |
michael@0 | 27 | NS_IMPL_RELEASE_INHERITED(TextTrack, DOMEventTargetHelper) |
michael@0 | 28 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TextTrack) |
michael@0 | 29 | NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) |
michael@0 | 30 | |
michael@0 | 31 | TextTrack::TextTrack(nsPIDOMWindow* aOwnerWindow, |
michael@0 | 32 | TextTrackKind aKind, |
michael@0 | 33 | const nsAString& aLabel, |
michael@0 | 34 | const nsAString& aLanguage, |
michael@0 | 35 | TextTrackMode aMode, |
michael@0 | 36 | TextTrackReadyState aReadyState, |
michael@0 | 37 | TextTrackSource aTextTrackSource) |
michael@0 | 38 | : DOMEventTargetHelper(aOwnerWindow) |
michael@0 | 39 | , mKind(aKind) |
michael@0 | 40 | , mLabel(aLabel) |
michael@0 | 41 | , mLanguage(aLanguage) |
michael@0 | 42 | , mMode(aMode) |
michael@0 | 43 | , mReadyState(aReadyState) |
michael@0 | 44 | , mTextTrackSource(aTextTrackSource) |
michael@0 | 45 | { |
michael@0 | 46 | SetDefaultSettings(); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | TextTrack::TextTrack(nsPIDOMWindow* aOwnerWindow, |
michael@0 | 50 | TextTrackList* aTextTrackList, |
michael@0 | 51 | TextTrackKind aKind, |
michael@0 | 52 | const nsAString& aLabel, |
michael@0 | 53 | const nsAString& aLanguage, |
michael@0 | 54 | TextTrackMode aMode, |
michael@0 | 55 | TextTrackReadyState aReadyState, |
michael@0 | 56 | TextTrackSource aTextTrackSource) |
michael@0 | 57 | : DOMEventTargetHelper(aOwnerWindow) |
michael@0 | 58 | , mTextTrackList(aTextTrackList) |
michael@0 | 59 | , mKind(aKind) |
michael@0 | 60 | , mLabel(aLabel) |
michael@0 | 61 | , mLanguage(aLanguage) |
michael@0 | 62 | , mMode(aMode) |
michael@0 | 63 | , mReadyState(aReadyState) |
michael@0 | 64 | , mTextTrackSource(aTextTrackSource) |
michael@0 | 65 | { |
michael@0 | 66 | SetDefaultSettings(); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | void |
michael@0 | 70 | TextTrack::SetDefaultSettings() |
michael@0 | 71 | { |
michael@0 | 72 | nsPIDOMWindow* ownerWindow = GetOwner(); |
michael@0 | 73 | mCueList = new TextTrackCueList(ownerWindow); |
michael@0 | 74 | mActiveCueList = new TextTrackCueList(ownerWindow); |
michael@0 | 75 | mCuePos = 0; |
michael@0 | 76 | mDirty = false; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | JSObject* |
michael@0 | 80 | TextTrack::WrapObject(JSContext* aCx) |
michael@0 | 81 | { |
michael@0 | 82 | return TextTrackBinding::Wrap(aCx, this); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | void |
michael@0 | 86 | TextTrack::SetMode(TextTrackMode aValue) |
michael@0 | 87 | { |
michael@0 | 88 | if (mMode != aValue) { |
michael@0 | 89 | mMode = aValue; |
michael@0 | 90 | if (mTextTrackList) { |
michael@0 | 91 | mTextTrackList->CreateAndDispatchChangeEvent(); |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | void |
michael@0 | 97 | TextTrack::GetId(nsAString& aId) const |
michael@0 | 98 | { |
michael@0 | 99 | // If the track has a track element then its id should be the same as the |
michael@0 | 100 | // track element's id. |
michael@0 | 101 | if (mTrackElement) { |
michael@0 | 102 | mTrackElement->GetAttribute(NS_LITERAL_STRING("id"), aId); |
michael@0 | 103 | } |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | void |
michael@0 | 107 | TextTrack::AddCue(TextTrackCue& aCue) |
michael@0 | 108 | { |
michael@0 | 109 | mCueList->AddCue(aCue); |
michael@0 | 110 | aCue.SetTrack(this); |
michael@0 | 111 | if (mTextTrackList) { |
michael@0 | 112 | HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement(); |
michael@0 | 113 | if (mediaElement) { |
michael@0 | 114 | mediaElement->AddCue(aCue); |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | SetDirty(); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | void |
michael@0 | 121 | TextTrack::RemoveCue(TextTrackCue& aCue, ErrorResult& aRv) |
michael@0 | 122 | { |
michael@0 | 123 | mCueList->RemoveCue(aCue, aRv); |
michael@0 | 124 | SetDirty(); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | void |
michael@0 | 128 | TextTrack::SetCuesDirty() |
michael@0 | 129 | { |
michael@0 | 130 | for (uint32_t i = 0; i < mCueList->Length(); i++) { |
michael@0 | 131 | ((*mCueList)[i])->Reset(); |
michael@0 | 132 | } |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | void |
michael@0 | 136 | TextTrack::UpdateActiveCueList() |
michael@0 | 137 | { |
michael@0 | 138 | if (!mTextTrackList) { |
michael@0 | 139 | return; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement(); |
michael@0 | 143 | if (!mediaElement) { |
michael@0 | 144 | return; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | // If we are dirty, i.e. an event happened that may cause the sorted mCueList |
michael@0 | 148 | // to have changed like a seek or an insert for a cue, than we need to rebuild |
michael@0 | 149 | // the active cue list from scratch. |
michael@0 | 150 | if (mDirty) { |
michael@0 | 151 | mCuePos = 0; |
michael@0 | 152 | mDirty = false; |
michael@0 | 153 | mActiveCueList->RemoveAll(); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | double playbackTime = mediaElement->CurrentTime(); |
michael@0 | 157 | // Remove all the cues from the active cue list whose end times now occur |
michael@0 | 158 | // earlier then the current playback time. |
michael@0 | 159 | for (uint32_t i = mActiveCueList->Length(); i > 0; i--) { |
michael@0 | 160 | if ((*mActiveCueList)[i - 1]->EndTime() < playbackTime) { |
michael@0 | 161 | mActiveCueList->RemoveCueAt(i - 1); |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | // Add all the cues, starting from the position of the last cue that was |
michael@0 | 165 | // added, that have valid start and end times for the current playback time. |
michael@0 | 166 | // We can stop iterating safely once we encounter a cue that does not have |
michael@0 | 167 | // a valid start time as the cue list is sorted. |
michael@0 | 168 | for (; mCuePos < mCueList->Length() && |
michael@0 | 169 | (*mCueList)[mCuePos]->StartTime() <= playbackTime; mCuePos++) { |
michael@0 | 170 | if ((*mCueList)[mCuePos]->EndTime() >= playbackTime) { |
michael@0 | 171 | mActiveCueList->AddCue(*(*mCueList)[mCuePos]); |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | TextTrackCueList* |
michael@0 | 177 | TextTrack::GetActiveCues() { |
michael@0 | 178 | if (mMode != TextTrackMode::Disabled) { |
michael@0 | 179 | UpdateActiveCueList(); |
michael@0 | 180 | return mActiveCueList; |
michael@0 | 181 | } |
michael@0 | 182 | return nullptr; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | void |
michael@0 | 186 | TextTrack::GetActiveCueArray(nsTArray<nsRefPtr<TextTrackCue> >& aCues) |
michael@0 | 187 | { |
michael@0 | 188 | if (mMode != TextTrackMode::Disabled) { |
michael@0 | 189 | UpdateActiveCueList(); |
michael@0 | 190 | mActiveCueList->GetArray(aCues); |
michael@0 | 191 | } |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | TextTrackReadyState |
michael@0 | 195 | TextTrack::ReadyState() const |
michael@0 | 196 | { |
michael@0 | 197 | return mReadyState; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | void |
michael@0 | 201 | TextTrack::SetReadyState(uint32_t aReadyState) |
michael@0 | 202 | { |
michael@0 | 203 | if (aReadyState <= TextTrackReadyState::FailedToLoad) { |
michael@0 | 204 | SetReadyState(static_cast<TextTrackReadyState>(aReadyState)); |
michael@0 | 205 | } |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | void |
michael@0 | 209 | TextTrack::SetReadyState(TextTrackReadyState aState) |
michael@0 | 210 | { |
michael@0 | 211 | mReadyState = aState; |
michael@0 | 212 | |
michael@0 | 213 | if (!mTextTrackList) { |
michael@0 | 214 | return; |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement(); |
michael@0 | 218 | if (mediaElement && (mReadyState == TextTrackReadyState::Loaded|| |
michael@0 | 219 | mReadyState == TextTrackReadyState::FailedToLoad)) { |
michael@0 | 220 | mediaElement->RemoveTextTrack(this, true); |
michael@0 | 221 | } |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | TextTrackList* |
michael@0 | 225 | TextTrack::GetTextTrackList() |
michael@0 | 226 | { |
michael@0 | 227 | return mTextTrackList; |
michael@0 | 228 | } |
michael@0 | 229 | |
michael@0 | 230 | void |
michael@0 | 231 | TextTrack::SetTextTrackList(TextTrackList* aTextTrackList) |
michael@0 | 232 | { |
michael@0 | 233 | mTextTrackList = aTextTrackList; |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | HTMLTrackElement* |
michael@0 | 237 | TextTrack::GetTrackElement() { |
michael@0 | 238 | return mTrackElement; |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | void |
michael@0 | 242 | TextTrack::SetTrackElement(HTMLTrackElement* aTrackElement) { |
michael@0 | 243 | mTrackElement = aTrackElement; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | } // namespace dom |
michael@0 | 247 | } // namespace mozilla |