| OLD | NEW |
| (Empty) |
| 1 // Copyright 2011, Google Inc. | |
| 2 // 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 are | |
| 6 // met: | |
| 7 // | |
| 8 // * Redistributions of source code must retain the above copyright | |
| 9 // notice, this list of conditions and the following disclaimer. | |
| 10 // * Redistributions in binary form must reproduce the above | |
| 11 // copyright notice, this list of conditions and the following disclaimer | |
| 12 // in the documentation and/or other materials provided with the | |
| 13 // distribution. | |
| 14 // * Neither the name of Google Inc. nor the names of its | |
| 15 // contributors may be used to endorse or promote products derived from | |
| 16 // this software without specific prior written permission. | |
| 17 // | |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | |
| 30 #ifndef DartApplicationLoader_h | |
| 31 #define DartApplicationLoader_h | |
| 32 | |
| 33 #include "bindings/core/v8/ScriptSourceCode.h" | |
| 34 #include "core/dom/Document.h" | |
| 35 #include "core/html/HTMLScriptElement.h" | |
| 36 #include "core/html/VoidCallback.h" | |
| 37 #include <dart_api.h> | |
| 38 #include <wtf/HashMap.h> | |
| 39 #include <wtf/HashSet.h> | |
| 40 #include <wtf/text/StringHash.h> | |
| 41 | |
| 42 namespace blink { | |
| 43 | |
| 44 class FetchRequest; | |
| 45 class KURL; | |
| 46 class ScriptLoader; | |
| 47 class ScriptLoaderClient; | |
| 48 class ScriptResource; | |
| 49 | |
| 50 class DartErrorEventDispatcher : public RefCounted<DartErrorEventDispatcher> { | |
| 51 public: | |
| 52 virtual ~DartErrorEventDispatcher() { } | |
| 53 virtual void dispatchErrorEvent() = 0; | |
| 54 }; | |
| 55 | |
| 56 // FIXME(vsm): We should be able to eliminate this class and reuse v8/ScriptSour
ceCode. | |
| 57 // Abstraction for HTMLScriptElement or SVGScriptElement. | |
| 58 class DartScriptInfo : public DartErrorEventDispatcher { | |
| 59 public: | |
| 60 static PassRefPtr<DartScriptInfo> create(PassRefPtr<Element> scriptElement); | |
| 61 | |
| 62 String sourceAttributeValue() const; | |
| 63 String typeAttributeValue() const; | |
| 64 String scriptContent(); | |
| 65 void dispatchErrorEvent(); | |
| 66 WTF::OrdinalNumber startLineNumber(); | |
| 67 ScriptLoader* loader(); | |
| 68 Document* ownerDocument(); | |
| 69 String url(); | |
| 70 | |
| 71 virtual ~DartScriptInfo() { } | |
| 72 | |
| 73 protected: | |
| 74 DartScriptInfo(PassRefPtr<Element>); | |
| 75 | |
| 76 // Note, the element keeps the corresponding ScriptLoader and ScriptLoaderCl
ient alive. | |
| 77 RefPtr<Element> m_element; | |
| 78 ScriptLoader* m_loader; | |
| 79 ScriptLoaderClient* m_client; | |
| 80 }; | |
| 81 | |
| 82 class DartApplicationLoader : public RefCounted<DartApplicationLoader> { | |
| 83 public: | |
| 84 class Callback : public RefCounted<Callback> { | |
| 85 public: | |
| 86 Callback(Document* originDocument, PassRefPtr<DartScriptInfo> scriptInfo
= nullptr) | |
| 87 : m_originDocument(originDocument) | |
| 88 , m_scriptInfo(scriptInfo) { } | |
| 89 | |
| 90 virtual ~Callback() { } | |
| 91 | |
| 92 virtual void ready() = 0; | |
| 93 void reportError(const String& error, const String& url); | |
| 94 | |
| 95 KURL completeURL(const String& url) { return document()->completeURL(url
); } | |
| 96 ResourcePtr<ScriptResource> requestScript(FetchRequest&); | |
| 97 | |
| 98 Document* document(); | |
| 99 Document* originDocument() const { return m_originDocument; } | |
| 100 PassRefPtr<DartScriptInfo> scriptInfo() const { return m_scriptInfo; } | |
| 101 | |
| 102 private: | |
| 103 Document* m_originDocument; | |
| 104 RefPtr<DartScriptInfo> m_scriptInfo; | |
| 105 }; | |
| 106 | |
| 107 static PassRefPtr<DartApplicationLoader> create(Document* document, bool dom
Enabled) | |
| 108 { | |
| 109 return adoptRef(new DartApplicationLoader(document, domEnabled)); | |
| 110 } | |
| 111 | |
| 112 void load(PassRefPtr<DartErrorEventDispatcher>); | |
| 113 | |
| 114 Document* document() { return m_originDocument; } | |
| 115 | |
| 116 void callEntryPoint(); | |
| 117 | |
| 118 void validateUrlLoaded(const String& url); | |
| 119 | |
| 120 // Registers a request to be fetched later. | |
| 121 void addRequest(PassRefPtr<DartScriptInfo>); | |
| 122 | |
| 123 // Fetches all pending requests and invokes callback when done. | |
| 124 void processRequests(Dart_Isolate, const ScriptSourceCode&, PassRefPtr<Callb
ack>); | |
| 125 void processSingleRequest(Dart_Isolate, const String& url, PassRefPtr<Callba
ck>); | |
| 126 | |
| 127 bool running() const { return m_state >= Running; } | |
| 128 bool error() const { return m_state == Error; } | |
| 129 | |
| 130 private: | |
| 131 enum State { | |
| 132 // The application failed to load. | |
| 133 Error = -1, | |
| 134 | |
| 135 // The isolate is not set. | |
| 136 Uninitialized = 0, | |
| 137 | |
| 138 // The isolate is initialized, but no user scripts have been requested o
r loaded. | |
| 139 Initialized, | |
| 140 | |
| 141 // The isolate is not running. The main script has been requested, but n
ot loaded. | |
| 142 Fetching, | |
| 143 | |
| 144 // The isolate is not running as requests are in the process of loading. | |
| 145 Loading, | |
| 146 | |
| 147 // The isolate is not running, but is ready to run. There are no outstan
ding requests. | |
| 148 Ready, | |
| 149 | |
| 150 // The isolate is running. There no outstanding requests or deferred lib
raries. | |
| 151 Running, | |
| 152 | |
| 153 // The isolate is running, but deferred requests are outstanding. | |
| 154 DeferredLoading, | |
| 155 | |
| 156 // The isolate is running, but there are deferred requests ready to fina
lize. | |
| 157 DeferredReady, | |
| 158 }; | |
| 159 | |
| 160 enum SnapshotMode { | |
| 161 // Snapshots are disabled. | |
| 162 SnapshotOff, | |
| 163 | |
| 164 // Snapshot only single resource cacheable apps. | |
| 165 SnapshotSingle, | |
| 166 | |
| 167 // Snapshot everything (experimental). | |
| 168 SnapshotAll, | |
| 169 }; | |
| 170 | |
| 171 typedef HashSet<String> UrlSet; | |
| 172 typedef Vector<Dart_PersistentHandle> HandleSet; | |
| 173 typedef HashMap<String, HandleSet*> UrlHandleMap; | |
| 174 typedef Deque<RefPtr<DartScriptInfo> > ScriptList; | |
| 175 | |
| 176 DartApplicationLoader(Document*, bool domEnabled = true); | |
| 177 | |
| 178 void loadScriptFromSnapshot(const String& url, const uint8_t* snapshot, intp
tr_t snapshotSize); | |
| 179 | |
| 180 Dart_Handle topLevelLibrary(); | |
| 181 | |
| 182 // FIXME(vsm): Do we need all of these? | |
| 183 void scriptLoadError(String failedUrl); | |
| 184 void reportDartError(Dart_Handle); | |
| 185 void reportError(Dart_Handle error, const String& url); | |
| 186 void reportError(const String& error, const String& url); | |
| 187 | |
| 188 void initialize(Dart_Isolate, const String& scriptUrl, PassRefPtr<Callback>)
; | |
| 189 | |
| 190 void process(const String& url, const String& source, intptr_t lineNumber); | |
| 191 void fetchScriptResource(const String& url); | |
| 192 | |
| 193 // All dependences are available. | |
| 194 bool ready() const { return m_pendingLibraries.isEmpty() && m_pendingSource.
isEmpty() && m_htmlImportedScripts.isEmpty() && (m_state >= Loading); } | |
| 195 | |
| 196 void findDependences(const String& url, const String& source, intptr_t lineN
umber); | |
| 197 void processLibrary(const String& url, const String& source, intptr_t lineNu
mber); | |
| 198 void processSource(const String& url, const String& source, intptr_t lineNum
ber); | |
| 199 static Dart_Handle libraryTagHandlerCallback(Dart_LibraryTag, Dart_Handle li
brary, Dart_Handle urlHandle); | |
| 200 | |
| 201 Dart_Isolate m_isolate; | |
| 202 // Client must ensure that DartApplicationLoader doesn't outlive correspondi
ng document. | |
| 203 Document* m_originDocument; | |
| 204 RefPtr<DartErrorEventDispatcher> m_errorEventDispatcher; | |
| 205 RefPtr<Callback> m_loadCallback; | |
| 206 bool m_domEnabled; | |
| 207 bool m_cacheable; | |
| 208 | |
| 209 ScriptList m_htmlImportedScripts; | |
| 210 UrlSet m_pendingLibraries; | |
| 211 UrlHandleMap m_pendingSource; | |
| 212 | |
| 213 KURL m_scriptUrl; | |
| 214 String m_scriptUrlString; | |
| 215 | |
| 216 State m_state; | |
| 217 SnapshotMode m_snapshotMode; | |
| 218 | |
| 219 friend class DartService; | |
| 220 friend class ScriptLoadedCallback; | |
| 221 }; | |
| 222 | |
| 223 } | |
| 224 | |
| 225 #endif // DartApplicationLoader_h | |
| OLD | NEW |