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 logObservatoryInformation(); |
| 119 |
| 120 void validateUrlLoaded(const String& url); |
| 121 |
| 122 // Registers a request to be fetched later. |
| 123 void addRequest(PassRefPtr<DartScriptInfo>); |
| 124 |
| 125 // Fetches all pending requests and invokes callback when done. |
| 126 void processRequests(Dart_Isolate, const ScriptSourceCode&, PassRefPtr<Callb
ack>); |
| 127 void processSingleRequest(Dart_Isolate, const String& url, PassRefPtr<Callba
ck>); |
| 128 |
| 129 bool running() const { return m_state >= Running; } |
| 130 bool error() const { return m_state == Error; } |
| 131 bool uninitialized() const { return m_state == Uninitialized; } |
| 132 |
| 133 const KURL& scriptUrl() { return m_scriptUrl; } |
| 134 private: |
| 135 enum State { |
| 136 // The application failed to load. |
| 137 Error = -1, |
| 138 |
| 139 // The isolate is not set. |
| 140 Uninitialized = 0, |
| 141 |
| 142 // The isolate is initialized, but no user scripts have been requested o
r loaded. |
| 143 Initialized, |
| 144 |
| 145 // The isolate is not running. The main script has been requested, but n
ot loaded. |
| 146 Fetching, |
| 147 |
| 148 // The isolate is not running as requests are in the process of loading. |
| 149 Loading, |
| 150 |
| 151 // The isolate is not running, but is ready to run. There are no outstan
ding requests. |
| 152 Ready, |
| 153 |
| 154 // The isolate is running. There no outstanding requests or deferred lib
raries. |
| 155 Running, |
| 156 |
| 157 // The isolate is running, but deferred requests are outstanding. |
| 158 DeferredLoading, |
| 159 |
| 160 // The isolate is running, but there are deferred requests ready to fina
lize. |
| 161 DeferredReady, |
| 162 }; |
| 163 |
| 164 enum SnapshotMode { |
| 165 // Snapshots are disabled. |
| 166 SnapshotOff, |
| 167 |
| 168 // Snapshot only single resource cacheable apps. |
| 169 SnapshotSingle, |
| 170 |
| 171 // Snapshot everything (experimental). |
| 172 SnapshotAll, |
| 173 }; |
| 174 |
| 175 typedef HashSet<String> UrlSet; |
| 176 typedef Vector<Dart_PersistentHandle> HandleSet; |
| 177 typedef HashMap<String, HandleSet*> UrlHandleMap; |
| 178 typedef Deque<RefPtr<DartScriptInfo> > ScriptList; |
| 179 |
| 180 DartApplicationLoader(Document*, bool domEnabled = true); |
| 181 |
| 182 void loadScriptFromSnapshot(const String& url, const uint8_t* snapshot, intp
tr_t snapshotSize); |
| 183 |
| 184 Dart_Handle topLevelLibrary(); |
| 185 |
| 186 // FIXME(vsm): Do we need all of these? |
| 187 void scriptLoadError(String failedUrl); |
| 188 void reportDartError(Dart_Handle); |
| 189 void reportError(Dart_Handle error, const String& url); |
| 190 void reportError(const String& error, const String& url); |
| 191 |
| 192 void initialize(Dart_Isolate, const String& scriptUrl, PassRefPtr<Callback>)
; |
| 193 |
| 194 void process(const String& url, const String& source, intptr_t lineNumber); |
| 195 void fetchScriptResource(const String& url); |
| 196 |
| 197 // All dependences are available. |
| 198 bool ready() const { return m_pendingLibraries.isEmpty() && m_pendingSource.
isEmpty() && m_htmlImportedScripts.isEmpty() && (m_state >= Loading); } |
| 199 |
| 200 void findDependences(const String& url, const String& source, intptr_t lineN
umber); |
| 201 void processLibrary(const String& url, const String& source, intptr_t lineNu
mber); |
| 202 void processSource(const String& url, const String& source, intptr_t lineNum
ber); |
| 203 static Dart_Handle libraryTagHandlerCallback(Dart_LibraryTag, Dart_Handle li
brary, Dart_Handle urlHandle); |
| 204 |
| 205 Dart_Isolate m_isolate; |
| 206 // Client must ensure that DartApplicationLoader doesn't outlive correspondi
ng document. |
| 207 Document* m_originDocument; |
| 208 RefPtr<DartErrorEventDispatcher> m_errorEventDispatcher; |
| 209 RefPtr<Callback> m_loadCallback; |
| 210 bool m_domEnabled; |
| 211 bool m_cacheable; |
| 212 |
| 213 ScriptList m_htmlImportedScripts; |
| 214 UrlSet m_pendingLibraries; |
| 215 UrlHandleMap m_pendingSource; |
| 216 |
| 217 KURL m_scriptUrl; |
| 218 String m_scriptUrlString; |
| 219 String m_packageRoot; |
| 220 |
| 221 State m_state; |
| 222 SnapshotMode m_snapshotMode; |
| 223 |
| 224 friend class DartService; |
| 225 friend class ScriptLoadedCallback; |
| 226 }; |
| 227 |
| 228 } |
| 229 |
| 230 #endif // DartApplicationLoader_h |
OLD | NEW |