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