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 a1b4db229982bafd0d9c0a4917fa2cd05fc70146..2769cbb32188cfacbab743f24f927de471630c72 100644 |
--- a/third_party/WebKit/Source/core/dom/ScriptLoader.cpp |
+++ b/third_party/WebKit/Source/core/dom/ScriptLoader.cpp |
@@ -26,11 +26,14 @@ |
#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/Modulator.h" |
+#include "core/dom/ModuleScript.h" |
#include "core/dom/ScriptElementBase.h" |
#include "core/dom/ScriptRunner.h" |
#include "core/dom/ScriptableDocumentParser.h" |
@@ -44,6 +47,7 @@ |
#include "core/html/imports/HTMLImport.h" |
#include "core/html/parser/HTMLParserIdioms.h" |
#include "core/inspector/ConsoleMessage.h" |
+#include "core/loader/modulescript/ModuleScriptFetchRequest.h" |
#include "platform/WebFrameScheduler.h" |
#include "platform/loader/fetch/AccessControlStatus.h" |
#include "platform/loader/fetch/FetchRequest.h" |
@@ -58,6 +62,46 @@ |
namespace blink { |
+class ScriptElementModuleClient |
+ : public GarbageCollectedFinalized<ScriptElementModuleClient>, |
+ public ModuleTreeClient { |
+ USING_GARBAGE_COLLECTED_MIXIN(ScriptElementModuleClient); |
+ |
+ public: |
+ static ScriptElementModuleClient* create(LocalFrame* frame) { |
+ return new ScriptElementModuleClient(frame); |
+ } |
+ virtual ~ScriptElementModuleClient() = default; |
+ |
+ DECLARE_TRACE(); |
+ |
+ private: |
+ ScriptElementModuleClient(LocalFrame* frame) : m_frame(frame) {} |
+ |
+ // Implements ModuleTreeClient |
+ void notifyModuleTreeLoadFinished(ModuleScript*) override; |
+ |
+ Member<LocalFrame> m_frame; |
+}; |
+ |
+void ScriptElementModuleClient::notifyModuleTreeLoadFinished( |
+ ModuleScript* moduleScript) { |
+ printf("notifyFinishedModuleTree!!! moduleScript: %p\n", moduleScript); |
+ if (!moduleScript) |
+ return; |
+ DCHECK_EQ(moduleScript->instantiationState(), |
+ ModuleInstantiationState::Instantiated); |
+ |
+ // TODO(kouhei): Actually we should just return w/ the prepared script here. |
+ // In this prototype, we will execute the module immediately just for fun. :) |
+ Modulator::from(m_frame.get())->executeModule(moduleScript->record()); |
+} |
+ |
+DEFINE_TRACE(ScriptElementModuleClient) { |
+ visitor->trace(m_frame); |
+ ModuleTreeClient::trace(visitor); |
+} |
+ |
ScriptLoader::ScriptLoader(ScriptElementBase* element, |
bool parserInserted, |
bool alreadyStarted, |
@@ -104,6 +148,7 @@ ScriptLoader::~ScriptLoader() {} |
DEFINE_TRACE(ScriptLoader) { |
visitor->trace(m_element); |
visitor->trace(m_resource); |
+ visitor->trace(m_elementModuleClient); |
visitor->trace(m_pendingScript); |
PendingScriptClient::trace(visitor); |
} |
@@ -517,7 +562,8 @@ bool ScriptLoader::fetchScript(const String& sourceUrl, |
// 21. "If the element has a src content attribute, run these substeps:" |
if (!stripLeadingAndTrailingHTMLSpaces(sourceUrl).isEmpty()) { |
// 21.4. "Parse src relative to the element's node document." |
- ResourceRequest resourceRequest(elementDocument->completeURL(sourceUrl)); |
+ KURL url = elementDocument->completeURL(sourceUrl); |
+ ResourceRequest resourceRequest(url); |
// [Intervention] |
if (m_documentWriteIntervention == |
@@ -537,6 +583,40 @@ bool ScriptLoader::fetchScript(const String& sourceUrl, |
// 16. "Let module script credentials mode be determined by switching |
// on CORS setting:" |
// TODO(hiroshige): Implement this step for "module". |
+ if (RuntimeEnabledFeatures::moduleScriptsEnabled() && |
+ m_element->typeAttributeValue() == "module") { |
+ m_elementModuleClient = ScriptElementModuleClient::create( |
+ elementDocument->contextDocument()->frame()); |
+ |
+ String nonce; |
+ if (m_element->isNonceableElement()) |
+ nonce = m_element->nonce(); |
+ ParserDisposition parserState = |
+ isParserInserted() ? ParserInserted : NotParserInserted; |
+ |
+ // 16. "Let module script credentials mode be determined by switching |
+ // on CORS setting:" |
+ WebURLRequest::FetchCredentialsMode credentialsMode; |
+ switch (crossOrigin) { |
+ case CrossOriginAttributeNotSet: |
+ credentialsMode = WebURLRequest::FetchCredentialsModeOmit; |
+ break; |
+ case CrossOriginAttributeAnonymous: |
+ credentialsMode = WebURLRequest::FetchCredentialsModeSameOrigin; |
+ break; |
+ case CrossOriginAttributeUseCredentials: |
+ credentialsMode = WebURLRequest::FetchCredentialsModeInclude; |
+ break; |
+ default: |
+ NOTREACHED(); |
+ } |
+ |
+ ModuleScriptFetchRequest moduleRequest(url, nonce, parserState, |
+ credentialsMode); |
+ Modulator::from(elementDocument->frame()) |
+ ->fetchTree(moduleRequest, m_elementModuleClient); |
+ return true; |
+ } |
// 21.6, "classic": "Fetch a classic script given ... CORS setting |
// ... and encoding." |