summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/bindings/core/v8/v8_script_runner.cc
diff options
context:
space:
mode:
Diffstat (limited to 'chromium/third_party/blink/renderer/bindings/core/v8/v8_script_runner.cc')
-rw-r--r--chromium/third_party/blink/renderer/bindings/core/v8/v8_script_runner.cc113
1 files changed, 64 insertions, 49 deletions
diff --git a/chromium/third_party/blink/renderer/bindings/core/v8/v8_script_runner.cc b/chromium/third_party/blink/renderer/bindings/core/v8/v8_script_runner.cc
index 43e2ca14dde..67f0f5d7a4b 100644
--- a/chromium/third_party/blink/renderer/bindings/core/v8/v8_script_runner.cc
+++ b/chromium/third_party/blink/renderer/bindings/core/v8/v8_script_runner.cc
@@ -56,7 +56,7 @@
#include "third_party/blink/renderer/platform/instrumentation/histogram.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/loader/fetch/cached_metadata.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/scheduler/public/event_loop.h"
#include "third_party/blink/renderer/platform/wtf/text/text_encoding.h"
@@ -114,7 +114,7 @@ v8::MaybeLocal<v8::Value> ThrowStackOverflowExceptionIfNeeded(
v8::MaybeLocal<v8::Script> CompileScriptInternal(
v8::Isolate* isolate,
- ExecutionContext* execution_context,
+ ScriptState* script_state,
const ScriptSourceCode& source_code,
v8::ScriptOrigin origin,
v8::ScriptCompiler::CompileOptions compile_options,
@@ -122,6 +122,8 @@ v8::MaybeLocal<v8::Script> CompileScriptInternal(
inspector_compile_script_event::V8CacheResult* cache_result) {
v8::Local<v8::String> code = V8String(isolate, source_code.Source());
+ // TODO(kouhei): Plumb the ScriptState into this function and replace all
+ // Isolate->GetCurrentContext in this function with ScriptState->GetContext.
if (ScriptStreamer* streamer = source_code.Streamer()) {
// Final compile call for a streamed compilation.
// Streaming compilation may involve use of code cache.
@@ -129,8 +131,8 @@ v8::MaybeLocal<v8::Script> CompileScriptInternal(
DCHECK(streamer->IsFinished());
DCHECK(!streamer->IsStreamingSuppressed());
return v8::ScriptCompiler::Compile(
- isolate->GetCurrentContext(),
- streamer->Source(v8::ScriptType::kClassic), code, origin);
+ script_state->GetContext(), streamer->Source(v8::ScriptType::kClassic),
+ code, origin);
}
// Allow inspector to use its own compilation cache store.
@@ -138,12 +140,13 @@ v8::MaybeLocal<v8::Script> CompileScriptInternal(
// The probe below allows inspector to either inject the cached code
// or override compile_options to force eager compilation of code
// when producing the cache.
- probe::ApplyCompilationModeOverride(execution_context, source_code,
- &inspector_data, &compile_options);
+ probe::ApplyCompilationModeOverride(ExecutionContext::From(script_state),
+ source_code, &inspector_data,
+ &compile_options);
if (inspector_data) {
v8::ScriptCompiler::Source source(code, origin, inspector_data);
v8::MaybeLocal<v8::Script> script =
- v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &source,
+ v8::ScriptCompiler::Compile(script_state->GetContext(), &source,
v8::ScriptCompiler::kConsumeCodeCache);
return script;
}
@@ -152,7 +155,7 @@ v8::MaybeLocal<v8::Script> CompileScriptInternal(
case v8::ScriptCompiler::kNoCompileOptions:
case v8::ScriptCompiler::kEagerCompile: {
v8::ScriptCompiler::Source source(code, origin);
- return v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &source,
+ return v8::ScriptCompiler::Compile(script_state->GetContext(), &source,
compile_options, no_cache_reason);
}
@@ -163,16 +166,27 @@ v8::MaybeLocal<v8::Script> CompileScriptInternal(
V8CodeCache::CreateCachedData(cache_handler);
v8::ScriptCompiler::Source source(code, origin, cached_data);
v8::MaybeLocal<v8::Script> script =
- v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &source,
+ v8::ScriptCompiler::Compile(script_state->GetContext(), &source,
v8::ScriptCompiler::kConsumeCodeCache);
+ // The ScriptState has an associated context. We expect the current
+ // context to match the context associated with Script context when
+ // compiling the script for main world. Hence it is safe to use the
+ // CodeCacheHost corresponding to the script execution context. For
+ // isolated world (for ex: extension scripts), the current context
+ // may not match the script context. Though currently code caching is
+ // disabled for extensions.
if (cached_data->rejected) {
cache_handler->ClearCachedMetadata(
+ ExecutionContext::GetCodeCacheHostFromContext(
+ ExecutionContext::From(script_state)),
CachedMetadataHandler::kClearPersistentStorage);
} else if (InDiscardExperiment()) {
// Experimentally free code cache from memory after first use. See
// http://crbug.com/1045052.
cache_handler->ClearCachedMetadata(
+ ExecutionContext::GetCodeCacheHostFromContext(
+ ExecutionContext::From(script_state)),
CachedMetadataHandler::kDiscardLocally);
}
if (cache_result) {
@@ -235,16 +249,16 @@ v8::MaybeLocal<v8::Script> V8ScriptRunner::CompileScript(
sanitize_script_errors == SanitizeScriptErrors::kSanitize,
false, // is_wasm
false, // is_module
- referrer_info.ToV8HostDefinedOptions(isolate));
+ referrer_info.ToV8HostDefinedOptions(isolate, source.Url()));
if (!*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(kTraceEventCategoryGroup)) {
- return CompileScriptInternal(isolate, execution_context, source, origin,
+ return CompileScriptInternal(isolate, script_state, source, origin,
compile_options, no_cache_reason, nullptr);
}
inspector_compile_script_event::V8CacheResult cache_result;
v8::MaybeLocal<v8::Script> script =
- CompileScriptInternal(isolate, execution_context, source, origin,
+ CompileScriptInternal(isolate, script_state, source, origin,
compile_options, no_cache_reason, &cache_result);
TRACE_EVENT_END1(kTraceEventCategoryGroup, "v8.compile", "data",
[&](perfetto::TracedValue context) {
@@ -270,16 +284,17 @@ v8::MaybeLocal<v8::Module> V8ScriptRunner::CompileModule(
// |resource_is_shared_cross_origin| is always true and |resource_is_opaque|
// is always false because CORS is enforced to module scripts.
- v8::ScriptOrigin origin(isolate, V8String(isolate, file_name),
- start_position.line_.ZeroBasedInt(),
- start_position.column_.ZeroBasedInt(),
- true, // resource_is_shared_cross_origin
- -1, // script id
- v8::String::Empty(isolate), // source_map_url
- false, // resource_is_opaque
- false, // is_wasm
- true, // is_module
- referrer_info.ToV8HostDefinedOptions(isolate));
+ v8::ScriptOrigin origin(
+ isolate, V8String(isolate, file_name),
+ start_position.line_.ZeroBasedInt(),
+ start_position.column_.ZeroBasedInt(),
+ true, // resource_is_shared_cross_origin
+ -1, // script id
+ v8::String::Empty(isolate), // source_map_url
+ false, // resource_is_opaque
+ false, // is_wasm
+ true, // is_module
+ referrer_info.ToV8HostDefinedOptions(isolate, params.SourceURL()));
v8::Local<v8::String> code = V8String(isolate, params.GetSourceText());
inspector_compile_script_event::V8CacheResult cache_result;
@@ -314,13 +329,21 @@ v8::MaybeLocal<v8::Module> V8ScriptRunner::CompileModule(
v8::ScriptCompiler::Source source(code, origin, cached_data);
script = v8::ScriptCompiler::CompileModule(
isolate, &source, compile_options, no_cache_reason);
+ // The ScriptState also has an associated context. We expect the current
+ // context to match the context associated with Script context when
+ // compiling the module. Hence it is safe to use the CodeCacheHost
+ // corresponding to the current execution context.
+ ExecutionContext* execution_context =
+ ExecutionContext::From(isolate->GetCurrentContext());
if (cached_data->rejected) {
cache_handler->ClearCachedMetadata(
+ ExecutionContext::GetCodeCacheHostFromContext(execution_context),
CachedMetadataHandler::kClearPersistentStorage);
} else if (InDiscardExperiment()) {
// Experimentally free code cache from memory after first use. See
// http://crbug.com/1045052.
cache_handler->ClearCachedMetadata(
+ ExecutionContext::GetCodeCacheHostFromContext(execution_context),
CachedMetadataHandler::kDiscardLocally);
}
cache_result.consume_result = absl::make_optional(
@@ -449,35 +472,17 @@ ScriptEvaluationResult V8ScriptRunner::CompileAndRunScript(
try_catch.SetVerbose(true);
}
- // Omit storing base URL if it is same as source URL.
- // Note: This improves chance of getting into a fast path in
- // ReferrerScriptInfo::ToV8HostDefinedOptions.
- const KURL base_url = classic_script->BaseURL();
- KURL stored_base_url = (base_url == source.Url()) ? KURL() : base_url;
-
- // TODO(hiroshige): Remove this code and related use counters once the
- // measurement is done.
- ReferrerScriptInfo::BaseUrlSource base_url_source =
- ReferrerScriptInfo::BaseUrlSource::kOther;
- if (source.SourceLocationType() ==
- ScriptSourceLocationType::kExternalFile &&
- !base_url.IsNull()) {
- switch (sanitize_script_errors) {
- case SanitizeScriptErrors::kDoNotSanitize:
- base_url_source =
- ReferrerScriptInfo::BaseUrlSource::kClassicScriptCORSSameOrigin;
- break;
- case SanitizeScriptErrors::kSanitize:
- base_url_source =
- ReferrerScriptInfo::BaseUrlSource::kClassicScriptCORSCrossOrigin;
- break;
- }
- }
- const ReferrerScriptInfo referrer_info(
- stored_base_url, classic_script->FetchOptions(), base_url_source);
+ const ReferrerScriptInfo referrer_info(classic_script->BaseURL(),
+ classic_script->FetchOptions());
v8::Local<v8::Script> script;
+ if (source.CacheHandler()) {
+ source.CacheHandler()->Check(
+ ExecutionContext::GetCodeCacheHostFromContext(execution_context),
+ source.Source());
+ }
+
v8::ScriptCompiler::CompileOptions compile_options;
V8CodeCache::ProduceCacheOptions produce_cache_options;
v8::ScriptCompiler::NoCacheReason no_cache_reason;
@@ -494,7 +499,17 @@ ScriptEvaluationResult V8ScriptRunner::CompileAndRunScript(
V8ScriptRunner::RunCompiledScript(isolate, script, execution_context);
probe::DidProduceCompilationCache(
probe::ToCoreProbeSink(execution_context), source, script);
- V8CodeCache::ProduceCache(isolate, script, source, produce_cache_options);
+
+ // The ScriptState has an associated context. We expect the current
+ // context to match the context associated with Script context when
+ // compiling the script in the main world. Hence it is safe to use the
+ // CodeCacheHost corresponding to the script execution context. For
+ // isolated world the contexts may not match. Though code caching is
+ // disabled for extensions so it is OK to use execution_context here.
+ V8CodeCache::ProduceCache(
+ isolate,
+ ExecutionContext::GetCodeCacheHostFromContext(execution_context),
+ script, source, produce_cache_options);
}
// TODO(crbug/1114601): Investigate whether to check CanContinue() in other