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
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "SourceBufferList.h"
9 #include "AsyncEventRunner.h"
10 #include "mozilla/ErrorResult.h"
11 #include "mozilla/dom/SourceBufferListBinding.h"
12 #include "mozilla/mozalloc.h"
13 #include "nsCOMPtr.h"
14 #include "nsIEventTarget.h"
15 #include "nsIRunnable.h"
16 #include "nsStringGlue.h"
17 #include "nsThreadUtils.h"
18 #include "prlog.h"
20 struct JSContext;
21 class JSObject;
23 #ifdef PR_LOGGING
24 extern PRLogModuleInfo* gMediaSourceLog;
25 #define MSE_DEBUG(...) PR_LOG(gMediaSourceLog, PR_LOG_DEBUG, (__VA_ARGS__))
26 #else
27 #define MSE_DEBUG(...)
28 #endif
30 namespace mozilla {
32 namespace dom {
34 SourceBuffer*
35 SourceBufferList::IndexedGetter(uint32_t aIndex, bool& aFound)
36 {
37 aFound = aIndex < mSourceBuffers.Length();
38 return aFound ? mSourceBuffers[aIndex] : nullptr;
39 }
41 uint32_t
42 SourceBufferList::Length()
43 {
44 return mSourceBuffers.Length();
45 }
47 void
48 SourceBufferList::Append(SourceBuffer* aSourceBuffer)
49 {
50 mSourceBuffers.AppendElement(aSourceBuffer);
51 QueueAsyncSimpleEvent("addsourcebuffer");
52 }
54 void
55 SourceBufferList::Remove(SourceBuffer* aSourceBuffer)
56 {
57 MOZ_ALWAYS_TRUE(mSourceBuffers.RemoveElement(aSourceBuffer));
58 aSourceBuffer->Detach();
59 QueueAsyncSimpleEvent("removesourcebuffer");
60 }
62 bool
63 SourceBufferList::Contains(SourceBuffer* aSourceBuffer)
64 {
65 return mSourceBuffers.Contains(aSourceBuffer);
66 }
68 void
69 SourceBufferList::Clear()
70 {
71 for (uint32_t i = 0; i < mSourceBuffers.Length(); ++i) {
72 mSourceBuffers[i]->Detach();
73 }
74 mSourceBuffers.Clear();
75 QueueAsyncSimpleEvent("removesourcebuffer");
76 }
78 bool
79 SourceBufferList::IsEmpty()
80 {
81 return mSourceBuffers.IsEmpty();
82 }
84 bool
85 SourceBufferList::AnyUpdating()
86 {
87 for (uint32_t i = 0; i < mSourceBuffers.Length(); ++i) {
88 if (mSourceBuffers[i]->Updating()) {
89 return true;
90 }
91 }
92 return false;
93 }
95 void
96 SourceBufferList::Remove(double aStart, double aEnd, ErrorResult& aRv)
97 {
98 for (uint32_t i = 0; i < mSourceBuffers.Length(); ++i) {
99 mSourceBuffers[i]->Remove(aStart, aEnd, aRv);
100 if (aRv.Failed()) {
101 return;
102 }
103 }
104 }
106 void
107 SourceBufferList::Evict(double aStart, double aEnd)
108 {
109 for (uint32_t i = 0; i < mSourceBuffers.Length(); ++i) {
110 mSourceBuffers[i]->Evict(aStart, aEnd);
111 }
112 }
114 bool
115 SourceBufferList::AllContainsTime(double aTime)
116 {
117 for (uint32_t i = 0; i < mSourceBuffers.Length(); ++i) {
118 if (!mSourceBuffers[i]->ContainsTime(aTime)) {
119 return false;
120 }
121 }
122 return mSourceBuffers.Length() > 0;
123 }
125 void
126 SourceBufferList::Ended()
127 {
128 for (uint32_t i = 0; i < mSourceBuffers.Length(); ++i) {
129 mSourceBuffers[i]->Ended();
130 }
131 }
133 void
134 SourceBufferList::DispatchSimpleEvent(const char* aName)
135 {
136 MSE_DEBUG("%p Dispatching event %s to SourceBufferList", this, aName);
137 DispatchTrustedEvent(NS_ConvertUTF8toUTF16(aName));
138 }
140 void
141 SourceBufferList::QueueAsyncSimpleEvent(const char* aName)
142 {
143 MSE_DEBUG("%p Queuing event %s to SourceBufferList", this, aName);
144 nsCOMPtr<nsIRunnable> event = new AsyncEventRunner<SourceBufferList>(this, aName);
145 NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL);
146 }
148 SourceBufferList::SourceBufferList(MediaSource* aMediaSource)
149 : DOMEventTargetHelper(aMediaSource->GetParentObject())
150 , mMediaSource(aMediaSource)
151 {
152 MOZ_ASSERT(aMediaSource);
153 }
155 MediaSource*
156 SourceBufferList::GetParentObject() const
157 {
158 return mMediaSource;
159 }
161 JSObject*
162 SourceBufferList::WrapObject(JSContext* aCx)
163 {
164 return SourceBufferListBinding::Wrap(aCx, this);
165 }
167 NS_IMPL_CYCLE_COLLECTION_INHERITED(SourceBufferList, DOMEventTargetHelper,
168 mMediaSource, mSourceBuffers)
170 NS_IMPL_ADDREF_INHERITED(SourceBufferList, DOMEventTargetHelper)
171 NS_IMPL_RELEASE_INHERITED(SourceBufferList, DOMEventTargetHelper)
173 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(SourceBufferList)
174 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
176 } // namespace dom
178 } // namespace mozilla