Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / html / parser / HTMLScriptRunner.h
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 Document;
40 class Element;
41 class HTMLScriptRunnerHost;
42
43 class HTMLScriptRunner final : public NoBaseWillBeGarbageCollectedFinalized<HTMLScriptRunner>, private ScriptResourceClient {
44     WTF_MAKE_NONCOPYABLE(HTMLScriptRunner); WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
45 public:
46     static PassOwnPtrWillBeRawPtr<HTMLScriptRunner> create(Document* document, HTMLScriptRunnerHost* host)
47     {
48         return adoptPtrWillBeNoop(new HTMLScriptRunner(document, host));
49     }
50     ~HTMLScriptRunner();
51
52     void detach();
53
54     // Processes the passed in script and any pending scripts if possible.
55     void execute(PassRefPtrWillBeRawPtr<Element> scriptToProcess, const TextPosition& scriptStartPosition);
56
57     void executeScriptsWaitingForLoad(Resource*);
58     bool hasScriptsWaitingForResources() const { return m_hasScriptsWaitingForResources; }
59     void executeScriptsWaitingForResources();
60     bool executeScriptsWaitingForParsing();
61
62     bool hasParserBlockingScript() const;
63     bool isExecutingScript() const { return !!m_scriptNestingLevel; }
64
65     // ResourceClient
66     virtual void notifyFinished(Resource*) override;
67
68     void trace(Visitor*);
69
70 private:
71     HTMLScriptRunner(Document*, HTMLScriptRunnerHost*);
72
73     void executeParsingBlockingScript();
74     void executePendingScriptAndDispatchEvent(PendingScript&, PendingScript::Type);
75     void executeParsingBlockingScripts();
76
77     void requestParsingBlockingScript(Element*);
78     void requestDeferredScript(Element*);
79     bool requestPendingScript(PendingScript&, Element*) const;
80
81     void runScript(Element*, const TextPosition& scriptStartPosition);
82
83     bool isPendingScriptReady(const PendingScript&);
84
85     RawPtrWillBeMember<Document> m_document;
86     RawPtrWillBeMember<HTMLScriptRunnerHost> m_host;
87     PendingScript m_parserBlockingScript;
88     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
89     unsigned m_scriptNestingLevel;
90
91     // We only want stylesheet loads to trigger script execution if script
92     // execution is currently stopped due to stylesheet loads, otherwise we'd
93     // cause nested script execution when parsing <style> tags since </style>
94     // tags can cause Document to call executeScriptsWaitingForResources.
95     bool m_hasScriptsWaitingForResources;
96
97     // For tracking the times between script load and compilation, we need to
98     // know whether a parser blocking script was loaded previously, or whether
99     // it's really loaded when requested.
100     bool m_parserBlockingScriptAlreadyLoaded;
101 };
102
103 }
104
105 #endif