rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 5 | #include "base/bind.h" |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 6 | #include "base/callback_helpers.h" |
| 7 | #include "base/run_loop.h" |
| 8 | #include "chrome/browser/extensions/chrome_test_extension_loader.h" |
| 9 | #include "chrome/browser/extensions/extension_browsertest.h" |
| 10 | #include "chrome/browser/extensions/extension_service.h" |
| 11 | #include "chrome/browser/extensions/navigation_observer.h" |
| 12 | #include "chrome/test/base/ui_test_utils.h" |
Lukasz Anforowicz | 951d6bc | 2019-06-14 18:29:18 | [diff] [blame] | 13 | #include "content/public/test/no_renderer_crashes_assertion.h" |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 14 | #include "extensions/browser/extension_dialog_auto_confirm.h" |
| 15 | #include "extensions/browser/extension_prefs.h" |
| 16 | #include "extensions/browser/extension_registry.h" |
| 17 | #include "extensions/common/extension.h" |
| 18 | |
| 19 | namespace extensions { |
| 20 | |
| 21 | // Test that visiting an url associated with a disabled extension offers to |
| 22 | // re-enable it. |
| 23 | IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, |
| 24 | PromptToReEnableExtensionsOnNavigation) { |
Lukasz Anforowicz | 951d6bc | 2019-06-14 18:29:18 | [diff] [blame] | 25 | // TODO(lukasza): https://crbug.com/970917: We should not terminate a renderer |
| 26 | // that hosts a disabled extension. Once that is fixed, we should remove |
| 27 | // ScopedAllowRendererCrashes below. |
| 28 | content::ScopedAllowRendererCrashes scoped_allow_renderer_crashes; |
| 29 | |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 30 | NavigationObserver::SetAllowedRepeatedPromptingForTesting(true); |
tzik | 4373d4b | 2018-03-13 04:42:06 | [diff] [blame] | 31 | base::ScopedClosureRunner reset_repeated_prompting(base::BindOnce([]() { |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 32 | NavigationObserver::SetAllowedRepeatedPromptingForTesting(false); |
| 33 | })); |
| 34 | scoped_refptr<const Extension> extension = |
| 35 | ChromeTestExtensionLoader(profile()).LoadExtension( |
| 36 | test_data_dir_.AppendASCII("simple_with_file")); |
| 37 | ASSERT_TRUE(extension); |
| 38 | const std::string kExtensionId = extension->id(); |
| 39 | const GURL kUrl = extension->GetResourceURL("file.html"); |
| 40 | |
| 41 | // We always navigate in a new tab because when we disable the extension, it |
| 42 | // closes all tabs for that extension. If we only opened in the current tab, |
| 43 | // this would result in the only open tab being closed, and the test quitting. |
| 44 | auto navigate_to_url_in_new_tab = [this](const GURL& url) { |
| 45 | ui_test_utils::NavigateToURLWithDisposition( |
| 46 | browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 47 | ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 48 | }; |
| 49 | |
| 50 | ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); |
| 51 | EXPECT_TRUE(registry->enabled_extensions().Contains(kExtensionId)); |
| 52 | |
| 53 | // Disable the extension due to a permissions increase. |
| 54 | extension_service()->DisableExtension( |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 55 | kExtensionId, disable_reason::DISABLE_PERMISSIONS_INCREASE); |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 56 | EXPECT_TRUE(registry->disabled_extensions().Contains(kExtensionId)); |
| 57 | |
| 58 | ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 59 | EXPECT_EQ(disable_reason::DISABLE_PERMISSIONS_INCREASE, |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 60 | prefs->GetDisableReasons(kExtensionId)); |
| 61 | |
| 62 | { |
| 63 | // Visit an associated url and deny the prompt. The extension should remain |
| 64 | // disabled. |
| 65 | ScopedTestDialogAutoConfirm auto_deny(ScopedTestDialogAutoConfirm::CANCEL); |
| 66 | navigate_to_url_in_new_tab(kUrl); |
| 67 | base::RunLoop().RunUntilIdle(); |
| 68 | EXPECT_TRUE(registry->disabled_extensions().Contains(kExtensionId)); |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 69 | EXPECT_EQ(disable_reason::DISABLE_PERMISSIONS_INCREASE, |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 70 | prefs->GetDisableReasons(kExtensionId)); |
| 71 | } |
| 72 | |
| 73 | { |
| 74 | // Visit an associated url and accept the prompt. The extension should get |
| 75 | // re-enabled. |
| 76 | ScopedTestDialogAutoConfirm auto_accept( |
| 77 | ScopedTestDialogAutoConfirm::ACCEPT); |
| 78 | navigate_to_url_in_new_tab(kUrl); |
| 79 | base::RunLoop().RunUntilIdle(); |
| 80 | EXPECT_TRUE(registry->enabled_extensions().Contains(kExtensionId)); |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 81 | EXPECT_EQ(disable_reason::DISABLE_NONE, |
| 82 | prefs->GetDisableReasons(kExtensionId)); |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Disable the extension for something other than a permissions increase. |
| 86 | extension_service()->DisableExtension(kExtensionId, |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 87 | disable_reason::DISABLE_USER_ACTION); |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 88 | EXPECT_TRUE(registry->disabled_extensions().Contains(kExtensionId)); |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 89 | EXPECT_EQ(disable_reason::DISABLE_USER_ACTION, |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 90 | prefs->GetDisableReasons(kExtensionId)); |
| 91 | |
| 92 | { |
| 93 | // We only prompt for permissions increases, not any other disable reason. |
| 94 | // As such, the extension should stay disabled. |
| 95 | ScopedTestDialogAutoConfirm auto_accept( |
| 96 | ScopedTestDialogAutoConfirm::ACCEPT); |
| 97 | navigate_to_url_in_new_tab(kUrl); |
| 98 | base::RunLoop().RunUntilIdle(); |
| 99 | EXPECT_TRUE(registry->disabled_extensions().Contains(kExtensionId)); |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 100 | EXPECT_EQ(disable_reason::DISABLE_USER_ACTION, |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 101 | prefs->GetDisableReasons(kExtensionId)); |
| 102 | } |
| 103 | |
| 104 | // Load a hosted app and disable it for a permissions increase. |
| 105 | scoped_refptr<const Extension> hosted_app = |
| 106 | ChromeTestExtensionLoader(profile()).LoadExtension( |
| 107 | test_data_dir_.AppendASCII("hosted_app")); |
| 108 | ASSERT_TRUE(hosted_app); |
| 109 | const std::string kHostedAppId = hosted_app->id(); |
| 110 | const GURL kHostedAppUrl("http://localhost/extensions/hosted_app/main.html"); |
| 111 | EXPECT_EQ(hosted_app, registry->enabled_extensions().GetExtensionOrAppByURL( |
| 112 | kHostedAppUrl)); |
| 113 | |
| 114 | extension_service()->DisableExtension( |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 115 | kHostedAppId, disable_reason::DISABLE_PERMISSIONS_INCREASE); |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 116 | EXPECT_TRUE(registry->disabled_extensions().Contains(kHostedAppId)); |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 117 | EXPECT_EQ(disable_reason::DISABLE_PERMISSIONS_INCREASE, |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 118 | prefs->GetDisableReasons(kHostedAppId)); |
| 119 | |
| 120 | { |
| 121 | // When visiting a site that's associated with a hosted app, but not a |
| 122 | // chrome-extension url, we don't prompt to re-enable. This is to avoid |
| 123 | // prompting when visiting a regular website like calendar.google.com. |
| 124 | // See crbug.com/678631. |
| 125 | ScopedTestDialogAutoConfirm auto_accept( |
| 126 | ScopedTestDialogAutoConfirm::ACCEPT); |
| 127 | navigate_to_url_in_new_tab(kHostedAppUrl); |
| 128 | base::RunLoop().RunUntilIdle(); |
| 129 | EXPECT_TRUE(registry->disabled_extensions().Contains(kHostedAppId)); |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 130 | EXPECT_EQ(disable_reason::DISABLE_PERMISSIONS_INCREASE, |
rdevlin.cronin | 4b184ca | 2017-01-13 01:35:44 | [diff] [blame] | 131 | prefs->GetDisableReasons(kHostedAppId)); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | } // namespace extensions |