2 * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #ifndef ScriptLoader_h
22 #define ScriptLoader_h
24 #include "core/CoreExport.h"
25 #include "core/dom/PendingScript.h"
26 #include "core/fetch/FetchRequest.h"
27 #include "core/fetch/ResourceClient.h"
28 #include "core/fetch/ResourcePtr.h"
29 #include "core/fetch/ScriptResource.h"
30 #include "wtf/text/TextPosition.h"
31 #include "wtf/text/WTFString.h"
36 class ScriptLoaderClient
;
37 class ScriptSourceCode
;
40 class CORE_EXPORT ScriptLoader
: public NoBaseWillBeGarbageCollectedFinalized
<ScriptLoader
>, private ScriptResourceClient
{
41 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED(ScriptLoader
);
43 static PassOwnPtrWillBeRawPtr
<ScriptLoader
> create(Element
* element
, bool createdByParser
, bool isEvaluated
)
45 return adoptPtrWillBeNoop(new ScriptLoader(element
, createdByParser
, isEvaluated
));
48 ~ScriptLoader() override
;
49 DECLARE_VIRTUAL_TRACE();
51 Element
* element() const { return m_element
; }
53 enum LegacyTypeSupport
{ DisallowLegacyTypeInTypeAttribute
, AllowLegacyTypeInTypeAttribute
};
54 bool prepareScript(const TextPosition
& scriptStartPosition
= TextPosition::minimumPosition(), LegacyTypeSupport
= DisallowLegacyTypeInTypeAttribute
);
56 String
scriptCharset() const { return m_characterEncoding
; }
57 String
scriptContent() const;
58 // Returns false if and only if execution was blocked.
59 bool executeScript(const ScriptSourceCode
&, double* compilationFinishTime
= 0);
60 virtual void execute();
62 // XML parser calls these
63 void dispatchLoadEvent();
64 void dispatchErrorEvent();
65 bool isScriptTypeSupported(LegacyTypeSupport
) const;
67 bool haveFiredLoadEvent() const { return m_haveFiredLoad
; }
68 bool willBeParserExecuted() const { return m_willBeParserExecuted
; }
69 bool readyToBeParserExecuted() const { return m_readyToBeParserExecuted
; }
70 bool willExecuteWhenDocumentFinishedParsing() const { return m_willExecuteWhenDocumentFinishedParsing
; }
71 ResourcePtr
<ScriptResource
> resource() { return m_resource
; }
73 void setHaveFiredLoadEvent(bool haveFiredLoad
) { m_haveFiredLoad
= haveFiredLoad
; }
74 bool isParserInserted() const { return m_parserInserted
; }
75 bool alreadyStarted() const { return m_alreadyStarted
; }
76 bool forceAsync() const { return m_forceAsync
; }
78 // Helper functions used by our parent classes.
79 void didNotifySubtreeInsertionsToDocument();
80 void childrenChanged();
81 void handleSourceAttribute(const String
& sourceUrl
);
82 void handleAsyncAttribute();
84 virtual bool isReady() const { return m_pendingScript
.isReady(); }
86 // Clears the connection to the PendingScript (and Element and Resource).
90 ScriptLoader(Element
*, bool createdByParser
, bool isEvaluated
);
93 bool ignoresLoadRequest() const;
94 bool isScriptForEventSupported() const;
96 bool fetchScript(const String
& sourceUrl
, FetchRequest::DeferOption
);
98 ScriptLoaderClient
* client() const;
101 void notifyFinished(Resource
*) override
;
103 RawPtrWillBeMember
<Element
> m_element
;
104 ResourcePtr
<ScriptResource
> m_resource
;
105 WTF::OrdinalNumber m_startLineNumber
;
106 String m_characterEncoding
;
107 String m_fallbackCharacterEncoding
;
109 PendingScript m_pendingScript
;
111 bool m_parserInserted
: 1;
112 bool m_isExternalScript
: 1;
113 bool m_alreadyStarted
: 1;
114 bool m_haveFiredLoad
: 1;
115 bool m_willBeParserExecuted
: 1; // Same as "The parser will handle executing the script."
116 bool m_readyToBeParserExecuted
: 1;
117 bool m_willExecuteWhenDocumentFinishedParsing
: 1;
118 bool m_forceAsync
: 1;
119 bool m_willExecuteInOrder
: 1;
122 ScriptLoader
* toScriptLoaderIfPossible(Element
*);
126 #endif // ScriptLoader_h