| // Copyright 2026 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| // This file tests the chrome.runtime extension API. | |
| #include "extensions/browser/api/runtime/runtime_api.h" | |
| #include <algorithm> | |
| #include <memory> | |
| #include <optional> | |
| #include <string> | |
| #include <utility> | |
| #include <vector> | |
| #include "base/functional/bind.h" | |
| #include "base/location.h" | |
| #include "base/memory/raw_ptr.h" | |
| #include "base/memory/scoped_refptr.h" | |
| #include "base/run_loop.h" | |
| #include "base/task/single_thread_task_runner.h" | |
| #include "base/test/scoped_feature_list.h" | |
| #include "base/values.h" | |
| #include "base/version.h" | |
| #include "content/public/common/child_process_id.h" | |
| #include "extensions/browser/api_unittest.h" | |
| #include "extensions/browser/event_listener_map.h" | |
| #include "extensions/browser/event_router.h" | |
| #include "extensions/browser/event_router_factory.h" | |
| #include "extensions/browser/events/lazy_event_dispatch_util.h" | |
| #include "extensions/browser/extension_function.h" | |
| #include "extensions/browser/extension_pref_names.h" | |
| #include "extensions/browser/extension_prefs.h" | |
| #include "extensions/browser/extension_registry.h" | |
| #include "extensions/browser/extension_registry_observer.h" | |
| #include "extensions/browser/service_worker/service_worker_task_queue.h" | |
| #include "extensions/browser/service_worker/service_worker_test_utils.h" | |
| #include "extensions/browser/service_worker/worker_id.h" | |
| #include "extensions/browser/test_event_router_observer.h" | |
| #include "extensions/browser/test_extensions_browser_client.h" | |
| #include "extensions/browser/unloaded_extension_reason.h" | |
| #include "extensions/common/api/runtime.h" | |
| #include "extensions/common/extension.h" | |
| #include "extensions/common/extension_builder.h" | |
| #include "extensions/common/extension_features.h" | |
| #include "extensions/common/manifest_handlers/background_info.h" | |
| #include "testing/gmock/include/gmock/gmock.h" | |
| #include "testing/gtest/include/gtest/gtest.h" | |
| #include "url/gurl.h" | |
| namespace extensions { | |
| class ExtensionRuntimeTest : public ApiUnitTest { | |
| protected: | |
| using ApiUnitTest::RunFunction; | |
| // Call runtime.setUninstallURL() and verify that the call succeeds and | |
| // the pref store is updated. | |
| void SetUninstallURL(const std::string& url) { | |
| RunFunction(base::MakeRefCounted<RuntimeSetUninstallURLFunction>(), | |
| "[\"" + url + "\"]"); | |
| // Verify that the URL was properly written to the pref store. | |
| EXPECT_EQ(url, GetUninstallURL()); | |
| } | |
| // Call runtime.setUninstallURL() and verify that the call throws an error and | |
| // the pref store is not affected. | |
| void SetUninstallURLError(const std::string& url) { | |
| std::string original_url = GetUninstallURL(); | |
| EXPECT_EQ("Invalid URL: \"" + url + "\".", | |
| RunFunctionAndReturnError( | |
| base::MakeRefCounted<RuntimeSetUninstallURLFunction>(), | |
| "[\"" + url + "\"]")); | |
| // Verify that the pref store was not affected. | |
| EXPECT_EQ(original_url, GetUninstallURL()); | |
| } | |
| std::string GetUninstallURL() { | |
| std::string url; | |
| ExtensionPrefs::Get(browser_context()) | |
| ->ReadPrefAsString(extension()->id(), "uninstall_url", &url); | |
| return url; | |
| } | |
| }; | |
| TEST_F(ExtensionRuntimeTest, SetUninstallURL) { | |
| // By default extensions should have no uninstall URLs. | |
| EXPECT_EQ("", GetUninstallURL()); | |
| SetUninstallURL("https://example.com"); | |
| // Empty URL string is accepted (to remove uninstall URL). | |
| SetUninstallURL(""); | |
| // URL parameters are accepted. | |
| SetUninstallURL("https://example.com/abcd?param=efg"); | |
| // Trailing spaces are accepted. | |
| SetUninstallURL("https://other.example.com/page "); | |
| // Leading spaces are accepted. | |
| SetUninstallURL(" https://other.example.com/some_page"); | |
| // HTTP URLs are accepted. | |
| SetUninstallURL("http://insecure.example/path"); | |
| // Ensure that only HTTP and HTTPS resources are accepted. | |
| SetUninstallURLError("ws://impossible"); | |
| SetUninstallURLError("wss://impossible"); | |
| SetUninstallURLError("about:blank"); | |
| SetUninstallURLError("chrome://settings"); | |
| SetUninstallURLError("://example.com"); | |
| } | |
| // Unit tests for runtime.markListenerRegistrationComplete() (API availability | |
| // and main-thread errors). Browser tests cover service worker success paths. | |
| class RuntimeMarkListenerRegistrationCompleteTest : public ApiUnitTest { | |
| public: | |
| void SetUp() override { | |
| ApiUnitTest::SetUp(); | |
| set_extension(BuildExtension(/*opted_in=*/true)); | |
| } | |
| protected: | |
| // Builds an MV3 service worker extension. `opted_in` sets | |
| // `background.async_listener_registration`. | |
| scoped_refptr<const Extension> BuildExtension(bool opted_in) { | |
| ExtensionBuilder builder("async listener registration"); | |
| builder.SetManifestVersion(3).SetBackgroundContext( | |
| ExtensionBuilder::BackgroundContext::SERVICE_WORKER); | |
| if (opted_in) { | |
| builder.SetManifestPath("background.async_listener_registration", true); | |
| } | |
| return builder.Build(); | |
| } | |
| // Simulates calling the function from an untracked service worker. | |
| scoped_refptr<ExtensionFunction> CreateFunctionFromWorker() { | |
| auto function = | |
| base::MakeRefCounted<RuntimeMarkListenerRegistrationCompleteFunction>(); | |
| function->set_worker_id( | |
| WorkerId(extension()->id(), content::ChildProcessId::FromUnsafeValue(1), | |
| /*version_id=*/1, /*thread_id=*/1)); | |
| return function; | |
| } | |
| private: | |
| base::test::ScopedFeatureList feature_list_{ | |
| extensions_features::kExtensionAsyncListenerRegistration}; | |
| }; | |
| // The function fails when not called from a service worker. | |
| TEST_F(RuntimeMarkListenerRegistrationCompleteTest, NotFromServiceWorker) { | |
| EXPECT_THAT(RunFunctionAndReturnError( | |
| base::MakeRefCounted< | |
| RuntimeMarkListenerRegistrationCompleteFunction>(), | |
| "[]"), | |
| testing::HasSubstr("service worker")); | |
| } | |
| // The function fails for an extension that does not declare the | |
| // "background.async_listener_registration" manifest key. | |
| TEST_F(RuntimeMarkListenerRegistrationCompleteTest, NotOptedIn) { | |
| set_extension(BuildExtension(/*opted_in=*/false)); | |
| EXPECT_THAT(RunFunctionAndReturnError(CreateFunctionFromWorker(), "[]"), | |
| testing::HasSubstr("background.async_listener_registration")); | |
| } | |
| // A completion from an untracked worker fails, with or without a current | |
| // activation. | |
| TEST_F(RuntimeMarkListenerRegistrationCompleteTest, UntrackedWorkerFails) { | |
| // No current activation. | |
| EXPECT_THAT(RunFunctionAndReturnError(CreateFunctionFromWorker(), "[]"), | |
| testing::HasSubstr("No listener registration is in progress")); | |
| // A current activation, but no tracked worker. | |
| ExtensionRegistry::Get(browser_context())->AddEnabled(extension()); | |
| ServiceWorkerTaskQueue::Get(browser_context()) | |
| ->ActivateExtension(extension()); | |
| EXPECT_THAT(RunFunctionAndReturnError(CreateFunctionFromWorker(), "[]"), | |
| testing::HasSubstr("No listener registration is in progress")); | |
| } | |
| // Test fixture for `runtime.onExtensionLoaded` event dispatching logic, | |
| // parameterized over all `api::runtime::OnLoadedReason` values. | |
| class RuntimeOnExtensionLoadedEventTest | |
| : public ApiUnitTest, | |
| public testing::WithParamInterface<api::runtime::OnLoadedReason> { | |
| public: | |
| void SetUp() override { | |
| ApiUnitTest::SetUp(); | |
| // Ensure `EventRouter` is created for `browser_context()`. | |
| EventRouterFactory::GetInstance()->SetTestingFactory( | |
| browser_context(), | |
| base::BindRepeating([](content::BrowserContext* context) | |
| -> std::unique_ptr<KeyedService> { | |
| return std::make_unique<EventRouter>(context, | |
| ExtensionPrefs::Get(context)); | |
| })); | |
| // Register the extension as enabled in | |
| // `ExtensionRegistry::enabled_extensions()`. | |
| ExtensionRegistry::Get(browser_context())->AddEnabled(extension()); | |
| } | |
| protected: | |
| // Adds a lazy event listener for `runtime.onExtensionLoaded` to | |
| // `EventRouter`. | |
| void AddOnExtensionLoadedListener() { | |
| auto listener = EventListener::CreateLazyListener( | |
| api::runtime::OnExtensionLoaded::kEventName, extension()->id(), | |
| browser_context(), /*is_for_service_worker=*/false, | |
| /*service_worker_scope=*/GURL(), /*filter=*/std::nullopt); | |
| EventRouter::Get(browser_context()) | |
| ->listeners() | |
| .AddListener(std::move(listener)); | |
| } | |
| }; | |
| // Verifies that `details.previous_version` is populated when the load reason is | |
| // `kUpdate` or `kEnable` and omitted for all other `OnLoadedReason` values, | |
| // even when a `previous_version` string is supplied. | |
| TEST_P(RuntimeOnExtensionLoadedEventTest, PreviousVersionHandling) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| AddOnExtensionLoadedListener(); | |
| const api::runtime::OnLoadedReason reason = GetParam(); | |
| // Dispatch `runtime.onExtensionLoaded` with `reason` and a `previous_version` | |
| // string. | |
| RuntimeEventRouter::DispatchOnExtensionLoadedEvent( | |
| browser_context(), extension()->id(), reason, | |
| /*previous_version=*/"1.0", /*wait_for_lazy_context=*/false); | |
| // Verify the dispatched event payload. | |
| ASSERT_TRUE( | |
| observer.events().contains(api::runtime::OnExtensionLoaded::kEventName)); | |
| const Event* dispatched_event = | |
| observer.events().at(api::runtime::OnExtensionLoaded::kEventName).get(); | |
| ASSERT_TRUE(dispatched_event); | |
| ASSERT_EQ(1u, dispatched_event->args().size()); | |
| ASSERT_TRUE(dispatched_event->args()[0].is_dict()); | |
| const base::DictValue& details_dict = dispatched_event->args()[0].GetDict(); | |
| EXPECT_EQ(api::runtime::ToString(reason), *details_dict.FindString("reason")); | |
| if (reason == api::runtime::OnLoadedReason::kUpdate || | |
| reason == api::runtime::OnLoadedReason::kEnable) { | |
| EXPECT_EQ("1.0", *details_dict.FindString("previousVersion")); | |
| } else { | |
| EXPECT_EQ(nullptr, details_dict.FindString("previousVersion")); | |
| } | |
| } | |
| // Verifies that `runtime.onExtensionLoaded` is not dispatched for any | |
| // `OnLoadedReason` when the extension has no registered listener, avoiding | |
| // waking up dormant service workers or creating unnecessary external requests, | |
| // and is dispatched once a listener is registered. | |
| TEST_P(RuntimeOnExtensionLoadedEventTest, | |
| EventListenerFilteringAvoidsWakingDormantWorkers) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| const api::runtime::OnLoadedReason reason = GetParam(); | |
| const std::optional<std::string> previous_version = | |
| (reason == api::runtime::OnLoadedReason::kUpdate || | |
| reason == api::runtime::OnLoadedReason::kEnable) | |
| ? std::make_optional<std::string>("1.0") | |
| : std::nullopt; | |
| // With no listener registered, verify early return without dispatching. | |
| RuntimeEventRouter::DispatchOnExtensionLoadedEvent( | |
| browser_context(), extension()->id(), reason, previous_version, | |
| /*wait_for_lazy_context=*/false); | |
| EXPECT_FALSE( | |
| observer.events().contains(api::runtime::OnExtensionLoaded::kEventName)); | |
| // Register a listener and verify that the event now dispatches. | |
| AddOnExtensionLoadedListener(); | |
| RuntimeEventRouter::DispatchOnExtensionLoadedEvent( | |
| browser_context(), extension()->id(), reason, previous_version, | |
| /*wait_for_lazy_context=*/false); | |
| EXPECT_TRUE( | |
| observer.events().contains(api::runtime::OnExtensionLoaded::kEventName)); | |
| } | |
| // Verifies that `runtime.onExtensionLoaded` is not dispatched if the extension | |
| // is not present in `ExtensionRegistry::enabled_extensions()`. | |
| TEST_P(RuntimeOnExtensionLoadedEventTest, | |
| DispatchIgnoredForDisabledOrMissingExtension) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| AddOnExtensionLoadedListener(); | |
| const api::runtime::OnLoadedReason reason = GetParam(); | |
| const std::optional<std::string> previous_version = | |
| (reason == api::runtime::OnLoadedReason::kUpdate || | |
| reason == api::runtime::OnLoadedReason::kEnable) | |
| ? std::make_optional<std::string>("1.0") | |
| : std::nullopt; | |
| // Remove the extension from `ExtensionRegistry::enabled_extensions()`. | |
| ExtensionRegistry::Get(browser_context())->RemoveEnabled(extension()->id()); | |
| // Attempt to dispatch for `reason`. | |
| RuntimeEventRouter::DispatchOnExtensionLoadedEvent( | |
| browser_context(), extension()->id(), reason, previous_version, | |
| /*wait_for_lazy_context=*/false); | |
| // Verify that the event was not dispatched because the extension is not | |
| // enabled. | |
| EXPECT_FALSE( | |
| observer.events().contains(api::runtime::OnExtensionLoaded::kEventName)); | |
| } | |
| INSTANTIATE_TEST_SUITE_P( | |
| All, | |
| RuntimeOnExtensionLoadedEventTest, | |
| testing::Values(api::runtime::OnLoadedReason::kInstall, | |
| api::runtime::OnLoadedReason::kUpdate, | |
| api::runtime::OnLoadedReason::kBrowserUpdate, | |
| api::runtime::OnLoadedReason::kEnable, | |
| api::runtime::OnLoadedReason::kStartup, | |
| api::runtime::OnLoadedReason::kReload), | |
| [](const testing::TestParamInfo<api::runtime::OnLoadedReason>& info) { | |
| return std::string(api::runtime::ToString(info.param)); | |
| }); | |
| // Test fixture that sends `RuntimeAPI` the `LazyEventDispatchUtil::Observer` | |
| // and `ExtensionRegistryObserver` notifications for an install, update, unload, | |
| // enable or reload, to verify which `runtime.onExtensionLoaded` reason | |
| // `RuntimeAPI` dispatches when more than one of them applies (see | |
| // `RuntimeEventRouter::DispatchOnExtensionLoadedEvent()` in `runtime_api.h` | |
| // for the precedence of reasons). | |
| class RuntimeAPIOnExtensionLoadedPrecedenceTest : public ApiUnitTest { | |
| public: | |
| void SetUp() override { | |
| ApiUnitTest::SetUp(); | |
| // Ensure `EventRouter` is created for `browser_context()`. | |
| EventRouterFactory::GetInstance()->SetTestingFactory( | |
| browser_context(), | |
| base::BindRepeating([](content::BrowserContext* context) | |
| -> std::unique_ptr<KeyedService> { | |
| return std::make_unique<EventRouter>(context, | |
| ExtensionPrefs::Get(context)); | |
| })); | |
| // Create `RuntimeAPI`. `RuntimeAPI::kServiceIsNULLWhileTesting` stops its | |
| // factory from creating it in tests, so use a testing factory. | |
| runtime_api_ = | |
| RuntimeAPI::GetFactoryInstance()->SetTestingSubclassFactoryAndUse( | |
| browser_context(), | |
| base::BindOnce([](content::BrowserContext* context) { | |
| return std::make_unique<RuntimeAPI>(context); | |
| })); | |
| ASSERT_TRUE(runtime_api_); | |
| // `RunPostedDispatchTasks()` relies on each `runtime.onExtensionLoaded` | |
| // dispatch reaching `EventRouter` synchronously from its posted task. That | |
| // holds only if the extension has no lazy background context, since | |
| // `RuntimeEventRouter` otherwise queues the dispatches that callers send | |
| // with `wait_for_lazy_context` set on the `LazyContextTaskQueue`. | |
| ASSERT_FALSE(BackgroundInfo::HasLazyContext(extension())); | |
| // Register the extension as enabled and add a lazy listener for | |
| // `runtime.onExtensionLoaded`, so that the dispatched events reach | |
| // `EventRouter`. | |
| ExtensionRegistry::Get(browser_context())->AddEnabled(extension()); | |
| EventRouter::Get(browser_context()) | |
| ->listeners() | |
| .AddListener(EventListener::CreateLazyListener( | |
| api::runtime::OnExtensionLoaded::kEventName, extension()->id(), | |
| browser_context(), /*is_for_service_worker=*/false, | |
| /*service_worker_scope=*/GURL(), /*filter=*/std::nullopt)); | |
| } | |
| void TearDown() override { | |
| runtime_api_ = nullptr; | |
| ApiUnitTest::TearDown(); | |
| } | |
| protected: | |
| // Sends `RuntimeAPI` the notification that `LazyEventDispatchUtil` sends when | |
| // the extension loads after it was installed or updated. `previous_version` | |
| // is invalid for a fresh install. | |
| void NotifyInstalledAndLoaded(const base::Version& previous_version) { | |
| LazyEventDispatchUtil::Observer* observer = runtime_api_; | |
| observer->OnExtensionInstalledAndLoaded(browser_context(), extension(), | |
| previous_version); | |
| } | |
| // Sends `RuntimeAPI` the notification that `ExtensionRegistry` sends when the | |
| // extension is loaded. The fixture never signals `ExtensionSystem::ready()`, | |
| // so `RuntimeAPI` treats this as a load during browser startup. | |
| void NotifyLoaded() { | |
| ExtensionRegistryObserver* observer = runtime_api_; | |
| observer->OnExtensionLoaded(browser_context(), extension()); | |
| } | |
| // Sends `RuntimeAPI` the notification that `ExtensionRegistry` sends when the | |
| // extension is unloaded because it was disabled. | |
| void NotifyUnloaded() { | |
| ExtensionRegistryObserver* observer = runtime_api_; | |
| observer->OnExtensionUnloaded(browser_context(), extension(), | |
| UnloadedExtensionReason::DISABLE); | |
| } | |
| // Sends `RuntimeAPI` the notification that `ExtensionRegistry` sends when the | |
| // extension is enabled. | |
| void NotifyEnabled() { | |
| ExtensionRegistryObserver* observer = runtime_api_; | |
| observer->OnExtensionEnabled(browser_context(), extension()); | |
| } | |
| // Sends `RuntimeAPI` the notification that `ExtensionRegistry` sends when the | |
| // extension is reloaded. | |
| void NotifyReloaded() { | |
| ExtensionRegistryObserver* observer = runtime_api_; | |
| observer->OnExtensionReloaded(browser_context(), extension()); | |
| } | |
| // Runs, in order, the tasks that the `Notify*()` calls posted to the current | |
| // thread, and returns once they have all run. `RuntimeAPI` posts each | |
| // `runtime.onExtensionLoaded` dispatch as one task to | |
| // `base::SingleThreadTaskRunner::GetCurrentDefault()`, and each of those | |
| // tasks dispatches to `EventRouter` synchronously. Posting the quit closure | |
| // after them means that once this returns: | |
| // - Every event that the notifications dispatched has been recorded by | |
| // `TestEventRouterObserver`. | |
| // - Any event missing from `TestEventRouterObserver` was not dispatched, so | |
| // the negative expectations don't depend on a timeout. | |
| void RunPostedDispatchTasks() { | |
| base::RunLoop run_loop; | |
| base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask( | |
| FROM_HERE, run_loop.QuitClosure()); | |
| run_loop.Run(); | |
| } | |
| // Returns the `details.reason` and `details.previousVersion` (or an empty | |
| // string if it's absent) of each `runtime.onExtensionLoaded` event that | |
| // `observer` saw, in dispatch order. | |
| static std::vector<std::pair<std::string, std::string>> | |
| GetOnExtensionLoadedDetails(const TestEventRouterObserver& observer) { | |
| std::vector<std::pair<std::string, std::string>> details; | |
| for (const std::unique_ptr<Event>& event : observer.all_events()) { | |
| if (event->event_name != api::runtime::OnExtensionLoaded::kEventName) { | |
| continue; | |
| } | |
| const base::DictValue& details_dict = event->args()[0].GetDict(); | |
| const std::string* previous_version = | |
| details_dict.FindString("previousVersion"); | |
| details.emplace_back(*details_dict.FindString("reason"), | |
| previous_version ? *previous_version : ""); | |
| } | |
| return details; | |
| } | |
| // Returns how many `runtime.onEnabled` events `observer` saw. | |
| static int CountOnEnabledEvents(const TestEventRouterObserver& observer) { | |
| return std::ranges::count_if( | |
| observer.all_events(), [](const std::unique_ptr<Event>& event) { | |
| return event->event_name == api::runtime::OnEnabled::kEventName; | |
| }); | |
| } | |
| private: | |
| raw_ptr<RuntimeAPI> runtime_api_ = nullptr; | |
| }; | |
| // Verifies that when an extension that was installed while disabled is | |
| // enabled: | |
| // - `runtime.onExtensionLoaded` is dispatched only with reason `kInstall`, | |
| // since `kInstall` takes precedence over `kEnable`. | |
| // - `runtime.onEnabled` is dispatched exactly once. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceTest, | |
| EnableAfterInstallDispatchesOnlyInstall) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Send the notifications in the order that | |
| // `ExtensionRegistrar::EnableExtension()` triggers them for an extension that | |
| // was installed while disabled, then let the posted dispatches run. | |
| NotifyInstalledAndLoaded(/*previous_version=*/base::Version()); | |
| NotifyEnabled(); | |
| RunPostedDispatchTasks(); | |
| // Verify that only `kInstall` was dispatched. | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre(testing::Pair("install", ""))); | |
| // Verify that `runtime.onEnabled` was dispatched once. | |
| EXPECT_EQ(1, CountOnEnabledEvents(observer)); | |
| } | |
| // Verifies that when an extension that was updated while disabled is enabled: | |
| // - `runtime.onExtensionLoaded` is dispatched only with reason `kUpdate` and | |
| // the version from before the update, since `kUpdate` takes precedence over | |
| // `kEnable`. | |
| // - `runtime.onEnabled` is dispatched exactly once. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceTest, | |
| EnableAfterUpdateDispatchesOnlyUpdate) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Send the notifications in the order that | |
| // `ExtensionRegistrar::EnableExtension()` triggers them for an extension that | |
| // was updated while disabled, then let the posted dispatches run. | |
| NotifyInstalledAndLoaded(/*previous_version=*/base::Version("0.0.1")); | |
| NotifyEnabled(); | |
| RunPostedDispatchTasks(); | |
| // Verify that only `kUpdate` was dispatched, with the pre-update version. | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre(testing::Pair("update", "0.0.1"))); | |
| // Verify that `runtime.onEnabled` was dispatched once. | |
| EXPECT_EQ(1, CountOnEnabledEvents(observer)); | |
| } | |
| // Verifies that if an extension is installed and then unloaded and enabled | |
| // again before the posted `kInstall` dispatch runs: | |
| // - `RuntimeAPI::OnExtensionUnloaded()` drops the pending `kInstall`. | |
| // - `runtime.onExtensionLoaded` is dispatched only with reason `kEnable` and | |
| // the extension's current version. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceTest, | |
| EnableAfterInstallThenUnloadDispatchesOnlyEnable) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Install the extension, then unload and enable it before any posted task | |
| // runs, then let the posted dispatches run. | |
| NotifyInstalledAndLoaded(/*previous_version=*/base::Version()); | |
| NotifyUnloaded(); | |
| NotifyEnabled(); | |
| RunPostedDispatchTasks(); | |
| // Verify that only `kEnable` was dispatched, with the current version. | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre( | |
| testing::Pair("enable", extension()->version().GetString()))); | |
| } | |
| // Verifies that if an extension is updated and then unloaded and enabled | |
| // again before the posted `kUpdate` dispatch runs: | |
| // - `RuntimeAPI::OnExtensionUnloaded()` drops the pending `kUpdate`. | |
| // - `runtime.onExtensionLoaded` is dispatched only with reason `kEnable` and | |
| // the extension's current version, not the version from before the update. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceTest, | |
| EnableAfterUpdateThenUnloadDispatchesOnlyEnable) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Update the extension, then unload and enable it before any posted task | |
| // runs, then let the posted dispatches run. | |
| NotifyInstalledAndLoaded(/*previous_version=*/base::Version("0.0.1")); | |
| NotifyUnloaded(); | |
| NotifyEnabled(); | |
| RunPostedDispatchTasks(); | |
| // Verify that only `kEnable` was dispatched, with the current version. | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre( | |
| testing::Pair("enable", extension()->version().GetString()))); | |
| } | |
| // Verifies that when an extension is reloaded through the install path, as an | |
| // unpacked extension is (including a terminated one), | |
| // `runtime.onExtensionLoaded` is dispatched only with reason `kUpdate` and not | |
| // also with reason `kReload`, since `kUpdate` takes precedence over `kReload`. | |
| // Regression test for crbug.com/579017501. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceTest, | |
| ReloadAfterUpdateDispatchesOnlyUpdate) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Send the notifications in the order that `ExtensionRegistrar` triggers them | |
| // when it reloads an extension through the install path, then let the posted | |
| // dispatches run. | |
| NotifyInstalledAndLoaded(/*previous_version=*/base::Version("0.0.1")); | |
| NotifyReloaded(); | |
| RunPostedDispatchTasks(); | |
| // Verify that only `kUpdate` was dispatched, with the pre-update version. | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre(testing::Pair("update", "0.0.1"))); | |
| } | |
| // Verifies that when an extension loads during browser startup, | |
| // `runtime.onExtensionLoaded` is dispatched with reason `kStartup`. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceTest, | |
| LoadDuringStartupDispatchesStartup) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Load the extension during startup, then let the posted dispatches run. | |
| NotifyLoaded(); | |
| RunPostedDispatchTasks(); | |
| // Verify that only `kStartup` was dispatched. | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre(testing::Pair("startup", ""))); | |
| } | |
| // Verifies that when an extension is enabled during browser startup: | |
| // - `runtime.onExtensionLoaded` is dispatched only with reason `kEnable` and | |
| // the extension's current version, since `kEnable` takes precedence over | |
| // `kStartup`. | |
| // - `runtime.onEnabled` is dispatched exactly once. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceTest, | |
| EnableDuringStartupDispatchesOnlyEnable) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Send the notifications in the order that | |
| // `ExtensionRegistrar::EnableExtension()` triggers them for an extension that | |
| // is enabled during startup, then let the posted dispatches run. | |
| NotifyLoaded(); | |
| NotifyEnabled(); | |
| RunPostedDispatchTasks(); | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre( | |
| testing::Pair("enable", extension()->version().GetString()))); | |
| EXPECT_EQ(1, CountOnEnabledEvents(observer)); | |
| } | |
| // Verifies that if an extension loads during browser startup and is unloaded | |
| // before the posted `kStartup` dispatch runs, | |
| // `RuntimeAPI::OnExtensionUnloaded()` drops the pending `kStartup`, so no | |
| // `runtime.onExtensionLoaded` is dispatched. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceTest, | |
| LoadThenUnloadDuringStartupDispatchesNothing) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Load the extension during startup and unload it before any posted task | |
| // runs, then let the posted dispatches run. | |
| NotifyLoaded(); | |
| NotifyUnloaded(); | |
| RunPostedDispatchTasks(); | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), testing::IsEmpty()); | |
| } | |
| // Verifies that `runtime.onExtensionLoaded` is dispatched only with reason | |
| // `kUpdate` when an extension loads with a pending update during browser | |
| // startup. `kUpdate` takes precedence over `kStartup`. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceTest, | |
| PendingUpdateThenLoadDuringStartupDispatchesOnlyUpdate) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Notify the update first, then the load. | |
| NotifyInstalledAndLoaded(/*previous_version=*/base::Version("0.0.1")); | |
| NotifyLoaded(); | |
| RunPostedDispatchTasks(); | |
| // Verify that only `kUpdate` was dispatched, with the pre-update version. | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre(testing::Pair("update", "0.0.1"))); | |
| } | |
| // Same as `PendingUpdateThenLoadDuringStartupDispatchesOnlyUpdate`, but | |
| // `RuntimeAPI` gets the load notification before the update notification. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceTest, | |
| LoadThenPendingUpdateDuringStartupDispatchesOnlyUpdate) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Notify the load first, then the update. | |
| NotifyLoaded(); | |
| NotifyInstalledAndLoaded(/*previous_version=*/base::Version("0.0.1")); | |
| RunPostedDispatchTasks(); | |
| // Verify that only `kUpdate` was dispatched, with the pre-update version. | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre(testing::Pair("update", "0.0.1"))); | |
| } | |
| // Verifies that `runtime.onExtensionLoaded` is dispatched only with reason | |
| // `kInstall` when an extension loads with a pending install during browser | |
| // startup. `kInstall` takes precedence over `kStartup`. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceTest, | |
| PendingInstallThenLoadDuringStartupDispatchesOnlyInstall) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Notify the install first, then the load. | |
| NotifyInstalledAndLoaded(/*previous_version=*/base::Version()); | |
| NotifyLoaded(); | |
| RunPostedDispatchTasks(); | |
| // Verify that only `kInstall` was dispatched. | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre(testing::Pair("install", ""))); | |
| } | |
| // Verifies that if an extension is updated while it was disabled, then it was | |
| // later enabled during browser startup: | |
| // - `runtime.onExtensionLoaded` is dispatched only with reason `kUpdate`. | |
| // `kUpdate` takes precedence over `kEnable` and `kStartup`. | |
| // - `runtime.onEnabled` is dispatched exactly once. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceTest, | |
| EnableWithPendingUpdateDuringStartupDispatchesOnlyUpdate) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Send the notifications in the order that | |
| // `ExtensionRegistrar::EnableExtension()` triggers them for an extension that | |
| // was updated while disabled and is enabled during startup. | |
| NotifyInstalledAndLoaded(/*previous_version=*/base::Version("0.0.1")); | |
| NotifyLoaded(); | |
| NotifyEnabled(); | |
| RunPostedDispatchTasks(); | |
| // Verify that only `kUpdate` was dispatched, with the pre-update version. | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre(testing::Pair("update", "0.0.1"))); | |
| // Verify that `runtime.onEnabled` was dispatched once. | |
| EXPECT_EQ(1, CountOnEnabledEvents(observer)); | |
| } | |
| // Test fixture like `RuntimeAPIOnExtensionLoadedPrecedenceTest`, but for a | |
| // service worker extension, to verify when lifecycle events wait for the | |
| // service worker to start. | |
| class RuntimeAPIEnableLazyContextTest | |
| : public RuntimeAPIOnExtensionLoadedPrecedenceTest { | |
| public: | |
| void SetUp() override { | |
| RuntimeAPIOnExtensionLoadedPrecedenceTest::SetUp(); | |
| // Replace the base fixture's extension, which has no lazy background | |
| // context, with an enabled service worker extension. | |
| set_extension(ExtensionBuilder("Service Worker Based Extension") | |
| .SetManifestVersion(3) | |
| .SetBackgroundContext( | |
| ExtensionBuilder::BackgroundContext::SERVICE_WORKER) | |
| .Build()); | |
| ExtensionRegistry::Get(browser_context())->AddEnabled(extension()); | |
| } | |
| protected: | |
| // Records that the extension's service worker has run since the extension | |
| // was installed, as `ServiceWorkerTaskQueue` does after the worker starts. | |
| void SetHasStartedServiceWorker() { | |
| ExtensionPrefs::Get(browser_context()) | |
| ->UpdateExtensionPref(extension()->id(), kPrefHasStartedServiceWorker, | |
| base::Value(true)); | |
| } | |
| // Adds lazy listeners for `runtime.onEnabled` and | |
| // `runtime.onExtensionLoaded`, as the service worker registers them when it | |
| // starts. | |
| void AddEnableEventListeners() { | |
| for (const char* event_name : | |
| {api::runtime::OnEnabled::kEventName, | |
| api::runtime::OnExtensionLoaded::kEventName}) { | |
| EventRouter::Get(browser_context()) | |
| ->listeners() | |
| .AddListener(EventListener::CreateLazyListener( | |
| event_name, extension()->id(), browser_context(), | |
| /*is_for_service_worker=*/false, | |
| /*service_worker_scope=*/GURL(), /*filter=*/std::nullopt)); | |
| } | |
| } | |
| // Returns the `ServiceWorkerTaskQueue` for `browser_context()`. | |
| ServiceWorkerTaskQueue* task_queue() { | |
| return ServiceWorkerTaskQueue::Get(browser_context()); | |
| } | |
| }; | |
| // Verifies that a plain enable of an extension whose service worker has | |
| // already run doesn't wait for the worker: | |
| // - No service worker start is requested. | |
| // - `runtime.onEnabled` reaches `EventRouter` in the task that the enable | |
| // posted. `EventRouter` then drops it, because there's no listener. | |
| // - `runtime.onExtensionLoaded` isn't dispatched, because there's no listener. | |
| // The extension has no enable event listeners on purpose. With listeners, | |
| // `EventRouter` starts the worker to deliver the events even when nothing | |
| // waits, so the worker start count would be 1 whether or not the bug is | |
| // present. Without listeners, only a wait can start the worker, so a wait | |
| // makes the start count 1 and the test fails. | |
| TEST_F(RuntimeAPIEnableLazyContextTest, | |
| PlainEnableDoesNotWakeDormantServiceWorker) { | |
| // Set up a registered but stopped service worker that has already run once. | |
| SetHasStartedServiceWorker(); | |
| task_queue()->SetRegisteredServiceWorkerInfoForTesting( | |
| extension()->id(), extension()->version()); | |
| // Enable the extension, and activate it before the posted dispatches run. | |
| // This test environment has no real worker registration, so the activation | |
| // posts a check that finds none and registers the worker again. Activating | |
| // after the enable lets the dispatches run while the worker still counts as | |
| // registered, so a dispatch that waited would request a worker start. | |
| service_worker_test_utils::TestServiceWorkerTaskQueueObserver worker_observer; | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| NotifyEnabled(); | |
| task_queue()->ActivateExtension(extension()); | |
| RunPostedDispatchTasks(); | |
| EXPECT_EQ(0, | |
| worker_observer.GetRequestedWorkerStartedCount(extension()->id())); | |
| EXPECT_EQ(1, CountOnEnabledEvents(observer)); | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), testing::IsEmpty()); | |
| } | |
| // Verifies that if the service worker is deactivated before it starts, the | |
| // enable events that wait for it are dropped. For example, this happens when | |
| // the extension is disabled again right after it's enabled. | |
| // `ServiceWorkerTaskQueue` then runs their queued tasks without a context. | |
| TEST_F(RuntimeAPIEnableLazyContextTest, | |
| EnableQueuedEventsDroppedIfWorkerDeactivatedBeforeStart) { | |
| // Activate the extension with a service worker that hasn't run since | |
| // install. With no registered worker info, the queued tasks wait for the | |
| // registration. | |
| task_queue()->ActivateExtension(extension()); | |
| // Enable the extension, so that both enable events are queued until the | |
| // worker is ready. | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| NotifyEnabled(); | |
| RunPostedDispatchTasks(); | |
| // Add the listeners that the worker would register, then deactivate the | |
| // extension before the worker starts. | |
| AddEnableEventListeners(); | |
| task_queue()->DeactivateExtension(extension()); | |
| EXPECT_EQ(0, CountOnEnabledEvents(observer)); | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), testing::IsEmpty()); | |
| } | |
| // Verifies that when an extension whose service worker has already run is | |
| // reloaded, `runtime.onExtensionLoaded` with reason `kReload` doesn't wait for | |
| // the worker. It reaches `EventRouter` in the task that the reload posted. | |
| TEST_F(RuntimeAPIEnableLazyContextTest, ReloadWhenWorkerAlreadyRanDoesNotWait) { | |
| // Activate the extension with a service worker that has already run once. | |
| // A dispatch that waited would stay queued until the worker is registered, | |
| // which doesn't happen in this test environment. | |
| SetHasStartedServiceWorker(); | |
| task_queue()->ActivateExtension(extension()); | |
| // Add the listeners, so that a dispatch that doesn't wait reaches | |
| // `EventRouter`. | |
| AddEnableEventListeners(); | |
| // Reload the extension, then let the posted dispatches run. | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| NotifyReloaded(); | |
| RunPostedDispatchTasks(); | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre(testing::Pair("reload", ""))); | |
| } | |
| // A `TestExtensionsBrowserClient` that reports that the browser was updated | |
| // since the last run. | |
| class BrowserUpdateTestExtensionsBrowserClient | |
| : public TestExtensionsBrowserClient { | |
| public: | |
| bool DidVersionUpdate(content::BrowserContext* context) override { | |
| return true; | |
| } | |
| }; | |
| // Test fixture like `RuntimeAPIOnExtensionLoadedPrecedenceTest`, for the first | |
| // browser startup after a browser update. | |
| class RuntimeAPIOnExtensionLoadedPrecedenceAfterBrowserUpdateTest | |
| : public RuntimeAPIOnExtensionLoadedPrecedenceTest { | |
| public: | |
| void SetUp() override { | |
| SetExtensionsBrowserClient( | |
| std::make_unique<BrowserUpdateTestExtensionsBrowserClient>()); | |
| // `RuntimeAPI` checks `DidVersionUpdate()` when `SetUp()` creates it. | |
| RuntimeAPIOnExtensionLoadedPrecedenceTest::SetUp(); | |
| // Add a lazy listener for `runtime.onInstalled`, because | |
| // `RuntimeEventRouter::DispatchOnInstalledEvent()` dispatches the | |
| // `"chrome_update"` reason only to an extension that listens for it. | |
| EventRouter::Get(browser_context()) | |
| ->listeners() | |
| .AddListener(EventListener::CreateLazyListener( | |
| api::runtime::OnInstalled::kEventName, extension()->id(), | |
| browser_context(), /*is_for_service_worker=*/false, | |
| /*service_worker_scope=*/GURL(), /*filter=*/std::nullopt)); | |
| } | |
| protected: | |
| // Returns the `details.reason` of each `runtime.onInstalled` event that | |
| // `observer` saw, in dispatch order. | |
| static std::vector<std::string> GetOnInstalledReasons( | |
| const TestEventRouterObserver& observer) { | |
| std::vector<std::string> reasons; | |
| for (const std::unique_ptr<Event>& event : observer.all_events()) { | |
| if (event->event_name == api::runtime::OnInstalled::kEventName) { | |
| reasons.push_back(*event->args()[0].GetDict().FindString("reason")); | |
| } | |
| } | |
| return reasons; | |
| } | |
| }; | |
| // Verifies that when an extension loads with a pending update during the first | |
| // browser startup after a browser update: | |
| // - `runtime.onExtensionLoaded` is dispatched only with reason `kUpdate`. | |
| // `kUpdate` takes precedence over `kBrowserUpdate`. | |
| // - `runtime.onInstalled` is still dispatched with reasons `"chrome_update"` | |
| // and `"update"`. | |
| TEST_F(RuntimeAPIOnExtensionLoadedPrecedenceAfterBrowserUpdateTest, | |
| PendingUpdateThenLoadDuringBrowserUpdateDispatchesOnlyUpdate) { | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| // Notify the update first, then the load. | |
| NotifyInstalledAndLoaded(/*previous_version=*/base::Version("0.0.1")); | |
| NotifyLoaded(); | |
| RunPostedDispatchTasks(); | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre(testing::Pair("update", "0.0.1"))); | |
| EXPECT_THAT(GetOnInstalledReasons(observer), | |
| testing::UnorderedElementsAre("chrome_update", "update")); | |
| } | |
| // Test fixture like `RuntimeAPIEnableLazyContextTest`, for the first browser | |
| // startup after a browser update, to verify when `runtime.onExtensionLoaded` | |
| // with reason `kBrowserUpdate` waits for the service worker to start. | |
| class RuntimeAPIBrowserUpdateLazyContextTest | |
| : public RuntimeAPIEnableLazyContextTest { | |
| public: | |
| void SetUp() override { | |
| SetExtensionsBrowserClient( | |
| std::make_unique<BrowserUpdateTestExtensionsBrowserClient>()); | |
| // `RuntimeAPI` checks `DidVersionUpdate()` when `SetUp()` creates it. | |
| RuntimeAPIEnableLazyContextTest::SetUp(); | |
| } | |
| }; | |
| // Verifies that when an extension whose service worker hasn't run since the | |
| // last install or update loads during the first browser startup after a | |
| // browser update, `runtime.onExtensionLoaded` with reason `kBrowserUpdate` | |
| // waits for the worker. The install or update cleared the extension's | |
| // persisted listeners, so the event would be dropped if it didn't wait for the | |
| // worker to register them again. | |
| TEST_F(RuntimeAPIBrowserUpdateLazyContextTest, | |
| LoadWhenWorkerHasNotRunWaitsForWorker) { | |
| // Activate the extension. With no registered worker info, a dispatch that | |
| // waits stays queued until the worker is registered, which doesn't happen in | |
| // this test environment. | |
| task_queue()->ActivateExtension(extension()); | |
| // Add the listeners, so that a dispatch that doesn't wait reaches | |
| // `EventRouter`. | |
| AddEnableEventListeners(); | |
| // Load the extension during startup, then let the posted dispatches run. | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| NotifyLoaded(); | |
| RunPostedDispatchTasks(); | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), testing::IsEmpty()); | |
| } | |
| // Verifies that when an extension whose service worker has already run loads | |
| // during the first browser startup after a browser update, | |
| // `runtime.onExtensionLoaded` with reason `kBrowserUpdate` doesn't wait for | |
| // the worker. It reaches `EventRouter` in the task that the load posted. | |
| TEST_F(RuntimeAPIBrowserUpdateLazyContextTest, | |
| LoadWhenWorkerAlreadyRanDoesNotWait) { | |
| // Activate the extension with a service worker that has already run once. | |
| // A dispatch that waited would stay queued until the worker is registered, | |
| // which doesn't happen in this test environment. | |
| SetHasStartedServiceWorker(); | |
| task_queue()->ActivateExtension(extension()); | |
| // Add the listeners, so that a dispatch that doesn't wait reaches | |
| // `EventRouter`. | |
| AddEnableEventListeners(); | |
| // Load the extension during startup, then let the posted dispatches run. | |
| TestEventRouterObserver observer(EventRouter::Get(browser_context())); | |
| NotifyLoaded(); | |
| RunPostedDispatchTasks(); | |
| EXPECT_THAT(GetOnExtensionLoadedDetails(observer), | |
| testing::ElementsAre(testing::Pair("browser_update", ""))); | |
| } | |
| } // namespace extensions |