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