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 a4695ba832fc5ae6f458baf38bbf7efa95dc610e..357df68c64071ef7dca98ce4aab885dd936b1c49 100644 --- a/content/public/renderer/content_renderer_client.h +++ b/content/public/renderer/content_renderer_client.h @@ -374,6 +374,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 9362fb2b7cb2fa0054c3b8578f1e56c7bfd82627..e7ee8f52f4cf276d301522e00fed6ab9f34e1f56 100644 --- a/content/renderer/renderer_blink_platform_impl.cc +++ b/content/renderer/renderer_blink_platform_impl.cc @@ -910,6 +910,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 aea8d1c53436dbb90352f2de1eebb287815e7e78..534c8f6032124d6b94e35f036158bbe8122f85bb 100644 --- a/content/renderer/renderer_blink_platform_impl.h +++ b/content/renderer/renderer_blink_platform_impl.h @@ -196,6 +196,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 AllowScriptExtensionForServiceWorker( const blink::WebSecurityOrigin& script_origin) override; blink::ProtocolHandlerSecurityLevel GetProtocolHandlerSecurityLevel() diff --git a/third_party/blink/public/platform/platform.h b/third_party/blink/public/platform/platform.h index 563c13288c6664469c0c5ae121b0c7add38a1ddd..96c5370e38f9960aa14d7dcfcc841a427b129aca 100644 --- a/third_party/blink/public/platform/platform.h +++ b/third_party/blink/public/platform/platform.h @@ -697,6 +697,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 cf8326757d4f1ad8acb27ad61c65ab0db61874c3..10292488d8700123edaa6f9834a6f37ae4c169f0 100644 --- a/third_party/blink/renderer/core/workers/worker_thread.cc +++ b/third_party/blink/renderer/core/workers/worker_thread.cc @@ -737,6 +737,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);