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 #ifndef HTMLScriptRunner_h 27 #define HTMLScriptRunner_h 28 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" 35 36 namespace WebCore { 37 38 class Resource; 39 class ScriptResource; 40 class Document; 41 class Element; 42 class LocalFrame; 43 class HTMLScriptRunnerHost; 44 class ScriptSourceCode; 45 46 class HTMLScriptRunner FINAL : public NoBaseWillBeGarbageCollectedFinalized<HTMLScriptRunner>, private ResourceClient { 47 WTF_MAKE_NONCOPYABLE(HTMLScriptRunner); WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED; 48 public: 49 static PassOwnPtrWillBeRawPtr<HTMLScriptRunner> create(Document* document, HTMLScriptRunnerHost* host) 50 { 51 return adoptPtrWillBeNoop(new HTMLScriptRunner(document, host)); 52 } 53 ~HTMLScriptRunner(); 54 55 void detach(); 56 57 // Processes the passed in script and any pending scripts if possible. 58 void execute(PassRefPtrWillBeRawPtr<Element> scriptToProcess, const TextPosition& scriptStartPosition); 59 60 void executeScriptsWaitingForLoad(Resource*); 61 bool hasScriptsWaitingForResources() const { return m_hasScriptsWaitingForResources; } 62 void executeScriptsWaitingForResources(); 63 bool executeScriptsWaitingForParsing(); 64 65 bool hasParserBlockingScript() const; 66 bool isExecutingScript() const { return !!m_scriptNestingLevel; } 67 68 // ResourceClient 69 virtual void notifyFinished(Resource*) OVERRIDE; 70 71 void trace(Visitor*); 72 73 private: 74 HTMLScriptRunner(Document*, HTMLScriptRunnerHost*); 75 76 LocalFrame* frame() const; 77 78 enum PendingScriptType { 79 PendingScriptBlockingParser, 80 PendingScriptDeferred 81 }; 82 83 void executeParsingBlockingScript(); 84 void executePendingScriptAndDispatchEvent(PendingScript&, PendingScriptType); 85 void executeParsingBlockingScripts(); 86 87 void requestParsingBlockingScript(Element*); 88 void requestDeferredScript(Element*); 89 bool requestPendingScript(PendingScript&, Element*) const; 90 91 void runScript(Element*, const TextPosition& scriptStartPosition); 92 93 // Helpers for dealing with HTMLScriptRunnerHost 94 void watchForLoad(PendingScript&); 95 void stopWatchingForLoad(PendingScript&); 96 bool isPendingScriptReady(const PendingScript&); 97 ScriptSourceCode sourceFromPendingScript(const PendingScript&, bool& errorOccurred) const; 98 99 RawPtrWillBeMember<Document> m_document; 100 RawPtrWillBeMember<HTMLScriptRunnerHost> m_host; 101 PendingScript m_parserBlockingScript; 102 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 103 unsigned m_scriptNestingLevel; 104 105 // We only want stylesheet loads to trigger script execution if script 106 // execution is currently stopped due to stylesheet loads, otherwise we'd 107 // cause nested script execution when parsing <style> tags since </style> 108 // tags can cause Document to call executeScriptsWaitingForResources. 109 bool m_hasScriptsWaitingForResources; 110 }; 111 112 } 113 114 #endif 115