diff options
Diffstat (limited to 'chromium/third_party/blink/renderer/core/loader/modulescript')
10 files changed, 231 insertions, 381 deletions
diff --git a/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_creation_params.h b/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_creation_params.h index 90675dd9c9d..73e41153e50 100644 --- a/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_creation_params.h +++ b/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_creation_params.h @@ -5,14 +5,13 @@ #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_MODULESCRIPT_MODULE_SCRIPT_CREATION_PARAMS_H_ #define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_MODULESCRIPT_MODULE_SCRIPT_CREATION_PARAMS_H_ -#include "third_party/abseil-cpp/absl/types/optional.h" #include "third_party/blink/public/platform/web_url_request.h" #include "third_party/blink/renderer/bindings/core/v8/script_source_location_type.h" #include "third_party/blink/renderer/bindings/core/v8/script_streamer.h" #include "third_party/blink/renderer/core/script/modulator.h" #include "third_party/blink/renderer/platform/bindings/parkable_string.h" #include "third_party/blink/renderer/platform/heap/persistent.h" -#include "third_party/blink/renderer/platform/loader/fetch/cached_metadata_handler.h" +#include "third_party/blink/renderer/platform/loader/fetch/url_loader/cached_metadata_handler.h" #include "third_party/blink/renderer/platform/weborigin/kurl.h" #include "third_party/blink/renderer/platform/wtf/cross_thread_copier.h" #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" diff --git a/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_fetcher.h b/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_fetcher.h index de102865eae..ca23d99a34e 100644 --- a/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_fetcher.h +++ b/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_fetcher.h @@ -5,7 +5,6 @@ #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_MODULESCRIPT_MODULE_SCRIPT_FETCHER_H_ #define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_MODULESCRIPT_MODULE_SCRIPT_FETCHER_H_ -#include "third_party/abseil-cpp/absl/types/optional.h" #include "third_party/blink/renderer/core/core_export.h" #include "third_party/blink/renderer/core/loader/modulescript/module_script_creation_params.h" #include "third_party/blink/renderer/core/loader/resource/script_resource.h" diff --git a/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_loader.h b/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_loader.h index 0328a325cea..4bb9ac940d8 100644 --- a/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_loader.h +++ b/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_loader.h @@ -6,7 +6,6 @@ #define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_MODULESCRIPT_MODULE_SCRIPT_LOADER_H_ #include "base/dcheck_is_on.h" -#include "base/macros.h" #include "third_party/blink/public/platform/web_url_request.h" #include "third_party/blink/renderer/core/core_export.h" #include "third_party/blink/renderer/core/loader/modulescript/module_script_creation_params.h" @@ -48,6 +47,8 @@ class CORE_EXPORT ModuleScriptLoader final const ScriptFetchOptions&, ModuleScriptLoaderRegistry*, ModuleScriptLoaderClient*); + ModuleScriptLoader(const ModuleScriptLoader&) = delete; + ModuleScriptLoader& operator=(const ModuleScriptLoader&) = delete; ~ModuleScriptLoader(); static void Fetch(const ModuleScriptFetchRequest&, @@ -99,8 +100,6 @@ class CORE_EXPORT ModuleScriptLoader final #if DCHECK_IS_ON() KURL url_; #endif - - DISALLOW_COPY_AND_ASSIGN(ModuleScriptLoader); }; } // namespace blink diff --git a/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_loader_test.cc b/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_loader_test.cc index 933391b7ea9..3943ae55eec 100644 --- a/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_loader_test.cc +++ b/chromium/third_party/blink/renderer/core/loader/modulescript/module_script_loader_test.cc @@ -87,18 +87,6 @@ class ModuleScriptLoaderTestModulator final : public DummyModulator { ScriptState* GetScriptState() override { return script_state_; } - void SetModuleRequests(const Vector<String>& requests) { - requests_.clear(); - for (const String& request : requests) { - requests_.emplace_back(request, TextPosition::MinimumPosition(), - Vector<ImportAssertion>()); - } - } - Vector<ModuleRequest> ModuleRequestsFromModuleRecord( - v8::Local<v8::Module>) override { - return requests_; - } - ModuleScriptFetcher* CreateModuleScriptFetcher( ModuleScriptCustomFetchType custom_fetch_type, base::PassKey<ModuleScriptLoader> pass_key) override { @@ -117,7 +105,6 @@ class ModuleScriptLoaderTestModulator final : public DummyModulator { private: Member<ScriptState> script_state_; - Vector<ModuleRequest> requests_; }; void ModuleScriptLoaderTestModulator::Trace(Visitor* visitor) const { @@ -128,10 +115,10 @@ void ModuleScriptLoaderTestModulator::Trace(Visitor* visitor) const { } // namespace class ModuleScriptLoaderTest : public PageTestBase { - DISALLOW_COPY_AND_ASSIGN(ModuleScriptLoaderTest); - public: ModuleScriptLoaderTest(); + ModuleScriptLoaderTest(const ModuleScriptLoaderTest&) = delete; + ModuleScriptLoaderTest& operator=(const ModuleScriptLoaderTest&) = delete; void SetUp() override; void InitializeForDocument(); @@ -424,7 +411,6 @@ void ModuleScriptLoaderTest::TestInvalidSpecifier( TestModuleScriptLoaderClient* client) { auto* registry = MakeGarbageCollected<ModuleScriptLoaderRegistry>(); KURL url("data:text/javascript,import 'invalid';export default 'grapes';"); - GetModulator()->SetModuleRequests({"invalid"}); ModuleScriptLoader::Fetch( ModuleScriptFetchRequest::CreateForTest(url, ModuleType::kJavaScript), fetcher_, ModuleGraphLevel::kTopLevelModuleFetch, GetModulator(), diff --git a/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker.cc b/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker.cc index 024aa5cb96a..5d10d269a24 100644 --- a/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker.cc +++ b/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker.cc @@ -7,11 +7,14 @@ #include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-blink.h" #include "third_party/blink/renderer/bindings/core/v8/module_record.h" #include "third_party/blink/renderer/bindings/core/v8/module_request.h" +#include "third_party/blink/renderer/core/execution_context/execution_context.h" +#include "third_party/blink/renderer/core/frame/web_feature.h" #include "third_party/blink/renderer/core/loader/modulescript/module_script_creation_params.h" #include "third_party/blink/renderer/core/loader/modulescript/module_script_fetch_request.h" #include "third_party/blink/renderer/core/loader/modulescript/module_tree_linker_registry.h" #include "third_party/blink/renderer/core/script/module_script.h" #include "third_party/blink/renderer/platform/bindings/v8_throw_exception.h" +#include "third_party/blink/renderer/platform/instrumentation/use_counter.h" #include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h" #include "third_party/blink/renderer/platform/loader/fetch/resource_loading_log.h" #include "third_party/blink/renderer/platform/weborigin/security_policy.h" @@ -48,43 +51,6 @@ struct ModuleScriptFetchTarget { } // namespace -void ModuleTreeLinker::Fetch( - const KURL& url, - const ModuleType& module_type, - ResourceFetcher* fetch_client_settings_object_fetcher, - mojom::blink::RequestContextType context_type, - network::mojom::RequestDestination destination, - const ScriptFetchOptions& options, - Modulator* modulator, - ModuleScriptCustomFetchType custom_fetch_type, - ModuleTreeLinkerRegistry* registry, - ModuleTreeClient* client) { - ModuleTreeLinker* fetcher = MakeGarbageCollected<ModuleTreeLinker>( - fetch_client_settings_object_fetcher, context_type, destination, - modulator, custom_fetch_type, registry, client); - registry->AddFetcher(fetcher); - fetcher->FetchRoot(url, module_type, options); - DCHECK(fetcher->IsFetching()); -} - -void ModuleTreeLinker::FetchDescendantsForInlineScript( - ModuleScript* module_script, - ResourceFetcher* fetch_client_settings_object_fetcher, - mojom::blink::RequestContextType context_type, - network::mojom::RequestDestination destination, - Modulator* modulator, - ModuleScriptCustomFetchType custom_fetch_type, - ModuleTreeLinkerRegistry* registry, - ModuleTreeClient* client) { - DCHECK(module_script); - ModuleTreeLinker* fetcher = MakeGarbageCollected<ModuleTreeLinker>( - fetch_client_settings_object_fetcher, context_type, destination, - modulator, custom_fetch_type, registry, client); - registry->AddFetcher(fetcher); - fetcher->FetchRootInline(module_script); - DCHECK(fetcher->IsFetching()); -} - ModuleTreeLinker::ModuleTreeLinker( ResourceFetcher* fetch_client_settings_object_fetcher, mojom::blink::RequestContextType context_type, @@ -92,7 +58,8 @@ ModuleTreeLinker::ModuleTreeLinker( Modulator* modulator, ModuleScriptCustomFetchType custom_fetch_type, ModuleTreeLinkerRegistry* registry, - ModuleTreeClient* client) + ModuleTreeClient* client, + base::PassKey<ModuleTreeLinkerRegistry>) : fetch_client_settings_object_fetcher_( fetch_client_settings_object_fetcher), context_type_(context_type), @@ -175,7 +142,7 @@ void ModuleTreeLinker::AdvanceState(State new_state) { } #endif - registry_->ReleaseFinishedFetcher(this); + registry_->ReleaseFinishedLinker(this); // <spec label="IMSGF" step="6">When the appropriate algorithm // asynchronously completes with final result, asynchronously complete this @@ -188,7 +155,8 @@ void ModuleTreeLinker::AdvanceState(State new_state) { // #fetch-a-module-worker-script-tree. void ModuleTreeLinker::FetchRoot(const KURL& original_url, ModuleType module_type, - const ScriptFetchOptions& options) { + const ScriptFetchOptions& options, + base::PassKey<ModuleTreeLinkerRegistry>) { #if DCHECK_IS_ON() original_url_ = original_url; module_type_ = module_type; @@ -262,7 +230,9 @@ void ModuleTreeLinker::FetchRoot(const KURL& original_url, // <specdef // href="https://html.spec.whatwg.org/C/#fetch-an-inline-module-script-graph"> -void ModuleTreeLinker::FetchRootInline(ModuleScript* module_script) { +void ModuleTreeLinker::FetchRootInline( + ModuleScript* module_script, + base::PassKey<ModuleTreeLinkerRegistry>) { DCHECK(module_script); #if DCHECK_IS_ON() original_url_ = module_script->BaseURL(); @@ -369,14 +339,14 @@ void ModuleTreeLinker::NotifyModuleLoadFinished(ModuleScript* module_script) { void ModuleTreeLinker::FetchDescendants(const ModuleScript* module_script) { DCHECK(module_script); - v8::Isolate* isolate = modulator_->GetScriptState()->GetIsolate(); - v8::HandleScope scope(isolate); // [nospec] Abort the steps if the browsing context is discarded. if (!modulator_->HasValidContext()) { result_ = nullptr; AdvanceState(State::kFinished); return; } + ScriptState* script_state = modulator_->GetScriptState(); + v8::HandleScope scope(script_state->GetIsolate()); // <spec step="2">Let record be module script's record.</spec> v8::Local<v8::Module> record = module_script->V8Module(); @@ -409,7 +379,7 @@ void ModuleTreeLinker::FetchDescendants(const ModuleScript* module_script) { // <spec step="5">For each ModuleRequest Record requested of // record.[[RequestedModules]],</spec> Vector<ModuleRequest> record_requested_modules = - modulator_->ModuleRequestsFromModuleRecord(record); + ModuleRecord::ModuleRequests(script_state, record); for (const auto& requested : record_requested_modules) { // <spec step="5.1">Let url be the result of resolving a module specifier @@ -549,8 +519,14 @@ void ModuleTreeLinker::Instantiate() { // <spec step="5.2">Perform record.Instantiate(). ...</spec> AdvanceState(State::kInstantiating); + + ScriptState* script_state = modulator_->GetScriptState(); + UseCounter::Count(ExecutionContext::From(script_state), + WebFeature::kInstantiateModuleScript); + + ScriptState::Scope scope(script_state); ScriptValue instantiation_error = - modulator_->InstantiateModule(record, result_->SourceURL()); + ModuleRecord::Instantiate(script_state, record, result_->SourceURL()); // <spec step="5.2">... If this throws an exception, set result's error to // rethrow to that exception.</spec> @@ -606,7 +582,7 @@ ScriptValue ModuleTreeLinker::FindFirstParseError( // <spec step="5.1">Let childSpecifiers be the value of moduleScript's // record's [[RequestedModules]] internal slot.</spec> Vector<ModuleRequest> child_specifiers = - modulator_->ModuleRequestsFromModuleRecord(record); + ModuleRecord::ModuleRequests(modulator_->GetScriptState(), record); for (const auto& module_request : child_specifiers) { // <spec step="5.2">Let childURLs be the list obtained by calling resolve a diff --git a/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker.h b/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker.h index cbf354522ed..8a6ab5634b8 100644 --- a/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker.h +++ b/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker.h @@ -6,6 +6,7 @@ #define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_MODULESCRIPT_MODULE_TREE_LINKER_H_ #include "base/dcheck_is_on.h" +#include "base/types/pass_key.h" #include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-blink-forward.h" #include "third_party/blink/public/platform/web_url_request.h" #include "third_party/blink/renderer/core/core_export.h" @@ -30,38 +31,6 @@ class ModuleTreeLinkerRegistry; // by its |Context()->GetFetchClientSettingsObject()|. class CORE_EXPORT ModuleTreeLinker final : public SingleModuleClient { public: - // https://html.spec.whatwg.org/C/#fetch-a-module-script-tree - // https://html.spec.whatwg.org/C/#fetch-a-module-worker-script-tree - // https://html.spec.whatwg.org/C/#fetch-an-import()-module-script-graph - static void Fetch(const KURL& url, - const ModuleType&, - ResourceFetcher* fetch_client_settings_object_fetcher, - mojom::blink::RequestContextType context_type, - network::mojom::RequestDestination destination, - const ScriptFetchOptions&, - Modulator*, - ModuleScriptCustomFetchType, - ModuleTreeLinkerRegistry*, - ModuleTreeClient*); - - // https://html.spec.whatwg.org/C/#fetch-an-inline-module-script-graph - static void FetchDescendantsForInlineScript( - ModuleScript*, - ResourceFetcher* fetch_client_settings_object_fetcher, - mojom::blink::RequestContextType context_type, - network::mojom::RequestDestination destination, - Modulator*, - ModuleScriptCustomFetchType, - ModuleTreeLinkerRegistry*, - ModuleTreeClient*); - - ModuleTreeLinker(ResourceFetcher* fetch_client_settings_object_fetcher, - mojom::blink::RequestContextType context_type, - network::mojom::RequestDestination destination, - Modulator*, - ModuleScriptCustomFetchType, - ModuleTreeLinkerRegistry*, - ModuleTreeClient*); ~ModuleTreeLinker() override = default; void Trace(Visitor*) const override; @@ -70,6 +39,21 @@ class CORE_EXPORT ModuleTreeLinker final : public SingleModuleClient { } bool HasFinished() const { return state_ == State::kFinished; } + // Below methods are intended to be called via ModuleTreeLinkerRegistry. + ModuleTreeLinker(ResourceFetcher* fetch_client_settings_object_fetcher, + mojom::blink::RequestContextType context_type, + network::mojom::RequestDestination destination, + Modulator*, + ModuleScriptCustomFetchType, + ModuleTreeLinkerRegistry*, + ModuleTreeClient*, + base::PassKey<ModuleTreeLinkerRegistry>); + void FetchRoot(const KURL&, + ModuleType, + const ScriptFetchOptions&, + base::PassKey<ModuleTreeLinkerRegistry>); + void FetchRootInline(ModuleScript*, base::PassKey<ModuleTreeLinkerRegistry>); + private: enum class State { kInitial, @@ -87,9 +71,6 @@ class CORE_EXPORT ModuleTreeLinker final : public SingleModuleClient { #endif void AdvanceState(State); - void FetchRoot(const KURL&, ModuleType, const ScriptFetchOptions&); - void FetchRootInline(ModuleScript*); - void NotifyModuleLoadFinished(ModuleScript*) override; void FetchDescendants(const ModuleScript*); diff --git a/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker_registry.cc b/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker_registry.cc index 86f6f3bde6b..5bc2ef08a76 100644 --- a/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker_registry.cc +++ b/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker_registry.cc @@ -6,25 +6,60 @@ #include "third_party/blink/renderer/core/loader/modulescript/module_tree_linker.h" #include "third_party/blink/renderer/platform/loader/fetch/fetch_client_settings_object_snapshot.h" -#include "third_party/blink/renderer/platform/weborigin/kurl.h" -#include "third_party/blink/renderer/platform/weborigin/kurl_hash.h" namespace blink { +void ModuleTreeLinkerRegistry::Fetch( + const KURL& url, + const ModuleType& module_type, + ResourceFetcher* fetch_client_settings_object_fetcher, + mojom::blink::RequestContextType context_type, + network::mojom::RequestDestination destination, + const ScriptFetchOptions& options, + Modulator* modulator, + ModuleScriptCustomFetchType custom_fetch_type, + ModuleTreeClient* client) { + ModuleTreeLinker* linker = MakeGarbageCollected<ModuleTreeLinker>( + fetch_client_settings_object_fetcher, context_type, destination, + modulator, custom_fetch_type, this, client, + base::PassKey<ModuleTreeLinkerRegistry>()); + AddLinker(linker); + linker->FetchRoot(url, module_type, options, + base::PassKey<ModuleTreeLinkerRegistry>()); + DCHECK(linker->IsFetching()); +} + +void ModuleTreeLinkerRegistry::FetchDescendantsForInlineScript( + ModuleScript* module_script, + ResourceFetcher* fetch_client_settings_object_fetcher, + mojom::blink::RequestContextType context_type, + network::mojom::RequestDestination destination, + Modulator* modulator, + ModuleScriptCustomFetchType custom_fetch_type, + ModuleTreeClient* client) { + ModuleTreeLinker* linker = MakeGarbageCollected<ModuleTreeLinker>( + fetch_client_settings_object_fetcher, context_type, destination, + modulator, custom_fetch_type, this, client, + base::PassKey<ModuleTreeLinkerRegistry>()); + AddLinker(linker); + linker->FetchRootInline(module_script, + base::PassKey<ModuleTreeLinkerRegistry>()); + DCHECK(linker->IsFetching()); +} + void ModuleTreeLinkerRegistry::Trace(Visitor* visitor) const { visitor->Trace(active_tree_linkers_); } -void ModuleTreeLinkerRegistry::AddFetcher(ModuleTreeLinker* fetcher) { - DCHECK(!active_tree_linkers_.Contains(fetcher)); - active_tree_linkers_.insert(fetcher); +void ModuleTreeLinkerRegistry::AddLinker(ModuleTreeLinker* linker) { + DCHECK(!active_tree_linkers_.Contains(linker)); + active_tree_linkers_.insert(linker); } -void ModuleTreeLinkerRegistry::ReleaseFinishedFetcher( - ModuleTreeLinker* fetcher) { - DCHECK(fetcher->HasFinished()); +void ModuleTreeLinkerRegistry::ReleaseFinishedLinker(ModuleTreeLinker* linker) { + DCHECK(linker->HasFinished()); - auto it = active_tree_linkers_.find(fetcher); + auto it = active_tree_linkers_.find(linker); DCHECK_NE(it, active_tree_linkers_.end()); active_tree_linkers_.erase(it); } diff --git a/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker_registry.h b/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker_registry.h index 68afcbcda9e..468b5f4a867 100644 --- a/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker_registry.h +++ b/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker_registry.h @@ -5,9 +5,12 @@ #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_MODULESCRIPT_MODULE_TREE_LINKER_REGISTRY_H_ #define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_MODULESCRIPT_MODULE_TREE_LINKER_REGISTRY_H_ +#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-blink-forward.h" #include "third_party/blink/renderer/core/core_export.h" +#include "third_party/blink/renderer/core/loader/modulescript/module_script_creation_params.h" #include "third_party/blink/renderer/platform/bindings/name_client.h" #include "third_party/blink/renderer/platform/heap/handle.h" +#include "third_party/blink/renderer/platform/weborigin/kurl.h" namespace blink { @@ -19,6 +22,30 @@ class CORE_EXPORT ModuleTreeLinkerRegistry final public NameClient { public: ModuleTreeLinkerRegistry() = default; + ~ModuleTreeLinkerRegistry() final = default; + + // https://html.spec.whatwg.org/C/#fetch-a-module-script-tree + // https://html.spec.whatwg.org/C/#fetch-a-module-worker-script-tree + // https://html.spec.whatwg.org/C/#fetch-an-import()-module-script-graph + void Fetch(const KURL& url, + const ModuleType&, + ResourceFetcher* fetch_client_settings_object_fetcher, + mojom::blink::RequestContextType context_type, + network::mojom::RequestDestination destination, + const ScriptFetchOptions&, + Modulator*, + ModuleScriptCustomFetchType, + ModuleTreeClient*); + + // https://html.spec.whatwg.org/C/#fetch-an-inline-module-script-graph + void FetchDescendantsForInlineScript( + ModuleScript*, + ResourceFetcher* fetch_client_settings_object_fetcher, + mojom::blink::RequestContextType context_type, + network::mojom::RequestDestination destination, + Modulator*, + ModuleScriptCustomFetchType, + ModuleTreeClient*); void Trace(Visitor*) const; const char* NameInHeapSnapshot() const override { @@ -27,8 +54,8 @@ class CORE_EXPORT ModuleTreeLinkerRegistry final private: friend class ModuleTreeLinker; - void AddFetcher(ModuleTreeLinker*); - void ReleaseFinishedFetcher(ModuleTreeLinker*); + void AddLinker(ModuleTreeLinker*); + void ReleaseFinishedLinker(ModuleTreeLinker*); HeapHashSet<Member<ModuleTreeLinker>> active_tree_linkers_; }; diff --git a/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker_test.cc b/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker_test.cc index b0c1f9aad10..96fd3c10984 100644 --- a/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker_test.cc +++ b/chromium/third_party/blink/renderer/core/loader/modulescript/module_tree_linker_test.cc @@ -19,9 +19,10 @@ #include "third_party/blink/renderer/core/loader/modulescript/module_tree_linker_registry.h" #include "third_party/blink/renderer/core/script/js_module_script.h" #include "third_party/blink/renderer/core/script/modulator.h" -#include "third_party/blink/renderer/core/testing/dummy_modulator.h" #include "third_party/blink/renderer/core/testing/module_test_base.h" #include "third_party/blink/renderer/core/testing/page_test_base.h" +#include "third_party/blink/renderer/core/testing/sim/sim_request.h" +#include "third_party/blink/renderer/core/testing/sim/sim_test.h" #include "third_party/blink/renderer/platform/bindings/script_state.h" #include "third_party/blink/renderer/platform/bindings/v8_throw_exception.h" #include "third_party/blink/renderer/platform/heap/handle.h" @@ -56,196 +57,84 @@ class TestModuleTreeClient final : public ModuleTreeClient { Member<ModuleScript> module_script_; }; -} // namespace - -class ModuleTreeLinkerTestModulator final : public DummyModulator { +class SimModuleRequest : public SimRequestBase { public: - explicit ModuleTreeLinkerTestModulator(ScriptState* script_state) - : script_state_(script_state) {} - ~ModuleTreeLinkerTestModulator() override = default; - - void Trace(Visitor*) const override; - - enum class ResolveResult { kFailure, kSuccess }; - - // Resolve last |Modulator::FetchSingle()| call. - ModuleScript* ResolveSingleModuleScriptFetch( - const KURL& url, - const Vector<String>& dependency_module_specifiers, - bool parse_error = false) { - ScriptState::Scope scope(script_state_); + explicit SimModuleRequest(KURL url) + : SimRequestBase(std::move(url), + "text/javascript", + /* start_immediately=*/false) {} + void CompleteWithImports(const Vector<String>& specifiers) { StringBuilder source_text; - for (const auto& specifier : dependency_module_specifiers) { + for (const auto& specifier : specifiers) { source_text.Append("import '"); source_text.Append(specifier); source_text.Append("';\n"); } source_text.Append("export default 'grapes';"); - v8::Local<v8::Module> module_record = ModuleTestBase::CompileModule( - script_state_->GetIsolate(), source_text.ToString(), url); - auto* module_script = - JSModuleScript::CreateForTest(this, module_record, url); - - auto result_map = module_map_.insert(url, module_script); - EXPECT_TRUE(result_map.is_new_entry); - - if (parse_error) { - v8::Local<v8::Value> error = V8ThrowException::CreateError( - script_state_->GetIsolate(), "Parse failure."); - module_script->SetParseErrorAndClearRecord( - ScriptValue(script_state_->GetIsolate(), error)); - } - - EXPECT_TRUE(pending_clients_.Contains(url)); - pending_clients_.Take(url)->NotifyModuleLoadFinished(module_script); - - return module_script; - } - - void ResolveDependentTreeFetch(const KURL& url, ResolveResult result) { - ResolveSingleModuleScriptFetch(url, Vector<String>(), - result == ResolveResult::kFailure); - } - - void SetInstantiateShouldFail(bool b) { instantiate_should_fail_ = b; } - - bool HasInstantiated(ModuleScript* module_script) const { - v8::Isolate* isolate = script_state_->GetIsolate(); - v8::HandleScope scope(isolate); - - return instantiated_records_.Contains(MakeGarbageCollected<BoxedV8Module>( - isolate, module_script->V8Module())); - } - - private: - // Implements Modulator: - - ScriptState* GetScriptState() override { return script_state_; } - - KURL ResolveModuleSpecifier(const String& module_request, - const KURL& base_url, - String* failure_reason) final { - return KURL(base_url, module_request); - } - void SetAcquiringImportMapsState(AcquiringImportMapsState) final {} - - void FetchSingle(const ModuleScriptFetchRequest& request, - ResourceFetcher*, - ModuleGraphLevel, - ModuleScriptCustomFetchType, - SingleModuleClient* client) override { - EXPECT_FALSE(pending_clients_.Contains(request.Url())); - pending_clients_.Set(request.Url(), client); - } - - ModuleScript* GetFetchedModuleScript(const KURL& url, - ModuleType module_type) override { - const auto& it = module_map_.find(url); - if (it == module_map_.end()) - return nullptr; - - return it->value; + Complete(source_text.ToString()); } - - ScriptValue InstantiateModule(v8::Local<v8::Module> record, - const KURL& source_url) override { - if (instantiate_should_fail_) { - ScriptState::Scope scope(script_state_); - v8::Local<v8::Value> error = V8ThrowException::CreateError( - script_state_->GetIsolate(), "Instantiation failure."); - return ScriptValue(script_state_->GetIsolate(), error); - } - instantiated_records_.insert(MakeGarbageCollected<BoxedV8Module>( - script_state_->GetIsolate(), record)); - return ScriptValue(); - } - - Vector<ModuleRequest> ModuleRequestsFromModuleRecord( - v8::Local<v8::Module> module_record) override { - ScriptState::Scope scope(script_state_); - return ModuleRecord::ModuleRequests(script_state_, module_record); - } - - Member<ScriptState> script_state_; - HeapHashMap<KURL, Member<SingleModuleClient>> pending_clients_; - HeapHashMap<KURL, Member<ModuleScript>> module_map_; - HeapHashSet<Member<BoxedV8Module>> instantiated_records_; - bool instantiate_should_fail_ = false; }; -void ModuleTreeLinkerTestModulator::Trace(Visitor* visitor) const { - visitor->Trace(script_state_); - visitor->Trace(pending_clients_); - visitor->Trace(module_map_); - visitor->Trace(instantiated_records_); - DummyModulator::Trace(visitor); -} - -class ModuleTreeLinkerTest : public PageTestBase { - DISALLOW_COPY_AND_ASSIGN(ModuleTreeLinkerTest); +} // namespace +class ModuleTreeLinkerTest : public SimTest { public: - ModuleTreeLinkerTest() = default; - void SetUp() override; - - ModuleTreeLinkerTestModulator* GetModulator() { return modulator_.Get(); } + Modulator* GetModulator() { + return Modulator::From(ToScriptStateForMainWorld(MainFrame().GetFrame())); + } - protected: - Persistent<ModuleTreeLinkerTestModulator> modulator_; + bool HasInstantiated(ModuleScript* module_script) { + if (!module_script) + return false; + ScriptState::Scope script_scope(GetModulator()->GetScriptState()); + return (module_script->V8Module()->GetStatus() == + v8::Module::kInstantiated); + } }; -void ModuleTreeLinkerTest::SetUp() { - PageTestBase::SetUp(IntSize(500, 500)); - ScriptState* script_state = ToScriptStateForMainWorld(&GetFrame()); - modulator_ = - MakeGarbageCollected<ModuleTreeLinkerTestModulator>(script_state); -} - TEST_F(ModuleTreeLinkerTest, FetchTreeNoDeps) { - auto* registry = MakeGarbageCollected<ModuleTreeLinkerRegistry>(); - - KURL url("http://example.com/root.js"); + SimModuleRequest sim_module(KURL("http://example.com/root.js")); TestModuleTreeClient* client = MakeGarbageCollected<TestModuleTreeClient>(); - ModuleTreeLinker::Fetch(url, ModuleType::kJavaScript, GetDocument().Fetcher(), - mojom::blink::RequestContextType::SCRIPT, - network::mojom::RequestDestination::kScript, - ScriptFetchOptions(), GetModulator(), - ModuleScriptCustomFetchType::kNone, registry, client); + GetModulator()->FetchTree( + sim_module.GetURL(), ModuleType::kJavaScript, GetDocument().Fetcher(), + mojom::blink::RequestContextType::SCRIPT, + network::mojom::RequestDestination::kScript, ScriptFetchOptions(), + ModuleScriptCustomFetchType::kNone, client); EXPECT_FALSE(client->WasNotifyFinished()) << "ModuleTreeLinker should always finish asynchronously."; EXPECT_FALSE(client->GetModuleScript()); - GetModulator()->ResolveSingleModuleScriptFetch(url, {}); + sim_module.Complete(R"(export default 'grapes';)"); + test::RunPendingTasks(); + EXPECT_TRUE(client->WasNotifyFinished()); - ASSERT_TRUE(client->GetModuleScript()); - EXPECT_TRUE(GetModulator()->HasInstantiated(client->GetModuleScript())); + + ModuleScript* module_script = client->GetModuleScript(); + ASSERT_TRUE(module_script); + EXPECT_TRUE(HasInstantiated(module_script)); } TEST_F(ModuleTreeLinkerTest, FetchTreeInstantiationFailure) { - GetModulator()->SetInstantiateShouldFail(true); + SimModuleRequest sim_module(KURL("http://example.com/root.js")); - ModuleTreeLinkerRegistry* registry = - MakeGarbageCollected<ModuleTreeLinkerRegistry>(); - - KURL url("http://example.com/root.js"); TestModuleTreeClient* client = MakeGarbageCollected<TestModuleTreeClient>(); - ModuleTreeLinker::Fetch(url, ModuleType::kJavaScript, GetDocument().Fetcher(), - mojom::blink::RequestContextType::SCRIPT, - network::mojom::RequestDestination::kScript, - ScriptFetchOptions(), GetModulator(), - ModuleScriptCustomFetchType::kNone, registry, client); + GetModulator()->FetchTree( + sim_module.GetURL(), ModuleType::kJavaScript, GetDocument().Fetcher(), + mojom::blink::RequestContextType::SCRIPT, + network::mojom::RequestDestination::kScript, ScriptFetchOptions(), + ModuleScriptCustomFetchType::kNone, client); EXPECT_FALSE(client->WasNotifyFinished()) << "ModuleTreeLinker should always finish asynchronously."; EXPECT_FALSE(client->GetModuleScript()); - GetModulator()->ResolveSingleModuleScriptFetch(url, {}); - - // Modulator::InstantiateModule() fails here, as - // we SetInstantiateShouldFail(true) earlier. + sim_module.Complete(R"( + import _self_should_fail from 'http://example.com/root.js'; + )"); + test::RunPendingTasks(); EXPECT_TRUE(client->WasNotifyFinished()); ASSERT_TRUE(client->GetModuleScript()); @@ -255,55 +144,49 @@ TEST_F(ModuleTreeLinkerTest, FetchTreeInstantiationFailure) { } TEST_F(ModuleTreeLinkerTest, FetchTreeWithSingleDependency) { - ModuleTreeLinkerRegistry* registry = - MakeGarbageCollected<ModuleTreeLinkerRegistry>(); + SimModuleRequest sim_module(KURL("http://example.com/root.js")); + SimModuleRequest sim_module_dep(KURL("http://example.com/dep1.js")); - KURL url("http://example.com/root.js"); TestModuleTreeClient* client = MakeGarbageCollected<TestModuleTreeClient>(); - ModuleTreeLinker::Fetch(url, ModuleType::kJavaScript, GetDocument().Fetcher(), - mojom::blink::RequestContextType::SCRIPT, - network::mojom::RequestDestination::kScript, - ScriptFetchOptions(), GetModulator(), - ModuleScriptCustomFetchType::kNone, registry, client); + GetModulator()->FetchTree( + sim_module.GetURL(), ModuleType::kJavaScript, GetDocument().Fetcher(), + mojom::blink::RequestContextType::SCRIPT, + network::mojom::RequestDestination::kScript, ScriptFetchOptions(), + ModuleScriptCustomFetchType::kNone, client); EXPECT_FALSE(client->WasNotifyFinished()) << "ModuleTreeLinker should always finish asynchronously."; EXPECT_FALSE(client->GetModuleScript()); - GetModulator()->ResolveSingleModuleScriptFetch(url, {"./dep1.js"}); + sim_module.CompleteWithImports({"./dep1.js"}); + test::RunPendingTasks(); + EXPECT_FALSE(client->WasNotifyFinished()); - KURL url_dep1("http://example.com/dep1.js"); + sim_module_dep.CompleteWithImports({}); + test::RunPendingTasks(); - GetModulator()->ResolveDependentTreeFetch( - url_dep1, ModuleTreeLinkerTestModulator::ResolveResult::kSuccess); EXPECT_TRUE(client->WasNotifyFinished()); - - ASSERT_TRUE(client->GetModuleScript()); - EXPECT_TRUE(GetModulator()->HasInstantiated(client->GetModuleScript())); + ModuleScript* module_script = client->GetModuleScript(); + ASSERT_TRUE(module_script); + EXPECT_TRUE(HasInstantiated(module_script)); } TEST_F(ModuleTreeLinkerTest, FetchTreeWith3Deps) { - ModuleTreeLinkerRegistry* registry = - MakeGarbageCollected<ModuleTreeLinkerRegistry>(); + SimModuleRequest sim_module(KURL("http://example.com/root.js")); - KURL url("http://example.com/root.js"); TestModuleTreeClient* client = MakeGarbageCollected<TestModuleTreeClient>(); - ModuleTreeLinker::Fetch(url, ModuleType::kJavaScript, GetDocument().Fetcher(), - mojom::blink::RequestContextType::SCRIPT, - network::mojom::RequestDestination::kScript, - ScriptFetchOptions(), GetModulator(), - ModuleScriptCustomFetchType::kNone, registry, client); + GetModulator()->FetchTree( + sim_module.GetURL(), ModuleType::kJavaScript, GetDocument().Fetcher(), + mojom::blink::RequestContextType::SCRIPT, + network::mojom::RequestDestination::kScript, ScriptFetchOptions(), + ModuleScriptCustomFetchType::kNone, client); EXPECT_FALSE(client->WasNotifyFinished()) << "ModuleTreeLinker should always finish asynchronously."; EXPECT_FALSE(client->GetModuleScript()); - GetModulator()->ResolveSingleModuleScriptFetch( - url, {"./dep1.js", "./dep2.js", "./dep3.js"}); - EXPECT_FALSE(client->WasNotifyFinished()); - - Vector<KURL> url_deps; + Vector<std::unique_ptr<SimModuleRequest>> sim_module_deps; for (int i = 1; i <= 3; ++i) { StringBuilder url_dep_str; url_dep_str.Append("http://example.com/dep"); @@ -311,41 +194,39 @@ TEST_F(ModuleTreeLinkerTest, FetchTreeWith3Deps) { url_dep_str.Append(".js"); KURL url_dep(url_dep_str.ToString()); - url_deps.push_back(url_dep); + sim_module_deps.push_back(std::make_unique<SimModuleRequest>(url_dep)); } - for (const auto& url_dep : url_deps) { + sim_module.CompleteWithImports({"./dep1.js", "./dep2.js", "./dep3.js"}); + test::RunPendingTasks(); + + for (const auto& sim_module_dep : sim_module_deps) { EXPECT_FALSE(client->WasNotifyFinished()); - GetModulator()->ResolveDependentTreeFetch( - url_dep, ModuleTreeLinkerTestModulator::ResolveResult::kSuccess); + sim_module_dep->CompleteWithImports({}); + test::RunPendingTasks(); } EXPECT_TRUE(client->WasNotifyFinished()); - ASSERT_TRUE(client->GetModuleScript()); - EXPECT_TRUE(GetModulator()->HasInstantiated(client->GetModuleScript())); + ModuleScript* module_script = client->GetModuleScript(); + ASSERT_TRUE(module_script); + EXPECT_TRUE(HasInstantiated(module_script)); } TEST_F(ModuleTreeLinkerTest, FetchTreeWith3Deps1Fail) { - ModuleTreeLinkerRegistry* registry = - MakeGarbageCollected<ModuleTreeLinkerRegistry>(); + SimModuleRequest sim_module(KURL("http://example.com/root.js")); - KURL url("http://example.com/root.js"); TestModuleTreeClient* client = MakeGarbageCollected<TestModuleTreeClient>(); - ModuleTreeLinker::Fetch(url, ModuleType::kJavaScript, GetDocument().Fetcher(), - mojom::blink::RequestContextType::SCRIPT, - network::mojom::RequestDestination::kScript, - ScriptFetchOptions(), GetModulator(), - ModuleScriptCustomFetchType::kNone, registry, client); + GetModulator()->FetchTree( + sim_module.GetURL(), ModuleType::kJavaScript, GetDocument().Fetcher(), + mojom::blink::RequestContextType::SCRIPT, + network::mojom::RequestDestination::kScript, ScriptFetchOptions(), + ModuleScriptCustomFetchType::kNone, client); EXPECT_FALSE(client->WasNotifyFinished()) << "ModuleTreeLinker should always finish asynchronously."; EXPECT_FALSE(client->GetModuleScript()); - GetModulator()->ResolveSingleModuleScriptFetch( - url, {"./dep1.js", "./dep2.js", "./dep3.js"}); - EXPECT_FALSE(client->WasNotifyFinished()); - - Vector<KURL> url_deps; + Vector<std::unique_ptr<SimModuleRequest>> sim_module_deps; for (int i = 1; i <= 3; ++i) { StringBuilder url_dep_str; url_dep_str.Append("http://example.com/dep"); @@ -353,88 +234,56 @@ TEST_F(ModuleTreeLinkerTest, FetchTreeWith3Deps1Fail) { url_dep_str.Append(".js"); KURL url_dep(url_dep_str.ToString()); - url_deps.push_back(url_dep); - } - - for (const auto& url_dep : url_deps) { - SCOPED_TRACE(url_dep.GetString()); + sim_module_deps.push_back(std::make_unique<SimModuleRequest>(url_dep)); } - auto url_dep = url_deps.back(); - url_deps.pop_back(); - GetModulator()->ResolveDependentTreeFetch( - url_dep, ModuleTreeLinkerTestModulator::ResolveResult::kSuccess); - EXPECT_FALSE(client->WasNotifyFinished()); - url_dep = url_deps.back(); - url_deps.pop_back(); - GetModulator()->ResolveDependentTreeFetch( - url_dep, ModuleTreeLinkerTestModulator::ResolveResult::kFailure); - - // TODO(kouhei): This may not hold once we implement early failure reporting. - EXPECT_FALSE(client->WasNotifyFinished()); - - // Check below doesn't crash. - url_dep = url_deps.back(); - url_deps.pop_back(); - GetModulator()->ResolveDependentTreeFetch( - url_dep, ModuleTreeLinkerTestModulator::ResolveResult::kSuccess); - EXPECT_TRUE(url_deps.IsEmpty()); - - EXPECT_TRUE(client->WasNotifyFinished()); - ASSERT_TRUE(client->GetModuleScript()); - EXPECT_FALSE(client->GetModuleScript()->HasParseError()); - EXPECT_TRUE(client->GetModuleScript()->HasErrorToRethrow()); -} - -TEST_F(ModuleTreeLinkerTest, FetchDependencyTree) { - ModuleTreeLinkerRegistry* registry = - MakeGarbageCollected<ModuleTreeLinkerRegistry>(); + sim_module.CompleteWithImports({"./dep1.js", "./dep2.js", "./dep3.js"}); + test::RunPendingTasks(); - KURL url("http://example.com/depth1.js"); - TestModuleTreeClient* client = MakeGarbageCollected<TestModuleTreeClient>(); - ModuleTreeLinker::Fetch(url, ModuleType::kJavaScript, GetDocument().Fetcher(), - mojom::blink::RequestContextType::SCRIPT, - network::mojom::RequestDestination::kScript, - ScriptFetchOptions(), GetModulator(), - ModuleScriptCustomFetchType::kNone, registry, client); + for (int i = 0; i < 3; ++i) { + const auto& sim_module_dep = sim_module_deps[i]; - EXPECT_FALSE(client->WasNotifyFinished()) - << "ModuleTreeLinker should always finish asynchronously."; - EXPECT_FALSE(client->GetModuleScript()); - - GetModulator()->ResolveSingleModuleScriptFetch(url, {"./depth2.js"}); - - KURL url_dep2("http://example.com/depth2.js"); + EXPECT_FALSE(client->WasNotifyFinished()); + if (i == 1) { + // Complete the request with un-parsable JavaScript fragment. + sim_module_dep->Complete("%!#$@#$@#$@"); + } else { + sim_module_dep->CompleteWithImports({}); + } - GetModulator()->ResolveDependentTreeFetch( - url_dep2, ModuleTreeLinkerTestModulator::ResolveResult::kSuccess); + test::RunPendingTasks(); + } EXPECT_TRUE(client->WasNotifyFinished()); - ASSERT_TRUE(client->GetModuleScript()); - EXPECT_TRUE(GetModulator()->HasInstantiated(client->GetModuleScript())); + ModuleScript* module_script = client->GetModuleScript(); + ASSERT_TRUE(module_script); + EXPECT_FALSE(HasInstantiated(module_script)); + EXPECT_FALSE(module_script->HasParseError()); + EXPECT_TRUE(module_script->HasErrorToRethrow()); } TEST_F(ModuleTreeLinkerTest, FetchDependencyOfCyclicGraph) { - ModuleTreeLinkerRegistry* registry = - MakeGarbageCollected<ModuleTreeLinkerRegistry>(); + SimModuleRequest sim_module(KURL("http://example.com/a.js")); - KURL url("http://example.com/a.js"); TestModuleTreeClient* client = MakeGarbageCollected<TestModuleTreeClient>(); - ModuleTreeLinker::Fetch(url, ModuleType::kJavaScript, GetDocument().Fetcher(), - mojom::blink::RequestContextType::SCRIPT, - network::mojom::RequestDestination::kScript, - ScriptFetchOptions(), GetModulator(), - ModuleScriptCustomFetchType::kNone, registry, client); + GetModulator()->FetchTree( + sim_module.GetURL(), ModuleType::kJavaScript, GetDocument().Fetcher(), + mojom::blink::RequestContextType::SCRIPT, + network::mojom::RequestDestination::kScript, ScriptFetchOptions(), + ModuleScriptCustomFetchType::kNone, client); EXPECT_FALSE(client->WasNotifyFinished()) << "ModuleTreeLinker should always finish asynchronously."; EXPECT_FALSE(client->GetModuleScript()); - GetModulator()->ResolveSingleModuleScriptFetch(url, {"./a.js"}); + sim_module.CompleteWithImports({"./a.js"}); + test::RunPendingTasks(); EXPECT_TRUE(client->WasNotifyFinished()); - ASSERT_TRUE(client->GetModuleScript()); - EXPECT_TRUE(GetModulator()->HasInstantiated(client->GetModuleScript())); + ModuleScript* module_script = client->GetModuleScript(); + ASSERT_TRUE(module_script); + EXPECT_TRUE(HasInstantiated(module_script)); + EXPECT_FALSE(module_script->HasParseError()); } } // namespace blink diff --git a/chromium/third_party/blink/renderer/core/loader/modulescript/worklet_module_script_fetcher.h b/chromium/third_party/blink/renderer/core/loader/modulescript/worklet_module_script_fetcher.h index 5b64c070883..b9d80a978d8 100644 --- a/chromium/third_party/blink/renderer/core/loader/modulescript/worklet_module_script_fetcher.h +++ b/chromium/third_party/blink/renderer/core/loader/modulescript/worklet_module_script_fetcher.h @@ -5,7 +5,6 @@ #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_MODULESCRIPT_WORKLET_MODULE_SCRIPT_FETCHER_H_ #define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_MODULESCRIPT_WORKLET_MODULE_SCRIPT_FETCHER_H_ -#include "third_party/abseil-cpp/absl/types/optional.h" #include "third_party/blink/renderer/core/loader/modulescript/module_script_fetcher.h" #include "third_party/blink/renderer/core/workers/worklet_module_responses_map.h" |