1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/profiles/profile_destroyer.h"
#include <array>
#include <vector>
#include "base/feature_list.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/profiles/keep_alive/profile_keep_alive_types.h"
#include "chrome/browser/profiles/keep_alive/scoped_profile_keep_alive.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/site_instance.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_renderer_host.h"
#include "testing/gtest/include/gtest/gtest.h"
class ProfileDestroyerTest : public testing::Test,
public testing::WithParamInterface<bool> {
public:
ProfileDestroyerTest() = default;
ProfileDestroyerTest(const ProfileDestroyerTest&) = delete;
ProfileDestroyerTest& operator=(const ProfileDestroyerTest&) = delete;
void SetUp() override { ASSERT_TRUE(profile_manager_.SetUp()); }
TestingProfile* original_profile() { return original_profile_; }
TestingProfile* OtrProfile(size_t i) {
return otr_profiles_.size() > i ? otr_profiles_[i] : nullptr;
}
void CreateOriginalProfile() {
original_profile_ = profile_manager_.CreateTestingProfile("foo");
original_profile_->SetProfileDestructionObserver(
base::BindOnce(&ProfileDestroyerTest::SetOriginalProfileDestroyed,
base::Unretained(this)));
original_profile_keep_alive_ = std::make_unique<ScopedProfileKeepAlive>(
original_profile_, ProfileKeepAliveOrigin::kBrowserWindow);
}
void CreateOTRProfile() {
Profile::OTRProfileID profile_id =
(is_primary_otr_ && otr_profiles_.size() == 0)
? Profile::OTRProfileID::PrimaryID()
: Profile::OTRProfileID::CreateUniqueForTesting();
TestingProfile::Builder builder;
builder.SetPath(original_profile_->GetPath());
TestingProfile* otr_profile =
builder.BuildOffTheRecord(original_profile_, profile_id);
otr_profile->SetProfileDestructionObserver(
base::BindOnce(&ProfileDestroyerTest::SetOTRProfileDestroyed,
base::Unretained(this), base::Unretained(otr_profile)));
otr_profiles_.push_back(otr_profile);
}
void SetOriginalProfileDestroyed() { original_profile_ = nullptr; }
void SetOTRProfileDestroyed(TestingProfile* destroyed_profile) {
for (auto& profile : otr_profiles_) {
if (profile == destroyed_profile) {
profile = nullptr;
}
}
}
// Creates a render process host based on a new site instance given the
// |profile| and mark it as used. Returns a reference to it.
content::RenderProcessHost* CreatedRendererProcessHost(Profile* profile) {
site_instances_.emplace_back(content::SiteInstance::Create(profile));
content::RenderProcessHost* rph =
site_instances_.back()->GetOrCreateProcessForTesting();
EXPECT_TRUE(rph);
rph->SetIsUsed();
return rph;
}
void StopKeepingAliveOriginalProfile() {
original_profile_keep_alive_.reset();
}
// Destroying profile is still not universally supported. We need to disable
// some tests, because it isn't possible to start destroying the profile.
bool IsScopedProfileKeepAliveSupported() {
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS)
return false;
#else
return base::FeatureList::IsEnabled(
features::kDestroyProfileOnBrowserClose);
#endif
}
protected:
const bool is_primary_otr_ = GetParam();
raw_ptr<TestingProfile> original_profile_;
std::vector<raw_ptr<TestingProfile>> otr_profiles_;
content::BrowserTaskEnvironment task_environment_;
TestingProfileManager profile_manager_{TestingBrowserProcess::GetGlobal()};
content::RenderViewHostTestEnabler rvh_test_enabler_;
std::unique_ptr<ScopedProfileKeepAlive> original_profile_keep_alive_;
std::vector<scoped_refptr<content::SiteInstance>> site_instances_;
};
TEST_P(ProfileDestroyerTest, DestroyOriginalProfileImmediately) {
if (!IsScopedProfileKeepAliveSupported())
return;
CreateOriginalProfile();
CreateOTRProfile();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
StopKeepingAliveOriginalProfile();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
// This doesn't really match real-world scenarios, because TestingProfile is
// different from OffTheRecordProfileImpl. The real impl acquires a keepalive
// on the parent profile, whereas OTR TestingProfile doesn't do that.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(original_profile());
EXPECT_FALSE(OtrProfile(0));
}
TEST_P(ProfileDestroyerTest, DestroyOriginalProfileDeferedByRenderProcessHost) {
if (!IsScopedProfileKeepAliveSupported())
return;
CreateOriginalProfile();
CreateOTRProfile();
content::RenderProcessHost* render_process_host =
CreatedRendererProcessHost(original_profile());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
// The original profile is not destroyed, because of the RenderProcessHost.
StopKeepingAliveOriginalProfile();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
// Releasing the RenderProcessHost triggers the deletion of the Profile. It
// happens in a posted task.
render_process_host->Cleanup();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(original_profile());
EXPECT_FALSE(OtrProfile(0));
}
TEST_P(ProfileDestroyerTest,
DestroyOriginalProfileDeferedByOffTheRecordRenderProcessHost) {
if (!IsScopedProfileKeepAliveSupported())
return;
CreateOriginalProfile();
CreateOTRProfile();
content::RenderProcessHost* render_process_host =
CreatedRendererProcessHost(OtrProfile(0));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
// The original profile is not destroyed, because of the RenderProcessHost.
StopKeepingAliveOriginalProfile();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
// Releasing the RenderProcessHost triggers the deletion of the Profile. It
// happens in a posted task.
render_process_host->Cleanup();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(original_profile());
EXPECT_FALSE(OtrProfile(0));
}
TEST_P(ProfileDestroyerTest,
DetroyBothProfileDeferedByMultipleRenderProcessHost) {
if (!IsScopedProfileKeepAliveSupported())
return;
CreateOriginalProfile();
CreateOTRProfile();
content::RenderProcessHost* rph_otr_profile =
CreatedRendererProcessHost(OtrProfile(0));
content::RenderProcessHost* rph_original_profile =
CreatedRendererProcessHost(original_profile());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
// No profile are destroyed, because of the RenderProcessHosts.
StopKeepingAliveOriginalProfile();
ProfileDestroyer::DestroyOTRProfileWhenAppropriate(OtrProfile(0));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
// Release the first process. It causes the associated profile to be released.
// This happens in a posted task.
rph_otr_profile->Cleanup();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile());
EXPECT_FALSE(OtrProfile(0));
// Release the second process. It causes the associated profile to be
// released. This happens in a posted task.
rph_original_profile->Cleanup();
EXPECT_TRUE(original_profile());
EXPECT_FALSE(OtrProfile(0));
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(original_profile());
EXPECT_FALSE(OtrProfile(0));
}
// Expect immediate OTR profile destruction when requested.
TEST_P(ProfileDestroyerTest, ImmediateOTRProfileDestructionNowWithNoHost) {
CreateOriginalProfile();
CreateOTRProfile();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
// Ask for immediate destruction of OTR profile with no hosts and expect
// immediate destruction.
ProfileDestroyer::DestroyOTRProfileImmediately(OtrProfile(0));
EXPECT_FALSE(OtrProfile(0));
}
#if defined(GTEST_HAS_DEATH_TEST)
// Expect immediate OTR profile destruction when requested.
TEST_P(ProfileDestroyerTest, CrashOTRProfileDestructionNowWithHosts) {
CreateOriginalProfile();
CreateOTRProfile();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
CreatedRendererProcessHost(OtrProfile(0));
// Immediate destruction of OTR profile with hosts will crash.
EXPECT_DEATH(ProfileDestroyer::DestroyOTRProfileImmediately(OtrProfile(0)),
"");
}
#endif // defined(GTEST_HAS_DEATH_TEST)
// Expect immediate OTR profile destruction when no pending renderer
// process host exists.
TEST_P(ProfileDestroyerTest, ImmediateOTRProfileDestructionWithNoHosts) {
CreateOriginalProfile();
CreateOTRProfile();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
// Ask for destruction of OTR profile, and expect immediate destruction.
ProfileDestroyer::DestroyOTRProfileWhenAppropriate(OtrProfile(0));
EXPECT_FALSE(OtrProfile(0));
}
// Expect pending renderer process hosts delay OTR profile destruction.
TEST_P(ProfileDestroyerTest, DelayedOTRProfileDestruction) {
CreateOriginalProfile();
CreateOTRProfile();
// Create two render process hosts.
content::RenderProcessHost* render_process_host1 =
CreatedRendererProcessHost(OtrProfile(0));
content::RenderProcessHost* render_process_host2 =
CreatedRendererProcessHost(OtrProfile(0));
// Ask for destruction of OTR profile, but expect it to be delayed.
ProfileDestroyer::DestroyOTRProfileWhenAppropriate(OtrProfile(0));
EXPECT_TRUE(OtrProfile(0));
// Destroy the first pending render process host, and expect it not to destroy
// the OTR profile.
render_process_host1->Cleanup();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(OtrProfile(0));
// Destroy the other renderer process, and expect destruction of OTR
// profile.
render_process_host2->Cleanup();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(OtrProfile(0));
}
TEST_P(ProfileDestroyerTest, RenderProcessAddedAfterDestroyRequested) {
if (!IsScopedProfileKeepAliveSupported())
return;
CreateOriginalProfile();
content::RenderProcessHost* render_process_host_1 =
CreatedRendererProcessHost(original_profile());
StopKeepingAliveOriginalProfile();
EXPECT_TRUE(original_profile());
content::RenderProcessHost* render_process_host_2 =
CreatedRendererProcessHost(original_profile());
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile()); // Waiting for 2 processes to be released
render_process_host_1->Cleanup();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile()); // Waiting for 1 process to be released.
render_process_host_2->Cleanup();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(original_profile()); // Destroyed.
}
// Regression test for:
// https://crbug.com/1337388#c11
TEST_P(ProfileDestroyerTest, DestructionRequestedTwiceWhileDelayedOTRProfile) {
CreateOriginalProfile();
CreateOTRProfile();
content::RenderProcessHost* render_process_host =
CreatedRendererProcessHost(OtrProfile(0));
ProfileDestroyer::DestroyOTRProfileWhenAppropriate(OtrProfile(0));
EXPECT_TRUE(OtrProfile(0));
ProfileDestroyer::DestroyOTRProfileWhenAppropriate(OtrProfile(0));
EXPECT_TRUE(OtrProfile(0));
render_process_host->Cleanup();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(OtrProfile(0));
}
// Regression for: https://crbug.com/1357476
// When two OTR profile are associated with the same original profile,
// requesting the destruction of one, was incorrectly causing the
// ProfileDestroyer to wait for the destruction of the RenderProcessHost of the
// second OTR profile.
TEST_P(ProfileDestroyerTest, MultipleOTRPRofile) {
CreateOriginalProfile();
CreateOTRProfile();
CreateOTRProfile();
CreateOTRProfile();
// Create a renderer process associated with every OTR profiles.
std::array<content::RenderProcessHost*, 3> render_process_host = {
CreatedRendererProcessHost(OtrProfile(0)),
CreatedRendererProcessHost(OtrProfile(1)),
CreatedRendererProcessHost(OtrProfile(2)),
};
// Ask for the destruction of two of them. The destruction is delayed, because
// they are kept alive by two RenderProcessHost depending on them.
ProfileDestroyer::DestroyOTRProfileWhenAppropriate(OtrProfile(0));
ProfileDestroyer::DestroyOTRProfileWhenAppropriate(OtrProfile(1));
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
EXPECT_TRUE(OtrProfile(1));
EXPECT_TRUE(OtrProfile(2));
// Destroy the RenderProcessHost keeping alive the first OTR profile.
render_process_host[0]->Cleanup();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile());
EXPECT_FALSE(OtrProfile(0));
EXPECT_TRUE(OtrProfile(1));
EXPECT_TRUE(OtrProfile(2));
// Destroy the RenderProcessHost keeping alive the second OTR profile.
render_process_host[1]->Cleanup();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile());
EXPECT_FALSE(OtrProfile(0));
EXPECT_FALSE(OtrProfile(1));
EXPECT_TRUE(OtrProfile(2));
// Destroy the third RenderProcessHost.
render_process_host[2]->Cleanup();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(original_profile());
EXPECT_FALSE(OtrProfile(0));
EXPECT_FALSE(OtrProfile(1));
EXPECT_TRUE(OtrProfile(2));
// Allow the deletion of the last OTR profile:
ProfileDestroyer::DestroyOTRProfileWhenAppropriate(OtrProfile(2));
EXPECT_TRUE(original_profile());
EXPECT_FALSE(OtrProfile(0));
EXPECT_FALSE(OtrProfile(1));
EXPECT_FALSE(OtrProfile(2));
}
#if defined(GTEST_HAS_DEATH_TEST)
// Crash if original profile has hosts at shutdown.
TEST_P(ProfileDestroyerTest, CrashShutdownAllPendingProfilesOriginalWithHosts) {
CreateOriginalProfile();
EXPECT_TRUE(original_profile());
CreatedRendererProcessHost(original_profile());
StopKeepingAliveOriginalProfile();
EXPECT_TRUE(original_profile());
EXPECT_DEATH(ProfileDestroyer::DestroyPendingProfilesForShutdown(), "");
}
// Crash if off-the-record profile has hosts at shutdown.
TEST_P(ProfileDestroyerTest, CrashShutdownAllPendingProfilesOTRWithHosts) {
CreateOriginalProfile();
CreateOTRProfile();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
CreatedRendererProcessHost(OtrProfile(0));
StopKeepingAliveOriginalProfile();
EXPECT_TRUE(original_profile());
EXPECT_TRUE(OtrProfile(0));
// Ask for destruction of OTR profile with hosts and expect it will crash.
EXPECT_DEATH(ProfileDestroyer::DestroyPendingProfilesForShutdown(), "");
}
#endif // defined(GTEST_HAS_DEATH_TEST)
INSTANTIATE_TEST_SUITE_P(AllOTRProfileTypes,
ProfileDestroyerTest,
/*is_primary_otr=*/testing::Bool());
|