OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/dom/ModulatorImpl.h" |
| 6 |
| 7 #include "core/dom/Document.h" |
| 8 #include "core/dom/ExecutionContext.h" |
| 9 #include "core/dom/ModuleMap.h" |
| 10 #include "core/dom/ModuleScript.h" |
| 11 #include "core/dom/ScriptModuleResolverImpl.h" |
| 12 #include "core/dom/TaskRunnerHelper.h" |
| 13 #include "core/frame/LocalFrame.h" |
| 14 #include "core/loader/modulescript//ModuleScriptFetchRequest.h" |
| 15 #include "core/loader/modulescript/ModuleScriptLoaderRegistry.h" |
| 16 #include "core/loader/modulescript/ModuleTreeLinkerRegistry.h" |
| 17 #include "platform/loader/fetch/ResourceFetcher.h" |
| 18 |
| 19 namespace blink { |
| 20 |
| 21 ModulatorImpl* ModulatorImpl::create(RefPtr<ScriptState> scriptState, |
| 22 Document& document) { |
| 23 return new ModulatorImpl( |
| 24 std::move(scriptState), |
| 25 TaskRunnerHelper::get(TaskType::Networking, &document), &document, |
| 26 document.fetcher()); |
| 27 } |
| 28 |
| 29 ModulatorImpl::ModulatorImpl(RefPtr<ScriptState> scriptState, |
| 30 RefPtr<WebTaskRunner> taskRunner, |
| 31 ExecutionContext* executionContext, |
| 32 ResourceFetcher* fetcher) |
| 33 : m_scriptState(std::move(scriptState)), |
| 34 m_taskRunner(std::move(taskRunner)), |
| 35 m_executionContext(executionContext), |
| 36 m_fetcher(fetcher), |
| 37 m_map(this, ModuleMap::create(this)), |
| 38 m_loaderRegistry(ModuleScriptLoaderRegistry::create()), |
| 39 m_treeLinkerRegistry(ModuleTreeLinkerRegistry::create()), |
| 40 m_scriptModuleResolver(ScriptModuleResolverImpl::create(this)) { |
| 41 DCHECK(m_scriptState); |
| 42 DCHECK(m_taskRunner); |
| 43 DCHECK(m_executionContext); |
| 44 DCHECK(m_fetcher); |
| 45 } |
| 46 |
| 47 ModulatorImpl::~ModulatorImpl() {} |
| 48 |
| 49 ReferrerPolicy ModulatorImpl::referrerPolicy() { |
| 50 return m_executionContext->getReferrerPolicy(); |
| 51 } |
| 52 |
| 53 SecurityOrigin* ModulatorImpl::securityOrigin() { |
| 54 return m_executionContext->getSecurityOrigin(); |
| 55 } |
| 56 |
| 57 void ModulatorImpl::fetchTree(const ModuleScriptFetchRequest& request, |
| 58 ModuleTreeClient* client) { |
| 59 // Step 1. Perform the internal module script graph fetching procedure given |
| 60 // url, settings object, destination, cryptographic nonce, parser state, |
| 61 // credentials mode, settings object, a new empty list, "client", and with the |
| 62 // top-level module fetch flag set. If the caller of this algorithm specified |
| 63 // custom perform the fetch steps, pass those along as well. |
| 64 |
| 65 // Note: "Fetch a module script graph" algorithm doesn't have "referrer" as |
| 66 // its argument. |
| 67 DCHECK(request.referrer().isNull()); |
| 68 |
| 69 AncestorList emptyAncestorList; |
| 70 fetchTreeInternal(request, emptyAncestorList, |
| 71 ModuleGraphLevel::TopLevelModuleFetch, client); |
| 72 |
| 73 // Step 2. When the internal module script graph fetching procedure |
| 74 // asynchronously completes with result, asynchronously complete this |
| 75 // algorithm with result. |
| 76 // Note: We delegate to ModuleTreeLinker to notify ModuleTreeClient. |
| 77 } |
| 78 |
| 79 void ModulatorImpl::fetchTreeInternal(const ModuleScriptFetchRequest& request, |
| 80 const AncestorList& ancestorList, |
| 81 ModuleGraphLevel level, |
| 82 ModuleTreeClient* client) { |
| 83 m_treeLinkerRegistry->fetch(request, ancestorList, level, this, client); |
| 84 } |
| 85 |
| 86 void ModulatorImpl::fetchSingle(const ModuleScriptFetchRequest& request, |
| 87 ModuleGraphLevel level, |
| 88 SingleModuleClient* client) { |
| 89 m_map->fetchSingleModuleScript(request, level, client); |
| 90 } |
| 91 |
| 92 void ModulatorImpl::fetchNewSingleModule( |
| 93 const ModuleScriptFetchRequest& request, |
| 94 ModuleGraphLevel level, |
| 95 ModuleScriptLoaderClient* client) { |
| 96 m_loaderRegistry->fetch(request, level, this, m_fetcher.get(), client); |
| 97 } |
| 98 |
| 99 ModuleScript* ModulatorImpl::getFetchedModuleScript(const KURL& url) { |
| 100 return m_map->getFetchedModuleScript(url); |
| 101 } |
| 102 |
| 103 ScriptModule ModulatorImpl::compileModule(const String& script, |
| 104 const String& urlStr) { |
| 105 // Implements Steps 3-6 of |
| 106 // https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-module-sc
ript |
| 107 |
| 108 // Step 3. Let realm be the provided environment settings object's Realm. |
| 109 // Note: Realm is v8::Context. |
| 110 // Step 4. If scripting is disabled for the given environment settings |
| 111 // object's responsible browsing context, then let script source be the empty |
| 112 // string. Otherwise, let script source be the provided script source. |
| 113 // TODO(kouhei): if(m_executionContext->canExecuteScripts) after the CL |
| 114 // landed. |
| 115 // Step 5. Let result be ParseModule(script source, realm, script). |
| 116 // Step 6. If result is a List of errors, report the exception given by the |
| 117 // first element of result for script, return null, and abort these steps. |
| 118 // Note: reporting is routed via V8Initializer::messageHandlerInMainThread. |
| 119 ScriptState::Scope scope(m_scriptState.get()); |
| 120 return ScriptModule::compile(m_scriptState->isolate(), script, urlStr); |
| 121 } |
| 122 |
| 123 ScriptValue ModulatorImpl::instantiateModule(ScriptModule scriptModule) { |
| 124 ScriptState::Scope scope(m_scriptState.get()); |
| 125 return scriptModule.instantiate(m_scriptState.get()); |
| 126 } |
| 127 |
| 128 Vector<String> ModulatorImpl::moduleRequestsFromScriptModule( |
| 129 ScriptModule scriptModule) { |
| 130 ScriptState::Scope scope(m_scriptState.get()); |
| 131 return scriptModule.moduleRequests(m_scriptState.get()); |
| 132 } |
| 133 |
| 134 void ModulatorImpl::executeModule(ScriptModule scriptModule) { |
| 135 ScriptState::Scope scope(m_scriptState.get()); |
| 136 scriptModule.evaluate(m_scriptState.get()); |
| 137 } |
| 138 |
| 139 DEFINE_TRACE(ModulatorImpl) { |
| 140 Modulator::trace(visitor); |
| 141 visitor->trace(m_executionContext); |
| 142 visitor->trace(m_fetcher); |
| 143 visitor->trace(m_map); |
| 144 visitor->trace(m_loaderRegistry); |
| 145 visitor->trace(m_treeLinkerRegistry); |
| 146 visitor->trace(m_scriptModuleResolver); |
| 147 } |
| 148 |
| 149 DEFINE_TRACE_WRAPPERS(ModulatorImpl) { |
| 150 visitor->traceWrappers(m_map); |
| 151 } |
| 152 |
| 153 } // namespace blink |
OLD | NEW |