This source file includes following definitions.
- m_timer
- queueScriptForExecution
- suspend
- resume
- notifyScriptReady
- notifyScriptLoadError
- timerFired
#include "config.h"
#include "core/dom/ScriptRunner.h"
#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include "core/dom/PendingScript.h"
#include "core/dom/ScriptLoader.h"
#include "core/fetch/ScriptResource.h"
namespace WebCore {
ScriptRunner::ScriptRunner(Document* document)
: m_document(document)
, m_timer(this, &ScriptRunner::timerFired)
{
ASSERT(document);
}
ScriptRunner::~ScriptRunner()
{
for (size_t i = 0; i < m_scriptsToExecuteSoon.size(); ++i)
m_document->decrementLoadEventDelayCount();
for (size_t i = 0; i < m_scriptsToExecuteInOrder.size(); ++i)
m_document->decrementLoadEventDelayCount();
for (size_t i = 0; i < m_pendingAsyncScripts.size(); ++i)
m_document->decrementLoadEventDelayCount();
}
void ScriptRunner::queueScriptForExecution(ScriptLoader* scriptLoader, ResourcePtr<ScriptResource> resource, ExecutionType executionType)
{
ASSERT(scriptLoader);
ASSERT(resource.get());
Element* element = scriptLoader->element();
ASSERT(element);
ASSERT(element->inDocument());
m_document->incrementLoadEventDelayCount();
switch (executionType) {
case ASYNC_EXECUTION:
m_pendingAsyncScripts.add(scriptLoader, PendingScript(element, resource.get()));
break;
case IN_ORDER_EXECUTION:
m_scriptsToExecuteInOrder.append(PendingScript(element, resource.get()));
break;
}
}
void ScriptRunner::suspend()
{
m_timer.stop();
}
void ScriptRunner::resume()
{
if (hasPendingScripts())
m_timer.startOneShot(0, FROM_HERE);
}
void ScriptRunner::notifyScriptReady(ScriptLoader* scriptLoader, ExecutionType executionType)
{
switch (executionType) {
case ASYNC_EXECUTION:
ASSERT(m_pendingAsyncScripts.contains(scriptLoader));
m_scriptsToExecuteSoon.append(m_pendingAsyncScripts.take(scriptLoader));
break;
case IN_ORDER_EXECUTION:
ASSERT(!m_scriptsToExecuteInOrder.isEmpty());
break;
}
m_timer.startOneShot(0, FROM_HERE);
}
void ScriptRunner::notifyScriptLoadError(ScriptLoader* scriptLoader, ExecutionType executionType)
{
switch (executionType) {
case ASYNC_EXECUTION:
ASSERT(m_pendingAsyncScripts.contains(scriptLoader));
m_pendingAsyncScripts.remove(scriptLoader);
m_document->decrementLoadEventDelayCount();
break;
case IN_ORDER_EXECUTION:
ASSERT(!m_scriptsToExecuteInOrder.isEmpty());
break;
}
}
void ScriptRunner::timerFired(Timer<ScriptRunner>* timer)
{
ASSERT_UNUSED(timer, timer == &m_timer);
RefPtr<Document> protect(m_document);
Vector<PendingScript> scripts;
scripts.swap(m_scriptsToExecuteSoon);
size_t numInOrderScriptsToExecute = 0;
for (; numInOrderScriptsToExecute < m_scriptsToExecuteInOrder.size() && m_scriptsToExecuteInOrder[numInOrderScriptsToExecute].resource()->isLoaded(); ++numInOrderScriptsToExecute)
scripts.append(m_scriptsToExecuteInOrder[numInOrderScriptsToExecute]);
if (numInOrderScriptsToExecute)
m_scriptsToExecuteInOrder.remove(0, numInOrderScriptsToExecute);
size_t size = scripts.size();
for (size_t i = 0; i < size; ++i) {
ScriptResource* resource = scripts[i].resource();
RefPtr<Element> element = scripts[i].releaseElementAndClear();
toScriptLoaderIfPossible(element.get())->execute(resource);
m_document->decrementLoadEventDelayCount();
}
}
}