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: */
|
2012-05-29 19:52:43 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-09-18 13:21:47 +04:00
|
|
|
|
2012-12-15 03:58:45 +04:00
|
|
|
#include "mozilla/DebugOnly.h"
|
|
|
|
#include "mozilla/Likely.h"
|
|
|
|
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2009-09-18 13:21:47 +04:00
|
|
|
#include "nsHtml5TreeOpExecutor.h"
|
|
|
|
#include "nsScriptLoader.h"
|
|
|
|
#include "nsIContentViewer.h"
|
|
|
|
#include "nsIDocShellTreeItem.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIScriptGlobalObject.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"
|
2011-08-01 11:48:24 +04:00
|
|
|
#include "mozilla/css/Loader.h"
|
2013-03-18 18:25:50 +04:00
|
|
|
#include "GeckoProfiler.h"
|
2012-03-24 15:34:42 +04:00
|
|
|
#include "nsIScriptError.h"
|
2012-07-02 03:45:59 +04:00
|
|
|
#include "nsIScriptContext.h"
|
2012-03-27 10:27:45 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-07-02 03:45:59 +04:00
|
|
|
#include "nsIHTMLDocument.h"
|
2011-09-04 14:49:29 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2009-09-18 13:21:47 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(nsHtml5TreeOpExecutor)
|
2014-08-06 17:31:21 +04:00
|
|
|
NS_INTERFACE_TABLE_INHERITED(nsHtml5TreeOpExecutor,
|
2014-04-27 11:06:00 +04:00
|
|
|
nsIContentSink)
|
2014-03-05 23:38:49 +04:00
|
|
|
NS_INTERFACE_TABLE_TAIL_INHERITING(nsHtml5DocumentBuilder)
|
2009-09-18 13:21:47 +04:00
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsHtml5TreeOpExecutor, nsContentSink)
|
|
|
|
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsHtml5TreeOpExecutor, nsContentSink)
|
|
|
|
|
2010-02-26 12:18:38 +03:00
|
|
|
class nsHtml5ExecutorReflusher : public nsRunnable
|
2010-01-14 16:58:27 +03:00
|
|
|
{
|
2010-02-26 12:18:38 +03:00
|
|
|
private:
|
|
|
|
nsRefPtr<nsHtml5TreeOpExecutor> mExecutor;
|
|
|
|
public:
|
|
|
|
nsHtml5ExecutorReflusher(nsHtml5TreeOpExecutor* aExecutor)
|
|
|
|
: mExecutor(aExecutor)
|
|
|
|
{}
|
|
|
|
NS_IMETHODIMP Run()
|
|
|
|
{
|
|
|
|
mExecutor->RunFlushLoop();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
2010-01-14 16:58:27 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
static mozilla::LinkedList<nsHtml5TreeOpExecutor>* gBackgroundFlushList = nullptr;
|
|
|
|
static nsITimer* gFlushTimer = nullptr;
|
2012-06-14 22:00:05 +04:00
|
|
|
|
2014-03-05 23:38:50 +04:00
|
|
|
nsHtml5TreeOpExecutor::nsHtml5TreeOpExecutor()
|
|
|
|
: nsHtml5DocumentBuilder(false)
|
|
|
|
, mPreloadedURLs(23) // Mean # of preloadable resources per page on dmoz
|
2009-09-18 13:21:47 +04:00
|
|
|
{
|
2010-02-26 12:18:38 +03:00
|
|
|
// zeroing operator new for everything else
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHtml5TreeOpExecutor::~nsHtml5TreeOpExecutor()
|
|
|
|
{
|
2012-06-14 22:00:05 +04:00
|
|
|
if (gBackgroundFlushList && isInList()) {
|
2012-06-21 18:13:10 +04:00
|
|
|
mOpQueue.Clear();
|
2012-10-24 04:43:23 +04:00
|
|
|
removeFrom(*gBackgroundFlushList);
|
2012-06-14 22:00:05 +04:00
|
|
|
if (gBackgroundFlushList->isEmpty()) {
|
|
|
|
delete gBackgroundFlushList;
|
2012-07-30 18:20:58 +04:00
|
|
|
gBackgroundFlushList = nullptr;
|
2012-06-14 22:00:05 +04:00
|
|
|
if (gFlushTimer) {
|
|
|
|
gFlushTimer->Cancel();
|
|
|
|
NS_RELEASE(gFlushTimer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-21 18:13:10 +04:00
|
|
|
NS_ASSERTION(mOpQueue.IsEmpty(), "Somehow there's stuff in the op queue.");
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIContentSink
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHtml5TreeOpExecutor::WillParse()
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("No one should call this");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2012-03-21 16:39:25 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHtml5TreeOpExecutor::WillBuildModel(nsDTDMode aDTDMode)
|
|
|
|
{
|
2014-04-12 13:02:21 +04:00
|
|
|
mDocument->AddObserver(this);
|
|
|
|
WillBuildModelImpl();
|
|
|
|
GetDocument()->BeginLoad();
|
2013-06-21 18:25:20 +04:00
|
|
|
if (mDocShell && !GetDocument()->GetWindow() &&
|
2012-03-27 10:27:45 +04:00
|
|
|
!IsExternalViewSource()) {
|
2012-03-21 16:39:25 +04:00
|
|
|
// Not loading as data but script global object not ready
|
|
|
|
return MarkAsBroken(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
// This is called when the tree construction has ended
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHtml5TreeOpExecutor::DidBuildModel(bool aTerminated)
|
2009-09-18 13:21:47 +04:00
|
|
|
{
|
2010-01-05 15:37:05 +03:00
|
|
|
if (!aTerminated) {
|
2010-03-15 15:04:42 +03:00
|
|
|
// This is needed to avoid unblocking loads too many times on one hand
|
|
|
|
// and on the other hand to avoid destroying the frame constructor from
|
|
|
|
// within an update batch. See bug 537683.
|
2010-01-05 15:37:05 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-01-26 17:59:22 +04:00
|
|
|
if (mRunsToCompletion) {
|
|
|
|
return NS_OK;
|
2012-01-20 16:03:49 +04:00
|
|
|
}
|
2009-11-30 18:34:51 +03:00
|
|
|
|
2012-01-26 17:59:22 +04:00
|
|
|
GetParser()->DropStreamParser();
|
|
|
|
|
2010-03-15 15:04:42 +03:00
|
|
|
// This comes from nsXMLContentSink and nsHTMLContentSink
|
2012-03-21 16:39:25 +04:00
|
|
|
// If this parser has been marked as broken, treat the end of parse as
|
|
|
|
// forced termination.
|
2012-08-14 13:10:41 +04:00
|
|
|
DidBuildModelImpl(aTerminated || NS_FAILED(IsBroken()));
|
2010-02-22 15:17:33 +03:00
|
|
|
|
|
|
|
if (!mLayoutStarted) {
|
|
|
|
// We never saw the body, and layout never got started. Force
|
|
|
|
// layout *now*, to get an initial reflow.
|
|
|
|
|
|
|
|
// NOTE: only force the layout if we are NOT destroying the
|
|
|
|
// docshell. If we are destroying it, then starting layout will
|
|
|
|
// likely cause us to crash, or at best waste a lot of time as we
|
|
|
|
// are just going to tear it down anyway.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool destroying = true;
|
2010-02-22 15:17:33 +03:00
|
|
|
if (mDocShell) {
|
|
|
|
mDocShell->IsBeingDestroyed(&destroying);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!destroying) {
|
2011-10-17 18:59:28 +04:00
|
|
|
nsContentSink::StartLayout(false);
|
2010-02-22 15:17:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
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;
|
|
|
|
}
|
2014-04-12 13:02:21 +04:00
|
|
|
|
|
|
|
// We may not have called BeginLoad() if loading is terminated before
|
|
|
|
// OnStartRequest call.
|
|
|
|
if (mStarted) {
|
|
|
|
mDocument->EndLoad();
|
|
|
|
}
|
2009-09-18 13:21:47 +04:00
|
|
|
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()
|
|
|
|
{
|
2010-02-26 12:18:38 +03:00
|
|
|
NS_NOTREACHED("Don't call. For interface compat only.");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHtml5TreeOpExecutor::WillResume()
|
|
|
|
{
|
2010-02-26 12:18:38 +03:00
|
|
|
NS_NOTREACHED("Don't call. For interface compat only.");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-01-20 15:16:27 +04:00
|
|
|
nsHtml5TreeOpExecutor::SetParser(nsParserBase* aParser)
|
2009-09-18 13:21:47 +04:00
|
|
|
{
|
|
|
|
mParser = aParser;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::FlushPendingNotifications(mozFlushType aType)
|
|
|
|
{
|
2010-07-21 13:49:36 +04:00
|
|
|
if (aType >= Flush_InterruptibleLayout) {
|
|
|
|
// Bug 577508 / 253951
|
2011-10-17 18:59:28 +04:00
|
|
|
nsContentSink::StartLayout(true);
|
2010-07-21 13:49:36 +04:00
|
|
|
}
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsISupports*
|
|
|
|
nsHtml5TreeOpExecutor::GetTarget()
|
|
|
|
{
|
|
|
|
return mDocument;
|
|
|
|
}
|
|
|
|
|
2012-03-21 16:39:25 +04:00
|
|
|
nsresult
|
|
|
|
nsHtml5TreeOpExecutor::MarkAsBroken(nsresult aReason)
|
2011-09-28 16:45:17 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
|
2012-03-21 16:39:25 +04:00
|
|
|
mBroken = aReason;
|
2011-09-28 16:45:17 +04:00
|
|
|
if (mStreamParser) {
|
|
|
|
mStreamParser->Terminate();
|
|
|
|
}
|
|
|
|
// We are under memory pressure, but let's hope the following allocation
|
|
|
|
// works out so that we get to terminate and clean up the parser from
|
|
|
|
// a safer point.
|
|
|
|
if (mParser) { // can mParser ever be null here?
|
|
|
|
nsCOMPtr<nsIRunnable> terminator =
|
|
|
|
NS_NewRunnableMethod(GetParser(), &nsHtml5Parser::Terminate);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(terminator))) {
|
|
|
|
NS_WARNING("failed to dispatch executor flush event");
|
|
|
|
}
|
|
|
|
}
|
2012-03-21 16:39:25 +04:00
|
|
|
return aReason;
|
2011-09-28 16:45:17 +04:00
|
|
|
}
|
|
|
|
|
2012-06-14 22:00:05 +04:00
|
|
|
void
|
|
|
|
FlushTimerCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsHtml5TreeOpExecutor> ex = gBackgroundFlushList->popFirst();
|
|
|
|
if (ex) {
|
|
|
|
ex->RunFlushLoop();
|
|
|
|
}
|
|
|
|
if (gBackgroundFlushList && gBackgroundFlushList->isEmpty()) {
|
|
|
|
delete gBackgroundFlushList;
|
2012-07-30 18:20:58 +04:00
|
|
|
gBackgroundFlushList = nullptr;
|
2012-06-14 22:00:05 +04:00
|
|
|
gFlushTimer->Cancel();
|
|
|
|
NS_RELEASE(gFlushTimer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-26 12:18:38 +03:00
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::ContinueInterruptedParsingAsync()
|
|
|
|
{
|
2012-06-14 22:00:05 +04:00
|
|
|
if (!mDocument || !mDocument->IsInBackgroundWindow()) {
|
|
|
|
nsCOMPtr<nsIRunnable> flusher = new nsHtml5ExecutorReflusher(this);
|
|
|
|
if (NS_FAILED(NS_DispatchToMainThread(flusher))) {
|
|
|
|
NS_WARNING("failed to dispatch executor flush event");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!gBackgroundFlushList) {
|
|
|
|
gBackgroundFlushList = new mozilla::LinkedList<nsHtml5TreeOpExecutor>();
|
|
|
|
}
|
|
|
|
if (!isInList()) {
|
|
|
|
gBackgroundFlushList->insertBack(this);
|
|
|
|
}
|
|
|
|
if (!gFlushTimer) {
|
|
|
|
nsCOMPtr<nsITimer> t = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
t.swap(gFlushTimer);
|
|
|
|
// The timer value 50 should not hopefully slow down background pages too
|
|
|
|
// much, yet lets event loop to process enough between ticks.
|
|
|
|
// See bug 734015.
|
2012-07-30 18:20:58 +04:00
|
|
|
gFlushTimer->InitWithFuncCallback(FlushTimerCallback, nullptr,
|
2012-06-14 22:00:05 +04:00
|
|
|
50, nsITimer::TYPE_REPEATING_SLACK);
|
|
|
|
}
|
|
|
|
}
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::FlushSpeculativeLoads()
|
|
|
|
{
|
|
|
|
nsTArray<nsHtml5SpeculativeLoad> speculativeLoadQueue;
|
|
|
|
mStage.MoveSpeculativeLoadsTo(speculativeLoadQueue);
|
|
|
|
const nsHtml5SpeculativeLoad* start = speculativeLoadQueue.Elements();
|
|
|
|
const nsHtml5SpeculativeLoad* end = start + speculativeLoadQueue.Length();
|
2011-09-04 14:49:29 +04:00
|
|
|
for (nsHtml5SpeculativeLoad* iter = const_cast<nsHtml5SpeculativeLoad*>(start);
|
|
|
|
iter < end;
|
2010-02-26 12:18:38 +03:00
|
|
|
++iter) {
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(!mParser)) {
|
2011-10-14 14:39:19 +04:00
|
|
|
// An extension terminated the parser from a HTTP observer.
|
|
|
|
return;
|
|
|
|
}
|
2010-02-26 12:18:38 +03:00
|
|
|
iter->Perform(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class nsHtml5FlushLoopGuard
|
2010-01-14 16:58:27 +03:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
nsRefPtr<nsHtml5TreeOpExecutor> mExecutor;
|
2010-02-26 12:18:38 +03:00
|
|
|
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mStartTime;
|
2010-02-26 12:18:38 +03:00
|
|
|
#endif
|
2010-01-14 16:58:27 +03:00
|
|
|
public:
|
2010-02-26 12:18:38 +03:00
|
|
|
nsHtml5FlushLoopGuard(nsHtml5TreeOpExecutor* aExecutor)
|
2010-01-14 16:58:27 +03:00
|
|
|
: mExecutor(aExecutor)
|
2010-02-26 12:18:38 +03:00
|
|
|
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
|
|
|
|
, mStartTime(PR_IntervalToMilliseconds(PR_IntervalNow()))
|
|
|
|
#endif
|
2010-01-14 16:58:27 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mExecutor->mRunFlushLoopOnStack = true;
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
|
|
|
~nsHtml5FlushLoopGuard()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t timeOffTheEventLoop =
|
2010-02-26 12:18:38 +03:00
|
|
|
PR_IntervalToMilliseconds(PR_IntervalNow()) - mStartTime;
|
|
|
|
if (timeOffTheEventLoop >
|
|
|
|
nsHtml5TreeOpExecutor::sLongestTimeOffTheEventLoop) {
|
|
|
|
nsHtml5TreeOpExecutor::sLongestTimeOffTheEventLoop =
|
|
|
|
timeOffTheEventLoop;
|
|
|
|
}
|
|
|
|
printf("Longest time off the event loop: %d\n",
|
|
|
|
nsHtml5TreeOpExecutor::sLongestTimeOffTheEventLoop);
|
|
|
|
#endif
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mExecutor->mRunFlushLoopOnStack = false;
|
2010-01-14 16:58:27 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-02-26 12:18:38 +03:00
|
|
|
/**
|
|
|
|
* The purpose of the loop here is to avoid returning to the main event loop
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::RunFlushLoop()
|
|
|
|
{
|
2014-05-24 01:12:29 +04:00
|
|
|
PROFILER_LABEL("nsHtml5TreeOpExecutor", "RunFlushLoop",
|
|
|
|
js::ProfileEntry::Category::OTHER);
|
|
|
|
|
2010-02-26 12:18:38 +03:00
|
|
|
if (mRunFlushLoopOnStack) {
|
|
|
|
// There's already a RunFlushLoop() on the call stack.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHtml5FlushLoopGuard guard(this); // this is also the self-kungfu!
|
|
|
|
|
2012-01-20 15:16:27 +04:00
|
|
|
nsCOMPtr<nsISupports> parserKungFuDeathGrip(mParser);
|
2010-02-26 12:18:38 +03:00
|
|
|
|
|
|
|
// Remember the entry time
|
|
|
|
(void) nsContentSink::WillParseImpl();
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (!mParser) {
|
|
|
|
// Parse has terminated.
|
|
|
|
mOpQueue.Clear(); // clear in order to be able to assert in destructor
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-14 13:10:41 +04:00
|
|
|
if (NS_FAILED(IsBroken())) {
|
2011-09-28 16:45:17 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-02-26 12:18:38 +03:00
|
|
|
if (!mParser->IsParserEnabled()) {
|
|
|
|
// The parser is blocked.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFlushState != eNotFlushing) {
|
|
|
|
// XXX Can this happen? In case it can, let's avoid crashing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are scripts executing, then the content sink is jumping the gun
|
|
|
|
// (probably due to a synchronous XMLHttpRequest) and will re-enable us
|
|
|
|
// later, see bug 460706.
|
|
|
|
if (IsScriptExecuting()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mReadingFromStage) {
|
|
|
|
nsTArray<nsHtml5SpeculativeLoad> speculativeLoadQueue;
|
|
|
|
mStage.MoveOpsAndSpeculativeLoadsTo(mOpQueue, speculativeLoadQueue);
|
|
|
|
// Make sure speculative loads never start after the corresponding
|
|
|
|
// normal loads for the same URLs.
|
|
|
|
const nsHtml5SpeculativeLoad* start = speculativeLoadQueue.Elements();
|
|
|
|
const nsHtml5SpeculativeLoad* end = start + speculativeLoadQueue.Length();
|
|
|
|
for (nsHtml5SpeculativeLoad* iter = (nsHtml5SpeculativeLoad*)start;
|
|
|
|
iter < end;
|
|
|
|
++iter) {
|
|
|
|
iter->Perform(this);
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(!mParser)) {
|
2011-10-14 14:39:19 +04:00
|
|
|
// An extension terminated the parser from a HTTP observer.
|
|
|
|
mOpQueue.Clear(); // clear in order to be able to assert in destructor
|
|
|
|
return;
|
|
|
|
}
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
FlushSpeculativeLoads(); // Make sure speculative loads never start after
|
|
|
|
// the corresponding normal loads for the same
|
|
|
|
// URLs.
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(!mParser)) {
|
2011-10-14 14:39:19 +04:00
|
|
|
// An extension terminated the parser from a HTTP observer.
|
|
|
|
mOpQueue.Clear(); // clear in order to be able to assert in destructor
|
|
|
|
return;
|
|
|
|
}
|
2010-02-26 12:18:38 +03:00
|
|
|
// Not sure if this grip is still needed, but previously, the code
|
|
|
|
// gripped before calling ParseUntilBlocked();
|
|
|
|
nsRefPtr<nsHtml5StreamParser> streamKungFuDeathGrip =
|
2011-09-04 14:49:29 +04:00
|
|
|
GetParser()->GetStreamParser();
|
2010-02-26 12:18:38 +03:00
|
|
|
// Now parse content left in the document.write() buffer queue if any.
|
|
|
|
// This may generate tree ops on its own or dequeue a speculation.
|
2011-09-04 14:49:29 +04:00
|
|
|
GetParser()->ParseUntilBlocked();
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mOpQueue.IsEmpty()) {
|
|
|
|
// Avoid bothering the rest of the engine with a doc update if there's
|
|
|
|
// nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFlushState = eInFlush;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIContent* scriptElement = nullptr;
|
2010-02-26 12:18:38 +03:00
|
|
|
|
|
|
|
BeginDocUpdate();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numberOfOpsToFlush = mOpQueue.Length();
|
2010-02-26 12:18:38 +03:00
|
|
|
|
|
|
|
const nsHtml5TreeOperation* first = mOpQueue.Elements();
|
|
|
|
const nsHtml5TreeOperation* last = first + numberOfOpsToFlush - 1;
|
2011-09-04 14:49:29 +04:00
|
|
|
for (nsHtml5TreeOperation* iter = const_cast<nsHtml5TreeOperation*>(first);;) {
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(!mParser)) {
|
2010-02-26 12:18:38 +03:00
|
|
|
// The previous tree op caused a call to nsIParser::Terminate().
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(mFlushState == eInDocUpdate,
|
|
|
|
"Tried to perform tree op outside update batch.");
|
2013-12-19 18:09:00 +04:00
|
|
|
nsresult rv = iter->Perform(this, &scriptElement);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MarkAsBroken(rv);
|
|
|
|
break;
|
|
|
|
}
|
2010-02-26 12:18:38 +03:00
|
|
|
|
|
|
|
// Be sure not to check the deadline if the last op was just performed.
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(iter == last)) {
|
2010-02-26 12:18:38 +03:00
|
|
|
break;
|
2012-10-26 17:32:10 +04:00
|
|
|
} else if (MOZ_UNLIKELY(nsContentSink::DidProcessATokenImpl() ==
|
|
|
|
NS_ERROR_HTMLPARSER_INTERRUPTED)) {
|
2010-02-26 12:18:38 +03:00
|
|
|
mOpQueue.RemoveElementsAt(0, (iter - first) + 1);
|
|
|
|
|
|
|
|
EndDocUpdate();
|
|
|
|
|
|
|
|
mFlushState = eNotFlushing;
|
|
|
|
|
|
|
|
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
|
|
|
|
printf("REFLUSH SCHEDULED (executing ops): %d\n",
|
|
|
|
++sTimesFlushLoopInterrupted);
|
|
|
|
#endif
|
|
|
|
nsHtml5TreeOpExecutor::ContinueInterruptedParsingAsync();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
mOpQueue.Clear();
|
|
|
|
|
|
|
|
EndDocUpdate();
|
|
|
|
|
|
|
|
mFlushState = eNotFlushing;
|
|
|
|
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(!mParser)) {
|
2010-02-26 12:18:38 +03:00
|
|
|
// The parse ended already.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scriptElement) {
|
|
|
|
// must be tail call when mFlushState is eNotFlushing
|
|
|
|
RunScript(scriptElement);
|
|
|
|
|
2012-01-20 15:16:27 +04:00
|
|
|
// Always check the clock in nsContentSink right after a script
|
|
|
|
StopDeflecting();
|
2010-02-26 12:18:38 +03:00
|
|
|
if (nsContentSink::DidProcessATokenImpl() ==
|
|
|
|
NS_ERROR_HTMLPARSER_INTERRUPTED) {
|
|
|
|
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
|
|
|
|
printf("REFLUSH SCHEDULED (after script): %d\n",
|
|
|
|
++sTimesFlushLoopInterrupted);
|
|
|
|
#endif
|
|
|
|
nsHtml5TreeOpExecutor::ContinueInterruptedParsingAsync();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
void
|
2010-02-26 12:18:38 +03:00
|
|
|
nsHtml5TreeOpExecutor::FlushDocumentWrite()
|
2009-09-18 13:21:47 +04:00
|
|
|
{
|
2011-10-14 14:39:19 +04:00
|
|
|
FlushSpeculativeLoads(); // Make sure speculative loads never start after the
|
|
|
|
// corresponding normal loads for the same URLs.
|
|
|
|
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(!mParser)) {
|
2010-02-26 12:18:38 +03:00
|
|
|
// The parse has ended.
|
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
|
|
|
}
|
2010-02-26 12:18:38 +03:00
|
|
|
|
2009-11-06 16:06:48 +03:00
|
|
|
if (mFlushState != eNotFlushing) {
|
2010-02-26 12:18:38 +03:00
|
|
|
// XXX Can this happen? In case it can, let's avoid crashing.
|
2009-10-15 15:29:11 +04:00
|
|
|
return;
|
|
|
|
}
|
2010-02-26 12:18:38 +03:00
|
|
|
|
2009-11-06 16:06:48 +03:00
|
|
|
mFlushState = eInFlush;
|
2009-09-21 17:18:20 +04:00
|
|
|
|
2010-02-26 12:18:38 +03:00
|
|
|
// avoid crashing near EOF
|
|
|
|
nsRefPtr<nsHtml5TreeOpExecutor> kungFuDeathGrip(this);
|
2012-01-20 15:16:27 +04:00
|
|
|
nsRefPtr<nsParserBase> parserKungFuDeathGrip(mParser);
|
2009-09-21 17:18:20 +04:00
|
|
|
|
2010-02-26 12:18:38 +03:00
|
|
|
NS_ASSERTION(!mReadingFromStage,
|
|
|
|
"Got doc write flush when reading from stage");
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
mStage.AssertEmpty();
|
|
|
|
#endif
|
2009-09-21 17:18:20 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIContent* scriptElement = nullptr;
|
2009-11-06 16:06:48 +03:00
|
|
|
|
2009-10-30 11:31:57 +03:00
|
|
|
BeginDocUpdate();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numberOfOpsToFlush = mOpQueue.Length();
|
2010-01-14 16:58:27 +03:00
|
|
|
|
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;
|
2011-09-04 14:49:29 +04:00
|
|
|
for (nsHtml5TreeOperation* iter = const_cast<nsHtml5TreeOperation*>(start);
|
|
|
|
iter < end;
|
2010-02-26 12:18:38 +03:00
|
|
|
++iter) {
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(!mParser)) {
|
2010-02-26 12:18:38 +03:00
|
|
|
// The previous tree op caused a call to nsIParser::Terminate().
|
2009-10-30 11:31:57 +03:00
|
|
|
break;
|
2009-09-21 17:18:20 +04:00
|
|
|
}
|
2010-02-26 12:18:38 +03:00
|
|
|
NS_ASSERTION(mFlushState == eInDocUpdate,
|
|
|
|
"Tried to perform tree op outside update batch.");
|
2013-12-19 18:09:00 +04:00
|
|
|
nsresult rv = iter->Perform(this, &scriptElement);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MarkAsBroken(rv);
|
|
|
|
break;
|
|
|
|
}
|
2009-10-30 11:31:57 +03:00
|
|
|
}
|
|
|
|
|
2010-02-26 12:18:38 +03:00
|
|
|
mOpQueue.Clear();
|
2010-01-14 16:58:27 +03: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
|
|
|
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(!mParser)) {
|
2010-02-26 12:18:38 +03:00
|
|
|
// Ending the doc update caused a call to nsIParser::Terminate().
|
2009-10-21 16:10:14 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-06 16:06:48 +03:00
|
|
|
if (scriptElement) {
|
2010-02-26 12:18:38 +03:00
|
|
|
// must be tail call when mFlushState is eNotFlushing
|
|
|
|
RunScript(scriptElement);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2009-09-21 17:18:20 +04:00
|
|
|
}
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
// copied from HTML content sink
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2009-09-18 13:21:47 +04:00
|
|
|
nsHtml5TreeOpExecutor::IsScriptEnabled()
|
|
|
|
{
|
2010-06-04 23:03:24 +04:00
|
|
|
if (!mDocument || !mDocShell)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2013-11-13 04:43:31 +04:00
|
|
|
nsCOMPtr<nsIScriptGlobalObject> globalObject = do_QueryInterface(mDocument->GetInnerWindow());
|
2009-09-18 13:21:47 +04:00
|
|
|
// Getting context is tricky if the document hasn't had its
|
|
|
|
// GlobalObject set yet
|
|
|
|
if (!globalObject) {
|
2013-05-27 14:07:01 +04:00
|
|
|
globalObject = mDocShell->GetScriptGlobalObject();
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(globalObject, true);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
2013-11-13 04:43:31 +04:00
|
|
|
NS_ENSURE_TRUE(globalObject && globalObject->GetGlobalJSObject(), true);
|
|
|
|
return nsContentUtils::GetSecurityManager()->
|
|
|
|
ScriptAllowed(globalObject->GetGlobalJSObject());
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
2010-02-22 15:17:33 +03:00
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::StartLayout() {
|
|
|
|
if (mLayoutStarted || !mDocument) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EndDocUpdate();
|
|
|
|
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(!mParser)) {
|
2010-02-22 15:17:33 +03:00
|
|
|
// got terminate
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
nsContentSink::StartLayout(false);
|
2010-02-22 15:17:33 +03:00
|
|
|
|
|
|
|
BeginDocUpdate();
|
|
|
|
}
|
|
|
|
|
2009-09-18 13:21:47 +04:00
|
|
|
/**
|
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
|
|
|
{
|
2012-06-20 11:05:39 +04:00
|
|
|
if (mRunsToCompletion) {
|
|
|
|
// We are in createContextualFragment() or in the upcoming document.parse().
|
|
|
|
// Do nothing. Let's not even mark scripts malformed here, because that
|
|
|
|
// could cause serialization weirdness later.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-11-17 11:52:30 +03:00
|
|
|
if (sele->GetScriptDeferred() || sele->GetScriptAsync()) {
|
2011-11-16 11:50:18 +04:00
|
|
|
DebugOnly<bool> block = sele->AttemptToExecute();
|
|
|
|
NS_ASSERTION(!block, "Defer or async script tried to block.");
|
2009-11-17 11:52:30 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mFlushState == eNotFlushing, "Tried to run script when flushing.");
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mReadingFromStage = false;
|
2009-11-17 11:52:30 +03:00
|
|
|
|
2012-01-20 15:16:27 +04:00
|
|
|
sele->SetCreatorParser(GetParser());
|
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
|
2011-11-16 11:50:18 +04:00
|
|
|
// or return true, or neither if the script doesn't need executing.
|
|
|
|
bool block = sele->AttemptToExecute();
|
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.
|
2011-11-16 11:50:18 +04:00
|
|
|
if (block) {
|
2011-02-16 16:14:11 +03:00
|
|
|
if (mParser) {
|
2012-01-20 15:16:27 +04:00
|
|
|
GetParser()->BlockParser();
|
2011-02-16 16:14:11 +03:00
|
|
|
}
|
2009-10-12 17:08:04 +04:00
|
|
|
} else {
|
2010-02-26 12:18:38 +03:00
|
|
|
// mParser may have been nulled out by now, but the flusher deals
|
|
|
|
|
|
|
|
// If this event isn't needed, it doesn't do anything. It is sometimes
|
|
|
|
// necessary for the parse to continue after complex situations.
|
|
|
|
nsHtml5TreeOpExecutor::ContinueInterruptedParsingAsync();
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::Start()
|
|
|
|
{
|
2009-10-12 17:08:04 +04:00
|
|
|
NS_PRECONDITION(!mStarted, "Tried to start when already started.");
|
2011-10-17 18:59:28 +04:00
|
|
|
mStarted = true;
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-02-11 13:33:32 +03:00
|
|
|
nsHtml5TreeOpExecutor::NeedsCharsetSwitchTo(const char* aEncoding,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aSource,
|
|
|
|
uint32_t aLineNumber)
|
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
|
|
|
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(!mParser)) {
|
2009-11-06 16:06:48 +03:00
|
|
|
// 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())) {
|
2011-02-11 13:33:32 +03:00
|
|
|
wss->ReloadDocument(aEncoding, aSource);
|
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
|
2012-03-24 15:34:42 +04:00
|
|
|
if (aSource == kCharsetFromMetaTag) {
|
|
|
|
MaybeComplainAboutCharset("EncLateMetaReload", false, aLineNumber);
|
|
|
|
}
|
2009-11-06 16:06:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-24 15:34:42 +04:00
|
|
|
if (aSource == kCharsetFromMetaTag) {
|
|
|
|
MaybeComplainAboutCharset("EncLateMetaTooLate", true, aLineNumber);
|
|
|
|
}
|
|
|
|
|
2011-09-04 14:49:29 +04:00
|
|
|
GetParser()->ContinueAfterFailedCharsetSwitch();
|
2009-11-06 16:06:48 +03:00
|
|
|
|
|
|
|
BeginDocUpdate();
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
2012-03-24 15:34:42 +04:00
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::MaybeComplainAboutCharset(const char* aMsgId,
|
|
|
|
bool aError,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aLineNumber)
|
2012-03-24 15:34:42 +04:00
|
|
|
{
|
|
|
|
if (mAlreadyComplainedAboutCharset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The EncNoDeclaration case for advertising iframes is so common that it
|
|
|
|
// would result is way too many errors. The iframe case doesn't matter
|
|
|
|
// when the ad is an image or a Flash animation anyway. When the ad is
|
|
|
|
// textual, a misrendered ad probably isn't a huge loss for users.
|
|
|
|
// Let's suppress the message in this case.
|
|
|
|
// This means that errors about other different-origin iframes in mashups
|
|
|
|
// are lost as well, but generally, the site author isn't in control of
|
|
|
|
// the embedded different-origin pages anyway and can't fix problems even
|
|
|
|
// if alerted about them.
|
|
|
|
if (!strcmp(aMsgId, "EncNoDeclaration") && mDocShell) {
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> parent;
|
2013-02-13 02:02:51 +04:00
|
|
|
mDocShell->GetSameTypeParent(getter_AddRefs(parent));
|
2012-03-24 15:34:42 +04:00
|
|
|
if (parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mAlreadyComplainedAboutCharset = true;
|
|
|
|
nsContentUtils::ReportToConsole(aError ? nsIScriptError::errorFlag
|
|
|
|
: nsIScriptError::warningFlag,
|
2013-08-21 23:28:26 +04:00
|
|
|
NS_LITERAL_CSTRING("HTML parser"),
|
2012-03-24 15:34:42 +04:00
|
|
|
mDocument,
|
|
|
|
nsContentUtils::eHTMLPARSER_PROPERTIES,
|
|
|
|
aMsgId,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr,
|
2012-03-24 15:34:42 +04:00
|
|
|
0,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr,
|
2012-03-24 15:34:42 +04:00
|
|
|
EmptyString(),
|
|
|
|
aLineNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::ComplainAboutBogusProtocolCharset(nsIDocument* aDoc)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mAlreadyComplainedAboutCharset,
|
|
|
|
"How come we already managed to complain?");
|
|
|
|
mAlreadyComplainedAboutCharset = true;
|
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::errorFlag,
|
2013-08-21 23:28:26 +04:00
|
|
|
NS_LITERAL_CSTRING("HTML parser"),
|
2012-03-24 15:34:42 +04:00
|
|
|
aDoc,
|
|
|
|
nsContentUtils::eHTMLPARSER_PROPERTIES,
|
|
|
|
"EncProtocolUnsupported");
|
|
|
|
}
|
|
|
|
|
2011-09-04 14:49:29 +04:00
|
|
|
nsHtml5Parser*
|
|
|
|
nsHtml5TreeOpExecutor::GetParser()
|
|
|
|
{
|
2012-01-20 16:03:49 +04:00
|
|
|
MOZ_ASSERT(!mRunsToCompletion);
|
2011-09-04 14:49:29 +04:00
|
|
|
return static_cast<nsHtml5Parser*>(mParser.get());
|
|
|
|
}
|
|
|
|
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHtml5TreeOpExecutor::InitializeDocWriteParserState(nsAHtml5TreeBuilderState* aState, int32_t aLine)
|
2009-09-21 17:18:20 +04:00
|
|
|
{
|
2011-09-04 14:49:29 +04:00
|
|
|
GetParser()->InitializeDocWriteParserState(aState, aLine);
|
2009-09-18 13:21:47 +04:00
|
|
|
}
|
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
nsIURI*
|
|
|
|
nsHtml5TreeOpExecutor::GetViewSourceBaseURI()
|
|
|
|
{
|
|
|
|
if (!mViewSourceBaseURI) {
|
2014-02-06 18:46:30 +04:00
|
|
|
|
2014-02-20 17:43:53 +04:00
|
|
|
// We query the channel for the baseURI because in certain situations it
|
|
|
|
// cannot otherwise be determined. If this process fails, fall back to the
|
|
|
|
// standard method.
|
2014-02-06 18:46:30 +04:00
|
|
|
nsCOMPtr<nsIViewSourceChannel> vsc;
|
|
|
|
vsc = do_QueryInterface(mDocument->GetChannel());
|
|
|
|
if (vsc) {
|
2014-02-20 17:43:53 +04:00
|
|
|
nsresult rv = vsc->GetBaseURI(getter_AddRefs(mViewSourceBaseURI));
|
|
|
|
if (NS_SUCCEEDED(rv) && mViewSourceBaseURI) {
|
|
|
|
return mViewSourceBaseURI;
|
2014-02-06 18:46:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-30 14:15:38 +04:00
|
|
|
nsCOMPtr<nsIURI> orig = mDocument->GetOriginalURI();
|
|
|
|
bool isViewSource;
|
|
|
|
orig->SchemeIs("view-source", &isViewSource);
|
|
|
|
if (isViewSource) {
|
|
|
|
nsCOMPtr<nsINestedURI> nested = do_QueryInterface(orig);
|
|
|
|
NS_ASSERTION(nested, "URI with scheme view-source didn't QI to nested!");
|
|
|
|
nested->GetInnerURI(getter_AddRefs(mViewSourceBaseURI));
|
|
|
|
} else {
|
2011-11-01 15:33:11 +04:00
|
|
|
// Fail gracefully if the base URL isn't a view-source: URL.
|
|
|
|
// Not sure if this can ever happen.
|
2010-07-30 14:15:38 +04:00
|
|
|
mViewSourceBaseURI = orig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mViewSourceBaseURI;
|
|
|
|
}
|
|
|
|
|
2012-03-27 10:27:45 +04:00
|
|
|
//static
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::InitializeStatics()
|
|
|
|
{
|
|
|
|
mozilla::Preferences::AddBoolVarCache(&sExternalViewSource,
|
|
|
|
"view_source.editor.external");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsHtml5TreeOpExecutor::IsExternalViewSource()
|
|
|
|
{
|
|
|
|
if (!sExternalViewSource) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
bool isViewSource = false;
|
|
|
|
if (mDocumentURI) {
|
|
|
|
mDocumentURI->SchemeIs("view-source", &isViewSource);
|
|
|
|
}
|
|
|
|
return isViewSource;
|
|
|
|
}
|
|
|
|
|
2010-02-26 12:18:38 +03:00
|
|
|
// Speculative loading
|
|
|
|
|
|
|
|
already_AddRefed<nsIURI>
|
|
|
|
nsHtml5TreeOpExecutor::ConvertIfNotPreloadedYet(const nsAString& aURL)
|
|
|
|
{
|
2011-09-19 18:37:04 +04:00
|
|
|
if (aURL.IsEmpty()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-09-19 18:37:04 +04:00
|
|
|
}
|
2010-07-26 15:18:43 +04:00
|
|
|
// The URL of the document without <base>
|
|
|
|
nsIURI* documentURI = mDocument->GetDocumentURI();
|
|
|
|
// The URL of the document with non-speculative <base>
|
|
|
|
nsIURI* documentBaseURI = mDocument->GetDocBaseURI();
|
|
|
|
|
|
|
|
// If the two above are different, use documentBaseURI. If they are the
|
|
|
|
// same, the document object isn't aware of a <base>, so attempt to use the
|
|
|
|
// mSpeculationBaseURI or, failing, that, documentURI.
|
|
|
|
nsIURI* base = (documentURI == documentBaseURI) ?
|
|
|
|
(mSpeculationBaseURI ?
|
|
|
|
mSpeculationBaseURI.get() : documentURI)
|
|
|
|
: documentBaseURI;
|
2010-02-26 12:18:38 +03:00
|
|
|
const nsCString& charset = mDocument->GetDocumentCharacterSet();
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uri), aURL, charset.get(), base);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to create a URI");
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2010-02-26 12:18:38 +03:00
|
|
|
uri->GetSpec(spec);
|
|
|
|
if (mPreloadedURLs.Contains(spec)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
2011-11-09 00:22:47 +04:00
|
|
|
mPreloadedURLs.PutEntry(spec);
|
2011-09-04 14:49:29 +04:00
|
|
|
return uri.forget();
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::PreloadScript(const nsAString& aURL,
|
|
|
|
const nsAString& aCharset,
|
2012-03-10 20:13:52 +04:00
|
|
|
const nsAString& aType,
|
2012-12-05 03:06:29 +04:00
|
|
|
const nsAString& aCrossOrigin,
|
|
|
|
bool aScriptFromHead)
|
2010-02-26 12:18:38 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri = ConvertIfNotPreloadedYet(aURL);
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
2012-12-05 03:06:29 +04:00
|
|
|
mDocument->ScriptLoader()->PreloadURI(uri, aCharset, aType, aCrossOrigin,
|
|
|
|
aScriptFromHead);
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::PreloadStyle(const nsAString& aURL,
|
2012-08-28 21:10:08 +04:00
|
|
|
const nsAString& aCharset,
|
|
|
|
const nsAString& aCrossOrigin)
|
2010-02-26 12:18:38 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri = ConvertIfNotPreloadedYet(aURL);
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-28 21:10:08 +04:00
|
|
|
mDocument->PreloadStyle(uri, aCharset, aCrossOrigin);
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-07-29 22:30:00 +04:00
|
|
|
nsHtml5TreeOpExecutor::PreloadImage(const nsAString& aURL,
|
|
|
|
const nsAString& aCrossOrigin)
|
2010-02-26 12:18:38 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri = ConvertIfNotPreloadedYet(aURL);
|
|
|
|
if (!uri) {
|
|
|
|
return;
|
|
|
|
}
|
2011-07-29 22:30:00 +04:00
|
|
|
mDocument->MaybePreLoadImage(uri, aCrossOrigin);
|
2010-02-26 12:18:38 +03:00
|
|
|
}
|
|
|
|
|
2010-07-26 15:18:43 +04:00
|
|
|
void
|
|
|
|
nsHtml5TreeOpExecutor::SetSpeculationBase(const nsAString& aURL)
|
|
|
|
{
|
|
|
|
if (mSpeculationBaseURI) {
|
|
|
|
// the first one wins
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const nsCString& charset = mDocument->GetDocumentCharacterSet();
|
2011-09-04 14:49:29 +04:00
|
|
|
DebugOnly<nsresult> rv = NS_NewURI(getter_AddRefs(mSpeculationBaseURI), aURL,
|
|
|
|
charset.get(), mDocument->GetDocumentURI());
|
|
|
|
NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Failed to create a URI");
|
2010-07-26 15:18:43 +04:00
|
|
|
}
|
|
|
|
|
2010-01-14 16:58:27 +03:00
|
|
|
#ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t nsHtml5TreeOpExecutor::sAppendBatchMaxSize = 0;
|
|
|
|
uint32_t nsHtml5TreeOpExecutor::sAppendBatchSlotsExamined = 0;
|
|
|
|
uint32_t nsHtml5TreeOpExecutor::sAppendBatchExaminations = 0;
|
|
|
|
uint32_t nsHtml5TreeOpExecutor::sLongestTimeOffTheEventLoop = 0;
|
|
|
|
uint32_t nsHtml5TreeOpExecutor::sTimesFlushLoopInterrupted = 0;
|
2009-09-18 13:21:47 +04:00
|
|
|
#endif
|
2012-03-27 10:27:45 +04:00
|
|
|
bool nsHtml5TreeOpExecutor::sExternalViewSource = false;
|