2013-05-21 16:14:00 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/TextTrackList.h"
|
|
|
|
#include "mozilla/dom/TextTrackListBinding.h"
|
2013-10-04 18:28:25 +00:00
|
|
|
#include "mozilla/dom/TrackEvent.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2013-12-12 15:57:21 +00:00
|
|
|
#include "mozilla/dom/TextTrackCue.h"
|
2014-01-27 19:51:28 +00:00
|
|
|
#include "mozilla/dom/TextTrackManager.h"
|
2013-05-21 16:14:00 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-01-27 19:51:28 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_3(TextTrackList,
|
2013-08-15 15:11:15 +00:00
|
|
|
nsDOMEventTargetHelper,
|
|
|
|
mGlobal,
|
2014-01-27 19:51:28 +00:00
|
|
|
mTextTracks,
|
|
|
|
mTextTrackManager)
|
2013-05-21 16:14:00 +00:00
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(TextTrackList, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(TextTrackList, nsDOMEventTargetHelper)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TextTrackList)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
TextTrackList::TextTrackList(nsISupports* aGlobal) : mGlobal(aGlobal)
|
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
2014-01-27 19:51:28 +00:00
|
|
|
TextTrackList::TextTrackList(nsISupports* aGlobal, TextTrackManager* aTextTrackManager)
|
|
|
|
: mGlobal(aGlobal)
|
|
|
|
, mTextTrackManager(aTextTrackManager)
|
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
2013-12-12 15:57:21 +00:00
|
|
|
void
|
|
|
|
TextTrackList::GetAllActiveCues(nsTArray<nsRefPtr<TextTrackCue> >& aCues)
|
|
|
|
{
|
|
|
|
nsTArray< nsRefPtr<TextTrackCue> > cues;
|
|
|
|
for (uint32_t i = 0; i < Length(); i++) {
|
|
|
|
if (mTextTracks[i]->Mode() != TextTrackMode::Disabled) {
|
|
|
|
mTextTracks[i]->GetActiveCueArray(cues);
|
|
|
|
aCues.AppendElements(cues);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:14:00 +00:00
|
|
|
JSObject*
|
|
|
|
TextTrackList::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return TextTrackListBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
TextTrack*
|
|
|
|
TextTrackList::IndexedGetter(uint32_t aIndex, bool& aFound)
|
|
|
|
{
|
|
|
|
aFound = aIndex < mTextTracks.Length();
|
|
|
|
return aFound ? mTextTracks[aIndex] : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<TextTrack>
|
2014-01-27 20:17:20 +00:00
|
|
|
TextTrackList::AddTextTrack(TextTrackKind aKind,
|
2013-05-21 16:14:00 +00:00
|
|
|
const nsAString& aLabel,
|
|
|
|
const nsAString& aLanguage)
|
|
|
|
{
|
2014-01-27 20:17:20 +00:00
|
|
|
nsRefPtr<TextTrack> track = new TextTrack(mGlobal, this, aKind, aLabel, aLanguage);
|
2014-02-13 18:28:21 +00:00
|
|
|
AddTextTrack(track);
|
2013-05-21 16:14:00 +00:00
|
|
|
return track.forget();
|
|
|
|
}
|
|
|
|
|
2014-02-13 18:28:21 +00:00
|
|
|
void
|
|
|
|
TextTrackList::AddTextTrack(TextTrack* aTextTrack)
|
|
|
|
{
|
|
|
|
if (mTextTracks.AppendElement(aTextTrack)) {
|
|
|
|
aTextTrack->SetTextTrackList(this);
|
|
|
|
CreateAndDispatchTrackEventRunner(aTextTrack, NS_LITERAL_STRING("addtrack"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 15:44:35 +00:00
|
|
|
TextTrack*
|
|
|
|
TextTrackList::GetTrackById(const nsAString& aId)
|
|
|
|
{
|
|
|
|
nsAutoString id;
|
|
|
|
for (uint32_t i = 0; i < Length(); i++) {
|
|
|
|
mTextTracks[i]->GetId(id);
|
|
|
|
if (aId.Equals(id)) {
|
|
|
|
return mTextTracks[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:14:00 +00:00
|
|
|
void
|
2013-10-21 14:15:00 +00:00
|
|
|
TextTrackList::RemoveTextTrack(TextTrack* aTrack)
|
2013-05-21 16:14:00 +00:00
|
|
|
{
|
2013-10-21 14:15:00 +00:00
|
|
|
if (mTextTracks.RemoveElement(aTrack)) {
|
|
|
|
CreateAndDispatchTrackEventRunner(aTrack, NS_LITERAL_STRING("removetrack"));
|
2013-10-04 18:28:25 +00:00
|
|
|
}
|
2013-05-21 16:14:00 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 20:10:36 +00:00
|
|
|
void
|
|
|
|
TextTrackList::DidSeek()
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mTextTracks.Length(); i++) {
|
|
|
|
mTextTracks[i]->SetDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 18:28:25 +00:00
|
|
|
class TrackEventRunner MOZ_FINAL: public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-12-18 05:19:09 +00:00
|
|
|
TrackEventRunner(TextTrackList* aList, nsIDOMEvent* aEvent)
|
2013-10-04 18:28:25 +00:00
|
|
|
: mList(aList)
|
|
|
|
, mEvent(aEvent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mList->DispatchTrackEvent(mEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<TextTrackList> mList;
|
2013-12-18 05:19:09 +00:00
|
|
|
nsRefPtr<nsIDOMEvent> mEvent;
|
2013-10-04 18:28:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
2013-12-18 05:19:09 +00:00
|
|
|
TextTrackList::DispatchTrackEvent(nsIDOMEvent* aEvent)
|
2013-10-04 18:28:25 +00:00
|
|
|
{
|
|
|
|
return DispatchTrustedEvent(aEvent);
|
|
|
|
}
|
|
|
|
|
2013-12-18 05:19:09 +00:00
|
|
|
void
|
|
|
|
TextTrackList::CreateAndDispatchChangeEvent()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMEvent> event;
|
|
|
|
nsresult rv = NS_NewDOMEvent(getter_AddRefs(event), this, nullptr, nullptr);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to create the error event!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = event->InitEvent(NS_LITERAL_STRING("change"), false, false);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to init the change event!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> eventRunner = new TrackEventRunner(this, event);
|
|
|
|
NS_DispatchToMainThread(eventRunner, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
2013-10-04 18:28:25 +00:00
|
|
|
void
|
|
|
|
TextTrackList::CreateAndDispatchTrackEventRunner(TextTrack* aTrack,
|
|
|
|
const nsAString& aEventName)
|
|
|
|
{
|
2013-10-09 16:05:22 +00:00
|
|
|
TrackEventInit eventInit;
|
2013-10-04 18:28:25 +00:00
|
|
|
eventInit.mBubbles = false;
|
|
|
|
eventInit.mCancelable = false;
|
|
|
|
eventInit.mTrack = aTrack;
|
2013-12-18 05:19:09 +00:00
|
|
|
nsRefPtr<TrackEvent> event =
|
2013-10-04 18:28:25 +00:00
|
|
|
TrackEvent::Constructor(this, aEventName, eventInit);
|
|
|
|
|
|
|
|
// Dispatch the TrackEvent asynchronously.
|
2013-12-18 05:19:09 +00:00
|
|
|
nsCOMPtr<nsIRunnable> eventRunner = new TrackEventRunner(this, event);
|
|
|
|
NS_DispatchToMainThread(eventRunner, NS_DISPATCH_NORMAL);
|
2013-10-04 18:28:25 +00:00
|
|
|
}
|
|
|
|
|
2014-01-27 19:51:28 +00:00
|
|
|
HTMLMediaElement*
|
|
|
|
TextTrackList::GetMediaElement()
|
|
|
|
{
|
|
|
|
if (mTextTrackManager) {
|
|
|
|
return mTextTrackManager->mMediaElement;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrackList::SetTextTrackManager(TextTrackManager* aTextTrackManager)
|
|
|
|
{
|
|
|
|
mTextTrackManager = aTextTrackManager;
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:14:00 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|