blob: 6b066154ac0f70363804fcdd7d762d9eea6ce68e [file] [log] [blame]
xiyuanf6a4c6a62016-04-19 18:14:541// 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 Defresne711ff6b2018-10-04 12:33:549#include "base/bind.h"
xiyuanf6a4c6a62016-04-19 18:14:5410#include "base/command_line.h"
11#include "base/memory/ptr_util.h"
Keishi Hattori0e45c022021-11-27 09:25:5212#include "base/memory/raw_ptr.h"
xiyuanf6a4c6a62016-04-19 18:14:5413#include "base/run_loop.h"
Yuta Hijikata1290fee22020-11-25 09:46:2814#include "build/chromeos_buildflags.h"
xiyuanf6a4c6a62016-04-19 18:14:5415#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 Charettec7108742019-08-23 03:31:4020#include "content/public/test/browser_task_environment.h"
xiyuanf6a4c6a62016-04-19 18:14:5421#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 Hijikata1290fee22020-11-25 09:46:2832#if BUILDFLAG(IS_CHROMEOS_ASH)
Henrique Ferreiro1ab7f12f2021-03-04 19:54:4033#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
Xiyuan Xiadfe3a9f2017-11-13 21:46:2634#include "components/user_manager/scoped_user_manager.h"
xiyuanf6a4c6a62016-04-19 18:14:5435#endif
36
37namespace extensions {
38
39namespace {
40
41const char kAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
42const char kPersistentExtensionId[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
43const char kNonPersistentExtensionId[] = "cccccccccccccccccccccccccccccccc";
44
45std::unique_ptr<KeyedService> BuildEventRouter(
46 content::BrowserContext* profile) {
Jinho Bangb5216cec2018-01-17 19:43:1147 return std::make_unique<extensions::EventRouter>(profile, nullptr);
xiyuanf6a4c6a62016-04-19 18:14:5448}
49
Devlin Cronin8e5892f2018-10-04 00:13:4350scoped_refptr<const Extension> CreateApp(const std::string& extension_id,
51 const std::string& version) {
52 scoped_refptr<const Extension> app =
xiyuanf6a4c6a62016-04-19 18:14:5453 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 Cronin8e5892f2018-10-04 00:13:4374scoped_refptr<const Extension> CreateExtension(const std::string& extension_id,
75 const std::string& version,
76 bool persistent) {
77 scoped_refptr<const Extension> extension =
xiyuanf6a4c6a62016-04-19 18:14:5478 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 Ahmed6f874682018-04-13 04:49:4686 .Set("persistent", persistent)
xiyuanf6a4c6a62016-04-19 18:14:5487 .Build())
88 .Build())
89 .SetID(extension_id)
90 .Build();
91 return extension;
92}
93
94ExtensionHost* 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
104class UpdateInstallGateTest : public testing::Test {
105 public:
106 UpdateInstallGateTest() {
Peter Boström924f8032021-04-02 20:36:02107 profile_manager_ = std::make_unique<TestingProfileManager>(
108 TestingBrowserProcess::GetGlobal());
xiyuanf6a4c6a62016-04-19 18:14:54109 }
110
Peter Boström6316db82021-09-24 16:15:11111 UpdateInstallGateTest(const UpdateInstallGateTest&) = delete;
112 UpdateInstallGateTest& operator=(const UpdateInstallGateTest&) = delete;
113
xiyuanf6a4c6a62016-04-19 18:14:54114 // 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 Hijikata1290fee22020-11-25 09:46:28120#if BUILDFLAG(IS_CHROMEOS_ASH)
xiyuanf6a4c6a62016-04-19 18:14:54121 const AccountId account_id(AccountId::FromUserEmail(kUserProfile));
122 // Needed to allow ChromeProcessManagerDelegate to allow background pages.
Henrique Ferreiro4c2fc0a2021-03-26 10:42:47123 fake_user_manager_ = new ash::FakeChromeUserManager();
xiyuanf6a4c6a62016-04-19 18:14:54124 // Takes ownership of fake_user_manager_.
Xiyuan Xiadfe3a9f2017-11-13 21:46:26125 scoped_user_manager_enabler_ =
126 std::make_unique<user_manager::ScopedUserManager>(
127 base::WrapUnique(fake_user_manager_));
xiyuanf6a4c6a62016-04-19 18:14:54128 fake_user_manager_->AddUser(account_id);
129 fake_user_manager_->LoginUser(account_id);
130#endif
131 profile_ = profile_manager_->CreateTestingProfile(kUserProfile);
Dominick Ng51154652019-09-25 07:44:20132 base::RunLoop().RunUntilIdle();
xiyuanf6a4c6a62016-04-19 18:14:54133
Ghazale Hosseinabadi1d810e92020-06-01 20:43:02134 system_ = static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_));
135 service_ = system_->CreateExtensionService(
xiyuanf6a4c6a62016-04-19 18:14:54136 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 Defresne711ff6b2018-10-04 12:33:54143 profile_, base::BindRepeating(&BuildEventRouter)));
xiyuanf6a4c6a62016-04-19 18:14:54144
Peter Boström924f8032021-04-02 20:36:02145 delayer_ = std::make_unique<UpdateInstallGate>(profile_);
xiyuanf6a4c6a62016-04-19 18:14:54146
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 Cronin8e5892f2018-10-04 00:13:43156 scoped_refptr<const Extension> app = CreateApp(kAppId, "1.0");
xiyuanf6a4c6a62016-04-19 18:14:54157 registry_->AddEnabled(app);
158
Devlin Cronin8e5892f2018-10-04 00:13:43159 scoped_refptr<const Extension> persistent =
xiyuanf6a4c6a62016-04-19 18:14:54160 CreateExtension(kPersistentExtensionId, "1.0", true);
161 registry_->AddEnabled(persistent);
162
Devlin Cronin8e5892f2018-10-04 00:13:43163 scoped_refptr<const Extension> none_persistent =
xiyuanf6a4c6a62016-04-19 18:14:54164 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 Zhang90c474642020-06-12 01:26:22171 ASSERT_TRUE(extension);
172 ASSERT_TRUE(CreateHost(profile_, extension));
xiyuanf6a4c6a62016-04-19 18:14:54173 }
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 Hosseinabadi1d810e92020-06-01 20:43:02197 ExtensionSystem* system() { return system_; }
xiyuanf6a4c6a62016-04-19 18:14:54198 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 Charette798fde72019-08-20 22:24:04208 content::BrowserTaskEnvironment task_environment_;
xiyuanf6a4c6a62016-04-19 18:14:54209
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 Hattori0e45c022021-11-27 09:25:52214 raw_ptr<TestingProfile> profile_ = nullptr;
xiyuanf6a4c6a62016-04-19 18:14:54215 std::unique_ptr<TestingProfileManager> profile_manager_;
216
Keishi Hattori0e45c022021-11-27 09:25:52217 raw_ptr<TestExtensionSystem> system_ = nullptr;
218 raw_ptr<ExtensionService> service_ = nullptr;
219 raw_ptr<ExtensionRegistry> registry_ = nullptr;
220 raw_ptr<EventRouter> event_router_ = nullptr;
xiyuanf6a4c6a62016-04-19 18:14:54221
Yuta Hijikata1290fee22020-11-25 09:46:28222#if BUILDFLAG(IS_CHROMEOS_ASH)
xiyuanf6a4c6a62016-04-19 18:14:54223 // Needed for creating ExtensionService.
Henrique Ferreiro4c2fc0a2021-03-26 10:42:47224 ash::FakeChromeUserManager* fake_user_manager_ = nullptr;
Xiyuan Xiadfe3a9f2017-11-13 21:46:26225 std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_enabler_;
xiyuanf6a4c6a62016-04-19 18:14:54226#endif
227
228 std::unique_ptr<UpdateInstallGate> delayer_;
229
Devlin Cronin8e5892f2018-10-04 00:13:43230 scoped_refptr<const Extension> new_app_;
231 scoped_refptr<const Extension> new_persistent_;
232 scoped_refptr<const Extension> new_none_persistent_;
xiyuanf6a4c6a62016-04-19 18:14:54233};
234
235TEST_F(UpdateInstallGateTest, InstallOnServiceNotReady) {
Ghazale Hosseinabadi1d810e92020-06-01 20:43:02236 ASSERT_FALSE(system()->is_ready());
xiyuanf6a4c6a62016-04-19 18:14:54237 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
242TEST_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
249TEST_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
266TEST_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