// Copyright 2023 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "chrome/browser/extensions/permissions/site_permissions_helper.h" #include <string_view> #include "base/run_loop.h" #include "chrome/browser/extensions/browsertest_util.h" #include "chrome/browser/extensions/extension_action_runner.h" #include "chrome/browser/extensions/extension_browsertest.h" #include "chrome/browser/extensions/permissions/scripting_permissions_modifier.h" #include "chrome/browser/extensions/tab_helper.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" #include "chrome/test/base/ui_test_utils.h" #include "content/public/browser/navigation_entry.h" #include "content/public/test/browser_test.h" #include "content/public/test/browser_test_utils.h" #include "extensions/browser/permissions_manager.h" #include "extensions/common/manifest_handlers/content_scripts_handler.h" #include "extensions/common/mojom/run_location.mojom-shared.h" #include "extensions/test/extension_test_message_listener.h" #include "net/dns/mock_host_resolver.h" #include "testing/gtest/include/gtest/gtest.h" namespace extensions { namespace { UserSiteAccess; } // namespace class SitePermissionsHelperBrowserTest : public ExtensionBrowserTest { … }; bool SitePermissionsHelperBrowserTest::ContentScriptInjected() { … } bool SitePermissionsHelperBrowserTest::ExtensionWantsToRun() { … } bool SitePermissionsHelperBrowserTest::ReloadPageAndWaitForLoad() { … } bool SitePermissionsHelperBrowserTest::WaitForReloadToFinish() { … } // TODO(crbug.com/40883928): Paramertize these test scenarios (and the setup as // well). This would allow us to concisely describe the multiple state changes // and expected end states without having an individual test case for each or // (as below) have two large tests that rely on previous tests steps creating // state to proceed successfully. // Tests the various states of permission changes that can occur. When changes // occur we automatically accept the reload bubble, confirm the content script // for the extension is running/not running, and that we are still on the same // page after changing permissions. User site access changes are expected to be // immediate. There are many ASSERTS here because each test case is relying on // the previous changes completing in order to properly test its scenario. // Scenarios tested (in order): // // on all sites -> on site // on site -> on-click (refresh needed due to revoking permissions) // on click -> on site (refresh needed due to script wanting to load at start) // on site -> on all sites // on all sites -> on-click (refresh needed due to revoking permissions) IN_PROC_BROWSER_TEST_F(SitePermissionsHelperBrowserTest, UpdateSiteAccess_AcceptReloadBubble) { … } // Tests the various states of permission changes that can occur. When changes // occur we automatically dismiss the reload bubble, confirm the content script // for the extension is running/not running, and that we are still on the same // page after changing permissions. User site access changes are expected to be // immediate, but a reload is expected so we instead simulate reloading via the // "Reload this page" button. There are many ASSERTS here because each test case // is relying on the previous changes completing in order to properly test its // scenario. Scenarios tested (in order): // // on all sites -> on site // on site -> on-click (refresh needed, and done manually, due to revoking // permissions) // on click -> on site (refresh needed, and done manually, due to // script wanting to load at start) // on site -> on all sites on all sites -> on-click (refresh needed, and done // manually due to revoking permissions) IN_PROC_BROWSER_TEST_F( SitePermissionsHelperBrowserTest, UpdateSiteAccess_DismissReloadBubble_ReloadPageManually) { … } // Provides test cases with an extension that executes a script programmatically // on every site it visits. class SitePermissionsHelperExecuteSciptBrowserTest : public SitePermissionsHelperBrowserTest { … }; // Tests that active tab is cleared when we revoke site permissions of an // extension that injects a script programmatically into the page. To fix // crbug.com/1433399. IN_PROC_BROWSER_TEST_F( SitePermissionsHelperExecuteSciptBrowserTest, UpdateSiteAccess_RevokingSitePermission_AlsoClearsActiveTab) { … } // Tests that active tab is cleared when we revoke site permissions after // granting active tab permissions of an extension that injects a script // programmatically into the page. To fix crbug.com/1433399. IN_PROC_BROWSER_TEST_F( SitePermissionsHelperExecuteSciptBrowserTest, UpdateSiteAccess_RevokingSitePermissionAfterGrantTab_AlsoClearsActiveTab) { … } // Tests that active tab is cleared when we renavigate to a site previously // granted one-time tab permission after a cross-origin navigation. Regression // rest for b/324455951. IN_PROC_BROWSER_TEST_F(SitePermissionsHelperExecuteSciptBrowserTest, CrossOriginRenavigationClearsGrantedTabPermission) { … } class SitePermissionsHelperContentScriptBrowserTest : public SitePermissionsHelperBrowserTest { … }; // Tests that active tab is cleared when we revoke site permissions of an // extension that injects a content script. To fix crbug.com/1433399. IN_PROC_BROWSER_TEST_F( SitePermissionsHelperContentScriptBrowserTest, UpdateSiteAccess_RevokingSitePermission_AlsoClearsActiveTab) { … } } // namespace extensions