1 /*
2 * Copyright (C) 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 *
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.
8 *
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.
13 *
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.
18 *
19 */
20
21 #ifndef ScriptLoader_h
22 #define ScriptLoader_h
23
24 #include "core/fetch/ResourceClient.h"
25 #include "core/fetch/ResourcePtr.h"
26 #include "wtf/text/TextPosition.h"
27 #include "wtf/text/WTFString.h"
28
29 namespace WebCore {
30
31 class ScriptResource;
32 class ContainerNode;
33 class Element;
34 class ScriptLoaderClient;
35 class ScriptSourceCode;
36
37
38 class ScriptLoader : private ResourceClient {
39 public:
40 static PassOwnPtr<ScriptLoader> create(Element*, bool createdByParser, bool isEvaluated);
41 virtual ~ScriptLoader();
42
element()43 Element* element() const { return m_element; }
44
45 enum LegacyTypeSupport { DisallowLegacyTypeInTypeAttribute, AllowLegacyTypeInTypeAttribute };
46 bool prepareScript(const TextPosition& scriptStartPosition = TextPosition::minimumPosition(), LegacyTypeSupport = DisallowLegacyTypeInTypeAttribute);
47
scriptCharset()48 String scriptCharset() const { return m_characterEncoding; }
49 String scriptContent() const;
50 void executeScript(const ScriptSourceCode&);
51 void execute(ScriptResource*);
52
53 // Check if potentially cross-origin enabled script is accessible
54 // prior to execution. Returns 'false' if not accessible, signalling
55 // that callers must not dispatch load events as the cross-origin
56 // fetch failed.
57 bool executePotentiallyCrossOriginScript(const ScriptSourceCode&);
58
59 // XML parser calls these
60 void dispatchLoadEvent();
61 void dispatchErrorEvent();
62 bool isScriptTypeSupported(LegacyTypeSupport) const;
63
haveFiredLoadEvent()64 bool haveFiredLoadEvent() const { return m_haveFiredLoad; }
willBeParserExecuted()65 bool willBeParserExecuted() const { return m_willBeParserExecuted; }
readyToBeParserExecuted()66 bool readyToBeParserExecuted() const { return m_readyToBeParserExecuted; }
willExecuteWhenDocumentFinishedParsing()67 bool willExecuteWhenDocumentFinishedParsing() const { return m_willExecuteWhenDocumentFinishedParsing; }
resource()68 ResourcePtr<ScriptResource> resource() { return m_resource; }
69
setHaveFiredLoadEvent(bool haveFiredLoad)70 void setHaveFiredLoadEvent(bool haveFiredLoad) { m_haveFiredLoad = haveFiredLoad; }
isParserInserted()71 bool isParserInserted() const { return m_parserInserted; }
alreadyStarted()72 bool alreadyStarted() const { return m_alreadyStarted; }
forceAsync()73 bool forceAsync() const { return m_forceAsync; }
isPotentiallyCORSEnabled()74 bool isPotentiallyCORSEnabled() const { return m_isPotentiallyCORSEnabled; }
75
76 // Helper functions used by our parent classes.
77 void didNotifySubtreeInsertionsToDocument();
78 void childrenChanged();
79 void handleSourceAttribute(const String& sourceUrl);
80 void handleAsyncAttribute();
81
82 private:
83 ScriptLoader(Element*, bool createdByParser, bool isEvaluated);
84
85 bool ignoresLoadRequest() const;
86 bool isScriptForEventSupported() const;
87
88 bool fetchScript(const String& sourceUrl);
89 void stopLoadRequest();
90
91 ScriptLoaderClient* client() const;
92
93 // ResourceClient
94 virtual void notifyFinished(Resource*) OVERRIDE;
95
96 Element* m_element;
97 ResourcePtr<ScriptResource> m_resource;
98 WTF::OrdinalNumber m_startLineNumber;
99 bool m_parserInserted : 1;
100 bool m_isExternalScript : 1;
101 bool m_alreadyStarted : 1;
102 bool m_haveFiredLoad : 1;
103 bool m_willBeParserExecuted : 1; // Same as "The parser will handle executing the script."
104 bool m_readyToBeParserExecuted : 1;
105 bool m_willExecuteWhenDocumentFinishedParsing : 1;
106 bool m_forceAsync : 1;
107 bool m_willExecuteInOrder : 1;
108 bool m_isPotentiallyCORSEnabled : 1;
109 String m_characterEncoding;
110 String m_fallbackCharacterEncoding;
111 };
112
113 ScriptLoader* toScriptLoaderIfPossible(Element*);
114
create(Element * element,bool createdByParser,bool isEvaluated)115 inline PassOwnPtr<ScriptLoader> ScriptLoader::create(Element* element, bool createdByParser, bool isEvaluated)
116 {
117 return adoptPtr(new ScriptLoader(element, createdByParser, isEvaluated));
118 }
119
120 }
121
122
123 #endif
124