Home | History | Annotate | Download | only in parser
      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 blink {
     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 ScriptResourceClient {
     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     void executeParsingBlockingScript();
     77     void executePendingScriptAndDispatchEvent(PendingScript&, PendingScript::Type);
     78     void executeParsingBlockingScripts();
     79 
     80     void requestParsingBlockingScript(Element*);
     81     void requestDeferredScript(Element*);
     82     bool requestPendingScript(PendingScript&, Element*) const;
     83 
     84     void runScript(Element*, const TextPosition& scriptStartPosition);
     85 
     86     bool isPendingScriptReady(const PendingScript&);
     87 
     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;
     93 
     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;
     99 
    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;
    104 };
    105 
    106 }
    107 
    108 #endif
    109