| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/macros.h" | 5 #include "base/macros.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "chrome/browser/extensions/extension_install_prompt.h" | 7 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 8 #include "chrome/browser/extensions/extension_reenabler.h" | 8 #include "chrome/browser/extensions/extension_reenabler.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_service_test_base.h" | 10 #include "chrome/browser/extensions/extension_service_test_base.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 74 |
| 75 private: | 75 private: |
| 76 // The callback to run once the reenable process finishes. | 76 // The callback to run once the reenable process finishes. |
| 77 void OnComplete(ExtensionReenabler::ReenableResult result) { | 77 void OnComplete(ExtensionReenabler::ReenableResult result) { |
| 78 result_.reset(new ExtensionReenabler::ReenableResult(result)); | 78 result_.reset(new ExtensionReenabler::ReenableResult(result)); |
| 79 } | 79 } |
| 80 | 80 |
| 81 // The callback to run when a test ExtensionInstallPrompt is ready to show. | 81 // The callback to run when a test ExtensionInstallPrompt is ready to show. |
| 82 void OnShow(ExtensionInstallPromptShowParams* show_params, | 82 void OnShow(ExtensionInstallPromptShowParams* show_params, |
| 83 const ExtensionInstallPrompt::DoneCallback& done_callback, | 83 const ExtensionInstallPrompt::DoneCallback& done_callback, |
| 84 scoped_ptr<ExtensionInstallPrompt::Prompt> prompt) { | 84 std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt) { |
| 85 DCHECK(!quit_closure_.is_null()); | 85 DCHECK(!quit_closure_.is_null()); |
| 86 quit_closure_.Run(); | 86 quit_closure_.Run(); |
| 87 quit_closure_ = base::Closure(); | 87 quit_closure_ = base::Closure(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 // The closure to quit the currently-running loop; used with test | 90 // The closure to quit the currently-running loop; used with test |
| 91 // ExtensionInstallPrompts. | 91 // ExtensionInstallPrompts. |
| 92 base::Closure quit_closure_; | 92 base::Closure quit_closure_; |
| 93 | 93 |
| 94 // The result of the reenable process, or null if the process hasn't finished. | 94 // The result of the reenable process, or null if the process hasn't finished. |
| 95 scoped_ptr<ExtensionReenabler::ReenableResult> result_; | 95 std::unique_ptr<ExtensionReenabler::ReenableResult> result_; |
| 96 | 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(CallbackHelper); | 97 DISALLOW_COPY_AND_ASSIGN(CallbackHelper); |
| 98 }; | 98 }; |
| 99 | 99 |
| 100 } // namespace | 100 } // namespace |
| 101 | 101 |
| 102 class ExtensionReenablerUnitTest : public ExtensionServiceTestBase { | 102 class ExtensionReenablerUnitTest : public ExtensionServiceTestBase { |
| 103 public: | 103 public: |
| 104 ExtensionReenablerUnitTest() {} | 104 ExtensionReenablerUnitTest() {} |
| 105 ~ExtensionReenablerUnitTest() override {} | 105 ~ExtensionReenablerUnitTest() override {} |
| 106 | 106 |
| 107 private: | 107 private: |
| 108 void SetUp() override; | 108 void SetUp() override; |
| 109 void TearDown() override; | 109 void TearDown() override; |
| 110 | 110 |
| 111 scoped_ptr<TestExtensionsBrowserClient> test_browser_client_; | 111 std::unique_ptr<TestExtensionsBrowserClient> test_browser_client_; |
| 112 | 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(ExtensionReenablerUnitTest); | 113 DISALLOW_COPY_AND_ASSIGN(ExtensionReenablerUnitTest); |
| 114 }; | 114 }; |
| 115 | 115 |
| 116 void ExtensionReenablerUnitTest::SetUp() { | 116 void ExtensionReenablerUnitTest::SetUp() { |
| 117 ExtensionServiceTestBase::SetUp(); | 117 ExtensionServiceTestBase::SetUp(); |
| 118 InitializeEmptyExtensionService(); | 118 InitializeEmptyExtensionService(); |
| 119 // We need a TestExtensionsBrowserClient because the real one tries to | 119 // We need a TestExtensionsBrowserClient because the real one tries to |
| 120 // implicitly convert any browser context to a (non-Testing)Profile. | 120 // implicitly convert any browser context to a (non-Testing)Profile. |
| 121 test_browser_client_.reset(new TestExtensionsBrowserClient(profile())); | 121 test_browser_client_.reset(new TestExtensionsBrowserClient(profile())); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 service()->DisableExtension(extension->id(), | 156 service()->DisableExtension(extension->id(), |
| 157 Extension::DISABLE_PERMISSIONS_INCREASE); | 157 Extension::DISABLE_PERMISSIONS_INCREASE); |
| 158 // Sanity check that it's disabled. | 158 // Sanity check that it's disabled. |
| 159 EXPECT_TRUE(registry()->disabled_extensions().Contains(extension->id())); | 159 EXPECT_TRUE(registry()->disabled_extensions().Contains(extension->id())); |
| 160 | 160 |
| 161 // Automatically confirm install prompts. | 161 // Automatically confirm install prompts. |
| 162 ScopedTestDialogAutoConfirm auto_confirm( | 162 ScopedTestDialogAutoConfirm auto_confirm( |
| 163 ScopedTestDialogAutoConfirm::ACCEPT); | 163 ScopedTestDialogAutoConfirm::ACCEPT); |
| 164 | 164 |
| 165 // Run the ExtensionReenabler. | 165 // Run the ExtensionReenabler. |
| 166 scoped_ptr<ExtensionReenabler> extension_reenabler = | 166 std::unique_ptr<ExtensionReenabler> extension_reenabler = |
| 167 ExtensionReenabler::PromptForReenable(extension, | 167 ExtensionReenabler::PromptForReenable(extension, profile(), |
| 168 profile(), | |
| 169 nullptr, // No web contents. | 168 nullptr, // No web contents. |
| 170 GURL(), // No referrer. | 169 GURL(), // No referrer. |
| 171 callback_helper.GetCallback()); | 170 callback_helper.GetCallback()); |
| 172 base::RunLoop().RunUntilIdle(); | 171 base::RunLoop().RunUntilIdle(); |
| 173 | 172 |
| 174 // The extension should be enabled. | 173 // The extension should be enabled. |
| 175 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension->id())); | 174 EXPECT_TRUE(registry()->enabled_extensions().Contains(extension->id())); |
| 176 EXPECT_TRUE( | 175 EXPECT_TRUE( |
| 177 callback_helper.result_matches(ExtensionReenabler::REENABLE_SUCCESS)); | 176 callback_helper.result_matches(ExtensionReenabler::REENABLE_SUCCESS)); |
| 178 } | 177 } |
| 179 | 178 |
| 180 // Check that we don't re-enable extensions that must remain disabled, and | 179 // Check that we don't re-enable extensions that must remain disabled, and |
| 181 // that the re-enabler reports failure correctly. | 180 // that the re-enabler reports failure correctly. |
| 182 { | 181 { |
| 183 ScopedTestDialogAutoConfirm auto_confirm( | 182 ScopedTestDialogAutoConfirm auto_confirm( |
| 184 ScopedTestDialogAutoConfirm::ACCEPT); | 183 ScopedTestDialogAutoConfirm::ACCEPT); |
| 185 | 184 |
| 186 ManagementPolicy* management_policy = | 185 ManagementPolicy* management_policy = |
| 187 ExtensionSystem::Get(browser_context())->management_policy(); | 186 ExtensionSystem::Get(browser_context())->management_policy(); |
| 188 ASSERT_TRUE(management_policy); | 187 ASSERT_TRUE(management_policy); |
| 189 TestManagementProvider test_provider; | 188 TestManagementProvider test_provider; |
| 190 management_policy->RegisterProvider(&test_provider); | 189 management_policy->RegisterProvider(&test_provider); |
| 191 service()->DisableExtension(extension->id(), | 190 service()->DisableExtension(extension->id(), |
| 192 Extension::DISABLE_PERMISSIONS_INCREASE); | 191 Extension::DISABLE_PERMISSIONS_INCREASE); |
| 193 | 192 |
| 194 scoped_ptr<ExtensionReenabler> extension_reenabler = | 193 std::unique_ptr<ExtensionReenabler> extension_reenabler = |
| 195 ExtensionReenabler::PromptForReenable(extension, | 194 ExtensionReenabler::PromptForReenable(extension, profile(), |
| 196 profile(), | |
| 197 nullptr, // No web contents. | 195 nullptr, // No web contents. |
| 198 GURL(), // No referrer. | 196 GURL(), // No referrer. |
| 199 callback_helper.GetCallback()); | 197 callback_helper.GetCallback()); |
| 200 base::RunLoop().RunUntilIdle(); | 198 base::RunLoop().RunUntilIdle(); |
| 201 | 199 |
| 202 // The extension should be enabled. | 200 // The extension should be enabled. |
| 203 EXPECT_TRUE(registry()->disabled_extensions().Contains(extension->id())); | 201 EXPECT_TRUE(registry()->disabled_extensions().Contains(extension->id())); |
| 204 EXPECT_TRUE( | 202 EXPECT_TRUE( |
| 205 callback_helper.result_matches(ExtensionReenabler::NOT_ALLOWED)); | 203 callback_helper.result_matches(ExtensionReenabler::NOT_ALLOWED)); |
| 206 | 204 |
| 207 management_policy->UnregisterProvider(&test_provider); | 205 management_policy->UnregisterProvider(&test_provider); |
| 208 } | 206 } |
| 209 | 207 |
| 210 // Check that canceling the re-enable prompt doesn't re-enable the extension. | 208 // Check that canceling the re-enable prompt doesn't re-enable the extension. |
| 211 { | 209 { |
| 212 // Disable it again, and try canceling the prompt. | 210 // Disable it again, and try canceling the prompt. |
| 213 service()->DisableExtension(extension->id(), | 211 service()->DisableExtension(extension->id(), |
| 214 Extension::DISABLE_PERMISSIONS_INCREASE); | 212 Extension::DISABLE_PERMISSIONS_INCREASE); |
| 215 ScopedTestDialogAutoConfirm auto_confirm( | 213 ScopedTestDialogAutoConfirm auto_confirm( |
| 216 ScopedTestDialogAutoConfirm::CANCEL); | 214 ScopedTestDialogAutoConfirm::CANCEL); |
| 217 scoped_ptr<ExtensionReenabler> extension_reenabler = | 215 std::unique_ptr<ExtensionReenabler> extension_reenabler = |
| 218 ExtensionReenabler::PromptForReenable(extension, | 216 ExtensionReenabler::PromptForReenable(extension, profile(), |
| 219 profile(), | |
| 220 nullptr, // No web contents. | 217 nullptr, // No web contents. |
| 221 GURL(), // No referrer. | 218 GURL(), // No referrer. |
| 222 callback_helper.GetCallback()); | 219 callback_helper.GetCallback()); |
| 223 base::RunLoop().RunUntilIdle(); | 220 base::RunLoop().RunUntilIdle(); |
| 224 | 221 |
| 225 // The extension should remain disabled. | 222 // The extension should remain disabled. |
| 226 EXPECT_TRUE(registry()->disabled_extensions().Contains(extension->id())); | 223 EXPECT_TRUE(registry()->disabled_extensions().Contains(extension->id())); |
| 227 EXPECT_TRUE( | 224 EXPECT_TRUE( |
| 228 callback_helper.result_matches(ExtensionReenabler::USER_CANCELED)); | 225 callback_helper.result_matches(ExtensionReenabler::USER_CANCELED)); |
| 229 } | 226 } |
| 230 | 227 |
| 231 // Test that if the extension is re-enabled while the prompt is active, the | 228 // Test that if the extension is re-enabled while the prompt is active, the |
| 232 // prompt exits and reports success. | 229 // prompt exits and reports success. |
| 233 { | 230 { |
| 234 base::RunLoop run_loop; | 231 base::RunLoop run_loop; |
| 235 scoped_ptr<ExtensionReenabler> extension_reenabler = | 232 std::unique_ptr<ExtensionReenabler> extension_reenabler = |
| 236 ExtensionReenabler::PromptForReenableWithCallbackForTest( | 233 ExtensionReenabler::PromptForReenableWithCallbackForTest( |
| 237 extension, profile(), callback_helper.GetCallback(), | 234 extension, profile(), callback_helper.GetCallback(), |
| 238 callback_helper.CreateShowCallback(run_loop.QuitClosure())); | 235 callback_helper.CreateShowCallback(run_loop.QuitClosure())); |
| 239 run_loop.Run(); | 236 run_loop.Run(); |
| 240 | 237 |
| 241 // We shouldn't have any result yet (the user hasn't confirmed or canceled). | 238 // We shouldn't have any result yet (the user hasn't confirmed or canceled). |
| 242 EXPECT_FALSE(callback_helper.has_result()); | 239 EXPECT_FALSE(callback_helper.has_result()); |
| 243 | 240 |
| 244 // Reenable the extension. This should count as a success for reenabling. | 241 // Reenable the extension. This should count as a success for reenabling. |
| 245 service()->GrantPermissionsAndEnableExtension(extension.get()); | 242 service()->GrantPermissionsAndEnableExtension(extension.get()); |
| 246 EXPECT_TRUE( | 243 EXPECT_TRUE( |
| 247 callback_helper.result_matches(ExtensionReenabler::REENABLE_SUCCESS)); | 244 callback_helper.result_matches(ExtensionReenabler::REENABLE_SUCCESS)); |
| 248 } | 245 } |
| 249 | 246 |
| 250 // Test that prematurely destroying the re-enable prompt doesn't crash and | 247 // Test that prematurely destroying the re-enable prompt doesn't crash and |
| 251 // reports an "aborted" result. | 248 // reports an "aborted" result. |
| 252 { | 249 { |
| 253 // Disable again, and create another prompt. | 250 // Disable again, and create another prompt. |
| 254 service()->DisableExtension(extension->id(), | 251 service()->DisableExtension(extension->id(), |
| 255 Extension::DISABLE_PERMISSIONS_INCREASE); | 252 Extension::DISABLE_PERMISSIONS_INCREASE); |
| 256 base::RunLoop run_loop; | 253 base::RunLoop run_loop; |
| 257 scoped_ptr<ExtensionReenabler> extension_reenabler = | 254 std::unique_ptr<ExtensionReenabler> extension_reenabler = |
| 258 ExtensionReenabler::PromptForReenableWithCallbackForTest( | 255 ExtensionReenabler::PromptForReenableWithCallbackForTest( |
| 259 extension, profile(), callback_helper.GetCallback(), | 256 extension, profile(), callback_helper.GetCallback(), |
| 260 callback_helper.CreateShowCallback(run_loop.QuitClosure())); | 257 callback_helper.CreateShowCallback(run_loop.QuitClosure())); |
| 261 run_loop.Run(); | 258 run_loop.Run(); |
| 262 EXPECT_FALSE(callback_helper.has_result()); | 259 EXPECT_FALSE(callback_helper.has_result()); |
| 263 // Destroy the reenabler to simulate the owning context being shut down | 260 // Destroy the reenabler to simulate the owning context being shut down |
| 264 // (e.g., the tab closing). | 261 // (e.g., the tab closing). |
| 265 extension_reenabler.reset(); | 262 extension_reenabler.reset(); |
| 266 EXPECT_TRUE( | 263 EXPECT_TRUE( |
| 267 callback_helper.result_matches(ExtensionReenabler::ABORTED)); | 264 callback_helper.result_matches(ExtensionReenabler::ABORTED)); |
| 268 } | 265 } |
| 269 } | 266 } |
| 270 | 267 |
| 271 } // namespace extensions | 268 } // namespace extensions |
| OLD | NEW |