Browse Source

fix: crash on extension unload when script validation finishes (#41701)

https://chromium-review.googlesource.com/c/chromium/src/+/5225796

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <[email protected]>
trop[bot] 1 year ago
parent
commit
d2c14f1c6c
1 changed files with 23 additions and 0 deletions
  1. 23 0
      shell/browser/extensions/api/scripting/scripting_api.cc

+ 23 - 0
shell/browser/extensions/api/scripting/scripting_api.cc

@@ -22,6 +22,7 @@
 #include "extensions/browser/api/scripting/scripting_utils.h"
 #include "extensions/browser/extension_api_frame_id_map.h"
 #include "extensions/browser/extension_file_task_runner.h"
+#include "extensions/browser/extension_registry.h"
 #include "extensions/browser/extension_system.h"
 #include "extensions/browser/extension_user_script_loader.h"
 #include "extensions/browser/extension_util.h"
@@ -1061,6 +1062,17 @@ void ScriptingRegisterContentScriptsFunction::OnContentScriptFilesValidated(
     return;
   }
 
+  // We cannot proceed if the extension is uninstalled or unloaded in the middle
+  // of validating its script files.
+  ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context());
+  if (!extension() ||
+      !registry->enabled_extensions().Contains(extension_id())) {
+    // Note: a Respond() is not needed if the system is shutting down or if the
+    // extension is no longer enabled.
+    Release();  // Matches the `AddRef()` in `Run()`.
+    return;
+  }
+
   auto error = std::move(result.second);
   auto scripts = std::move(result.first);
   ExtensionUserScriptLoader* loader =
@@ -1306,6 +1318,17 @@ void ScriptingUpdateContentScriptsFunction::OnContentScriptFilesValidated(
     return;
   }
 
+  // We cannot proceed if the extension is uninstalled or unloaded in the middle
+  // of validating its script files.
+  ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context());
+  if (!extension() ||
+      !registry->enabled_extensions().Contains(extension_id())) {
+    // Note: a Respond() is not needed if the system is shutting down or if the
+    // extension is no longer enabled.
+    Release();  // Matches the `AddRef()` in `Run()`.
+    return;
+  }
+
   auto error = std::move(result.second);
   auto scripts = std::move(result.first);
   ExtensionUserScriptLoader* loader =