From 313476a72478cbbdd9baad033ad3d8d2d1c864bd Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Tue, 26 Mar 2024 23:50:13 +0100 Subject: [PATCH] fix: crash on extension unload when script validation finishes (#41702) 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 --- .../extensions/api/scripting/scripting_api.cc | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/shell/browser/extensions/api/scripting/scripting_api.cc b/shell/browser/extensions/api/scripting/scripting_api.cc index 069d70d98044..cea869d8b45c 100644 --- a/shell/browser/extensions/api/scripting/scripting_api.cc +++ b/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 =