Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / parser / HTMLScriptRunner.cpp
1 /*
2  * Copyright (C) 2010 Google, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "core/html/parser/HTMLScriptRunner.h"
28
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"
41
42 namespace WebCore {
43
44 using namespace HTMLNames;
45
46 HTMLScriptRunner::HTMLScriptRunner(Document* document, HTMLScriptRunnerHost* host)
47     : m_document(document)
48     , m_host(host)
49     , m_scriptNestingLevel(0)
50     , m_hasScriptsWaitingForResources(false)
51 {
52     ASSERT(m_host);
53 }
54
55 HTMLScriptRunner::~HTMLScriptRunner()
56 {
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);
60
61     while (!m_scriptsToExecuteAfterParsing.isEmpty()) {
62         PendingScript pendingScript = m_scriptsToExecuteAfterParsing.takeFirst();
63         if (pendingScript.resource() && pendingScript.watchingForLoad())
64             stopWatchingForLoad(pendingScript);
65     }
66 }
67
68 void HTMLScriptRunner::detach()
69 {
70     m_document = 0;
71 }
72
73 static KURL documentURLForScriptExecution(Document* document)
74 {
75     if (!document || !document->frame())
76         return KURL();
77
78     // Use the URL of the currently active document for this frame.
79     return document->frame()->document()->url();
80 }
81
82 inline PassRefPtr<Event> createScriptLoadEvent()
83 {
84     return Event::create(EventTypeNames::load);
85 }
86
87 ScriptSourceCode HTMLScriptRunner::sourceFromPendingScript(const PendingScript& script, bool& errorOccurred) const
88 {
89     if (script.resource()) {
90         errorOccurred = script.resource()->errorOccurred();
91         ASSERT(script.resource()->isLoaded());
92         return ScriptSourceCode(script.resource());
93     }
94     errorOccurred = false;
95     return ScriptSourceCode(script.element()->textContent(), documentURLForScriptExecution(m_document), script.startingPosition());
96 }
97
98 bool HTMLScriptRunner::isPendingScriptReady(const PendingScript& script)
99 {
100     m_hasScriptsWaitingForResources = !m_document->haveStylesheetsAndImportsLoaded();
101     if (m_hasScriptsWaitingForResources)
102         return false;
103     if (script.resource() && !script.resource()->isLoaded())
104         return false;
105     return true;
106 }
107
108 void HTMLScriptRunner::executeParsingBlockingScript()
109 {
110     ASSERT(m_document);
111     ASSERT(!isExecutingScript());
112     ASSERT(m_document->haveStylesheetsAndImportsLoaded());
113     ASSERT(isPendingScriptReady(m_parserBlockingScript));
114
115     InsertionPointRecord insertionPointRecord(m_host->inputStream());
116     executePendingScriptAndDispatchEvent(m_parserBlockingScript);
117 }
118
119 void HTMLScriptRunner::executePendingScriptAndDispatchEvent(PendingScript& pendingScript)
120 {
121     bool errorOccurred = false;
122     ScriptSourceCode sourceCode = sourceFromPendingScript(pendingScript, errorOccurred);
123
124     // Stop watching loads before executeScript to prevent recursion if the script reloads itself.
125     if (pendingScript.resource() && pendingScript.watchingForLoad())
126         stopWatchingForLoad(pendingScript);
127
128     if (!isExecutingScript())
129         Microtask::performCheckpoint();
130
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);
136         if (errorOccurred)
137             scriptLoader->dispatchErrorEvent();
138         else {
139             ASSERT(isExecutingScript());
140             scriptLoader->executeScript(sourceCode);
141             element->dispatchEvent(createScriptLoadEvent());
142         }
143     }
144     ASSERT(!isExecutingScript());
145 }
146
147 void HTMLScriptRunner::watchForLoad(PendingScript& pendingScript)
148 {
149     ASSERT(!pendingScript.watchingForLoad());
150     m_host->watchForLoad(pendingScript.resource());
151     pendingScript.setWatchingForLoad(true);
152 }
153
154 void HTMLScriptRunner::stopWatchingForLoad(PendingScript& pendingScript)
155 {
156     ASSERT(pendingScript.watchingForLoad());
157     m_host->stopWatchingForLoad(pendingScript.resource());
158     pendingScript.setWatchingForLoad(false);
159 }
160
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)
165 {
166     ASSERT(scriptElement);
167     // FIXME: If scripting is disabled, always just return.
168
169     bool hadPreloadScanner = m_host->hasPreloadScanner();
170
171     // Try to execute the script given to us.
172     runScript(scriptElement.get(), scriptStartPosition);
173
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();
181     }
182 }
183
184 bool HTMLScriptRunner::hasParserBlockingScript() const
185 {
186     return !!m_parserBlockingScript.element();
187 }
188
189 void HTMLScriptRunner::executeParsingBlockingScripts()
190 {
191     while (hasParserBlockingScript() && isPendingScriptReady(m_parserBlockingScript))
192         executeParsingBlockingScript();
193 }
194
195 void HTMLScriptRunner::executeScriptsWaitingForLoad(Resource* resource)
196 {
197     ASSERT(!isExecutingScript());
198     ASSERT(hasParserBlockingScript());
199     ASSERT_UNUSED(resource, m_parserBlockingScript.resource() == resource);
200     ASSERT(m_parserBlockingScript.resource()->isLoaded());
201     executeParsingBlockingScripts();
202 }
203
204 void HTMLScriptRunner::executeScriptsWaitingForResources()
205 {
206     ASSERT(m_document);
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();
213 }
214
215 bool HTMLScriptRunner::executeScriptsWaitingForParsing()
216 {
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());
223             return false;
224         }
225         PendingScript first = m_scriptsToExecuteAfterParsing.takeFirst();
226         executePendingScriptAndDispatchEvent(first);
227         // FIXME: What is this m_document check for?
228         if (!m_document)
229             return false;
230     }
231     return true;
232 }
233
234 void HTMLScriptRunner::requestParsingBlockingScript(Element* element)
235 {
236     if (!requestPendingScript(m_parserBlockingScript, element))
237         return;
238
239     ASSERT(m_parserBlockingScript.resource());
240
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);
246 }
247
248 void HTMLScriptRunner::requestDeferredScript(Element* element)
249 {
250     PendingScript pendingScript;
251     if (!requestPendingScript(pendingScript, element))
252         return;
253
254     ASSERT(pendingScript.resource());
255     m_scriptsToExecuteAfterParsing.append(pendingScript);
256 }
257
258 bool HTMLScriptRunner::requestPendingScript(PendingScript& pendingScript, Element* script) const
259 {
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();
264     if (!resource) {
265         notImplemented(); // Dispatch error event.
266         return false;
267     }
268     pendingScript.setScriptResource(resource);
269     return true;
270 }
271
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)
275 {
276     ASSERT(m_document);
277     ASSERT(!hasParserBlockingScript());
278     {
279         ScriptLoader* scriptLoader = toScriptLoaderIfPossible(script);
280
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);
286         if (!scriptLoader)
287             return;
288
289         ASSERT(scriptLoader->isParserInserted());
290
291         if (!isExecutingScript())
292             Microtask::performCheckpoint();
293
294         InsertionPointRecord insertionPointRecord(m_host->inputStream());
295         NestingLevelIncrementer nestingLevelIncrementer(m_scriptNestingLevel);
296
297         scriptLoader->prepareScript(scriptStartPosition);
298
299         if (!scriptLoader->willBeParserExecuted())
300             return;
301
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);
308             } else {
309                 ScriptSourceCode sourceCode(script->textContent(), documentURLForScriptExecution(m_document), scriptStartPosition);
310                 scriptLoader->executeScript(sourceCode);
311             }
312         } else {
313             requestParsingBlockingScript(script);
314         }
315     }
316 }
317
318 }