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