From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Cheng Zhao <zcbenz@gmail.com>
Date: Thu, 20 Sep 2018 17:47:12 -0700
Subject: worker_context_will_destroy.patch


diff --git a/content/public/renderer/content_renderer_client.h b/content/public/renderer/content_renderer_client.h
index cfe87b6c3903db7835c3998a65cf18a2301bbf87..e9046ad91236418942cc98372220bf59590dbdf7 100644
--- a/content/public/renderer/content_renderer_client.h
+++ b/content/public/renderer/content_renderer_client.h
@@ -378,6 +378,11 @@ class CONTENT_EXPORT ContentRendererClient {
   virtual void DidInitializeWorkerContextOnWorkerThread(
       v8::Local<v8::Context> context) {}
 
+  // Notifies that a worker context will be destroyed. This function is called
+  // from the worker thread.
+  virtual void WillDestroyWorkerContextOnWorkerThread(
+      v8::Local<v8::Context> 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 27a164a5afb50ffb7414f5867f000ea77a16dc3f..167e697b6143ec8b8a3edf4db395d3205808dd54 100644
--- a/content/renderer/renderer_blink_platform_impl.cc
+++ b/content/renderer/renderer_blink_platform_impl.cc
@@ -1046,6 +1046,12 @@ void RendererBlinkPlatformImpl::WillStopWorkerThread() {
   WorkerThreadRegistry::Instance()->WillStopCurrentWorkerThread();
 }
 
+void RendererBlinkPlatformImpl::WorkerContextWillDestroy(
+    const v8::Local<v8::Context>& worker) {
+  GetContentClient()->renderer()->WillDestroyWorkerContextOnWorkerThread(
+      worker);
+}
+
 void RendererBlinkPlatformImpl::WorkerContextCreated(
     const v8::Local<v8::Context>& worker) {
   GetContentClient()->renderer()->DidInitializeWorkerContextOnWorkerThread(
diff --git a/content/renderer/renderer_blink_platform_impl.h b/content/renderer/renderer_blink_platform_impl.h
index a65c2138b708f3527b8174c97c5e8754a6a759e0..89f6ee990a913828194a245c380a60d5dea91ad5 100644
--- a/content/renderer/renderer_blink_platform_impl.h
+++ b/content/renderer/renderer_blink_platform_impl.h
@@ -204,6 +204,7 @@ class CONTENT_EXPORT RendererBlinkPlatformImpl : public BlinkPlatformImpl {
   void DidStartWorkerThread() override;
   void WillStopWorkerThread() override;
   void WorkerContextCreated(const v8::Local<v8::Context>& worker) override;
+  void WorkerContextWillDestroy(const v8::Local<v8::Context>& worker) override;
 
   // Disables the WebSandboxSupport implementation for testing.
   // Tests that do not set up a full sandbox environment should call
diff --git a/third_party/blink/public/platform/platform.h b/third_party/blink/public/platform/platform.h
index a6d7bc8dfe52ce17926d018467371579101a687f..a6571f68f8bce81f4cf6b7b9ec6247cea7f6a9ee 100644
--- a/third_party/blink/public/platform/platform.h
+++ b/third_party/blink/public/platform/platform.h
@@ -685,6 +685,7 @@ class BLINK_PLATFORM_EXPORT Platform {
   virtual void DidStartWorkerThread() {}
   virtual void WillStopWorkerThread() {}
   virtual void WorkerContextCreated(const v8::Local<v8::Context>& worker) {}
+  virtual void WorkerContextWillDestroy(const v8::Local<v8::Context>& worker) {}
   virtual bool AllowScriptExtensionForServiceWorker(const WebURL& script_url) {
     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 d4549efdf247d50750ae168db6549fbab0bbc5bf..35c8ce0a141f68fc7011942120272805e69d15eb 100644
--- a/third_party/blink/renderer/core/workers/worker_thread.cc
+++ b/third_party/blink/renderer/core/workers/worker_thread.cc
@@ -544,6 +544,12 @@ void WorkerThread::PrepareForShutdownOnWorkerThread() {
       SetExitCode(ExitCode::kGracefullyTerminated);
   }
 
+  {
+    v8::HandleScope handle_scope(GetIsolate());
+    Platform::Current()->WorkerContextWillDestroy(
+        GlobalScope()->ScriptController()->GetContext());
+  }
+
   if (WorkerThreadDebugger* debugger = WorkerThreadDebugger::From(GetIsolate()))
     debugger->WorkerThreadDestroyed(this);
 
