2009-09-18 13:21:47 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-11-30 18:39:55 +03:00
|
|
|
/* vim: set sw=2 ts=2 et tw=80: */
|
2012-05-29 19:52:43 +04:00
|
|
|
/* 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/. */
|
2009-09-18 13:21:47 +04:00
|
|
|
|
|
|
|
#include "nsHtml5StreamParser.h"
|
2017-06-17 05:54:40 +03:00
|
|
|
|
2020-12-07 17:21:43 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <new>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
|
|
|
#include "GeckoProfiler.h"
|
|
|
|
#include "js/GCAPI.h"
|
|
|
|
#include "mozilla/ArrayIterator.h"
|
|
|
|
#include "mozilla/Buffer.h"
|
|
|
|
#include "mozilla/CheckedInt.h"
|
2017-06-17 05:54:40 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
#include "mozilla/Encoding.h"
|
2020-12-07 17:21:43 +03:00
|
|
|
#include "mozilla/EncodingDetector.h"
|
|
|
|
#include "mozilla/Likely.h"
|
|
|
|
#include "mozilla/Maybe.h"
|
2020-04-07 18:16:33 +03:00
|
|
|
#include "mozilla/SchedulerGroup.h"
|
2020-12-07 17:25:59 +03:00
|
|
|
#include "mozilla/ScopeExit.h"
|
2020-12-07 17:21:43 +03:00
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "mozilla/StaticPrefs_html5.h"
|
|
|
|
#include "mozilla/StaticPrefs_intl.h"
|
|
|
|
#include "mozilla/TaskCategory.h"
|
|
|
|
#include "mozilla/Tuple.h"
|
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
|
|
|
#include "mozilla/Unused.h"
|
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "mozilla/dom/BrowsingContext.h"
|
|
|
|
#include "mozilla/dom/DebuggerUtilsBinding.h"
|
|
|
|
#include "mozilla/dom/DocGroup.h"
|
|
|
|
#include "mozilla/dom/Document.h"
|
|
|
|
#include "mozilla/mozalloc.h"
|
|
|
|
#include "nsContentSink.h"
|
2009-09-18 13:21:47 +04:00
|
|
|
#include "nsContentUtils.h"
|
2020-12-07 17:21:43 +03:00
|
|
|
#include "nsCycleCollectionTraversalCallback.h"
|
2009-09-21 17:18:20 +04:00
|
|
|
#include "nsHtml5AtomTable.h"
|
2020-12-07 17:21:43 +03:00
|
|
|
#include "nsHtml5ByteReadable.h"
|
|
|
|
#include "nsHtml5Highlighter.h"
|
|
|
|
#include "nsHtml5MetaScanner.h"
|
2009-09-25 21:11:02 +04:00
|
|
|
#include "nsHtml5Module.h"
|
2020-12-07 17:21:43 +03:00
|
|
|
#include "nsHtml5OwningUTF16Buffer.h"
|
|
|
|
#include "nsHtml5Parser.h"
|
|
|
|
#include "nsHtml5Speculation.h"
|
2017-06-27 20:38:30 +03:00
|
|
|
#include "nsHtml5StreamParserPtr.h"
|
2020-12-07 17:21:43 +03:00
|
|
|
#include "nsHtml5Tokenizer.h"
|
|
|
|
#include "nsHtml5TreeBuilder.h"
|
|
|
|
#include "nsHtml5TreeOpExecutor.h"
|
|
|
|
#include "nsHtml5TreeOpStage.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIContentSink.h"
|
|
|
|
#include "nsID.h"
|
|
|
|
#include "nsIDTD.h"
|
2017-12-05 00:09:15 +03:00
|
|
|
#include "nsIDocShell.h"
|
2020-12-07 17:21:43 +03:00
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsIInputStream.h"
|
2011-11-30 21:44:31 +04:00
|
|
|
#include "nsINestedURI.h"
|
2020-12-07 17:21:43 +03:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIRequest.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsIScriptError.h"
|
|
|
|
#include "nsIThread.h"
|
2013-07-09 05:45:26 +04:00
|
|
|
#include "nsIThreadRetargetableRequest.h"
|
2020-12-07 17:21:43 +03:00
|
|
|
#include "nsIThreadRetargetableStreamListener.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "nsLiteralString.h"
|
2013-09-10 21:40:50 +04:00
|
|
|
#include "nsNetUtil.h"
|
2020-12-07 17:21:43 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsTPromiseFlatString.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2014-11-03 20:49:05 +03:00
|
|
|
#include "nsXULAppAPI.h"
|
2020-12-07 17:21:43 +03:00
|
|
|
|
|
|
|
// Include expat after the other, since it defines XML_NS, which conflicts with
|
|
|
|
// our symbol names.
|
|
|
|
#include "expat_config.h"
|
|
|
|
#include "expat.h"
|
2011-05-28 11:03:00 +04:00
|
|
|
|
2021-03-23 12:52:04 +03:00
|
|
|
extern "C" {
|
|
|
|
// Defined in intl/encoding_glue/src/lib.rs
|
|
|
|
const mozilla::Encoding* xmldecl_parse(const uint8_t* buf, size_t buf_len);
|
|
|
|
};
|
|
|
|
|
2012-11-08 03:04:22 +04:00
|
|
|
using namespace mozilla;
|
2019-01-30 16:35:02 +03:00
|
|
|
using namespace mozilla::dom;
|
2009-09-18 13:21:47 +04:00
|
|
|
|
2010-04-16 14:52:06 +04:00
|
|
|
/*
|
|
|
|
* Note that nsHtml5StreamParser implements cycle collecting AddRef and
|
|
|
|
* Release. Therefore, nsHtml5StreamParser must never be refcounted from
|
|
|
|
* the parser thread!
|
|
|
|
*
|
|
|
|
* To work around this limitation, runnables posted by the main thread to the
|
|
|
|
* parser thread hold their reference to the stream parser in an
|
2018-03-16 18:26:06 +03:00
|
|
|
* nsHtml5StreamParserPtr. Upon creation, nsHtml5StreamParserPtr addrefs the
|
|
|
|
* object it holds
|
2010-04-16 14:52:06 +04:00
|
|
|
* just like a regular nsRefPtr. This is OK, since the creation of the
|
2017-06-27 20:38:30 +03:00
|
|
|
* runnable and the nsHtml5StreamParserPtr happens on the main thread.
|
2010-04-16 14:52:06 +04:00
|
|
|
*
|
|
|
|
* When the runnable is done on the parser thread, the destructor of
|
2017-06-27 20:38:30 +03:00
|
|
|
* nsHtml5StreamParserPtr runs there. It doesn't call Release on the held object
|
2010-04-16 14:52:06 +04:00
|
|
|
* directly. Instead, it posts another runnable back to the main thread where
|
|
|
|
* that runnable calls Release on the wrapped object.
|
|
|
|
*
|
|
|
|
* When posting runnables in the other direction, the runnables have to be
|
|
|
|
* created on the main thread when nsHtml5StreamParser is instantiated and
|
|
|
|
* held for the lifetime of the nsHtml5StreamParser. This works, because the
|
|
|
|
* same runnabled can be dispatched multiple times and currently runnables
|
|
|
|
* posted from the parser thread to main thread don't need to wrap any
|
|
|
|
* runnable-specific data. (In the other direction, the runnables most notably
|
|
|
|
* wrap the byte data of the stream.)
|
|
|
|
*/
|
2009-09-18 13:21:47 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsHtml5StreamParser)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsHtml5StreamParser)
|
|
|
|
|
|
|
|
NS_INTERFACE_TABLE_HEAD(nsHtml5StreamParser)
|
2019-12-12 20:50:19 +03:00
|
|
|
NS_INTERFACE_TABLE(nsHtml5StreamParser, nsISupports)
|
2009-09-18 13:21:47 +04:00
|
|
|
NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(nsHtml5StreamParser)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsHtml5StreamParser)
|
|
|
|
|
2009-09-25 21:11:02 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsHtml5StreamParser)
|
2010-04-16 14:52:06 +04:00
|
|
|
tmp->DropTimer();
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mObserver)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRequest)
|
2012-11-28 03:08:22 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner)
|
2012-07-30 18:20:58 +04:00
|
|
|
tmp->mExecutorFlusher = nullptr;
|
|
|
|
tmp->mLoadFlusher = nullptr;
|
|
|
|
tmp->mExecutor = nullptr;
|
2009-09-25 21:11:02 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsHtml5StreamParser)
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mObserver)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRequest)
|
2012-11-28 03:08:22 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner)
|
2009-09-25 21:11:02 +04:00
|
|
|
// hack: count the strongly owned edge wrapped in the runnable
|
|
|
|
if (tmp->mExecutorFlusher) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mExecutorFlusher->mExecutor");
|
2018-03-16 18:26:06 +03:00
|
|
|
cb.NoteXPCOMChild(static_cast<nsIContentSink*>(tmp->mExecutor));
|
2009-09-25 21:11:02 +04:00
|
|
|
}
|
2010-02-26 12:18:38 +03:00
|
|
|
// hack: count the strongly owned edge wrapped in the runnable
|
|
|
|
if (tmp->mLoadFlusher) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mLoadFlusher->mExecutor");
|
2018-03-16 18:26:06 +03:00
|
|
|
cb.NoteXPCOMChild(static_cast<nsIContentSink*>(tmp->mExecutor));
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
2009-09-25 21:11:02 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsHtml5ExecutorFlusher : public Runnable {
|
2018-03-16 18:26:06 +03:00
|
|
|
private:
|
|
|
|
RefPtr<nsHtml5TreeOpExecutor> mExecutor;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit nsHtml5ExecutorFlusher(nsHtml5TreeOpExecutor* aExecutor)
|
|
|
|
: Runnable("nsHtml5ExecutorFlusher"), mExecutor(aExecutor) {}
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
if (!mExecutor->isInList()) {
|
2019-01-30 16:35:02 +03:00
|
|
|
Document* doc = mExecutor->GetDocument();
|
|
|
|
if (XRE_IsContentProcess() &&
|
|
|
|
nsContentUtils::
|
|
|
|
HighPriorityEventPendingForTopLevelDocumentBeforeContentfulPaint(
|
|
|
|
doc)) {
|
|
|
|
// Possible early paint pending, reuse the runnable and try to
|
|
|
|
// call RunFlushLoop later.
|
|
|
|
nsCOMPtr<nsIRunnable> flusher = this;
|
|
|
|
if (NS_SUCCEEDED(
|
|
|
|
doc->Dispatch(TaskCategory::Network, flusher.forget()))) {
|
2020-09-10 06:02:36 +03:00
|
|
|
PROFILER_MARKER_UNTYPED("HighPrio blocking parser flushing(1)", DOM);
|
2019-01-30 16:35:02 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2018-03-16 18:26:06 +03:00
|
|
|
mExecutor->RunFlushLoop();
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
2018-03-16 18:26:06 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-02-26 12:18:38 +03:00
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsHtml5LoadFlusher : public Runnable {
|
2018-03-16 18:26:06 +03:00
|
|
|
private:
|
|
|
|
RefPtr<nsHtml5TreeOpExecutor> mExecutor;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit nsHtml5LoadFlusher(nsHtml5TreeOpExecutor* aExecutor)
|
|
|
|
: Runnable("nsHtml5LoadFlusher"), mExecutor(aExecutor) {}
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
mExecutor->FlushSpeculativeLoads();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-09-25 21:11:02 +04:00
|
|
|
};
|
2009-09-18 13:21:47 +04:00
|
|
|
|
2009-09-21 17:18:20 +04:00
|
|
|
nsHtml5StreamParser::nsHtml5StreamParser(nsHtml5TreeOpExecutor* aExecutor,
|
2010-07-30 14:15:38 +04:00
|
|
|
nsHtml5Parser* aOwner,
|
|
|
|
eParserMode aMode)
|
2017-01-24 19:11:43 +03:00
|
|
|
: mSniffingLength(0),
|
|
|
|
mBomState(eBomState::BOM_SNIFFING_NOT_STARTED),
|
|
|
|
mCharsetSource(kCharsetUninitialized),
|
2017-06-18 14:37:50 +03:00
|
|
|
mEncoding(WINDOWS_1252_ENCODING),
|
2019-04-16 22:05:26 +03:00
|
|
|
mFeedChardet(true),
|
2019-12-12 20:50:19 +03:00
|
|
|
mGuessEncoding(true),
|
2017-01-24 19:11:43 +03:00
|
|
|
mReparseForbidden(false),
|
2021-03-23 12:52:04 +03:00
|
|
|
mChannelHadCharset(false),
|
2018-12-04 11:02:04 +03:00
|
|
|
mLastBuffer(nullptr), // Will be filled when starting
|
2009-09-18 13:21:47 +04:00
|
|
|
mExecutor(aExecutor),
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder(new nsHtml5TreeBuilder(
|
|
|
|
(aMode == VIEW_SOURCE_HTML || aMode == VIEW_SOURCE_XML)
|
|
|
|
? nullptr
|
|
|
|
: mExecutor->GetStage(),
|
|
|
|
aMode == NORMAL ? mExecutor->GetStage() : nullptr)),
|
2020-02-28 23:49:06 +03:00
|
|
|
mTokenizer(
|
|
|
|
new nsHtml5Tokenizer(mTreeBuilder.get(), aMode == VIEW_SOURCE_XML)),
|
2009-09-21 17:18:20 +04:00
|
|
|
mTokenizerMutex("nsHtml5StreamParser mTokenizerMutex"),
|
2009-09-18 13:21:47 +04:00
|
|
|
mOwner(aOwner),
|
2017-01-24 19:11:43 +03:00
|
|
|
mLastWasCR(false),
|
|
|
|
mStreamState(eHtml5StreamState::STREAM_NOT_STARTED),
|
|
|
|
mSpeculating(false),
|
|
|
|
mAtEOF(false),
|
2009-10-12 17:08:04 +04:00
|
|
|
mSpeculationMutex("nsHtml5StreamParser mSpeculationMutex"),
|
2017-01-24 19:11:43 +03:00
|
|
|
mSpeculationFailureCount(0),
|
2018-12-04 11:02:04 +03:00
|
|
|
mLocalFileBytesBuffered(0),
|
2017-01-24 19:11:43 +03:00
|
|
|
mTerminated(false),
|
|
|
|
mInterrupted(false),
|
2009-09-21 17:18:20 +04:00
|
|
|
mTerminatedMutex("nsHtml5StreamParser mTerminatedMutex"),
|
2017-06-08 02:23:29 +03:00
|
|
|
mEventTarget(nsHtml5Module::GetStreamParserThread()->SerialEventTarget()),
|
2009-09-25 21:11:02 +04:00
|
|
|
mExecutorFlusher(new nsHtml5ExecutorFlusher(aExecutor)),
|
2010-02-26 12:18:38 +03:00
|
|
|
mLoadFlusher(new nsHtml5LoadFlusher(aExecutor)),
|
2017-01-24 19:11:43 +03:00
|
|
|
mInitialEncodingWasFromParentFrame(false),
|
2017-11-23 10:03:41 +03:00
|
|
|
mHasHadErrors(false),
|
2021-01-24 03:11:07 +03:00
|
|
|
mDetectorHasSeenNonAscii(false),
|
|
|
|
mDetectorHadOnlySeenAsciiWhenFirstGuessing(false),
|
2019-12-12 20:50:19 +03:00
|
|
|
mDecodingLocalFileWithoutTokenizing(false),
|
2018-10-18 02:57:36 +03:00
|
|
|
mFlushTimer(NS_NewTimer(mEventTarget)),
|
2017-01-26 09:45:31 +03:00
|
|
|
mFlushTimerMutex("nsHtml5StreamParser mFlushTimerMutex"),
|
2017-01-24 19:11:43 +03:00
|
|
|
mFlushTimerArmed(false),
|
|
|
|
mFlushTimerEverFired(false),
|
2019-12-16 20:40:26 +03:00
|
|
|
mMode(aMode) {
|
2009-09-21 17:18:20 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-04-16 14:52:06 +04:00
|
|
|
#ifdef DEBUG
|
2017-06-08 02:23:29 +03:00
|
|
|
mAtomTable.SetPermittedLookupEventTarget(mEventTarget);
|
2010-04-16 14:52:06 +04:00
|
|
|
#endif
|
2009-09-21 17:18:20 +04:00
|
|
|
mTokenizer->setInterner(&mAtomTable);
|
|
|
|
mTokenizer->setEncodingDeclarationHandler(this);
|
2010-01-12 16:15:10 +03:00
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
if (aMode == VIEW_SOURCE_HTML || aMode == VIEW_SOURCE_XML) {
|
2011-11-01 15:33:11 +04:00
|
|
|
nsHtml5Highlighter* highlighter =
|
|
|
|
new nsHtml5Highlighter(mExecutor->GetStage());
|
2018-03-16 18:26:06 +03:00
|
|
|
mTokenizer->EnableViewSource(highlighter); // takes ownership
|
2011-11-01 15:33:11 +04:00
|
|
|
mTreeBuilder->EnableViewSource(highlighter); // doesn't own
|
2010-07-30 14:15:38 +04:00
|
|
|
}
|
|
|
|
|
2009-09-21 17:18:20 +04:00
|
|
|
// There's a zeroing operator new for everything else
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHtml5StreamParser::~nsHtml5StreamParser() {
|
2009-09-21 17:18:20 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
mTokenizer->end();
|
2010-04-16 14:52:06 +04:00
|
|
|
#ifdef DEBUG
|
2017-01-26 09:45:31 +03:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock flushTimerLock(mFlushTimerMutex);
|
|
|
|
MOZ_ASSERT(!mFlushTimer, "Flush timer was not dropped before dtor!");
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mRequest = nullptr;
|
|
|
|
mObserver = nullptr;
|
|
|
|
mUnicodeDecoder = nullptr;
|
|
|
|
mSniffingBuffer = nullptr;
|
|
|
|
mMetaScanner = nullptr;
|
|
|
|
mFirstBuffer = nullptr;
|
|
|
|
mExecutor = nullptr;
|
|
|
|
mTreeBuilder = nullptr;
|
|
|
|
mTokenizer = nullptr;
|
|
|
|
mOwner = nullptr;
|
2010-04-16 14:52:06 +04:00
|
|
|
#endif
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsHtml5StreamParser::GetChannel(nsIChannel** aChannel) {
|
2009-09-21 17:18:20 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2018-03-16 18:26:06 +03:00
|
|
|
return mRequest ? CallQueryInterface(mRequest, aChannel)
|
|
|
|
: NS_ERROR_NOT_AVAILABLE;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
2021-01-24 03:11:07 +03:00
|
|
|
int32_t nsHtml5StreamParser::MaybeRollBackSource(int32_t aSource) {
|
|
|
|
if (aSource ==
|
|
|
|
kCharsetFromFinalAutoDetectionWouldNotHaveBeenUTF8DependedOnTLD) {
|
|
|
|
return kCharsetFromInitialAutoDetectionWouldNotHaveBeenUTF8DependedOnTLD;
|
|
|
|
}
|
|
|
|
if (aSource == kCharsetFromFinalAutoDetectionWouldNotHaveBeenUTF8Generic) {
|
|
|
|
return kCharsetFromInitialAutoDetectionWouldNotHaveBeenUTF8Generic;
|
|
|
|
}
|
|
|
|
if (aSource == kCharsetFromFinalAutoDetectionWouldNotHaveBeenUTF8Content) {
|
|
|
|
return kCharsetFromInitialAutoDetectionWouldNotHaveBeenUTF8Content;
|
|
|
|
}
|
|
|
|
if (aSource == kCharsetFromFinalAutoDetectionWouldHaveBeenUTF8 &&
|
|
|
|
!mDetectorHadOnlySeenAsciiWhenFirstGuessing) {
|
|
|
|
return kCharsetFromInitialAutoDetectionWouldHaveBeenUTF8;
|
|
|
|
}
|
|
|
|
if (aSource == kCharsetFromFinalUserForcedAutoDetection) {
|
|
|
|
aSource = kCharsetFromInitialUserForcedAutoDetection;
|
|
|
|
}
|
|
|
|
return aSource;
|
|
|
|
}
|
|
|
|
|
2019-12-12 20:50:19 +03:00
|
|
|
void nsHtml5StreamParser::GuessEncoding(bool aEof, bool aInitial) {
|
2021-01-24 03:11:07 +03:00
|
|
|
if (aInitial) {
|
|
|
|
if (!mDetectorHasSeenNonAscii) {
|
|
|
|
mDetectorHadOnlySeenAsciiWhenFirstGuessing = true;
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-12 20:50:19 +03:00
|
|
|
mGuessEncoding = false;
|
|
|
|
}
|
2021-01-14 10:06:53 +03:00
|
|
|
bool forced = (mCharsetSource == kCharsetFromPendingUserForcedAutoDetection ||
|
|
|
|
mCharsetSource == kCharsetFromInitialUserForcedAutoDetection);
|
|
|
|
MOZ_ASSERT(
|
|
|
|
mCharsetSource != kCharsetFromFinalUserForcedAutoDetection &&
|
|
|
|
mCharsetSource != kCharsetFromFinalAutoDetectionWouldHaveBeenUTF8 &&
|
2021-01-24 03:11:07 +03:00
|
|
|
mCharsetSource !=
|
|
|
|
kCharsetFromFinalAutoDetectionWouldNotHaveBeenUTF8Generic &&
|
|
|
|
mCharsetSource !=
|
|
|
|
kCharsetFromFinalAutoDetectionWouldNotHaveBeenUTF8Content &&
|
|
|
|
mCharsetSource !=
|
|
|
|
kCharsetFromFinalAutoDetectionWouldNotHaveBeenUTF8DependedOnTLD &&
|
2021-01-14 10:06:53 +03:00
|
|
|
mCharsetSource != kCharsetFromFinalAutoDetectionFile);
|
2021-01-24 03:11:07 +03:00
|
|
|
auto ifHadBeenForced = mDetector->Guess(EmptyCString(), true);
|
2021-01-14 10:06:53 +03:00
|
|
|
auto encoding =
|
2021-01-24 03:11:07 +03:00
|
|
|
forced ? ifHadBeenForced
|
2021-01-14 10:06:53 +03:00
|
|
|
: mDetector->Guess(mTLD, mDecodingLocalFileWithoutTokenizing);
|
2021-01-24 03:11:07 +03:00
|
|
|
int32_t source =
|
2021-01-14 10:06:53 +03:00
|
|
|
aInitial
|
2021-01-24 03:11:07 +03:00
|
|
|
? (forced
|
|
|
|
? kCharsetFromInitialUserForcedAutoDetection
|
|
|
|
: kCharsetFromInitialAutoDetectionWouldNotHaveBeenUTF8Generic)
|
2021-01-14 10:06:53 +03:00
|
|
|
: (forced
|
|
|
|
? kCharsetFromFinalUserForcedAutoDetection
|
|
|
|
: (mDecodingLocalFileWithoutTokenizing
|
|
|
|
? kCharsetFromFinalAutoDetectionFile
|
2021-01-24 03:11:07 +03:00
|
|
|
: kCharsetFromFinalAutoDetectionWouldNotHaveBeenUTF8Generic));
|
|
|
|
if (source == kCharsetFromFinalAutoDetectionWouldNotHaveBeenUTF8Generic) {
|
|
|
|
if (encoding == ISO_2022_JP_ENCODING) {
|
|
|
|
if (EncodingDetector::TldMayAffectGuess(mTLD)) {
|
|
|
|
source = kCharsetFromFinalAutoDetectionWouldNotHaveBeenUTF8Content;
|
|
|
|
}
|
|
|
|
} else if (!mDetectorHasSeenNonAscii) {
|
|
|
|
source = kCharsetFromInitialAutoDetectionASCII; // deliberately Initial
|
|
|
|
} else if (ifHadBeenForced == UTF_8_ENCODING) {
|
2021-01-14 10:06:53 +03:00
|
|
|
source = kCharsetFromFinalAutoDetectionWouldHaveBeenUTF8;
|
2021-01-24 03:11:07 +03:00
|
|
|
} else if (encoding != ifHadBeenForced) {
|
|
|
|
source = kCharsetFromFinalAutoDetectionWouldNotHaveBeenUTF8DependedOnTLD;
|
|
|
|
} else if (EncodingDetector::TldMayAffectGuess(mTLD)) {
|
|
|
|
source = kCharsetFromFinalAutoDetectionWouldNotHaveBeenUTF8Content;
|
2021-01-14 10:06:53 +03:00
|
|
|
}
|
2021-01-24 03:11:07 +03:00
|
|
|
} else if (source ==
|
|
|
|
kCharsetFromInitialAutoDetectionWouldNotHaveBeenUTF8Generic) {
|
|
|
|
if (encoding == ISO_2022_JP_ENCODING) {
|
|
|
|
if (EncodingDetector::TldMayAffectGuess(mTLD)) {
|
|
|
|
source = kCharsetFromInitialAutoDetectionWouldNotHaveBeenUTF8Content;
|
|
|
|
}
|
|
|
|
} else if (!mDetectorHasSeenNonAscii) {
|
|
|
|
source = kCharsetFromInitialAutoDetectionASCII;
|
|
|
|
} else if (ifHadBeenForced == UTF_8_ENCODING) {
|
2021-01-14 10:06:53 +03:00
|
|
|
source = kCharsetFromInitialAutoDetectionWouldHaveBeenUTF8;
|
2021-01-24 03:11:07 +03:00
|
|
|
} else if (encoding != ifHadBeenForced) {
|
|
|
|
source =
|
|
|
|
kCharsetFromInitialAutoDetectionWouldNotHaveBeenUTF8DependedOnTLD;
|
|
|
|
} else if (EncodingDetector::TldMayAffectGuess(mTLD)) {
|
|
|
|
source = kCharsetFromInitialAutoDetectionWouldNotHaveBeenUTF8Content;
|
2021-01-14 10:06:53 +03:00
|
|
|
}
|
|
|
|
}
|
2019-12-12 20:50:19 +03:00
|
|
|
if (HasDecoder() && !mDecodingLocalFileWithoutTokenizing) {
|
|
|
|
if (mEncoding == encoding) {
|
2021-01-24 03:11:07 +03:00
|
|
|
MOZ_ASSERT(mCharsetSource == kCharsetFromInitialAutoDetectionASCII ||
|
|
|
|
mCharsetSource < source,
|
|
|
|
"Why are we running chardet at all?");
|
2021-01-14 10:06:53 +03:00
|
|
|
// Source didn't actually change between initial and final, so roll it
|
2021-01-24 03:11:07 +03:00
|
|
|
// back for telemetry purposes.
|
|
|
|
mCharsetSource = MaybeRollBackSource(source);
|
2019-12-12 17:38:54 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
2019-12-12 20:50:19 +03:00
|
|
|
} else {
|
2021-06-21 15:09:01 +03:00
|
|
|
MOZ_ASSERT(mCharsetSource < kCharsetFromXmlDeclarationUtf16 || forced);
|
2019-12-12 20:50:19 +03:00
|
|
|
// We've already committed to a decoder. Request a reload from the
|
|
|
|
// docshell.
|
2021-01-14 10:06:53 +03:00
|
|
|
mTreeBuilder->NeedsCharsetSwitchTo(encoding, source, 0);
|
2019-12-12 20:50:19 +03:00
|
|
|
FlushTreeOpsAndDisarmTimer();
|
|
|
|
Interrupt();
|
2011-02-11 13:33:32 +03:00
|
|
|
}
|
2019-12-12 20:50:19 +03:00
|
|
|
} else {
|
|
|
|
// Got a confident answer from the sniffing buffer. That code will
|
|
|
|
// take care of setting up the decoder.
|
2021-01-24 03:11:07 +03:00
|
|
|
if (mCharsetSource == kCharsetUninitialized && aEof) {
|
|
|
|
// The document is so short that the initial buffer is the last
|
|
|
|
// buffer.
|
|
|
|
source = MaybeRollBackSource(source);
|
|
|
|
}
|
2019-12-12 20:50:19 +03:00
|
|
|
mEncoding = encoding;
|
2021-01-14 10:06:53 +03:00
|
|
|
mCharsetSource = source;
|
2019-12-12 20:50:19 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 22:05:26 +03:00
|
|
|
void nsHtml5StreamParser::FeedDetector(Span<const uint8_t> aBuffer,
|
|
|
|
bool aLast) {
|
2021-06-21 15:09:01 +03:00
|
|
|
mDetectorHasSeenNonAscii = mDetector->Feed(aBuffer, aLast);
|
2019-04-16 22:05:26 +03:00
|
|
|
}
|
|
|
|
|
2011-11-30 21:44:31 +04:00
|
|
|
void nsHtml5StreamParser::SetViewSourceTitle(nsIURI* aURL) {
|
2019-08-05 00:33:07 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2020-05-12 12:18:26 +03:00
|
|
|
BrowsingContext* browsingContext =
|
|
|
|
mExecutor->GetDocument()->GetBrowsingContext();
|
|
|
|
if (browsingContext && browsingContext->WatchedByDevTools()) {
|
2019-08-05 00:33:07 +03:00
|
|
|
mURIToSendToDevtools = aURL;
|
|
|
|
|
|
|
|
nsID uuid;
|
|
|
|
nsresult rv = nsContentUtils::GenerateUUIDInPlace(uuid);
|
|
|
|
if (!NS_FAILED(rv)) {
|
|
|
|
char buffer[NSID_LENGTH];
|
|
|
|
uuid.ToProvidedString(buffer);
|
|
|
|
mUUIDForDevtools = NS_ConvertASCIItoUTF16(buffer);
|
|
|
|
}
|
2018-07-24 00:43:13 +03:00
|
|
|
}
|
|
|
|
|
2011-11-30 21:44:31 +04:00
|
|
|
if (aURL) {
|
|
|
|
nsCOMPtr<nsIURI> temp;
|
2019-08-07 22:49:40 +03:00
|
|
|
if (aURL->SchemeIs("view-source")) {
|
2011-11-30 21:44:31 +04:00
|
|
|
nsCOMPtr<nsINestedURI> nested = do_QueryInterface(aURL);
|
|
|
|
nested->GetInnerURI(getter_AddRefs(temp));
|
|
|
|
} else {
|
|
|
|
temp = aURL;
|
|
|
|
}
|
2019-08-07 22:49:40 +03:00
|
|
|
if (temp->SchemeIs("data")) {
|
2011-11-30 21:44:31 +04:00
|
|
|
// Avoid showing potentially huge data: URLs. The three last bytes are
|
|
|
|
// UTF-8 for an ellipsis.
|
|
|
|
mViewSourceTitle.AssignLiteral("data:\xE2\x80\xA6");
|
|
|
|
} else {
|
2016-08-31 05:13:59 +03:00
|
|
|
nsresult rv = temp->GetSpec(mViewSourceTitle);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mViewSourceTitle.AssignLiteral("\xE2\x80\xA6");
|
|
|
|
}
|
2011-11-30 21:44:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
nsresult
|
2018-03-16 18:26:06 +03:00
|
|
|
nsHtml5StreamParser::SetupDecodingAndWriteSniffingBufferAndCurrentSegment(
|
2018-12-06 21:59:56 +03:00
|
|
|
Span<const uint8_t> aFromSegment) {
|
2009-09-25 21:11:02 +04:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2009-09-18 13:21:47 +04:00
|
|
|
nsresult rv = NS_OK;
|
2019-12-12 20:50:19 +03:00
|
|
|
if (mDecodingLocalFileWithoutTokenizing &&
|
2021-06-21 15:09:01 +03:00
|
|
|
mCharsetSource <= kCharsetFromFallback) {
|
2018-12-04 11:02:04 +03:00
|
|
|
MOZ_ASSERT(mEncoding != UTF_8_ENCODING);
|
|
|
|
mUnicodeDecoder = UTF_8_ENCODING->NewDecoderWithBOMRemoval();
|
|
|
|
} else {
|
2021-06-21 15:09:01 +03:00
|
|
|
if (mCharsetSource >= kCharsetFromFinalAutoDetectionWouldHaveBeenUTF8) {
|
|
|
|
if (!(mCharsetSource == kCharsetFromPendingUserForcedAutoDetection ||
|
2021-01-14 10:06:53 +03:00
|
|
|
mCharsetSource == kCharsetFromInitialUserForcedAutoDetection)) {
|
2019-12-12 20:50:19 +03:00
|
|
|
DontGuessEncoding();
|
|
|
|
}
|
|
|
|
mDecodingLocalFileWithoutTokenizing = false;
|
2019-04-17 14:36:52 +03:00
|
|
|
}
|
2018-12-04 11:02:04 +03:00
|
|
|
mUnicodeDecoder = mEncoding->NewDecoderWithBOMRemoval();
|
|
|
|
}
|
2009-09-18 13:21:47 +04:00
|
|
|
if (mSniffingBuffer) {
|
2020-08-07 10:49:47 +03:00
|
|
|
rv = WriteStreamBytes(Span(mSniffingBuffer.get(), mSniffingLength));
|
2009-09-18 13:21:47 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-07-30 18:20:58 +04:00
|
|
|
mSniffingBuffer = nullptr;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mMetaScanner = nullptr;
|
2018-12-06 21:59:56 +03:00
|
|
|
return WriteStreamBytes(aFromSegment);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
2021-03-23 12:52:04 +03:00
|
|
|
void nsHtml5StreamParser::SetupDecodingFromBom(
|
2017-06-18 14:37:50 +03:00
|
|
|
NotNull<const Encoding*> aEncoding) {
|
2021-03-23 12:52:04 +03:00
|
|
|
MOZ_ASSERT(IsParserThread(), "Wrong thread!");
|
2017-06-18 14:37:50 +03:00
|
|
|
mEncoding = aEncoding;
|
2019-12-12 20:50:19 +03:00
|
|
|
mDecodingLocalFileWithoutTokenizing = false;
|
2018-08-13 11:01:56 +03:00
|
|
|
mUnicodeDecoder = mEncoding->NewDecoderWithoutBOMHandling();
|
2009-09-18 13:21:47 +04:00
|
|
|
mCharsetSource = kCharsetFromByteOrderMark;
|
2019-12-12 20:50:19 +03:00
|
|
|
DontGuessEncoding();
|
2017-06-18 14:37:50 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
2012-07-30 18:20:58 +04:00
|
|
|
mSniffingBuffer = nullptr;
|
|
|
|
mMetaScanner = nullptr;
|
2009-09-18 13:21:47 +04:00
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
}
|
|
|
|
|
2021-03-23 12:52:04 +03:00
|
|
|
void nsHtml5StreamParser::SetupDecodingFromUtf16BogoXml(
|
|
|
|
NotNull<const Encoding*> aEncoding) {
|
|
|
|
MOZ_ASSERT(IsParserThread(), "Wrong thread!");
|
|
|
|
mEncoding = aEncoding;
|
|
|
|
mDecodingLocalFileWithoutTokenizing = false;
|
|
|
|
mUnicodeDecoder = mEncoding->NewDecoderWithoutBOMHandling();
|
|
|
|
mCharsetSource = kCharsetFromXmlDeclarationUtf16;
|
|
|
|
DontGuessEncoding();
|
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
|
|
|
mSniffingBuffer = nullptr;
|
|
|
|
mMetaScanner = nullptr;
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
auto dst = mLastBuffer->TailAsSpan(READ_BUFFER_SIZE);
|
|
|
|
dst[0] = '<';
|
|
|
|
dst[1] = '?';
|
|
|
|
dst[2] = 'x';
|
|
|
|
mLastBuffer->AdvanceEnd(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsHtml5StreamParser::SniffBOMlessUTF16BasicLatin(const uint8_t* aBuf,
|
|
|
|
size_t aBufLen) {
|
2011-11-16 11:38:51 +04:00
|
|
|
// Avoid underspecified heuristic craziness for XHR
|
|
|
|
if (mMode == LOAD_AS_DATA) {
|
|
|
|
return;
|
|
|
|
}
|
2011-02-16 09:40:35 +03:00
|
|
|
// Make sure there's enough data. Require room for "<title></title>"
|
2021-03-23 12:52:04 +03:00
|
|
|
if (aBufLen < 30) {
|
2011-02-16 09:40:35 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// even-numbered bytes tracked at 0, odd-numbered bytes tracked at 1
|
2011-09-29 10:19:26 +04:00
|
|
|
bool byteZero[2] = {false, false};
|
|
|
|
bool byteNonZero[2] = {false, false};
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
2021-03-23 12:52:04 +03:00
|
|
|
for (; i < aBufLen; ++i) {
|
|
|
|
if (aBuf[i]) {
|
|
|
|
if (byteNonZero[1 - (i % 2)]) {
|
2018-12-06 21:59:56 +03:00
|
|
|
return;
|
2011-02-16 09:40:35 +03:00
|
|
|
}
|
2021-03-23 12:52:04 +03:00
|
|
|
byteNonZero[i % 2] = true;
|
2018-12-06 21:59:56 +03:00
|
|
|
} else {
|
2021-03-23 12:52:04 +03:00
|
|
|
if (byteZero[1 - (i % 2)]) {
|
2018-12-06 21:59:56 +03:00
|
|
|
return;
|
|
|
|
}
|
2021-03-23 12:52:04 +03:00
|
|
|
byteZero[i % 2] = true;
|
2011-02-16 09:40:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (byteNonZero[0]) {
|
2017-06-18 14:37:50 +03:00
|
|
|
mEncoding = UTF_16LE_ENCODING;
|
2011-02-16 09:40:35 +03:00
|
|
|
} else {
|
2017-06-18 14:37:50 +03:00
|
|
|
mEncoding = UTF_16BE_ENCODING;
|
2011-02-16 09:40:35 +03:00
|
|
|
}
|
|
|
|
mCharsetSource = kCharsetFromIrreversibleAutoDetection;
|
2017-06-18 14:37:50 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
2019-12-12 20:50:19 +03:00
|
|
|
DontGuessEncoding();
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->MaybeComplainAboutCharset("EncBomlessUtf16", true, 0);
|
2011-02-16 09:40:35 +03:00
|
|
|
}
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
void nsHtml5StreamParser::SetEncodingFromExpat(const char16_t* aEncoding) {
|
2011-11-03 18:31:27 +04:00
|
|
|
if (aEncoding) {
|
|
|
|
nsDependentString utf16(aEncoding);
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString utf8;
|
2011-11-03 18:31:27 +04:00
|
|
|
CopyUTF16toUTF8(utf16, utf8);
|
2017-06-18 14:37:50 +03:00
|
|
|
auto encoding = PreferredForInternalEncodingDecl(utf8);
|
|
|
|
if (encoding) {
|
|
|
|
mEncoding = WrapNotNull(encoding);
|
2011-11-03 18:31:27 +04:00
|
|
|
mCharsetSource = kCharsetFromMetaTag; // closest for XML
|
2020-06-15 18:32:21 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
2011-11-03 18:31:27 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// else the page declared an encoding Gecko doesn't support and we'd
|
|
|
|
// end up defaulting to UTF-8 anyway. Might as well fall through here
|
|
|
|
// right away and let the encoding be set to UTF-8 which we'd default to
|
|
|
|
// anyway.
|
2011-11-01 15:33:11 +04:00
|
|
|
}
|
2018-03-16 18:26:06 +03:00
|
|
|
mEncoding = UTF_8_ENCODING; // XML defaults to UTF-8 without a BOM
|
2011-11-03 18:31:27 +04:00
|
|
|
mCharsetSource = kCharsetFromMetaTag; // means confident
|
2020-06-15 18:32:21 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
2011-11-01 15:33:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// A separate user data struct is used instead of passing the
|
|
|
|
// nsHtml5StreamParser instance as user data in order to avoid including
|
|
|
|
// expat.h in nsHtml5StreamParser.h. Doing that would cause naming conflicts.
|
|
|
|
// Using a separate user data struct also avoids bloating nsHtml5StreamParser
|
|
|
|
// by one pointer.
|
2018-03-16 18:26:06 +03:00
|
|
|
struct UserData {
|
2011-11-01 15:33:11 +04:00
|
|
|
XML_Parser mExpat;
|
|
|
|
nsHtml5StreamParser* mStreamParser;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Using no-namespace handler callbacks to avoid including expat.h in
|
|
|
|
// nsHtml5StreamParser.h, since doing so would cause naming conclicts.
|
|
|
|
static void HandleXMLDeclaration(void* aUserData, const XML_Char* aVersion,
|
|
|
|
const XML_Char* aEncoding, int aStandalone) {
|
|
|
|
UserData* ud = static_cast<UserData*>(aUserData);
|
2011-11-03 18:31:27 +04:00
|
|
|
ud->mStreamParser->SetEncodingFromExpat(
|
2018-03-16 18:26:06 +03:00
|
|
|
reinterpret_cast<const char16_t*>(aEncoding));
|
2011-11-01 15:33:11 +04:00
|
|
|
XML_StopParser(ud->mExpat, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleStartElement(void* aUserData, const XML_Char* aName,
|
2018-03-16 18:26:06 +03:00
|
|
|
const XML_Char** aAtts) {
|
2011-11-01 15:33:11 +04:00
|
|
|
UserData* ud = static_cast<UserData*>(aUserData);
|
|
|
|
XML_StopParser(ud->mExpat, false);
|
|
|
|
}
|
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
static void HandleEndElement(void* aUserData, const XML_Char* aName) {
|
2011-11-01 15:33:11 +04:00
|
|
|
UserData* ud = static_cast<UserData*>(aUserData);
|
|
|
|
XML_StopParser(ud->mExpat, false);
|
|
|
|
}
|
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
static void HandleComment(void* aUserData, const XML_Char* aName) {
|
2011-11-01 15:33:11 +04:00
|
|
|
UserData* ud = static_cast<UserData*>(aUserData);
|
|
|
|
XML_StopParser(ud->mExpat, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleProcessingInstruction(void* aUserData,
|
|
|
|
const XML_Char* aTarget,
|
|
|
|
const XML_Char* aData) {
|
|
|
|
UserData* ud = static_cast<UserData*>(aUserData);
|
|
|
|
XML_StopParser(ud->mExpat, false);
|
|
|
|
}
|
|
|
|
|
2019-04-17 14:36:52 +03:00
|
|
|
void nsHtml5StreamParser::FinalizeSniffingWithDetector(
|
|
|
|
Span<const uint8_t> aFromSegment, uint32_t aCountToSniffingLimit,
|
|
|
|
bool aEof) {
|
2021-01-27 13:36:39 +03:00
|
|
|
if (mFeedChardet && mSniffingBuffer) {
|
2020-08-07 10:49:47 +03:00
|
|
|
FeedDetector(Span(mSniffingBuffer.get(), mSniffingLength), false);
|
2019-04-17 14:36:52 +03:00
|
|
|
}
|
|
|
|
if (mFeedChardet && !aFromSegment.IsEmpty()) {
|
2019-12-12 20:50:19 +03:00
|
|
|
// Avoid buffer boundary-dependent behavior.
|
|
|
|
FeedDetector(aFromSegment.To(aCountToSniffingLimit), false);
|
|
|
|
}
|
|
|
|
bool guess = mFeedChardet;
|
|
|
|
if (mFeedChardet && aEof && aCountToSniffingLimit <= aFromSegment.Length()) {
|
2019-12-12 17:38:54 +03:00
|
|
|
FeedDetector(Span<const uint8_t>(), true);
|
2019-12-12 20:50:19 +03:00
|
|
|
mFeedChardet = false;
|
|
|
|
}
|
|
|
|
if (guess) {
|
|
|
|
GuessEncoding(aEof, (guess == mFeedChardet));
|
|
|
|
}
|
|
|
|
if (mReparseForbidden) {
|
|
|
|
DontGuessEncoding();
|
|
|
|
}
|
|
|
|
if (mFeedChardet && !aEof && aCountToSniffingLimit < aFromSegment.Length()) {
|
|
|
|
// Avoid buffer boundary-dependent behavior.
|
|
|
|
FeedDetector(aFromSegment.From(aCountToSniffingLimit), false);
|
2019-04-17 14:36:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:59:56 +03:00
|
|
|
nsresult nsHtml5StreamParser::FinalizeSniffing(Span<const uint8_t> aFromSegment,
|
|
|
|
uint32_t aCountToSniffingLimit,
|
|
|
|
bool aEof) {
|
2019-04-17 14:36:52 +03:00
|
|
|
MOZ_ASSERT(IsParserThread(), "Wrong thread!");
|
2021-06-21 15:09:01 +03:00
|
|
|
MOZ_ASSERT(mCharsetSource < kCharsetFromXmlDeclarationUtf16,
|
2019-04-17 14:36:52 +03:00
|
|
|
"Should not finalize sniffing with strong decision already made.");
|
2011-11-01 15:33:11 +04:00
|
|
|
if (mMode == VIEW_SOURCE_XML) {
|
2018-03-16 18:26:06 +03:00
|
|
|
static const XML_Memory_Handling_Suite memsuite = {
|
|
|
|
(void* (*)(size_t))moz_xmalloc, (void* (*)(void*, size_t))moz_xrealloc,
|
|
|
|
free};
|
2011-11-01 15:33:11 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
static const char16_t kExpatSeparator[] = {0xFFFF, '\0'};
|
2011-11-01 15:33:11 +04:00
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
static const char16_t kISO88591[] = {'I', 'S', 'O', '-', '8', '8',
|
|
|
|
'5', '9', '-', '1', '\0'};
|
2011-11-01 15:33:11 +04:00
|
|
|
|
|
|
|
UserData ud;
|
|
|
|
ud.mStreamParser = this;
|
|
|
|
|
|
|
|
// If we got this far, the stream didn't have a BOM. UTF-16-encoded XML
|
|
|
|
// documents MUST begin with a BOM. We don't support EBCDIC and such.
|
|
|
|
// Thus, at this point, what we have is garbage or something encoded using
|
|
|
|
// a rough ASCII superset. ISO-8859-1 allows us to decode ASCII bytes
|
|
|
|
// without throwing errors when bytes have the most significant bit set
|
|
|
|
// and without triggering expat's unknown encoding code paths. This is
|
|
|
|
// enough to be able to use expat to parse the XML declaration in order
|
|
|
|
// to extract the encoding name from it.
|
|
|
|
ud.mExpat = XML_ParserCreate_MM(kISO88591, &memsuite, kExpatSeparator);
|
|
|
|
XML_SetXmlDeclHandler(ud.mExpat, HandleXMLDeclaration);
|
|
|
|
XML_SetElementHandler(ud.mExpat, HandleStartElement, HandleEndElement);
|
|
|
|
XML_SetCommentHandler(ud.mExpat, HandleComment);
|
|
|
|
XML_SetProcessingInstructionHandler(ud.mExpat, HandleProcessingInstruction);
|
|
|
|
XML_SetUserData(ud.mExpat, static_cast<void*>(&ud));
|
|
|
|
|
|
|
|
XML_Status status = XML_STATUS_OK;
|
2011-11-01 15:33:11 +04:00
|
|
|
|
|
|
|
// aFromSegment points to the data obtained from the current network
|
|
|
|
// event. mSniffingBuffer (if it exists) contains the data obtained before
|
|
|
|
// the current event. Thus, mSniffingLenth bytes of mSniffingBuffer
|
|
|
|
// followed by aCountToSniffingLimit bytes from aFromSegment are the
|
|
|
|
// first 1024 bytes of the file (or the file as a whole if the file is
|
|
|
|
// 1024 bytes long or shorter). Thus, we parse both buffers, but if the
|
|
|
|
// first call succeeds already, we skip parsing the second buffer.
|
2011-11-01 15:33:11 +04:00
|
|
|
if (mSniffingBuffer) {
|
|
|
|
status = XML_Parse(ud.mExpat,
|
|
|
|
reinterpret_cast<const char*>(mSniffingBuffer.get()),
|
|
|
|
mSniffingLength, false);
|
|
|
|
}
|
2018-12-06 21:59:56 +03:00
|
|
|
if (status == XML_STATUS_OK && mCharsetSource < kCharsetFromMetaTag) {
|
|
|
|
mozilla::Unused << XML_Parse(
|
|
|
|
ud.mExpat, reinterpret_cast<const char*>(aFromSegment.Elements()),
|
|
|
|
aCountToSniffingLimit, false);
|
2011-11-01 15:33:11 +04:00
|
|
|
}
|
|
|
|
XML_ParserFree(ud.mExpat);
|
|
|
|
|
|
|
|
if (mCharsetSource < kCharsetFromMetaTag) {
|
|
|
|
// Failed to get an encoding from the XML declaration. XML defaults
|
|
|
|
// confidently to UTF-8 in this case.
|
|
|
|
// It is also possible that the document has an XML declaration that is
|
|
|
|
// longer than 1024 bytes, but that case is not worth worrying about.
|
2017-06-18 14:37:50 +03:00
|
|
|
mEncoding = UTF_8_ENCODING;
|
2011-11-01 15:33:11 +04:00
|
|
|
mCharsetSource = kCharsetFromMetaTag; // means confident
|
2020-06-15 18:32:21 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
2011-11-01 15:33:11 +04:00
|
|
|
}
|
|
|
|
|
2018-12-06 21:59:56 +03:00
|
|
|
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(aFromSegment);
|
2011-11-01 15:33:11 +04:00
|
|
|
}
|
2021-06-21 15:09:01 +03:00
|
|
|
bool forced = (mCharsetSource == kCharsetFromPendingUserForcedAutoDetection ||
|
|
|
|
mCharsetSource == kCharsetFromInitialUserForcedAutoDetection ||
|
|
|
|
mCharsetSource == kCharsetFromFinalUserForcedAutoDetection);
|
2021-03-23 12:52:04 +03:00
|
|
|
if (!mChannelHadCharset &&
|
|
|
|
(forced || mCharsetSource < kCharsetFromMetaPrescan) &&
|
|
|
|
(mMode == NORMAL || mMode == VIEW_SOURCE_HTML || mMode == LOAD_AS_DATA)) {
|
|
|
|
// Look for XML declaration in text/html.
|
2011-11-01 15:33:11 +04:00
|
|
|
|
2021-03-23 12:52:04 +03:00
|
|
|
const uint8_t* buf;
|
|
|
|
size_t bufLen;
|
|
|
|
if (mSniffingLength) {
|
|
|
|
// Copy data to a contiguous buffer if we already have something buffered
|
|
|
|
// up.
|
|
|
|
memcpy(mSniffingBuffer.get() + mSniffingLength, aFromSegment.Elements(),
|
|
|
|
aCountToSniffingLimit);
|
|
|
|
mSniffingLength += aCountToSniffingLimit;
|
|
|
|
aFromSegment = aFromSegment.From(aCountToSniffingLimit);
|
|
|
|
aCountToSniffingLimit = 0;
|
|
|
|
buf = mSniffingBuffer.get();
|
|
|
|
bufLen = mSniffingLength;
|
|
|
|
} else {
|
|
|
|
buf = aFromSegment.Elements();
|
|
|
|
bufLen = aCountToSniffingLimit;
|
|
|
|
}
|
|
|
|
const Encoding* encoding = xmldecl_parse(buf, bufLen);
|
|
|
|
if (encoding) {
|
|
|
|
if (forced &&
|
|
|
|
(encoding->IsAsciiCompatible() || encoding == ISO_2022_JP_ENCODING)) {
|
|
|
|
// Honor override
|
2021-06-21 15:09:01 +03:00
|
|
|
if (mCharsetSource == kCharsetFromFinalUserForcedAutoDetection) {
|
2021-03-23 12:52:04 +03:00
|
|
|
DontGuessEncoding();
|
|
|
|
} else {
|
|
|
|
FinalizeSniffingWithDetector(aFromSegment, aCountToSniffingLimit,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(
|
|
|
|
aFromSegment);
|
|
|
|
}
|
|
|
|
DontGuessEncoding();
|
|
|
|
mEncoding = WrapNotNull(encoding);
|
|
|
|
mCharsetSource = kCharsetFromXmlDeclaration;
|
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
|
|
|
} else if (mCharsetSource < kCharsetFromIrreversibleAutoDetection) {
|
|
|
|
// meta scan and XML declaration check failed.
|
|
|
|
// Check for BOMless UTF-16 with Basic
|
|
|
|
// Latin content for compat with IE. See bug 631751.
|
|
|
|
SniffBOMlessUTF16BasicLatin(buf, bufLen);
|
|
|
|
}
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2021-03-23 12:52:04 +03:00
|
|
|
if (forced && mCharsetSource != kCharsetFromIrreversibleAutoDetection) {
|
|
|
|
// neither meta nor XML declaration found, honor override
|
2021-06-21 15:09:01 +03:00
|
|
|
if (mCharsetSource == kCharsetFromFinalUserForcedAutoDetection) {
|
2021-03-23 12:52:04 +03:00
|
|
|
DontGuessEncoding();
|
|
|
|
} else {
|
|
|
|
FinalizeSniffingWithDetector(aFromSegment, aCountToSniffingLimit, false);
|
|
|
|
}
|
|
|
|
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(aFromSegment);
|
|
|
|
}
|
|
|
|
|
2011-02-16 09:40:35 +03:00
|
|
|
// the charset may have been set now
|
2017-04-27 13:27:03 +03:00
|
|
|
// maybe try chardet now;
|
2019-04-24 14:45:12 +03:00
|
|
|
if (mFeedChardet) {
|
2019-04-17 14:36:52 +03:00
|
|
|
FinalizeSniffingWithDetector(aFromSegment, aCountToSniffingLimit, aEof);
|
2021-03-23 12:52:04 +03:00
|
|
|
// fall thru; charset may have changed
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
if (mCharsetSource == kCharsetUninitialized) {
|
|
|
|
// Hopefully this case is never needed, but dealing with it anyway
|
2017-06-18 14:37:50 +03:00
|
|
|
mEncoding = WINDOWS_1252_ENCODING;
|
2013-11-04 15:24:33 +04:00
|
|
|
mCharsetSource = kCharsetFromFallback;
|
2017-06-18 14:37:50 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
2018-03-16 18:26:06 +03:00
|
|
|
} else if (mMode == LOAD_AS_DATA && mCharsetSource == kCharsetFromFallback) {
|
2011-11-16 11:38:51 +04:00
|
|
|
NS_ASSERTION(mReparseForbidden, "Reparse should be forbidden for XHR");
|
2019-04-16 22:05:26 +03:00
|
|
|
NS_ASSERTION(!mFeedChardet, "Should not feed chardet for XHR");
|
2018-03-16 18:26:06 +03:00
|
|
|
NS_ASSERTION(mEncoding == UTF_8_ENCODING, "XHR should default to UTF-8");
|
2011-11-16 11:38:51 +04:00
|
|
|
// Now mark charset source as non-weak to signal that we have a decision
|
|
|
|
mCharsetSource = kCharsetFromDocTypeDefault;
|
2017-06-18 14:37:50 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2018-12-06 21:59:56 +03:00
|
|
|
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(aFromSegment);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
2018-12-06 21:59:56 +03:00
|
|
|
nsresult nsHtml5StreamParser::SniffStreamBytes(
|
|
|
|
Span<const uint8_t> aFromSegment) {
|
2021-03-23 12:52:04 +03:00
|
|
|
MOZ_ASSERT(IsParserThread(), "Wrong thread!");
|
2017-06-18 14:37:50 +03:00
|
|
|
// mEncoding and mCharsetSource potentially have come from channel or higher
|
2012-11-06 15:57:51 +04:00
|
|
|
// by now. If we find a BOM, SetupDecodingFromBom() will overwrite them.
|
2017-06-18 14:37:50 +03:00
|
|
|
// If we don't find a BOM, the previously set values of mEncoding and
|
2012-11-06 15:57:51 +04:00
|
|
|
// mCharsetSource are not modified by the BOM sniffing here.
|
2018-12-06 21:59:56 +03:00
|
|
|
for (uint32_t i = 0;
|
|
|
|
i < aFromSegment.Length() && mBomState != BOM_SNIFFING_OVER; i++) {
|
2009-09-18 13:21:47 +04:00
|
|
|
switch (mBomState) {
|
|
|
|
case BOM_SNIFFING_NOT_STARTED:
|
2021-03-23 12:52:04 +03:00
|
|
|
MOZ_ASSERT(i == 0, "Bad BOM sniffing state.");
|
2018-12-06 21:59:56 +03:00
|
|
|
switch (aFromSegment[0]) {
|
2009-09-18 13:21:47 +04:00
|
|
|
case 0xEF:
|
|
|
|
mBomState = SEEN_UTF_8_FIRST_BYTE;
|
|
|
|
break;
|
|
|
|
case 0xFF:
|
|
|
|
mBomState = SEEN_UTF_16_LE_FIRST_BYTE;
|
|
|
|
break;
|
|
|
|
case 0xFE:
|
|
|
|
mBomState = SEEN_UTF_16_BE_FIRST_BYTE;
|
|
|
|
break;
|
2021-03-23 12:52:04 +03:00
|
|
|
case 0x00:
|
|
|
|
if (mCharsetSource < kCharsetFromXmlDeclarationUtf16 &&
|
|
|
|
!mChannelHadCharset) {
|
|
|
|
mBomState = SEEN_UTF_16_BE_XML_FIRST;
|
|
|
|
} else {
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0x3C:
|
|
|
|
if (mCharsetSource < kCharsetFromXmlDeclarationUtf16 &&
|
|
|
|
!mChannelHadCharset) {
|
|
|
|
mBomState = SEEN_UTF_16_LE_XML_FIRST;
|
|
|
|
} else {
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
}
|
|
|
|
break;
|
2009-09-18 13:21:47 +04:00
|
|
|
default:
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_LE_FIRST_BYTE:
|
|
|
|
if (aFromSegment[i] == 0xFE) {
|
2021-03-23 12:52:04 +03:00
|
|
|
SetupDecodingFromBom(UTF_16LE_ENCODING);
|
2018-12-06 21:59:56 +03:00
|
|
|
return WriteStreamBytes(aFromSegment.From(i + 1));
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_BE_FIRST_BYTE:
|
|
|
|
if (aFromSegment[i] == 0xFF) {
|
2021-03-23 12:52:04 +03:00
|
|
|
SetupDecodingFromBom(UTF_16BE_ENCODING);
|
2018-12-06 21:59:56 +03:00
|
|
|
return WriteStreamBytes(aFromSegment.From(i + 1));
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_8_FIRST_BYTE:
|
|
|
|
if (aFromSegment[i] == 0xBB) {
|
|
|
|
mBomState = SEEN_UTF_8_SECOND_BYTE;
|
|
|
|
} else {
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_8_SECOND_BYTE:
|
|
|
|
if (aFromSegment[i] == 0xBF) {
|
2021-03-23 12:52:04 +03:00
|
|
|
SetupDecodingFromBom(UTF_8_ENCODING);
|
|
|
|
return WriteStreamBytes(aFromSegment.From(i + 1));
|
|
|
|
}
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_BE_XML_FIRST:
|
|
|
|
if (aFromSegment[i] == 0x3C) {
|
|
|
|
mBomState = SEEN_UTF_16_BE_XML_SECOND;
|
|
|
|
} else {
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_BE_XML_SECOND:
|
|
|
|
if (aFromSegment[i] == 0x00) {
|
|
|
|
mBomState = SEEN_UTF_16_BE_XML_THIRD;
|
|
|
|
} else {
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_BE_XML_THIRD:
|
|
|
|
if (aFromSegment[i] == 0x3F) {
|
|
|
|
mBomState = SEEN_UTF_16_BE_XML_FOURTH;
|
|
|
|
} else {
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_BE_XML_FOURTH:
|
|
|
|
if (aFromSegment[i] == 0x00) {
|
|
|
|
mBomState = SEEN_UTF_16_BE_XML_FIFTH;
|
|
|
|
} else {
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_BE_XML_FIFTH:
|
|
|
|
if (aFromSegment[i] == 0x78) {
|
|
|
|
SetupDecodingFromUtf16BogoXml(UTF_16BE_ENCODING);
|
|
|
|
return WriteStreamBytes(aFromSegment.From(i + 1));
|
|
|
|
}
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_LE_XML_FIRST:
|
|
|
|
if (aFromSegment[i] == 0x00) {
|
|
|
|
mBomState = SEEN_UTF_16_LE_XML_SECOND;
|
|
|
|
} else {
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_LE_XML_SECOND:
|
|
|
|
if (aFromSegment[i] == 0x3F) {
|
|
|
|
mBomState = SEEN_UTF_16_LE_XML_THIRD;
|
|
|
|
} else {
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_LE_XML_THIRD:
|
|
|
|
if (aFromSegment[i] == 0x00) {
|
|
|
|
mBomState = SEEN_UTF_16_LE_XML_FOURTH;
|
|
|
|
} else {
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_LE_XML_FOURTH:
|
|
|
|
if (aFromSegment[i] == 0x78) {
|
|
|
|
mBomState = SEEN_UTF_16_LE_XML_FIFTH;
|
|
|
|
} else {
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_LE_XML_FIFTH:
|
|
|
|
if (aFromSegment[i] == 0x00) {
|
|
|
|
SetupDecodingFromUtf16BogoXml(UTF_16LE_ENCODING);
|
2018-12-06 21:59:56 +03:00
|
|
|
return WriteStreamBytes(aFromSegment.From(i + 1));
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
break;
|
|
|
|
default:
|
2010-04-09 18:11:56 +04:00
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
break;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
}
|
2012-11-06 15:57:51 +04:00
|
|
|
// if we get here, there either was no BOM or the BOM sniffing isn't complete
|
|
|
|
// yet
|
2017-04-27 13:27:03 +03:00
|
|
|
|
2013-11-25 12:06:56 +04:00
|
|
|
MOZ_ASSERT(mCharsetSource != kCharsetFromByteOrderMark,
|
|
|
|
"Should not come here if BOM was found.");
|
2021-03-23 12:52:04 +03:00
|
|
|
MOZ_ASSERT(mCharsetSource != kCharsetFromXmlDeclarationUtf16,
|
|
|
|
"Should not come here if UTF-16 bogo-XML declaration was found.");
|
2013-11-25 12:06:56 +04:00
|
|
|
MOZ_ASSERT(mCharsetSource != kCharsetFromOtherComponent,
|
|
|
|
"kCharsetFromOtherComponent is for XSLT.");
|
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
if (mBomState == BOM_SNIFFING_OVER && mCharsetSource == kCharsetFromChannel) {
|
2017-06-18 14:37:50 +03:00
|
|
|
// There was no BOM and the charset came from channel. mEncoding
|
2013-11-25 12:06:56 +04:00
|
|
|
// still contains the charset from the channel as set by an
|
2012-11-06 15:57:51 +04:00
|
|
|
// earlier call to SetDocumentCharset(), since we didn't find a BOM and
|
2017-06-18 14:37:50 +03:00
|
|
|
// overwrite mEncoding. (Note that if the user has overridden the charset,
|
2013-11-25 12:06:56 +04:00
|
|
|
// we don't come here but check <meta> for XSS-dangerous charsets first.)
|
2017-06-18 14:37:50 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
2018-12-06 21:59:56 +03:00
|
|
|
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(aFromSegment);
|
2012-11-06 15:57:51 +04:00
|
|
|
}
|
|
|
|
|
2021-03-23 12:52:04 +03:00
|
|
|
if (!mChannelHadCharset && !mMetaScanner &&
|
2018-03-16 18:26:06 +03:00
|
|
|
(mMode == NORMAL || mMode == VIEW_SOURCE_HTML || mMode == LOAD_AS_DATA)) {
|
2020-02-28 23:49:06 +03:00
|
|
|
mMetaScanner = MakeUnique<nsHtml5MetaScanner>(mTreeBuilder.get());
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
|
2018-12-06 21:59:56 +03:00
|
|
|
if (mSniffingLength + aFromSegment.Length() >= SNIFFING_BUFFER_SIZE) {
|
2009-09-18 13:21:47 +04:00
|
|
|
// this is the last buffer
|
2018-12-04 11:02:04 +03:00
|
|
|
uint32_t countToSniffingLimit = SNIFFING_BUFFER_SIZE - mSniffingLength;
|
2021-01-14 10:06:53 +03:00
|
|
|
bool forced =
|
2021-06-21 15:09:01 +03:00
|
|
|
(mCharsetSource == kCharsetFromPendingUserForcedAutoDetection ||
|
2021-01-14 10:06:53 +03:00
|
|
|
mCharsetSource == kCharsetFromInitialUserForcedAutoDetection ||
|
|
|
|
mCharsetSource == kCharsetFromFinalUserForcedAutoDetection);
|
2021-03-23 12:52:04 +03:00
|
|
|
if (!mChannelHadCharset && (mMode == NORMAL || mMode == VIEW_SOURCE_HTML ||
|
|
|
|
mMode == LOAD_AS_DATA)) {
|
2018-12-06 21:59:56 +03:00
|
|
|
nsHtml5ByteReadable readable(
|
|
|
|
aFromSegment.Elements(),
|
|
|
|
aFromSegment.Elements() + countToSniffingLimit);
|
2017-06-17 05:54:40 +03:00
|
|
|
nsAutoCString charset;
|
2017-06-18 14:37:50 +03:00
|
|
|
auto encoding = mMetaScanner->sniff(&readable);
|
2015-08-25 18:05:46 +03:00
|
|
|
// Due to the way nsHtml5Portability reports OOM, ask the tree buider
|
|
|
|
nsresult rv;
|
|
|
|
if (NS_FAILED((rv = mTreeBuilder->IsBroken()))) {
|
|
|
|
MarkAsBroken(rv);
|
|
|
|
return rv;
|
|
|
|
}
|
2021-03-23 12:52:04 +03:00
|
|
|
|
2017-06-18 14:37:50 +03:00
|
|
|
if (encoding) {
|
2013-11-25 12:06:56 +04:00
|
|
|
// meta scan successful; honor overrides unless meta is XSS-dangerous
|
2021-01-14 10:06:53 +03:00
|
|
|
if (forced && (encoding->IsAsciiCompatible() ||
|
|
|
|
encoding == ISO_2022_JP_ENCODING)) {
|
2013-11-25 12:06:56 +04:00
|
|
|
// Honor override
|
2021-06-21 15:09:01 +03:00
|
|
|
if (mCharsetSource == kCharsetFromFinalUserForcedAutoDetection) {
|
2019-12-12 20:50:19 +03:00
|
|
|
DontGuessEncoding();
|
2021-01-14 10:06:53 +03:00
|
|
|
} else {
|
|
|
|
FinalizeSniffingWithDetector(aFromSegment, countToSniffingLimit,
|
|
|
|
false);
|
2019-04-17 14:36:52 +03:00
|
|
|
}
|
2013-11-25 12:06:56 +04:00
|
|
|
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(
|
2018-12-06 21:59:56 +03:00
|
|
|
aFromSegment);
|
2013-11-25 12:06:56 +04:00
|
|
|
}
|
2021-03-23 12:52:04 +03:00
|
|
|
DontGuessEncoding();
|
2017-06-18 14:37:50 +03:00
|
|
|
mEncoding = WrapNotNull(encoding);
|
2011-11-01 15:33:11 +04:00
|
|
|
mCharsetSource = kCharsetFromMetaPrescan;
|
2017-06-18 14:37:50 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
2013-11-25 12:06:56 +04:00
|
|
|
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(
|
2018-12-06 21:59:56 +03:00
|
|
|
aFromSegment);
|
2011-11-01 15:33:11 +04:00
|
|
|
}
|
|
|
|
}
|
2018-12-06 21:59:56 +03:00
|
|
|
return FinalizeSniffing(aFromSegment, countToSniffingLimit, false);
|
2011-11-01 15:33:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// not the last buffer
|
2021-03-23 12:52:04 +03:00
|
|
|
if (!mChannelHadCharset &&
|
|
|
|
(mMode == NORMAL || mMode == VIEW_SOURCE_HTML || mMode == LOAD_AS_DATA)) {
|
2018-12-06 21:59:56 +03:00
|
|
|
nsHtml5ByteReadable readable(
|
|
|
|
aFromSegment.Elements(),
|
|
|
|
aFromSegment.Elements() + aFromSegment.Length());
|
2017-06-18 14:37:50 +03:00
|
|
|
auto encoding = mMetaScanner->sniff(&readable);
|
2015-08-25 18:05:46 +03:00
|
|
|
// Due to the way nsHtml5Portability reports OOM, ask the tree buider
|
|
|
|
nsresult rv;
|
|
|
|
if (NS_FAILED((rv = mTreeBuilder->IsBroken()))) {
|
|
|
|
MarkAsBroken(rv);
|
|
|
|
return rv;
|
|
|
|
}
|
2017-06-18 14:37:50 +03:00
|
|
|
if (encoding) {
|
2013-11-25 12:06:56 +04:00
|
|
|
// meta scan successful; honor overrides unless meta is XSS-dangerous
|
2021-06-21 15:09:01 +03:00
|
|
|
if ((mCharsetSource == kCharsetFromFinalUserForcedAutoDetection) &&
|
2018-03-16 18:26:06 +03:00
|
|
|
(encoding->IsAsciiCompatible() || encoding == ISO_2022_JP_ENCODING)) {
|
2013-11-25 12:06:56 +04:00
|
|
|
// Honor override
|
2018-03-16 18:26:06 +03:00
|
|
|
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(
|
2018-12-06 21:59:56 +03:00
|
|
|
aFromSegment);
|
2013-11-25 12:06:56 +04:00
|
|
|
}
|
2021-01-14 10:06:53 +03:00
|
|
|
if ((mCharsetSource == kCharsetFromPendingUserForcedAutoDetection ||
|
|
|
|
mCharsetSource == kCharsetFromInitialUserForcedAutoDetection) &&
|
|
|
|
(encoding->IsAsciiCompatible() || encoding == ISO_2022_JP_ENCODING)) {
|
|
|
|
FinalizeSniffingWithDetector(aFromSegment, aFromSegment.Length(),
|
|
|
|
false);
|
|
|
|
} else {
|
2021-03-23 12:52:04 +03:00
|
|
|
DontGuessEncoding();
|
2021-01-14 10:06:53 +03:00
|
|
|
mEncoding = WrapNotNull(encoding);
|
|
|
|
mCharsetSource = kCharsetFromMetaPrescan;
|
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
|
|
|
}
|
2018-12-06 21:59:56 +03:00
|
|
|
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(aFromSegment);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSniffingBuffer) {
|
2018-03-16 18:26:06 +03:00
|
|
|
mSniffingBuffer = MakeUniqueFallible<uint8_t[]>(SNIFFING_BUFFER_SIZE);
|
2011-09-28 16:45:17 +04:00
|
|
|
if (!mSniffingBuffer) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2018-12-06 21:59:56 +03:00
|
|
|
memcpy(&mSniffingBuffer[mSniffingLength], aFromSegment.Elements(),
|
|
|
|
aFromSegment.Length());
|
|
|
|
mSniffingLength += aFromSegment.Length();
|
2009-09-18 13:21:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-08-05 00:33:07 +03:00
|
|
|
class AddContentRunnable : public Runnable {
|
|
|
|
public:
|
|
|
|
AddContentRunnable(const nsAString& aParserID, nsIURI* aURI,
|
|
|
|
Span<const char16_t> aData, bool aComplete)
|
|
|
|
: Runnable("AddContent") {
|
|
|
|
nsAutoCString spec;
|
|
|
|
aURI->GetSpec(spec);
|
|
|
|
mData.mUri.Construct(NS_ConvertUTF8toUTF16(spec));
|
|
|
|
mData.mParserID.Construct(aParserID);
|
|
|
|
mData.mContents.Construct(aData.Elements(), aData.Length());
|
|
|
|
mData.mComplete.Construct(aComplete);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
nsAutoString json;
|
|
|
|
if (!mData.ToJSON(json)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obsService = services::GetObserverService();
|
|
|
|
if (obsService) {
|
|
|
|
obsService->NotifyObservers(nullptr, "devtools-html-content",
|
|
|
|
PromiseFlatString(json).get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLContent mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void nsHtml5StreamParser::OnNewContent(Span<const char16_t> aData) {
|
|
|
|
if (mURIToSendToDevtools) {
|
|
|
|
NS_DispatchToMainThread(new AddContentRunnable(mUUIDForDevtools,
|
|
|
|
mURIToSendToDevtools, aData,
|
|
|
|
/* aComplete */ false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void nsHtml5StreamParser::OnContentComplete() {
|
|
|
|
if (mURIToSendToDevtools) {
|
|
|
|
NS_DispatchToMainThread(new AddContentRunnable(
|
|
|
|
mUUIDForDevtools, mURIToSendToDevtools, Span<const char16_t>(),
|
|
|
|
/* aComplete */ true));
|
|
|
|
mURIToSendToDevtools = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:59:56 +03:00
|
|
|
nsresult nsHtml5StreamParser::WriteStreamBytes(
|
|
|
|
Span<const uint8_t> aFromSegment) {
|
2009-09-25 21:11:02 +04:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2013-10-16 19:16:36 +04:00
|
|
|
// mLastBuffer should always point to a buffer of the size
|
2018-12-04 11:02:04 +03:00
|
|
|
// READ_BUFFER_SIZE.
|
2013-10-16 19:16:36 +04:00
|
|
|
if (!mLastBuffer) {
|
|
|
|
NS_WARNING("mLastBuffer should not be null!");
|
2014-11-05 18:44:37 +03:00
|
|
|
MarkAsBroken(NS_ERROR_NULL_POINTER);
|
2013-10-16 19:16:36 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
size_t totalRead = 0;
|
2018-12-06 21:59:56 +03:00
|
|
|
auto src = aFromSegment;
|
2009-09-18 13:21:47 +04:00
|
|
|
for (;;) {
|
2018-12-04 11:02:04 +03:00
|
|
|
auto dst = mLastBuffer->TailAsSpan(READ_BUFFER_SIZE);
|
2017-04-27 13:27:03 +03:00
|
|
|
uint32_t result;
|
|
|
|
size_t read;
|
|
|
|
size_t written;
|
|
|
|
bool hadErrors;
|
|
|
|
Tie(result, read, written, hadErrors) =
|
|
|
|
mUnicodeDecoder->DecodeToUTF16(src, dst, false);
|
2019-12-12 20:50:19 +03:00
|
|
|
if (!mDecodingLocalFileWithoutTokenizing) {
|
2019-08-05 00:33:07 +03:00
|
|
|
OnNewContent(dst.To(written));
|
2018-07-24 00:43:13 +03:00
|
|
|
}
|
2018-04-06 00:20:28 +03:00
|
|
|
if (hadErrors && !mHasHadErrors) {
|
|
|
|
mHasHadErrors = true;
|
|
|
|
if (mEncoding == UTF_8_ENCODING) {
|
|
|
|
mTreeBuilder->TryToEnableEncodingMenu();
|
|
|
|
}
|
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
src = src.From(read);
|
|
|
|
totalRead += read;
|
|
|
|
mLastBuffer->AdvanceEnd(written);
|
|
|
|
if (result == kOutputFull) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsHtml5OwningUTF16Buffer> newBuf =
|
2011-09-28 16:45:17 +04:00
|
|
|
nsHtml5OwningUTF16Buffer::FalliblyCreate(READ_BUFFER_SIZE);
|
|
|
|
if (!newBuf) {
|
2017-06-22 14:32:34 +03:00
|
|
|
MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
|
2011-09-28 16:45:17 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2020-02-13 17:38:48 +03:00
|
|
|
mLastBuffer = (mLastBuffer->next = std::move(newBuf));
|
2009-09-18 13:21:47 +04:00
|
|
|
} else {
|
2018-12-06 21:59:56 +03:00
|
|
|
MOZ_ASSERT(totalRead == aFromSegment.Length(),
|
2017-04-27 13:27:03 +03:00
|
|
|
"The Unicode decoder consumed the wrong number of bytes.");
|
2019-12-12 20:50:19 +03:00
|
|
|
if (mDecodingLocalFileWithoutTokenizing &&
|
2018-12-04 11:02:04 +03:00
|
|
|
mLocalFileBytesBuffered == LOCAL_FILE_UTF_8_BUFFER_SIZE) {
|
2019-12-12 20:50:19 +03:00
|
|
|
auto encoding = mEncoding;
|
|
|
|
GuessEncoding(false, false);
|
|
|
|
if (encoding == mEncoding) {
|
|
|
|
CommitLocalFileToEncoding();
|
|
|
|
} else {
|
|
|
|
ReDecodeLocalFile();
|
|
|
|
}
|
2018-12-04 11:02:04 +03:00
|
|
|
}
|
2009-09-18 13:21:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 11:02:04 +03:00
|
|
|
void nsHtml5StreamParser::ReDecodeLocalFile() {
|
2019-12-12 20:50:19 +03:00
|
|
|
MOZ_ASSERT(mDecodingLocalFileWithoutTokenizing);
|
|
|
|
mDecodingLocalFileWithoutTokenizing = false;
|
2018-12-04 11:02:04 +03:00
|
|
|
mUnicodeDecoder = mEncoding->NewDecoderWithBOMRemoval();
|
|
|
|
mHasHadErrors = false;
|
|
|
|
|
2019-12-12 20:50:19 +03:00
|
|
|
DontGuessEncoding();
|
2019-04-24 14:45:12 +03:00
|
|
|
|
2018-12-04 11:02:04 +03:00
|
|
|
// Throw away previous decoded data
|
|
|
|
mLastBuffer = mFirstBuffer;
|
|
|
|
mLastBuffer->next = nullptr;
|
|
|
|
mLastBuffer->setStart(0);
|
|
|
|
mLastBuffer->setEnd(0);
|
|
|
|
|
|
|
|
// Decode again
|
|
|
|
for (auto&& buffer : mBufferedLocalFileData) {
|
|
|
|
DoDataAvailable(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 20:50:19 +03:00
|
|
|
void nsHtml5StreamParser::CommitLocalFileToEncoding() {
|
|
|
|
MOZ_ASSERT(mDecodingLocalFileWithoutTokenizing);
|
|
|
|
mDecodingLocalFileWithoutTokenizing = false;
|
2019-04-16 22:05:26 +03:00
|
|
|
mFeedChardet = false;
|
2019-12-12 20:50:19 +03:00
|
|
|
mGuessEncoding = false;
|
2018-12-04 11:02:04 +03:00
|
|
|
|
2019-08-05 00:33:07 +03:00
|
|
|
nsHtml5OwningUTF16Buffer* buffer = mFirstBuffer;
|
|
|
|
while (buffer) {
|
|
|
|
Span<const char16_t> data(buffer->getBuffer() + buffer->getStart(),
|
|
|
|
buffer->getLength());
|
|
|
|
OnNewContent(data);
|
|
|
|
buffer = buffer->next;
|
2018-12-04 11:02:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-07 15:55:07 +03:00
|
|
|
class MaybeRunCollector : public Runnable {
|
|
|
|
public:
|
|
|
|
explicit MaybeRunCollector(nsIDocShell* aDocShell)
|
|
|
|
: Runnable("MaybeRunCollector"), mDocShell(aDocShell) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
nsJSContext::MaybeRunNextCollectorSlice(mDocShell,
|
|
|
|
JS::GCReason::HTML_PARSER);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShell> mDocShell;
|
|
|
|
};
|
|
|
|
|
2019-02-28 02:41:04 +03:00
|
|
|
nsresult nsHtml5StreamParser::OnStartRequest(nsIRequest* aRequest) {
|
2017-10-06 10:42:21 +03:00
|
|
|
MOZ_RELEASE_ASSERT(STREAM_NOT_STARTED == mStreamState,
|
|
|
|
"Got OnStartRequest when the stream had already started.");
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(
|
2017-04-27 13:27:03 +03:00
|
|
|
!mExecutor->HasStarted(),
|
|
|
|
"Got OnStartRequest at the wrong stage in the executor life cycle.");
|
2019-12-12 20:50:19 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
|
|
|
|
// To avoid the cost of instantiating the detector when it's not needed,
|
|
|
|
// let's instantiate only if we make it out of this method with the
|
|
|
|
// intent to use it.
|
|
|
|
auto detectorCreator = MakeScopeExit([&] {
|
2021-06-21 15:09:01 +03:00
|
|
|
if (mFeedChardet) {
|
2019-12-12 20:50:19 +03:00
|
|
|
mDetector = mozilla::EncodingDetector::Create();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
if (mObserver) {
|
2019-02-28 02:41:04 +03:00
|
|
|
mObserver->OnStartRequest(aRequest);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
mRequest = aRequest;
|
|
|
|
|
2009-09-21 17:18:20 +04:00
|
|
|
mStreamState = STREAM_BEING_READ;
|
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
if (mMode == VIEW_SOURCE_HTML || mMode == VIEW_SOURCE_XML) {
|
2011-11-30 21:44:31 +04:00
|
|
|
mTokenizer->StartViewSource(NS_ConvertUTF8toUTF16(mViewSourceTitle));
|
2010-07-30 14:15:38 +04:00
|
|
|
}
|
2011-11-30 21:44:31 +04:00
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
// For View Source, the parser should run with scripts "enabled" if a normal
|
|
|
|
// load would have scripts enabled.
|
2018-03-16 18:26:06 +03:00
|
|
|
bool scriptingEnabled =
|
|
|
|
mMode == LOAD_AS_DATA ? false : mExecutor->IsScriptEnabled();
|
2009-09-21 17:18:20 +04:00
|
|
|
mOwner->StartTokenizer(scriptingEnabled);
|
2013-09-10 21:40:50 +04:00
|
|
|
|
2019-12-12 20:50:19 +03:00
|
|
|
MOZ_ASSERT(!mDecodingLocalFileWithoutTokenizing);
|
2013-09-10 21:40:50 +04:00
|
|
|
bool isSrcdoc = false;
|
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
nsresult rv = GetChannel(getter_AddRefs(channel));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
isSrcdoc = NS_IsSrcdocChannel(channel);
|
2021-06-21 15:09:01 +03:00
|
|
|
if (!isSrcdoc && mCharsetSource <= kCharsetFromFallback) {
|
2018-12-04 11:02:04 +03:00
|
|
|
nsCOMPtr<nsIURI> originalURI;
|
|
|
|
rv = channel->GetOriginalURI(getter_AddRefs(originalURI));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2019-08-07 22:49:40 +03:00
|
|
|
if (originalURI->SchemeIs("resource")) {
|
2018-12-04 11:02:04 +03:00
|
|
|
mCharsetSource = kCharsetFromBuiltIn;
|
|
|
|
mEncoding = UTF_8_ENCODING;
|
2020-06-15 18:32:21 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
2018-12-04 11:02:04 +03:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIURI> currentURI;
|
|
|
|
rv = channel->GetURI(getter_AddRefs(currentURI));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIURI> innermost = NS_GetInnermostURI(currentURI);
|
2019-12-12 20:50:19 +03:00
|
|
|
if (innermost->SchemeIs("file")) {
|
|
|
|
mDecodingLocalFileWithoutTokenizing = true;
|
|
|
|
} else {
|
|
|
|
nsAutoCString host;
|
|
|
|
innermost->GetAsciiHost(host);
|
|
|
|
if (!host.IsEmpty()) {
|
|
|
|
// First let's see if the host is DNS-absolute and ends with a
|
|
|
|
// dot and get rid of that one.
|
|
|
|
if (host.Last() == '.') {
|
|
|
|
host.SetLength(host.Length() - 1);
|
|
|
|
}
|
|
|
|
int32_t index = host.RFindChar('.');
|
|
|
|
if (index != kNotFound) {
|
|
|
|
// We tolerate an IPv4 component as generic "TLD", so don't
|
|
|
|
// bother checking.
|
|
|
|
ToLowerCase(
|
|
|
|
Substring(host, index + 1, host.Length() - (index + 1)),
|
|
|
|
mTLD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-04 11:02:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-10 21:40:50 +04:00
|
|
|
}
|
|
|
|
mTreeBuilder->setIsSrcdocDocument(isSrcdoc);
|
2009-09-21 17:18:20 +04:00
|
|
|
mTreeBuilder->setScriptingEnabled(scriptingEnabled);
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->SetPreventScriptExecution(
|
|
|
|
!((mMode == NORMAL) && scriptingEnabled));
|
2009-09-21 17:18:20 +04:00
|
|
|
mTokenizer->start();
|
|
|
|
mExecutor->Start();
|
|
|
|
mExecutor->StartReadingFromStage();
|
2011-11-01 19:27:36 +04:00
|
|
|
|
|
|
|
if (mMode == PLAIN_TEXT) {
|
|
|
|
mTreeBuilder->StartPlainText();
|
|
|
|
mTokenizer->StartPlainText();
|
2011-11-30 21:44:31 +04:00
|
|
|
} else if (mMode == VIEW_SOURCE_PLAIN) {
|
2015-08-25 18:05:45 +03:00
|
|
|
nsAutoString viewSourceTitle;
|
|
|
|
CopyUTF8toUTF16(mViewSourceTitle, viewSourceTitle);
|
|
|
|
mTreeBuilder->EnsureBufferSpace(viewSourceTitle.Length());
|
|
|
|
mTreeBuilder->StartPlainTextViewSource(viewSourceTitle);
|
2011-11-30 21:44:31 +04:00
|
|
|
mTokenizer->StartPlainText();
|
2011-11-01 19:27:36 +04:00
|
|
|
}
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
/*
|
2017-04-27 13:27:03 +03:00
|
|
|
* If you move the following line, be very careful not to cause
|
|
|
|
* WillBuildModel to be called before the document has had its
|
2009-09-18 13:21:47 +04:00
|
|
|
* script global object set.
|
|
|
|
*/
|
2014-11-05 18:44:37 +03:00
|
|
|
rv = mExecutor->WillBuildModel(eDTDMode_unknown);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-04-27 13:27:03 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsHtml5OwningUTF16Buffer> newBuf =
|
2011-09-28 16:45:17 +04:00
|
|
|
nsHtml5OwningUTF16Buffer::FalliblyCreate(READ_BUFFER_SIZE);
|
|
|
|
if (!newBuf) {
|
2012-03-21 16:39:25 +04:00
|
|
|
// marks this stream parser as terminated,
|
|
|
|
// which prevents entry to code paths that
|
|
|
|
// would use mFirstBuffer or mLastBuffer.
|
|
|
|
return mExecutor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
|
2011-09-28 16:45:17 +04:00
|
|
|
}
|
2018-12-04 11:02:04 +03:00
|
|
|
MOZ_ASSERT(!mFirstBuffer, "How come we have the first buffer set?");
|
|
|
|
MOZ_ASSERT(!mLastBuffer, "How come we have the last buffer set?");
|
2011-09-28 16:45:17 +04:00
|
|
|
mFirstBuffer = mLastBuffer = newBuf;
|
|
|
|
|
2013-09-10 21:40:50 +04:00
|
|
|
rv = NS_OK;
|
2009-09-21 17:18:20 +04:00
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
// The line below means that the encoding can end up being wrong if
|
|
|
|
// a view-source URL is loaded without having the encoding hint from a
|
|
|
|
// previous normal load in the history.
|
2011-11-01 19:27:36 +04:00
|
|
|
mReparseForbidden = !(mMode == NORMAL || mMode == PLAIN_TEXT);
|
2010-07-30 14:15:38 +04:00
|
|
|
|
2021-03-18 22:24:50 +03:00
|
|
|
mNetworkEventTarget =
|
|
|
|
mExecutor->GetDocument()->EventTargetFor(TaskCategory::Network);
|
2017-06-27 20:38:30 +03:00
|
|
|
|
2009-09-21 17:18:20 +04:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(mRequest, &rv));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2017-06-27 20:38:30 +03:00
|
|
|
// Non-HTTP channels are bogus enough that we let them work with unlabeled
|
|
|
|
// runnables for now. Asserting for HTTP channels only.
|
2021-03-18 22:24:50 +03:00
|
|
|
MOZ_ASSERT(mNetworkEventTarget || mMode == LOAD_AS_DATA,
|
|
|
|
"How come the network event target is still null?");
|
2017-06-27 20:38:30 +03:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString method;
|
2016-12-20 09:36:24 +03:00
|
|
|
Unused << httpChannel->GetRequestMethod(method);
|
2009-09-21 17:18:20 +04:00
|
|
|
// XXX does Necko have a way to renavigate POST, etc. without hitting
|
|
|
|
// the network?
|
|
|
|
if (!method.EqualsLiteral("GET")) {
|
|
|
|
// This is the old Gecko behavior but the HTML5 spec disagrees.
|
|
|
|
// Don't reparse on POST.
|
2011-10-17 18:59:28 +04:00
|
|
|
mReparseForbidden = true;
|
2009-09-21 17:18:20 +04:00
|
|
|
}
|
2014-06-17 20:39:19 +04:00
|
|
|
}
|
2013-07-09 05:45:26 +04:00
|
|
|
|
2014-06-17 20:39:19 +04:00
|
|
|
// Attempt to retarget delivery of data (via OnDataAvailable) to the parser
|
|
|
|
// thread, rather than through the main thread.
|
|
|
|
nsCOMPtr<nsIThreadRetargetableRequest> threadRetargetableRequest =
|
|
|
|
do_QueryInterface(mRequest, &rv);
|
|
|
|
if (threadRetargetableRequest) {
|
2017-06-08 02:23:29 +03:00
|
|
|
rv = threadRetargetableRequest->RetargetDeliveryTo(mEventTarget);
|
2021-01-07 15:55:07 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Parser thread should be now ready to get data from necko and parse it
|
|
|
|
// and main thread might have a chance to process a collector slice.
|
|
|
|
// We need to do this asynchronously so that necko may continue processing
|
|
|
|
// the request.
|
|
|
|
nsCOMPtr<nsIRunnable> runnable =
|
|
|
|
new MaybeRunCollector(mExecutor->GetDocument()->GetDocShell());
|
|
|
|
mozilla::SchedulerGroup::Dispatch(
|
|
|
|
mozilla::TaskCategory::GarbageCollection, runnable.forget());
|
|
|
|
}
|
2014-06-17 20:39:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2017-03-20 12:29:07 +03:00
|
|
|
NS_WARNING("Failed to retarget HTML data delivery to the parser thread.");
|
2009-09-21 17:18:20 +04:00
|
|
|
}
|
2011-02-11 13:33:32 +03:00
|
|
|
|
2012-03-24 15:34:42 +04:00
|
|
|
if (mCharsetSource == kCharsetFromParentFrame) {
|
2019-12-12 20:50:19 +03:00
|
|
|
// Remember this for error reporting.
|
2012-03-24 15:34:42 +04:00
|
|
|
mInitialEncodingWasFromParentFrame = true;
|
|
|
|
}
|
|
|
|
|
2021-01-14 10:06:53 +03:00
|
|
|
if (!(mCharsetSource == kCharsetFromPendingUserForcedAutoDetection ||
|
|
|
|
mCharsetSource == kCharsetFromInitialUserForcedAutoDetection ||
|
|
|
|
mCharsetSource == kCharsetFromFinalUserForcedAutoDetection)) {
|
2021-06-21 15:09:01 +03:00
|
|
|
if (mCharsetSource >= kCharsetFromFinalAutoDetectionWouldHaveBeenUTF8) {
|
|
|
|
DontGuessEncoding();
|
2019-12-12 20:50:19 +03:00
|
|
|
}
|
2011-02-11 13:33:32 +03:00
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
|
2019-02-28 04:09:48 +03:00
|
|
|
if (mCharsetSource < kCharsetFromUtf8OnlyMime) {
|
2009-09-18 13:21:47 +04:00
|
|
|
// we aren't ready to commit to an encoding yet
|
|
|
|
// leave converter uninstantiated for now
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-01-18 18:27:03 +04:00
|
|
|
|
2019-04-17 14:36:52 +03:00
|
|
|
// We are loading JSON/WebVTT/etc. into a browsing context.
|
|
|
|
// There's no need to remove the BOM manually here, because
|
|
|
|
// the UTF-8 decoder removes it.
|
2013-01-18 18:27:03 +04:00
|
|
|
mReparseForbidden = true;
|
2019-12-12 20:50:19 +03:00
|
|
|
DontGuessEncoding();
|
2013-01-18 18:27:03 +04:00
|
|
|
|
|
|
|
// Instantiate the converter here to avoid BOM sniffing.
|
2019-12-12 20:50:19 +03:00
|
|
|
mDecodingLocalFileWithoutTokenizing = false;
|
2017-06-18 14:37:50 +03:00
|
|
|
mUnicodeDecoder = mEncoding->NewDecoderWithBOMRemoval();
|
2009-09-18 13:21:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-09 05:45:26 +04:00
|
|
|
nsresult nsHtml5StreamParser::CheckListenerChain() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!");
|
|
|
|
if (!mObserver) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIThreadRetargetableStreamListener> retargetable =
|
|
|
|
do_QueryInterface(mObserver, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv) && retargetable) {
|
|
|
|
rv = retargetable->CheckListenerChain();
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-09-25 21:11:02 +04:00
|
|
|
void nsHtml5StreamParser::DoStopRequest() {
|
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2017-10-06 10:42:21 +03:00
|
|
|
MOZ_RELEASE_ASSERT(STREAM_BEING_READ == mStreamState,
|
|
|
|
"Stream ended without being open.");
|
2009-10-12 17:08:04 +04:00
|
|
|
mTokenizerMutex.AssertCurrentThreadOwns();
|
|
|
|
|
2019-08-05 00:33:07 +03:00
|
|
|
auto guard = MakeScopeExit([&] { OnContentComplete(); });
|
|
|
|
|
2009-10-12 17:08:04 +04:00
|
|
|
if (IsTerminated()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
if (!mUnicodeDecoder) {
|
2014-11-05 18:44:37 +03:00
|
|
|
nsresult rv;
|
2018-12-06 21:59:56 +03:00
|
|
|
Span<const uint8_t> empty;
|
|
|
|
if (NS_FAILED(rv = FinalizeSniffing(empty, 0, true))) {
|
2014-11-05 18:44:37 +03:00
|
|
|
MarkAsBroken(rv);
|
2011-09-28 16:45:17 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-12-04 11:02:04 +03:00
|
|
|
}
|
2019-04-24 14:45:12 +03:00
|
|
|
if (mFeedChardet) {
|
2019-04-16 22:05:26 +03:00
|
|
|
mFeedChardet = false;
|
|
|
|
FeedDetector(Span<uint8_t>(), true);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2009-09-21 17:18:20 +04:00
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
MOZ_ASSERT(mUnicodeDecoder,
|
|
|
|
"Should have a decoder after finalizing sniffing.");
|
2017-06-22 14:32:34 +03:00
|
|
|
|
|
|
|
// mLastBuffer should always point to a buffer of the size
|
2018-12-04 11:02:04 +03:00
|
|
|
// READ_BUFFER_SIZE.
|
2017-06-22 14:32:34 +03:00
|
|
|
if (!mLastBuffer) {
|
|
|
|
NS_WARNING("mLastBuffer should not be null!");
|
|
|
|
MarkAsBroken(NS_ERROR_NULL_POINTER);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Span<uint8_t> src; // empty span
|
|
|
|
for (;;) {
|
2018-12-04 11:02:04 +03:00
|
|
|
auto dst = mLastBuffer->TailAsSpan(READ_BUFFER_SIZE);
|
2017-06-22 14:32:34 +03:00
|
|
|
uint32_t result;
|
|
|
|
size_t read;
|
|
|
|
size_t written;
|
|
|
|
bool hadErrors;
|
|
|
|
Tie(result, read, written, hadErrors) =
|
|
|
|
mUnicodeDecoder->DecodeToUTF16(src, dst, true);
|
2019-12-12 20:50:19 +03:00
|
|
|
if (!mDecodingLocalFileWithoutTokenizing) {
|
2019-08-05 00:33:07 +03:00
|
|
|
OnNewContent(dst.To(written));
|
2018-07-24 00:43:13 +03:00
|
|
|
}
|
2018-04-06 00:20:28 +03:00
|
|
|
if (hadErrors && !mHasHadErrors) {
|
|
|
|
mHasHadErrors = true;
|
|
|
|
if (mEncoding == UTF_8_ENCODING) {
|
|
|
|
mTreeBuilder->TryToEnableEncodingMenu();
|
|
|
|
}
|
|
|
|
}
|
2017-06-22 14:32:34 +03:00
|
|
|
MOZ_ASSERT(read == 0, "How come an empty span was read form?");
|
|
|
|
mLastBuffer->AdvanceEnd(written);
|
|
|
|
if (result == kOutputFull) {
|
|
|
|
RefPtr<nsHtml5OwningUTF16Buffer> newBuf =
|
|
|
|
nsHtml5OwningUTF16Buffer::FalliblyCreate(READ_BUFFER_SIZE);
|
|
|
|
if (!newBuf) {
|
|
|
|
MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
2020-02-13 17:38:48 +03:00
|
|
|
mLastBuffer = (mLastBuffer->next = std::move(newBuf));
|
2017-06-22 14:32:34 +03:00
|
|
|
} else {
|
2019-12-12 20:50:19 +03:00
|
|
|
if (mDecodingLocalFileWithoutTokenizing) {
|
2018-12-04 11:02:04 +03:00
|
|
|
MOZ_ASSERT(mLocalFileBytesBuffered < LOCAL_FILE_UTF_8_BUFFER_SIZE);
|
2019-12-12 20:50:19 +03:00
|
|
|
MOZ_ASSERT(mGuessEncoding);
|
|
|
|
auto encoding = mEncoding;
|
|
|
|
GuessEncoding(true, false);
|
|
|
|
if (encoding == mEncoding) {
|
|
|
|
CommitLocalFileToEncoding();
|
|
|
|
} else {
|
|
|
|
ReDecodeLocalFile();
|
|
|
|
DoStopRequest();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (mGuessEncoding) {
|
|
|
|
GuessEncoding(true, false);
|
2018-12-04 11:02:04 +03:00
|
|
|
}
|
2017-06-22 14:32:34 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 11:02:04 +03:00
|
|
|
mStreamState = STREAM_ENDED;
|
|
|
|
|
2009-10-12 17:08:04 +04:00
|
|
|
if (IsTerminatedOrInterrupted()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
ParseAvailableData();
|
2009-09-25 21:11:02 +04:00
|
|
|
}
|
2009-09-21 17:18:20 +04:00
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsHtml5RequestStopper : public Runnable {
|
2018-03-16 18:26:06 +03:00
|
|
|
private:
|
|
|
|
nsHtml5StreamParserPtr mStreamParser;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit nsHtml5RequestStopper(nsHtml5StreamParser* aStreamParser)
|
|
|
|
: Runnable("nsHtml5RequestStopper"), mStreamParser(aStreamParser) {}
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
mozilla::MutexAutoLock autoLock(mStreamParser->mTokenizerMutex);
|
|
|
|
mStreamParser->DoStopRequest();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-09-25 21:11:02 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
nsresult nsHtml5StreamParser::OnStopRequest(nsIRequest* aRequest,
|
2018-03-16 18:26:06 +03:00
|
|
|
nsresult status) {
|
2009-09-25 21:11:02 +04:00
|
|
|
NS_ASSERTION(mRequest == aRequest, "Got Stop on wrong stream.");
|
2013-06-17 23:44:07 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2009-09-18 13:21:47 +04:00
|
|
|
if (mObserver) {
|
2019-02-28 02:41:31 +03:00
|
|
|
mObserver->OnStopRequest(aRequest, status);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2013-06-17 23:44:07 +04:00
|
|
|
nsCOMPtr<nsIRunnable> stopper = new nsHtml5RequestStopper(this);
|
2017-06-08 02:23:29 +03:00
|
|
|
if (NS_FAILED(mEventTarget->Dispatch(stopper, nsIThread::DISPATCH_NORMAL))) {
|
2013-06-17 23:44:07 +04:00
|
|
|
NS_WARNING("Dispatching StopRequest event failed.");
|
2009-09-25 21:11:02 +04:00
|
|
|
}
|
2009-09-18 13:21:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2018-12-04 11:02:04 +03:00
|
|
|
void nsHtml5StreamParser::DoDataAvailableBuffer(
|
|
|
|
mozilla::Buffer<uint8_t>&& aBuffer) {
|
2019-12-12 20:50:19 +03:00
|
|
|
if (MOZ_LIKELY(!mDecodingLocalFileWithoutTokenizing)) {
|
2018-12-04 11:02:04 +03:00
|
|
|
DoDataAvailable(aBuffer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CheckedInt<size_t> bufferedPlusLength(aBuffer.Length());
|
|
|
|
bufferedPlusLength += mLocalFileBytesBuffered;
|
|
|
|
if (!bufferedPlusLength.isValid()) {
|
|
|
|
MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Ensure that WriteStreamBytes() sees a buffer ending
|
|
|
|
// exactly at LOCAL_FILE_UTF_8_BUFFER_SIZE
|
|
|
|
// if we are about to cross the threshold. This way,
|
|
|
|
// Necko buffer boundaries don't affect user-visible
|
|
|
|
// behavior.
|
|
|
|
if (bufferedPlusLength.value() <= LOCAL_FILE_UTF_8_BUFFER_SIZE) {
|
|
|
|
// Truncation OK, because we just checked the range.
|
|
|
|
mLocalFileBytesBuffered = bufferedPlusLength.value();
|
|
|
|
mBufferedLocalFileData.AppendElement(std::move(aBuffer));
|
|
|
|
DoDataAvailable(mBufferedLocalFileData.LastElement());
|
|
|
|
} else {
|
|
|
|
// Truncation OK, because the constant is small enough.
|
2018-12-17 20:08:51 +03:00
|
|
|
size_t overBoundary =
|
|
|
|
bufferedPlusLength.value() - LOCAL_FILE_UTF_8_BUFFER_SIZE;
|
|
|
|
MOZ_RELEASE_ASSERT(overBoundary < aBuffer.Length());
|
|
|
|
size_t untilBoundary = aBuffer.Length() - overBoundary;
|
2018-12-04 11:02:04 +03:00
|
|
|
auto span = aBuffer.AsSpan();
|
2018-12-17 20:08:51 +03:00
|
|
|
auto head = span.To(untilBoundary);
|
|
|
|
auto tail = span.From(untilBoundary);
|
|
|
|
MOZ_RELEASE_ASSERT(mLocalFileBytesBuffered + untilBoundary ==
|
|
|
|
LOCAL_FILE_UTF_8_BUFFER_SIZE);
|
2018-12-04 11:02:04 +03:00
|
|
|
// We make a theoretically useless copy here, because avoiding
|
|
|
|
// the copy adds too much complexity.
|
|
|
|
Maybe<Buffer<uint8_t>> maybe = Buffer<uint8_t>::CopyFrom(head);
|
|
|
|
if (maybe.isNothing()) {
|
|
|
|
MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mLocalFileBytesBuffered = LOCAL_FILE_UTF_8_BUFFER_SIZE;
|
|
|
|
mBufferedLocalFileData.AppendElement(std::move(*maybe));
|
|
|
|
|
|
|
|
DoDataAvailable(head);
|
|
|
|
// Re-decode may have happened here.
|
|
|
|
DoDataAvailable(tail);
|
|
|
|
}
|
|
|
|
// Do this clean-up here to avoid use-after-free when
|
|
|
|
// DoDataAvailable is passed a span pointing into an
|
|
|
|
// element of mBufferedLocalFileData.
|
2019-12-12 20:50:19 +03:00
|
|
|
if (!mDecodingLocalFileWithoutTokenizing) {
|
2018-12-04 11:02:04 +03:00
|
|
|
mBufferedLocalFileData.Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:59:56 +03:00
|
|
|
void nsHtml5StreamParser::DoDataAvailable(Span<const uint8_t> aBuffer) {
|
2009-09-25 21:11:02 +04:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2017-10-06 10:42:21 +03:00
|
|
|
MOZ_RELEASE_ASSERT(STREAM_BEING_READ == mStreamState,
|
|
|
|
"DoDataAvailable called when stream not open.");
|
2009-10-12 17:08:04 +04:00
|
|
|
mTokenizerMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (IsTerminated()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 16:45:17 +04:00
|
|
|
nsresult rv;
|
2011-02-11 13:33:32 +03:00
|
|
|
if (HasDecoder()) {
|
2019-04-24 14:45:12 +03:00
|
|
|
if (mFeedChardet) {
|
2019-04-16 22:05:26 +03:00
|
|
|
FeedDetector(aBuffer, false);
|
2011-02-11 13:33:32 +03:00
|
|
|
}
|
2018-12-06 21:59:56 +03:00
|
|
|
rv = WriteStreamBytes(aBuffer);
|
2011-02-11 13:33:32 +03:00
|
|
|
} else {
|
2018-12-06 21:59:56 +03:00
|
|
|
rv = SniffStreamBytes(aBuffer);
|
2011-09-28 16:45:17 +04:00
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
2014-11-05 18:44:37 +03:00
|
|
|
MarkAsBroken(rv);
|
2011-09-28 16:45:17 +04:00
|
|
|
return;
|
2011-02-11 13:33:32 +03:00
|
|
|
}
|
2009-10-12 17:08:04 +04:00
|
|
|
|
|
|
|
if (IsTerminatedOrInterrupted()) {
|
|
|
|
return;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2009-10-12 17:08:04 +04:00
|
|
|
|
2019-12-12 20:50:19 +03:00
|
|
|
if (mDecodingLocalFileWithoutTokenizing) {
|
2018-12-04 11:02:04 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-12 17:08:04 +04:00
|
|
|
ParseAvailableData();
|
2010-04-16 14:52:06 +04:00
|
|
|
|
|
|
|
if (mFlushTimerArmed || mSpeculating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-26 09:45:31 +03:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock flushTimerLock(mFlushTimerMutex);
|
2017-06-12 22:34:10 +03:00
|
|
|
mFlushTimer->InitWithNamedFuncCallback(
|
|
|
|
nsHtml5StreamParser::TimerCallback, static_cast<void*>(this),
|
2018-03-27 03:59:48 +03:00
|
|
|
mFlushTimerEverFired ? StaticPrefs::html5_flushtimer_initialdelay()
|
|
|
|
: StaticPrefs::html5_flushtimer_subsequentdelay(),
|
2017-06-12 22:34:10 +03:00
|
|
|
nsITimer::TYPE_ONE_SHOT, "nsHtml5StreamParser::DoDataAvailable");
|
2017-01-26 09:45:31 +03:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mFlushTimerArmed = true;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsHtml5DataAvailable : public Runnable {
|
2018-03-16 18:26:06 +03:00
|
|
|
private:
|
|
|
|
nsHtml5StreamParserPtr mStreamParser;
|
2018-12-07 11:28:08 +03:00
|
|
|
Buffer<uint8_t> mData;
|
2018-03-16 18:26:06 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
nsHtml5DataAvailable(nsHtml5StreamParser* aStreamParser,
|
2018-12-07 11:28:08 +03:00
|
|
|
Buffer<uint8_t>&& aData)
|
2018-03-16 18:26:06 +03:00
|
|
|
: Runnable("nsHtml5DataAvailable"),
|
|
|
|
mStreamParser(aStreamParser),
|
2018-12-07 11:28:08 +03:00
|
|
|
mData(std::move(aData)) {}
|
2018-03-16 18:26:06 +03:00
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
mozilla::MutexAutoLock autoLock(mStreamParser->mTokenizerMutex);
|
2018-12-04 11:02:04 +03:00
|
|
|
mStreamParser->DoDataAvailableBuffer(std::move(mData));
|
2018-03-16 18:26:06 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-09-25 21:11:02 +04:00
|
|
|
};
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
nsresult nsHtml5StreamParser::OnDataAvailable(nsIRequest* aRequest,
|
2014-04-16 09:41:39 +04:00
|
|
|
nsIInputStream* aInStream,
|
|
|
|
uint64_t aSourceOffset,
|
|
|
|
uint32_t aLength) {
|
2012-03-21 16:39:25 +04:00
|
|
|
nsresult rv;
|
|
|
|
if (NS_FAILED(rv = mExecutor->IsBroken())) {
|
|
|
|
return rv;
|
2011-09-28 16:45:17 +04:00
|
|
|
}
|
|
|
|
|
2018-12-04 11:02:04 +03:00
|
|
|
MOZ_ASSERT(mRequest == aRequest, "Got data on wrong stream.");
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t totalRead;
|
2013-07-09 05:45:26 +04:00
|
|
|
// Main thread to parser thread dispatch requires copying to buffer first.
|
2018-12-04 11:02:04 +03:00
|
|
|
if (MOZ_UNLIKELY(NS_IsMainThread())) {
|
2018-12-07 11:28:08 +03:00
|
|
|
Maybe<Buffer<uint8_t>> maybe = Buffer<uint8_t>::Alloc(aLength);
|
|
|
|
if (maybe.isNothing()) {
|
2013-07-09 05:45:26 +04:00
|
|
|
return mExecutor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
2018-12-07 11:28:08 +03:00
|
|
|
Buffer<uint8_t> data(std::move(*maybe));
|
|
|
|
rv = aInStream->Read(reinterpret_cast<char*>(data.Elements()),
|
|
|
|
data.Length(), &totalRead);
|
2013-07-09 05:45:26 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-12-07 11:28:08 +03:00
|
|
|
MOZ_ASSERT(totalRead == aLength);
|
2013-07-09 05:45:26 +04:00
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
nsCOMPtr<nsIRunnable> dataAvailable =
|
2018-12-07 11:28:08 +03:00
|
|
|
new nsHtml5DataAvailable(this, std::move(data));
|
2018-03-16 18:26:06 +03:00
|
|
|
if (NS_FAILED(mEventTarget->Dispatch(dataAvailable,
|
|
|
|
nsIThread::DISPATCH_NORMAL))) {
|
2013-07-09 05:45:26 +04:00
|
|
|
NS_WARNING("Dispatching DataAvailable event failed.");
|
|
|
|
}
|
|
|
|
return rv;
|
2018-12-04 11:02:04 +03:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(IsParserThread(), "Wrong thread!");
|
|
|
|
mozilla::MutexAutoLock autoLock(mTokenizerMutex);
|
|
|
|
|
2019-12-12 20:50:19 +03:00
|
|
|
if (MOZ_UNLIKELY(mDecodingLocalFileWithoutTokenizing)) {
|
2018-12-04 11:02:04 +03:00
|
|
|
// It's a bit sad to potentially buffer the first 1024
|
|
|
|
// bytes in two places, but it's a lot simpler than trying
|
|
|
|
// to optitize out that copy. It only happens for local files
|
|
|
|
// and not for the http(s) content anyway.
|
|
|
|
Maybe<Buffer<uint8_t>> maybe = Buffer<uint8_t>::Alloc(aLength);
|
|
|
|
if (maybe.isNothing()) {
|
|
|
|
MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2013-07-09 05:45:26 +04:00
|
|
|
}
|
2018-12-04 11:02:04 +03:00
|
|
|
Buffer<uint8_t> data(std::move(*maybe));
|
|
|
|
rv = aInStream->Read(reinterpret_cast<char*>(data.Elements()),
|
|
|
|
data.Length(), &totalRead);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
MOZ_ASSERT(totalRead == aLength);
|
|
|
|
DoDataAvailableBuffer(std::move(data));
|
|
|
|
return rv;
|
2013-06-17 23:44:07 +04:00
|
|
|
}
|
2018-12-04 11:02:04 +03:00
|
|
|
// Read directly from response buffer.
|
|
|
|
rv = aInStream->ReadSegments(CopySegmentsToParser, this, aLength, &totalRead);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
MOZ_ASSERT(totalRead == aLength);
|
|
|
|
return rv;
|
2013-07-09 05:45:26 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:11:34 +03:00
|
|
|
/* static */
|
|
|
|
nsresult nsHtml5StreamParser::CopySegmentsToParser(
|
2018-03-16 18:26:06 +03:00
|
|
|
nsIInputStream* aInStream, void* aClosure, const char* aFromSegment,
|
|
|
|
uint32_t aToOffset, uint32_t aCount, uint32_t* aWriteCount) {
|
2013-07-09 05:45:26 +04:00
|
|
|
nsHtml5StreamParser* parser = static_cast<nsHtml5StreamParser*>(aClosure);
|
|
|
|
|
2020-08-07 10:49:47 +03:00
|
|
|
parser->DoDataAvailable(AsBytes(Span(aFromSegment, aCount)));
|
2013-07-09 05:45:26 +04:00
|
|
|
// Assume DoDataAvailable consumed all available bytes.
|
|
|
|
*aWriteCount = aCount;
|
|
|
|
return NS_OK;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
const Encoding* nsHtml5StreamParser::PreferredForInternalEncodingDecl(
|
|
|
|
const nsACString& aEncoding) {
|
2017-06-17 05:54:40 +03:00
|
|
|
const Encoding* newEncoding = Encoding::ForLabel(aEncoding);
|
|
|
|
if (!newEncoding) {
|
2012-11-08 03:04:22 +04:00
|
|
|
// the encoding name is bogus
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->MaybeComplainAboutCharset("EncMetaUnsupported", true,
|
|
|
|
mTokenizer->getLineNumber());
|
2017-06-18 14:37:50 +03:00
|
|
|
return nullptr;
|
2010-07-30 14:03:54 +04:00
|
|
|
}
|
|
|
|
|
2018-03-16 18:26:06 +03:00
|
|
|
if (newEncoding == UTF_16BE_ENCODING || newEncoding == UTF_16LE_ENCODING) {
|
|
|
|
mTreeBuilder->MaybeComplainAboutCharset("EncMetaUtf16", true,
|
|
|
|
mTokenizer->getLineNumber());
|
2017-06-17 05:54:40 +03:00
|
|
|
newEncoding = UTF_8_ENCODING;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2012-11-08 03:04:22 +04:00
|
|
|
|
2017-06-17 05:54:40 +03:00
|
|
|
if (newEncoding == X_USER_DEFINED_ENCODING) {
|
2014-01-02 11:18:19 +04:00
|
|
|
// WebKit/Blink hack for Indian and Armenian legacy sites
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->MaybeComplainAboutCharset("EncMetaUserDefined", true,
|
|
|
|
mTokenizer->getLineNumber());
|
2017-06-17 05:54:40 +03:00
|
|
|
newEncoding = WINDOWS_1252_ENCODING;
|
2014-01-02 11:18:19 +04:00
|
|
|
}
|
|
|
|
|
2017-06-18 14:37:50 +03:00
|
|
|
if (newEncoding == mEncoding) {
|
2012-03-24 15:34:42 +04:00
|
|
|
if (mCharsetSource < kCharsetFromMetaPrescan) {
|
|
|
|
if (mInitialEncodingWasFromParentFrame) {
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->MaybeComplainAboutCharset("EncLateMetaFrame", false,
|
|
|
|
mTokenizer->getLineNumber());
|
2012-03-24 15:34:42 +04:00
|
|
|
} else {
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->MaybeComplainAboutCharset("EncLateMeta", false,
|
|
|
|
mTokenizer->getLineNumber());
|
2012-03-24 15:34:42 +04:00
|
|
|
}
|
|
|
|
}
|
2009-09-18 13:21:47 +04:00
|
|
|
mCharsetSource = kCharsetFromMetaTag; // become confident
|
2020-06-15 18:32:21 +03:00
|
|
|
mTreeBuilder->SetDocumentCharset(mEncoding, mCharsetSource);
|
|
|
|
DontGuessEncoding(); // don't feed chardet when confident
|
2017-06-18 14:37:50 +03:00
|
|
|
return nullptr;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2012-09-18 18:46:30 +04:00
|
|
|
|
2017-06-18 14:37:50 +03:00
|
|
|
return newEncoding;
|
2011-11-01 15:33:11 +04:00
|
|
|
}
|
|
|
|
|
2017-03-20 15:45:15 +03:00
|
|
|
bool nsHtml5StreamParser::internalEncodingDeclaration(nsHtml5String aEncoding) {
|
2011-11-01 15:33:11 +04:00
|
|
|
// This code needs to stay in sync with
|
|
|
|
// nsHtml5MetaScanner::tryCharset. Unfortunately, the
|
|
|
|
// trickery with member fields there leads to some copy-paste reuse. :-(
|
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2018-03-16 18:26:06 +03:00
|
|
|
if (mCharsetSource >= kCharsetFromMetaTag) { // this threshold corresponds to
|
|
|
|
// "confident" in the HTML5 spec
|
2011-11-01 15:33:11 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-20 15:45:15 +03:00
|
|
|
nsString newEncoding16; // Not Auto, because using it to hold nsStringBuffer*
|
|
|
|
aEncoding.ToString(newEncoding16);
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString newEncoding;
|
2017-03-20 15:45:15 +03:00
|
|
|
CopyUTF16toUTF8(newEncoding16, newEncoding);
|
2011-11-01 15:33:11 +04:00
|
|
|
|
2017-06-18 14:37:50 +03:00
|
|
|
auto encoding = PreferredForInternalEncodingDecl(newEncoding);
|
|
|
|
if (!encoding) {
|
2011-11-01 15:33:11 +04:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-30 14:03:54 +04:00
|
|
|
|
2012-03-24 15:34:42 +04:00
|
|
|
if (mReparseForbidden) {
|
|
|
|
// This mReparseForbidden check happens after the call to
|
|
|
|
// PreferredForInternalEncodingDecl so that if that method calls
|
|
|
|
// MaybeComplainAboutCharset, its charset complaint wins over the one
|
|
|
|
// below.
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->MaybeComplainAboutCharset("EncLateMetaTooLate", true,
|
|
|
|
mTokenizer->getLineNumber());
|
2012-03-24 15:34:42 +04:00
|
|
|
return false; // not reparsing even if we wanted to
|
|
|
|
}
|
|
|
|
|
2011-07-28 14:45:29 +04:00
|
|
|
// Avoid having the chardet ask for another restart after this restart
|
|
|
|
// request.
|
2019-12-12 20:50:19 +03:00
|
|
|
DontGuessEncoding();
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->NeedsCharsetSwitchTo(WrapNotNull(encoding), kCharsetFromMetaTag,
|
|
|
|
mTokenizer->getLineNumber());
|
2010-04-16 14:52:06 +04:00
|
|
|
FlushTreeOpsAndDisarmTimer();
|
2009-10-21 16:12:50 +04:00
|
|
|
Interrupt();
|
2009-09-21 17:18:20 +04:00
|
|
|
// the tree op executor will cause the stream parser to terminate
|
2017-04-27 13:27:03 +03:00
|
|
|
// if the charset switch request is accepted or it'll uninterrupt
|
2011-07-28 14:45:29 +04:00
|
|
|
// if the request failed. Note that if the restart request fails,
|
|
|
|
// we don't bother trying to make chardet resume. Might as well
|
|
|
|
// assume that chardet-requested restarts would fail, too.
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
2010-04-16 14:52:06 +04:00
|
|
|
void nsHtml5StreamParser::FlushTreeOpsAndDisarmTimer() {
|
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
|
|
|
if (mFlushTimerArmed) {
|
|
|
|
// avoid calling Cancel if the flush timer isn't armed to avoid acquiring
|
|
|
|
// a mutex
|
2017-01-26 09:45:31 +03:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock flushTimerLock(mFlushTimerMutex);
|
|
|
|
mFlushTimer->Cancel();
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mFlushTimerArmed = false;
|
2010-04-16 14:52:06 +04:00
|
|
|
}
|
2010-07-30 14:15:38 +04:00
|
|
|
if (mMode == VIEW_SOURCE_HTML || mMode == VIEW_SOURCE_XML) {
|
|
|
|
mTokenizer->FlushViewSource();
|
|
|
|
}
|
2010-04-16 14:52:06 +04:00
|
|
|
mTreeBuilder->Flush();
|
2016-09-19 21:59:54 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(mExecutorFlusher);
|
2017-07-26 11:13:35 +03:00
|
|
|
if (NS_FAILED(DispatchToMain(runnable.forget()))) {
|
2010-04-16 14:52:06 +04:00
|
|
|
NS_WARNING("failed to dispatch executor flush event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-12 17:08:04 +04:00
|
|
|
void nsHtml5StreamParser::ParseAvailableData() {
|
2018-12-04 11:02:04 +03:00
|
|
|
MOZ_ASSERT(IsParserThread(), "Wrong thread!");
|
2009-10-12 17:08:04 +04:00
|
|
|
mTokenizerMutex.AssertCurrentThreadOwns();
|
2019-12-12 20:50:19 +03:00
|
|
|
MOZ_ASSERT(!mDecodingLocalFileWithoutTokenizing);
|
2009-10-12 17:08:04 +04:00
|
|
|
|
|
|
|
if (IsTerminatedOrInterrupted()) {
|
2009-09-18 13:21:47 +04:00
|
|
|
return;
|
|
|
|
}
|
2015-04-14 02:36:33 +03:00
|
|
|
|
|
|
|
if (mSpeculating && !IsSpeculationEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
for (;;) {
|
|
|
|
if (!mFirstBuffer->hasMore()) {
|
|
|
|
if (mFirstBuffer == mLastBuffer) {
|
2009-09-21 17:18:20 +04:00
|
|
|
switch (mStreamState) {
|
|
|
|
case STREAM_BEING_READ:
|
2009-10-12 17:08:04 +04:00
|
|
|
// never release the last buffer.
|
|
|
|
if (!mSpeculating) {
|
|
|
|
// reuse buffer space if not speculating
|
|
|
|
mFirstBuffer->setStart(0);
|
|
|
|
mFirstBuffer->setEnd(0);
|
|
|
|
}
|
2010-02-26 12:18:38 +03:00
|
|
|
mTreeBuilder->FlushLoads();
|
2017-06-27 20:38:30 +03:00
|
|
|
{
|
|
|
|
// Dispatch this runnable unconditionally, because the loads
|
|
|
|
// that need flushing may have been flushed earlier even if the
|
|
|
|
// flush right above here did nothing.
|
|
|
|
nsCOMPtr<nsIRunnable> runnable(mLoadFlusher);
|
2018-03-16 18:26:06 +03:00
|
|
|
if (NS_FAILED(DispatchToMain(runnable.forget()))) {
|
2017-06-27 20:38:30 +03:00
|
|
|
NS_WARNING("failed to dispatch load flush event");
|
|
|
|
}
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
2009-09-18 13:21:47 +04:00
|
|
|
return; // no more data for now but expecting more
|
2009-09-21 17:18:20 +04:00
|
|
|
case STREAM_ENDED:
|
2009-10-12 17:08:04 +04:00
|
|
|
if (mAtEOF) {
|
2010-04-09 18:11:56 +04:00
|
|
|
return;
|
2009-10-12 17:08:04 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
mAtEOF = true;
|
2012-03-24 15:34:42 +04:00
|
|
|
if (mCharsetSource < kCharsetFromMetaTag) {
|
|
|
|
if (mInitialEncodingWasFromParentFrame) {
|
|
|
|
// Unfortunately, this check doesn't take effect for
|
|
|
|
// cross-origin frames, so cross-origin ad frames that have
|
|
|
|
// no text and only an image or a Flash embed get the more
|
|
|
|
// severe message from the next if block. The message is
|
|
|
|
// technically accurate, though.
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->MaybeComplainAboutCharset("EncNoDeclarationFrame",
|
|
|
|
false, 0);
|
2012-03-24 15:34:42 +04:00
|
|
|
} else if (mMode == NORMAL) {
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->MaybeComplainAboutCharset("EncNoDeclaration",
|
|
|
|
true, 0);
|
2012-03-24 15:34:42 +04:00
|
|
|
} else if (mMode == PLAIN_TEXT) {
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->MaybeComplainAboutCharset("EncNoDeclarationPlain",
|
|
|
|
true, 0);
|
2012-03-24 15:34:42 +04:00
|
|
|
}
|
|
|
|
}
|
2015-08-25 18:05:45 +03:00
|
|
|
if (NS_SUCCEEDED(mTreeBuilder->IsBroken())) {
|
|
|
|
mTokenizer->eof();
|
2015-08-25 18:05:46 +03:00
|
|
|
nsresult rv;
|
|
|
|
if (NS_FAILED((rv = mTreeBuilder->IsBroken()))) {
|
|
|
|
MarkAsBroken(rv);
|
|
|
|
} else {
|
|
|
|
mTreeBuilder->StreamEnded();
|
|
|
|
if (mMode == VIEW_SOURCE_HTML || mMode == VIEW_SOURCE_XML) {
|
|
|
|
mTokenizer->EndViewSource();
|
|
|
|
}
|
2015-08-25 18:05:45 +03:00
|
|
|
}
|
2010-07-30 14:15:38 +04:00
|
|
|
}
|
2010-04-16 14:52:06 +04:00
|
|
|
FlushTreeOpsAndDisarmTimer();
|
2009-09-18 13:21:47 +04:00
|
|
|
return; // no more data and not expecting more
|
|
|
|
default:
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("It should be impossible to reach this.");
|
2009-09-18 13:21:47 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-04-09 18:11:56 +04:00
|
|
|
mFirstBuffer = mFirstBuffer->next;
|
|
|
|
continue;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// now we have a non-empty buffer
|
|
|
|
mFirstBuffer->adjust(mLastWasCR);
|
2011-10-17 18:59:28 +04:00
|
|
|
mLastWasCR = false;
|
2009-09-18 13:21:47 +04:00
|
|
|
if (mFirstBuffer->hasMore()) {
|
2015-08-25 18:05:45 +03:00
|
|
|
if (!mTokenizer->EnsureBufferSpace(mFirstBuffer->getLength())) {
|
|
|
|
MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-18 13:21:47 +04:00
|
|
|
mLastWasCR = mTokenizer->tokenizeBuffer(mFirstBuffer);
|
2015-08-25 18:05:46 +03:00
|
|
|
nsresult rv;
|
|
|
|
if (NS_FAILED((rv = mTreeBuilder->IsBroken()))) {
|
|
|
|
MarkAsBroken(rv);
|
|
|
|
return;
|
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
// At this point, internalEncodingDeclaration() may have called
|
2009-09-21 17:18:20 +04:00
|
|
|
// Terminate, but that never happens together with script.
|
|
|
|
// Can't assert that here, though, because it's possible that the main
|
|
|
|
// thread has called Terminate() while this thread was parsing.
|
2012-06-20 11:05:39 +04:00
|
|
|
if (mTreeBuilder->HasScript()) {
|
|
|
|
// HasScript() cannot return true if the tree builder is preventing
|
|
|
|
// script execution.
|
|
|
|
MOZ_ASSERT(mMode == NORMAL);
|
2009-10-12 17:08:04 +04:00
|
|
|
mozilla::MutexAutoLock speculationAutoLock(mSpeculationMutex);
|
2017-04-27 13:27:03 +03:00
|
|
|
nsHtml5Speculation* speculation = new nsHtml5Speculation(
|
2009-10-28 16:48:37 +03:00
|
|
|
mFirstBuffer, mFirstBuffer->getStart(), mTokenizer->getLineNumber(),
|
2009-10-12 17:08:04 +04:00
|
|
|
mTreeBuilder->newSnapshot());
|
2017-04-27 13:27:03 +03:00
|
|
|
mTreeBuilder->AddSnapshotToScript(speculation->GetSnapshot(),
|
2009-10-28 16:48:37 +03:00
|
|
|
speculation->GetStartLineNumber());
|
2010-04-16 14:52:06 +04:00
|
|
|
FlushTreeOpsAndDisarmTimer();
|
2009-10-12 17:08:04 +04:00
|
|
|
mTreeBuilder->SetOpSink(speculation);
|
|
|
|
mSpeculations.AppendElement(speculation); // adopts the pointer
|
2011-10-17 18:59:28 +04:00
|
|
|
mSpeculating = true;
|
2009-10-12 17:08:04 +04:00
|
|
|
}
|
|
|
|
if (IsTerminatedOrInterrupted()) {
|
|
|
|
return;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 17:18:20 +04:00
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsHtml5StreamParserContinuation : public Runnable {
|
2009-09-25 21:11:02 +04:00
|
|
|
private:
|
2017-06-27 20:38:30 +03:00
|
|
|
nsHtml5StreamParserPtr mStreamParser;
|
|
|
|
|
2009-09-25 21:11:02 +04:00
|
|
|
public:
|
2014-09-02 02:04:52 +04:00
|
|
|
explicit nsHtml5StreamParserContinuation(nsHtml5StreamParser* aStreamParser)
|
2017-05-10 02:19:37 +03:00
|
|
|
: Runnable("nsHtml5StreamParserContinuation"),
|
|
|
|
mStreamParser(aStreamParser) {}
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2009-10-12 17:08:04 +04:00
|
|
|
mozilla::MutexAutoLock autoLock(mStreamParser->mTokenizerMutex);
|
2010-04-16 14:52:06 +04:00
|
|
|
mStreamParser->Uninterrupt();
|
2009-10-12 17:08:04 +04:00
|
|
|
mStreamParser->ParseAvailableData();
|
2009-09-25 21:11:02 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
void nsHtml5StreamParser::ContinueAfterScripts(nsHtml5Tokenizer* aTokenizer,
|
2009-09-21 17:18:20 +04:00
|
|
|
nsHtml5TreeBuilder* aTreeBuilder,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aLastWasCR) {
|
2009-09-21 17:18:20 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-07-30 14:15:38 +04:00
|
|
|
NS_ASSERTION(!(mMode == VIEW_SOURCE_HTML || mMode == VIEW_SOURCE_XML),
|
2018-03-16 18:26:06 +03:00
|
|
|
"ContinueAfterScripts called in view source mode!");
|
2012-08-14 13:10:41 +04:00
|
|
|
if (NS_FAILED(mExecutor->IsBroken())) {
|
2011-09-28 16:45:17 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-03-16 18:26:06 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
mExecutor->AssertStageEmpty();
|
|
|
|
#endif
|
2011-09-29 10:19:26 +04:00
|
|
|
bool speculationFailed = false;
|
2009-10-12 17:08:04 +04:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock speculationAutoLock(mSpeculationMutex);
|
|
|
|
if (mSpeculations.IsEmpty()) {
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE(
|
|
|
|
"ContinueAfterScripts called without "
|
|
|
|
"speculations.");
|
2009-10-12 17:08:04 +04:00
|
|
|
return;
|
|
|
|
}
|
2018-06-18 08:43:11 +03:00
|
|
|
|
2020-02-28 23:49:06 +03:00
|
|
|
const auto& speculation = mSpeculations.ElementAt(0);
|
2017-04-27 13:27:03 +03:00
|
|
|
if (aLastWasCR || !aTokenizer->isInDataState() ||
|
2009-10-12 17:08:04 +04:00
|
|
|
!aTreeBuilder->snapshotMatches(speculation->GetSnapshot())) {
|
2011-10-17 18:59:28 +04:00
|
|
|
speculationFailed = true;
|
2009-10-12 17:08:04 +04:00
|
|
|
// We've got a failed speculation :-(
|
2015-04-14 02:36:33 +03:00
|
|
|
MaybeDisableFutureSpeculation();
|
2009-10-12 17:08:04 +04:00
|
|
|
Interrupt(); // Make the parser thread release the tokenizer mutex sooner
|
2018-03-16 18:26:06 +03:00
|
|
|
// now fall out of the speculationAutoLock into the tokenizerAutoLock
|
|
|
|
// block
|
2009-10-12 17:08:04 +04:00
|
|
|
} else {
|
|
|
|
// We've got a successful speculation!
|
|
|
|
if (mSpeculations.Length() > 1) {
|
2017-04-27 13:27:03 +03:00
|
|
|
// the first speculation isn't the current speculation, so there's
|
2009-10-12 17:08:04 +04:00
|
|
|
// no need to bother the parser thread.
|
|
|
|
speculation->FlushToSink(mExecutor);
|
2010-02-26 12:18:38 +03:00
|
|
|
NS_ASSERTION(!mExecutor->IsScriptExecuting(),
|
2018-03-16 18:26:06 +03:00
|
|
|
"ParseUntilBlocked() was supposed to ensure we don't come "
|
|
|
|
"here when scripts are executing.");
|
|
|
|
NS_ASSERTION(
|
|
|
|
mExecutor->IsInFlushLoop(),
|
|
|
|
"How are we here if "
|
2010-02-26 12:18:38 +03:00
|
|
|
"RunFlushLoop() didn't call ParseUntilBlocked() which is the "
|
|
|
|
"only caller of this method?");
|
2009-10-12 17:08:04 +04:00
|
|
|
mSpeculations.RemoveElementAt(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// else
|
|
|
|
Interrupt(); // Make the parser thread release the tokenizer mutex sooner
|
2017-04-27 13:27:03 +03:00
|
|
|
|
2009-10-12 17:08:04 +04:00
|
|
|
// now fall through
|
2017-04-27 13:27:03 +03:00
|
|
|
// the first speculation is the current speculation. Need to
|
|
|
|
// release the the speculation mutex and acquire the tokenizer
|
2009-10-12 17:08:04 +04:00
|
|
|
// mutex. (Just acquiring the other mutex here would deadlock)
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 17:18:20 +04:00
|
|
|
{
|
2009-10-12 17:08:04 +04:00
|
|
|
mozilla::MutexAutoLock tokenizerAutoLock(mTokenizerMutex);
|
2018-03-16 18:26:06 +03:00
|
|
|
#ifdef DEBUG
|
2010-04-09 18:11:56 +04:00
|
|
|
{
|
2018-03-16 18:26:06 +03:00
|
|
|
mAtomTable.SetPermittedLookupEventTarget(
|
|
|
|
GetMainThreadSerialEventTarget());
|
2010-04-09 18:11:56 +04:00
|
|
|
}
|
2018-03-16 18:26:06 +03:00
|
|
|
#endif
|
2009-10-12 17:08:04 +04:00
|
|
|
// In principle, the speculation mutex should be acquired here,
|
|
|
|
// but there's no point, because the parser thread only acquires it
|
|
|
|
// when it has also acquired the tokenizer mutex and we are already
|
|
|
|
// holding the tokenizer mutex.
|
|
|
|
if (speculationFailed) {
|
|
|
|
// Rewind the stream
|
2011-10-17 18:59:28 +04:00
|
|
|
mAtEOF = false;
|
2020-02-28 23:49:06 +03:00
|
|
|
const auto& speculation = mSpeculations.ElementAt(0);
|
2009-10-12 17:08:04 +04:00
|
|
|
mFirstBuffer = speculation->GetBuffer();
|
|
|
|
mFirstBuffer->setStart(speculation->GetStart());
|
2009-10-28 16:48:37 +03:00
|
|
|
mTokenizer->setLineNumber(speculation->GetStartLineNumber());
|
2010-06-23 12:33:43 +04:00
|
|
|
|
2011-12-15 18:47:03 +04:00
|
|
|
nsContentUtils::ReportToConsole(
|
2013-08-21 23:28:26 +04:00
|
|
|
nsIScriptError::warningFlag, "DOM Events"_ns,
|
2011-12-15 18:47:03 +04:00
|
|
|
mExecutor->GetDocument(), nsContentUtils::eDOM_PROPERTIES,
|
2020-09-23 18:17:15 +03:00
|
|
|
"SpeculationFailed", nsTArray<nsString>(), nullptr, u""_ns,
|
2011-12-15 18:47:03 +04:00
|
|
|
speculation->GetStartLineNumber());
|
2010-06-23 12:33:43 +04:00
|
|
|
|
2011-09-28 16:45:17 +04:00
|
|
|
nsHtml5OwningUTF16Buffer* buffer = mFirstBuffer->next;
|
2009-10-12 17:08:04 +04:00
|
|
|
while (buffer) {
|
|
|
|
buffer->setStart(0);
|
|
|
|
buffer = buffer->next;
|
|
|
|
}
|
2017-04-27 13:27:03 +03:00
|
|
|
|
|
|
|
mSpeculations.Clear(); // potentially a huge number of destructors
|
2009-10-12 17:08:04 +04:00
|
|
|
// run here synchronously on the main thread...
|
2009-10-30 14:47:17 +03:00
|
|
|
|
|
|
|
mTreeBuilder->flushCharacters(); // empty the pending buffer
|
2018-03-16 18:26:06 +03:00
|
|
|
mTreeBuilder->ClearOps(); // now get rid of the failed ops
|
2009-10-30 14:47:17 +03:00
|
|
|
|
2009-10-12 17:08:04 +04:00
|
|
|
mTreeBuilder->SetOpSink(mExecutor->GetStage());
|
|
|
|
mExecutor->StartReadingFromStage();
|
2011-10-17 18:59:28 +04:00
|
|
|
mSpeculating = false;
|
2010-04-16 14:52:06 +04:00
|
|
|
|
2009-10-12 17:08:04 +04:00
|
|
|
// Copy state over
|
|
|
|
mLastWasCR = aLastWasCR;
|
|
|
|
mTokenizer->loadState(aTokenizer);
|
2018-11-12 17:37:09 +03:00
|
|
|
mTreeBuilder->loadState(aTreeBuilder);
|
2017-04-27 13:27:03 +03:00
|
|
|
} else {
|
2009-10-12 17:08:04 +04:00
|
|
|
// We've got a successful speculation and at least a moment ago it was
|
|
|
|
// the current speculation
|
|
|
|
mSpeculations.ElementAt(0)->FlushToSink(mExecutor);
|
2010-02-26 12:18:38 +03:00
|
|
|
NS_ASSERTION(!mExecutor->IsScriptExecuting(),
|
2018-03-16 18:26:06 +03:00
|
|
|
"ParseUntilBlocked() was supposed to ensure we don't come "
|
|
|
|
"here when scripts are executing.");
|
|
|
|
NS_ASSERTION(
|
|
|
|
mExecutor->IsInFlushLoop(),
|
|
|
|
"How are we here if "
|
2010-02-26 12:18:38 +03:00
|
|
|
"RunFlushLoop() didn't call ParseUntilBlocked() which is the "
|
|
|
|
"only caller of this method?");
|
2009-10-12 17:08:04 +04:00
|
|
|
mSpeculations.RemoveElementAt(0);
|
|
|
|
if (mSpeculations.IsEmpty()) {
|
|
|
|
// yes, it was still the only speculation. Now stop speculating
|
2011-05-19 12:42:01 +04:00
|
|
|
// However, before telling the executor to read from stage, flush
|
|
|
|
// any pending ops straight to the executor, because otherwise
|
|
|
|
// they remain unflushed until we get more data from the network.
|
|
|
|
mTreeBuilder->SetOpSink(mExecutor);
|
2011-10-17 18:59:28 +04:00
|
|
|
mTreeBuilder->Flush(true);
|
2009-10-12 17:08:04 +04:00
|
|
|
mTreeBuilder->SetOpSink(mExecutor->GetStage());
|
|
|
|
mExecutor->StartReadingFromStage();
|
2011-10-17 18:59:28 +04:00
|
|
|
mSpeculating = false;
|
2009-10-12 17:08:04 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRunnable> event = new nsHtml5StreamParserContinuation(this);
|
2017-06-08 02:23:29 +03:00
|
|
|
if (NS_FAILED(mEventTarget->Dispatch(event, nsIThread::DISPATCH_NORMAL))) {
|
2010-04-16 14:52:06 +04:00
|
|
|
NS_WARNING("Failed to dispatch nsHtml5StreamParserContinuation");
|
2009-10-12 17:08:04 +04:00
|
|
|
}
|
2018-03-16 18:26:06 +03:00
|
|
|
// A stream event might run before this event runs, but that's harmless.
|
|
|
|
#ifdef DEBUG
|
|
|
|
mAtomTable.SetPermittedLookupEventTarget(mEventTarget);
|
|
|
|
#endif
|
2009-09-21 17:18:20 +04:00
|
|
|
}
|
|
|
|
}
|
2009-10-21 16:12:50 +04:00
|
|
|
|
|
|
|
void nsHtml5StreamParser::ContinueAfterFailedCharsetSwitch() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
nsCOMPtr<nsIRunnable> event = new nsHtml5StreamParserContinuation(this);
|
2017-06-08 02:23:29 +03:00
|
|
|
if (NS_FAILED(mEventTarget->Dispatch(event, nsIThread::DISPATCH_NORMAL))) {
|
2009-11-17 11:52:30 +03:00
|
|
|
NS_WARNING("Failed to dispatch nsHtml5StreamParserContinuation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class nsHtml5TimerKungFu : public Runnable {
|
2010-04-16 14:52:06 +04:00
|
|
|
private:
|
2017-06-27 20:38:30 +03:00
|
|
|
nsHtml5StreamParserPtr mStreamParser;
|
|
|
|
|
2010-04-16 14:52:06 +04:00
|
|
|
public:
|
2014-09-02 02:04:52 +04:00
|
|
|
explicit nsHtml5TimerKungFu(nsHtml5StreamParser* aStreamParser)
|
2017-05-10 02:19:37 +03:00
|
|
|
: Runnable("nsHtml5TimerKungFu"), mStreamParser(aStreamParser) {}
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2017-01-26 09:45:31 +03:00
|
|
|
mozilla::MutexAutoLock flushTimerLock(mStreamParser->mFlushTimerMutex);
|
2010-04-16 14:52:06 +04:00
|
|
|
if (mStreamParser->mFlushTimer) {
|
|
|
|
mStreamParser->mFlushTimer->Cancel();
|
2012-07-30 18:20:58 +04:00
|
|
|
mStreamParser->mFlushTimer = nullptr;
|
2010-04-16 14:52:06 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void nsHtml5StreamParser::DropTimer() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
/*
|
|
|
|
* Simply nulling out the timer wouldn't work, because if the timer is
|
|
|
|
* armed, it needs to be canceled first. Simply canceling it first wouldn't
|
|
|
|
* work, because nsTimerImpl::Cancel is not safe for calling from outside
|
|
|
|
* the thread where nsTimerImpl::Fire would run. It's not safe to
|
|
|
|
* dispatch a runnable to cancel the timer from the destructor of this
|
|
|
|
* class, because the timer has a weak (void*) pointer back to this instance
|
|
|
|
* of the stream parser and having the timer fire before the runnable
|
|
|
|
* cancels it would make the timer access a deleted object.
|
|
|
|
*
|
|
|
|
* This DropTimer method addresses these issues. This method must be called
|
|
|
|
* on the main thread before the destructor of this class is reached.
|
2018-03-16 18:26:06 +03:00
|
|
|
* The nsHtml5TimerKungFu object has an nsHtml5StreamParserPtr that addrefs
|
|
|
|
* this
|
2010-04-16 14:52:06 +04:00
|
|
|
* stream parser object to keep it alive until the runnable is done.
|
|
|
|
* The runnable cancels the timer on the parser thread, drops the timer
|
2017-06-27 20:38:30 +03:00
|
|
|
* and lets nsHtml5StreamParserPtr send a runnable back to the main thread to
|
2010-04-16 14:52:06 +04:00
|
|
|
* release the stream parser.
|
|
|
|
*/
|
2017-01-26 09:45:31 +03:00
|
|
|
mozilla::MutexAutoLock flushTimerLock(mFlushTimerMutex);
|
2010-04-16 14:52:06 +04:00
|
|
|
if (mFlushTimer) {
|
|
|
|
nsCOMPtr<nsIRunnable> event = new nsHtml5TimerKungFu(this);
|
2017-06-08 02:23:29 +03:00
|
|
|
if (NS_FAILED(mEventTarget->Dispatch(event, nsIThread::DISPATCH_NORMAL))) {
|
2010-04-16 14:52:06 +04:00
|
|
|
NS_WARNING("Failed to dispatch TimerKungFu event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 13:27:03 +03:00
|
|
|
// Using a static, because the method name Notify is taken by the chardet
|
2009-11-17 11:52:30 +03:00
|
|
|
// callback.
|
|
|
|
void nsHtml5StreamParser::TimerCallback(nsITimer* aTimer, void* aClosure) {
|
2018-03-16 18:26:06 +03:00
|
|
|
(static_cast<nsHtml5StreamParser*>(aClosure))->TimerFlush();
|
2009-11-17 11:52:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsHtml5StreamParser::TimerFlush() {
|
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2010-04-16 14:52:06 +04:00
|
|
|
mozilla::MutexAutoLock autoLock(mTokenizerMutex);
|
|
|
|
|
|
|
|
NS_ASSERTION(!mSpeculating, "Flush timer fired while speculating.");
|
|
|
|
|
|
|
|
// The timer fired if we got here. No need to cancel it. Mark it as
|
|
|
|
// not armed, though.
|
2011-10-17 18:59:28 +04:00
|
|
|
mFlushTimerArmed = false;
|
2009-11-17 11:52:30 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mFlushTimerEverFired = true;
|
2010-04-16 14:52:06 +04:00
|
|
|
|
|
|
|
if (IsTerminatedOrInterrupted()) {
|
2009-11-17 11:52:30 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
if (mMode == VIEW_SOURCE_HTML || mMode == VIEW_SOURCE_XML) {
|
|
|
|
mTreeBuilder->Flush(); // delete useless ops
|
|
|
|
if (mTokenizer->FlushViewSource()) {
|
2017-06-27 20:38:30 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(mExecutorFlusher);
|
2018-03-16 18:26:06 +03:00
|
|
|
if (NS_FAILED(DispatchToMain(runnable.forget()))) {
|
2017-06-27 20:38:30 +03:00
|
|
|
NS_WARNING("failed to dispatch executor flush event");
|
|
|
|
}
|
|
|
|
}
|
2010-07-30 14:15:38 +04:00
|
|
|
} else {
|
|
|
|
// we aren't speculating and we don't know when new data is
|
|
|
|
// going to arrive. Send data to the main thread.
|
|
|
|
if (mTreeBuilder->Flush(true)) {
|
2017-06-27 20:38:30 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(mExecutorFlusher);
|
2018-03-16 18:26:06 +03:00
|
|
|
if (NS_FAILED(DispatchToMain(runnable.forget()))) {
|
2010-07-30 14:15:38 +04:00
|
|
|
NS_WARNING("failed to dispatch executor flush event");
|
|
|
|
}
|
2009-11-17 11:52:30 +03:00
|
|
|
}
|
|
|
|
}
|
2009-10-21 16:12:50 +04:00
|
|
|
}
|
2011-09-28 16:45:17 +04:00
|
|
|
|
2014-11-05 18:44:37 +03:00
|
|
|
void nsHtml5StreamParser::MarkAsBroken(nsresult aRv) {
|
2011-09-28 16:45:17 +04:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
|
|
|
mTokenizerMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
Terminate();
|
2014-11-05 18:44:37 +03:00
|
|
|
mTreeBuilder->MarkAsBroken(aRv);
|
2011-10-17 18:59:28 +04:00
|
|
|
mozilla::DebugOnly<bool> hadOps = mTreeBuilder->Flush(false);
|
2011-09-28 16:45:17 +04:00
|
|
|
NS_ASSERTION(hadOps, "Should have had the markAsBroken op!");
|
2017-06-27 20:38:30 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(mExecutorFlusher);
|
2017-07-26 11:13:35 +03:00
|
|
|
if (NS_FAILED(DispatchToMain(runnable.forget()))) {
|
2011-09-28 16:45:17 +04:00
|
|
|
NS_WARNING("failed to dispatch executor flush event");
|
|
|
|
}
|
|
|
|
}
|
2017-06-27 20:38:30 +03:00
|
|
|
|
2017-07-26 11:13:35 +03:00
|
|
|
nsresult nsHtml5StreamParser::DispatchToMain(
|
|
|
|
already_AddRefed<nsIRunnable>&& aRunnable) {
|
2021-03-18 22:24:50 +03:00
|
|
|
if (mNetworkEventTarget) {
|
|
|
|
return mNetworkEventTarget->Dispatch(std::move(aRunnable));
|
2017-06-27 20:38:30 +03:00
|
|
|
}
|
2018-03-16 18:26:06 +03:00
|
|
|
return SchedulerGroup::UnlabeledDispatch(TaskCategory::Network,
|
2018-05-30 22:15:35 +03:00
|
|
|
std::move(aRunnable));
|
2017-06-27 20:38:30 +03:00
|
|
|
}
|