diff --git a/content/media/TextTrackList.cpp b/content/media/TextTrackList.cpp index f8dbc98cce64..30893c06bbcc 100644 --- a/content/media/TextTrackList.cpp +++ b/content/media/TextTrackList.cpp @@ -5,6 +5,8 @@ #include "mozilla/dom/TextTrackList.h" #include "mozilla/dom/TextTrackListBinding.h" +#include "mozilla/dom/TrackEvent.h" +#include "nsThreadUtils.h" namespace mozilla { namespace dom { @@ -54,8 +56,10 @@ TextTrackList::AddTextTrack(HTMLMediaElement* aMediaElement, { nsRefPtr track = new TextTrack(mGlobal, aMediaElement, aKind, aLabel, aLanguage); - mTextTracks.AppendElement(track); - // TODO: dispatch addtrack event + if (mTextTracks.AppendElement(track)) { + CreateAndDispatchTrackEventRunner(track, NS_LITERAL_STRING("addtrack")); + } + return track.forget(); } @@ -73,9 +77,11 @@ TextTrackList::GetTrackById(const nsAString& aId) } void -TextTrackList::RemoveTextTrack(const TextTrack& aTrack) +TextTrackList::RemoveTextTrack(TextTrack& aTrack) { - mTextTracks.RemoveElement(&aTrack); + if (mTextTracks.RemoveElement(&aTrack)) { + CreateAndDispatchTrackEventRunner(&aTrack, NS_LITERAL_STRING("removetrack")); + } } void @@ -86,5 +92,45 @@ TextTrackList::DidSeek() } } +class TrackEventRunner MOZ_FINAL: public nsRunnable +{ +public: + TrackEventRunner(TextTrackList* aList, TrackEvent* aEvent) + : mList(aList) + , mEvent(aEvent) + {} + + NS_IMETHOD Run() MOZ_OVERRIDE + { + return mList->DispatchTrackEvent(mEvent); + } + +private: + nsRefPtr mList; + nsRefPtr mEvent; +}; + +nsresult +TextTrackList::DispatchTrackEvent(TrackEvent* aEvent) +{ + return DispatchTrustedEvent(aEvent); +} + +void +TextTrackList::CreateAndDispatchTrackEventRunner(TextTrack* aTrack, + const nsAString& aEventName) +{ + TrackEventInitInitializer eventInit; + eventInit.mBubbles = false; + eventInit.mCancelable = false; + eventInit.mTrack = aTrack; + nsRefPtr trackEvent = + TrackEvent::Constructor(this, aEventName, eventInit); + + // Dispatch the TrackEvent asynchronously. + nsCOMPtr event = new TrackEventRunner(this, trackEvent); + NS_DispatchToMainThread(event, NS_DISPATCH_NORMAL); +} + } // namespace dom } // namespace mozilla diff --git a/content/media/TextTrackList.h b/content/media/TextTrackList.h index be307554e4ab..aa716a78b36d 100644 --- a/content/media/TextTrackList.h +++ b/content/media/TextTrackList.h @@ -14,6 +14,9 @@ namespace mozilla { namespace dom { +class TrackEvent; +class TrackEventRunner; + class TextTrackList MOZ_FINAL : public nsDOMEventTargetHelper { public: @@ -50,15 +53,20 @@ public: mTextTracks.AppendElement(aTextTrack); } - void RemoveTextTrack(const TextTrack& aTrack); + void RemoveTextTrack(TextTrack& aTrack); void DidSeek(); + nsresult DispatchTrackEvent(TrackEvent* aEvent); + IMPL_EVENT_HANDLER(addtrack) IMPL_EVENT_HANDLER(removetrack) private: nsCOMPtr mGlobal; nsTArray< nsRefPtr > mTextTracks; + + void CreateAndDispatchTrackEventRunner(TextTrack* aTrack, + const nsAString& aEventName); }; } // namespace dom diff --git a/content/media/test/Makefile.in b/content/media/test/Makefile.in index 64bea04b50b9..b52aba5affab 100644 --- a/content/media/test/Makefile.in +++ b/content/media/test/Makefile.in @@ -145,6 +145,7 @@ MOCHITEST_FILES = \ $(filter disabled-for-intermittent-failures--bug-608634, test_error_in_video_document.html) \ test_texttrack.html \ test_texttrackcue.html \ + test_trackevent.html \ test_texttrackregion.html \ test_timeupdate_small_files.html \ test_unseekable.html \ diff --git a/content/media/test/test_trackevent.html b/content/media/test/test_trackevent.html new file mode 100644 index 000000000000..3a695427fbff --- /dev/null +++ b/content/media/test/test_trackevent.html @@ -0,0 +1,62 @@ + + + + + Test for Bug 893309 - Implement TrackEvent + + + + + +

+ +
+
+
+ + + diff --git a/dom/webidl/TrackEvent.webidl b/dom/webidl/TrackEvent.webidl new file mode 100644 index 000000000000..fce0d8bd31da --- /dev/null +++ b/dom/webidl/TrackEvent.webidl @@ -0,0 +1,17 @@ +/* -*- Mode: IDL; 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/. + */ + + +[Constructor(DOMString type, optional TrackEventInit eventInitDict)] +interface TrackEvent : Event +{ + readonly attribute TextTrack? track; +}; + +dictionary TrackEventInit : EventInit +{ + TextTrack? track = null; +}; diff --git a/dom/webidl/moz.build b/dom/webidl/moz.build index 0a6a98c2e4d1..845309d93782 100644 --- a/dom/webidl/moz.build +++ b/dom/webidl/moz.build @@ -548,6 +548,7 @@ GENERATED_EVENTS_WEBIDL_FILES = [ 'MozInterAppMessageEvent.webidl', 'RTCDataChannelEvent.webidl', 'RTCPeerConnectionIceEvent.webidl', + 'TrackEvent.webidl', 'UserProximityEvent.webidl', ]