2009-09-18 13:21:47 +04: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 "nsHtml5TreeOpExecutor.h"
|
|
|
|
#include "nsScriptLoader.h"
|
|
|
|
#include "nsIMarkupDocumentViewer.h"
|
|
|
|
#include "nsIContentViewer.h"
|
|
|
|
#include "nsIDocShellTreeItem.h"
|
|
|
|
#include "nsIStyleSheetLinkingElement.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptGlobalObjectOwner.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
|
|
|
#include "nsIWebShellServices.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "mozAutoDocUpdate.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsHtml5Parser.h"
|
|
|
|
#include "nsHtml5Tokenizer.h"
|
|
|
|
#include "nsHtml5TreeBuilder.h"
|
|
|
|
#include "nsHtml5StreamParser.h"
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsHtml5TreeOpExecutor)
|
|
|
|
|
|
|
|
NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(nsHtml5TreeOpExecutor)
|
|
|
|
NS_INTERFACE_TABLE_INHERITED1(nsHtml5TreeOpExecutor,
|
|
|
|
nsIContentSink)
|
|
|
|
NS_INTERFACE_TABLE_TAIL_INHERITING(nsContentSink)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsHtml5TreeOpExecutor, nsContentSink)
|
|
|
|
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsHtml5TreeOpExecutor, nsContentSink)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsHtml5TreeOpExecutor, nsContentSink)
|
2009-09-21 15:43:43 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(mOwnedElements)
|
2009-09-18 13:21:47 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsHtml5TreeOpExecutor, nsContentSink)
|
2009-09-21 15:43:43 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMARRAY(mOwnedElements)
|
2009-09-18 13:21:47 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2010-01-14 16:58:27 +03:00
|
|
|
PRInt32 nsHtml5TreeOpExecutor::sTreeOpQueueLengthLimit = 200;
|
|
|
|
PRInt32 nsHtml5TreeOpExecutor::sTreeOpQueueMaxTime = 100; // milliseconds
|
|
|
|
PRInt32 nsHtml5TreeOpExecutor::sTreeOpQueueMinLength = 100;
|
|
|
|
PRInt32 nsHtml5TreeOpExecutor::sTreeOpQueueMaxLength = 4500;
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::InitializeStatics()
|
|
|
|
{
|
|
|
|
// Changes to the initial max length pref are intentionally allowed
|
|
|
|
// to reset the run-time-calibrated value.
|
|
|
|
nsContentUtils::AddIntPrefVarCache("html5.opqueue.initiallengthlimit",
|
|
|
|
&sTreeOpQueueLengthLimit);
|
|
|
|
nsContentUtils::AddIntPrefVarCache("html5.opqueue.maxtime",
|
|
|
|
&sTreeOpQueueMaxTime);
|
|
|
|
nsContentUtils::AddIntPrefVarCache("html5.opqueue.minlength",
|
|
|
|
&sTreeOpQueueMinLength);
|
|
|
|
nsContentUtils::AddIntPrefVarCache("html5.opqueue.maxlength",
|
|
|
|
&sTreeOpQueueMaxLength);
|
|
|
|
// Now do some sanity checking to prevent breaking the app via
|
|
|
|
// about:config so badly that it can't be recovered via about:config.
|
|
|
|
if (sTreeOpQueueMinLength <= 0) {
|
|
|
|
sTreeOpQueueMinLength = 200;
|
|
|
|
}
|
|
|
|
if (sTreeOpQueueLengthLimit < sTreeOpQueueMinLength) {
|
|
|
|
sTreeOpQueueLengthLimit = sTreeOpQueueMinLength;
|
|
|
|
}
|
|
|
|
if (sTreeOpQueueMaxLength < sTreeOpQueueMinLength) {
|
|
|
|
sTreeOpQueueMaxLength = sTreeOpQueueMinLength;
|
|
|
|
}
|
|
|
|
if (sTreeOpQueueMaxTime <= 0) {
|
|
|
|
sTreeOpQueueMaxTime = 200;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
nsHtml5TreeOpExecutor::nsHtml5TreeOpExecutor()
|
|
|
|
{
|
2009-11-17 11:52:30 +03:00
|
|
|
// zeroing operator new for everything
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHtml5TreeOpExecutor::~nsHtml5TreeOpExecutor()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mOpQueue.IsEmpty(), "Somehow there's stuff in the op queue.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIContentSink
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHtml5TreeOpExecutor::WillParse()
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("No one should call this");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is called when the tree construction has ended
|
|
|
|
NS_IMETHODIMP
|
2009-10-02 15:13:59 +04:00
|
|
|
nsHtml5TreeOpExecutor::DidBuildModel(PRBool aTerminated)
|
2009-09-18 13:21:47 +04:00
|
|
|
{
|
2009-11-06 16:06:48 +03:00
|
|
|
NS_PRECONDITION(mStarted, "Bad life cycle.");
|
|
|
|
|
2010-01-05 15:37:05 +03:00
|
|
|
if (!aTerminated) {
|
|
|
|
// Break out of update batch if we are in one
|
|
|
|
// and aren't forcibly terminating
|
|
|
|
EndDocUpdate();
|
|
|
|
|
|
|
|
// If the above caused a call to nsIParser::Terminate(), let that call
|
|
|
|
// win.
|
|
|
|
if (!mParser) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-11-06 16:06:48 +03:00
|
|
|
}
|
|
|
|
|
2009-11-30 18:34:51 +03:00
|
|
|
static_cast<nsHtml5Parser*> (mParser.get())->DropStreamParser();
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
// This is comes from nsXMLContentSink
|
2009-10-02 15:13:59 +04:00
|
|
|
DidBuildModelImpl(aTerminated);
|
2009-09-18 13:21:47 +04:00
|
|
|
mDocument->ScriptLoader()->RemoveObserver(this);
|
|
|
|
ScrollToRef();
|
|
|
|
mDocument->RemoveObserver(this);
|
2009-11-30 18:34:51 +03:00
|
|
|
if (!mParser) {
|
|
|
|
// DidBuildModelImpl may cause mParser to be nulled out
|
|
|
|
// Return early to avoid unblocking the onload event too many times.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-09-18 13:21:47 +04:00
|
|
|
mDocument->EndLoad();
|
|
|
|
DropParserAndPerfHint();
|
|
|
|
#ifdef GATHER_DOCWRITE_STATISTICS
|
|
|
|
printf("UNSAFE SCRIPTS: %d\n", sUnsafeDocWrites);
|
|
|
|
printf("TOKENIZER-SAFE SCRIPTS: %d\n", sTokenSafeDocWrites);
|
|
|
|
printf("TREEBUILDER-SAFE SCRIPTS: %d\n", sTreeSafeDocWrites);
|
|
|
|
#endif
|
2010-01-14 16:58:27 +03:00
|
|
|
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
|
2009-09-18 13:21:47 +04:00
|
|
|
printf("MAX NOTIFICATION BATCH LEN: %d\n", sAppendBatchMaxSize);
|
|
|
|
if (sAppendBatchExaminations != 0) {
|
|
|
|
printf("AVERAGE SLOTS EXAMINED: %d\n", sAppendBatchSlotsExamined / sAppendBatchExaminations);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHtml5TreeOpExecutor::WillInterrupt()
|
|
|
|
{
|
|
|
|
return WillInterruptImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHtml5TreeOpExecutor::WillResume()
|
|
|
|
{
|
|
|
|
WillResumeImpl();
|
|
|
|
return WillParseImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHtml5TreeOpExecutor::SetParser(nsIParser* aParser)
|
|
|
|
{
|
|
|
|
mParser = aParser;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::FlushPendingNotifications(mozFlushType aType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHtml5TreeOpExecutor::SetDocumentCharset(nsACString& aCharset)
|
|
|
|
{
|
|
|
|
if (mDocShell) {
|
|
|
|
// the following logic to get muCV is copied from
|
|
|
|
// nsHTMLDocument::StartDocumentLoad
|
|
|
|
// We need to call muCV->SetPrevDocCharacterSet here in case
|
|
|
|
// the charset is detected by parser DetectMetaTag
|
|
|
|
nsCOMPtr<nsIMarkupDocumentViewer> muCV;
|
|
|
|
nsCOMPtr<nsIContentViewer> cv;
|
|
|
|
mDocShell->GetContentViewer(getter_AddRefs(cv));
|
|
|
|
if (cv) {
|
|
|
|
muCV = do_QueryInterface(cv);
|
|
|
|
} else {
|
|
|
|
// in this block of code, if we get an error result, we return
|
|
|
|
// it but if we get a null pointer, that's perfectly legal for
|
|
|
|
// parent and parentContentViewer
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> docShellAsItem =
|
|
|
|
do_QueryInterface(mDocShell);
|
|
|
|
NS_ENSURE_TRUE(docShellAsItem, NS_ERROR_FAILURE);
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> parentAsItem;
|
|
|
|
docShellAsItem->GetSameTypeParent(getter_AddRefs(parentAsItem));
|
|
|
|
nsCOMPtr<nsIDocShell> parent(do_QueryInterface(parentAsItem));
|
|
|
|
if (parent) {
|
|
|
|
nsCOMPtr<nsIContentViewer> parentContentViewer;
|
|
|
|
nsresult rv =
|
|
|
|
parent->GetContentViewer(getter_AddRefs(parentContentViewer));
|
|
|
|
if (NS_SUCCEEDED(rv) && parentContentViewer) {
|
|
|
|
muCV = do_QueryInterface(parentContentViewer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (muCV) {
|
|
|
|
muCV->SetPrevDocCharacterSet(aCharset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mDocument) {
|
|
|
|
mDocument->SetDocumentCharacterSet(aCharset);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsISupports*
|
|
|
|
nsHtml5TreeOpExecutor::GetTarget()
|
|
|
|
{
|
|
|
|
return mDocument;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsContentSink overrides
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::UpdateChildCounts()
|
|
|
|
{
|
|
|
|
// No-op
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHtml5TreeOpExecutor::FlushTags()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::PostEvaluateScript(nsIScriptElement *aElement)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDocument = do_QueryInterface(mDocument);
|
|
|
|
NS_ASSERTION(htmlDocument, "Document didn't QI into HTML document.");
|
|
|
|
htmlDocument->ScriptExecuted(aElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::UpdateStyleSheet(nsIContent* aElement)
|
|
|
|
{
|
2009-10-30 11:31:57 +03:00
|
|
|
// Break out of the doc update created by Flush() to zap a runnable
|
|
|
|
// waiting to call UpdateStyleSheet without the right observer
|
|
|
|
EndDocUpdate();
|
|
|
|
|
|
|
|
if (NS_UNLIKELY(!mParser)) {
|
|
|
|
// EndDocUpdate ran stuff that called nsIParser::Terminate()
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
nsCOMPtr<nsIStyleSheetLinkingElement> ssle(do_QueryInterface(aElement));
|
2009-10-30 11:31:57 +03:00
|
|
|
NS_ASSERTION(ssle, "Node didn't QI to style.");
|
|
|
|
|
|
|
|
ssle->SetEnableUpdates(PR_TRUE);
|
|
|
|
|
|
|
|
PRBool willNotify;
|
|
|
|
PRBool isAlternate;
|
|
|
|
nsresult rv = ssle->UpdateStyleSheet(this, &willNotify, &isAlternate);
|
|
|
|
if (NS_SUCCEEDED(rv) && willNotify && !isAlternate) {
|
|
|
|
++mPendingSheetCount;
|
|
|
|
mScriptLoader->AddExecuteBlocker();
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2009-10-30 11:31:57 +03:00
|
|
|
|
|
|
|
// Re-open update
|
|
|
|
BeginDocUpdate();
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
2010-01-14 16:58:27 +03:00
|
|
|
class nsHtml5ExecutorReflusher : public nsRunnable
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsHtml5TreeOpExecutor> mExecutor;
|
|
|
|
public:
|
|
|
|
nsHtml5ExecutorReflusher(nsHtml5TreeOpExecutor* aExecutor)
|
|
|
|
: mExecutor(aExecutor)
|
|
|
|
{}
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
mExecutor->Flush(PR_FALSE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
void
|
2010-01-14 16:58:27 +03:00
|
|
|
nsHtml5TreeOpExecutor::Flush(PRBool aForceWholeQueue)
|
2009-09-18 13:21:47 +04:00
|
|
|
{
|
2009-10-12 17:08:04 +04:00
|
|
|
if (!mParser) {
|
2010-01-05 15:37:05 +03:00
|
|
|
mOpQueue.Clear(); // clear in order to be able to assert in destructor
|
2009-09-21 17:18:20 +04:00
|
|
|
return;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2009-11-06 16:06:48 +03:00
|
|
|
if (mFlushState != eNotFlushing) {
|
2009-10-15 15:29:11 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-06 16:06:48 +03:00
|
|
|
mFlushState = eInFlush;
|
2009-09-21 17:18:20 +04:00
|
|
|
|
2009-10-12 17:08:04 +04:00
|
|
|
nsRefPtr<nsHtml5TreeOpExecutor> kungFuDeathGrip(this); // avoid crashing near EOF
|
|
|
|
nsCOMPtr<nsIParser> parserKungFuDeathGrip(mParser);
|
2009-09-21 17:18:20 +04:00
|
|
|
|
|
|
|
if (mReadingFromStage) {
|
2009-11-17 11:52:30 +03:00
|
|
|
mStage.MoveOpsTo(mOpQueue);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2009-09-21 17:18:20 +04:00
|
|
|
|
2009-11-06 16:06:48 +03:00
|
|
|
nsIContent* scriptElement = nsnull;
|
|
|
|
|
2009-10-30 11:31:57 +03:00
|
|
|
BeginDocUpdate();
|
|
|
|
|
|
|
|
PRIntervalTime flushStart = 0;
|
2010-01-14 16:58:27 +03:00
|
|
|
PRUint32 numberOfOpsToFlush = mOpQueue.Length();
|
|
|
|
PRBool reflushNeeded = PR_FALSE;
|
|
|
|
|
|
|
|
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
|
|
|
|
if (numberOfOpsToFlush > sOpQueueMaxLength) {
|
|
|
|
sOpQueueMaxLength = numberOfOpsToFlush;
|
|
|
|
}
|
|
|
|
printf("QUEUE LENGTH: %d\n", numberOfOpsToFlush);
|
|
|
|
printf("MAX QUEUE LENGTH: %d\n", sOpQueueMaxLength);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (aForceWholeQueue) {
|
|
|
|
if (numberOfOpsToFlush > (PRUint32)sTreeOpQueueMinLength) {
|
|
|
|
flushStart = PR_IntervalNow(); // compute averages only if enough ops
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (numberOfOpsToFlush > (PRUint32)sTreeOpQueueMinLength) {
|
|
|
|
flushStart = PR_IntervalNow(); // compute averages only if enough ops
|
|
|
|
if (numberOfOpsToFlush > (PRUint32)sTreeOpQueueLengthLimit) {
|
|
|
|
numberOfOpsToFlush = (PRUint32)sTreeOpQueueLengthLimit;
|
|
|
|
reflushNeeded = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
2009-10-30 11:31:57 +03:00
|
|
|
}
|
2010-01-14 16:58:27 +03:00
|
|
|
|
|
|
|
mElementsSeenInThisAppendBatch.SetCapacity(numberOfOpsToFlush * 2);
|
|
|
|
|
2009-10-30 11:31:57 +03:00
|
|
|
const nsHtml5TreeOperation* start = mOpQueue.Elements();
|
2010-01-14 16:58:27 +03:00
|
|
|
const nsHtml5TreeOperation* end = start + numberOfOpsToFlush;
|
2009-10-30 11:31:57 +03:00
|
|
|
for (nsHtml5TreeOperation* iter = (nsHtml5TreeOperation*)start; iter < end; ++iter) {
|
|
|
|
if (NS_UNLIKELY(!mParser)) {
|
|
|
|
// The previous tree op caused a call to nsIParser::Terminate();
|
|
|
|
break;
|
2009-09-21 17:18:20 +04:00
|
|
|
}
|
2009-11-06 16:06:48 +03:00
|
|
|
NS_ASSERTION(mFlushState == eInDocUpdate, "Tried to perform tree op outside update batch.");
|
|
|
|
iter->Perform(this, &scriptElement);
|
2009-10-30 11:31:57 +03:00
|
|
|
}
|
|
|
|
|
2010-01-14 16:58:27 +03:00
|
|
|
if (NS_LIKELY(mParser)) {
|
|
|
|
mOpQueue.RemoveElementsAt(0, numberOfOpsToFlush);
|
|
|
|
} else {
|
|
|
|
mOpQueue.Clear(); // only for diagnostics in the destructor of this class
|
2009-10-30 11:31:57 +03:00
|
|
|
}
|
2010-01-14 16:58:27 +03:00
|
|
|
|
2009-10-30 11:31:57 +03:00
|
|
|
if (flushStart) {
|
|
|
|
PRUint32 delta = PR_IntervalToMilliseconds(PR_IntervalNow() - flushStart);
|
2010-01-14 16:58:27 +03:00
|
|
|
sTreeOpQueueLengthLimit = delta ?
|
|
|
|
(PRUint32)(((PRUint64)sTreeOpQueueMaxTime * (PRUint64)numberOfOpsToFlush)
|
|
|
|
/ delta) :
|
|
|
|
sTreeOpQueueMaxLength; // if the delta is less than one ms, use max
|
|
|
|
if (sTreeOpQueueLengthLimit < sTreeOpQueueMinLength) {
|
|
|
|
// both are signed and sTreeOpQueueMinLength is always positive, so this
|
|
|
|
// also takes care of the theoretical overflow of sTreeOpQueueLengthLimit
|
|
|
|
sTreeOpQueueLengthLimit = sTreeOpQueueMinLength;
|
|
|
|
}
|
|
|
|
if (sTreeOpQueueLengthLimit > sTreeOpQueueMaxLength) {
|
|
|
|
sTreeOpQueueLengthLimit = sTreeOpQueueMaxLength;
|
2009-10-30 11:31:57 +03:00
|
|
|
}
|
2010-01-14 16:58:27 +03:00
|
|
|
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
|
|
|
|
printf("FLUSH DURATION (millis): %d\n", delta);
|
|
|
|
printf("QUEUE NEW MAX LENGTH: %d\n", sTreeOpQueueLengthLimit);
|
2009-09-18 13:21:47 +04:00
|
|
|
#endif
|
2009-09-21 17:18:20 +04:00
|
|
|
}
|
2009-10-21 16:10:14 +04:00
|
|
|
|
2009-10-30 11:31:57 +03:00
|
|
|
EndDocUpdate();
|
|
|
|
|
2009-11-06 16:06:48 +03:00
|
|
|
mFlushState = eNotFlushing;
|
2009-10-21 16:10:14 +04:00
|
|
|
|
2010-01-14 16:58:27 +03:00
|
|
|
if (NS_UNLIKELY(!mParser)) {
|
2009-10-21 16:10:14 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-06 16:06:48 +03:00
|
|
|
if (scriptElement) {
|
2010-01-14 16:58:27 +03:00
|
|
|
NS_ASSERTION(!reflushNeeded, "Got scriptElement when queue not fully flushed.");
|
2009-11-06 16:06:48 +03:00
|
|
|
RunScript(scriptElement); // must be tail call when mFlushState is eNotFlushing
|
2010-01-14 16:58:27 +03:00
|
|
|
} else if (reflushNeeded) {
|
|
|
|
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
|
|
|
|
printf("REFLUSH SCHEDULED.\n");
|
|
|
|
#endif
|
|
|
|
nsCOMPtr<nsIRunnable> flusher = new nsHtml5ExecutorReflusher(this);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(flusher))) {
|
|
|
|
NS_WARNING("failed to dispatch executor flush event");
|
|
|
|
}
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2009-09-21 17:18:20 +04:00
|
|
|
}
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
nsresult
|
|
|
|
nsHtml5TreeOpExecutor::ProcessBASETag(nsIContent* aContent)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aContent, "missing base-element");
|
|
|
|
if (mHasProcessedBase) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
mHasProcessedBase = PR_TRUE;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mDocument) {
|
|
|
|
nsAutoString value;
|
|
|
|
if (aContent->GetAttr(kNameSpaceID_None, nsHtml5Atoms::target, value)) {
|
|
|
|
mDocument->SetBaseTarget(value);
|
|
|
|
}
|
|
|
|
if (aContent->GetAttr(kNameSpaceID_None, nsHtml5Atoms::href, value)) {
|
|
|
|
nsCOMPtr<nsIURI> baseURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(baseURI), value);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = mDocument->SetBaseURI(baseURI); // The document checks if it is legal to set this base
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
mDocumentBaseURI = mDocument->GetBaseURI();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copied from HTML content sink
|
|
|
|
PRBool
|
|
|
|
nsHtml5TreeOpExecutor::IsScriptEnabled()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mDocument && mDocShell, PR_TRUE);
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> globalObject = mDocument->GetScriptGlobalObject();
|
|
|
|
// Getting context is tricky if the document hasn't had its
|
|
|
|
// GlobalObject set yet
|
|
|
|
if (!globalObject) {
|
|
|
|
nsCOMPtr<nsIScriptGlobalObjectOwner> owner = do_GetInterface(mDocShell);
|
|
|
|
NS_ENSURE_TRUE(owner, PR_TRUE);
|
|
|
|
globalObject = owner->GetScriptGlobalObject();
|
|
|
|
NS_ENSURE_TRUE(globalObject, PR_TRUE);
|
|
|
|
}
|
|
|
|
nsIScriptContext *scriptContext = globalObject->GetContext();
|
|
|
|
NS_ENSURE_TRUE(scriptContext, PR_TRUE);
|
|
|
|
JSContext* cx = (JSContext *) scriptContext->GetNativeContext();
|
|
|
|
NS_ENSURE_TRUE(cx, PR_TRUE);
|
|
|
|
PRBool enabled = PR_TRUE;
|
|
|
|
nsContentUtils::GetSecurityManager()->
|
|
|
|
CanExecuteScripts(cx, mDocument->NodePrincipal(), &enabled);
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::DocumentMode(nsHtml5DocumentMode m)
|
|
|
|
{
|
|
|
|
nsCompatibility mode = eCompatibility_NavQuirks;
|
|
|
|
switch (m) {
|
|
|
|
case STANDARDS_MODE:
|
|
|
|
mode = eCompatibility_FullStandards;
|
|
|
|
break;
|
|
|
|
case ALMOST_STANDARDS_MODE:
|
|
|
|
mode = eCompatibility_AlmostStandards;
|
|
|
|
break;
|
|
|
|
case QUIRKS_MODE:
|
|
|
|
mode = eCompatibility_NavQuirks;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDocument = do_QueryInterface(mDocument);
|
|
|
|
NS_ASSERTION(htmlDocument, "Document didn't QI into HTML document.");
|
|
|
|
htmlDocument->SetCompatibilityMode(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-09-21 17:18:20 +04:00
|
|
|
* The reason why this code is here and not in the tree builder even in the
|
|
|
|
* main-thread case is to allow the control to return from the tokenizer
|
|
|
|
* before scripts run. This way, the tokenizer is not invoked re-entrantly
|
|
|
|
* although the parser is.
|
2009-11-06 16:06:48 +03:00
|
|
|
*
|
|
|
|
* The reason why this is called as a tail call when mFlushState is set to
|
|
|
|
* eNotFlushing is to allow re-entry to Flush() but only after the current
|
|
|
|
* Flush() has cleared the op queue and is otherwise done cleaning up after
|
|
|
|
* itself.
|
2009-09-18 13:21:47 +04:00
|
|
|
*/
|
|
|
|
void
|
2009-11-06 16:06:48 +03:00
|
|
|
nsHtml5TreeOpExecutor::RunScript(nsIContent* aScriptElement)
|
2009-09-18 13:21:47 +04:00
|
|
|
{
|
2009-11-06 16:06:48 +03:00
|
|
|
NS_ASSERTION(aScriptElement, "No script to run");
|
|
|
|
nsCOMPtr<nsIScriptElement> sele = do_QueryInterface(aScriptElement);
|
2009-11-17 11:52:30 +03:00
|
|
|
|
2009-10-12 17:08:04 +04:00
|
|
|
if (!mParser) {
|
2009-09-21 17:18:20 +04:00
|
|
|
NS_ASSERTION(sele->IsMalformed(), "Script wasn't marked as malformed.");
|
|
|
|
// We got here not because of an end tag but because the tree builder
|
|
|
|
// popped an incomplete script element on EOF. Returning here to avoid
|
2009-11-06 16:06:48 +03:00
|
|
|
// calling back into mParser anymore.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFragmentMode) {
|
|
|
|
// ending the doc update called nsIParser::Terminate or we are in the
|
|
|
|
// fragment mode
|
|
|
|
sele->PreventExecution();
|
2009-09-21 17:18:20 +04:00
|
|
|
return;
|
|
|
|
}
|
2009-11-06 16:06:48 +03:00
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
if (sele->GetScriptDeferred() || sele->GetScriptAsync()) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
aScriptElement->DoneAddingChildren(PR_TRUE); // scripts ignore the argument
|
|
|
|
NS_ASSERTION(rv != NS_ERROR_HTMLPARSER_BLOCK,
|
|
|
|
"Defer or async script tried to block.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mFlushState == eNotFlushing, "Tried to run script when flushing.");
|
|
|
|
|
|
|
|
mReadingFromStage = PR_FALSE;
|
|
|
|
|
2009-10-15 15:29:11 +04:00
|
|
|
sele->SetCreatorParser(mParser);
|
2009-11-17 11:52:30 +03:00
|
|
|
|
2009-09-21 17:18:20 +04:00
|
|
|
// Notify our document that we're loading this script.
|
2009-09-18 13:21:47 +04:00
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDocument = do_QueryInterface(mDocument);
|
|
|
|
NS_ASSERTION(htmlDocument, "Document didn't QI into HTML document.");
|
|
|
|
htmlDocument->ScriptLoading(sele);
|
2009-11-17 11:52:30 +03:00
|
|
|
|
2009-11-06 16:06:48 +03:00
|
|
|
// Copied from nsXMLContentSink
|
2009-09-18 13:21:47 +04:00
|
|
|
// Now tell the script that it's ready to go. This may execute the script
|
|
|
|
// or return NS_ERROR_HTMLPARSER_BLOCK. Or neither if the script doesn't
|
|
|
|
// need executing.
|
2009-11-06 16:06:48 +03:00
|
|
|
nsresult rv = aScriptElement->DoneAddingChildren(PR_TRUE);
|
2009-11-17 11:52:30 +03:00
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
// If the act of insertion evaluated the script, we're fine.
|
|
|
|
// Else, block the parser till the script has loaded.
|
|
|
|
if (rv == NS_ERROR_HTMLPARSER_BLOCK) {
|
|
|
|
mScriptElements.AppendObject(sele);
|
|
|
|
mParser->BlockParser();
|
2009-10-12 17:08:04 +04:00
|
|
|
} else {
|
2009-09-18 13:21:47 +04:00
|
|
|
// This may have already happened if the script executed, but in case
|
|
|
|
// it didn't then remove the element so that it doesn't get stuck forever.
|
|
|
|
htmlDocument->ScriptExecuted(sele);
|
2009-10-12 17:08:04 +04:00
|
|
|
// mParser may have been nulled out by now, but nsContentSink deals
|
|
|
|
ContinueInterruptedParsingAsync();
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHtml5TreeOpExecutor::Init(nsIDocument* aDoc,
|
|
|
|
nsIURI* aURI,
|
|
|
|
nsISupports* aContainer,
|
|
|
|
nsIChannel* aChannel)
|
|
|
|
{
|
|
|
|
nsresult rv = nsContentSink::Init(aDoc, aURI, aContainer, aChannel);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-09-21 17:18:20 +04:00
|
|
|
mCanInterruptParser = PR_FALSE; // without this, nsContentSink calls
|
|
|
|
// UnblockOnload from DropParserAndPerfHint
|
2009-09-18 13:21:47 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::Start()
|
|
|
|
{
|
2009-10-12 17:08:04 +04:00
|
|
|
NS_PRECONDITION(!mStarted, "Tried to start when already started.");
|
|
|
|
mStarted = PR_TRUE;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-09-21 17:18:20 +04:00
|
|
|
nsHtml5TreeOpExecutor::NeedsCharsetSwitchTo(const char* aEncoding)
|
2009-10-21 16:12:50 +04:00
|
|
|
{
|
2009-11-06 16:06:48 +03:00
|
|
|
EndDocUpdate();
|
2009-10-21 16:12:50 +04:00
|
|
|
|
2009-11-06 16:06:48 +03:00
|
|
|
if(NS_UNLIKELY(!mParser)) {
|
|
|
|
// got terminate
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-21 17:18:20 +04:00
|
|
|
nsCOMPtr<nsIWebShellServices> wss = do_QueryInterface(mDocShell);
|
|
|
|
if (!wss) {
|
|
|
|
return;
|
|
|
|
}
|
2010-02-18 22:23:23 +03:00
|
|
|
|
2009-09-21 17:18:20 +04:00
|
|
|
// ask the webshellservice to load the URL
|
2010-02-18 22:23:23 +03:00
|
|
|
if (NS_SUCCEEDED(wss->StopDocumentLoad())) {
|
|
|
|
wss->ReloadDocument(aEncoding, kCharsetFromMetaTag);
|
2009-09-21 17:18:20 +04:00
|
|
|
}
|
|
|
|
// if the charset switch was accepted, wss has called Terminate() on the
|
|
|
|
// parser by now
|
2009-11-06 16:06:48 +03:00
|
|
|
|
|
|
|
if (!mParser) {
|
|
|
|
// success
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(static_cast<nsHtml5Parser*> (mParser.get()))->ContinueAfterFailedCharsetSwitch();
|
|
|
|
|
|
|
|
BeginDocUpdate();
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHtml5Tokenizer*
|
|
|
|
nsHtml5TreeOpExecutor::GetTokenizer()
|
|
|
|
{
|
|
|
|
return (static_cast<nsHtml5Parser*> (mParser.get()))->GetTokenizer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-09-21 17:18:20 +04:00
|
|
|
nsHtml5TreeOpExecutor::Reset() {
|
|
|
|
mHasProcessedBase = PR_FALSE;
|
|
|
|
mReadingFromStage = PR_FALSE;
|
|
|
|
mOpQueue.Clear();
|
2009-10-12 17:08:04 +04:00
|
|
|
mStarted = PR_FALSE;
|
2009-11-06 16:06:48 +03:00
|
|
|
mFlushState = eNotFlushing;
|
|
|
|
mFragmentMode = PR_FALSE;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-11-17 11:52:30 +03:00
|
|
|
nsHtml5TreeOpExecutor::MoveOpsFrom(nsTArray<nsHtml5TreeOperation>& aOpQueue)
|
2009-09-21 17:18:20 +04:00
|
|
|
{
|
2009-11-06 16:06:48 +03:00
|
|
|
NS_PRECONDITION(mFlushState == eNotFlushing, "mOpQueue modified during tree op execution.");
|
2009-09-21 17:18:20 +04:00
|
|
|
if (mOpQueue.IsEmpty()) {
|
|
|
|
mOpQueue.SwapElements(aOpQueue);
|
|
|
|
return;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2009-09-21 17:18:20 +04:00
|
|
|
mOpQueue.MoveElementsFrom(aOpQueue);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-10-28 16:48:37 +03:00
|
|
|
nsHtml5TreeOpExecutor::InitializeDocWriteParserState(nsAHtml5TreeBuilderState* aState, PRInt32 aLine)
|
2009-09-21 17:18:20 +04:00
|
|
|
{
|
2009-10-28 16:48:37 +03:00
|
|
|
static_cast<nsHtml5Parser*> (mParser.get())->InitializeDocWriteParserState(aState, aLine);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
2010-01-14 16:58:27 +03:00
|
|
|
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
|
|
|
|
PRUint32 nsHtml5TreeOpExecutor::sOpQueueMaxLength = 0;
|
2009-09-18 13:21:47 +04:00
|
|
|
PRUint32 nsHtml5TreeOpExecutor::sAppendBatchMaxSize = 0;
|
|
|
|
PRUint32 nsHtml5TreeOpExecutor::sAppendBatchSlotsExamined = 0;
|
|
|
|
PRUint32 nsHtml5TreeOpExecutor::sAppendBatchExaminations = 0;
|
|
|
|
#endif
|