2009-09-18 02:21:47 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=2 et tw=79: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
|
|
|
* Henri Sivonen <hsivonen@iki.fi>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsHtml5StreamParser.h"
|
|
|
|
#include "nsICharsetConverterManager.h"
|
2012-03-04 19:57:51 -08:00
|
|
|
#include "nsCharsetAlias.h"
|
2009-09-18 02:21:47 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsEncoderDecoderUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsHtml5Tokenizer.h"
|
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsHtml5Parser.h"
|
2009-09-21 06:18:20 -07:00
|
|
|
#include "nsHtml5TreeBuilder.h"
|
|
|
|
#include "nsHtml5AtomTable.h"
|
2009-09-25 10:11:02 -07:00
|
|
|
#include "nsHtml5Module.h"
|
|
|
|
#include "nsHtml5RefPtr.h"
|
2010-06-23 01:33:43 -07:00
|
|
|
#include "nsIScriptError.h"
|
2011-05-28 00:03:00 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-07-30 03:15:38 -07:00
|
|
|
#include "nsHtml5Highlighter.h"
|
2011-11-01 04:33:11 -07:00
|
|
|
#include "expat_config.h"
|
|
|
|
#include "expat.h"
|
2011-11-30 09:44:31 -08:00
|
|
|
#include "nsINestedURI.h"
|
2011-05-28 00:03:00 -07:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2009-09-18 02:21:47 -07:00
|
|
|
|
|
|
|
|
2010-04-29 04:22:27 -07:00
|
|
|
PRInt32 nsHtml5StreamParser::sTimerInitialDelay = 120;
|
|
|
|
PRInt32 nsHtml5StreamParser::sTimerSubsequentDelay = 120;
|
2009-11-17 00:52:30 -08:00
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
nsHtml5StreamParser::InitializeStatics()
|
|
|
|
{
|
2011-05-28 16:42:57 -07:00
|
|
|
Preferences::AddIntVarCache(&sTimerInitialDelay,
|
|
|
|
"html5.flushtimer.initialdelay");
|
|
|
|
Preferences::AddIntVarCache(&sTimerSubsequentDelay,
|
|
|
|
"html5.flushtimer.subsequentdelay");
|
2009-11-17 00:52:30 -08:00
|
|
|
}
|
|
|
|
|
2010-04-16 03:52:06 -07: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
|
|
|
|
* nsHtml5RefPtr. Upon creation, nsHtml5RefPtr addrefs the object it holds
|
|
|
|
* just like a regular nsRefPtr. This is OK, since the creation of the
|
|
|
|
* runnable and the nsHtml5RefPtr happens on the main thread.
|
|
|
|
*
|
|
|
|
* When the runnable is done on the parser thread, the destructor of
|
|
|
|
* nsHtml5RefPtr runs there. It doesn't call Release on the held object
|
|
|
|
* 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 02:21:47 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsHtml5StreamParser)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsHtml5StreamParser)
|
|
|
|
|
|
|
|
NS_INTERFACE_TABLE_HEAD(nsHtml5StreamParser)
|
|
|
|
NS_INTERFACE_TABLE2(nsHtml5StreamParser,
|
|
|
|
nsIStreamListener,
|
|
|
|
nsICharsetDetectionObserver)
|
|
|
|
NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(nsHtml5StreamParser)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsHtml5StreamParser)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsHtml5StreamParser)
|
2010-04-16 03:52:06 -07:00
|
|
|
tmp->DropTimer();
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mObserver)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mRequest)
|
2009-10-15 04:29:11 -07:00
|
|
|
tmp->mOwner = nsnull;
|
2009-09-25 10:11:02 -07:00
|
|
|
tmp->mExecutorFlusher = nsnull;
|
2010-02-26 01:18:38 -08:00
|
|
|
tmp->mLoadFlusher = nsnull;
|
2009-09-25 10:11:02 -07:00
|
|
|
tmp->mExecutor = nsnull;
|
2010-01-12 05:15:10 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mChardet)
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsHtml5StreamParser)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mObserver)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mRequest)
|
2009-10-15 04:29:11 -07:00
|
|
|
if (tmp->mOwner) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mOwner");
|
|
|
|
cb.NoteXPCOMChild(static_cast<nsIParser*> (tmp->mOwner));
|
|
|
|
}
|
2009-09-25 10:11:02 -07:00
|
|
|
// hack: count the strongly owned edge wrapped in the runnable
|
|
|
|
if (tmp->mExecutorFlusher) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mExecutorFlusher->mExecutor");
|
|
|
|
cb.NoteXPCOMChild(static_cast<nsIContentSink*> (tmp->mExecutor));
|
|
|
|
}
|
2010-02-26 01:18:38 -08:00
|
|
|
// hack: count the strongly owned edge wrapped in the runnable
|
|
|
|
if (tmp->mLoadFlusher) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mLoadFlusher->mExecutor");
|
|
|
|
cb.NoteXPCOMChild(static_cast<nsIContentSink*> (tmp->mExecutor));
|
|
|
|
}
|
2010-01-12 05:15:10 -08:00
|
|
|
// hack: count self if held by mChardet
|
|
|
|
if (tmp->mChardet) {
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb,
|
|
|
|
"mChardet->mObserver");
|
|
|
|
cb.NoteXPCOMChild(static_cast<nsIStreamListener*>(tmp));
|
|
|
|
}
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
class nsHtml5ExecutorFlusher : public nsRunnable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsHtml5TreeOpExecutor> mExecutor;
|
|
|
|
public:
|
|
|
|
nsHtml5ExecutorFlusher(nsHtml5TreeOpExecutor* aExecutor)
|
|
|
|
: mExecutor(aExecutor)
|
|
|
|
{}
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
2010-02-26 01:18:38 -08:00
|
|
|
mExecutor->RunFlushLoop();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class nsHtml5LoadFlusher : public nsRunnable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsHtml5TreeOpExecutor> mExecutor;
|
|
|
|
public:
|
|
|
|
nsHtml5LoadFlusher(nsHtml5TreeOpExecutor* aExecutor)
|
|
|
|
: mExecutor(aExecutor)
|
|
|
|
{}
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
mExecutor->FlushSpeculativeLoads();
|
2009-09-25 10:11:02 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
2009-09-18 02:21:47 -07:00
|
|
|
|
2009-09-21 06:18:20 -07:00
|
|
|
nsHtml5StreamParser::nsHtml5StreamParser(nsHtml5TreeOpExecutor* aExecutor,
|
2010-07-30 03:15:38 -07:00
|
|
|
nsHtml5Parser* aOwner,
|
|
|
|
eParserMode aMode)
|
2011-09-28 05:45:17 -07:00
|
|
|
: mFirstBuffer(nsnull) // Will be filled when starting
|
|
|
|
, mLastBuffer(nsnull) // Will be filled when starting
|
2009-09-18 02:21:47 -07:00
|
|
|
, mExecutor(aExecutor)
|
2010-07-30 03:15:38 -07:00
|
|
|
, mTreeBuilder(new nsHtml5TreeBuilder((aMode == VIEW_SOURCE_HTML ||
|
|
|
|
aMode == VIEW_SOURCE_XML) ?
|
|
|
|
nsnull : mExecutor->GetStage(),
|
|
|
|
aMode == NORMAL ?
|
|
|
|
mExecutor->GetStage() : nsnull))
|
2011-11-01 04:33:11 -07:00
|
|
|
, mTokenizer(new nsHtml5Tokenizer(mTreeBuilder, aMode == VIEW_SOURCE_XML))
|
2009-09-21 06:18:20 -07:00
|
|
|
, mTokenizerMutex("nsHtml5StreamParser mTokenizerMutex")
|
2009-09-18 02:21:47 -07:00
|
|
|
, mOwner(aOwner)
|
2009-10-12 06:08:04 -07:00
|
|
|
, mSpeculationMutex("nsHtml5StreamParser mSpeculationMutex")
|
2009-09-21 06:18:20 -07:00
|
|
|
, mTerminatedMutex("nsHtml5StreamParser mTerminatedMutex")
|
2009-09-25 10:11:02 -07:00
|
|
|
, mThread(nsHtml5Module::GetStreamParserThread())
|
|
|
|
, mExecutorFlusher(new nsHtml5ExecutorFlusher(aExecutor))
|
2010-02-26 01:18:38 -08:00
|
|
|
, mLoadFlusher(new nsHtml5LoadFlusher(aExecutor))
|
2009-11-17 00:52:30 -08:00
|
|
|
, mFlushTimer(do_CreateInstance("@mozilla.org/timer;1"))
|
2010-07-30 03:15:38 -07:00
|
|
|
, mMode(aMode)
|
2009-09-18 02:21:47 -07:00
|
|
|
{
|
2009-09-21 06:18:20 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-04-16 03:52:06 -07:00
|
|
|
mFlushTimer->SetTarget(mThread);
|
2009-09-21 06:18:20 -07:00
|
|
|
mAtomTable.Init(); // we aren't checking for OOM anyway...
|
2010-04-16 03:52:06 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
mAtomTable.SetPermittedLookupThread(mThread);
|
|
|
|
#endif
|
2009-09-21 06:18:20 -07:00
|
|
|
mTokenizer->setInterner(&mAtomTable);
|
|
|
|
mTokenizer->setEncodingDeclarationHandler(this);
|
2010-01-12 05:15:10 -08:00
|
|
|
|
2010-07-30 03:15:38 -07:00
|
|
|
if (aMode == VIEW_SOURCE_HTML || aMode == VIEW_SOURCE_XML) {
|
2011-11-01 04:33:11 -07:00
|
|
|
nsHtml5Highlighter* highlighter =
|
|
|
|
new nsHtml5Highlighter(mExecutor->GetStage());
|
|
|
|
mTokenizer->EnableViewSource(highlighter); // takes ownership
|
|
|
|
mTreeBuilder->EnableViewSource(highlighter); // doesn't own
|
2010-07-30 03:15:38 -07:00
|
|
|
}
|
|
|
|
|
2010-01-12 05:15:10 -08:00
|
|
|
// Chardet instantiation adapted from nsDOMFile.
|
|
|
|
// Chardet is initialized here even if it turns out to be useless
|
|
|
|
// to make the chardet refcount its observer (nsHtml5StreamParser)
|
|
|
|
// on the main thread.
|
2011-05-28 00:03:00 -07:00
|
|
|
const nsAdoptingCString& detectorName =
|
|
|
|
Preferences::GetLocalizedCString("intl.charset.detector");
|
2010-01-12 05:15:10 -08:00
|
|
|
if (!detectorName.IsEmpty()) {
|
|
|
|
nsCAutoString detectorContractID;
|
|
|
|
detectorContractID.AssignLiteral(NS_CHARSET_DETECTOR_CONTRACTID_BASE);
|
2011-05-28 00:03:00 -07:00
|
|
|
detectorContractID += detectorName;
|
2011-02-11 02:33:32 -08:00
|
|
|
if ((mChardet = do_CreateInstance(detectorContractID.get()))) {
|
2010-01-12 05:15:10 -08:00
|
|
|
(void) mChardet->Init(this);
|
2011-10-17 07:59:28 -07:00
|
|
|
mFeedChardet = true;
|
2010-01-12 05:15:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-21 06:18:20 -07:00
|
|
|
// There's a zeroing operator new for everything else
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHtml5StreamParser::~nsHtml5StreamParser()
|
|
|
|
{
|
2009-09-21 06:18:20 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
mTokenizer->end();
|
2010-04-16 03:52:06 -07:00
|
|
|
NS_ASSERTION(!mFlushTimer, "Flush timer was not dropped before dtor!");
|
|
|
|
#ifdef DEBUG
|
2009-09-18 02:21:47 -07:00
|
|
|
mRequest = nsnull;
|
|
|
|
mObserver = nsnull;
|
|
|
|
mUnicodeDecoder = nsnull;
|
|
|
|
mSniffingBuffer = nsnull;
|
|
|
|
mMetaScanner = nsnull;
|
2009-10-12 06:08:04 -07:00
|
|
|
mFirstBuffer = nsnull;
|
2009-09-18 02:21:47 -07:00
|
|
|
mExecutor = nsnull;
|
|
|
|
mTreeBuilder = nsnull;
|
|
|
|
mTokenizer = nsnull;
|
|
|
|
mOwner = nsnull;
|
2010-04-16 03:52:06 -07:00
|
|
|
#endif
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHtml5StreamParser::GetChannel(nsIChannel** aChannel)
|
|
|
|
{
|
2009-09-21 06:18:20 -07:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2009-09-18 02:21:47 -07:00
|
|
|
return mRequest ? CallQueryInterface(mRequest, aChannel) :
|
|
|
|
NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHtml5StreamParser::Notify(const char* aCharset, nsDetectionConfident aConf)
|
|
|
|
{
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2009-09-18 02:21:47 -07:00
|
|
|
if (aConf == eBestAnswer || aConf == eSureAnswer) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mFeedChardet = false; // just in case
|
2011-02-11 02:33:32 -08:00
|
|
|
if (HasDecoder()) {
|
|
|
|
if (mCharset.Equals(aCharset)) {
|
|
|
|
NS_ASSERTION(mCharsetSource < kCharsetFromAutoDetection,
|
|
|
|
"Why are we running chardet at all?");
|
|
|
|
mCharsetSource = kCharsetFromAutoDetection;
|
|
|
|
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
|
|
|
|
} else {
|
|
|
|
// We've already committed to a decoder. Request a reload from the
|
|
|
|
// docshell.
|
|
|
|
nsCAutoString charset(aCharset);
|
|
|
|
mTreeBuilder->NeedsCharsetSwitchTo(charset, kCharsetFromAutoDetection);
|
|
|
|
FlushTreeOpsAndDisarmTimer();
|
|
|
|
Interrupt();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Got a confident answer from the sniffing buffer. That code will
|
|
|
|
// take care of setting up the decoder.
|
|
|
|
mCharset.Assign(aCharset);
|
|
|
|
mCharsetSource = kCharsetFromAutoDetection;
|
|
|
|
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
|
|
|
|
}
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-11-30 09:44:31 -08:00
|
|
|
void
|
|
|
|
nsHtml5StreamParser::SetViewSourceTitle(nsIURI* aURL)
|
|
|
|
{
|
|
|
|
if (aURL) {
|
|
|
|
nsCOMPtr<nsIURI> temp;
|
|
|
|
bool isViewSource;
|
|
|
|
aURL->SchemeIs("view-source", &isViewSource);
|
|
|
|
if (isViewSource) {
|
|
|
|
nsCOMPtr<nsINestedURI> nested = do_QueryInterface(aURL);
|
|
|
|
nested->GetInnerURI(getter_AddRefs(temp));
|
|
|
|
} else {
|
|
|
|
temp = aURL;
|
|
|
|
}
|
|
|
|
bool isData;
|
|
|
|
temp->SchemeIs("data", &isData);
|
|
|
|
if (isData) {
|
|
|
|
// Avoid showing potentially huge data: URLs. The three last bytes are
|
|
|
|
// UTF-8 for an ellipsis.
|
|
|
|
mViewSourceTitle.AssignLiteral("data:\xE2\x80\xA6");
|
|
|
|
} else {
|
|
|
|
temp->GetSpec(mViewSourceTitle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-18 02:21:47 -07:00
|
|
|
nsresult
|
|
|
|
nsHtml5StreamParser::SetupDecodingAndWriteSniffingBufferAndCurrentSegment(const PRUint8* aFromSegment, // can be null
|
|
|
|
PRUint32 aCount,
|
|
|
|
PRUint32* aWriteCount)
|
|
|
|
{
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2009-09-18 02:21:47 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> convManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = convManager->GetUnicodeDecoder(mCharset.get(), getter_AddRefs(mUnicodeDecoder));
|
|
|
|
if (rv == NS_ERROR_UCONV_NOCONV) {
|
2010-04-09 07:11:56 -07:00
|
|
|
mCharset.AssignLiteral("windows-1252"); // lower case is the raw form
|
2009-09-18 02:21:47 -07:00
|
|
|
mCharsetSource = kCharsetFromWeakDocTypeDefault;
|
|
|
|
rv = convManager->GetUnicodeDecoderRaw(mCharset.get(), getter_AddRefs(mUnicodeDecoder));
|
2010-03-09 04:39:32 -08:00
|
|
|
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mUnicodeDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Recover);
|
|
|
|
return WriteSniffingBufferAndCurrentSegment(aFromSegment, aCount, aWriteCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHtml5StreamParser::WriteSniffingBufferAndCurrentSegment(const PRUint8* aFromSegment, // can be null
|
|
|
|
PRUint32 aCount,
|
|
|
|
PRUint32* aWriteCount)
|
|
|
|
{
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2009-09-18 02:21:47 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mSniffingBuffer) {
|
|
|
|
PRUint32 writeCount;
|
|
|
|
rv = WriteStreamBytes(mSniffingBuffer, mSniffingLength, &writeCount);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mSniffingBuffer = nsnull;
|
|
|
|
}
|
|
|
|
mMetaScanner = nsnull;
|
|
|
|
if (aFromSegment) {
|
|
|
|
rv = WriteStreamBytes(aFromSegment, aCount, aWriteCount);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHtml5StreamParser::SetupDecodingFromBom(const char* aCharsetName, const char* aDecoderCharsetName)
|
|
|
|
{
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2009-09-18 02:21:47 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> convManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = convManager->GetUnicodeDecoderRaw(aDecoderCharsetName, getter_AddRefs(mUnicodeDecoder));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-04-09 07:11:56 -07:00
|
|
|
mUnicodeDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Recover);
|
2009-09-18 02:21:47 -07:00
|
|
|
mCharset.Assign(aCharsetName);
|
|
|
|
mCharsetSource = kCharsetFromByteOrderMark;
|
2011-10-17 07:59:28 -07:00
|
|
|
mFeedChardet = false;
|
2010-03-09 04:39:32 -08:00
|
|
|
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
|
2009-09-18 02:21:47 -07:00
|
|
|
mSniffingBuffer = nsnull;
|
|
|
|
mMetaScanner = nsnull;
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-02-15 22:40:35 -08:00
|
|
|
void
|
|
|
|
nsHtml5StreamParser::SniffBOMlessUTF16BasicLatin(const PRUint8* aFromSegment,
|
|
|
|
PRUint32 aCountToSniffingLimit)
|
|
|
|
{
|
2011-11-15 23:38:51 -08:00
|
|
|
// Avoid underspecified heuristic craziness for XHR
|
|
|
|
if (mMode == LOAD_AS_DATA) {
|
|
|
|
return;
|
|
|
|
}
|
2011-02-15 22:40:35 -08:00
|
|
|
// Make sure there's enough data. Require room for "<title></title>"
|
|
|
|
if (mSniffingLength + aCountToSniffingLimit < 30) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// even-numbered bytes tracked at 0, odd-numbered bytes tracked at 1
|
2011-09-28 23:19:26 -07:00
|
|
|
bool byteZero[2] = { false, false };
|
|
|
|
bool byteNonZero[2] = { false, false };
|
2011-02-15 22:40:35 -08:00
|
|
|
PRUint32 i = 0;
|
|
|
|
if (mSniffingBuffer) {
|
|
|
|
for (; i < mSniffingLength; ++i) {
|
|
|
|
if (mSniffingBuffer[i]) {
|
|
|
|
if (byteNonZero[1 - (i % 2)]) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
byteNonZero[i % 2] = true;
|
2011-03-02 22:52:47 -08:00
|
|
|
} else {
|
|
|
|
if (byteZero[1 - (i % 2)]) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
byteZero[i % 2] = true;
|
2011-02-15 22:40:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aFromSegment) {
|
|
|
|
for (PRUint32 j = 0; j < aCountToSniffingLimit; ++j) {
|
|
|
|
if (aFromSegment[j]) {
|
|
|
|
if (byteNonZero[1 - ((i + j) % 2)]) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
byteNonZero[(i + j) % 2] = true;
|
2011-03-02 22:52:47 -08:00
|
|
|
} else {
|
|
|
|
if (byteZero[1 - ((i + j) % 2)]) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
byteZero[(i + j) % 2] = true;
|
2011-02-15 22:40:35 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (byteNonZero[0]) {
|
|
|
|
mCharset.Assign("UTF-16LE");
|
|
|
|
} else {
|
|
|
|
mCharset.Assign("UTF-16BE");
|
|
|
|
}
|
|
|
|
mCharsetSource = kCharsetFromIrreversibleAutoDetection;
|
|
|
|
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
|
2011-10-17 07:59:28 -07:00
|
|
|
mFeedChardet = false;
|
2011-02-15 22:40:35 -08:00
|
|
|
}
|
|
|
|
|
2011-11-01 04:33:11 -07:00
|
|
|
void
|
2011-11-03 07:31:27 -07:00
|
|
|
nsHtml5StreamParser::SetEncodingFromExpat(const PRUnichar* aEncoding)
|
2011-11-01 04:33:11 -07:00
|
|
|
{
|
2011-11-03 07:31:27 -07:00
|
|
|
if (aEncoding) {
|
|
|
|
nsDependentString utf16(aEncoding);
|
|
|
|
nsCAutoString utf8;
|
|
|
|
CopyUTF16toUTF8(utf16, utf8);
|
|
|
|
if (PreferredForInternalEncodingDecl(utf8)) {
|
|
|
|
mCharset.Assign(utf8);
|
|
|
|
mCharsetSource = kCharsetFromMetaTag; // closest for XML
|
|
|
|
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 04:33:11 -07:00
|
|
|
}
|
2011-11-03 07:31:27 -07:00
|
|
|
mCharset.AssignLiteral("UTF-8"); // XML defaults to UTF-8 without a BOM
|
|
|
|
mCharsetSource = kCharsetFromMetaTag; // means confident
|
2011-11-01 04:33:11 -07: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.
|
|
|
|
struct UserData {
|
|
|
|
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 07:31:27 -07:00
|
|
|
ud->mStreamParser->SetEncodingFromExpat(
|
2011-11-01 04:33:11 -07:00
|
|
|
reinterpret_cast<const PRUnichar*>(aEncoding));
|
|
|
|
XML_StopParser(ud->mExpat, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
HandleStartElement(void* aUserData,
|
|
|
|
const XML_Char* aName,
|
|
|
|
const XML_Char **aAtts)
|
|
|
|
{
|
|
|
|
UserData* ud = static_cast<UserData*>(aUserData);
|
|
|
|
XML_StopParser(ud->mExpat, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
HandleEndElement(void* aUserData,
|
|
|
|
const XML_Char* aName)
|
|
|
|
{
|
|
|
|
UserData* ud = static_cast<UserData*>(aUserData);
|
|
|
|
XML_StopParser(ud->mExpat, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
HandleComment(void* aUserData,
|
|
|
|
const XML_Char* aName)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-09-18 02:21:47 -07:00
|
|
|
nsresult
|
|
|
|
nsHtml5StreamParser::FinalizeSniffing(const PRUint8* aFromSegment, // can be null
|
|
|
|
PRUint32 aCount,
|
|
|
|
PRUint32* aWriteCount,
|
|
|
|
PRUint32 aCountToSniffingLimit)
|
|
|
|
{
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2011-11-01 04:33:11 -07:00
|
|
|
NS_ASSERTION(mCharsetSource < kCharsetFromMetaTag,
|
|
|
|
"Should not finalize sniffing when already confident.");
|
|
|
|
if (mMode == VIEW_SOURCE_XML) {
|
|
|
|
static const XML_Memory_Handling_Suite memsuite =
|
|
|
|
{
|
|
|
|
(void *(*)(size_t))moz_xmalloc,
|
|
|
|
(void *(*)(void *, size_t))moz_xrealloc,
|
|
|
|
moz_free
|
|
|
|
};
|
|
|
|
|
|
|
|
static const PRUnichar kExpatSeparator[] = { 0xFFFF, '\0' };
|
|
|
|
|
|
|
|
static const PRUnichar kISO88591[] =
|
|
|
|
{ 'I', 'S', 'O', '-', '8', '8', '5', '9', '-', '1', '\0' };
|
|
|
|
|
|
|
|
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 04:33:11 -07: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 04:33:11 -07:00
|
|
|
if (mSniffingBuffer) {
|
|
|
|
status = XML_Parse(ud.mExpat,
|
|
|
|
reinterpret_cast<const char*>(mSniffingBuffer.get()),
|
|
|
|
mSniffingLength,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
if (status == XML_STATUS_OK &&
|
|
|
|
mCharsetSource < kCharsetFromMetaTag &&
|
|
|
|
aFromSegment) {
|
|
|
|
status = XML_Parse(ud.mExpat,
|
|
|
|
reinterpret_cast<const char*>(aFromSegment),
|
|
|
|
aCountToSniffingLimit,
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
mCharset.AssignLiteral("UTF-8");
|
|
|
|
mCharsetSource = kCharsetFromMetaTag; // means confident
|
|
|
|
}
|
|
|
|
|
|
|
|
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(aFromSegment,
|
|
|
|
aCount,
|
|
|
|
aWriteCount);
|
|
|
|
}
|
|
|
|
|
2009-09-18 02:21:47 -07:00
|
|
|
// meta scan failed.
|
|
|
|
if (mCharsetSource >= kCharsetFromHintPrevDoc) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mFeedChardet = false;
|
2009-09-18 02:21:47 -07:00
|
|
|
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(aFromSegment, aCount, aWriteCount);
|
|
|
|
}
|
2011-02-15 22:40:35 -08:00
|
|
|
// Check for BOMless UTF-16 with Basic
|
|
|
|
// Latin content for compat with IE. See bug 631751.
|
|
|
|
SniffBOMlessUTF16BasicLatin(aFromSegment, aCountToSniffingLimit);
|
|
|
|
// the charset may have been set now
|
2010-01-12 05:15:10 -08:00
|
|
|
// maybe try chardet now;
|
2011-02-11 02:33:32 -08:00
|
|
|
if (mFeedChardet) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool dontFeed;
|
2010-01-12 05:15:10 -08:00
|
|
|
nsresult rv;
|
|
|
|
if (mSniffingBuffer) {
|
|
|
|
rv = mChardet->DoIt((const char*)mSniffingBuffer.get(), mSniffingLength, &dontFeed);
|
2011-02-11 02:33:32 -08:00
|
|
|
mFeedChardet = !dontFeed;
|
2009-09-18 02:21:47 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-01-12 05:15:10 -08:00
|
|
|
}
|
2011-02-11 02:33:32 -08:00
|
|
|
if (mFeedChardet && aFromSegment) {
|
|
|
|
rv = mChardet->DoIt((const char*)aFromSegment,
|
|
|
|
// Avoid buffer boundary-dependent behavior when
|
|
|
|
// reparsing is forbidden. If reparse is forbidden,
|
|
|
|
// act as if we only saw the first 1024 bytes.
|
|
|
|
// When reparsing isn't forbidden, buffer boundaries
|
|
|
|
// can have an effect on whether the page is loaded
|
|
|
|
// once or twice. :-(
|
|
|
|
mReparseForbidden ? aCountToSniffingLimit : aCount,
|
|
|
|
&dontFeed);
|
|
|
|
mFeedChardet = !dontFeed;
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
if (mFeedChardet && (!aFromSegment || mReparseForbidden)) {
|
|
|
|
// mReparseForbidden is checked so that we get to use the sniffing
|
|
|
|
// buffer with the best guess so far if we aren't allowed to guess
|
|
|
|
// better later.
|
2011-10-17 07:59:28 -07:00
|
|
|
mFeedChardet = false;
|
2011-02-11 02:33:32 -08:00
|
|
|
rv = mChardet->Done();
|
2009-09-18 02:21:47 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2010-01-12 05:15:10 -08:00
|
|
|
// fall thru; callback may have changed charset
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
if (mCharsetSource == kCharsetUninitialized) {
|
|
|
|
// Hopefully this case is never needed, but dealing with it anyway
|
2010-04-09 07:11:56 -07:00
|
|
|
mCharset.AssignLiteral("windows-1252");
|
2009-09-18 02:21:47 -07:00
|
|
|
mCharsetSource = kCharsetFromWeakDocTypeDefault;
|
2010-03-09 04:39:32 -08:00
|
|
|
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
|
2011-11-15 23:38:51 -08:00
|
|
|
} else if (mMode == LOAD_AS_DATA &&
|
|
|
|
mCharsetSource == kCharsetFromWeakDocTypeDefault) {
|
|
|
|
NS_ASSERTION(mReparseForbidden, "Reparse should be forbidden for XHR");
|
|
|
|
NS_ASSERTION(!mFeedChardet, "Should not feed chardet for XHR");
|
|
|
|
NS_ASSERTION(mCharset.EqualsLiteral("UTF-8"),
|
|
|
|
"XHR should default to UTF-8");
|
|
|
|
// Now mark charset source as non-weak to signal that we have a decision
|
|
|
|
mCharsetSource = kCharsetFromDocTypeDefault;
|
|
|
|
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
return SetupDecodingAndWriteSniffingBufferAndCurrentSegment(aFromSegment, aCount, aWriteCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHtml5StreamParser::SniffStreamBytes(const PRUint8* aFromSegment,
|
|
|
|
PRUint32 aCount,
|
|
|
|
PRUint32* aWriteCount)
|
|
|
|
{
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2009-09-18 02:21:47 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRUint32 writeCount;
|
2010-04-09 07:11:56 -07:00
|
|
|
for (PRUint32 i = 0; i < aCount && mBomState != BOM_SNIFFING_OVER; i++) {
|
2009-09-18 02:21:47 -07:00
|
|
|
switch (mBomState) {
|
|
|
|
case BOM_SNIFFING_NOT_STARTED:
|
|
|
|
NS_ASSERTION(i == 0, "Bad BOM sniffing state.");
|
|
|
|
switch (*aFromSegment) {
|
|
|
|
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;
|
|
|
|
default:
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_LE_FIRST_BYTE:
|
|
|
|
if (aFromSegment[i] == 0xFE) {
|
|
|
|
rv = SetupDecodingFromBom("UTF-16", "UTF-16LE"); // upper case is the raw form
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
PRUint32 count = aCount - (i + 1);
|
|
|
|
rv = WriteStreamBytes(aFromSegment + (i + 1), count, &writeCount);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
*aWriteCount = writeCount + (i + 1);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
break;
|
|
|
|
case SEEN_UTF_16_BE_FIRST_BYTE:
|
|
|
|
if (aFromSegment[i] == 0xFF) {
|
|
|
|
rv = SetupDecodingFromBom("UTF-16", "UTF-16BE"); // upper case is the raw form
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
PRUint32 count = aCount - (i + 1);
|
|
|
|
rv = WriteStreamBytes(aFromSegment + (i + 1), count, &writeCount);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
*aWriteCount = writeCount + (i + 1);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
rv = SetupDecodingFromBom("UTF-8", "UTF-8"); // upper case is the raw form
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
PRUint32 count = aCount - (i + 1);
|
|
|
|
rv = WriteStreamBytes(aFromSegment + (i + 1), count, &writeCount);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
*aWriteCount = writeCount + (i + 1);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
break;
|
|
|
|
default:
|
2010-04-09 07:11:56 -07:00
|
|
|
mBomState = BOM_SNIFFING_OVER;
|
|
|
|
break;
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// if we get here, there either was no BOM or the BOM sniffing isn't complete yet
|
|
|
|
|
2011-11-15 23:38:51 -08:00
|
|
|
if (!mMetaScanner && (mMode == NORMAL ||
|
|
|
|
mMode == VIEW_SOURCE_HTML ||
|
|
|
|
mMode == LOAD_AS_DATA)) {
|
2009-09-18 02:21:47 -07:00
|
|
|
mMetaScanner = new nsHtml5MetaScanner();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSniffingLength + aCount >= NS_HTML5_STREAM_PARSER_SNIFFING_BUFFER_SIZE) {
|
|
|
|
// this is the last buffer
|
2011-11-01 04:33:11 -07:00
|
|
|
PRUint32 countToSniffingLimit =
|
|
|
|
NS_HTML5_STREAM_PARSER_SNIFFING_BUFFER_SIZE - mSniffingLength;
|
2011-11-15 23:38:51 -08:00
|
|
|
if (mMode == NORMAL || mMode == VIEW_SOURCE_HTML || mMode == LOAD_AS_DATA) {
|
2011-11-01 04:33:11 -07:00
|
|
|
nsHtml5ByteReadable readable(aFromSegment, aFromSegment +
|
|
|
|
countToSniffingLimit);
|
2011-11-01 04:33:11 -07:00
|
|
|
mMetaScanner->sniff(&readable, getter_AddRefs(mUnicodeDecoder), mCharset);
|
|
|
|
if (mUnicodeDecoder) {
|
|
|
|
mUnicodeDecoder->SetInputErrorBehavior(
|
|
|
|
nsIUnicodeDecoder::kOnError_Recover);
|
|
|
|
// meta scan successful
|
|
|
|
mCharsetSource = kCharsetFromMetaPrescan;
|
|
|
|
mFeedChardet = false;
|
|
|
|
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
|
|
|
|
mMetaScanner = nsnull;
|
|
|
|
return WriteSniffingBufferAndCurrentSegment(aFromSegment, aCount,
|
|
|
|
aWriteCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FinalizeSniffing(aFromSegment, aCount, aWriteCount,
|
|
|
|
countToSniffingLimit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// not the last buffer
|
2011-11-15 23:38:51 -08:00
|
|
|
if (mMode == NORMAL || mMode == VIEW_SOURCE_HTML || mMode == LOAD_AS_DATA) {
|
2011-11-01 04:33:11 -07:00
|
|
|
nsHtml5ByteReadable readable(aFromSegment, aFromSegment + aCount);
|
2009-09-18 02:21:47 -07:00
|
|
|
mMetaScanner->sniff(&readable, getter_AddRefs(mUnicodeDecoder), mCharset);
|
|
|
|
if (mUnicodeDecoder) {
|
|
|
|
// meta scan successful
|
2011-11-01 04:33:11 -07:00
|
|
|
mUnicodeDecoder->SetInputErrorBehavior(
|
|
|
|
nsIUnicodeDecoder::kOnError_Recover);
|
2009-09-18 02:21:47 -07:00
|
|
|
mCharsetSource = kCharsetFromMetaPrescan;
|
2011-10-17 07:59:28 -07:00
|
|
|
mFeedChardet = false;
|
2010-03-09 04:39:32 -08:00
|
|
|
mTreeBuilder->SetDocumentCharset(mCharset, mCharsetSource);
|
2009-09-18 02:21:47 -07:00
|
|
|
mMetaScanner = nsnull;
|
2011-11-01 04:33:11 -07:00
|
|
|
return WriteSniffingBufferAndCurrentSegment(aFromSegment,
|
|
|
|
aCount,
|
|
|
|
aWriteCount);
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mSniffingBuffer) {
|
2011-09-28 05:45:17 -07:00
|
|
|
const mozilla::fallible_t fallible = mozilla::fallible_t();
|
|
|
|
mSniffingBuffer = new (fallible)
|
|
|
|
PRUint8[NS_HTML5_STREAM_PARSER_SNIFFING_BUFFER_SIZE];
|
|
|
|
if (!mSniffingBuffer) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
memcpy(mSniffingBuffer + mSniffingLength, aFromSegment, aCount);
|
|
|
|
mSniffingLength += aCount;
|
|
|
|
*aWriteCount = aCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHtml5StreamParser::WriteStreamBytes(const PRUint8* aFromSegment,
|
|
|
|
PRUint32 aCount,
|
|
|
|
PRUint32* aWriteCount)
|
|
|
|
{
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2011-09-28 05:45:17 -07:00
|
|
|
// mLastBuffer always points to a buffer of the size
|
|
|
|
// NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE.
|
2009-09-18 02:21:47 -07:00
|
|
|
if (mLastBuffer->getEnd() == NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE) {
|
2011-09-28 05:45:17 -07:00
|
|
|
nsRefPtr<nsHtml5OwningUTF16Buffer> newBuf =
|
|
|
|
nsHtml5OwningUTF16Buffer::FalliblyCreate(
|
|
|
|
NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE);
|
|
|
|
if (!newBuf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mLastBuffer = (mLastBuffer->next = newBuf.forget());
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
2010-07-26 12:11:09 -07:00
|
|
|
PRInt32 totalByteCount = 0;
|
2009-09-18 02:21:47 -07:00
|
|
|
for (;;) {
|
|
|
|
PRInt32 end = mLastBuffer->getEnd();
|
|
|
|
PRInt32 byteCount = aCount - totalByteCount;
|
|
|
|
PRInt32 utf16Count = NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE - end;
|
|
|
|
|
|
|
|
NS_ASSERTION(utf16Count, "Trying to convert into a buffer with no free space!");
|
2011-02-18 03:54:23 -08:00
|
|
|
// byteCount may be zero to force the decoder to output a pending surrogate
|
|
|
|
// pair.
|
2009-09-18 02:21:47 -07:00
|
|
|
|
|
|
|
nsresult convResult = mUnicodeDecoder->Convert((const char*)aFromSegment, &byteCount, mLastBuffer->getBuffer() + end, &utf16Count);
|
|
|
|
|
|
|
|
end += utf16Count;
|
|
|
|
mLastBuffer->setEnd(end);
|
|
|
|
totalByteCount += byteCount;
|
|
|
|
aFromSegment += byteCount;
|
|
|
|
|
2010-07-26 12:11:09 -07:00
|
|
|
NS_ASSERTION(end <= NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE,
|
|
|
|
"The Unicode decoder wrote too much data.");
|
|
|
|
NS_ASSERTION(byteCount >= -1, "The decoder consumed fewer than -1 bytes.");
|
2009-09-18 02:21:47 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(convResult)) {
|
2010-07-26 12:11:09 -07:00
|
|
|
// Using the more generic NS_FAILED test above in case there are still
|
|
|
|
// decoders around that don't use NS_ERROR_ILLEGAL_INPUT properly.
|
|
|
|
NS_ASSERTION(convResult == NS_ERROR_ILLEGAL_INPUT,
|
|
|
|
"The decoder signaled an error other than NS_ERROR_ILLEGAL_INPUT.");
|
|
|
|
|
2010-04-09 07:11:56 -07:00
|
|
|
// There's an illegal byte in the input. It's now the responsibility
|
|
|
|
// of this calling code to output a U+FFFD REPLACEMENT CHARACTER and
|
|
|
|
// reset the decoder.
|
|
|
|
|
2010-07-26 12:11:09 -07:00
|
|
|
if (totalByteCount < (PRInt32)aCount) {
|
2010-04-09 07:11:56 -07:00
|
|
|
// advance over the bad byte
|
2009-09-18 02:21:47 -07:00
|
|
|
++totalByteCount;
|
|
|
|
++aFromSegment;
|
2010-07-26 12:11:09 -07:00
|
|
|
} else {
|
|
|
|
NS_NOTREACHED("The decoder signaled an error but consumed all input.");
|
|
|
|
// Recovering from this situation in case there are still broken
|
|
|
|
// decoders, since nsScanner had recovery code, too.
|
|
|
|
totalByteCount = (PRInt32)aCount;
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
2010-04-09 07:11:56 -07:00
|
|
|
|
|
|
|
// Emit the REPLACEMENT CHARACTER
|
2012-03-14 04:19:15 -07:00
|
|
|
if (end >= NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE) {
|
|
|
|
nsRefPtr<nsHtml5OwningUTF16Buffer> newBuf =
|
|
|
|
nsHtml5OwningUTF16Buffer::FalliblyCreate(
|
|
|
|
NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE);
|
|
|
|
if (!newBuf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mLastBuffer = (mLastBuffer->next = newBuf.forget());
|
|
|
|
end = 0;
|
|
|
|
}
|
2009-09-18 02:21:47 -07:00
|
|
|
mLastBuffer->getBuffer()[end] = 0xFFFD;
|
|
|
|
++end;
|
|
|
|
mLastBuffer->setEnd(end);
|
2012-03-14 04:19:15 -07:00
|
|
|
if (end >= NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE) {
|
2011-09-28 05:45:17 -07:00
|
|
|
nsRefPtr<nsHtml5OwningUTF16Buffer> newBuf =
|
|
|
|
nsHtml5OwningUTF16Buffer::FalliblyCreate(
|
|
|
|
NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE);
|
|
|
|
if (!newBuf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mLastBuffer = (mLastBuffer->next = newBuf.forget());
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
2010-04-09 07:11:56 -07:00
|
|
|
|
2009-09-18 02:21:47 -07:00
|
|
|
mUnicodeDecoder->Reset();
|
2010-07-26 12:11:09 -07:00
|
|
|
if (totalByteCount == (PRInt32)aCount) {
|
|
|
|
*aWriteCount = (PRUint32)totalByteCount;
|
2009-09-18 02:21:47 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
} else if (convResult == NS_PARTIAL_MORE_OUTPUT) {
|
2011-09-28 05:45:17 -07:00
|
|
|
nsRefPtr<nsHtml5OwningUTF16Buffer> newBuf =
|
|
|
|
nsHtml5OwningUTF16Buffer::FalliblyCreate(
|
|
|
|
NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE);
|
|
|
|
if (!newBuf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
mLastBuffer = (mLastBuffer->next = newBuf.forget());
|
2011-02-18 03:54:23 -08:00
|
|
|
// All input may have been consumed if there is a pending surrogate pair
|
|
|
|
// that doesn't fit in the output buffer. Loop back to push a zero-length
|
|
|
|
// input to the decoder in that case.
|
2009-09-18 02:21:47 -07:00
|
|
|
} else {
|
2010-07-26 12:11:09 -07:00
|
|
|
NS_ASSERTION(totalByteCount == (PRInt32)aCount,
|
|
|
|
"The Unicode decoder consumed the wrong number of bytes.");
|
|
|
|
*aWriteCount = (PRUint32)totalByteCount;
|
2009-09-18 02:21:47 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIRequestObserver methods:
|
|
|
|
nsresult
|
|
|
|
nsHtml5StreamParser::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
|
|
|
|
{
|
2009-09-21 06:18:20 -07:00
|
|
|
NS_PRECONDITION(STREAM_NOT_STARTED == mStreamState,
|
|
|
|
"Got OnStartRequest when the stream had already started.");
|
2009-10-12 06:08:04 -07:00
|
|
|
NS_PRECONDITION(!mExecutor->HasStarted(),
|
2009-09-21 06:18:20 -07:00
|
|
|
"Got OnStartRequest at the wrong stage in the executor life cycle.");
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2009-09-18 02:21:47 -07:00
|
|
|
if (mObserver) {
|
|
|
|
mObserver->OnStartRequest(aRequest, aContext);
|
|
|
|
}
|
|
|
|
mRequest = aRequest;
|
|
|
|
|
2009-09-21 06:18:20 -07:00
|
|
|
mStreamState = STREAM_BEING_READ;
|
|
|
|
|
2010-07-30 03:15:38 -07:00
|
|
|
if (mMode == VIEW_SOURCE_HTML || mMode == VIEW_SOURCE_XML) {
|
2011-11-30 09:44:31 -08:00
|
|
|
mTokenizer->StartViewSource(NS_ConvertUTF8toUTF16(mViewSourceTitle));
|
2010-07-30 03:15:38 -07:00
|
|
|
}
|
2011-11-30 09:44:31 -08:00
|
|
|
|
2010-07-30 03:15:38 -07:00
|
|
|
// For View Source, the parser should run with scripts "enabled" if a normal
|
|
|
|
// load would have scripts enabled.
|
2011-11-15 23:38:51 -08:00
|
|
|
bool scriptingEnabled = mMode == LOAD_AS_DATA ?
|
|
|
|
false : mExecutor->IsScriptEnabled();
|
2009-09-21 06:18:20 -07:00
|
|
|
mOwner->StartTokenizer(scriptingEnabled);
|
|
|
|
mTreeBuilder->setScriptingEnabled(scriptingEnabled);
|
|
|
|
mTokenizer->start();
|
|
|
|
mExecutor->Start();
|
|
|
|
mExecutor->StartReadingFromStage();
|
2011-11-01 08:27:36 -07:00
|
|
|
|
|
|
|
if (mMode == PLAIN_TEXT) {
|
|
|
|
mTreeBuilder->StartPlainText();
|
|
|
|
mTokenizer->StartPlainText();
|
2011-11-30 09:44:31 -08:00
|
|
|
} else if (mMode == VIEW_SOURCE_PLAIN) {
|
|
|
|
mTreeBuilder->StartPlainTextViewSource(NS_ConvertUTF8toUTF16(mViewSourceTitle));
|
|
|
|
mTokenizer->StartPlainText();
|
2011-11-01 08:27:36 -07:00
|
|
|
}
|
|
|
|
|
2009-09-18 02:21:47 -07:00
|
|
|
/*
|
|
|
|
* If you move the following line, be very careful not to cause
|
|
|
|
* WillBuildModel to be called before the document has had its
|
|
|
|
* script global object set.
|
|
|
|
*/
|
2009-09-21 06:18:20 -07:00
|
|
|
mExecutor->WillBuildModel(eDTDMode_unknown);
|
|
|
|
|
2011-09-28 05:45:17 -07:00
|
|
|
nsRefPtr<nsHtml5OwningUTF16Buffer> newBuf =
|
|
|
|
nsHtml5OwningUTF16Buffer::FalliblyCreate(
|
|
|
|
NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE);
|
|
|
|
if (!newBuf) {
|
2012-03-21 05:39:25 -07: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 05:45:17 -07:00
|
|
|
}
|
|
|
|
NS_ASSERTION(!mFirstBuffer, "How come we have the first buffer set?");
|
|
|
|
NS_ASSERTION(!mLastBuffer, "How come we have the last buffer set?");
|
|
|
|
mFirstBuffer = mLastBuffer = newBuf;
|
|
|
|
|
2009-09-21 06:18:20 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2010-07-30 03:15:38 -07: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 08:27:36 -07:00
|
|
|
mReparseForbidden = !(mMode == NORMAL || mMode == PLAIN_TEXT);
|
2010-07-30 03:15:38 -07:00
|
|
|
|
2009-09-21 06:18:20 -07:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(mRequest, &rv));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCAutoString method;
|
|
|
|
httpChannel->GetRequestMethod(method);
|
|
|
|
// 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 07:59:28 -07:00
|
|
|
mReparseForbidden = true;
|
|
|
|
mFeedChardet = false; // can't restart anyway
|
2009-09-21 06:18:20 -07:00
|
|
|
}
|
|
|
|
}
|
2011-02-11 02:33:32 -08:00
|
|
|
|
|
|
|
if (mCharsetSource >= kCharsetFromAutoDetection) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mFeedChardet = false;
|
2011-02-11 02:33:32 -08:00
|
|
|
}
|
2009-09-18 02:21:47 -07:00
|
|
|
|
2010-12-08 04:37:19 -08:00
|
|
|
if (mCharsetSource <= kCharsetFromMetaPrescan) {
|
2009-09-18 02:21:47 -07:00
|
|
|
// we aren't ready to commit to an encoding yet
|
|
|
|
// leave converter uninstantiated for now
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> convManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = convManager->GetUnicodeDecoder(mCharset.get(), getter_AddRefs(mUnicodeDecoder));
|
2010-11-14 06:40:14 -08:00
|
|
|
// if we failed to get a decoder, there will be fallback, so don't propagate
|
|
|
|
// the error.
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mUnicodeDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Recover);
|
|
|
|
} else {
|
|
|
|
mCharsetSource = kCharsetFromWeakDocTypeDefault;
|
|
|
|
}
|
2009-09-18 02:21:47 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-25 10:11:02 -07:00
|
|
|
void
|
|
|
|
nsHtml5StreamParser::DoStopRequest()
|
2009-09-18 02:21:47 -07:00
|
|
|
{
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2009-09-21 06:18:20 -07:00
|
|
|
NS_PRECONDITION(STREAM_BEING_READ == mStreamState,
|
|
|
|
"Stream ended without being open.");
|
2009-10-12 06:08:04 -07:00
|
|
|
mTokenizerMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (IsTerminated()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 05:45:17 -07:00
|
|
|
mStreamState = STREAM_ENDED;
|
|
|
|
|
2009-09-18 02:21:47 -07:00
|
|
|
if (!mUnicodeDecoder) {
|
|
|
|
PRUint32 writeCount;
|
2011-09-28 05:45:17 -07:00
|
|
|
if (NS_FAILED(FinalizeSniffing(nsnull, 0, &writeCount, 0))) {
|
|
|
|
MarkAsBroken();
|
|
|
|
return;
|
|
|
|
}
|
2011-02-11 02:33:32 -08:00
|
|
|
} else if (mFeedChardet) {
|
|
|
|
mChardet->Done();
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
2009-09-21 06:18:20 -07:00
|
|
|
|
2009-10-12 06:08:04 -07:00
|
|
|
if (IsTerminatedOrInterrupted()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParseAvailableData();
|
2009-09-25 10:11:02 -07:00
|
|
|
}
|
2009-09-21 06:18:20 -07:00
|
|
|
|
2009-09-25 10:11:02 -07:00
|
|
|
class nsHtml5RequestStopper : public nsRunnable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsHtml5RefPtr<nsHtml5StreamParser> mStreamParser;
|
|
|
|
public:
|
|
|
|
nsHtml5RequestStopper(nsHtml5StreamParser* aStreamParser)
|
|
|
|
: mStreamParser(aStreamParser)
|
|
|
|
{}
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
2009-10-12 06:08:04 -07:00
|
|
|
mozilla::MutexAutoLock autoLock(mStreamParser->mTokenizerMutex);
|
2009-09-25 10:11:02 -07:00
|
|
|
mStreamParser->DoStopRequest();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHtml5StreamParser::OnStopRequest(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult status)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mRequest == aRequest, "Got Stop on wrong stream.");
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2009-09-18 02:21:47 -07:00
|
|
|
if (mObserver) {
|
|
|
|
mObserver->OnStopRequest(aRequest, aContext, status);
|
|
|
|
}
|
2009-09-25 10:11:02 -07:00
|
|
|
nsCOMPtr<nsIRunnable> stopper = new nsHtml5RequestStopper(this);
|
|
|
|
if (NS_FAILED(mThread->Dispatch(stopper, nsIThread::DISPATCH_NORMAL))) {
|
|
|
|
NS_WARNING("Dispatching StopRequest event failed.");
|
|
|
|
}
|
2009-09-18 02:21:47 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-25 10:11:02 -07:00
|
|
|
void
|
|
|
|
nsHtml5StreamParser::DoDataAvailable(PRUint8* aBuffer, PRUint32 aLength)
|
2009-09-18 02:21:47 -07:00
|
|
|
{
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
|
|
|
NS_PRECONDITION(STREAM_BEING_READ == mStreamState,
|
|
|
|
"DoDataAvailable called when stream not open.");
|
2009-10-12 06:08:04 -07:00
|
|
|
mTokenizerMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (IsTerminated()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-25 10:11:02 -07:00
|
|
|
PRUint32 writeCount;
|
2011-09-28 05:45:17 -07:00
|
|
|
nsresult rv;
|
2011-02-11 02:33:32 -08:00
|
|
|
if (HasDecoder()) {
|
|
|
|
if (mFeedChardet) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool dontFeed;
|
2011-02-11 02:33:32 -08:00
|
|
|
mChardet->DoIt((const char*)aBuffer, aLength, &dontFeed);
|
|
|
|
mFeedChardet = !dontFeed;
|
|
|
|
}
|
2011-09-28 05:45:17 -07:00
|
|
|
rv = WriteStreamBytes(aBuffer, aLength, &writeCount);
|
2011-02-11 02:33:32 -08:00
|
|
|
} else {
|
2011-09-28 05:45:17 -07:00
|
|
|
rv = SniffStreamBytes(aBuffer, aLength, &writeCount);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MarkAsBroken();
|
|
|
|
return;
|
2011-02-11 02:33:32 -08:00
|
|
|
}
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_ASSERTION(writeCount == aLength, "Wrong number of stream bytes written/sniffed.");
|
2009-10-12 06:08:04 -07:00
|
|
|
|
|
|
|
if (IsTerminatedOrInterrupted()) {
|
|
|
|
return;
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
2009-10-12 06:08:04 -07:00
|
|
|
|
|
|
|
ParseAvailableData();
|
2010-04-16 03:52:06 -07:00
|
|
|
|
|
|
|
if (mFlushTimerArmed || mSpeculating) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFlushTimer->InitWithFuncCallback(nsHtml5StreamParser::TimerCallback,
|
|
|
|
static_cast<void*> (this),
|
|
|
|
mFlushTimerEverFired ?
|
|
|
|
sTimerInitialDelay :
|
|
|
|
sTimerSubsequentDelay,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
2011-10-17 07:59:28 -07:00
|
|
|
mFlushTimerArmed = true;
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
|
2009-09-25 10:11:02 -07:00
|
|
|
class nsHtml5DataAvailable : public nsRunnable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsHtml5RefPtr<nsHtml5StreamParser> mStreamParser;
|
|
|
|
nsAutoArrayPtr<PRUint8> mData;
|
|
|
|
PRUint32 mLength;
|
|
|
|
public:
|
|
|
|
nsHtml5DataAvailable(nsHtml5StreamParser* aStreamParser,
|
|
|
|
PRUint8* aData,
|
|
|
|
PRUint32 aLength)
|
|
|
|
: mStreamParser(aStreamParser)
|
|
|
|
, mData(aData)
|
|
|
|
, mLength(aLength)
|
|
|
|
{}
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
2009-10-12 06:08:04 -07:00
|
|
|
mozilla::MutexAutoLock autoLock(mStreamParser->mTokenizerMutex);
|
2009-09-25 10:11:02 -07:00
|
|
|
mStreamParser->DoDataAvailable(mData, mLength);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// nsIStreamListener method:
|
2009-09-18 02:21:47 -07:00
|
|
|
nsresult
|
|
|
|
nsHtml5StreamParser::OnDataAvailable(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsIInputStream* aInStream,
|
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
2012-03-21 05:39:25 -07:00
|
|
|
nsresult rv;
|
|
|
|
if (NS_FAILED(rv = mExecutor->IsBroken())) {
|
|
|
|
return rv;
|
2011-09-28 05:45:17 -07:00
|
|
|
}
|
|
|
|
|
2009-09-18 02:21:47 -07:00
|
|
|
NS_ASSERTION(mRequest == aRequest, "Got data on wrong stream.");
|
|
|
|
PRUint32 totalRead;
|
2011-09-28 05:45:17 -07:00
|
|
|
const mozilla::fallible_t fallible = mozilla::fallible_t();
|
|
|
|
nsAutoArrayPtr<PRUint8> data(new (fallible) PRUint8[aLength]);
|
|
|
|
if (!data) {
|
2012-03-21 05:39:25 -07:00
|
|
|
return mExecutor->MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
|
2011-09-28 05:45:17 -07:00
|
|
|
}
|
2012-03-21 05:39:25 -07:00
|
|
|
rv = aInStream->Read(reinterpret_cast<char*>(data.get()),
|
2009-09-25 10:11:02 -07:00
|
|
|
aLength, &totalRead);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ASSERTION(totalRead <= aLength, "Read more bytes than were available?");
|
|
|
|
nsCOMPtr<nsIRunnable> dataAvailable = new nsHtml5DataAvailable(this,
|
2011-09-28 05:45:17 -07:00
|
|
|
data.forget(),
|
2009-09-25 10:11:02 -07:00
|
|
|
totalRead);
|
|
|
|
if (NS_FAILED(mThread->Dispatch(dataAvailable, nsIThread::DISPATCH_NORMAL))) {
|
|
|
|
NS_WARNING("Dispatching DataAvailable event failed.");
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2011-11-01 04:33:11 -07:00
|
|
|
nsHtml5StreamParser::PreferredForInternalEncodingDecl(nsACString& aEncoding)
|
2009-09-18 02:21:47 -07:00
|
|
|
{
|
2011-11-01 04:33:11 -07:00
|
|
|
nsCAutoString newEncoding(aEncoding);
|
2011-01-12 00:05:09 -08:00
|
|
|
newEncoding.Trim(" \t\r\n\f");
|
2010-07-30 03:03:54 -07:00
|
|
|
if (newEncoding.LowerCaseEqualsLiteral("utf-16") ||
|
|
|
|
newEncoding.LowerCaseEqualsLiteral("utf-16be") ||
|
|
|
|
newEncoding.LowerCaseEqualsLiteral("utf-16le")) {
|
|
|
|
newEncoding.Assign("UTF-8");
|
|
|
|
}
|
|
|
|
|
2009-09-18 02:21:47 -07:00
|
|
|
nsresult rv = NS_OK;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool eq;
|
2012-03-04 19:57:51 -08:00
|
|
|
rv = nsCharsetAlias::Equals(newEncoding, mCharset, &eq);
|
2009-09-18 02:21:47 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2009-10-21 05:12:50 -07:00
|
|
|
NS_NOTREACHED("Charset name equality check failed.");
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
if (eq) {
|
|
|
|
mCharsetSource = kCharsetFromMetaTag; // become confident
|
2011-10-17 07:59:28 -07:00
|
|
|
mFeedChardet = false; // don't feed chardet when confident
|
|
|
|
return false;
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX check HTML5 non-IANA aliases here
|
2009-10-21 05:12:50 -07:00
|
|
|
|
|
|
|
nsCAutoString preferred;
|
|
|
|
|
2012-03-04 19:57:51 -08:00
|
|
|
rv = nsCharsetAlias::GetPreferred(newEncoding, preferred);
|
2009-10-21 05:12:50 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// the encoding name is bogus
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
|
2010-07-30 03:03:54 -07:00
|
|
|
if (preferred.LowerCaseEqualsLiteral("utf-16") ||
|
|
|
|
preferred.LowerCaseEqualsLiteral("utf-16be") ||
|
|
|
|
preferred.LowerCaseEqualsLiteral("utf-16le") ||
|
|
|
|
preferred.LowerCaseEqualsLiteral("utf-7") ||
|
|
|
|
preferred.LowerCaseEqualsLiteral("jis_x0212-1990") ||
|
|
|
|
preferred.LowerCaseEqualsLiteral("x-jis0208") ||
|
|
|
|
preferred.LowerCaseEqualsLiteral("x-imap4-modified-utf7") ||
|
|
|
|
preferred.LowerCaseEqualsLiteral("x-user-defined")) {
|
|
|
|
// Not a rough ASCII superset
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-07-30 03:03:54 -07:00
|
|
|
}
|
2011-11-01 04:33:11 -07:00
|
|
|
aEncoding.Assign(preferred);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsHtml5StreamParser::internalEncodingDeclaration(nsString* aEncoding)
|
|
|
|
{
|
|
|
|
// 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!");
|
|
|
|
if (mCharsetSource >= kCharsetFromMetaTag) { // this threshold corresponds to "confident" in the HTML5 spec
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mReparseForbidden) {
|
|
|
|
return false; // not reparsing even if we wanted to
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString newEncoding;
|
|
|
|
CopyUTF16toUTF8(*aEncoding, newEncoding);
|
|
|
|
|
|
|
|
if (!PreferredForInternalEncodingDecl(newEncoding)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-07-30 03:03:54 -07:00
|
|
|
|
2011-07-28 03:45:29 -07:00
|
|
|
// Avoid having the chardet ask for another restart after this restart
|
|
|
|
// request.
|
2011-10-17 07:59:28 -07:00
|
|
|
mFeedChardet = false;
|
2011-11-01 04:33:11 -07:00
|
|
|
mTreeBuilder->NeedsCharsetSwitchTo(newEncoding, kCharsetFromMetaTag);
|
2010-04-16 03:52:06 -07:00
|
|
|
FlushTreeOpsAndDisarmTimer();
|
2009-10-21 05:12:50 -07:00
|
|
|
Interrupt();
|
2009-09-21 06:18:20 -07:00
|
|
|
// the tree op executor will cause the stream parser to terminate
|
2009-10-21 05:12:50 -07:00
|
|
|
// if the charset switch request is accepted or it'll uninterrupt
|
2011-07-28 03:45:29 -07: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 07:59:28 -07:00
|
|
|
return true;
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
|
2010-04-16 03:52:06 -07: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
|
|
|
|
mFlushTimer->Cancel();
|
2011-10-17 07:59:28 -07:00
|
|
|
mFlushTimerArmed = false;
|
2010-04-16 03:52:06 -07:00
|
|
|
}
|
2010-07-30 03:15:38 -07:00
|
|
|
if (mMode == VIEW_SOURCE_HTML || mMode == VIEW_SOURCE_XML) {
|
|
|
|
mTokenizer->FlushViewSource();
|
|
|
|
}
|
2010-04-16 03:52:06 -07:00
|
|
|
mTreeBuilder->Flush();
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(mExecutorFlusher))) {
|
|
|
|
NS_WARNING("failed to dispatch executor flush event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-18 02:21:47 -07:00
|
|
|
void
|
2009-10-12 06:08:04 -07:00
|
|
|
nsHtml5StreamParser::ParseAvailableData()
|
2009-09-18 02:21:47 -07:00
|
|
|
{
|
2009-09-25 10:11:02 -07:00
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2009-10-12 06:08:04 -07:00
|
|
|
mTokenizerMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (IsTerminatedOrInterrupted()) {
|
2009-09-18 02:21:47 -07:00
|
|
|
return;
|
|
|
|
}
|
2009-10-12 06:08:04 -07:00
|
|
|
|
2009-09-18 02:21:47 -07:00
|
|
|
for (;;) {
|
|
|
|
if (!mFirstBuffer->hasMore()) {
|
|
|
|
if (mFirstBuffer == mLastBuffer) {
|
2009-09-21 06:18:20 -07:00
|
|
|
switch (mStreamState) {
|
|
|
|
case STREAM_BEING_READ:
|
2009-10-12 06:08:04 -07:00
|
|
|
// never release the last buffer.
|
|
|
|
if (!mSpeculating) {
|
|
|
|
// reuse buffer space if not speculating
|
|
|
|
mFirstBuffer->setStart(0);
|
|
|
|
mFirstBuffer->setEnd(0);
|
|
|
|
}
|
2010-02-26 01:18:38 -08:00
|
|
|
mTreeBuilder->FlushLoads();
|
|
|
|
// Dispatch this runnable unconditionally, because the loads
|
|
|
|
// that need flushing may have been flushed earlier even if the
|
|
|
|
// flush right above here did nothing.
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(mLoadFlusher))) {
|
|
|
|
NS_WARNING("failed to dispatch load flush event");
|
|
|
|
}
|
2009-09-18 02:21:47 -07:00
|
|
|
return; // no more data for now but expecting more
|
2009-09-21 06:18:20 -07:00
|
|
|
case STREAM_ENDED:
|
2009-10-12 06:08:04 -07:00
|
|
|
if (mAtEOF) {
|
2010-04-09 07:11:56 -07:00
|
|
|
return;
|
2009-10-12 06:08:04 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
mAtEOF = true;
|
2009-09-21 06:18:20 -07:00
|
|
|
mTokenizer->eof();
|
|
|
|
mTreeBuilder->StreamEnded();
|
2010-07-30 03:15:38 -07:00
|
|
|
if (mMode == VIEW_SOURCE_HTML || mMode == VIEW_SOURCE_XML) {
|
|
|
|
mTokenizer->EndViewSource();
|
|
|
|
}
|
2010-04-16 03:52:06 -07:00
|
|
|
FlushTreeOpsAndDisarmTimer();
|
2009-09-18 02:21:47 -07:00
|
|
|
return; // no more data and not expecting more
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("It should be impossible to reach this.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-04-09 07:11:56 -07:00
|
|
|
mFirstBuffer = mFirstBuffer->next;
|
|
|
|
continue;
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// now we have a non-empty buffer
|
|
|
|
mFirstBuffer->adjust(mLastWasCR);
|
2011-10-17 07:59:28 -07:00
|
|
|
mLastWasCR = false;
|
2009-09-18 02:21:47 -07:00
|
|
|
if (mFirstBuffer->hasMore()) {
|
|
|
|
mLastWasCR = mTokenizer->tokenizeBuffer(mFirstBuffer);
|
2009-09-21 06:18:20 -07:00
|
|
|
// At this point, internalEncodingDeclaration() may have called
|
|
|
|
// 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.
|
2010-07-30 03:15:38 -07:00
|
|
|
if (mMode == NORMAL && mTreeBuilder->HasScript()) {
|
2009-10-12 06:08:04 -07:00
|
|
|
mozilla::MutexAutoLock speculationAutoLock(mSpeculationMutex);
|
|
|
|
nsHtml5Speculation* speculation =
|
|
|
|
new nsHtml5Speculation(mFirstBuffer,
|
|
|
|
mFirstBuffer->getStart(),
|
2009-10-28 06:48:37 -07:00
|
|
|
mTokenizer->getLineNumber(),
|
2009-10-12 06:08:04 -07:00
|
|
|
mTreeBuilder->newSnapshot());
|
2009-10-28 06:48:37 -07:00
|
|
|
mTreeBuilder->AddSnapshotToScript(speculation->GetSnapshot(),
|
|
|
|
speculation->GetStartLineNumber());
|
2010-04-16 03:52:06 -07:00
|
|
|
FlushTreeOpsAndDisarmTimer();
|
2009-10-12 06:08:04 -07:00
|
|
|
mTreeBuilder->SetOpSink(speculation);
|
|
|
|
mSpeculations.AppendElement(speculation); // adopts the pointer
|
2011-10-17 07:59:28 -07:00
|
|
|
mSpeculating = true;
|
2009-10-12 06:08:04 -07:00
|
|
|
}
|
|
|
|
if (IsTerminatedOrInterrupted()) {
|
|
|
|
return;
|
2009-09-18 02:21:47 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 06:18:20 -07:00
|
|
|
|
2009-10-12 06:08:04 -07:00
|
|
|
class nsHtml5StreamParserContinuation : public nsRunnable
|
2009-09-25 10:11:02 -07:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsHtml5RefPtr<nsHtml5StreamParser> mStreamParser;
|
|
|
|
public:
|
2009-10-12 06:08:04 -07:00
|
|
|
nsHtml5StreamParserContinuation(nsHtml5StreamParser* aStreamParser)
|
2009-09-25 10:11:02 -07:00
|
|
|
: mStreamParser(aStreamParser)
|
|
|
|
{}
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
2009-10-12 06:08:04 -07:00
|
|
|
mozilla::MutexAutoLock autoLock(mStreamParser->mTokenizerMutex);
|
2010-04-16 03:52:06 -07:00
|
|
|
mStreamParser->Uninterrupt();
|
2009-10-12 06:08:04 -07:00
|
|
|
mStreamParser->ParseAvailableData();
|
2009-09-25 10:11:02 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-09-21 06:18:20 -07:00
|
|
|
void
|
|
|
|
nsHtml5StreamParser::ContinueAfterScripts(nsHtml5Tokenizer* aTokenizer,
|
|
|
|
nsHtml5TreeBuilder* aTreeBuilder,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aLastWasCR)
|
2009-09-21 06:18:20 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2010-07-30 03:15:38 -07:00
|
|
|
NS_ASSERTION(!(mMode == VIEW_SOURCE_HTML || mMode == VIEW_SOURCE_XML),
|
|
|
|
"ContinueAfterScripts called in view source mode!");
|
2011-09-28 05:45:17 -07:00
|
|
|
if (mExecutor->IsBroken()) {
|
|
|
|
return;
|
|
|
|
}
|
2009-10-12 06:08:04 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
mExecutor->AssertStageEmpty();
|
|
|
|
#endif
|
2011-09-28 23:19:26 -07:00
|
|
|
bool speculationFailed = false;
|
2009-10-12 06:08:04 -07:00
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock speculationAutoLock(mSpeculationMutex);
|
|
|
|
if (mSpeculations.IsEmpty()) {
|
2010-04-19 06:25:13 -07:00
|
|
|
NS_NOTREACHED("ContinueAfterScripts called without speculations.");
|
2009-10-12 06:08:04 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsHtml5Speculation* speculation = mSpeculations.ElementAt(0);
|
|
|
|
if (aLastWasCR ||
|
|
|
|
!aTokenizer->isInDataState() ||
|
|
|
|
!aTreeBuilder->snapshotMatches(speculation->GetSnapshot())) {
|
2011-10-17 07:59:28 -07:00
|
|
|
speculationFailed = true;
|
2009-10-12 06:08:04 -07:00
|
|
|
// We've got a failed speculation :-(
|
|
|
|
Interrupt(); // Make the parser thread release the tokenizer mutex sooner
|
|
|
|
// now fall out of the speculationAutoLock into the tokenizerAutoLock block
|
|
|
|
} else {
|
|
|
|
// We've got a successful speculation!
|
|
|
|
if (mSpeculations.Length() > 1) {
|
|
|
|
// the first speculation isn't the current speculation, so there's
|
|
|
|
// no need to bother the parser thread.
|
|
|
|
speculation->FlushToSink(mExecutor);
|
2010-02-26 01:18:38 -08:00
|
|
|
NS_ASSERTION(!mExecutor->IsScriptExecuting(),
|
|
|
|
"ParseUntilBlocked() was supposed to ensure we don't come "
|
|
|
|
"here when scripts are executing.");
|
|
|
|
NS_ASSERTION(mExecutor->IsInFlushLoop(), "How are we here if "
|
|
|
|
"RunFlushLoop() didn't call ParseUntilBlocked() which is the "
|
|
|
|
"only caller of this method?");
|
2009-10-12 06:08:04 -07:00
|
|
|
mSpeculations.RemoveElementAt(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// else
|
|
|
|
Interrupt(); // Make the parser thread release the tokenizer mutex sooner
|
|
|
|
|
|
|
|
// now fall through
|
|
|
|
// the first speculation is the current speculation. Need to
|
|
|
|
// release the the speculation mutex and acquire the tokenizer
|
|
|
|
// mutex. (Just acquiring the other mutex here would deadlock)
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 06:18:20 -07:00
|
|
|
{
|
2009-10-12 06:08:04 -07:00
|
|
|
mozilla::MutexAutoLock tokenizerAutoLock(mTokenizerMutex);
|
2009-09-25 10:11:02 -07:00
|
|
|
#ifdef DEBUG
|
2010-04-09 07:11:56 -07:00
|
|
|
{
|
2009-09-25 10:11:02 -07:00
|
|
|
nsCOMPtr<nsIThread> mainThread;
|
|
|
|
NS_GetMainThread(getter_AddRefs(mainThread));
|
|
|
|
mAtomTable.SetPermittedLookupThread(mainThread);
|
2010-04-09 07:11:56 -07:00
|
|
|
}
|
2009-09-25 10:11:02 -07:00
|
|
|
#endif
|
2009-10-12 06:08:04 -07: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 07:59:28 -07:00
|
|
|
mAtEOF = false;
|
2009-10-12 06:08:04 -07:00
|
|
|
nsHtml5Speculation* speculation = mSpeculations.ElementAt(0);
|
|
|
|
mFirstBuffer = speculation->GetBuffer();
|
|
|
|
mFirstBuffer->setStart(speculation->GetStart());
|
2009-10-28 06:48:37 -07:00
|
|
|
mTokenizer->setLineNumber(speculation->GetStartLineNumber());
|
2010-06-23 01:33:43 -07:00
|
|
|
|
2011-12-15 06:47:03 -08:00
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
|
|
|
"DOM Events",
|
|
|
|
mExecutor->GetDocument(),
|
|
|
|
nsContentUtils::eDOM_PROPERTIES,
|
2010-06-23 01:33:43 -07:00
|
|
|
"SpeculationFailed",
|
|
|
|
nsnull, 0,
|
2010-12-20 08:21:58 -08:00
|
|
|
nsnull,
|
2010-06-23 01:33:43 -07:00
|
|
|
EmptyString(),
|
2011-12-15 06:47:03 -08:00
|
|
|
speculation->GetStartLineNumber());
|
2010-06-23 01:33:43 -07:00
|
|
|
|
2011-09-28 05:45:17 -07:00
|
|
|
nsHtml5OwningUTF16Buffer* buffer = mFirstBuffer->next;
|
2009-10-12 06:08:04 -07:00
|
|
|
while (buffer) {
|
|
|
|
buffer->setStart(0);
|
|
|
|
buffer = buffer->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSpeculations.Clear(); // potentially a huge number of destructors
|
|
|
|
// run here synchronously on the main thread...
|
2009-10-30 04:47:17 -07:00
|
|
|
|
|
|
|
mTreeBuilder->flushCharacters(); // empty the pending buffer
|
|
|
|
mTreeBuilder->ClearOps(); // now get rid of the failed ops
|
|
|
|
|
2009-10-12 06:08:04 -07:00
|
|
|
mTreeBuilder->SetOpSink(mExecutor->GetStage());
|
|
|
|
mExecutor->StartReadingFromStage();
|
2011-10-17 07:59:28 -07:00
|
|
|
mSpeculating = false;
|
2010-04-16 03:52:06 -07:00
|
|
|
|
2009-10-12 06:08:04 -07:00
|
|
|
// Copy state over
|
|
|
|
mLastWasCR = aLastWasCR;
|
|
|
|
mTokenizer->loadState(aTokenizer);
|
|
|
|
mTreeBuilder->loadState(aTreeBuilder, &mAtomTable);
|
|
|
|
} else {
|
|
|
|
// 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 01:18:38 -08:00
|
|
|
NS_ASSERTION(!mExecutor->IsScriptExecuting(),
|
|
|
|
"ParseUntilBlocked() was supposed to ensure we don't come "
|
|
|
|
"here when scripts are executing.");
|
|
|
|
NS_ASSERTION(mExecutor->IsInFlushLoop(), "How are we here if "
|
|
|
|
"RunFlushLoop() didn't call ParseUntilBlocked() which is the "
|
|
|
|
"only caller of this method?");
|
2009-10-12 06:08:04 -07:00
|
|
|
mSpeculations.RemoveElementAt(0);
|
|
|
|
if (mSpeculations.IsEmpty()) {
|
|
|
|
// yes, it was still the only speculation. Now stop speculating
|
2011-05-19 01:42:01 -07: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 07:59:28 -07:00
|
|
|
mTreeBuilder->Flush(true);
|
2009-10-12 06:08:04 -07:00
|
|
|
mTreeBuilder->SetOpSink(mExecutor->GetStage());
|
|
|
|
mExecutor->StartReadingFromStage();
|
2011-10-17 07:59:28 -07:00
|
|
|
mSpeculating = false;
|
2009-10-12 06:08:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRunnable> event = new nsHtml5StreamParserContinuation(this);
|
|
|
|
if (NS_FAILED(mThread->Dispatch(event, nsIThread::DISPATCH_NORMAL))) {
|
2010-04-16 03:52:06 -07:00
|
|
|
NS_WARNING("Failed to dispatch nsHtml5StreamParserContinuation");
|
2009-10-12 06:08:04 -07:00
|
|
|
}
|
|
|
|
// A stream event might run before this event runs, but that's harmless.
|
2009-09-25 10:11:02 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
mAtomTable.SetPermittedLookupThread(mThread);
|
|
|
|
#endif
|
2009-09-21 06:18:20 -07:00
|
|
|
}
|
|
|
|
}
|
2009-10-21 05:12:50 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5StreamParser::ContinueAfterFailedCharsetSwitch()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
|
|
|
nsCOMPtr<nsIRunnable> event = new nsHtml5StreamParserContinuation(this);
|
|
|
|
if (NS_FAILED(mThread->Dispatch(event, nsIThread::DISPATCH_NORMAL))) {
|
2009-11-17 00:52:30 -08:00
|
|
|
NS_WARNING("Failed to dispatch nsHtml5StreamParserContinuation");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-16 03:52:06 -07:00
|
|
|
class nsHtml5TimerKungFu : public nsRunnable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsHtml5RefPtr<nsHtml5StreamParser> mStreamParser;
|
|
|
|
public:
|
|
|
|
nsHtml5TimerKungFu(nsHtml5StreamParser* aStreamParser)
|
|
|
|
: mStreamParser(aStreamParser)
|
|
|
|
{}
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
if (mStreamParser->mFlushTimer) {
|
|
|
|
mStreamParser->mFlushTimer->Cancel();
|
|
|
|
mStreamParser->mFlushTimer = nsnull;
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
* The nsHtml5TimerKungFu object has an nsHtml5RefPtr that addrefs this
|
|
|
|
* stream parser object to keep it alive until the runnable is done.
|
|
|
|
* The runnable cancels the timer on the parser thread, drops the timer
|
|
|
|
* and lets nsHtml5RefPtr send a runnable back to the main thread to
|
|
|
|
* release the stream parser.
|
|
|
|
*/
|
|
|
|
if (mFlushTimer) {
|
|
|
|
nsCOMPtr<nsIRunnable> event = new nsHtml5TimerKungFu(this);
|
|
|
|
if (NS_FAILED(mThread->Dispatch(event, nsIThread::DISPATCH_NORMAL))) {
|
|
|
|
NS_WARNING("Failed to dispatch TimerKungFu event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-17 00:52:30 -08:00
|
|
|
// Using a static, because the method name Notify is taken by the chardet
|
|
|
|
// callback.
|
|
|
|
void
|
|
|
|
nsHtml5StreamParser::TimerCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
2010-04-16 03:52:06 -07:00
|
|
|
(static_cast<nsHtml5StreamParser*> (aClosure))->TimerFlush();
|
2009-11-17 00:52:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5StreamParser::TimerFlush()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
2010-04-16 03:52:06 -07: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 07:59:28 -07:00
|
|
|
mFlushTimerArmed = false;
|
2009-11-17 00:52:30 -08:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mFlushTimerEverFired = true;
|
2010-04-16 03:52:06 -07:00
|
|
|
|
|
|
|
if (IsTerminatedOrInterrupted()) {
|
2009-11-17 00:52:30 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-30 03:15:38 -07:00
|
|
|
if (mMode == VIEW_SOURCE_HTML || mMode == VIEW_SOURCE_XML) {
|
|
|
|
mTreeBuilder->Flush(); // delete useless ops
|
|
|
|
if (mTokenizer->FlushViewSource()) {
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(mExecutorFlusher))) {
|
|
|
|
NS_WARNING("failed to dispatch executor flush event");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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)) {
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(mExecutorFlusher))) {
|
|
|
|
NS_WARNING("failed to dispatch executor flush event");
|
|
|
|
}
|
2009-11-17 00:52:30 -08:00
|
|
|
}
|
|
|
|
}
|
2009-10-21 05:12:50 -07:00
|
|
|
}
|
2011-09-28 05:45:17 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5StreamParser::MarkAsBroken()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsParserThread(), "Wrong thread!");
|
|
|
|
mTokenizerMutex.AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
Terminate();
|
|
|
|
mTreeBuilder->MarkAsBroken();
|
2011-10-17 07:59:28 -07:00
|
|
|
mozilla::DebugOnly<bool> hadOps = mTreeBuilder->Flush(false);
|
2011-09-28 05:45:17 -07:00
|
|
|
NS_ASSERTION(hadOps, "Should have had the markAsBroken op!");
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(mExecutorFlusher))) {
|
|
|
|
NS_WARNING("failed to dispatch executor flush event");
|
|
|
|
}
|
|
|
|
}
|