Line data Source code
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/. */
5 :
6 : #include "mozilla/dom/TextTrackCueList.h"
7 : #include "mozilla/dom/TextTrackCueListBinding.h"
8 : #include "mozilla/dom/TextTrackCue.h"
9 :
10 : namespace mozilla {
11 : namespace dom {
12 :
13 : class CompareCuesByTime
14 : {
15 : public:
16 0 : bool Equals(TextTrackCue* aOne, TextTrackCue* aTwo) const {
17 0 : return false;
18 : }
19 0 : bool LessThan(TextTrackCue* aOne, TextTrackCue* aTwo) const {
20 0 : return aOne->StartTime() < aTwo->StartTime() ||
21 0 : (aOne->StartTime() == aTwo->StartTime() &&
22 0 : aOne->EndTime() >= aTwo->EndTime());
23 : }
24 : };
25 :
26 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TextTrackCueList, mParent, mList)
27 :
28 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(TextTrackCueList)
29 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(TextTrackCueList)
30 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TextTrackCueList)
31 0 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
32 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
33 0 : NS_INTERFACE_MAP_END
34 :
35 0 : TextTrackCueList::TextTrackCueList(nsISupports* aParent) : mParent(aParent)
36 : {
37 0 : }
38 :
39 0 : TextTrackCueList::~TextTrackCueList()
40 0 : {}
41 :
42 : JSObject*
43 0 : TextTrackCueList::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
44 : {
45 0 : return TextTrackCueListBinding::Wrap(aCx, this, aGivenProto);
46 : }
47 :
48 : TextTrackCue*
49 0 : TextTrackCueList::IndexedGetter(uint32_t aIndex, bool& aFound)
50 : {
51 0 : aFound = aIndex < mList.Length();
52 0 : if (!aFound) {
53 0 : return nullptr;
54 : }
55 0 : return mList[aIndex];
56 : }
57 :
58 : TextTrackCue*
59 0 : TextTrackCueList::operator[](uint32_t aIndex)
60 : {
61 0 : return mList.SafeElementAt(aIndex, nullptr);
62 : }
63 :
64 : TextTrackCueList&
65 0 : TextTrackCueList::operator=(const TextTrackCueList& aOther)
66 : {
67 0 : mList = aOther.mList;
68 0 : return *this;
69 : }
70 :
71 : TextTrackCue*
72 0 : TextTrackCueList::GetCueById(const nsAString& aId)
73 : {
74 0 : if (aId.IsEmpty()) {
75 0 : return nullptr;
76 : }
77 :
78 0 : for (uint32_t i = 0; i < mList.Length(); i++) {
79 0 : if (aId.Equals(mList[i]->Id())) {
80 0 : return mList[i];
81 : }
82 : }
83 0 : return nullptr;
84 : }
85 :
86 : void
87 0 : TextTrackCueList::AddCue(TextTrackCue& aCue)
88 : {
89 0 : if (mList.Contains(&aCue)) {
90 0 : return;
91 : }
92 0 : mList.InsertElementSorted(&aCue, CompareCuesByTime());
93 : }
94 :
95 : void
96 0 : TextTrackCueList::RemoveCue(TextTrackCue& aCue, ErrorResult& aRv)
97 : {
98 0 : if (!mList.Contains(&aCue)) {
99 0 : aRv.Throw(NS_ERROR_DOM_NOT_FOUND_ERR);
100 0 : return;
101 : }
102 0 : mList.RemoveElement(&aCue);
103 : }
104 :
105 : void
106 0 : TextTrackCueList::RemoveCue(TextTrackCue& aCue)
107 : {
108 0 : mList.RemoveElement(&aCue);
109 0 : }
110 :
111 : void
112 0 : TextTrackCueList::RemoveCueAt(uint32_t aIndex)
113 : {
114 0 : if (aIndex < mList.Length()) {
115 0 : mList.RemoveElementAt(aIndex);
116 : }
117 0 : }
118 :
119 : void
120 0 : TextTrackCueList::RemoveAll()
121 : {
122 0 : mList.Clear();
123 0 : }
124 :
125 : void
126 0 : TextTrackCueList::GetArray(nsTArray<RefPtr<TextTrackCue> >& aCues)
127 : {
128 0 : aCues = nsTArray<RefPtr<TextTrackCue> >(mList);
129 0 : }
130 :
131 :
132 : void
133 0 : TextTrackCueList::SetCuesInactive()
134 : {
135 0 : for(uint32_t i = 0; i < mList.Length(); ++i) {
136 0 : mList[i]->SetActive(false);
137 : }
138 0 : }
139 :
140 : already_AddRefed<TextTrackCueList>
141 0 : TextTrackCueList::GetCueListByTimeInterval(media::Interval<double>& aInterval)
142 : {
143 0 : RefPtr<TextTrackCueList> output = new TextTrackCueList(mParent);
144 0 : for (uint32_t i = 0; i < mList.Length(); ++i) {
145 0 : TextTrackCue* cue = mList[i];
146 0 : if (cue->StartTime() <= aInterval.mEnd &&
147 0 : aInterval.mStart <= cue->EndTime()) {
148 0 : output->AddCue(*cue);
149 : }
150 : }
151 0 : return output.forget();
152 : }
153 :
154 : void
155 0 : TextTrackCueList::NotifyCueUpdated(TextTrackCue *aCue)
156 : {
157 0 : if (aCue) {
158 0 : mList.RemoveElement(aCue);
159 0 : mList.InsertElementSorted(aCue, CompareCuesByTime());
160 : }
161 0 : }
162 :
163 : bool
164 0 : TextTrackCueList::IsCueExist(TextTrackCue *aCue)
165 : {
166 0 : if (aCue && mList.Contains(aCue)) {
167 0 : return true;
168 : }
169 0 : return false;
170 : }
171 :
172 : nsTArray<RefPtr<TextTrackCue>>&
173 0 : TextTrackCueList::GetCuesArray()
174 : {
175 0 : return mList;
176 : }
177 :
178 : } // namespace dom
179 9 : } // namespace mozilla
|