Index: third_party/WebKit/Source/core/dom/ScriptLoader.cpp |
diff --git a/third_party/WebKit/Source/core/dom/ScriptLoader.cpp b/third_party/WebKit/Source/core/dom/ScriptLoader.cpp |
index a02e2ca2086a275cb4d1cf3ef5647e11e0236ce3..fadd65b3993f026f200fac5c5d3e4cb7537a2939 100644 |
--- a/third_party/WebKit/Source/core/dom/ScriptLoader.cpp |
+++ b/third_party/WebKit/Source/core/dom/ScriptLoader.cpp |
@@ -26,11 +26,13 @@ |
#include "bindings/core/v8/ScriptController.h" |
#include "bindings/core/v8/ScriptSourceCode.h" |
+#include "bindings/core/v8/ScriptState.h" |
#include "core/HTMLNames.h" |
#include "core/SVGNames.h" |
#include "core/dom/Document.h" |
#include "core/dom/DocumentParserTiming.h" |
#include "core/dom/IgnoreDestructiveWriteCountIncrementer.h" |
+#include "core/dom/ModuleMap.h" |
#include "core/dom/ScriptLoaderClient.h" |
#include "core/dom/ScriptRunner.h" |
#include "core/dom/ScriptableDocumentParser.h" |
@@ -60,6 +62,64 @@ |
namespace blink { |
+class ScriptElementModuleLoaderClient |
+ : public GarbageCollectedFinalized<ScriptElementModuleLoaderClient>, |
+ public ModuleLoaderClient { |
+ USING_GARBAGE_COLLECTED_MIXIN(ScriptElementModuleLoaderClient); |
+ |
+ public: |
+ static ScriptElementModuleLoaderClient* create(Element* element) { |
+ return new ScriptElementModuleLoaderClient(element); |
+ } |
+ virtual ~ScriptElementModuleLoaderClient() {} |
+ |
+ DECLARE_VIRTUAL_TRACE(); |
+ |
+ private: |
+ ScriptElementModuleLoaderClient(Element* element) : m_element(element) {} |
+ |
+ // Implements ModuleLoaderClient |
+ void notifyFinished() override; |
+ |
+ Member<Element> m_element; |
+}; |
+ |
+void ScriptElementModuleLoaderClient::notifyFinished() { |
+ DCHECK(loader()); |
+ DCHECK(loader()->hasFinished()); |
+ |
+ // The code below is entirely wrong o(`ω´*)o |
+ // TODO(kouhei? dominicc?): Fix this as a part of implementing "fetch a module |
+ // script graph" algorithm. |
+ if (!loader()->hasFinishedSuccessfully()) { |
+ printf( |
+ "ScriptElementModuleLoaderClient::notifyFinished: load has failed\n"); |
+ return; |
+ } |
+ printf("ScriptElementModuleLoaderClient::notifyFinished: load successful\n"); |
+ |
+ Document* contextDocument = m_element->document().contextDocument(); |
+ DCHECK(contextDocument); |
+ |
+ // Below should belong to ScriptController.cpp |
+ DCHECK(contextDocument->frame()); |
dominicc (has gone to gerrit)
2016/12/13 07:45:29
Who guarantees this?
kouhei (in TOK)
2016/12/15 04:56:27
After this is moved to ScriptController, ScriptCon
|
+ ScriptState* scriptState = |
+ ScriptState::forMainWorld(contextDocument->frame()); |
+ DCHECK(scriptState); |
+ ScriptState::Scope scope(scriptState); |
+ |
+ ScriptModule module = loader()->scriptModule(); |
+ CHECK(module.instantiate(scriptState)); |
+ module.evaluate(scriptState); |
+ |
+ dispose(); |
+} |
+ |
+DEFINE_TRACE(ScriptElementModuleLoaderClient) { |
+ visitor->trace(m_element); |
+ ModuleLoaderClient::trace(visitor); |
+} |
+ |
ScriptLoader::ScriptLoader(Element* element, |
bool parserInserted, |
bool alreadyStarted, |
@@ -90,6 +150,7 @@ ScriptLoader::~ScriptLoader() {} |
DEFINE_TRACE(ScriptLoader) { |
visitor->trace(m_element); |
visitor->trace(m_resource); |
+ visitor->trace(m_elementModuleClient); |
visitor->trace(m_pendingScript); |
ScriptResourceClient::trace(visitor); |
} |
@@ -260,6 +321,7 @@ bool ScriptLoader::prepareScript(const TextPosition& scriptStartPosition, |
if (m_documentWriteIntervention == |
DocumentWriteIntervention::FetchDocWrittenScriptDeferIdle) |
defer = FetchRequest::IdleLoad; |
+ |
if (!fetchScript(client->sourceAttributeValue(), defer)) |
return false; |
} |
@@ -335,10 +397,23 @@ bool ScriptLoader::fetchScript(const String& sourceUrl, |
return false; |
DCHECK(!m_resource); |
+ |
if (!stripLeadingAndTrailingHTMLSpaces(sourceUrl).isEmpty()) { |
- FetchRequest request( |
- ResourceRequest(elementDocument->completeURL(sourceUrl)), |
- m_element->localName()); |
+ KURL url = elementDocument->completeURL(sourceUrl); |
+ |
+ if (RuntimeEnabledFeatures::moduleScriptsEnabled() && |
+ client()->typeAttributeValue() == "module") { |
+ m_elementModuleClient = |
+ ScriptElementModuleLoaderClient::create(m_element); |
+ |
+ // TODO(kouhei,dominicc): Below should trigger "fetch a module script |
+ // graph", not "fetch a single module script" algorithm. |
+ elementDocument->ensureModuleMap()->fetchSingle(url, |
+ m_elementModuleClient); |
+ return true; |
+ } |
+ |
+ FetchRequest request(ResourceRequest(url), m_element->localName()); |
CrossOriginAttributeValue crossOrigin = crossOriginAttributeValue( |
m_element->fastGetAttribute(HTMLNames::crossoriginAttr)); |