Commit b734a673 authored by Hiroshige Hayashizaki's avatar Hiroshige Hayashizaki Committed by Commit Bot

Merge ScriptController logic into V8ScriptRunner (2/n)

This CL moves:

- TRACE_EVENT
- v8::TryCatch and related exception handling
- RethrowErrorsOption

from ScriptController/WorkerOrWorkletScriptController
to V8ScriptRunner.

This CL doesn't change the behavior.

Bug: 1111134, 1129743
Change-Id: I62db5643acdf362a44420e7cf5694839bca4c4a9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2351605
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812638}
parent 270b0650
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "third_party/blink/public/mojom/v8_cache_options.mojom-blink.h" #include "third_party/blink/public/mojom/v8_cache_options.mojom-blink.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/script_evaluation_result.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_gc_controller.h" #include "third_party/blink/renderer/bindings/core/v8/v8_gc_controller.h"
...@@ -83,40 +84,29 @@ void ScriptController::UpdateSecurityOrigin( ...@@ -83,40 +84,29 @@ void ScriptController::UpdateSecurityOrigin(
window_proxy_manager_->UpdateSecurityOrigin(security_origin); window_proxy_manager_->UpdateSecurityOrigin(security_origin);
} }
// TODO(crbug/1129743): Use ScriptEvaluationResult instead of
// v8::Local<v8::Value> as the return type.
v8::Local<v8::Value> ScriptController::ExecuteScriptAndReturnValue( v8::Local<v8::Value> ScriptController::ExecuteScriptAndReturnValue(
v8::Local<v8::Context> context, v8::Local<v8::Context> context,
const ScriptSourceCode& source, const ScriptSourceCode& source,
const KURL& base_url, const KURL& base_url,
SanitizeScriptErrors sanitize_script_errors, SanitizeScriptErrors sanitize_script_errors,
const ScriptFetchOptions& fetch_options) { const ScriptFetchOptions& fetch_options) {
TRACE_EVENT1("devtools.timeline", "EvaluateScript", "data",
inspector_evaluate_script_event::Data(window_->GetFrame(),
source.Url().GetString(),
source.StartPosition()));
v8::Local<v8::Value> result;
{
mojom::blink::V8CacheOptions v8_cache_options = mojom::blink::V8CacheOptions v8_cache_options =
mojom::blink::V8CacheOptions::kDefault; mojom::blink::V8CacheOptions::kDefault;
if (const Settings* settings = window_->GetFrame()->GetSettings()) if (const Settings* settings = window_->GetFrame()->GetSettings())
v8_cache_options = settings->GetV8CacheOptions(); v8_cache_options = settings->GetV8CacheOptions();
// Isolate exceptions that occur when compiling and executing ScriptEvaluationResult result = V8ScriptRunner::CompileAndRunScript(
// the code. These exceptions should not interfere with GetIsolate(), ScriptState::From(context), window_.Get(), source,
// javascript code we might evaluate from C++ when returning base_url, sanitize_script_errors, fetch_options,
// from here. std::move(v8_cache_options),
v8::TryCatch try_catch(GetIsolate()); V8ScriptRunner::RethrowErrorsOption::DoNotRethrow());
try_catch.SetVerbose(true);
if (result.GetResultType() == ScriptEvaluationResult::ResultType::kSuccess)
if (!V8ScriptRunner::CompileAndRunScript( return result.GetSuccessValue();
GetIsolate(), ScriptState::From(context), window_.Get(), source,
base_url, sanitize_script_errors, fetch_options,
std::move(v8_cache_options))
.ToLocal(&result)) {
return result;
}
}
return result; return v8::Local<v8::Value>();
} }
TextPosition ScriptController::EventHandlerPosition() const { TextPosition ScriptController::EventHandlerPosition() const {
......
...@@ -31,11 +31,13 @@ ...@@ -31,11 +31,13 @@
#include "third_party/blink/public/mojom/v8_cache_options.mojom-blink.h" #include "third_party/blink/public/mojom/v8_cache_options.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/binding_security.h" #include "third_party/blink/renderer/bindings/core/v8/binding_security.h"
#include "third_party/blink/renderer/bindings/core/v8/referrer_script_info.h" #include "third_party/blink/renderer/bindings/core/v8/referrer_script_info.h"
#include "third_party/blink/renderer/bindings/core/v8/script_evaluation_result.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/script_streamer.h" #include "third_party/blink/renderer/bindings/core/v8/script_streamer.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_code_cache.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_initializer.h" #include "third_party/blink/renderer/bindings/core/v8/v8_initializer.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_throw_dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/agent.h" #include "third_party/blink/renderer/core/execution_context/agent.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h" #include "third_party/blink/renderer/core/frame/local_dom_window.h"
...@@ -364,7 +366,7 @@ v8::MaybeLocal<v8::Value> V8ScriptRunner::RunCompiledScript( ...@@ -364,7 +366,7 @@ v8::MaybeLocal<v8::Value> V8ScriptRunner::RunCompiledScript(
return result; return result;
} }
v8::MaybeLocal<v8::Value> V8ScriptRunner::CompileAndRunScript( ScriptEvaluationResult V8ScriptRunner::CompileAndRunScript(
v8::Isolate* isolate, v8::Isolate* isolate,
ScriptState* script_state, ScriptState* script_state,
ExecutionContext* execution_context, ExecutionContext* execution_context,
...@@ -372,55 +374,131 @@ v8::MaybeLocal<v8::Value> V8ScriptRunner::CompileAndRunScript( ...@@ -372,55 +374,131 @@ v8::MaybeLocal<v8::Value> V8ScriptRunner::CompileAndRunScript(
const KURL& base_url, const KURL& base_url,
SanitizeScriptErrors sanitize_script_errors, SanitizeScriptErrors sanitize_script_errors,
const ScriptFetchOptions& fetch_options, const ScriptFetchOptions& fetch_options,
mojom::blink::V8CacheOptions v8_cache_options) { mojom::blink::V8CacheOptions v8_cache_options,
RethrowErrorsOption rethrow_errors) {
DCHECK_EQ(isolate, script_state->GetIsolate()); DCHECK_EQ(isolate, script_state->GetIsolate());
// Omit storing base URL if it is same as source URL. LocalDOMWindow* window = DynamicTo<LocalDOMWindow>(execution_context);
// Note: This improves chance of getting into a fast path in LocalFrame* frame = window ? window->GetFrame() : nullptr;
// ReferrerScriptInfo::ToV8HostDefinedOptions. TRACE_EVENT1("devtools.timeline", "EvaluateScript", "data",
KURL stored_base_url = (base_url == source.Url()) ? KURL() : base_url; inspector_evaluate_script_event::Data(
frame, source.Url().GetString(), source.StartPosition()));
// TODO(hiroshige): Remove this code and related use counters once the
// measurement is done. // Scope for |v8::TryCatch|.
ReferrerScriptInfo::BaseUrlSource base_url_source = {
ReferrerScriptInfo::BaseUrlSource::kOther; v8::TryCatch try_catch(isolate);
if (source.SourceLocationType() == ScriptSourceLocationType::kExternalFile && // Step 8.3. Otherwise, rethrow errors is false. Perform the following
!base_url.IsNull()) { // steps: [spec text]
switch (sanitize_script_errors) { // Step 8.3.1. Report the exception given by evaluationStatus.[[Value]]
case SanitizeScriptErrors::kDoNotSanitize: // for script. [spec text]
base_url_source = //
ReferrerScriptInfo::BaseUrlSource::kClassicScriptCORSSameOrigin; // This will be done inside V8 by setting TryCatch::SetVerbose(true) here.
break; if (!rethrow_errors.ShouldRethrow()) {
case SanitizeScriptErrors::kSanitize: try_catch.SetVerbose(true);
base_url_source =
ReferrerScriptInfo::BaseUrlSource::kClassicScriptCORSCrossOrigin;
break;
} }
}
const ReferrerScriptInfo referrer_info(stored_base_url, fetch_options,
base_url_source);
v8::Local<v8::Script> script; // 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);
v8::MaybeLocal<v8::Value> maybe_result;
if (V8ScriptRunner::CompileScript(script_state, source,
sanitize_script_errors, compile_options,
no_cache_reason, referrer_info)
.ToLocal(&script)) {
maybe_result =
V8ScriptRunner::RunCompiledScript(isolate, script, execution_context);
probe::ProduceCompilationCache(probe::ToCoreProbeSink(execution_context),
source, script);
V8CodeCache::ProduceCache(isolate, script, source, produce_cache_options);
}
v8::ScriptCompiler::CompileOptions compile_options; // TODO(crbug/1114601): Investigate whether to check CanContinue() in other
V8CodeCache::ProduceCacheOptions produce_cache_options; // script evaluation code paths.
v8::ScriptCompiler::NoCacheReason no_cache_reason; if (!try_catch.CanContinue()) {
std::tie(compile_options, produce_cache_options, no_cache_reason) = return ScriptEvaluationResult::FromClassicAborted();
V8CodeCache::GetCompileOptions(v8_cache_options, source); }
if (!V8ScriptRunner::CompileScript(script_state, source, if (!try_catch.HasCaught()) {
sanitize_script_errors, compile_options, // Step 10. If evaluationStatus is a normal completion, then return
no_cache_reason, referrer_info) // evaluationStatus. [spec text]
.ToLocal(&script)) v8::Local<v8::Value> result;
return v8::MaybeLocal<v8::Value>(); bool success = maybe_result.ToLocal(&result);
DCHECK(success);
return ScriptEvaluationResult::FromClassicSuccess(result);
}
v8::MaybeLocal<v8::Value> maybe_result = DCHECK(maybe_result.IsEmpty());
V8ScriptRunner::RunCompiledScript(isolate, script, execution_context);
probe::ProduceCompilationCache(probe::ToCoreProbeSink(execution_context), if (rethrow_errors.ShouldRethrow() &&
source, script); sanitize_script_errors == SanitizeScriptErrors::kDoNotSanitize) {
V8CodeCache::ProduceCache(isolate, script, source, produce_cache_options); // Step 8.1. If rethrow errors is true and script's muted errors is
// false, then: [spec text]
//
// Step 8.1.2. Rethrow evaluationStatus.[[Value]]. [spec text]
//
// We rethrow exceptions reported from importScripts() here. The
// original filename/lineno/colno information (which points inside of
// imported scripts) is kept through ReThrow(), and will be eventually
// reported to WorkerGlobalScope.onerror via `TryCatch::SetVerbose(true)`
// called at top-level worker script evaluation.
try_catch.ReThrow();
return ScriptEvaluationResult::FromClassicException();
}
}
// |v8::TryCatch| is (and should be) exited, before ThrowException() below.
if (rethrow_errors.ShouldRethrow()) {
// kDoNotSanitize case is processed and early-exited above.
DCHECK_EQ(sanitize_script_errors, SanitizeScriptErrors::kSanitize);
// Step 8.2. If rethrow errors is true and script's muted errors is
// true, then: [spec text]
//
// Step 8.2.2. Throw a "NetworkError" DOMException. [spec text]
//
// We don't supply any message here to avoid leaking details of muted
// errors.
V8ThrowException::ThrowException(
isolate, V8ThrowDOMException::CreateOrEmpty(
isolate, DOMExceptionCode::kNetworkError,
rethrow_errors.Message()));
return ScriptEvaluationResult::FromClassicException();
}
return maybe_result; // #report-the-error for rethrow errors == true is already handled via
// |TryCatch::SetVerbose(true)| above.
return ScriptEvaluationResult::FromClassicException();
} }
v8::MaybeLocal<v8::Value> V8ScriptRunner::CompileAndRunInternalScript( v8::MaybeLocal<v8::Value> V8ScriptRunner::CompileAndRunInternalScript(
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#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"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "v8/include/v8.h" #include "v8/include/v8.h"
namespace WTF { namespace WTF {
...@@ -39,6 +40,7 @@ class TextPosition; ...@@ -39,6 +40,7 @@ class TextPosition;
namespace blink { namespace blink {
class ScriptEvaluationResult;
class ExecutionContext; class ExecutionContext;
class KURL; class KURL;
class ReferrerScriptInfo; class ReferrerScriptInfo;
...@@ -51,6 +53,43 @@ class CORE_EXPORT V8ScriptRunner final { ...@@ -51,6 +53,43 @@ class CORE_EXPORT V8ScriptRunner final {
STATIC_ONLY(V8ScriptRunner); STATIC_ONLY(V8ScriptRunner);
public: public:
// Rethrow errors flag in
// https://html.spec.whatwg.org/C/#run-a-classic-script
class RethrowErrorsOption final {
STACK_ALLOCATED();
public:
RethrowErrorsOption(RethrowErrorsOption&&) = default;
RethrowErrorsOption& operator=(RethrowErrorsOption&&) = default;
RethrowErrorsOption(const RethrowErrorsOption&) = delete;
RethrowErrorsOption& operator=(const RethrowErrorsOption&) = delete;
// Rethrow errors flag is false.
static RethrowErrorsOption DoNotRethrow() {
return RethrowErrorsOption(base::nullopt);
}
// Rethrow errors flag is true. When rethrowing, a NetworkError with
// `message` is thrown. This is used only for importScripts(), and
// `message` is used to throw NetworkErrors with the same message text,
// no matter whether the NetworkError is thrown inside or outside
// EvaluateAndReturnValue().
static RethrowErrorsOption Rethrow(const String& message) {
return RethrowErrorsOption(message);
}
bool ShouldRethrow() const { return static_cast<bool>(message_); }
String Message() const { return *message_; }
private:
explicit RethrowErrorsOption(base::Optional<String> message)
: message_(std::move(message)) {}
// `nullopt` <=> rethrow errors is false.
base::Optional<String> message_;
};
// For the following methods, the caller sites have to hold // For the following methods, the caller sites have to hold
// a HandleScope and a ContextScope. // a HandleScope and a ContextScope.
static v8::MaybeLocal<v8::Script> CompileScript( static v8::MaybeLocal<v8::Script> CompileScript(
...@@ -69,7 +108,7 @@ class CORE_EXPORT V8ScriptRunner final { ...@@ -69,7 +108,7 @@ 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> CompileAndRunScript( static ScriptEvaluationResult CompileAndRunScript(
v8::Isolate*, v8::Isolate*,
ScriptState*, ScriptState*,
ExecutionContext*, ExecutionContext*,
...@@ -77,7 +116,8 @@ class CORE_EXPORT V8ScriptRunner final { ...@@ -77,7 +116,8 @@ class CORE_EXPORT V8ScriptRunner final {
const KURL&, const KURL&,
SanitizeScriptErrors, SanitizeScriptErrors,
const ScriptFetchOptions&, const ScriptFetchOptions&,
mojom::blink::V8CacheOptions); mojom::blink::V8CacheOptions,
RethrowErrorsOption);
static v8::MaybeLocal<v8::Value> CompileAndRunInternalScript( static v8::MaybeLocal<v8::Value> CompileAndRunInternalScript(
v8::Isolate*, v8::Isolate*,
ScriptState*, ScriptState*,
......
...@@ -316,114 +316,41 @@ ScriptEvaluationResult WorkerOrWorkletScriptController::EvaluateAndReturnValue( ...@@ -316,114 +316,41 @@ ScriptEvaluationResult WorkerOrWorkletScriptController::EvaluateAndReturnValue(
const ScriptSourceCode& source_code, const ScriptSourceCode& source_code,
SanitizeScriptErrors sanitize_script_errors, SanitizeScriptErrors sanitize_script_errors,
mojom::blink::V8CacheOptions v8_cache_options, mojom::blink::V8CacheOptions v8_cache_options,
RethrowErrorsOption rethrow_errors) { V8ScriptRunner::RethrowErrorsOption rethrow_errors) {
if (IsExecutionForbidden()) { if (IsExecutionForbidden())
return ScriptEvaluationResult::FromClassicNotRun(); return ScriptEvaluationResult::FromClassicNotRun();
}
// Scope for |TRACE_EVENT1| and |v8::TryCatch| below.
{
DCHECK(IsContextInitialized());
DCHECK(is_ready_to_evaluate_);
TRACE_EVENT1("devtools.timeline", "EvaluateScript", "data",
inspector_evaluate_script_event::Data(
nullptr, source_code.Url(), source_code.StartPosition()));
v8::TryCatch block(isolate_);
// Step 8.3. Otherwise, rethrow errors is false. Perform the following
// steps: [spec text]
// Step 8.3.1. Report the exception given by evaluationStatus.[[Value]]
// for script. [spec text]
//
// This will be done inside V8 (inside V8ScriptRunner::CompileAndRunScript()
// below) by setting TryCatch::SetVerbose(true) here.
if (!rethrow_errors.ShouldRethrow()) {
block.SetVerbose(true);
}
// TODO(crbug/1114994): Plumb this from ClassicScript.
const KURL base_url = source_code.Url();
// Use default ReferrerScriptInfo here, as
// - A work{er,let} script doesn't have a nonce, and
// - a work{er,let} script is always "not parser inserted".
// TODO(crbug/1114988): After crbug/1114988 is fixed, this can be the
// default ScriptFetchOptions(). Currently the default ScriptFetchOptions()
// is not used because it has CredentialsMode::kOmit.
// TODO(crbug/1114989): Plumb this from ClassicScript.
ScriptFetchOptions script_fetch_options(
String(), IntegrityMetadataSet(), String(),
ParserDisposition::kNotParserInserted,
network::mojom::CredentialsMode::kSameOrigin,
network::mojom::ReferrerPolicy::kDefault,
mojom::blink::FetchImportanceMode::kImportanceAuto);
v8::MaybeLocal<v8::Value> maybe_result =
V8ScriptRunner::CompileAndRunScript(
isolate_, script_state_, global_scope_, source_code, base_url,
sanitize_script_errors, script_fetch_options, v8_cache_options);
// TODO(crbug/1114601): Investigate whether to check CanContinue() in other
// script evaluation code paths.
if (!block.CanContinue()) {
ForbidExecution();
return ScriptEvaluationResult::FromClassicAborted();
}
DCHECK(IsContextInitialized());
DCHECK(is_ready_to_evaluate_);
// TODO(crbug/1114994): Plumb this from ClassicScript.
const KURL base_url = source_code.Url();
// Use default ReferrerScriptInfo here, as
// - A work{er,let} script doesn't have a nonce, and
// - a work{er,let} script is always "not parser inserted".
// TODO(crbug/1114988): After crbug/1114988 is fixed, this can be the
// default ScriptFetchOptions(). Currently the default ScriptFetchOptions()
// is not used because it has CredentialsMode::kOmit.
// TODO(crbug/1114989): Plumb this from ClassicScript.
ScriptFetchOptions script_fetch_options(
String(), IntegrityMetadataSet(), String(),
ParserDisposition::kNotParserInserted,
network::mojom::CredentialsMode::kSameOrigin,
network::mojom::ReferrerPolicy::kDefault,
mojom::blink::FetchImportanceMode::kImportanceAuto);
ScriptEvaluationResult result = V8ScriptRunner::CompileAndRunScript(
isolate_, script_state_, global_scope_, source_code, base_url,
sanitize_script_errors, script_fetch_options, v8_cache_options,
std::move(rethrow_errors));
if (result.GetResultType() == ScriptEvaluationResult::ResultType::kAborted)
ForbidExecution();
else
CHECK(!IsExecutionForbidden()); CHECK(!IsExecutionForbidden());
if (!block.HasCaught()) { return result;
// Step 10. If evaluationStatus is a normal completion, then return
// evaluationStatus. [spec text]
v8::Local<v8::Value> result;
bool success = maybe_result.ToLocal(&result);
DCHECK(success);
return ScriptEvaluationResult::FromClassicSuccess(result);
}
DCHECK(maybe_result.IsEmpty());
if (rethrow_errors.ShouldRethrow() &&
sanitize_script_errors == SanitizeScriptErrors::kDoNotSanitize) {
// Step 8.1. If rethrow errors is true and script's muted errors is
// false, then: [spec text]
//
// Step 8.1.2. Rethrow evaluationStatus.[[Value]]. [spec text]
//
// We rethrow exceptions reported from importScripts() here. The
// original filename/lineno/colno information (which points inside of
// imported scripts) is kept through ReThrow(), and will be eventually
// reported to WorkerGlobalScope.onerror via `TryCatch::SetVerbose(true)`
// called at top-level worker script evaluation.
block.ReThrow();
return ScriptEvaluationResult::FromClassicException();
}
}
// |v8::TryCatch| is (and should be) exited, before ThrowException() below.
if (rethrow_errors.ShouldRethrow()) {
// kDoNotSanitize case is processed and early-exited above.
DCHECK_EQ(sanitize_script_errors, SanitizeScriptErrors::kSanitize);
// Step 8.2. If rethrow errors is true and script's muted errors is
// true, then: [spec text]
//
// Step 8.2.2. Throw a "NetworkError" DOMException. [spec text]
//
// We don't supply any message here to avoid leaking details of muted
// errors.
V8ThrowException::ThrowException(
isolate_, V8ThrowDOMException::CreateOrEmpty(
isolate_, DOMExceptionCode::kNetworkError,
rethrow_errors.Message()));
return ScriptEvaluationResult::FromClassicException();
}
// #report-the-error for rethrow errors == true is already handled via
// |TryCatch::SetVerbose(true)| above.
return ScriptEvaluationResult::FromClassicException();
} }
void WorkerOrWorkletScriptController::ForbidExecution() { void WorkerOrWorkletScriptController::ForbidExecution() {
......
...@@ -57,50 +57,14 @@ class CORE_EXPORT WorkerOrWorkletScriptController final ...@@ -57,50 +57,14 @@ class CORE_EXPORT WorkerOrWorkletScriptController final
bool IsExecutionForbidden() const; bool IsExecutionForbidden() const;
// Rethrow errors flag in
// https://html.spec.whatwg.org/C/#run-a-classic-script
class RethrowErrorsOption final {
STACK_ALLOCATED();
public:
RethrowErrorsOption(RethrowErrorsOption&&) = default;
RethrowErrorsOption& operator=(RethrowErrorsOption&&) = default;
RethrowErrorsOption(const RethrowErrorsOption&) = delete;
RethrowErrorsOption& operator=(const RethrowErrorsOption&) = delete;
// Rethrow errors flag is false.
static RethrowErrorsOption DoNotRethrow() {
return RethrowErrorsOption(base::nullopt);
}
// Rethrow errors flag is true. When rethrowing, a NetworkError with
// `message` is thrown. This is used only for importScripts(), and
// `message` is used to throw NetworkErrors with the same message text,
// no matter whether the NetworkError is thrown inside or outside
// EvaluateAndReturnValue().
static RethrowErrorsOption Rethrow(const String& message) {
return RethrowErrorsOption(message);
}
bool ShouldRethrow() const { return static_cast<bool>(message_); }
String Message() const { return *message_; }
private:
explicit RethrowErrorsOption(base::Optional<String> message)
: message_(std::move(message)) {}
// `nullopt` <=> rethrow errors is false.
base::Optional<String> message_;
};
// https://html.spec.whatwg.org/C/#run-a-classic-script // https://html.spec.whatwg.org/C/#run-a-classic-script
// Callers should enter ScriptState::Scope before calling this. // Callers should enter ScriptState::Scope before calling this.
ScriptEvaluationResult EvaluateAndReturnValue( ScriptEvaluationResult EvaluateAndReturnValue(
const ScriptSourceCode&, const ScriptSourceCode&,
SanitizeScriptErrors sanitize_script_errors, SanitizeScriptErrors sanitize_script_errors,
mojom::blink::V8CacheOptions = mojom::blink::V8CacheOptions::kDefault, mojom::blink::V8CacheOptions = mojom::blink::V8CacheOptions::kDefault,
RethrowErrorsOption = RethrowErrorsOption::DoNotRethrow()); V8ScriptRunner::RethrowErrorsOption =
V8ScriptRunner::RethrowErrorsOption::DoNotRethrow());
// Prevents future JavaScript execution. // Prevents future JavaScript execution.
void ForbidExecution(); void ForbidExecution();
......
...@@ -339,8 +339,7 @@ void WorkerGlobalScope::ImportScriptsInternal(const Vector<String>& urls) { ...@@ -339,8 +339,7 @@ void WorkerGlobalScope::ImportScriptsInternal(const Vector<String>& urls) {
ScriptSourceCode::UsePostRedirectURL() ? response_url ScriptSourceCode::UsePostRedirectURL() ? response_url
: complete_url), : complete_url),
sanitize_script_errors, GetV8CacheOptions(), sanitize_script_errors, GetV8CacheOptions(),
WorkerOrWorkletScriptController::RethrowErrorsOption::Rethrow( V8ScriptRunner::RethrowErrorsOption::Rethrow(error_message));
error_message));
// Step 5.2: "If an exception was thrown or if the script was prematurely // Step 5.2: "If an exception was thrown or if the script was prematurely
// aborted, then abort all these steps, letting the exception or aborting // aborted, then abort all these steps, letting the exception or aborting
......
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