From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 20 Sep 2018 17:47:12 -0700 Subject: worker_context_will_destroy.patch This adds a hook for worker context destruction, which we use in Electron to shutdown node integration in the worker if relevant. An attempt to upstream this was made, but rejected: https://chromium-review.googlesource.com/c/chromium/src/+/1954347 diff --git a/content/public/renderer/content_renderer_client.h b/content/public/renderer/content_renderer_client.h index 87df60c52d1e6adf464e85deccef87ae2c6cb48c..5dabb49f061b4e0643256cbf9162d128c1e0bacf 100644 --- a/content/public/renderer/content_renderer_client.h +++ b/content/public/renderer/content_renderer_client.h @@ -389,6 +389,11 @@ class CONTENT_EXPORT ContentRendererClient { virtual void DidInitializeWorkerContextOnWorkerThread( v8::Local context) {} + // Notifies that a worker context will be destroyed. This function is called + // from the worker thread. + virtual void WillDestroyWorkerContextOnWorkerThread( + v8::Local context) {} + // Overwrites the given URL to use an HTML5 embed if possible. // An empty URL is returned if the URL is not overriden. virtual GURL OverrideFlashEmbedWithHTML(const GURL& url); diff --git a/content/renderer/renderer_blink_platform_impl.cc b/content/renderer/renderer_blink_platform_impl.cc index 58c1b0e78a66229a23cf2c930fa059a94dc70323..d549bdc3d300f8eb11e7fd9127cb06e1a0e9c720 100644 --- a/content/renderer/renderer_blink_platform_impl.cc +++ b/content/renderer/renderer_blink_platform_impl.cc @@ -879,6 +879,12 @@ void RendererBlinkPlatformImpl::WillStopWorkerThread() { WorkerThreadRegistry::Instance()->WillStopCurrentWorkerThread(); } +void RendererBlinkPlatformImpl::WorkerContextWillDestroy( + const v8::Local& worker) { + GetContentClient()->renderer()->WillDestroyWorkerContextOnWorkerThread( + worker); +} + void RendererBlinkPlatformImpl::WorkerContextCreated( const v8::Local& worker) { GetContentClient()->renderer()->DidInitializeWorkerContextOnWorkerThread( diff --git a/content/renderer/renderer_blink_platform_impl.h b/content/renderer/renderer_blink_platform_impl.h index 096cd162c7d5228fea0c56ad007b9939e6f27809..1c332800cb133f93d21ee628b3b85e89ab6d7da7 100644 --- a/content/renderer/renderer_blink_platform_impl.h +++ b/content/renderer/renderer_blink_platform_impl.h @@ -181,6 +181,7 @@ class CONTENT_EXPORT RendererBlinkPlatformImpl : public BlinkPlatformImpl { void DidStartWorkerThread() override; void WillStopWorkerThread() override; void WorkerContextCreated(const v8::Local& worker) override; + void WorkerContextWillDestroy(const v8::Local& worker) override; bool IsExcludedHeaderForServiceWorkerFetchEvent( const blink::WebString& header_name) override; diff --git a/third_party/blink/public/platform/platform.h b/third_party/blink/public/platform/platform.h index 8690ec1fdd213a4898ebce1c8dce38079972f7df..b06e541ec3cbb6cac7b58222a4718b4560a49ff0 100644 --- a/third_party/blink/public/platform/platform.h +++ b/third_party/blink/public/platform/platform.h @@ -623,6 +623,7 @@ class BLINK_PLATFORM_EXPORT Platform { virtual void DidStartWorkerThread() {} virtual void WillStopWorkerThread() {} virtual void WorkerContextCreated(const v8::Local& worker) {} + virtual void WorkerContextWillDestroy(const v8::Local& worker) {} virtual bool AllowScriptExtensionForServiceWorker( const WebSecurityOrigin& script_origin) { return false; diff --git a/third_party/blink/renderer/core/workers/worker_thread.cc b/third_party/blink/renderer/core/workers/worker_thread.cc index e00f788541d7c0f96d2c00c4840da1598c3f7a81..0c3d589ba1ca0727d46feda8ae48c5d0049c304e 100644 --- a/third_party/blink/renderer/core/workers/worker_thread.cc +++ b/third_party/blink/renderer/core/workers/worker_thread.cc @@ -681,6 +681,12 @@ void WorkerThread::PrepareForShutdownOnWorkerThread() { nested_runner_->QuitNow(); } + { + v8::HandleScope handle_scope(GetIsolate()); + Platform::Current()->WorkerContextWillDestroy( + GlobalScope()->ScriptController()->GetContext()); + } + if (WorkerThreadDebugger* debugger = WorkerThreadDebugger::From(GetIsolate())) debugger->WorkerThreadDestroyed(this);