Remove dependency from WorkerThreadDebugger to global scope's Url()

Previously, WorkerThreadDebugger::ContextCreated() sets
V8ContextInfo using global scope's Url().
However, After [1] Url() can be null when the global scope is
created and thus ContextCreated() can't use Url().

This CL uses |GlobalScopeCreationParams::script_url| instead.
This CL shouldn't change the behavior, because currently
global scope's Url() is equal to
|GlobalScopeCreationParams::script_url|.

[1] https://chromium-review.googlesource.com/1139074

Bug: 866666, 861564
Change-Id: Ic19a87d77c4b01d3f850e51ce734dae11e1b4e44
Reviewed-on: https://chromium-review.googlesource.com/1139052
Reviewed-by: Aleksey Kozyatinskiy <kozyatinskiy@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Reviewed-by: Ian Kilpatrick <ikilpatrick@chromium.org>
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Commit-Queue: Hiroshige Hayashizaki <hiroshige@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579702}
diff --git a/third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.cc b/third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.cc
index 6a5c351..73fca4f 100644
--- a/third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.cc
+++ b/third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.cc
@@ -129,8 +129,7 @@
   if (!IsContextInitialized())
     return;
 
-  if (global_scope_->IsWorkerGlobalScope() ||
-      global_scope_->IsThreadedWorkletGlobalScope()) {
+  if (!global_scope_->IsMainThreadWorkletGlobalScope()) {
     ScriptState::Scope scope(script_state_);
     WorkerThreadDebugger* debugger = WorkerThreadDebugger::From(isolate_);
     debugger->ContextWillBeDestroyed(global_scope_->GetThread(),
@@ -142,7 +141,8 @@
 }
 
 bool WorkerOrWorkletScriptController::InitializeContextIfNeeded(
-    const String& human_readable_name) {
+    const String& human_readable_name,
+    const KURL& url_for_debugger) {
   v8::HandleScope handle_scope(isolate_);
 
   if (IsContextInitialized())
@@ -230,19 +230,19 @@
   // So we explicitly call constructorForType for the global object.
   V8PerContextData::From(context)->ConstructorForType(wrapper_type_info);
 
-  // Name new context for debugging. For main thread worklet global scopes
-  // this is done once the context is initialized.
-  if (global_scope_->IsWorkerGlobalScope() ||
-      global_scope_->IsThreadedWorkletGlobalScope()) {
+  if (global_scope_->IsMainThreadWorkletGlobalScope()) {
+    // Set the human readable name for the world if the call passes an actual
+    // |human_readable name|.
+    if (!human_readable_name.IsEmpty()) {
+      world_->SetNonMainWorldHumanReadableName(world_->GetWorldId(),
+                                               human_readable_name);
+    }
+  } else {
+    // Name new context for debugging. For main thread worklet global scopes
+    // this is done once the context is initialized.
     WorkerThreadDebugger* debugger = WorkerThreadDebugger::From(isolate_);
-    debugger->ContextCreated(global_scope_->GetThread(), context);
-  }
-
-  // Set the human readable name for the world if the call passes an actual
-  // |human_readable name|.
-  if (!human_readable_name.IsEmpty()) {
-    world_->SetNonMainWorldHumanReadableName(world_->GetWorldId(),
-                                             human_readable_name);
+    debugger->ContextCreated(global_scope_->GetThread(), url_for_debugger,
+                             context);
   }
 
   wrapper_type_info->InstallConditionalFeatures(
@@ -262,11 +262,11 @@
 ScriptValue WorkerOrWorkletScriptController::EvaluateInternal(
     const ScriptSourceCode& source_code,
     V8CacheOptions v8_cache_options) {
+  DCHECK(IsContextInitialized());
+
   TRACE_EVENT1("devtools.timeline", "EvaluateScript", "data",
                InspectorEvaluateScriptEvent::Data(nullptr, source_code.Url(),
                                                   source_code.StartPosition()));
-  if (!InitializeContextIfNeeded(String()))
-    return ScriptValue();
 
   ScriptState::Scope scope(script_state_);
 
diff --git a/third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.h b/third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.h
index 354f96a..9d40582 100644
--- a/third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.h
+++ b/third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.h
@@ -69,7 +69,12 @@
 
   // Used by WorkerThread. Returns true if the context is successfully
   // initialized or already initialized.
-  bool InitializeContextIfNeeded(const String& human_readable_name);
+  // For WorkerGlobalScope and ThreadedWorkletGlobalScope, |url_for_debugger| is
+  // and should be used only for setting name/origin that appears in DevTools.
+  // For other global scopes, |human_readable_name| is used for setting
+  // DOMWrapperWorld's human readable name.
+  bool InitializeContextIfNeeded(const String& human_readable_name,
+                                 const KURL& url_for_debugger);
 
   // Used by WorkerGlobalScope:
   void RethrowExceptionFromImportedScript(ErrorEvent*, ExceptionState&);
diff --git a/third_party/blink/renderer/core/inspector/worker_thread_debugger.cc b/third_party/blink/renderer/core/inspector/worker_thread_debugger.cc
index 57f03bb..7a03ef6 100644
--- a/third_party/blink/renderer/core/inspector/worker_thread_debugger.cc
+++ b/third_party/blink/renderer/core/inspector/worker_thread_debugger.cc
@@ -101,12 +101,13 @@
 }
 
 void WorkerThreadDebugger::ContextCreated(WorkerThread* worker_thread,
+                                          const KURL& url_for_debugger,
                                           v8::Local<v8::Context> context) {
   int worker_context_group_id = ContextGroupId(worker_thread);
   DCHECK(worker_threads_.Contains(worker_context_group_id));
   v8_inspector::V8ContextInfo context_info(context, worker_context_group_id,
                                            v8_inspector::StringView());
-  String origin = worker_thread->GlobalScope()->Url().GetString();
+  String origin = url_for_debugger;
   context_info.origin = ToV8InspectorStringView(origin);
   GetV8Inspector()->contextCreated(context_info);
 }
diff --git a/third_party/blink/renderer/core/inspector/worker_thread_debugger.h b/third_party/blink/renderer/core/inspector/worker_thread_debugger.h
index ca4ffef..eeb8c8f 100644
--- a/third_party/blink/renderer/core/inspector/worker_thread_debugger.h
+++ b/third_party/blink/renderer/core/inspector/worker_thread_debugger.h
@@ -52,7 +52,9 @@
   int ContextGroupId(WorkerThread*);
   void WorkerThreadCreated(WorkerThread*);
   void WorkerThreadDestroyed(WorkerThread*);
-  void ContextCreated(WorkerThread*, v8::Local<v8::Context>);
+  void ContextCreated(WorkerThread*,
+                      const KURL& url_for_debugger,
+                      v8::Local<v8::Context>);
   void ContextWillBeDestroyed(WorkerThread*, v8::Local<v8::Context>);
   void ExceptionThrown(WorkerThread*, ErrorEvent*);
 
diff --git a/third_party/blink/renderer/core/layout/custom/layout_worklet_global_scope.cc b/third_party/blink/renderer/core/layout/custom/layout_worklet_global_scope.cc
index fc68953..b5dde5c 100644
--- a/third_party/blink/renderer/core/layout/custom/layout_worklet_global_scope.cc
+++ b/third_party/blink/renderer/core/layout/custom/layout_worklet_global_scope.cc
@@ -31,7 +31,8 @@
                                    reporting_proxy, pending_layout_registry);
   String context_name("LayoutWorklet #");
   context_name.append(String::Number(global_scope_number));
-  global_scope->ScriptController()->InitializeContextIfNeeded(context_name);
+  global_scope->ScriptController()->InitializeContextIfNeeded(context_name,
+                                                              NullURL());
   MainThreadDebugger::Instance()->ContextCreated(
       global_scope->ScriptController()->GetScriptState(),
       global_scope->GetFrame(), global_scope->DocumentSecurityOrigin());
diff --git a/third_party/blink/renderer/core/loader/modulescript/module_script_loader_test.cc b/third_party/blink/renderer/core/loader/modulescript/module_script_loader_test.cc
index 0e9271fc..37876d9 100644
--- a/third_party/blink/renderer/core/loader/modulescript/module_script_loader_test.cc
+++ b/third_party/blink/renderer/core/loader/modulescript/module_script_loader_test.cc
@@ -192,7 +192,8 @@
       kV8CacheOptionsDefault, new WorkletModuleResponsesMap);
   global_scope_ = new MainThreadWorkletGlobalScope(
       &GetFrame(), std::move(creation_params), *reporting_proxy_);
-  global_scope_->ScriptController()->InitializeContextIfNeeded("Dummy Context");
+  global_scope_->ScriptController()->InitializeContextIfNeeded("Dummy Context",
+                                                               NullURL());
   modulator_ = new ModuleScriptLoaderTestModulator(
       global_scope_->ScriptController()->GetScriptState(),
       GetDocument().GetSecurityOrigin(), fetcher);
diff --git a/third_party/blink/renderer/core/workers/worker_thread.cc b/third_party/blink/renderer/core/workers/worker_thread.cc
index 49ce7587..7cbe6c5 100644
--- a/third_party/blink/renderer/core/workers/worker_thread.cc
+++ b/third_party/blink/renderer/core/workers/worker_thread.cc
@@ -420,6 +420,8 @@
     }
     GetWorkerBackingThread().BackingThread().AddTaskObserver(this);
 
+    const KURL url_for_debugger = global_scope_creation_params->script_url;
+
     console_message_storage_ = new ConsoleMessageStorage();
     global_scope_ =
         CreateWorkerGlobalScope(std::move(global_scope_creation_params));
@@ -436,7 +438,7 @@
     // TODO(nhiroki): Handle a case where the script controller fails to
     // initialize the context.
     if (GlobalScope()->ScriptController()->InitializeContextIfNeeded(
-            String())) {
+            String(), url_for_debugger)) {
       worker_reporting_proxy_.DidInitializeWorkerContext();
       v8::HandleScope handle_scope(GetIsolate());
       Platform::Current()->WorkerContextCreated(
diff --git a/third_party/blink/renderer/modules/csspaint/paint_worklet_global_scope.cc b/third_party/blink/renderer/modules/csspaint/paint_worklet_global_scope.cc
index e9b5c08..69116af 100644
--- a/third_party/blink/renderer/modules/csspaint/paint_worklet_global_scope.cc
+++ b/third_party/blink/renderer/modules/csspaint/paint_worklet_global_scope.cc
@@ -98,7 +98,8 @@
                                   reporting_proxy, pending_generator_registry);
   String context_name("PaintWorklet #");
   context_name.append(String::Number(global_scope_number));
-  global_scope->ScriptController()->InitializeContextIfNeeded(context_name);
+  global_scope->ScriptController()->InitializeContextIfNeeded(context_name,
+                                                              NullURL());
   MainThreadDebugger::Instance()->ContextCreated(
       global_scope->ScriptController()->GetScriptState(),
       global_scope->GetFrame(), global_scope->DocumentSecurityOrigin());