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/Element.h"
|
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
|
|
|
#include "mozilla/dom/HTMLTrackElement.h"
|
|
|
|
#include "mozilla/dom/HTMLTrackElementBinding.h"
|
|
|
|
#include "mozilla/dom/HTMLUnknownElement.h"
|
2014-09-21 16:36:25 +00:00
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "mozilla/LoadInfo.h"
|
2013-09-19 15:26:00 +00:00
|
|
|
#include "WebVTTListener.h"
|
2013-05-21 16:14:00 +00:00
|
|
|
#include "nsAttrValueInlines.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsGenericHTMLElement.h"
|
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
|
|
|
#include "nsICachingChannel.h"
|
|
|
|
#include "nsIChannelEventSink.h"
|
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "nsIContentSecurityPolicy.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMEventTarget.h"
|
|
|
|
#include "nsIDOMHTMLMediaElement.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsILoadGroup.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsMappedAttributes.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsRuleData.h"
|
|
|
|
#include "nsStyleConsts.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsVideoFrame.h"
|
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
static PRLogModuleInfo* gTrackElementLog;
|
|
|
|
#define LOG(type, msg) PR_LOG(gTrackElementLog, type, msg)
|
|
|
|
#else
|
|
|
|
#define LOG(type, msg)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Replace the usual NS_IMPL_NS_NEW_HTML_ELEMENT(Track) so
|
|
|
|
// we can return an UnknownElement instead when pref'd off.
|
|
|
|
nsGenericHTMLElement*
|
2014-06-20 02:01:40 +00:00
|
|
|
NS_NewHTMLTrackElement(already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
|
2013-05-21 16:14:00 +00:00
|
|
|
mozilla::dom::FromParser aFromParser)
|
|
|
|
{
|
|
|
|
if (!mozilla::dom::HTMLTrackElement::IsWebVTTEnabled()) {
|
2013-06-26 14:59:46 +00:00
|
|
|
return new mozilla::dom::HTMLUnknownElement(aNodeInfo);
|
2013-05-21 16:14:00 +00:00
|
|
|
}
|
|
|
|
|
2013-06-26 14:59:46 +00:00
|
|
|
return new mozilla::dom::HTMLTrackElement(aNodeInfo);
|
2013-05-21 16:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-02-22 03:06:48 +00:00
|
|
|
// Map html attribute string values to TextTrackKind enums.
|
|
|
|
static MOZ_CONSTEXPR nsAttrValue::EnumTable kKindTable[] = {
|
|
|
|
{ "subtitles", static_cast<int16_t>(TextTrackKind::Subtitles) },
|
|
|
|
{ "captions", static_cast<int16_t>(TextTrackKind::Captions) },
|
|
|
|
{ "descriptions", static_cast<int16_t>(TextTrackKind::Descriptions) },
|
|
|
|
{ "chapters", static_cast<int16_t>(TextTrackKind::Chapters) },
|
|
|
|
{ "metadata", static_cast<int16_t>(TextTrackKind::Metadata) },
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
2013-07-04 18:16:05 +00:00
|
|
|
// The default value for kKindTable is "subtitles"
|
2013-11-22 19:45:50 +00:00
|
|
|
static MOZ_CONSTEXPR const char* kKindTableDefaultString = kKindTable->tag;
|
2013-07-04 18:16:05 +00:00
|
|
|
|
2013-05-21 16:14:00 +00:00
|
|
|
/** HTMLTrackElement */
|
2014-06-20 02:01:40 +00:00
|
|
|
HTMLTrackElement::HTMLTrackElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
|
2013-05-21 16:14:00 +00:00
|
|
|
: nsGenericHTMLElement(aNodeInfo)
|
|
|
|
{
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gTrackElementLog) {
|
|
|
|
gTrackElementLog = PR_NewLogModule("nsTrackElement");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLTrackElement::~HTMLTrackElement()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ELEMENT_CLONE(HTMLTrackElement)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(HTMLTrackElement, Element)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(HTMLTrackElement, Element)
|
|
|
|
|
2014-04-25 16:49:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(HTMLTrackElement, nsGenericHTMLElement,
|
2014-06-18 20:10:24 +00:00
|
|
|
mTrack, mMediaParent, mListener)
|
2013-05-21 16:14:00 +00:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(HTMLTrackElement)
|
2013-08-07 20:23:08 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsGenericHTMLElement)
|
2013-05-21 16:14:00 +00:00
|
|
|
|
2013-07-04 18:16:05 +00:00
|
|
|
void
|
|
|
|
HTMLTrackElement::GetKind(DOMString& aKind) const
|
|
|
|
{
|
|
|
|
GetEnumAttr(nsGkAtoms::kind, kKindTableDefaultString, aKind);
|
|
|
|
}
|
|
|
|
|
2013-06-10 15:30:00 +00:00
|
|
|
void
|
|
|
|
HTMLTrackElement::OnChannelRedirect(nsIChannel* aChannel,
|
|
|
|
nsIChannel* aNewChannel,
|
|
|
|
uint32_t aFlags)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aChannel == mChannel, "Channels should match!");
|
|
|
|
mChannel = aNewChannel;
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:14:00 +00:00
|
|
|
JSObject*
|
2014-04-08 22:27:17 +00:00
|
|
|
HTMLTrackElement::WrapNode(JSContext* aCx)
|
2013-05-21 16:14:00 +00:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 22:27:17 +00:00
|
|
|
return HTMLTrackElementBinding::Wrap(aCx, this);
|
2013-05-21 16:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HTMLTrackElement::IsWebVTTEnabled()
|
|
|
|
{
|
2013-06-13 05:12:26 +00:00
|
|
|
// Our callee does not use its arguments.
|
|
|
|
return HTMLTrackElementBinding::ConstructorEnabled(nullptr, JS::NullPtr());
|
2013-05-21 16:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextTrack*
|
2014-04-07 17:58:38 +00:00
|
|
|
HTMLTrackElement::GetTrack()
|
2013-05-21 16:14:00 +00:00
|
|
|
{
|
|
|
|
if (!mTrack) {
|
2014-01-27 20:17:20 +00:00
|
|
|
CreateTextTrack();
|
2013-05-21 16:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mTrack;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLTrackElement::CreateTextTrack()
|
|
|
|
{
|
2013-05-24 02:57:00 +00:00
|
|
|
nsString label, srcLang;
|
2013-05-21 16:14:00 +00:00
|
|
|
GetSrclang(srcLang);
|
|
|
|
GetLabel(label);
|
|
|
|
|
2013-07-04 18:16:05 +00:00
|
|
|
TextTrackKind kind;
|
|
|
|
if (const nsAttrValue* value = GetParsedAttr(nsGkAtoms::kind)) {
|
|
|
|
kind = static_cast<TextTrackKind>(value->GetEnumValue());
|
|
|
|
} else {
|
|
|
|
kind = TextTrackKind::Subtitles;
|
2013-05-21 16:14:00 +00:00
|
|
|
}
|
|
|
|
|
2015-01-12 11:07:38 +00:00
|
|
|
nsISupports* parentObject =
|
|
|
|
OwnerDoc()->GetParentObject();
|
2014-04-07 17:58:38 +00:00
|
|
|
|
2015-01-12 11:07:38 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(parentObject);
|
2014-04-07 17:58:38 +00:00
|
|
|
|
2015-01-12 11:07:38 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(parentObject);
|
2014-04-07 17:58:38 +00:00
|
|
|
mTrack = new TextTrack(window, kind, label, srcLang,
|
2014-03-13 18:41:21 +00:00
|
|
|
TextTrackMode::Disabled,
|
|
|
|
TextTrackReadyState::NotLoaded,
|
|
|
|
TextTrackSource::Track);
|
2014-02-27 19:07:39 +00:00
|
|
|
mTrack->SetTrackElement(this);
|
2013-05-21 16:14:00 +00:00
|
|
|
|
2013-07-04 18:16:05 +00:00
|
|
|
if (mMediaParent) {
|
|
|
|
mMediaParent->AddTextTrack(mTrack);
|
|
|
|
}
|
2013-05-21 16:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HTMLTrackElement::ParseAttribute(int32_t aNamespaceID,
|
|
|
|
nsIAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
nsAttrValue& aResult)
|
|
|
|
{
|
|
|
|
if (aNamespaceID == kNameSpaceID_None && aAttribute == nsGkAtoms::kind) {
|
|
|
|
// Case-insensitive lookup, with the first element as the default.
|
|
|
|
return aResult.ParseEnumValue(aValue, kKindTable, false, kKindTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise call the generic implementation.
|
|
|
|
return nsGenericHTMLElement::ParseAttribute(aNamespaceID,
|
|
|
|
aAttribute,
|
|
|
|
aValue,
|
|
|
|
aResult);
|
|
|
|
}
|
|
|
|
|
2013-06-10 15:30:00 +00:00
|
|
|
void
|
|
|
|
HTMLTrackElement::LoadResource()
|
|
|
|
{
|
|
|
|
// Find our 'src' url
|
|
|
|
nsAutoString src;
|
|
|
|
if (!GetAttr(kNameSpaceID_None, nsGkAtoms::src, src)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NewURIFromString(src, getter_AddRefs(uri));
|
|
|
|
NS_ENSURE_TRUE_VOID(NS_SUCCEEDED(rv));
|
|
|
|
LOG(PR_LOG_ALWAYS, ("%p Trying to load from src=%s", this,
|
|
|
|
NS_ConvertUTF16toUTF8(src).get()));
|
|
|
|
|
|
|
|
if (mChannel) {
|
|
|
|
mChannel->Cancel(NS_BINDING_ABORTED);
|
|
|
|
mChannel = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = nsContentUtils::GetSecurityManager()->
|
|
|
|
CheckLoadURIWithPrincipal(NodePrincipal(), uri,
|
|
|
|
nsIScriptSecurityManager::STANDARD);
|
|
|
|
NS_ENSURE_TRUE_VOID(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
int16_t shouldLoad = nsIContentPolicy::ACCEPT;
|
|
|
|
rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_MEDIA,
|
|
|
|
uri,
|
|
|
|
NodePrincipal(),
|
2013-08-07 20:23:08 +00:00
|
|
|
static_cast<Element*>(this),
|
2013-06-10 15:30:00 +00:00
|
|
|
NS_LITERAL_CSTRING("text/vtt"), // mime type
|
|
|
|
nullptr, // extra
|
|
|
|
&shouldLoad,
|
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
nsContentUtils::GetSecurityManager());
|
|
|
|
NS_ENSURE_TRUE_VOID(NS_SUCCEEDED(rv));
|
|
|
|
if (NS_CP_REJECTED(shouldLoad)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-02 17:10:00 +00:00
|
|
|
// We may already have a TextTrack at this point if GetTrack() has already
|
|
|
|
// been called. This happens, for instance, if script tries to get the
|
|
|
|
// TextTrack before its mTrackElement has been bound to the DOM tree.
|
|
|
|
if (!mTrack) {
|
|
|
|
CreateTextTrack();
|
|
|
|
}
|
2013-06-10 15:30:00 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = OwnerDoc()->GetDocumentLoadGroup();
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
uri,
|
2014-09-21 16:36:25 +00:00
|
|
|
static_cast<Element*>(this),
|
|
|
|
nsILoadInfo::SEC_NORMAL,
|
|
|
|
nsIContentPolicy::TYPE_MEDIA,
|
|
|
|
loadGroup);
|
|
|
|
|
2013-06-10 15:30:00 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(NS_SUCCEEDED(rv));
|
|
|
|
|
2013-09-19 15:26:00 +00:00
|
|
|
mListener = new WebVTTListener(this);
|
|
|
|
rv = mListener->LoadResource();
|
2013-06-10 15:30:00 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(NS_SUCCEEDED(rv));
|
2013-09-19 15:26:00 +00:00
|
|
|
channel->SetNotificationCallbacks(mListener);
|
2013-06-10 15:30:00 +00:00
|
|
|
|
|
|
|
LOG(PR_LOG_DEBUG, ("opening webvtt channel"));
|
2013-09-19 15:26:00 +00:00
|
|
|
rv = channel->AsyncOpen(mListener, nullptr);
|
2013-06-10 15:30:00 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
mChannel = channel;
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:14:00 +00:00
|
|
|
nsresult
|
|
|
|
HTMLTrackElement::BindToTree(nsIDocument* aDocument,
|
|
|
|
nsIContent* aParent,
|
|
|
|
nsIContent* aBindingParent,
|
|
|
|
bool aCompileEventHandlers)
|
|
|
|
{
|
|
|
|
nsresult rv = nsGenericHTMLElement::BindToTree(aDocument,
|
|
|
|
aParent,
|
|
|
|
aBindingParent,
|
|
|
|
aCompileEventHandlers);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!aDocument) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(PR_LOG_DEBUG, ("Track Element bound to tree."));
|
|
|
|
if (!aParent || !aParent->IsNodeOfType(nsINode::eMEDIA)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store our parent so we can look up its frame for display.
|
|
|
|
if (!mMediaParent) {
|
|
|
|
mMediaParent = static_cast<HTMLMediaElement*>(aParent);
|
|
|
|
|
|
|
|
HTMLMediaElement* media = static_cast<HTMLMediaElement*>(aParent);
|
|
|
|
// TODO: separate notification for 'alternate' tracks?
|
|
|
|
media->NotifyAddedSource();
|
|
|
|
LOG(PR_LOG_DEBUG, ("Track element sent notification to parent."));
|
|
|
|
|
2013-06-28 01:05:03 +00:00
|
|
|
mMediaParent->RunInStableState(
|
2013-06-10 15:30:00 +00:00
|
|
|
NS_NewRunnableMethod(this, &HTMLTrackElement::LoadResource));
|
2013-05-21 16:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLTrackElement::UnbindFromTree(bool aDeep, bool aNullParent)
|
|
|
|
{
|
2013-06-24 15:35:58 +00:00
|
|
|
if (mMediaParent) {
|
|
|
|
// mTrack can be null if HTMLTrackElement::LoadResource has never been
|
|
|
|
// called.
|
|
|
|
if (mTrack) {
|
|
|
|
mMediaParent->RemoveTextTrack(mTrack);
|
|
|
|
}
|
|
|
|
if (aNullParent) {
|
|
|
|
mMediaParent = nullptr;
|
|
|
|
}
|
2013-05-21 16:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsGenericHTMLElement::UnbindFromTree(aDeep, aNullParent);
|
|
|
|
}
|
|
|
|
|
2013-10-25 04:14:36 +00:00
|
|
|
uint16_t
|
|
|
|
HTMLTrackElement::ReadyState() const
|
|
|
|
{
|
|
|
|
if (!mTrack) {
|
2014-03-13 18:29:32 +00:00
|
|
|
return TextTrackReadyState::NotLoaded;
|
2013-10-25 04:14:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mTrack->ReadyState();
|
|
|
|
}
|
|
|
|
|
2014-03-14 16:13:41 +00:00
|
|
|
void
|
|
|
|
HTMLTrackElement::SetReadyState(uint16_t aReadyState)
|
|
|
|
{
|
|
|
|
if (mTrack) {
|
|
|
|
switch (aReadyState) {
|
|
|
|
case TextTrackReadyState::Loaded:
|
2014-07-07 14:49:00 +00:00
|
|
|
DispatchTrackRunnable(NS_LITERAL_STRING("load"));
|
2014-03-14 16:13:41 +00:00
|
|
|
break;
|
|
|
|
case TextTrackReadyState::FailedToLoad:
|
|
|
|
DispatchTrackRunnable(NS_LITERAL_STRING("error"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mTrack->SetReadyState(aReadyState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLTrackElement::DispatchTrackRunnable(const nsString& aEventName)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
NS_NewRunnableMethodWithArg
|
|
|
|
<const nsString>(this,
|
|
|
|
&HTMLTrackElement::DispatchTrustedEvent,
|
|
|
|
aEventName);
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HTMLTrackElement::DispatchTrustedEvent(const nsAString& aName)
|
|
|
|
{
|
|
|
|
nsIDocument* doc = OwnerDoc();
|
|
|
|
if (!doc) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsContentUtils::DispatchTrustedEvent(doc, static_cast<nsIContent*>(this),
|
|
|
|
aName, false, false);
|
|
|
|
}
|
|
|
|
|
2014-06-18 20:10:24 +00:00
|
|
|
void
|
|
|
|
HTMLTrackElement::DropChannel()
|
|
|
|
{
|
|
|
|
mChannel = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-21 16:14:00 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|