Chromium Code Reviews| 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..28248a65546f4581b64239ba9b502c45e20f1066 100644 |
| --- a/third_party/WebKit/Source/core/dom/ScriptLoader.cpp |
| +++ b/third_party/WebKit/Source/core/dom/ScriptLoader.cpp |
| @@ -26,6 +26,7 @@ |
| #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" |
| @@ -34,6 +35,7 @@ |
| #include "core/dom/ScriptLoaderClient.h" |
| #include "core/dom/ScriptRunner.h" |
| #include "core/dom/ScriptableDocumentParser.h" |
| +#include "core/dom/ModuleMap.h" |
| #include "core/dom/Text.h" |
| #include "core/events/Event.h" |
| #include "core/fetch/AccessControlStatus.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()); |
| + ScriptState* scriptState = |
| + ScriptState::forMainWorld(contextDocument->frame()); |
| + DCHECK(scriptState); |
| + ScriptState::Scope scope(scriptState); |
| + |
| + ScriptModule module = loader()->scriptModule(); |
| + CHECK(module.instantiate(scriptState)); |
| + module.evaluate(scriptState); |
|
kouhei (in TOK)
2016/12/13 05:42:50
This somehow crashes w/in V8
|
| + |
| + 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,22 @@ 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()->fetch(url, m_elementModuleClient); |
| + return true; |
| + } |
| + |
| + FetchRequest request(ResourceRequest(url), m_element->localName()); |
| CrossOriginAttributeValue crossOrigin = crossOriginAttributeValue( |
| m_element->fastGetAttribute(HTMLNames::crossoriginAttr)); |