xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [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 | |
| 5 | #include "chrome/browser/extensions/update_install_gate.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
Sylvain Defresne | 711ff6b | 2018-10-04 12:33:54 | [diff] [blame] | 9 | #include "base/bind.h" |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 10 | #include "base/command_line.h" |
| 11 | #include "base/memory/ptr_util.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 12 | #include "base/memory/raw_ptr.h" |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 13 | #include "base/run_loop.h" |
Yuta Hijikata | 1290fee2 | 2020-11-25 09:46:28 | [diff] [blame] | 14 | #include "build/chromeos_buildflags.h" |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 15 | #include "chrome/browser/extensions/extension_service.h" |
| 16 | #include "chrome/browser/extensions/test_extension_system.h" |
| 17 | #include "chrome/test/base/testing_browser_process.h" |
| 18 | #include "chrome/test/base/testing_profile.h" |
| 19 | #include "chrome/test/base/testing_profile_manager.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 20 | #include "content/public/test/browser_task_environment.h" |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 21 | #include "content/public/test/test_renderer_host.h" |
| 22 | #include "extensions/browser/event_router.h" |
| 23 | #include "extensions/browser/event_router_factory.h" |
| 24 | #include "extensions/browser/extension_host.h" |
| 25 | #include "extensions/browser/extension_prefs.h" |
| 26 | #include "extensions/browser/extension_registry.h" |
| 27 | #include "extensions/common/extension_builder.h" |
| 28 | #include "extensions/common/manifest_handlers/background_info.h" |
| 29 | #include "extensions/common/value_builder.h" |
| 30 | #include "testing/gtest/include/gtest/gtest.h" |
| 31 | |
Yuta Hijikata | 1290fee2 | 2020-11-25 09:46:28 | [diff] [blame] | 32 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Henrique Ferreiro | 1ab7f12f | 2021-03-04 19:54:40 | [diff] [blame] | 33 | #include "chrome/browser/ash/login/users/fake_chrome_user_manager.h" |
Xiyuan Xia | dfe3a9f | 2017-11-13 21:46:26 | [diff] [blame] | 34 | #include "components/user_manager/scoped_user_manager.h" |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | namespace extensions { |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | const char kAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 42 | const char kPersistentExtensionId[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; |
| 43 | const char kNonPersistentExtensionId[] = "cccccccccccccccccccccccccccccccc"; |
| 44 | |
| 45 | std::unique_ptr<KeyedService> BuildEventRouter( |
| 46 | content::BrowserContext* profile) { |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 47 | return std::make_unique<extensions::EventRouter>(profile, nullptr); |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 48 | } |
| 49 | |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 50 | scoped_refptr<const Extension> CreateApp(const std::string& extension_id, |
| 51 | const std::string& version) { |
| 52 | scoped_refptr<const Extension> app = |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 53 | ExtensionBuilder() |
| 54 | .SetManifest( |
| 55 | DictionaryBuilder() |
| 56 | .Set("name", "Test app") |
| 57 | .Set("version", version) |
| 58 | .Set("manifest_version", 2) |
| 59 | .Set("app", |
| 60 | DictionaryBuilder() |
| 61 | .Set("background", |
| 62 | DictionaryBuilder() |
| 63 | .Set("scripts", ListBuilder() |
| 64 | .Append("background.js") |
| 65 | .Build()) |
| 66 | .Build()) |
| 67 | .Build()) |
| 68 | .Build()) |
| 69 | .SetID(extension_id) |
| 70 | .Build(); |
| 71 | return app; |
| 72 | } |
| 73 | |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 74 | scoped_refptr<const Extension> CreateExtension(const std::string& extension_id, |
| 75 | const std::string& version, |
| 76 | bool persistent) { |
| 77 | scoped_refptr<const Extension> extension = |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 78 | ExtensionBuilder() |
| 79 | .SetManifest( |
| 80 | DictionaryBuilder() |
| 81 | .Set("name", "Test extension") |
| 82 | .Set("version", version) |
| 83 | .Set("manifest_version", 2) |
| 84 | .Set("background", DictionaryBuilder() |
| 85 | .Set("page", "background.html") |
Istiaque Ahmed | 6f87468 | 2018-04-13 04:49:46 | [diff] [blame] | 86 | .Set("persistent", persistent) |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 87 | .Build()) |
| 88 | .Build()) |
| 89 | .SetID(extension_id) |
| 90 | .Build(); |
| 91 | return extension; |
| 92 | } |
| 93 | |
| 94 | ExtensionHost* CreateHost(Profile* profile, const Extension* app) { |
| 95 | ProcessManager::Get(profile)->CreateBackgroundHost( |
| 96 | app, BackgroundInfo::GetBackgroundURL(app)); |
| 97 | base::RunLoop().RunUntilIdle(); |
| 98 | |
| 99 | return ProcessManager::Get(profile)->GetBackgroundHostForExtension(app->id()); |
| 100 | } |
| 101 | |
| 102 | } // namespace |
| 103 | |
| 104 | class UpdateInstallGateTest : public testing::Test { |
| 105 | public: |
| 106 | UpdateInstallGateTest() { |
Peter Boström | 924f803 | 2021-04-02 20:36:02 | [diff] [blame] | 107 | profile_manager_ = std::make_unique<TestingProfileManager>( |
| 108 | TestingBrowserProcess::GetGlobal()); |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 109 | } |
| 110 | |
Peter Boström | 6316db8 | 2021-09-24 16:15:11 | [diff] [blame] | 111 | UpdateInstallGateTest(const UpdateInstallGateTest&) = delete; |
| 112 | UpdateInstallGateTest& operator=(const UpdateInstallGateTest&) = delete; |
| 113 | |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 114 | // testing::Test |
| 115 | void SetUp() override { |
| 116 | // Must be called from ::testing::Test::SetUp. |
| 117 | ASSERT_TRUE(profile_manager_->SetUp()); |
| 118 | |
| 119 | const char kUserProfile[] = "[email protected]"; |
Yuta Hijikata | 1290fee2 | 2020-11-25 09:46:28 | [diff] [blame] | 120 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 121 | const AccountId account_id(AccountId::FromUserEmail(kUserProfile)); |
| 122 | // Needed to allow ChromeProcessManagerDelegate to allow background pages. |
Henrique Ferreiro | 4c2fc0a | 2021-03-26 10:42:47 | [diff] [blame] | 123 | fake_user_manager_ = new ash::FakeChromeUserManager(); |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 124 | // Takes ownership of fake_user_manager_. |
Xiyuan Xia | dfe3a9f | 2017-11-13 21:46:26 | [diff] [blame] | 125 | scoped_user_manager_enabler_ = |
| 126 | std::make_unique<user_manager::ScopedUserManager>( |
| 127 | base::WrapUnique(fake_user_manager_)); |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 128 | fake_user_manager_->AddUser(account_id); |
| 129 | fake_user_manager_->LoginUser(account_id); |
| 130 | #endif |
| 131 | profile_ = profile_manager_->CreateTestingProfile(kUserProfile); |
Dominick Ng | 5115465 | 2019-09-25 07:44:20 | [diff] [blame] | 132 | base::RunLoop().RunUntilIdle(); |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 133 | |
Ghazale Hosseinabadi | 1d810e9 | 2020-06-01 20:43:02 | [diff] [blame] | 134 | system_ = static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_)); |
| 135 | service_ = system_->CreateExtensionService( |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 136 | base::CommandLine::ForCurrentProcess(), |
| 137 | base::FilePath() /* install_directory */, |
| 138 | false /* autoupdate_enabled */); |
| 139 | registry_ = ExtensionRegistry::Get(profile_); |
| 140 | |
| 141 | event_router_ = static_cast<EventRouter*>( |
| 142 | EventRouterFactory::GetInstance()->SetTestingFactoryAndUse( |
Sylvain Defresne | 711ff6b | 2018-10-04 12:33:54 | [diff] [blame] | 143 | profile_, base::BindRepeating(&BuildEventRouter))); |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 144 | |
Peter Boström | 924f803 | 2021-04-02 20:36:02 | [diff] [blame] | 145 | delayer_ = std::make_unique<UpdateInstallGate>(profile_); |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 146 | |
| 147 | new_app_ = CreateApp(kAppId, "2.0"); |
| 148 | new_persistent_ = CreateExtension(kPersistentExtensionId, "2.0", true); |
| 149 | new_none_persistent_ = |
| 150 | CreateExtension(kNonPersistentExtensionId, "2.0", false); |
| 151 | } |
| 152 | |
| 153 | void TearDown() override { profile_manager_->DeleteAllTestingProfiles(); } |
| 154 | |
| 155 | void AddExistingExtensions() { |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 156 | scoped_refptr<const Extension> app = CreateApp(kAppId, "1.0"); |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 157 | registry_->AddEnabled(app); |
| 158 | |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 159 | scoped_refptr<const Extension> persistent = |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 160 | CreateExtension(kPersistentExtensionId, "1.0", true); |
| 161 | registry_->AddEnabled(persistent); |
| 162 | |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 163 | scoped_refptr<const Extension> none_persistent = |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 164 | CreateExtension(kNonPersistentExtensionId, "1.0", false); |
| 165 | registry_->AddEnabled(none_persistent); |
| 166 | } |
| 167 | |
| 168 | void MakeExtensionInUse(const std::string& extension_id) { |
| 169 | const Extension* const extension = |
| 170 | registry_->GetInstalledExtension(extension_id); |
Lei Zhang | 90c47464 | 2020-06-12 01:26:22 | [diff] [blame] | 171 | ASSERT_TRUE(extension); |
| 172 | ASSERT_TRUE(CreateHost(profile_, extension)); |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void MakeExtensionListenForOnUpdateAvailable( |
| 176 | const std::string& extension_id) { |
| 177 | const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable"; |
| 178 | event_router_->AddEventListener(kOnUpdateAvailableEvent, NULL, |
| 179 | extension_id); |
| 180 | } |
| 181 | |
| 182 | void Check(const Extension* extension, |
| 183 | bool is_in_use, |
| 184 | bool has_listener, |
| 185 | bool install_immediately, |
| 186 | InstallGate::Action expected_action) { |
| 187 | if (is_in_use) |
| 188 | MakeExtensionInUse(extension->id()); |
| 189 | if (has_listener) |
| 190 | MakeExtensionListenForOnUpdateAvailable(extension->id()); |
| 191 | |
| 192 | EXPECT_EQ(expected_action, |
| 193 | delayer()->ShouldDelay(extension, install_immediately)); |
| 194 | } |
| 195 | |
| 196 | UpdateInstallGate* delayer() { return delayer_.get(); } |
Ghazale Hosseinabadi | 1d810e9 | 2020-06-01 20:43:02 | [diff] [blame] | 197 | ExtensionSystem* system() { return system_; } |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 198 | ExtensionService* service() { return service_; } |
| 199 | |
| 200 | const Extension* new_app() const { return new_app_.get(); } |
| 201 | const Extension* new_persistent() const { return new_persistent_.get(); } |
| 202 | const Extension* new_none_persistent() const { |
| 203 | return new_none_persistent_.get(); |
| 204 | } |
| 205 | |
| 206 | private: |
| 207 | // Needed by extension system. |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 208 | content::BrowserTaskEnvironment task_environment_; |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 209 | |
| 210 | // Needed to ensure we don't end up creating actual RenderViewHosts |
| 211 | // and RenderProcessHosts. |
| 212 | content::RenderViewHostTestEnabler render_view_host_test_enabler_; |
| 213 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 214 | raw_ptr<TestingProfile> profile_ = nullptr; |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 215 | std::unique_ptr<TestingProfileManager> profile_manager_; |
| 216 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 217 | raw_ptr<TestExtensionSystem> system_ = nullptr; |
| 218 | raw_ptr<ExtensionService> service_ = nullptr; |
| 219 | raw_ptr<ExtensionRegistry> registry_ = nullptr; |
| 220 | raw_ptr<EventRouter> event_router_ = nullptr; |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 221 | |
Yuta Hijikata | 1290fee2 | 2020-11-25 09:46:28 | [diff] [blame] | 222 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 223 | // Needed for creating ExtensionService. |
Henrique Ferreiro | 4c2fc0a | 2021-03-26 10:42:47 | [diff] [blame] | 224 | ash::FakeChromeUserManager* fake_user_manager_ = nullptr; |
Xiyuan Xia | dfe3a9f | 2017-11-13 21:46:26 | [diff] [blame] | 225 | std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_enabler_; |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 226 | #endif |
| 227 | |
| 228 | std::unique_ptr<UpdateInstallGate> delayer_; |
| 229 | |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 230 | scoped_refptr<const Extension> new_app_; |
| 231 | scoped_refptr<const Extension> new_persistent_; |
| 232 | scoped_refptr<const Extension> new_none_persistent_; |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | TEST_F(UpdateInstallGateTest, InstallOnServiceNotReady) { |
Ghazale Hosseinabadi | 1d810e9 | 2020-06-01 20:43:02 | [diff] [blame] | 236 | ASSERT_FALSE(system()->is_ready()); |
xiyuan | f6a4c6a6 | 2016-04-19 18:14:54 | [diff] [blame] | 237 | Check(new_app(), false, false, false, InstallGate::INSTALL); |
| 238 | Check(new_persistent(), false, false, false, InstallGate::INSTALL); |
| 239 | Check(new_none_persistent(), false, false, false, InstallGate::INSTALL); |
| 240 | } |
| 241 | |
| 242 | TEST_F(UpdateInstallGateTest, InstallOnFirstInstall) { |
| 243 | service()->Init(); |
| 244 | Check(new_app(), false, false, false, InstallGate::INSTALL); |
| 245 | Check(new_persistent(), false, false, false, InstallGate::INSTALL); |
| 246 | Check(new_none_persistent(), false, false, false, InstallGate::INSTALL); |
| 247 | } |
| 248 | |
| 249 | TEST_F(UpdateInstallGateTest, InstallOnInstallImmediately) { |
| 250 | service()->Init(); |
| 251 | AddExistingExtensions(); |
| 252 | |
| 253 | const bool kInstallImmediately = true; |
| 254 | for (bool in_use : {false, true}) { |
| 255 | for (bool has_listener : {false, true}) { |
| 256 | Check(new_app(), in_use, has_listener, kInstallImmediately, |
| 257 | InstallGate::INSTALL); |
| 258 | Check(new_persistent(), in_use, has_listener, kInstallImmediately, |
| 259 | InstallGate::INSTALL); |
| 260 | Check(new_none_persistent(), in_use, has_listener, kInstallImmediately, |
| 261 | InstallGate::INSTALL); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | TEST_F(UpdateInstallGateTest, DelayInstallWhenInUse) { |
| 267 | service()->Init(); |
| 268 | AddExistingExtensions(); |
| 269 | |
| 270 | const bool kInUse = true; |
| 271 | const bool kDontInstallImmediately = false; |
| 272 | for (bool has_listener : {false, true}) { |
| 273 | Check(new_app(), kInUse, has_listener, kDontInstallImmediately, |
| 274 | InstallGate::DELAY); |
| 275 | Check(new_persistent(), kInUse, has_listener, kDontInstallImmediately, |
| 276 | has_listener ? InstallGate::DELAY : InstallGate::INSTALL); |
| 277 | Check(new_none_persistent(), kInUse, has_listener, kDontInstallImmediately, |
| 278 | InstallGate::DELAY); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | } // namespace extensions |