Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: Source/WebCore/bindings/dart/DartApplicationLoader.cpp

Issue 8802010: Dart bindings for WebKit (Closed) Base URL: http://svn.webkit.org/repository/webkit/trunk
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2009, 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 #include "config.h"
31 #include "DartApplicationLoader.h"
32
33 #include "CachedResourceClient.h"
34 #include "CachedResourceLoader.h"
35 #include "DOMWindow.h"
36 #include "DartDOMWrapper.h"
37 #include "DartHTMLLibrarySource.h"
38 #include "DartHTMLImplLibrarySource.h"
39 #include "DartIsolateState.h"
40 #include "DartJSONLibrarySource.h"
41 #include "DartUtilities.h"
42 #include "Document.h"
43 #include "Frame.h"
44 #include "ScriptCallStack.h"
45 #include "ScriptSourceCode.h"
46
47 #include <dart_api.h>
48
49 namespace WebCore {
50
51 DartApplicationLoader::DartApplicationLoader(Document* document, NPObject* layou tTestController)
52 : m_document(document)
53 , m_layoutTestController(layoutTestController)
54 , m_libraryUrl()
55 , m_importedLibraries()
56 , m_importersForSource()
57 , m_sources()
58 , m_mainScriptHasBeenLoaded(false)
59 {
60 }
61
62 void DartApplicationLoader::load(const String& url, const String& source)
63 {
64 DartApiScope dartApiScope;
65
66 if (m_importedLibraries.isEmpty() && m_importersForSource.isEmpty())
67 loadScript(url, source);
68 else {
69 if (m_importedLibraries.contains(url)) {
70 loadLibrary(url, source);
71 m_importedLibraries.remove(url);
72 }
73
74 if (m_importersForSource.contains(url)) {
75 loadSource(url, source);
76 m_importersForSource.remove(url);
77 }
78 }
79
80 m_sources.add(url, source);
81
82 if (m_importedLibraries.isEmpty() && m_importersForSource.isEmpty() && m_mai nScriptHasBeenLoaded)
83 callEntryPoint();
84 }
85
86 void DartApplicationLoader::importBuiltinLibrary(const String& url, const String & source)
87 {
88 Dart_Handle result = Dart_LoadLibrary(DartUtilities::stringToDartString(url) , DartUtilities::stringToDartString(source));
89 if (Dart_IsError(result)) {
90 DartUtilities::reportProblem(m_document, result);
91 return;
92 }
93 m_sources.add(url, source);
94 }
95
96 IsolateToDartApplicationLoaderMap& isolateToDartApplicationLoaderMap()
97 {
98 DEFINE_STATIC_LOCAL(IsolateToDartApplicationLoaderMap, map, ());
99 return map;
100 }
101
102 static DartApplicationLoader* currentAppLoader()
103 {
104 IsolateToDartApplicationLoaderMap::iterator it = isolateToDartApplicationLoa derMap().find(Dart_CurrentIsolate());
105 ASSERT(it != isolateToDartApplicationLoaderMap().end());
106 return it->second;
107 }
108
109 template <class Loader>
110 class ScriptLoader {
111 public:
112 static void loadScript(const String& url, const String& source, ScriptExecut ionContext* context)
113 {
114 Dart_Handle result = Dart_LoadScript(
115 DartUtilities::stringToDartString(url),
116 DartUtilities::stringToDartString(source),
117 &handler);
118 if (Dart_IsError(result))
119 DartUtilities::reportProblem(context, result);
120 }
121
122 private:
123 static Dart_Handle handler(Dart_LibraryTag tag, Dart_Handle library, Dart_Ha ndle urlHandle)
124 {
125 ASSERT(Dart_IsLibrary(library));
126
127 String url = DartUtilities::dartStringToString(urlHandle);
128
129 Dart_Handle libraryURLHandle = Dart_LibraryUrl(library);
130 ASSERT(!Dart_IsError(libraryURLHandle));
131 const String libraryURL = DartUtilities::dartStringToString(libraryURLHa ndle);
132
133 if (tag == kCanonicalizeUrl) {
134 const KURL canonical = KURL(KURL(KURL(), libraryURL), url);
135 return DartUtilities::stringToDartString(canonical.string());
136 }
137
138 return Loader::load(tag, library, libraryURL, url);
139 }
140 };
141
142 struct InitialLoader {
143 static Dart_Handle load(Dart_LibraryTag tag, Dart_Handle library, const Stri ng& libraryUrl, const String& importedUrl)
144 {
145 currentAppLoader()->triggerImport(libraryUrl, importedUrl, tag);
146 return Dart_NewBoolean(true);
147 }
148 };
149
150 void DartApplicationLoader::loadScript(const String& url, const String& source)
151 {
152 ASSERT(m_importedLibraries.isEmpty());
153 ASSERT(m_importersForSource.isEmpty());
154 m_libraryUrl = url;
155 ScriptLoader<InitialLoader>::loadScript(url, source, m_document);
156 m_mainScriptHasBeenLoaded = true;
157 }
158
159 void DartApplicationLoader::loadSource(const String& url, const String& source)
160 {
161 ASSERT(m_importersForSource.contains(url));
162 const UrlSet* importers = m_importersForSource.find(url)->second;
163 Dart_Handle dartUrl = DartUtilities::stringToDartString(url);
164 Dart_Handle dartSource = DartUtilities::stringToDartString(source);
165 for (UrlSet::iterator iter = importers->begin(); iter != importers->end(); + +iter) {
166 Dart_Handle library = Dart_LookupLibrary(DartUtilities::stringToDartStri ng(*iter));
167 if (Dart_IsError(library)) {
168 DartUtilities::reportProblem(m_document, library);
169 return;
170 }
171 Dart_Handle result = Dart_LoadSource(library, dartUrl, dartSource);
172 if (Dart_IsError(result)) {
173 DartUtilities::reportProblem(m_document, result);
174 return;
175 }
176 }
177 }
178
179 void DartApplicationLoader::loadLibrary(const String& url, const String& source)
180 {
181 ASSERT(m_importedLibraries.contains(url));
182 Dart_Handle result = Dart_LoadLibrary(DartUtilities::stringToDartString(url) , DartUtilities::stringToDartString(source));
183 if (Dart_IsError(result))
184 DartUtilities::reportProblem(m_document, result);
185 }
186
187 static void invokeMain(Document* document, Dart_Handle topLevelLibrary)
188 {
189 Dart_Handle result = Dart_InvokeStatic(topLevelLibrary, Dart_NewString(""), Dart_NewString("main"), 0, 0);
190 if (Dart_IsError(result))
191 DartUtilities::reportProblem(document, result);
192 }
193
194 class ScriptStarter : public EventListener {
195 public:
196 static PassRefPtr<ScriptStarter> create(Dart_Isolate isolate, Dart_Handle to pLevelLibrary)
197 {
198 return adoptRef(new ScriptStarter(isolate, Dart_NewPersistentHandle(topL evelLibrary)));
199 }
200
201 virtual ~ScriptStarter()
202 {
203 DartIsolateState::Scope scope(m_isolate);
204 DartApiScope apiScope;
205 Dart_DeletePersistentHandle(m_topLevelLibrary);
206 }
207
208 virtual void handleEvent(ScriptExecutionContext* context, Event*)
209 {
210 ASSERT(context->isDocument());
211 Document* document = static_cast<Document*>(context);
212
213 // this gets removed below, so protect it while handler runs.
214 RefPtr<ScriptStarter> protect = this;
215 document->domWindow()->removeEventListener(String("DOMContentLoaded"), t his, false);
216
217 DartIsolateState::Scope scope(m_isolate);
218 DartApiScope apiScope;
219 invokeMain(document, m_topLevelLibrary);
220 }
221
222 virtual bool operator==(const EventListener& other)
223 {
224 return this == &other;
225 }
226
227 private:
228 ScriptStarter(Dart_Isolate isolate, Dart_Handle topLevelLibrary)
229 : EventListener(EventListener::NativeEventListenerType)
230 , m_isolate(isolate)
231 , m_topLevelLibrary(topLevelLibrary)
232 {
233 }
234
235 Dart_Isolate m_isolate;
236 Dart_Handle m_topLevelLibrary;
237 };
238
239 void DartApplicationLoader::callEntryPoint()
240 {
241 Dart_Handle result = Dart_CompileAll();
242 if (Dart_IsError(result)) {
243 DartUtilities::reportProblem(m_document, result);
244 return;
245 }
246
247 if (m_layoutTestController) {
248 Dart_Handle npObject = toDartValue(m_layoutTestController);
249 result = Dart_InvokeStatic(domLibrary(), Dart_NewString("LayoutTestContr oller"), Dart_NewString("_initLayoutTestController"), 1, &npObject);
250 ASSERT(!Dart_IsError(result));
251 }
252
253 if (m_document->readyState() == "loading")
254 m_document->domWindow()->addEventListener(String("DOMContentLoaded"), Sc riptStarter::create(Dart_CurrentIsolate(), topLevelLibrary()), false);
255 else
256 invokeMain(m_document, topLevelLibrary());
257 }
258
259 Dart_Handle DartApplicationLoader::topLevelLibrary()
260 {
261 Dart_Handle library = Dart_LookupLibrary(DartUtilities::stringToDartString(m _libraryUrl));
262 ASSERT(!Dart_IsError(library));
263 return library;
264 }
265
266 Dart_Handle DartApplicationLoader::domLibrary()
267 {
268 Dart_Handle library = Dart_LookupLibrary(Dart_NewString(DartUtilities::domLi braryName));
269 ASSERT(!Dart_IsError(library));
270 return library;
271 }
272
273 class ScriptLoadCallback : public CachedResourceClient {
274 public:
275 ScriptLoadCallback(DartApplicationLoader* loader, CachedScript* cachedScript , Dart_Isolate isolate)
276 : m_loader(loader),
277 m_cachedScript(cachedScript),
278 m_isolate(isolate)
279 {
280 }
281
282 virtual void notifyFinished(CachedResource* cachedResource)
283 {
284 ASSERT(cachedResource->type() == CachedResource::Script);
285 ASSERT(cachedResource == m_cachedScript.get());
286 ASSERT(WTF::isMainThread());
287 if (cachedResource->errorOccurred())
288 m_loader->scriptLoadError(cachedResource->url());
289 else if (cachedResource->wasCanceled())
290 m_loader->scriptLoadCancelled(cachedResource->url());
291 else {
292 ScriptSourceCode source(m_cachedScript.get());
293 m_loader->scriptLoaded(source, m_isolate);
294 }
295 m_cachedScript->removeClient(this);
296 delete this;
297 }
298
299 private:
300 RefPtr<DartApplicationLoader> m_loader;
301 CachedResourceHandle<CachedScript> m_cachedScript;
302 Dart_Isolate m_isolate;
303 };
304
305
306 void DartApplicationLoader::triggerImport(const String& libraryUrl, const String & importedUrl, Dart_LibraryTag tag)
307 {
308 if (importedUrl == "dart:json") {
309 ASSERT(tag == kImportTag);
310 importBuiltinLibrary(importedUrl, dartJSONLibrarySource);
311 return;
312 }
313 if (importedUrl == "dart:html") {
314 ASSERT(tag == kImportTag);
315 importBuiltinLibrary(importedUrl, dartHTMLLibrarySource);
316 return;
317 }
318 if (importedUrl == "dart:htmlimpl") {
319 ASSERT(tag == kImportTag);
320 importBuiltinLibrary(importedUrl, dartHTMLImplLibrarySource);
321 return;
322 }
323 ASSERT(importedUrl != "dart:dom");
324
325 // Record the importer.
326 if (tag == kImportTag)
327 m_importedLibraries.add(importedUrl);
328 else if (tag == kSourceTag)
329 add(m_importersForSource, importedUrl, libraryUrl);
330 else
331 ASSERT_NOT_REACHED();
332
333 // Request loading of script dependencies.
334 CachedResourceLoader* loader = m_document->cachedResourceLoader();
335 ResourceRequest request(m_document->completeURL(importedUrl));
336 // FIXME: what about charset for this script, maybe use charset of initial s cript tag?
337 CachedScript* cachedScript = loader->requestScript(request, "utf-8");
338
339 cachedScript->addClient(new ScriptLoadCallback(this, cachedScript, Dart_Curr entIsolate()));
340 }
341
342 void DartApplicationLoader::scriptLoadError(const KURL& url)
343 {
344 // FIXME: source file info (must be captured when requesting resource).
345 String sourceFile = "FIXME";
346 // FIXME: line number info (must be captured when requesting resource).
347 int lineNumber = 0;
348 // FIXME: call stack info (must be captured when requesting resource).
349 RefPtr<ScriptCallStack> callStack;
350 String errorMessage = "Cannot load Dart script " + url.string();
351 m_document->reportException(errorMessage, lineNumber, sourceFile, callStack) ;
352 // FIXME: shall we let VM know, so it can inform application some of its
353 // resources cannot be loaded?
354 }
355
356 void DartApplicationLoader::scriptLoadCancelled(const KURL& url)
357 {
358 // FIXME: shall we let VM know, so it can inform application some of its
359 // resources cannot be loaded?
360 }
361
362 void DartApplicationLoader::scriptLoaded(const ScriptSourceCode& script, Dart_Is olate isolate)
363 {
364 // FIXME: we can get rid of imported libs and sources map.
365 DartIsolateState::Scope scope(isolate);
366 load(script.url(), script.source());
367 }
368
369 Dart_Handle DartApplicationLoader::reinject(Dart_Handle library, const String& l ibraryUrl, const String& importedUrl, Dart_LibraryTag tag)
370 {
371 ASSERT(m_sources.contains(importedUrl));
372 const String& source = m_sources.get(importedUrl);
373
374 Dart_Handle dartSource = DartUtilities::stringToDartString(source);
375 if (tag == kImportTag) {
376 Dart_Handle result = Dart_LoadLibrary(DartUtilities::stringToDartString( importedUrl), dartSource);
377 if (Dart_IsError(result))
378 return result;
379 } else if (tag == kSourceTag) {
380 Dart_Handle result = Dart_LoadSource(library, DartUtilities::stringToDar tString(importedUrl), dartSource);
381 if (Dart_IsError(result))
382 return result;
383 } else
384 ASSERT_NOT_REACHED();
385
386 return Dart_NewBoolean(true);
387 }
388
389 void DartApplicationLoader::add(UrlMultiMap& map, const String& key, const Strin g& value)
390 {
391 // We should never have a self dependence.
392 ASSERT(key != value);
393 UrlMultiMap::iterator iter = map.find(key);
394 if (iter == map.end())
395 iter = map.add(key, new UrlSet()).first;
396 UrlSet* set = iter->second;
397 set->add(value);
398 }
399
400 struct ReinjectLoader {
401 static Dart_Handle load(Dart_LibraryTag tag, Dart_Handle library, const Stri ng& libraryUrl, const String& importedUrl)
402 {
403 return currentAppLoader()->reinject(library, libraryUrl, importedUrl, ta g);
404 }
405 };
406
407 void DartApplicationLoader::reinjectSources()
408 {
409 ASSERT(!isLoadingMainIsolate());
410
411 // FIXME: propagate flags as well.
412
413 // Set proper dart loader for child isolate.
414 isolateToDartApplicationLoaderMap().set(Dart_CurrentIsolate(), this);
415
416 ASSERT(m_sources.contains(m_libraryUrl));
417 ScriptLoader<ReinjectLoader>::loadScript(m_libraryUrl, m_sources.get(m_libra ryUrl), m_document);
418 }
419
420 }
OLDNEW
« no previous file with comments | « Source/WebCore/bindings/dart/DartApplicationLoader.h ('k') | Source/WebCore/bindings/dart/DartBindingsCommonIncludes.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698