gecko-dev/dom/media/TextTrackCue.cpp

223 строки
5.9 KiB
C++
Исходник Обычный вид История

/* -*- 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/HTMLTrackElement.h"
#include "mozilla/dom/TextTrackCue.h"
#include "mozilla/dom/TextTrackList.h"
#include "mozilla/dom/TextTrackRegion.h"
#include "nsComponentManagerUtils.h"
#include "mozilla/ClearOnShutdown.h"
namespace mozilla {
namespace dom {
NS_IMPL_CYCLE_COLLECTION_INHERITED(TextTrackCue,
DOMEventTargetHelper,
mDocument,
mTrack,
mTrackElement,
mDisplayState,
mRegion)
NS_IMPL_ADDREF_INHERITED(TextTrackCue, DOMEventTargetHelper)
NS_IMPL_RELEASE_INHERITED(TextTrackCue, DOMEventTargetHelper)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TextTrackCue)
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
StaticRefPtr<nsIWebVTTParserWrapper> TextTrackCue::sParserWrapper;
// Set cue setting defaults based on step 19 & seq.
// in http://dev.w3.org/html5/webvtt/#parsing
void
TextTrackCue::SetDefaultCueSettings()
{
mPosition = 50;
mPositionAlign = PositionAlignSetting::Center;
mSize = 100.0;
mPauseOnExit = false;
mSnapToLines = true;
mLineIsAutoKeyword = true;
mAlign = AlignSetting::Center;
mLineAlign = LineAlignSetting::Start;
mVertical = DirectionSetting::_empty;
mActive = false;
}
TextTrackCue::TextTrackCue(nsPIDOMWindowInner* aOwnerWindow,
double aStartTime,
double aEndTime,
const nsAString& aText,
ErrorResult& aRv)
: DOMEventTargetHelper(aOwnerWindow)
, mText(aText)
, mStartTime(aStartTime)
, mEndTime(aEndTime)
, mReset(false)
{
SetDefaultCueSettings();
MOZ_ASSERT(aOwnerWindow);
if (NS_FAILED(StashDocument())) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
}
}
TextTrackCue::TextTrackCue(nsPIDOMWindowInner* aOwnerWindow,
double aStartTime,
double aEndTime,
const nsAString& aText,
HTMLTrackElement* aTrackElement,
ErrorResult& aRv)
: DOMEventTargetHelper(aOwnerWindow)
, mText(aText)
, mStartTime(aStartTime)
, mEndTime(aEndTime)
, mTrackElement(aTrackElement)
, mReset(false)
{
SetDefaultCueSettings();
MOZ_ASSERT(aOwnerWindow);
if (NS_FAILED(StashDocument())) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
}
}
TextTrackCue::~TextTrackCue()
{
}
/** Save a reference to our creating document so we don't have to
* keep getting it from our window.
*/
nsresult
TextTrackCue::StashDocument()
{
nsPIDOMWindowInner* window = GetOwner();
if (!window) {
return NS_ERROR_NO_INTERFACE;
}
mDocument = window->GetDoc();
if (!mDocument) {
return NS_ERROR_NOT_AVAILABLE;
}
return NS_OK;
}
already_AddRefed<DocumentFragment>
TextTrackCue::GetCueAsHTML()
{
// mDocument may be null during cycle collector shutdown.
// See bug 941701.
if (!mDocument) {
return nullptr;
}
if (!sParserWrapper) {
nsresult rv;
nsCOMPtr<nsIWebVTTParserWrapper> parserWrapper =
do_CreateInstance(NS_WEBVTTPARSERWRAPPER_CONTRACTID, &rv);
if (NS_FAILED(rv)) {
return mDocument->CreateDocumentFragment();
}
sParserWrapper = parserWrapper;
ClearOnShutdown(&sParserWrapper);
}
nsPIDOMWindowInner* window = mDocument->GetInnerWindow();
if (!window) {
return mDocument->CreateDocumentFragment();
}
nsCOMPtr<nsIDOMHTMLElement> div;
sParserWrapper->ConvertCueToDOMTree(window, this,
getter_AddRefs(div));
if (!div) {
return mDocument->CreateDocumentFragment();
}
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<DocumentFragment> docFrag = mDocument->CreateDocumentFragment();
nsCOMPtr<nsIDOMNode> throwAway;
docFrag->AppendChild(div, getter_AddRefs(throwAway));
return docFrag.forget();
}
void
TextTrackCue::SetTrackElement(HTMLTrackElement* aTrackElement)
{
mTrackElement = aTrackElement;
}
JSObject*
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv The only manual changes here are to BindingUtils.h, BindingUtils.cpp, Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp, dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp, Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp, Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The rest of this diff was generated by running the following commands: find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
TextTrackCue::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv The only manual changes here are to BindingUtils.h, BindingUtils.cpp, Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp, dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp, Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp, Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The rest of this diff was generated by running the following commands: find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g' find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
return VTTCueBinding::Wrap(aCx, this, aGivenProto);
}
TextTrackRegion*
TextTrackCue::GetRegion()
{
return mRegion;
}
void
TextTrackCue::SetRegion(TextTrackRegion* aRegion)
{
if (mRegion == aRegion) {
return;
}
mRegion = aRegion;
mReset = true;
}
double
TextTrackCue::ComputedLine()
{
// See spec https://w3c.github.io/webvtt/#cue-computed-line
if (!mLineIsAutoKeyword && !mSnapToLines &&
(mLine < 0.0 || mLine > 100.0)) {
return 100.0;
} else if (!mLineIsAutoKeyword) {
return mLine;
} else if (mLineIsAutoKeyword && !mSnapToLines) {
return 100.0;
} else if (!mTrack ||
!mTrack->GetTextTrackList() ||
!mTrack->GetTextTrackList()->GetMediaElement()) {
return -1.0;
}
RefPtr<TextTrackList> trackList = mTrack->GetTextTrackList();
bool dummy;
uint32_t showingTracksNum = 0;
for (uint32_t idx = 0; idx < trackList->Length(); idx++) {
RefPtr<TextTrack> track = trackList->IndexedGetter(idx, dummy);
if (track->Mode() == TextTrackMode::Showing) {
showingTracksNum++;
}
if (mTrack == track) {
break;
}
}
return (-1.0) * showingTracksNum;
}
PositionAlignSetting
TextTrackCue::ComputedPositionAlign()
{
// See spec https://w3c.github.io/webvtt/#cue-computed-position-alignment
if (mPositionAlign != PositionAlignSetting::Auto) {
return mPositionAlign;
} else if (mAlign == AlignSetting::Left) {
return PositionAlignSetting::Line_left;
} else if (mAlign == AlignSetting::Right) {
return PositionAlignSetting::Line_right;
}
return PositionAlignSetting::Center;
}
} // namespace dom
} // namespace mozilla