Index: Source/WebCore/bindings/dart/DartApplicationLoader.cpp |
diff --git a/Source/WebCore/bindings/dart/DartApplicationLoader.cpp b/Source/WebCore/bindings/dart/DartApplicationLoader.cpp |
deleted file mode 100644 |
index 41310c3594d30797663f85cd46f1f679538a8f4e..0000000000000000000000000000000000000000 |
--- a/Source/WebCore/bindings/dart/DartApplicationLoader.cpp |
+++ /dev/null |
@@ -1,338 +0,0 @@ |
-// Copyright (c) 2009, Google Inc. |
-// All rights reserved. |
-// |
-// Redistribution and use in source and binary forms, with or without |
-// modification, are permitted provided that the following conditions are |
-// met: |
-// |
-// * Redistributions of source code must retain the above copyright |
-// notice, this list of conditions and the following disclaimer. |
-// * Redistributions in binary form must reproduce the above |
-// copyright notice, this list of conditions and the following disclaimer |
-// in the documentation and/or other materials provided with the |
-// distribution. |
-// * Neither the name of Google Inc. nor the names of its |
-// contributors may be used to endorse or promote products derived from |
-// this software without specific prior written permission. |
-// |
-// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
-// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
-// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
-// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
-// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
-// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
-// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
-// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
-// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
-// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
-// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
- |
-#include "config.h" |
-#include "DartApplicationLoader.h" |
- |
-#include "CachedResourceClient.h" |
-#include "CachedResourceLoader.h" |
-#include "DOMWindow.h" |
-#include "DartDOMWrapper.h" |
-#include "DartIsolateState.h" |
-#include "DartUtilities.h" |
-#include "Document.h" |
-#include "Frame.h" |
-#include "ScriptCallStack.h" |
-#include "ScriptSourceCode.h" |
- |
-#include <dart_api.h> |
- |
-namespace WebCore { |
- |
-DartApplicationLoader::DartApplicationLoader(Document* document) |
- : m_document(document) |
- , m_libraryUrl() |
- , m_importedLibraries() |
- , m_importersForSource() |
- , m_sources() |
- , m_mainScriptHasBeenLoaded(false) |
-{ |
-} |
- |
-void DartApplicationLoader::load(const String& url, const String& source) |
-{ |
- DartApiScope dartApiScope; |
- |
- if (m_importedLibraries.isEmpty() && m_importersForSource.isEmpty()) |
- loadScript(url, source); |
- else { |
- if (m_importedLibraries.contains(url)) { |
- loadLibrary(url, source); |
- m_importedLibraries.remove(url); |
- } |
- |
- if (m_importersForSource.contains(url)) { |
- loadSource(url, source); |
- m_importersForSource.remove(url); |
- } |
- } |
- |
- m_sources.add(url, source); |
- |
- if (m_importedLibraries.isEmpty() && m_importersForSource.isEmpty() && m_mainScriptHasBeenLoaded) |
- callEntryPoint(); |
-} |
- |
-void DartApplicationLoader::importBuiltinLibrary(const String& url, const String& source) |
-{ |
- Dart_Handle result = Dart_LoadLibrary(DartUtilities::stringToDartString(url), DartUtilities::stringToDartString(source)); |
- if (Dart_IsError(result)) { |
- DartUtilities::reportProblem(m_document, result); |
- return; |
- } |
- m_sources.add(url, source); |
-} |
- |
-IsolateToDartApplicationLoaderMap& isolateToDartApplicationLoaderMap() |
-{ |
- DEFINE_STATIC_LOCAL(IsolateToDartApplicationLoaderMap, map, ()); |
- return map; |
-} |
- |
-static DartApplicationLoader* currentAppLoader() |
-{ |
- IsolateToDartApplicationLoaderMap::iterator it = isolateToDartApplicationLoaderMap().find(Dart_CurrentIsolate()); |
- ASSERT(it != isolateToDartApplicationLoaderMap().end()); |
- return it->second; |
-} |
- |
-template <class Loader> |
-class ScriptLoader { |
-public: |
- static void loadScript(const String& url, const String& source, ScriptExecutionContext* context) |
- { |
- Dart_Handle result = Dart_LoadScript( |
- DartUtilities::stringToDartString(url), |
- DartUtilities::stringToDartString(source), |
- &handler); |
- if (Dart_IsError(result)) |
- DartUtilities::reportProblem(context, result); |
- } |
- |
-private: |
- static Dart_Handle handler(Dart_LibraryTag tag, Dart_Handle library, Dart_Handle urlHandle) |
- { |
- ASSERT(Dart_IsLibrary(library)); |
- |
- String url = DartUtilities::dartStringToString(urlHandle); |
- |
- Dart_Handle libraryURLHandle = Dart_LibraryUrl(library); |
- ASSERT(!Dart_IsError(libraryURLHandle)); |
- const String libraryURL = DartUtilities::dartStringToString(libraryURLHandle); |
- |
- if (tag == kCanonicalizeUrl) { |
- const KURL canonical = KURL(KURL(KURL(), libraryURL), url); |
- return DartUtilities::stringToDartString(canonical.string()); |
- } |
- |
- return Loader::load(tag, library, libraryURL, url); |
- } |
-}; |
- |
-struct InitialLoader { |
- static Dart_Handle load(Dart_LibraryTag tag, Dart_Handle library, const String& libraryUrl, const String& importedUrl) |
- { |
- currentAppLoader()->triggerImport(libraryUrl, importedUrl, tag); |
- return Dart_NewBoolean(true); |
- } |
-}; |
- |
-void DartApplicationLoader::loadScript(const String& url, const String& source) |
-{ |
- ASSERT(m_importedLibraries.isEmpty()); |
- ASSERT(m_importersForSource.isEmpty()); |
- m_libraryUrl = url; |
- ScriptLoader<InitialLoader>::loadScript(url, source, m_document); |
- m_mainScriptHasBeenLoaded = true; |
-} |
- |
-void DartApplicationLoader::loadSource(const String& url, const String& source) |
-{ |
- ASSERT(m_importersForSource.contains(url)); |
- const UrlSet* importers = m_importersForSource.find(url)->second; |
- Dart_Handle dartUrl = DartUtilities::stringToDartString(url); |
- Dart_Handle dartSource = DartUtilities::stringToDartString(source); |
- for (UrlSet::iterator iter = importers->begin(); iter != importers->end(); ++iter) { |
- Dart_Handle library = Dart_LookupLibrary(DartUtilities::stringToDartString(*iter)); |
- if (Dart_IsError(library)) { |
- DartUtilities::reportProblem(m_document, library); |
- return; |
- } |
- Dart_Handle result = Dart_LoadSource(library, dartUrl, dartSource); |
- if (Dart_IsError(result)) { |
- DartUtilities::reportProblem(m_document, result); |
- return; |
- } |
- } |
-} |
- |
-void DartApplicationLoader::loadLibrary(const String& url, const String& source) |
-{ |
- ASSERT(m_importedLibraries.contains(url)); |
- Dart_Handle result = Dart_LoadLibrary(DartUtilities::stringToDartString(url), DartUtilities::stringToDartString(source)); |
- if (Dart_IsError(result)) |
- DartUtilities::reportProblem(m_document, result); |
-} |
- |
-void DartApplicationLoader::callEntryPoint() |
-{ |
- ASSERT(m_document->readyState() != "loading"); |
- Dart_Handle result = Dart_InvokeStatic(topLevelLibrary(), Dart_NewString(""), Dart_NewString("main"), 0, 0); |
- if (Dart_IsError(result)) |
- DartUtilities::reportProblem(m_document, result); |
-} |
- |
-Dart_Handle DartApplicationLoader::topLevelLibrary() |
-{ |
- Dart_Handle library = Dart_LookupLibrary(DartUtilities::stringToDartString(m_libraryUrl)); |
- ASSERT(!Dart_IsError(library)); |
- return library; |
-} |
- |
-Dart_Handle DartApplicationLoader::domLibrary() |
-{ |
- Dart_Handle library = Dart_LookupLibrary(Dart_NewString(DartUtilities::domLibraryName)); |
- ASSERT(!Dart_IsError(library)); |
- return library; |
-} |
- |
-class ScriptLoadCallback : public CachedResourceClient { |
-public: |
- ScriptLoadCallback(DartApplicationLoader* loader, CachedScript* cachedScript, Dart_Isolate isolate) |
- : m_loader(loader) |
- , m_cachedScript(cachedScript) |
- , m_isolate(isolate) |
- { |
- } |
- |
- virtual void notifyFinished(CachedResource* cachedResource) |
- { |
- ASSERT(cachedResource->type() == CachedResource::Script); |
- ASSERT(cachedResource == m_cachedScript.get()); |
- ASSERT(WTF::isMainThread()); |
- |
- if (cachedResource->errorOccurred()) |
- m_loader->scriptLoadError(cachedResource->url()); |
- else if (cachedResource->wasCanceled()) { |
- // FIXME: shall we let VM know, so it can inform application some of its |
- // resources cannot be loaded? |
- } else { |
- // FIXME: we can get rid of imported libs and sources map. |
- ScriptSourceCode sourceCode(m_cachedScript.get()); |
- DartIsolateState::Scope scope(m_isolate); |
- m_loader->load(sourceCode.url(), sourceCode.source()); |
- } |
- |
- m_cachedScript->removeClient(this); |
- delete this; |
- } |
- |
- private: |
- RefPtr<DartApplicationLoader> m_loader; |
- CachedResourceHandle<CachedScript> m_cachedScript; |
- Dart_Isolate m_isolate; |
-}; |
- |
-void DartApplicationLoader::triggerImport(const String& libraryUrl, const String& importedUrl, Dart_LibraryTag tag) |
-{ |
- ASSERT(importedUrl != "dart:dom"); |
- ASSERT(importedUrl != "dart:json"); |
- ASSERT(importedUrl != "dart:html"); |
- ASSERT(importedUrl != "dart:htmlimpl"); |
- |
- // Record the importer. |
- if (tag == kImportTag) |
- m_importedLibraries.add(importedUrl); |
- else if (tag == kSourceTag) |
- add(m_importersForSource, importedUrl, libraryUrl); |
- else |
- ASSERT_NOT_REACHED(); |
- |
- loadScriptResource(importedUrl); |
-} |
- |
-void DartApplicationLoader::loadScriptResource(const String& url) |
-{ |
- // Request loading of script dependencies. |
- CachedResourceLoader* loader = m_document->cachedResourceLoader(); |
- ResourceRequest request(m_document->completeURL(url)); |
- // FIXME: what about charset for this script, maybe use charset of initial script tag? |
- CachedScript* cachedScript = loader->requestScript(request, "utf-8"); |
- if (cachedScript) |
- cachedScript->addClient(new ScriptLoadCallback(this, cachedScript, Dart_CurrentIsolate())); |
- else |
- scriptLoadError(KURL(KURL(), url)); |
-} |
- |
-void DartApplicationLoader::scriptLoadError(const KURL& url) |
-{ |
- // FIXME: source file info (must be captured when requesting resource). |
- String sourceFile = "FIXME"; |
- // FIXME: line number info (must be captured when requesting resource). |
- int lineNumber = 0; |
- // FIXME: call stack info (must be captured when requesting resource). |
- RefPtr<ScriptCallStack> callStack; |
- String errorMessage = "Cannot load Dart script " + url.string(); |
- m_document->reportException(errorMessage, lineNumber, sourceFile, callStack); |
- // FIXME: shall we let VM know, so it can inform application some of its |
- // resources cannot be loaded? |
-} |
- |
-Dart_Handle DartApplicationLoader::reinject(Dart_Handle library, const String& libraryUrl, const String& importedUrl, Dart_LibraryTag tag) |
-{ |
- ASSERT(m_sources.contains(importedUrl)); |
- const String& source = m_sources.get(importedUrl); |
- |
- Dart_Handle dartSource = DartUtilities::stringToDartString(source); |
- if (tag == kImportTag) { |
- Dart_Handle result = Dart_LoadLibrary(DartUtilities::stringToDartString(importedUrl), dartSource); |
- if (Dart_IsError(result)) |
- return result; |
- } else if (tag == kSourceTag) { |
- Dart_Handle result = Dart_LoadSource(library, DartUtilities::stringToDartString(importedUrl), dartSource); |
- if (Dart_IsError(result)) |
- return result; |
- } else |
- ASSERT_NOT_REACHED(); |
- |
- return Dart_NewBoolean(true); |
-} |
- |
-void DartApplicationLoader::add(UrlMultiMap& map, const String& key, const String& value) |
-{ |
- // We should never have a self dependence. |
- ASSERT(key != value); |
- UrlMultiMap::iterator iter = map.find(key); |
- if (iter == map.end()) |
- iter = map.add(key, new UrlSet()).first; |
- UrlSet* set = iter->second; |
- set->add(value); |
-} |
- |
-struct ReinjectLoader { |
- static Dart_Handle load(Dart_LibraryTag tag, Dart_Handle library, const String& libraryUrl, const String& importedUrl) |
- { |
- return currentAppLoader()->reinject(library, libraryUrl, importedUrl, tag); |
- } |
-}; |
- |
-void DartApplicationLoader::reinjectSources() |
-{ |
- ASSERT(!isLoadingMainIsolate()); |
- |
- // FIXME: propagate flags as well. |
- |
- // Set proper dart loader for child isolate. |
- isolateToDartApplicationLoaderMap().set(Dart_CurrentIsolate(), this); |
- |
- ASSERT(m_sources.contains(m_libraryUrl)); |
- ScriptLoader<ReinjectLoader>::loadScript(m_libraryUrl, m_sources.get(m_libraryUrl), m_document); |
-} |
- |
-} |