2013-05-21 09:14:00 -07: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/HTMLTrackElement.h"
|
|
|
|
#include "mozilla/dom/TextTrackCue.h"
|
2013-06-10 08:30:00 -07:00
|
|
|
#include "mozilla/dom/ProcessingInstruction.h"
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsTextNode.h"
|
|
|
|
#include "nsVideoFrame.h"
|
2013-07-09 17:02:00 -07:00
|
|
|
|
|
|
|
// Alternate value for the 'auto' keyword.
|
|
|
|
#define WEBVTT_AUTO -1
|
2013-05-21 09:14:00 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-08-15 08:11:15 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED_4(TextTrackCue,
|
|
|
|
nsDOMEventTargetHelper,
|
|
|
|
mDocument,
|
|
|
|
mTrack,
|
|
|
|
mTrackElement,
|
|
|
|
mDisplayState)
|
2013-05-21 09:14:00 -07:00
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(TextTrackCue, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(TextTrackCue, nsDOMEventTargetHelper)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(TextTrackCue)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
// Set cue setting defaults based on step 19 & seq.
|
|
|
|
// in http://dev.w3.org/html5/webvtt/#parsing
|
|
|
|
void
|
|
|
|
TextTrackCue::SetDefaultCueSettings()
|
|
|
|
{
|
|
|
|
mPosition = 50;
|
|
|
|
mSize = 100;
|
|
|
|
mPauseOnExit = false;
|
|
|
|
mSnapToLines = true;
|
|
|
|
mLine = WEBVTT_AUTO;
|
|
|
|
mAlign = TextTrackCueAlign::Middle;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextTrackCue::TextTrackCue(nsISupports* aGlobal,
|
|
|
|
double aStartTime,
|
|
|
|
double aEndTime,
|
2013-07-16 22:14:40 -07:00
|
|
|
const nsAString& aText,
|
|
|
|
ErrorResult& aRv)
|
2013-07-18 15:01:48 -07:00
|
|
|
: mText(aText)
|
2013-05-21 09:14:00 -07:00
|
|
|
, mStartTime(aStartTime)
|
|
|
|
, mEndTime(aEndTime)
|
|
|
|
, mHead(nullptr)
|
2013-07-04 14:24:43 -07:00
|
|
|
, mReset(false)
|
2013-05-21 09:14:00 -07:00
|
|
|
{
|
|
|
|
SetDefaultCueSettings();
|
|
|
|
MOZ_ASSERT(aGlobal);
|
|
|
|
SetIsDOMBinding();
|
2013-07-18 15:01:48 -07:00
|
|
|
if (NS_FAILED(StashDocument(aGlobal))) {
|
2013-07-16 22:14:40 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
}
|
2013-05-21 09:14:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TextTrackCue::TextTrackCue(nsISupports* aGlobal,
|
|
|
|
double aStartTime,
|
|
|
|
double aEndTime,
|
|
|
|
const nsAString& aText,
|
|
|
|
HTMLTrackElement* aTrackElement,
|
2013-07-16 22:14:40 -07:00
|
|
|
webvtt_node* head,
|
|
|
|
ErrorResult& aRv)
|
2013-07-18 15:01:48 -07:00
|
|
|
: mText(aText)
|
2013-05-21 09:14:00 -07:00
|
|
|
, mStartTime(aStartTime)
|
|
|
|
, mEndTime(aEndTime)
|
|
|
|
, mTrackElement(aTrackElement)
|
|
|
|
, mHead(head)
|
2013-07-04 14:24:43 -07:00
|
|
|
, mReset(false)
|
2013-05-21 09:14:00 -07:00
|
|
|
{
|
2013-07-09 17:02:00 -07:00
|
|
|
// Ref mHead here.
|
2013-05-21 09:14:00 -07:00
|
|
|
SetDefaultCueSettings();
|
|
|
|
MOZ_ASSERT(aGlobal);
|
|
|
|
SetIsDOMBinding();
|
2013-07-18 15:01:48 -07:00
|
|
|
if (NS_FAILED(StashDocument(aGlobal))) {
|
2013-07-16 22:14:40 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
}
|
2013-05-21 09:14:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TextTrackCue::~TextTrackCue()
|
|
|
|
{
|
|
|
|
if (mHead) {
|
2013-07-09 17:02:00 -07:00
|
|
|
// Release mHead here.
|
2013-05-21 09:14:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:14:40 -07:00
|
|
|
/** Save a reference to our creating document so it's available
|
|
|
|
* even when unlinked during discard/teardown.
|
|
|
|
*/
|
|
|
|
nsresult
|
2013-07-18 15:01:48 -07:00
|
|
|
TextTrackCue::StashDocument(nsISupports* aGlobal)
|
2013-05-21 09:14:00 -07:00
|
|
|
{
|
2013-07-18 15:01:48 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aGlobal));
|
2013-07-16 22:14:40 -07:00
|
|
|
if (!window) {
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
2013-06-19 11:02:04 -07:00
|
|
|
}
|
2013-07-16 22:14:40 -07:00
|
|
|
mDocument = window->GetDoc();
|
|
|
|
if (!mDocument) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2013-06-19 11:02:04 -07:00
|
|
|
}
|
2013-07-16 22:14:40 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrackCue::CreateCueOverlay()
|
|
|
|
{
|
|
|
|
mDocument->CreateElem(NS_LITERAL_STRING("div"), nullptr,
|
|
|
|
kNameSpaceID_XHTML,
|
|
|
|
getter_AddRefs(mDisplayState));
|
2013-06-10 08:30:00 -07:00
|
|
|
nsGenericHTMLElement* cueDiv =
|
2013-07-04 14:24:43 -07:00
|
|
|
static_cast<nsGenericHTMLElement*>(mDisplayState.get());
|
2013-06-10 08:30:00 -07:00
|
|
|
cueDiv->SetClassName(NS_LITERAL_STRING("caption-text"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrackCue::RenderCue()
|
|
|
|
{
|
|
|
|
nsRefPtr<DocumentFragment> frag = GetCueAsHTML();
|
|
|
|
if (!frag || !mTrackElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-04 14:24:43 -07:00
|
|
|
if (!mDisplayState) {
|
2013-06-10 08:30:00 -07:00
|
|
|
CreateCueOverlay();
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLMediaElement* parent = mTrackElement->mMediaParent;
|
|
|
|
if (!parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame* frame = parent->GetPrimaryFrame();
|
|
|
|
if (!frame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsVideoFrame* videoFrame = do_QueryFrame(frame);
|
|
|
|
if (!videoFrame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent* overlay = videoFrame->GetCaptionOverlay();
|
|
|
|
if (!overlay) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
nsContentUtils::SetNodeTextContent(overlay, EmptyString(), true);
|
2013-07-04 14:24:43 -07:00
|
|
|
nsContentUtils::SetNodeTextContent(mDisplayState, EmptyString(), true);
|
2013-06-10 08:30:00 -07:00
|
|
|
|
2013-07-04 14:24:43 -07:00
|
|
|
mDisplayState->AppendChild(*frag, rv);
|
|
|
|
overlay->AppendChild(*mDisplayState, rv);
|
2013-06-10 08:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<DocumentFragment>
|
|
|
|
TextTrackCue::GetCueAsHTML()
|
|
|
|
{
|
2013-07-16 22:14:40 -07:00
|
|
|
MOZ_ASSERT(mDocument);
|
|
|
|
nsRefPtr<DocumentFragment> frag = mDocument->CreateDocumentFragment();
|
2013-06-10 08:30:00 -07:00
|
|
|
ConvertNodeTreeToDOMTree(frag);
|
|
|
|
|
|
|
|
return frag.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WebVTTNodeParentPair
|
|
|
|
{
|
|
|
|
webvtt_node* mNode;
|
|
|
|
nsIContent* mParent;
|
|
|
|
|
|
|
|
WebVTTNodeParentPair(webvtt_node* aNode, nsIContent* aParent)
|
|
|
|
: mNode(aNode)
|
|
|
|
, mParent(aParent)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrackCue::ConvertNodeTreeToDOMTree(nsIContent* aParentContent)
|
|
|
|
{
|
|
|
|
nsTArray<WebVTTNodeParentPair> nodeParentPairStack;
|
|
|
|
|
|
|
|
// mHead should actually be the head of a node tree.
|
|
|
|
// Seed the stack for traversal.
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIContent>
|
|
|
|
TextTrackCue::ConvertInternalNodeToContent(const webvtt_node* aWebVTTNode)
|
|
|
|
{
|
2013-07-09 17:02:00 -07:00
|
|
|
nsIAtom* atom = nsGkAtoms::span;
|
2013-06-10 08:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> cueTextContent;
|
2013-07-16 22:14:40 -07:00
|
|
|
mDocument->CreateElem(nsDependentAtomString(atom), nullptr,
|
|
|
|
kNameSpaceID_XHTML,
|
|
|
|
getter_AddRefs(cueTextContent));
|
2013-06-10 08:30:00 -07:00
|
|
|
return cueTextContent.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIContent>
|
|
|
|
TextTrackCue::ConvertLeafNodeToContent(const webvtt_node* aWebVTTNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> cueTextContent;
|
2013-07-16 22:14:40 -07:00
|
|
|
// Use mDocument to create nodes on cueTextContent.
|
|
|
|
|
2013-06-10 08:30:00 -07:00
|
|
|
return cueTextContent.forget();
|
2013-05-21 09:14:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
TextTrackCue::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
2013-08-27 13:07:00 -07:00
|
|
|
return VTTCueBinding::Wrap(aCx, aScope, this);
|
2013-05-21 09:14:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TextTrackCue::CueChanged()
|
|
|
|
{
|
|
|
|
if (mTrack) {
|
|
|
|
mTrack->CueChanged(*this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|