2 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "core/html/parser/HTMLScriptRunner.h"
29 #include "bindings/v8/ScriptSourceCode.h"
30 #include "core/dom/Element.h"
31 #include "core/events/Event.h"
32 #include "core/dom/IgnoreDestructiveWriteCountIncrementer.h"
33 #include "core/dom/Microtask.h"
34 #include "core/dom/ScriptLoader.h"
35 #include "core/fetch/ScriptResource.h"
36 #include "core/html/parser/HTMLInputStream.h"
37 #include "core/html/parser/HTMLScriptRunnerHost.h"
38 #include "core/html/parser/NestingLevelIncrementer.h"
39 #include "core/frame/Frame.h"
40 #include "platform/NotImplemented.h"
44 using namespace HTMLNames;
46 HTMLScriptRunner::HTMLScriptRunner(Document* document, HTMLScriptRunnerHost* host)
47 : m_document(document)
49 , m_scriptNestingLevel(0)
50 , m_hasScriptsWaitingForResources(false)
55 HTMLScriptRunner::~HTMLScriptRunner()
57 // FIXME: Should we be passed a "done loading/parsing" callback sooner than destruction?
58 if (m_parserBlockingScript.resource() && m_parserBlockingScript.watchingForLoad())
59 stopWatchingForLoad(m_parserBlockingScript);
61 while (!m_scriptsToExecuteAfterParsing.isEmpty()) {
62 PendingScript pendingScript = m_scriptsToExecuteAfterParsing.takeFirst();
63 if (pendingScript.resource() && pendingScript.watchingForLoad())
64 stopWatchingForLoad(pendingScript);
68 void HTMLScriptRunner::detach()
73 static KURL documentURLForScriptExecution(Document* document)
75 if (!document || !document->frame())
78 // Use the URL of the currently active document for this frame.
79 return document->frame()->document()->url();
82 inline PassRefPtr<Event> createScriptLoadEvent()
84 return Event::create(EventTypeNames::load);
87 ScriptSourceCode HTMLScriptRunner::sourceFromPendingScript(const PendingScript& script, bool& errorOccurred) const
89 if (script.resource()) {
90 errorOccurred = script.resource()->errorOccurred();
91 ASSERT(script.resource()->isLoaded());
92 return ScriptSourceCode(script.resource());
94 errorOccurred = false;
95 return ScriptSourceCode(script.element()->textContent(), documentURLForScriptExecution(m_document), script.startingPosition());
98 bool HTMLScriptRunner::isPendingScriptReady(const PendingScript& script)
100 m_hasScriptsWaitingForResources = !m_document->haveStylesheetsAndImportsLoaded();
101 if (m_hasScriptsWaitingForResources)
103 if (script.resource() && !script.resource()->isLoaded())
108 void HTMLScriptRunner::executeParsingBlockingScript()
111 ASSERT(!isExecutingScript());
112 ASSERT(m_document->haveStylesheetsAndImportsLoaded());
113 ASSERT(isPendingScriptReady(m_parserBlockingScript));
115 InsertionPointRecord insertionPointRecord(m_host->inputStream());
116 executePendingScriptAndDispatchEvent(m_parserBlockingScript);
119 void HTMLScriptRunner::executePendingScriptAndDispatchEvent(PendingScript& pendingScript)
121 bool errorOccurred = false;
122 ScriptSourceCode sourceCode = sourceFromPendingScript(pendingScript, errorOccurred);
124 // Stop watching loads before executeScript to prevent recursion if the script reloads itself.
125 if (pendingScript.resource() && pendingScript.watchingForLoad())
126 stopWatchingForLoad(pendingScript);
128 if (!isExecutingScript())
129 Microtask::performCheckpoint();
131 // Clear the pending script before possible rentrancy from executeScript()
132 RefPtr<Element> element = pendingScript.releaseElementAndClear();
133 if (ScriptLoader* scriptLoader = toScriptLoaderIfPossible(element.get())) {
134 NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel);
135 IgnoreDestructiveWriteCountIncrementer ignoreDestructiveWriteCountIncrementer(m_document);
137 scriptLoader->dispatchErrorEvent();
139 ASSERT(isExecutingScript());
140 scriptLoader->executeScript(sourceCode);
141 element->dispatchEvent(createScriptLoadEvent());
144 ASSERT(!isExecutingScript());
147 void HTMLScriptRunner::watchForLoad(PendingScript& pendingScript)
149 ASSERT(!pendingScript.watchingForLoad());
150 m_host->watchForLoad(pendingScript.resource());
151 pendingScript.setWatchingForLoad(true);
154 void HTMLScriptRunner::stopWatchingForLoad(PendingScript& pendingScript)
156 ASSERT(pendingScript.watchingForLoad());
157 m_host->stopWatchingForLoad(pendingScript.resource());
158 pendingScript.setWatchingForLoad(false);
161 // Implements the steps for 'An end tag whose tag name is "script"'
162 // http://whatwg.org/html#scriptEndTag
163 // Script handling lives outside the tree builder to keep each class simple.
164 void HTMLScriptRunner::execute(PassRefPtr<Element> scriptElement, const TextPosition& scriptStartPosition)
166 ASSERT(scriptElement);
167 // FIXME: If scripting is disabled, always just return.
169 bool hadPreloadScanner = m_host->hasPreloadScanner();
171 // Try to execute the script given to us.
172 runScript(scriptElement.get(), scriptStartPosition);
174 if (hasParserBlockingScript()) {
175 if (isExecutingScript())
176 return; // Unwind to the outermost HTMLScriptRunner::execute before continuing parsing.
177 // If preload scanner got created, it is missing the source after the current insertion point. Append it and scan.
178 if (!hadPreloadScanner && m_host->hasPreloadScanner())
179 m_host->appendCurrentInputStreamToPreloadScannerAndScan();
180 executeParsingBlockingScripts();
184 bool HTMLScriptRunner::hasParserBlockingScript() const
186 return !!m_parserBlockingScript.element();
189 void HTMLScriptRunner::executeParsingBlockingScripts()
191 while (hasParserBlockingScript() && isPendingScriptReady(m_parserBlockingScript))
192 executeParsingBlockingScript();
195 void HTMLScriptRunner::executeScriptsWaitingForLoad(Resource* resource)
197 ASSERT(!isExecutingScript());
198 ASSERT(hasParserBlockingScript());
199 ASSERT_UNUSED(resource, m_parserBlockingScript.resource() == resource);
200 ASSERT(m_parserBlockingScript.resource()->isLoaded());
201 executeParsingBlockingScripts();
204 void HTMLScriptRunner::executeScriptsWaitingForResources()
207 // Callers should check hasScriptsWaitingForResources() before calling
208 // to prevent parser or script re-entry during </style> parsing.
209 ASSERT(hasScriptsWaitingForResources());
210 ASSERT(!isExecutingScript());
211 ASSERT(m_document->haveStylesheetsAndImportsLoaded());
212 executeParsingBlockingScripts();
215 bool HTMLScriptRunner::executeScriptsWaitingForParsing()
217 while (!m_scriptsToExecuteAfterParsing.isEmpty()) {
218 ASSERT(!isExecutingScript());
219 ASSERT(!hasParserBlockingScript());
220 ASSERT(m_scriptsToExecuteAfterParsing.first().resource());
221 if (!m_scriptsToExecuteAfterParsing.first().resource()->isLoaded()) {
222 watchForLoad(m_scriptsToExecuteAfterParsing.first());
225 PendingScript first = m_scriptsToExecuteAfterParsing.takeFirst();
226 executePendingScriptAndDispatchEvent(first);
227 // FIXME: What is this m_document check for?
234 void HTMLScriptRunner::requestParsingBlockingScript(Element* element)
236 if (!requestPendingScript(m_parserBlockingScript, element))
239 ASSERT(m_parserBlockingScript.resource());
241 // We only care about a load callback if resource is not already
242 // in the cache. Callers will attempt to run the m_parserBlockingScript
243 // if possible before returning control to the parser.
244 if (!m_parserBlockingScript.resource()->isLoaded())
245 watchForLoad(m_parserBlockingScript);
248 void HTMLScriptRunner::requestDeferredScript(Element* element)
250 PendingScript pendingScript;
251 if (!requestPendingScript(pendingScript, element))
254 ASSERT(pendingScript.resource());
255 m_scriptsToExecuteAfterParsing.append(pendingScript);
258 bool HTMLScriptRunner::requestPendingScript(PendingScript& pendingScript, Element* script) const
260 ASSERT(!pendingScript.element());
261 pendingScript.setElement(script);
262 // This should correctly return 0 for empty or invalid srcValues.
263 ScriptResource* resource = toScriptLoaderIfPossible(script)->resource().get();
265 notImplemented(); // Dispatch error event.
268 pendingScript.setScriptResource(resource);
272 // Implements the initial steps for 'An end tag whose tag name is "script"'
273 // http://whatwg.org/html#scriptEndTag
274 void HTMLScriptRunner::runScript(Element* script, const TextPosition& scriptStartPosition)
277 ASSERT(!hasParserBlockingScript());
279 ScriptLoader* scriptLoader = toScriptLoaderIfPossible(script);
281 // This contains both and ASSERTION and a null check since we should not
282 // be getting into the case of a null script element, but seem to be from
283 // time to time. The assertion is left in to help find those cases and
284 // is being tracked by <https://bugs.webkit.org/show_bug.cgi?id=60559>.
285 ASSERT(scriptLoader);
289 ASSERT(scriptLoader->isParserInserted());
291 if (!isExecutingScript())
292 Microtask::performCheckpoint();
294 InsertionPointRecord insertionPointRecord(m_host->inputStream());
295 NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel);
297 scriptLoader->prepareScript(scriptStartPosition);
299 if (!scriptLoader->willBeParserExecuted())
302 if (scriptLoader->willExecuteWhenDocumentFinishedParsing()) {
303 requestDeferredScript(script);
304 } else if (scriptLoader->readyToBeParserExecuted()) {
305 if (m_scriptNestingLevel == 1) {
306 m_parserBlockingScript.setElement(script);
307 m_parserBlockingScript.setStartingPosition(scriptStartPosition);
309 ScriptSourceCode sourceCode(script->textContent(), documentURLForScriptExecution(m_document), scriptStartPosition);
310 scriptLoader->executeScript(sourceCode);
313 requestParsingBlockingScript(script);