content/media/TextTrackCue.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/dom/HTMLTrackElement.h"
michael@0 7 #include "mozilla/dom/TextTrackCue.h"
michael@0 8 #include "mozilla/dom/TextTrackRegion.h"
michael@0 9 #include "nsComponentManagerUtils.h"
michael@0 10 #include "mozilla/ClearOnShutdown.h"
michael@0 11
michael@0 12 namespace mozilla {
michael@0 13 namespace dom {
michael@0 14
michael@0 15 NS_IMPL_CYCLE_COLLECTION_INHERITED(TextTrackCue,
michael@0 16 DOMEventTargetHelper,
michael@0 17 mDocument,
michael@0 18 mTrack,
michael@0 19 mTrackElement,
michael@0 20 mDisplayState,
michael@0 21 mRegion)
michael@0 22
michael@0 23 NS_IMPL_ADDREF_INHERITED(TextTrackCue, DOMEventTargetHelper)
michael@0 24 NS_IMPL_RELEASE_INHERITED(TextTrackCue, DOMEventTargetHelper)
michael@0 25 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TextTrackCue)
michael@0 26 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
michael@0 27
michael@0 28 StaticRefPtr<nsIWebVTTParserWrapper> TextTrackCue::sParserWrapper;
michael@0 29
michael@0 30 // Set cue setting defaults based on step 19 & seq.
michael@0 31 // in http://dev.w3.org/html5/webvtt/#parsing
michael@0 32 void
michael@0 33 TextTrackCue::SetDefaultCueSettings()
michael@0 34 {
michael@0 35 mPosition = 50;
michael@0 36 mPositionAlign = AlignSetting::Middle;
michael@0 37 mSize = 100;
michael@0 38 mPauseOnExit = false;
michael@0 39 mSnapToLines = true;
michael@0 40 mLineIsAutoKeyword = true;
michael@0 41 mAlign = AlignSetting::Middle;
michael@0 42 mLineAlign = AlignSetting::Start;
michael@0 43 mVertical = DirectionSetting::_empty;
michael@0 44 }
michael@0 45
michael@0 46 TextTrackCue::TextTrackCue(nsPIDOMWindow* aOwnerWindow,
michael@0 47 double aStartTime,
michael@0 48 double aEndTime,
michael@0 49 const nsAString& aText,
michael@0 50 ErrorResult& aRv)
michael@0 51 : DOMEventTargetHelper(aOwnerWindow)
michael@0 52 , mText(aText)
michael@0 53 , mStartTime(aStartTime)
michael@0 54 , mEndTime(aEndTime)
michael@0 55 , mReset(false)
michael@0 56 {
michael@0 57 SetDefaultCueSettings();
michael@0 58 MOZ_ASSERT(aOwnerWindow);
michael@0 59 if (NS_FAILED(StashDocument())) {
michael@0 60 aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
michael@0 61 }
michael@0 62 }
michael@0 63
michael@0 64 TextTrackCue::TextTrackCue(nsPIDOMWindow* aOwnerWindow,
michael@0 65 double aStartTime,
michael@0 66 double aEndTime,
michael@0 67 const nsAString& aText,
michael@0 68 HTMLTrackElement* aTrackElement,
michael@0 69 ErrorResult& aRv)
michael@0 70 : DOMEventTargetHelper(aOwnerWindow)
michael@0 71 , mText(aText)
michael@0 72 , mStartTime(aStartTime)
michael@0 73 , mEndTime(aEndTime)
michael@0 74 , mTrackElement(aTrackElement)
michael@0 75 , mReset(false)
michael@0 76 {
michael@0 77 SetDefaultCueSettings();
michael@0 78 MOZ_ASSERT(aOwnerWindow);
michael@0 79 if (NS_FAILED(StashDocument())) {
michael@0 80 aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
michael@0 81 }
michael@0 82 }
michael@0 83
michael@0 84 /** Save a reference to our creating document so we don't have to
michael@0 85 * keep getting it from our window.
michael@0 86 */
michael@0 87 nsresult
michael@0 88 TextTrackCue::StashDocument()
michael@0 89 {
michael@0 90 nsPIDOMWindow* window = GetOwner();
michael@0 91 if (!window) {
michael@0 92 return NS_ERROR_NO_INTERFACE;
michael@0 93 }
michael@0 94 mDocument = window->GetDoc();
michael@0 95 if (!mDocument) {
michael@0 96 return NS_ERROR_NOT_AVAILABLE;
michael@0 97 }
michael@0 98 return NS_OK;
michael@0 99 }
michael@0 100
michael@0 101 already_AddRefed<DocumentFragment>
michael@0 102 TextTrackCue::GetCueAsHTML()
michael@0 103 {
michael@0 104 // mDocument may be null during cycle collector shutdown.
michael@0 105 // See bug 941701.
michael@0 106 if (!mDocument) {
michael@0 107 return nullptr;
michael@0 108 }
michael@0 109
michael@0 110 if (!sParserWrapper) {
michael@0 111 nsresult rv;
michael@0 112 nsCOMPtr<nsIWebVTTParserWrapper> parserWrapper =
michael@0 113 do_CreateInstance(NS_WEBVTTPARSERWRAPPER_CONTRACTID, &rv);
michael@0 114 if (NS_FAILED(rv)) {
michael@0 115 return mDocument->CreateDocumentFragment();
michael@0 116 }
michael@0 117 sParserWrapper = parserWrapper;
michael@0 118 ClearOnShutdown(&sParserWrapper);
michael@0 119 }
michael@0 120
michael@0 121 nsPIDOMWindow* window = mDocument->GetWindow();
michael@0 122 if (!window) {
michael@0 123 return mDocument->CreateDocumentFragment();
michael@0 124 }
michael@0 125
michael@0 126 nsCOMPtr<nsIDOMHTMLElement> div;
michael@0 127 sParserWrapper->ConvertCueToDOMTree(window, this,
michael@0 128 getter_AddRefs(div));
michael@0 129 if (!div) {
michael@0 130 return mDocument->CreateDocumentFragment();
michael@0 131 }
michael@0 132 nsRefPtr<DocumentFragment> docFrag = mDocument->CreateDocumentFragment();
michael@0 133 nsCOMPtr<nsIDOMNode> throwAway;
michael@0 134 docFrag->AppendChild(div, getter_AddRefs(throwAway));
michael@0 135
michael@0 136 return docFrag.forget();
michael@0 137 }
michael@0 138
michael@0 139 void
michael@0 140 TextTrackCue::SetTrackElement(HTMLTrackElement* aTrackElement)
michael@0 141 {
michael@0 142 mTrackElement = aTrackElement;
michael@0 143 }
michael@0 144
michael@0 145 JSObject*
michael@0 146 TextTrackCue::WrapObject(JSContext* aCx)
michael@0 147 {
michael@0 148 return VTTCueBinding::Wrap(aCx, this);
michael@0 149 }
michael@0 150
michael@0 151 TextTrackRegion*
michael@0 152 TextTrackCue::GetRegion()
michael@0 153 {
michael@0 154 return mRegion;
michael@0 155 }
michael@0 156
michael@0 157 void
michael@0 158 TextTrackCue::SetRegion(TextTrackRegion* aRegion)
michael@0 159 {
michael@0 160 if (mRegion == aRegion) {
michael@0 161 return;
michael@0 162 }
michael@0 163 mRegion = aRegion;
michael@0 164 mReset = true;
michael@0 165 }
michael@0 166
michael@0 167 } // namespace dom
michael@0 168 } // namespace mozilla

mercurial