This source file includes following definitions.
- m_hasScriptsWaitingForResources
- detach
- documentURLForScriptExecution
- createScriptLoadEvent
- sourceFromPendingScript
- isPendingScriptReady
- executeParsingBlockingScript
- executePendingScriptAndDispatchEvent
- watchForLoad
- stopWatchingForLoad
- execute
- hasParserBlockingScript
- executeParsingBlockingScripts
- executeScriptsWaitingForLoad
- executeScriptsWaitingForResources
- executeScriptsWaitingForParsing
- requestParsingBlockingScript
- requestDeferredScript
- requestPendingScript
- runScript
#include "config.h"
#include "core/html/parser/HTMLScriptRunner.h"
#include "bindings/v8/ScriptSourceCode.h"
#include "core/dom/Element.h"
#include "core/events/Event.h"
#include "core/dom/IgnoreDestructiveWriteCountIncrementer.h"
#include "core/dom/Microtask.h"
#include "core/dom/ScriptLoader.h"
#include "core/fetch/ScriptResource.h"
#include "core/frame/LocalFrame.h"
#include "core/html/parser/HTMLInputStream.h"
#include "core/html/parser/HTMLScriptRunnerHost.h"
#include "core/html/parser/NestingLevelIncrementer.h"
#include "platform/NotImplemented.h"
namespace WebCore {
using namespace HTMLNames;
HTMLScriptRunner::HTMLScriptRunner(Document* document, HTMLScriptRunnerHost* host)
: m_document(document)
, m_host(host)
, m_scriptNestingLevel(0)
, m_hasScriptsWaitingForResources(false)
{
ASSERT(m_host);
}
HTMLScriptRunner::~HTMLScriptRunner()
{
if (m_parserBlockingScript.resource() && m_parserBlockingScript.watchingForLoad())
stopWatchingForLoad(m_parserBlockingScript);
while (!m_scriptsToExecuteAfterParsing.isEmpty()) {
PendingScript pendingScript = m_scriptsToExecuteAfterParsing.takeFirst();
if (pendingScript.resource() && pendingScript.watchingForLoad())
stopWatchingForLoad(pendingScript);
}
}
void HTMLScriptRunner::detach()
{
m_document = 0;
}
static KURL documentURLForScriptExecution(Document* document)
{
if (!document)
return KURL();
if (!document->frame()) {
if (document->import())
return document->url();
return KURL();
}
return document->frame()->document()->url();
}
inline PassRefPtrWillBeRawPtr<Event> createScriptLoadEvent()
{
return Event::create(EventTypeNames::load);
}
ScriptSourceCode HTMLScriptRunner::sourceFromPendingScript(const PendingScript& script, bool& errorOccurred) const
{
if (script.resource()) {
errorOccurred = script.resource()->errorOccurred();
ASSERT(script.resource()->isLoaded());
return ScriptSourceCode(script.resource());
}
errorOccurred = false;
return ScriptSourceCode(script.element()->textContent(), documentURLForScriptExecution(m_document), script.startingPosition());
}
bool HTMLScriptRunner::isPendingScriptReady(const PendingScript& script)
{
m_hasScriptsWaitingForResources = !m_document->haveStylesheetsAndImportsLoaded();
if (m_hasScriptsWaitingForResources)
return false;
if (script.resource() && !script.resource()->isLoaded())
return false;
return true;
}
void HTMLScriptRunner::executeParsingBlockingScript()
{
ASSERT(m_document);
ASSERT(!isExecutingScript());
ASSERT(m_document->haveStylesheetsAndImportsLoaded());
ASSERT(isPendingScriptReady(m_parserBlockingScript));
InsertionPointRecord insertionPointRecord(m_host->inputStream());
executePendingScriptAndDispatchEvent(m_parserBlockingScript, PendingScriptBlockingParser);
}
void HTMLScriptRunner::executePendingScriptAndDispatchEvent(PendingScript& pendingScript, PendingScriptType pendingScriptType)
{
bool errorOccurred = false;
ScriptSourceCode sourceCode = sourceFromPendingScript(pendingScript, errorOccurred);
if (pendingScript.resource() && pendingScript.watchingForLoad())
stopWatchingForLoad(pendingScript);
if (!isExecutingScript()) {
Microtask::performCheckpoint();
if (pendingScriptType == PendingScriptBlockingParser) {
m_hasScriptsWaitingForResources = !m_document->haveStylesheetsAndImportsLoaded();
if (m_hasScriptsWaitingForResources)
return;
}
}
RefPtr<Element> element = pendingScript.releaseElementAndClear();
if (ScriptLoader* scriptLoader = toScriptLoaderIfPossible(element.get())) {
NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel);
IgnoreDestructiveWriteCountIncrementer ignoreDestructiveWriteCountIncrementer(m_document);
if (errorOccurred)
scriptLoader->dispatchErrorEvent();
else {
ASSERT(isExecutingScript());
scriptLoader->executeScript(sourceCode);
element->dispatchEvent(createScriptLoadEvent());
}
}
ASSERT(!isExecutingScript());
}
void HTMLScriptRunner::watchForLoad(PendingScript& pendingScript)
{
ASSERT(!pendingScript.watchingForLoad());
m_host->watchForLoad(pendingScript.resource());
pendingScript.setWatchingForLoad(true);
}
void HTMLScriptRunner::stopWatchingForLoad(PendingScript& pendingScript)
{
ASSERT(pendingScript.watchingForLoad());
m_host->stopWatchingForLoad(pendingScript.resource());
pendingScript.setWatchingForLoad(false);
}
void HTMLScriptRunner::execute(PassRefPtr<Element> scriptElement, const TextPosition& scriptStartPosition)
{
ASSERT(scriptElement);
bool hadPreloadScanner = m_host->hasPreloadScanner();
runScript(scriptElement.get(), scriptStartPosition);
if (hasParserBlockingScript()) {
if (isExecutingScript())
return;
if (!hadPreloadScanner && m_host->hasPreloadScanner())
m_host->appendCurrentInputStreamToPreloadScannerAndScan();
executeParsingBlockingScripts();
}
}
bool HTMLScriptRunner::hasParserBlockingScript() const
{
return !!m_parserBlockingScript.element();
}
void HTMLScriptRunner::executeParsingBlockingScripts()
{
while (hasParserBlockingScript() && isPendingScriptReady(m_parserBlockingScript))
executeParsingBlockingScript();
}
void HTMLScriptRunner::executeScriptsWaitingForLoad(Resource* resource)
{
ASSERT(!isExecutingScript());
ASSERT(hasParserBlockingScript());
ASSERT_UNUSED(resource, m_parserBlockingScript.resource() == resource);
ASSERT(m_parserBlockingScript.resource()->isLoaded());
executeParsingBlockingScripts();
}
void HTMLScriptRunner::executeScriptsWaitingForResources()
{
ASSERT(m_document);
ASSERT(hasScriptsWaitingForResources());
ASSERT(!isExecutingScript());
ASSERT(m_document->haveStylesheetsAndImportsLoaded());
executeParsingBlockingScripts();
}
bool HTMLScriptRunner::executeScriptsWaitingForParsing()
{
while (!m_scriptsToExecuteAfterParsing.isEmpty()) {
ASSERT(!isExecutingScript());
ASSERT(!hasParserBlockingScript());
ASSERT(m_scriptsToExecuteAfterParsing.first().resource());
if (!m_scriptsToExecuteAfterParsing.first().resource()->isLoaded()) {
watchForLoad(m_scriptsToExecuteAfterParsing.first());
return false;
}
PendingScript first = m_scriptsToExecuteAfterParsing.takeFirst();
executePendingScriptAndDispatchEvent(first, PendingScriptDeferred);
if (!m_document)
return false;
}
return true;
}
void HTMLScriptRunner::requestParsingBlockingScript(Element* element)
{
if (!requestPendingScript(m_parserBlockingScript, element))
return;
ASSERT(m_parserBlockingScript.resource());
if (!m_parserBlockingScript.resource()->isLoaded())
watchForLoad(m_parserBlockingScript);
}
void HTMLScriptRunner::requestDeferredScript(Element* element)
{
PendingScript pendingScript;
if (!requestPendingScript(pendingScript, element))
return;
ASSERT(pendingScript.resource());
m_scriptsToExecuteAfterParsing.append(pendingScript);
}
bool HTMLScriptRunner::requestPendingScript(PendingScript& pendingScript, Element* script) const
{
ASSERT(!pendingScript.element());
pendingScript.setElement(script);
ScriptResource* resource = toScriptLoaderIfPossible(script)->resource().get();
if (!resource) {
notImplemented();
return false;
}
pendingScript.setScriptResource(resource);
return true;
}
void HTMLScriptRunner::runScript(Element* script, const TextPosition& scriptStartPosition)
{
ASSERT(m_document);
ASSERT(!hasParserBlockingScript());
{
ScriptLoader* scriptLoader = toScriptLoaderIfPossible(script);
ASSERT(scriptLoader);
if (!scriptLoader)
return;
ASSERT(scriptLoader->isParserInserted());
if (!isExecutingScript())
Microtask::performCheckpoint();
InsertionPointRecord insertionPointRecord(m_host->inputStream());
NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel);
scriptLoader->prepareScript(scriptStartPosition);
if (!scriptLoader->willBeParserExecuted())
return;
if (scriptLoader->willExecuteWhenDocumentFinishedParsing()) {
requestDeferredScript(script);
} else if (scriptLoader->readyToBeParserExecuted()) {
if (m_scriptNestingLevel == 1) {
m_parserBlockingScript.setElement(script);
m_parserBlockingScript.setStartingPosition(scriptStartPosition);
} else {
ScriptSourceCode sourceCode(script->textContent(), documentURLForScriptExecution(m_document), scriptStartPosition);
scriptLoader->executeScript(sourceCode);
}
} else {
requestParsingBlockingScript(script);
}
}
}
}