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/core/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/frame/LocalFrame.h"
37 #include "core/html/parser/HTMLInputStream.h"
38 #include "core/html/parser/HTMLScriptRunnerHost.h"
39 #include "core/html/parser/NestingLevelIncrementer.h"
40 #include "platform/NotImplemented.h"
41 #include "public/platform/Platform.h"
45 using namespace HTMLNames;
47 HTMLScriptRunner::HTMLScriptRunner(Document* document, HTMLScriptRunnerHost* host)
48 : m_document(document)
50 , m_scriptNestingLevel(0)
51 , m_hasScriptsWaitingForResources(false)
52 , m_parserBlockingScriptAlreadyLoaded(false)
57 HTMLScriptRunner::~HTMLScriptRunner()
60 // If the document is destructed without having explicitly
61 // detached the parser (and this script runner object), perform
62 // detach steps now. This will happen if the Document, the parser
63 // and this script runner object are swept out in the same GC.
66 // Verify that detach() has been called.
71 void HTMLScriptRunner::detach()
76 m_parserBlockingScript.stopWatchingForLoad(this);
78 while (!m_scriptsToExecuteAfterParsing.isEmpty()) {
79 PendingScript pendingScript = m_scriptsToExecuteAfterParsing.takeFirst();
80 pendingScript.stopWatchingForLoad(this);
85 static KURL documentURLForScriptExecution(Document* document)
90 if (!document->frame()) {
91 if (document->importsController())
92 return document->url();
96 // Use the URL of the currently active document for this frame.
97 return document->frame()->document()->url();
100 inline PassRefPtrWillBeRawPtr<Event> createScriptLoadEvent()
102 return Event::create(EventTypeNames::load);
105 bool HTMLScriptRunner::isPendingScriptReady(const PendingScript& script)
107 m_hasScriptsWaitingForResources = !m_document->isScriptExecutionReady();
108 if (m_hasScriptsWaitingForResources)
110 return script.isReady();
113 void HTMLScriptRunner::executeParsingBlockingScript()
116 ASSERT(!isExecutingScript());
117 ASSERT(m_document->isScriptExecutionReady());
118 ASSERT(isPendingScriptReady(m_parserBlockingScript));
120 InsertionPointRecord insertionPointRecord(m_host->inputStream());
121 executePendingScriptAndDispatchEvent(m_parserBlockingScript, PendingScript::ParsingBlocking);
124 void HTMLScriptRunner::executePendingScriptAndDispatchEvent(PendingScript& pendingScript, PendingScript::Type pendingScriptType)
126 bool errorOccurred = false;
127 double loadFinishTime = pendingScript.resource() && pendingScript.resource()->url().protocolIsInHTTPFamily() ? pendingScript.resource()->loadFinishTime() : 0;
128 ScriptSourceCode sourceCode = pendingScript.getSource(documentURLForScriptExecution(m_document), errorOccurred);
130 // Stop watching loads before executeScript to prevent recursion if the script reloads itself.
131 pendingScript.stopWatchingForLoad(this);
133 if (!isExecutingScript()) {
134 Microtask::performCheckpoint();
135 if (pendingScriptType == PendingScript::ParsingBlocking) {
136 m_hasScriptsWaitingForResources = !m_document->isScriptExecutionReady();
137 // The parser cannot be unblocked as a microtask requested another resource
138 if (m_hasScriptsWaitingForResources)
143 // Clear the pending script before possible rentrancy from executeScript()
144 RefPtrWillBeRawPtr<Element> element = pendingScript.releaseElementAndClear();
145 double compilationFinishTime = 0;
146 if (ScriptLoader* scriptLoader = toScriptLoaderIfPossible(element.get())) {
147 NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel);
148 IgnoreDestructiveWriteCountIncrementer ignoreDestructiveWriteCountIncrementer(m_document);
150 scriptLoader->dispatchErrorEvent();
152 ASSERT(isExecutingScript());
153 scriptLoader->executeScript(sourceCode, &compilationFinishTime);
154 element->dispatchEvent(createScriptLoadEvent());
157 // The exact value doesn't matter; valid time stamps are much bigger than this value.
158 const double epsilon = 1;
159 if (pendingScriptType == PendingScript::ParsingBlocking && !m_parserBlockingScriptAlreadyLoaded && compilationFinishTime > epsilon && loadFinishTime > epsilon) {
160 blink::Platform::current()->histogramCustomCounts("WebCore.Scripts.ParsingBlocking.TimeBetweenLoadedAndCompiled", (compilationFinishTime - loadFinishTime) * 1000, 0, 10000, 50);
163 ASSERT(!isExecutingScript());
166 void HTMLScriptRunner::notifyFinished(Resource* cachedResource)
168 m_host->notifyScriptLoaded(cachedResource);
171 // Implements the steps for 'An end tag whose tag name is "script"'
172 // http://whatwg.org/html#scriptEndTag
173 // Script handling lives outside the tree builder to keep each class simple.
174 void HTMLScriptRunner::execute(PassRefPtrWillBeRawPtr<Element> scriptElement, const TextPosition& scriptStartPosition)
176 ASSERT(scriptElement);
177 // FIXME: If scripting is disabled, always just return.
179 bool hadPreloadScanner = m_host->hasPreloadScanner();
181 // Try to execute the script given to us.
182 runScript(scriptElement.get(), scriptStartPosition);
184 if (hasParserBlockingScript()) {
185 if (isExecutingScript())
186 return; // Unwind to the outermost HTMLScriptRunner::execute before continuing parsing.
187 // If preload scanner got created, it is missing the source after the current insertion point. Append it and scan.
188 if (!hadPreloadScanner && m_host->hasPreloadScanner())
189 m_host->appendCurrentInputStreamToPreloadScannerAndScan();
190 executeParsingBlockingScripts();
194 bool HTMLScriptRunner::hasParserBlockingScript() const
196 return !!m_parserBlockingScript.element();
199 void HTMLScriptRunner::executeParsingBlockingScripts()
201 while (hasParserBlockingScript() && isPendingScriptReady(m_parserBlockingScript))
202 executeParsingBlockingScript();
205 void HTMLScriptRunner::executeScriptsWaitingForLoad(Resource* resource)
207 ASSERT(!isExecutingScript());
208 ASSERT(hasParserBlockingScript());
209 ASSERT_UNUSED(resource, m_parserBlockingScript.resource() == resource);
210 ASSERT(m_parserBlockingScript.isReady());
211 executeParsingBlockingScripts();
214 void HTMLScriptRunner::executeScriptsWaitingForResources()
217 // Callers should check hasScriptsWaitingForResources() before calling
218 // to prevent parser or script re-entry during </style> parsing.
219 ASSERT(hasScriptsWaitingForResources());
220 ASSERT(!isExecutingScript());
221 ASSERT(m_document->isScriptExecutionReady());
222 executeParsingBlockingScripts();
225 bool HTMLScriptRunner::executeScriptsWaitingForParsing()
227 while (!m_scriptsToExecuteAfterParsing.isEmpty()) {
228 ASSERT(!isExecutingScript());
229 ASSERT(!hasParserBlockingScript());
230 ASSERT(m_scriptsToExecuteAfterParsing.first().resource());
231 if (!m_scriptsToExecuteAfterParsing.first().isReady()) {
232 m_scriptsToExecuteAfterParsing.first().watchForLoad(this);
235 PendingScript first = m_scriptsToExecuteAfterParsing.takeFirst();
236 executePendingScriptAndDispatchEvent(first, PendingScript::Deferred);
237 // FIXME: What is this m_document check for?
244 void HTMLScriptRunner::requestParsingBlockingScript(Element* element)
246 if (!requestPendingScript(m_parserBlockingScript, element))
249 ASSERT(m_parserBlockingScript.resource());
251 // Exclude already loaded resources (from memory cache) and reloads from the
253 // WebCore.Scripts.ParsingBlocking.TimeBetweenLoadedAndCompiled (done after
254 // the script is compiled).
255 m_parserBlockingScriptAlreadyLoaded = m_parserBlockingScript.resource()->isLoaded() || m_parserBlockingScript.resource()->resourceToRevalidate();
256 blink::Platform::current()->histogramEnumeration("WebCore.Scripts.ParsingBlocking.AlreadyLoaded", m_parserBlockingScriptAlreadyLoaded ? 1 : 0, 2);
258 // We only care about a load callback if resource is not already
259 // in the cache. Callers will attempt to run the m_parserBlockingScript
260 // if possible before returning control to the parser.
261 if (!m_parserBlockingScript.isReady()) {
262 if (m_document->frame())
263 ScriptStreamer::startStreaming(m_parserBlockingScript, m_document->frame()->settings(), ScriptState::forMainWorld(m_document->frame()), PendingScript::ParsingBlocking);
264 m_parserBlockingScript.watchForLoad(this);
268 void HTMLScriptRunner::requestDeferredScript(Element* element)
270 PendingScript pendingScript;
271 if (!requestPendingScript(pendingScript, element))
274 ASSERT(pendingScript.resource());
275 if (m_document->frame() && !pendingScript.isReady())
276 ScriptStreamer::startStreaming(pendingScript, m_document->frame()->settings(), ScriptState::forMainWorld(m_document->frame()), PendingScript::Deferred);
278 m_scriptsToExecuteAfterParsing.append(pendingScript);
281 bool HTMLScriptRunner::requestPendingScript(PendingScript& pendingScript, Element* script) const
283 ASSERT(!pendingScript.element());
284 pendingScript.setElement(script);
285 // This should correctly return 0 for empty or invalid srcValues.
286 ScriptResource* resource = toScriptLoaderIfPossible(script)->resource().get();
288 notImplemented(); // Dispatch error event.
291 pendingScript.setScriptResource(resource);
295 // Implements the initial steps for 'An end tag whose tag name is "script"'
296 // http://whatwg.org/html#scriptEndTag
297 void HTMLScriptRunner::runScript(Element* script, const TextPosition& scriptStartPosition)
300 ASSERT(!hasParserBlockingScript());
302 ScriptLoader* scriptLoader = toScriptLoaderIfPossible(script);
304 // This contains both and ASSERTION and a null check since we should not
305 // be getting into the case of a null script element, but seem to be from
306 // time to time. The assertion is left in to help find those cases and
307 // is being tracked by <https://bugs.webkit.org/show_bug.cgi?id=60559>.
308 ASSERT(scriptLoader);
312 ASSERT(scriptLoader->isParserInserted());
314 if (!isExecutingScript())
315 Microtask::performCheckpoint();
317 InsertionPointRecord insertionPointRecord(m_host->inputStream());
318 NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel);
320 scriptLoader->prepareScript(scriptStartPosition);
322 if (!scriptLoader->willBeParserExecuted())
325 if (scriptLoader->willExecuteWhenDocumentFinishedParsing()) {
326 requestDeferredScript(script);
327 } else if (scriptLoader->readyToBeParserExecuted()) {
328 if (m_scriptNestingLevel == 1) {
329 m_parserBlockingScript.setElement(script);
330 m_parserBlockingScript.setStartingPosition(scriptStartPosition);
332 ScriptSourceCode sourceCode(script->textContent(), documentURLForScriptExecution(m_document), scriptStartPosition);
333 scriptLoader->executeScript(sourceCode);
336 requestParsingBlockingScript(script);
341 void HTMLScriptRunner::trace(Visitor* visitor)
343 visitor->trace(m_document);
344 visitor->trace(m_host);
345 visitor->trace(m_parserBlockingScript);
346 visitor->trace(m_scriptsToExecuteAfterParsing);