blob: 8091c4a2bd65bb12cb0aeefb10174b4121a1f120 [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"
12#include "base/run_loop.h"
13#include "chrome/browser/extensions/extension_service.h"
14#include "chrome/browser/extensions/test_extension_system.h"
15#include "chrome/test/base/testing_browser_process.h"
16#include "chrome/test/base/testing_profile.h"
17#include "chrome/test/base/testing_profile_manager.h"
Gabriel Charettec7108742019-08-23 03:31:4018#include "content/public/test/browser_task_environment.h"
xiyuanf6a4c6a62016-04-19 18:14:5419#include "content/public/test/test_renderer_host.h"
20#include "extensions/browser/event_router.h"
21#include "extensions/browser/event_router_factory.h"
22#include "extensions/browser/extension_host.h"
23#include "extensions/browser/extension_prefs.h"
24#include "extensions/browser/extension_registry.h"
25#include "extensions/common/extension_builder.h"
26#include "extensions/common/manifest_handlers/background_info.h"
27#include "extensions/common/value_builder.h"
28#include "testing/gtest/include/gtest/gtest.h"
29
30#if defined(OS_CHROMEOS)
31#include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
xiyuanf6a4c6a62016-04-19 18:14:5432#include "chrome/browser/chromeos/settings/cros_settings.h"
33#include "chrome/browser/chromeos/settings/device_settings_service.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() {
107 profile_manager_.reset(
108 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
109 }
110
111 // testing::Test
112 void SetUp() override {
113 // Must be called from ::testing::Test::SetUp.
114 ASSERT_TRUE(profile_manager_->SetUp());
115
116 const char kUserProfile[] = "[email protected]";
117#if defined(OS_CHROMEOS)
118 const AccountId account_id(AccountId::FromUserEmail(kUserProfile));
119 // Needed to allow ChromeProcessManagerDelegate to allow background pages.
120 fake_user_manager_ = new chromeos::FakeChromeUserManager();
121 // Takes ownership of fake_user_manager_.
Xiyuan Xiadfe3a9f2017-11-13 21:46:26122 scoped_user_manager_enabler_ =
123 std::make_unique<user_manager::ScopedUserManager>(
124 base::WrapUnique(fake_user_manager_));
xiyuanf6a4c6a62016-04-19 18:14:54125 fake_user_manager_->AddUser(account_id);
126 fake_user_manager_->LoginUser(account_id);
127#endif
128 profile_ = profile_manager_->CreateTestingProfile(kUserProfile);
Dominick Ng51154652019-09-25 07:44:20129 base::RunLoop().RunUntilIdle();
xiyuanf6a4c6a62016-04-19 18:14:54130
Ghazale Hosseinabadi1d810e92020-06-01 20:43:02131 system_ = static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_));
132 service_ = system_->CreateExtensionService(
xiyuanf6a4c6a62016-04-19 18:14:54133 base::CommandLine::ForCurrentProcess(),
134 base::FilePath() /* install_directory */,
135 false /* autoupdate_enabled */);
136 registry_ = ExtensionRegistry::Get(profile_);
137
138 event_router_ = static_cast<EventRouter*>(
139 EventRouterFactory::GetInstance()->SetTestingFactoryAndUse(
Sylvain Defresne711ff6b2018-10-04 12:33:54140 profile_, base::BindRepeating(&BuildEventRouter)));
xiyuanf6a4c6a62016-04-19 18:14:54141
Ghazale Hosseinabadi1d810e92020-06-01 20:43:02142 delayer_.reset(new UpdateInstallGate(profile_));
xiyuanf6a4c6a62016-04-19 18:14:54143
144 new_app_ = CreateApp(kAppId, "2.0");
145 new_persistent_ = CreateExtension(kPersistentExtensionId, "2.0", true);
146 new_none_persistent_ =
147 CreateExtension(kNonPersistentExtensionId, "2.0", false);
148 }
149
150 void TearDown() override { profile_manager_->DeleteAllTestingProfiles(); }
151
152 void AddExistingExtensions() {
Devlin Cronin8e5892f2018-10-04 00:13:43153 scoped_refptr<const Extension> app = CreateApp(kAppId, "1.0");
xiyuanf6a4c6a62016-04-19 18:14:54154 registry_->AddEnabled(app);
155
Devlin Cronin8e5892f2018-10-04 00:13:43156 scoped_refptr<const Extension> persistent =
xiyuanf6a4c6a62016-04-19 18:14:54157 CreateExtension(kPersistentExtensionId, "1.0", true);
158 registry_->AddEnabled(persistent);
159
Devlin Cronin8e5892f2018-10-04 00:13:43160 scoped_refptr<const Extension> none_persistent =
xiyuanf6a4c6a62016-04-19 18:14:54161 CreateExtension(kNonPersistentExtensionId, "1.0", false);
162 registry_->AddEnabled(none_persistent);
163 }
164
165 void MakeExtensionInUse(const std::string& extension_id) {
166 const Extension* const extension =
167 registry_->GetInstalledExtension(extension_id);
Lei Zhang90c474642020-06-12 01:26:22168 ASSERT_TRUE(extension);
169 ASSERT_TRUE(CreateHost(profile_, extension));
xiyuanf6a4c6a62016-04-19 18:14:54170 }
171
172 void MakeExtensionListenForOnUpdateAvailable(
173 const std::string& extension_id) {
174 const char kOnUpdateAvailableEvent[] = "runtime.onUpdateAvailable";
175 event_router_->AddEventListener(kOnUpdateAvailableEvent, NULL,
176 extension_id);
177 }
178
179 void Check(const Extension* extension,
180 bool is_in_use,
181 bool has_listener,
182 bool install_immediately,
183 InstallGate::Action expected_action) {
184 if (is_in_use)
185 MakeExtensionInUse(extension->id());
186 if (has_listener)
187 MakeExtensionListenForOnUpdateAvailable(extension->id());
188
189 EXPECT_EQ(expected_action,
190 delayer()->ShouldDelay(extension, install_immediately));
191 }
192
193 UpdateInstallGate* delayer() { return delayer_.get(); }
Ghazale Hosseinabadi1d810e92020-06-01 20:43:02194 ExtensionSystem* system() { return system_; }
xiyuanf6a4c6a62016-04-19 18:14:54195 ExtensionService* service() { return service_; }
196
197 const Extension* new_app() const { return new_app_.get(); }
198 const Extension* new_persistent() const { return new_persistent_.get(); }
199 const Extension* new_none_persistent() const {
200 return new_none_persistent_.get();
201 }
202
203 private:
204 // Needed by extension system.
Gabriel Charette798fde72019-08-20 22:24:04205 content::BrowserTaskEnvironment task_environment_;
xiyuanf6a4c6a62016-04-19 18:14:54206
207 // Needed to ensure we don't end up creating actual RenderViewHosts
208 // and RenderProcessHosts.
209 content::RenderViewHostTestEnabler render_view_host_test_enabler_;
210
211 TestingProfile* profile_ = nullptr;
212 std::unique_ptr<TestingProfileManager> profile_manager_;
213
Ghazale Hosseinabadi1d810e92020-06-01 20:43:02214 TestExtensionSystem* system_ = nullptr;
xiyuanf6a4c6a62016-04-19 18:14:54215 ExtensionService* service_ = nullptr;
216 ExtensionRegistry* registry_ = nullptr;
217 EventRouter* event_router_ = nullptr;
218
219#if defined(OS_CHROMEOS)
220 // Needed for creating ExtensionService.
221 chromeos::FakeChromeUserManager* fake_user_manager_ = nullptr;
Xiyuan Xiadfe3a9f2017-11-13 21:46:26222 std::unique_ptr<user_manager::ScopedUserManager> scoped_user_manager_enabler_;
xiyuanf6a4c6a62016-04-19 18:14:54223#endif
224
225 std::unique_ptr<UpdateInstallGate> delayer_;
226
Devlin Cronin8e5892f2018-10-04 00:13:43227 scoped_refptr<const Extension> new_app_;
228 scoped_refptr<const Extension> new_persistent_;
229 scoped_refptr<const Extension> new_none_persistent_;
xiyuanf6a4c6a62016-04-19 18:14:54230
231 DISALLOW_COPY_AND_ASSIGN(UpdateInstallGateTest);
232};
233
234TEST_F(UpdateInstallGateTest, InstallOnServiceNotReady) {
Ghazale Hosseinabadi1d810e92020-06-01 20:43:02235 ASSERT_FALSE(system()->is_ready());
xiyuanf6a4c6a62016-04-19 18:14:54236 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
241TEST_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
248TEST_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
265TEST_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