Index: Source/bindings/dart/DartController.cpp |
diff --git a/Source/bindings/dart/DartController.cpp b/Source/bindings/dart/DartController.cpp |
index 7e462c0989d4fdec8717c9ddba3c4b341bb5cfe2..779a088a033fc40ce6f15cd328cb61183210147d 100644 |
--- a/Source/bindings/dart/DartController.cpp |
+++ b/Source/bindings/dart/DartController.cpp |
@@ -35,6 +35,7 @@ |
#include "HTMLNames.h" |
#include "V8Navigator.h" |
#include "bindings/dart/DartApplicationLoader.h" |
+#include "bindings/dart/DartAsyncLoader.h" |
#include "bindings/dart/DartDOMData.h" |
#include "bindings/dart/DartDOMWrapper.h" |
#include "bindings/dart/DartDebugServer.h" |
@@ -317,57 +318,34 @@ public: |
void dispatchErrorEvent() { } |
}; |
-class ApplicationLoadedCallback : public VoidCallback { |
+class DartSpawnLoadCallback : public DartAsyncLoader::LoadCallback { |
public: |
- ApplicationLoadedCallback(Dart_Isolate isolate, PassRefPtr<Document> document) : m_isolate(isolate), m_document(document) { } |
- |
- bool handleEvent() |
+ DartSpawnLoadCallback(Dart_Isolate isolate, const String& url, Document* originDocument) |
+ : LoadCallback(originDocument) |
+ , m_isolate(isolate) |
+ , m_url(url) |
{ |
- ASSERT(isMainThread()); |
- ASSERT(Dart_CurrentIsolate() == m_isolate); |
- |
- Dart_ExitIsolate(); |
- Dart_IsolateMakeRunnable(m_isolate); |
- Dart_EnterIsolate(m_isolate); |
- |
- return true; |
} |
-private: |
- Dart_Isolate m_isolate; |
- RefPtr<Document> m_document; |
-}; |
- |
-class LoadIsolateSourcesTask : public ScriptExecutionContext::Task { |
-public: |
- LoadIsolateSourcesTask(const char* url, Dart_Isolate isolate, Document* document) |
- : m_url(strdup(url)), m_isolate(isolate), m_document(document) |
- { } |
- |
- ~LoadIsolateSourcesTask() |
+ void ready(PassRefPtr<DartAsyncLoader> loader) |
{ |
- free(m_url); |
- } |
+ RefPtr<DartAsyncLoader> asyncLoader = loader; |
+ RefPtr<SpawnUriErrorEventDispatcher> errorEventDispatcher = adoptRef(new SpawnUriErrorEventDispatcher()); |
- virtual void performTask(ScriptExecutionContext* context) |
- { |
- ASSERT(isMainThread()); |
+ RefPtr<DartApplicationLoader> applicationLoader = adoptRef(new DartApplicationLoader(m_isolate, document(), errorEventDispatcher, asyncLoader, false)); |
- RefPtr<SpawnUriErrorEventDispatcher> errorEventDispatcher = adoptRef(new SpawnUriErrorEventDispatcher()); |
- RefPtr<VoidCallback> applicationLoadedCallback = adoptRef(new ApplicationLoadedCallback(m_isolate, m_document)); |
- RefPtr<DartApplicationLoader> applicationLoader = adoptRef(new DartApplicationLoader(m_isolate, m_document, errorEventDispatcher, applicationLoadedCallback, false)); |
- applicationLoader->loadScriptResource(m_url); |
+ applicationLoader->load(asyncLoader->scriptUrl(), asyncLoader->scriptSource(), asyncLoader->scriptLineNumber()); |
+ Dart_IsolateMakeRunnable(m_isolate); |
} |
private: |
- char* const m_url; |
- const Dart_Isolate m_isolate; |
- Document* m_document; |
+ Dart_Isolate m_isolate; |
+ String m_url; |
}; |
Dart_Isolate DartController::createPureIsolateCallback(const char* scriptURL, const char* entryPoint, void* data, char** errorMsg) |
{ |
- bool needApplicationLoader = false; |
+ bool needAsyncLoader = false; |
{ |
DartApiScope apiScope; |
@@ -389,7 +367,7 @@ Dart_Isolate DartController::createPureIsolateCallback(const char* scriptURL, co |
return 0; |
} |
- needApplicationLoader = strcmp(scriptURL, parentIsolateURL); |
+ needAsyncLoader = strcmp(scriptURL, parentIsolateURL); |
} |
DartDOMData* parentDOMData = static_cast<DartDOMData*>(data); |
@@ -401,8 +379,12 @@ Dart_Isolate DartController::createPureIsolateCallback(const char* scriptURL, co |
if (!isolate) |
return 0; |
- if (needApplicationLoader) { |
- context->postTask(adoptPtr(new LoadIsolateSourcesTask(scriptURL, isolate, document))); |
+ if (needAsyncLoader) { |
+ // We need to request the sources asynchronously. |
+ RefPtr<DartSpawnLoadCallback> callback = adoptRef(new DartSpawnLoadCallback(isolate, scriptURL, document)); |
+ // A DartAsyncLoader will delete itself once all resources are fetched. |
+ DartAsyncLoader* asyncLoader = new DartAsyncLoader(scriptURL, callback); |
+ asyncLoader->fetchScriptResource(scriptURL); |
Dart_ExitIsolate(); |
} else { |
// FIXME: avoid copying parent snapshot. |
@@ -556,7 +538,6 @@ bool DartController::isDartMimeType(const String& mimeType) |
return !mimeType.isEmpty() && types.contains(mimeType); |
} |
- |
class DartScriptRunner : public EventListener { |
public: |
static PassRefPtr<DartScriptRunner> create() |
@@ -659,6 +640,11 @@ public: |
return m_scriptElement->startLineNumber(); |
} |
+ ScriptLoader* loader() |
+ { |
+ return m_scriptElement; |
+ } |
+ |
virtual ~ScriptElementProxy() { } |
protected: |
@@ -728,6 +714,42 @@ private: |
} |
}; |
+class DartDomLoadCallback : public DartAsyncLoader::LoadCallback { |
+public: |
+ DartDomLoadCallback(DartController* controller, const String& url, Document* originDocument, PassRefPtr<ScriptElementProxy> proxy) |
+ : LoadCallback(originDocument, proxy->loader()) |
+ , m_controller(controller) |
+ , m_url(url) |
+ , m_proxy(proxy) |
+ { |
+ } |
+ |
+ void ready(PassRefPtr<DartAsyncLoader> asyncLoader) |
+ { |
+ m_controller->loadAndRunScript(m_url, asyncLoader, m_proxy); |
+ } |
+ |
+private: |
+ DartController* m_controller; |
+ String m_url; |
+ RefPtr<ScriptElementProxy> m_proxy; |
+}; |
+ |
+void DartController::loadAndRunScript(const String& url, PassRefPtr<DartAsyncLoader> loader, PassRefPtr<ScriptElementProxy> proxy) |
+{ |
+ RefPtr<DartAsyncLoader> asyncLoader = loader; |
+ Document* document = frame()->document(); |
+ |
+ createDOMEnabledIsolate(url, "main", document); |
+ |
+ // FIXME: it may make sense to ensure that we'll never call evaluate more than once for the same script tag. |
+ RefPtr<DartApplicationLoader> applicationLoader = adoptRef(new DartApplicationLoader(Dart_CurrentIsolate(), document, proxy, asyncLoader)); |
+ applicationLoader->load(asyncLoader->scriptUrl(), asyncLoader->scriptSource(), asyncLoader->scriptLineNumber()); |
+ |
+ Dart_ExitIsolate(); |
+} |
+ |
+ |
void DartController::loadScripts() |
{ |
if (m_scriptsLoaded) |
@@ -754,16 +776,18 @@ void DartController::loadScripts() |
continue; |
String scriptURL = scriptElementProxy->sourceAttributeValue(); |
- createDOMEnabledIsolate(scriptURL, "main", document); |
+ String url = scriptURL.isEmpty() ? document->url() : scriptURL; |
+ |
+ RefPtr<DartDomLoadCallback> callback = adoptRef(new DartDomLoadCallback(this, url, document, scriptElementProxy)); |
- // FIXME: it may make sense to ensure that we'll never call evaluate more than once for the same script tag. |
- RefPtr<DartApplicationLoader> applicationLoader = adoptRef(new DartApplicationLoader(Dart_CurrentIsolate(), document, scriptElementProxy)); |
+ // A DartAsyncLoader will delete itself once all resources are fetched. |
+ DartAsyncLoader* asyncLoader = new DartAsyncLoader(url, callback); |
+ WTF::OrdinalNumber startLineNumber = scriptElementProxy->startLineNumber(); |
+ intptr_t lineOffset = startLineNumber == OrdinalNumber::beforeFirst() ? 0 : startLineNumber.zeroBasedInt(); |
if (scriptURL.isEmpty()) |
- applicationLoader->load(m_frame->document()->url(), scriptElementProxy->scriptContent(), scriptElementProxy->startLineNumber()); |
+ asyncLoader->process(m_frame->document()->url(), scriptElementProxy->scriptContent(), lineOffset); |
else |
- applicationLoader->loadScriptResource(scriptURL); |
- |
- Dart_ExitIsolate(); |
+ asyncLoader->fetchScriptResource(scriptURL); |
} |
m_scriptsLoaded = true; |