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
{
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
)
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();
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();
57 TextTrackCue
* TextTrackCueList::GetCueById(const nsAString
& aId
) {
62 for (uint32_t i
= 0; i
< mList
.Length(); i
++) {
63 if (aId
.Equals(mList
[i
]->Id())) {
70 void TextTrackCueList::AddCue(TextTrackCue
& aCue
) {
71 if (mList
.Contains(&aCue
)) {
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
);
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
) {
109 mList
.RemoveElement(aCue
);
110 mList
.InsertElementSorted(aCue
, CompareCuesByTime());
114 bool TextTrackCueList::IsCueExist(TextTrackCue
* aCue
) {
115 if (aCue
&& mList
.Contains(aCue
)) {
121 nsTArray
<RefPtr
<TextTrackCue
>>& TextTrackCueList::GetCuesArray() {
125 } // namespace mozilla::dom