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 "platform/loader/fetch/ResourceFetcher.h" |
| 17 |
| 18 namespace blink { |
| 19 |
| 20 ModulatorImpl* ModulatorImpl::Create(RefPtr<ScriptState> script_state, |
| 21 Document& document) { |
| 22 return new ModulatorImpl( |
| 23 std::move(script_state), |
| 24 TaskRunnerHelper::Get(TaskType::kNetworking, &document), |
| 25 document.Fetcher()); |
| 26 } |
| 27 |
| 28 ModulatorImpl::ModulatorImpl(RefPtr<ScriptState> script_state, |
| 29 RefPtr<WebTaskRunner> task_runner, |
| 30 ResourceFetcher* fetcher) |
| 31 : script_state_(std::move(script_state)), |
| 32 task_runner_(std::move(task_runner)), |
| 33 fetcher_(fetcher), |
| 34 map_(this, ModuleMap::Create(this)), |
| 35 loader_registry_(ModuleScriptLoaderRegistry::Create()), |
| 36 script_module_resolver_(ScriptModuleResolverImpl::Create(this)) { |
| 37 DCHECK(script_state_); |
| 38 DCHECK(task_runner_); |
| 39 DCHECK(fetcher_); |
| 40 } |
| 41 |
| 42 ModulatorImpl::~ModulatorImpl() {} |
| 43 |
| 44 ReferrerPolicy ModulatorImpl::GetReferrerPolicy() { |
| 45 return GetExecutionContext()->GetReferrerPolicy(); |
| 46 } |
| 47 |
| 48 SecurityOrigin* ModulatorImpl::GetSecurityOrigin() { |
| 49 return GetExecutionContext()->GetSecurityOrigin(); |
| 50 } |
| 51 |
| 52 void ModulatorImpl::FetchTree(const ModuleScriptFetchRequest& request, |
| 53 ModuleTreeClient* client) { |
| 54 // Step 1. Perform the internal module script graph fetching procedure given |
| 55 // url, settings object, destination, cryptographic nonce, parser state, |
| 56 // credentials mode, settings object, a new empty list, "client", and with the |
| 57 // top-level module fetch flag set. If the caller of this algorithm specified |
| 58 // custom perform the fetch steps, pass those along as well. |
| 59 |
| 60 // Note: "Fetch a module script graph" algorithm doesn't have "referrer" as |
| 61 // its argument. |
| 62 DCHECK(request.GetReferrer().IsNull()); |
| 63 |
| 64 AncestorList empty_ancestor_list; |
| 65 FetchTreeInternal(request, empty_ancestor_list, |
| 66 ModuleGraphLevel::kTopLevelModuleFetch, client); |
| 67 |
| 68 // Step 2. When the internal module script graph fetching procedure |
| 69 // asynchronously completes with result, asynchronously complete this |
| 70 // algorithm with result. |
| 71 // Note: We delegate to ModuleTreeLinker to notify ModuleTreeClient. |
| 72 } |
| 73 |
| 74 void ModulatorImpl::FetchTreeInternal(const ModuleScriptFetchRequest& request, |
| 75 const AncestorList& ancestor_list, |
| 76 ModuleGraphLevel level, |
| 77 ModuleTreeClient* client) { |
| 78 NOTIMPLEMENTED(); |
| 79 } |
| 80 |
| 81 void ModulatorImpl::FetchSingle(const ModuleScriptFetchRequest& request, |
| 82 ModuleGraphLevel level, |
| 83 SingleModuleClient* client) { |
| 84 map_->FetchSingleModuleScript(request, level, client); |
| 85 } |
| 86 |
| 87 void ModulatorImpl::FetchNewSingleModule( |
| 88 const ModuleScriptFetchRequest& request, |
| 89 ModuleGraphLevel level, |
| 90 ModuleScriptLoaderClient* client) { |
| 91 loader_registry_->Fetch(request, level, this, fetcher_.Get(), client); |
| 92 } |
| 93 |
| 94 ModuleScript* ModulatorImpl::GetFetchedModuleScript(const KURL& url) { |
| 95 return map_->GetFetchedModuleScript(url); |
| 96 } |
| 97 |
| 98 ScriptModule ModulatorImpl::CompileModule( |
| 99 const String& provided_source, |
| 100 const String& url_str, |
| 101 AccessControlStatus access_control_status) { |
| 102 // Implements Steps 3-6 of |
| 103 // https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-module-sc
ript |
| 104 |
| 105 // Step 3. Let realm be the provided environment settings object's Realm. |
| 106 // Note: Realm is v8::Context. |
| 107 |
| 108 // Step 4. If scripting is disabled for the given environment settings |
| 109 // object's responsible browsing context, then let script source be the empty |
| 110 // string. Otherwise, let script source be the provided script source. |
| 111 String script_source; |
| 112 if (GetExecutionContext()->CanExecuteScripts(kAboutToExecuteScript)) |
| 113 script_source = provided_source; |
| 114 |
| 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(script_state_.Get()); |
| 120 return ScriptModule::Compile(script_state_->GetIsolate(), script_source, |
| 121 url_str, access_control_status); |
| 122 } |
| 123 |
| 124 ScriptValue ModulatorImpl::InstantiateModule(ScriptModule script_module) { |
| 125 ScriptState::Scope scope(script_state_.Get()); |
| 126 return script_module.Instantiate(script_state_.Get()); |
| 127 } |
| 128 |
| 129 Vector<String> ModulatorImpl::ModuleRequestsFromScriptModule( |
| 130 ScriptModule script_module) { |
| 131 ScriptState::Scope scope(script_state_.Get()); |
| 132 return script_module.ModuleRequests(script_state_.Get()); |
| 133 } |
| 134 |
| 135 inline ExecutionContext* ModulatorImpl::GetExecutionContext() const { |
| 136 return ExecutionContext::From(script_state_.Get()); |
| 137 } |
| 138 |
| 139 DEFINE_TRACE(ModulatorImpl) { |
| 140 Modulator::Trace(visitor); |
| 141 visitor->Trace(fetcher_); |
| 142 visitor->Trace(map_); |
| 143 visitor->Trace(loader_registry_); |
| 144 visitor->Trace(script_module_resolver_); |
| 145 } |
| 146 |
| 147 DEFINE_TRACE_WRAPPERS(ModulatorImpl) { |
| 148 visitor->TraceWrappers(map_); |
| 149 } |
| 150 |
| 151 } // namespace blink |
OLD | NEW |