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.
26 #ifndef HTMLScriptRunner_h
27 #define HTMLScriptRunner_h
29 #include "core/dom/PendingScript.h"
30 #include "core/fetch/ResourceClient.h"
31 #include "platform/heap/Handle.h"
32 #include "wtf/Deque.h"
33 #include "wtf/PassRefPtr.h"
34 #include "wtf/text/TextPosition.h"
43 class HTMLScriptRunnerHost;
44 class ScriptSourceCode;
46 class HTMLScriptRunner FINAL : public NoBaseWillBeGarbageCollectedFinalized<HTMLScriptRunner>, private ScriptResourceClient {
47 WTF_MAKE_NONCOPYABLE(HTMLScriptRunner); WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
49 static PassOwnPtrWillBeRawPtr<HTMLScriptRunner> create(Document* document, HTMLScriptRunnerHost* host)
51 return adoptPtrWillBeNoop(new HTMLScriptRunner(document, host));
57 // Processes the passed in script and any pending scripts if possible.
58 void execute(PassRefPtrWillBeRawPtr<Element> scriptToProcess, const TextPosition& scriptStartPosition);
60 void executeScriptsWaitingForLoad(Resource*);
61 bool hasScriptsWaitingForResources() const { return m_hasScriptsWaitingForResources; }
62 void executeScriptsWaitingForResources();
63 bool executeScriptsWaitingForParsing();
65 bool hasParserBlockingScript() const;
66 bool isExecutingScript() const { return !!m_scriptNestingLevel; }
69 virtual void notifyFinished(Resource*) OVERRIDE;
74 HTMLScriptRunner(Document*, HTMLScriptRunnerHost*);
76 void executeParsingBlockingScript();
77 void executePendingScriptAndDispatchEvent(PendingScript&, PendingScript::Type);
78 void executeParsingBlockingScripts();
80 void requestParsingBlockingScript(Element*);
81 void requestDeferredScript(Element*);
82 bool requestPendingScript(PendingScript&, Element*) const;
84 void runScript(Element*, const TextPosition& scriptStartPosition);
86 bool isPendingScriptReady(const PendingScript&);
88 RawPtrWillBeMember<Document> m_document;
89 RawPtrWillBeMember<HTMLScriptRunnerHost> m_host;
90 PendingScript m_parserBlockingScript;
91 Deque<PendingScript> m_scriptsToExecuteAfterParsing; // http://www.whatwg.org/specs/web-apps/current-work/#list-of-scripts-that-will-execute-when-the-document-has-finished-parsing
92 unsigned m_scriptNestingLevel;
94 // We only want stylesheet loads to trigger script execution if script
95 // execution is currently stopped due to stylesheet loads, otherwise we'd
96 // cause nested script execution when parsing <style> tags since </style>
97 // tags can cause Document to call executeScriptsWaitingForResources.
98 bool m_hasScriptsWaitingForResources;
100 // For tracking the times between script load and compilation, we need to
101 // know whether a parser blocking script was loaded previously, or whether
102 // it's really loaded when requested.
103 bool m_parserBlockingScriptAlreadyLoaded;