2013-09-19 19:26:00 +04: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 "WebVTTListener.h"
|
2018-12-13 23:06:02 +03:00
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
|
|
|
#include "mozilla/dom/HTMLTrackElement.h"
|
2013-09-19 19:26:00 +04:00
|
|
|
#include "mozilla/dom/TextTrackCue.h"
|
2013-09-18 20:46:51 +04:00
|
|
|
#include "mozilla/dom/TextTrackRegion.h"
|
|
|
|
#include "mozilla/dom/VTTRegionBinding.h"
|
2013-09-19 19:26:00 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2017-02-02 06:26:56 +03:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
2018-12-13 23:06:02 +03:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIWebVTTParserWrapper.h"
|
2013-09-19 19:26:00 +04:00
|
|
|
|
2019-07-04 02:07:43 +03:00
|
|
|
extern mozilla::LazyLogModule gTextTrackLog;
|
|
|
|
#define LOG(msg, ...) \
|
|
|
|
MOZ_LOG(gTextTrackLog, LogLevel::Debug, \
|
|
|
|
("WebVTTListener=%p, " msg, this, ##__VA_ARGS__))
|
|
|
|
#define LOG_WIHTOUT_ADDRESS(msg, ...) \
|
|
|
|
MOZ_LOG(gTextTrackLog, LogLevel::Debug, (msg, ##__VA_ARGS__))
|
|
|
|
|
2013-09-19 19:26:00 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-04-25 20:49:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(WebVTTListener, mElement, mParserWrapper)
|
2013-09-19 19:26:00 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WebVTTListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIWebVTTListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIChannelEventSink)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWebVTTListener)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(WebVTTListener)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(WebVTTListener)
|
|
|
|
|
|
|
|
WebVTTListener::WebVTTListener(HTMLTrackElement* aElement)
|
2018-12-13 23:06:02 +03:00
|
|
|
: mElement(aElement), mParserWrapperError(NS_OK) {
|
2013-09-19 19:26:00 +04:00
|
|
|
MOZ_ASSERT(mElement, "Must pass an element to the callback");
|
2019-07-04 02:07:43 +03:00
|
|
|
LOG("Created listener for track element %p", aElement);
|
2018-12-13 23:06:02 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(
|
|
|
|
CycleCollectedJSContext::Get() &&
|
|
|
|
!CycleCollectedJSContext::Get()->IsInStableOrMetaStableState());
|
|
|
|
mParserWrapper = do_CreateInstance(NS_WEBVTTPARSERWRAPPER_CONTRACTID,
|
|
|
|
&mParserWrapperError);
|
|
|
|
if (NS_SUCCEEDED(mParserWrapperError)) {
|
|
|
|
nsPIDOMWindowInner* window = mElement->OwnerDoc()->GetInnerWindow();
|
|
|
|
mParserWrapperError = mParserWrapper->LoadParser(window);
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(mParserWrapperError)) {
|
|
|
|
mParserWrapperError = mParserWrapper->Watch(this);
|
|
|
|
}
|
2013-09-19 19:26:00 +04:00
|
|
|
}
|
|
|
|
|
2019-07-04 02:07:43 +03:00
|
|
|
WebVTTListener::~WebVTTListener() { LOG("destroyed."); }
|
2013-09-19 19:26:00 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebVTTListener::GetInterface(const nsIID& aIID, void** aResult) {
|
|
|
|
return QueryInterface(aIID, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult WebVTTListener::LoadResource() {
|
2019-07-04 02:23:29 +03:00
|
|
|
if (IsCanceled()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-12-13 23:06:02 +03:00
|
|
|
// Exit if we failed to create the WebVTTParserWrapper (vtt.jsm)
|
|
|
|
NS_ENSURE_SUCCESS(mParserWrapperError, mParserWrapperError);
|
2013-09-19 19:26:00 +04:00
|
|
|
|
2014-03-14 20:13:41 +04:00
|
|
|
mElement->SetReadyState(TextTrackReadyState::Loading);
|
2013-09-19 19:26:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
WebVTTListener::AsyncOnChannelRedirect(nsIChannel* aOldChannel,
|
|
|
|
nsIChannel* aNewChannel, uint32_t aFlags,
|
|
|
|
nsIAsyncVerifyRedirectCallback* cb) {
|
2019-07-04 02:23:29 +03:00
|
|
|
if (IsCanceled()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-09-19 19:26:00 +04:00
|
|
|
if (mElement) {
|
|
|
|
mElement->OnChannelRedirect(aOldChannel, aNewChannel, aFlags);
|
|
|
|
}
|
2017-02-02 06:26:56 +03:00
|
|
|
cb->OnRedirectVerifyCallback(NS_OK);
|
2013-09-19 19:26:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:04 +03:00
|
|
|
WebVTTListener::OnStartRequest(nsIRequest* aRequest) {
|
2019-07-04 02:23:29 +03:00
|
|
|
if (IsCanceled()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-04 02:07:43 +03:00
|
|
|
LOG("OnStartRequest");
|
2019-07-04 02:28:31 +03:00
|
|
|
mElement->DispatchTestEvent(NS_LITERAL_STRING("mozStartedLoadingTextTrack"));
|
2013-09-19 19:26:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:41:31 +03:00
|
|
|
WebVTTListener::OnStopRequest(nsIRequest* aRequest, nsresult aStatus) {
|
2019-07-04 02:23:29 +03:00
|
|
|
if (IsCanceled()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-04 02:07:43 +03:00
|
|
|
LOG("OnStopRequest");
|
2014-03-18 01:49:52 +04:00
|
|
|
if (NS_FAILED(aStatus)) {
|
2019-07-04 02:07:43 +03:00
|
|
|
LOG("Got error status");
|
2014-03-18 01:49:52 +04:00
|
|
|
mElement->SetReadyState(TextTrackReadyState::FailedToLoad);
|
|
|
|
}
|
2014-03-18 00:52:11 +04:00
|
|
|
// Attempt to parse any final data the parser might still have.
|
|
|
|
mParserWrapper->Flush();
|
2014-03-13 22:29:32 +04:00
|
|
|
if (mElement->ReadyState() != TextTrackReadyState::FailedToLoad) {
|
2014-03-14 20:13:41 +04:00
|
|
|
mElement->SetReadyState(TextTrackReadyState::Loaded);
|
2013-09-19 19:26:00 +04:00
|
|
|
}
|
2014-06-19 00:10:24 +04:00
|
|
|
|
2019-07-04 02:27:12 +03:00
|
|
|
mElement->CancelChannelAndListener();
|
2014-06-19 00:10:24 +04:00
|
|
|
|
2014-03-18 01:49:52 +04:00
|
|
|
return aStatus;
|
2013-09-19 19:26:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult WebVTTListener::ParseChunk(nsIInputStream* aInStream, void* aClosure,
|
|
|
|
const char* aFromSegment,
|
|
|
|
uint32_t aToOffset, uint32_t aCount,
|
|
|
|
uint32_t* aWriteCount) {
|
|
|
|
nsCString buffer(aFromSegment, aCount);
|
|
|
|
WebVTTListener* listener = static_cast<WebVTTListener*>(aClosure);
|
2019-07-04 02:23:29 +03:00
|
|
|
MOZ_ASSERT(!listener->IsCanceled());
|
2013-09-19 19:26:00 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(listener->mParserWrapper->Parse(buffer))) {
|
2019-07-04 02:07:43 +03:00
|
|
|
LOG_WIHTOUT_ADDRESS(
|
|
|
|
"WebVTTListener=%p, Unable to parse chunk of WEBVTT text. Aborting.",
|
|
|
|
listener);
|
2013-09-19 19:26:00 +04:00
|
|
|
*aWriteCount = 0;
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aWriteCount = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-02-28 02:42:27 +03:00
|
|
|
WebVTTListener::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* aStream,
|
2013-09-19 19:26:00 +04:00
|
|
|
uint64_t aOffset, uint32_t aCount) {
|
2019-07-04 02:23:29 +03:00
|
|
|
if (IsCanceled()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-04 02:07:43 +03:00
|
|
|
LOG("OnDataAvailable");
|
2013-09-19 19:26:00 +04:00
|
|
|
uint32_t count = aCount;
|
|
|
|
while (count > 0) {
|
|
|
|
uint32_t read;
|
|
|
|
nsresult rv = aStream->ReadSegments(ParseChunk, this, count, &read);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!read) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
count -= read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 21:39:36 +04:00
|
|
|
WebVTTListener::OnCue(JS::Handle<JS::Value> aCue, JSContext* aCx) {
|
2019-07-04 02:23:29 +03:00
|
|
|
MOZ_ASSERT(!IsCanceled());
|
2013-09-19 19:26:00 +04:00
|
|
|
if (!aCue.isObject()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-07-10 23:05:24 +03:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, &aCue.toObject());
|
2016-09-14 05:16:34 +03:00
|
|
|
TextTrackCue* cue = nullptr;
|
2017-07-10 23:05:24 +03:00
|
|
|
nsresult rv = UNWRAP_OBJECT(VTTCue, &obj, cue);
|
2013-09-19 19:26:00 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
cue->SetTrackElement(mElement);
|
|
|
|
mElement->mTrack->AddCue(*cue);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-09 21:39:36 +04:00
|
|
|
WebVTTListener::OnRegion(JS::Handle<JS::Value> aRegion, JSContext* aCx) {
|
2019-07-04 02:23:29 +03:00
|
|
|
MOZ_ASSERT(!IsCanceled());
|
2014-03-10 19:33:06 +04:00
|
|
|
// Nothing for this callback to do.
|
2013-09-19 19:26:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-17 23:11:41 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
WebVTTListener::OnParsingError(int32_t errorCode, JSContext* cx) {
|
2019-07-04 02:23:29 +03:00
|
|
|
MOZ_ASSERT(!IsCanceled());
|
2014-03-17 23:11:41 +04:00
|
|
|
// We only care about files that have a bad WebVTT file signature right now
|
|
|
|
// as that means the file failed to load.
|
|
|
|
if (errorCode == ErrorCodes::BadSignature) {
|
2019-07-04 02:07:43 +03:00
|
|
|
LOG("parsing error");
|
2014-03-17 23:11:41 +04:00
|
|
|
mElement->SetReadyState(TextTrackReadyState::FailedToLoad);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-07-04 02:23:29 +03:00
|
|
|
bool WebVTTListener::IsCanceled() const { return mCancel; }
|
|
|
|
|
|
|
|
void WebVTTListener::Cancel() {
|
|
|
|
MOZ_ASSERT(!IsCanceled(), "Do not cancel canceled listener again!");
|
|
|
|
LOG("Cancel listen to channel's response.");
|
|
|
|
mCancel = true;
|
|
|
|
mParserWrapper->Cancel();
|
|
|
|
mParserWrapper = nullptr;
|
|
|
|
mElement = nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-19 19:26:00 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|