Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / dom / media / webvtt / TextTrackCueList.cpp
blobd6fb8baedc65587200f773b9091ac9b1a729334c
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/TextTrackCueList.h"
7 #include "mozilla/dom/TextTrackCueListBinding.h"
8 #include "mozilla/dom/TextTrackCue.h"
10 namespace mozilla::dom {
12 class CompareCuesByTime {
13 public:
14 bool Equals(TextTrackCue* aOne, TextTrackCue* aTwo) const { return false; }
15 bool LessThan(TextTrackCue* aOne, TextTrackCue* aTwo) const {
16 return aOne->StartTime() < aTwo->StartTime() ||
17 (aOne->StartTime() == aTwo->StartTime() &&
18 aOne->EndTime() >= aTwo->EndTime());
22 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TextTrackCueList, mParent, mList)
24 NS_IMPL_CYCLE_COLLECTING_ADDREF(TextTrackCueList)
25 NS_IMPL_CYCLE_COLLECTING_RELEASE(TextTrackCueList)
26 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TextTrackCueList)
27 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
28 NS_INTERFACE_MAP_ENTRY(nsISupports)
29 NS_INTERFACE_MAP_END
31 TextTrackCueList::TextTrackCueList(nsISupports* aParent) : mParent(aParent) {}
33 TextTrackCueList::~TextTrackCueList() = default;
35 JSObject* TextTrackCueList::WrapObject(JSContext* aCx,
36 JS::Handle<JSObject*> aGivenProto) {
37 return TextTrackCueList_Binding::Wrap(aCx, this, aGivenProto);
40 TextTrackCue* TextTrackCueList::IndexedGetter(uint32_t aIndex, bool& aFound) {
41 aFound = aIndex < mList.Length();
42 if (!aFound) {
43 return nullptr;
45 return mList[aIndex];
48 TextTrackCue* TextTrackCueList::operator[](uint32_t aIndex) {
49 return mList.SafeElementAt(aIndex, nullptr);
52 TextTrackCueList& TextTrackCueList::operator=(const TextTrackCueList& aOther) {
53 mList = aOther.mList.Clone();
54 return *this;
57 TextTrackCue* TextTrackCueList::GetCueById(const nsAString& aId) {
58 if (aId.IsEmpty()) {
59 return nullptr;
62 for (uint32_t i = 0; i < mList.Length(); i++) {
63 if (aId.Equals(mList[i]->Id())) {
64 return mList[i];
67 return nullptr;
70 void TextTrackCueList::AddCue(TextTrackCue& aCue) {
71 if (mList.Contains(&aCue)) {
72 return;
74 mList.InsertElementSorted(&aCue, CompareCuesByTime());
77 void TextTrackCueList::RemoveCue(TextTrackCue& aCue, ErrorResult& aRv) {
78 if (!mList.Contains(&aCue)) {
79 aRv.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
80 return;
82 mList.RemoveElement(&aCue);
85 void TextTrackCueList::RemoveCue(TextTrackCue& aCue) {
86 mList.RemoveElement(&aCue);
89 void TextTrackCueList::RemoveCueAt(uint32_t aIndex) {
90 if (aIndex < mList.Length()) {
91 mList.RemoveElementAt(aIndex);
95 void TextTrackCueList::RemoveAll() { mList.Clear(); }
97 void TextTrackCueList::GetArray(nsTArray<RefPtr<TextTrackCue>>& aCues) {
98 aCues = mList.Clone();
101 void TextTrackCueList::SetCuesInactive() {
102 for (uint32_t i = 0; i < mList.Length(); ++i) {
103 mList[i]->SetActive(false);
107 void TextTrackCueList::NotifyCueUpdated(TextTrackCue* aCue) {
108 if (aCue) {
109 mList.RemoveElement(aCue);
110 mList.InsertElementSorted(aCue, CompareCuesByTime());
114 bool TextTrackCueList::IsCueExist(TextTrackCue* aCue) {
115 if (aCue && mList.Contains(aCue)) {
116 return true;
118 return false;
121 nsTArray<RefPtr<TextTrackCue>>& TextTrackCueList::GetCuesArray() {
122 return mList;
125 } // namespace mozilla::dom