Commit 26afe117 authored by Hiroshige Hayashizaki's avatar Hiroshige Hayashizaki Committed by Commit Bot

Merge ScriptController logic into V8ScriptRunner (1/n)

In order to unify script evaluation paths in
ScriptController and WorkerOrWorkletScriptController,
This CL moves

- ReferrerScriptInfo construction,
- V8CodeCache::GetCompileOptions(),
- V8ScriptRunner::CompileScript(),
- V8ScriptRunner::RunCompiledScript(), and
- V8CodeCache::ProduceCache() calls

from

- ScriptController::ExecuteScriptAndReturnValue() and
- WorkerOrWorkletScriptController::EvaluateAndReturnValue()

to V8ScriptRunner::CompileAndRunScript().

The base URL and ScriptFetchOptions in
WorkerOrWorkletScriptController are set so that the
existing behavior is preserved.

Behavior changes: No changes, except that:
  probe::ProduceCompilationCache() will be called from
  WorkerOrWorkletScriptController after this CL.
  This was only called from ScriptController before this CL.

Bug: 1111134, 1114989, 1114994
Change-Id: I989c9a7c3e221f6f2c330849325111e9e209c85f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2251439Reviewed-by: default avatarKouhei Ueno <kouhei@chromium.org>
Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarDominic Farolino <dom@chromium.org>
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/master@{#797611}
parent 04cf6047
...@@ -37,10 +37,8 @@ ...@@ -37,10 +37,8 @@
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "third_party/blink/public/web/web_settings.h" #include "third_party/blink/public/web/web_settings.h"
#include "third_party/blink/renderer/bindings/core/v8/referrer_script_info.h"
#include "third_party/blink/renderer/bindings/core/v8/script_source_code.h" #include "third_party/blink/renderer/bindings/core/v8/script_source_code.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h" #include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_code_cache.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_gc_controller.h" #include "third_party/blink/renderer/bindings/core/v8/v8_gc_controller.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_script_runner.h" #include "third_party/blink/renderer/bindings/core/v8/v8_script_runner.h"
#include "third_party/blink/renderer/bindings/core/v8/window_proxy.h" #include "third_party/blink/renderer/bindings/core/v8/window_proxy.h"
...@@ -112,53 +110,11 @@ v8::Local<v8::Value> ScriptController::ExecuteScriptAndReturnValue( ...@@ -112,53 +110,11 @@ v8::Local<v8::Value> ScriptController::ExecuteScriptAndReturnValue(
v8::TryCatch try_catch(GetIsolate()); v8::TryCatch try_catch(GetIsolate());
try_catch.SetVerbose(true); try_catch.SetVerbose(true);
// Omit storing base URL if it is same as source URL. if (!V8ScriptRunner::CompileAndRunScript(
// Note: This improves chance of getting into a fast path in GetIsolate(), ScriptState::From(context), GetFrame()->DomWindow(),
// ReferrerScriptInfo::ToV8HostDefinedOptions. source, base_url, sanitize_script_errors, fetch_options,
KURL stored_base_url = (base_url == source.Url()) ? KURL() : base_url; v8_cache_options)
.ToLocal(&result)) {
// 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, fetch_options,
base_url_source);
v8::Local<v8::Script> script;
v8::ScriptCompiler::CompileOptions compile_options;
V8CodeCache::ProduceCacheOptions produce_cache_options;
v8::ScriptCompiler::NoCacheReason no_cache_reason;
std::tie(compile_options, produce_cache_options, no_cache_reason) =
V8CodeCache::GetCompileOptions(v8_cache_options, source);
if (!V8ScriptRunner::CompileScript(ScriptState::From(context), source,
sanitize_script_errors, compile_options,
no_cache_reason, referrer_info)
.ToLocal(&script))
return result;
v8::MaybeLocal<v8::Value> maybe_result;
maybe_result = V8ScriptRunner::RunCompiledScript(GetIsolate(), script,
GetFrame()->DomWindow());
probe::ProduceCompilationCache(frame_, source, script);
V8CodeCache::ProduceCache(GetIsolate(), script, source,
produce_cache_options);
if (!maybe_result.ToLocal(&result)) {
return result; return result;
} }
} }
......
...@@ -362,6 +362,65 @@ v8::MaybeLocal<v8::Value> V8ScriptRunner::RunCompiledScript( ...@@ -362,6 +362,65 @@ v8::MaybeLocal<v8::Value> V8ScriptRunner::RunCompiledScript(
return result; return result;
} }
v8::MaybeLocal<v8::Value> V8ScriptRunner::CompileAndRunScript(
v8::Isolate* isolate,
ScriptState* script_state,
ExecutionContext* execution_context,
const ScriptSourceCode& source,
const KURL& base_url,
SanitizeScriptErrors sanitize_script_errors,
const ScriptFetchOptions& fetch_options,
V8CacheOptions v8_cache_options) {
DCHECK_EQ(isolate, script_state->GetIsolate());
// Omit storing base URL if it is same as source URL.
// Note: This improves chance of getting into a fast path in
// ReferrerScriptInfo::ToV8HostDefinedOptions.
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, fetch_options,
base_url_source);
v8::Local<v8::Script> script;
v8::ScriptCompiler::CompileOptions compile_options;
V8CodeCache::ProduceCacheOptions produce_cache_options;
v8::ScriptCompiler::NoCacheReason no_cache_reason;
std::tie(compile_options, produce_cache_options, no_cache_reason) =
V8CodeCache::GetCompileOptions(v8_cache_options, source);
if (!V8ScriptRunner::CompileScript(script_state, source,
sanitize_script_errors, compile_options,
no_cache_reason, referrer_info)
.ToLocal(&script))
return v8::MaybeLocal<v8::Value>();
v8::MaybeLocal<v8::Value> maybe_result =
V8ScriptRunner::RunCompiledScript(isolate, script, execution_context);
probe::ProduceCompilationCache(probe::ToCoreProbeSink(execution_context),
source, script);
V8CodeCache::ProduceCache(isolate, script, source, produce_cache_options);
return maybe_result;
}
v8::MaybeLocal<v8::Value> V8ScriptRunner::CompileAndRunInternalScript( v8::MaybeLocal<v8::Value> V8ScriptRunner::CompileAndRunInternalScript(
v8::Isolate* isolate, v8::Isolate* isolate,
ScriptState* script_state, ScriptState* script_state,
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_V8_SCRIPT_RUNNER_H_ #define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_V8_SCRIPT_RUNNER_H_
#include "third_party/blink/renderer/bindings/core/v8/sanitize_script_errors.h" #include "third_party/blink/renderer/bindings/core/v8/sanitize_script_errors.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_cache_options.h"
#include "third_party/blink/renderer/core/core_export.h" #include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h" #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/forward.h" #include "third_party/blink/renderer/platform/wtf/forward.h"
...@@ -39,7 +40,9 @@ class TextPosition; ...@@ -39,7 +40,9 @@ class TextPosition;
namespace blink { namespace blink {
class ExecutionContext; class ExecutionContext;
class KURL;
class ReferrerScriptInfo; class ReferrerScriptInfo;
class ScriptFetchOptions;
class ScriptSourceCode; class ScriptSourceCode;
class ScriptState; class ScriptState;
class SingleCachedMetadataHandler; class SingleCachedMetadataHandler;
...@@ -66,9 +69,15 @@ class CORE_EXPORT V8ScriptRunner final { ...@@ -66,9 +69,15 @@ class CORE_EXPORT V8ScriptRunner final {
v8::ScriptCompiler::CompileOptions, v8::ScriptCompiler::CompileOptions,
v8::ScriptCompiler::NoCacheReason, v8::ScriptCompiler::NoCacheReason,
const ReferrerScriptInfo&); const ReferrerScriptInfo&);
static v8::MaybeLocal<v8::Value> RunCompiledScript(v8::Isolate*, static v8::MaybeLocal<v8::Value> CompileAndRunScript(
v8::Local<v8::Script>, v8::Isolate*,
ExecutionContext*); ScriptState*,
ExecutionContext*,
const ScriptSourceCode&,
const KURL&,
SanitizeScriptErrors,
const ScriptFetchOptions&,
V8CacheOptions);
static v8::MaybeLocal<v8::Value> CompileAndRunInternalScript( static v8::MaybeLocal<v8::Value> CompileAndRunInternalScript(
v8::Isolate*, v8::Isolate*,
ScriptState*, ScriptState*,
...@@ -102,6 +111,11 @@ class CORE_EXPORT V8ScriptRunner final { ...@@ -102,6 +111,11 @@ class CORE_EXPORT V8ScriptRunner final {
// TODO(adamk): This should live on V8ThrowException, but it depends on // TODO(adamk): This should live on V8ThrowException, but it depends on
// V8Initializer and so can't trivially move to platform/bindings. // V8Initializer and so can't trivially move to platform/bindings.
static void ReportException(v8::Isolate*, v8::Local<v8::Value> exception); static void ReportException(v8::Isolate*, v8::Local<v8::Value> exception);
private:
static v8::MaybeLocal<v8::Value> RunCompiledScript(v8::Isolate*,
v8::Local<v8::Script>,
ExecutionContext*);
}; };
} // namespace blink } // namespace blink
......
...@@ -358,26 +358,26 @@ v8::Local<v8::Value> WorkerOrWorkletScriptController::EvaluateInternal( ...@@ -358,26 +358,26 @@ v8::Local<v8::Value> WorkerOrWorkletScriptController::EvaluateInternal(
v8::TryCatch block(isolate_); v8::TryCatch block(isolate_);
v8::Local<v8::Script> compiled_script; // TODO(crbug/1114994): Plumb this from ClassicScript.
v8::MaybeLocal<v8::Value> maybe_result; const KURL base_url = source_code.Url();
// Use default ReferrerScriptInfo here, as // Use default ReferrerScriptInfo here, as
// - A work{er,let} script doesn't have a nonce, and // - A work{er,let} script doesn't have a nonce, and
// - a work{er,let} script is always "not parser inserted". // - a work{er,let} script is always "not parser inserted".
ReferrerScriptInfo referrer_info; // TODO(crbug/1114988): After crbug/1114988 is fixed, this can be the
v8::ScriptCompiler::CompileOptions compile_options; // default ScriptFetchOptions(). Currently the default ScriptFetchOptions()
V8CodeCache::ProduceCacheOptions produce_cache_options; // is not used because it has CredentialsMode::kOmit.
v8::ScriptCompiler::NoCacheReason no_cache_reason; // TODO(crbug/1114989): Plumb this from ClassicScript.
std::tie(compile_options, produce_cache_options, no_cache_reason) = ScriptFetchOptions script_fetch_options(
V8CodeCache::GetCompileOptions(v8_cache_options, source_code); String(), IntegrityMetadataSet(), String(),
if (V8ScriptRunner::CompileScript(script_state_, source_code, ParserDisposition::kNotParserInserted,
sanitize_script_errors, compile_options, network::mojom::CredentialsMode::kSameOrigin,
no_cache_reason, referrer_info) network::mojom::ReferrerPolicy::kDefault,
.ToLocal(&compiled_script)) { mojom::blink::FetchImportanceMode::kImportanceAuto);
maybe_result = V8ScriptRunner::RunCompiledScript(isolate_, compiled_script,
global_scope_); v8::MaybeLocal<v8::Value> maybe_result = V8ScriptRunner::CompileAndRunScript(
V8CodeCache::ProduceCache(isolate_, compiled_script, source_code, isolate_, script_state_, global_scope_, source_code, base_url,
produce_cache_options); sanitize_script_errors, script_fetch_options, v8_cache_options);
}
if (!block.CanContinue()) { if (!block.CanContinue()) {
ForbidExecution(); ForbidExecution();
......
...@@ -170,7 +170,7 @@ interface CoreProbes { ...@@ -170,7 +170,7 @@ interface CoreProbes {
void DidCloseAudioContext(Document*); void DidCloseAudioContext(Document*);
void DidResumeAudioContext(Document*); void DidResumeAudioContext(Document*);
void DidSuspendAudioContext(Document*); void DidSuspendAudioContext(Document*);
void ProduceCompilationCache(LocalFrame*, const ScriptSourceCode& source, v8::Local<v8::Script> script); void ProduceCompilationCache(CoreProbeSink*, const ScriptSourceCode& source, v8::Local<v8::Script> script);
void ConsumeCompilationCache(ExecutionContext*, const ScriptSourceCode& source, v8::ScriptCompiler::CachedData** cached_data); void ConsumeCompilationCache(ExecutionContext*, const ScriptSourceCode& source, v8::ScriptCompiler::CachedData** cached_data);
void NodeCreated([Keep] Node* node); void NodeCreated([Keep] Node* node);
void PortalRemoteFrameCreated(Document*, HTMLPortalElement* portal_element); void PortalRemoteFrameCreated(Document*, HTMLPortalElement* portal_element);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment