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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
|
// 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 "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/background/background_contents_service.h"
#include "chrome/browser/background/background_contents_service_factory.h"
#include "chrome/browser/background/background_contents_service_observer.h"
#include "chrome/browser/background/background_contents_test_waiter.h"
#include "chrome/browser/background/extensions/background_mode_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_browser_main_extra_parts_nacl_deprecation.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/extensions/application_launch.h"
#include "chrome/common/chrome_paths.h"
#include "components/embedder_support/switches.h"
#include "components/nacl/common/buildflags.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/process_manager.h"
#include "extensions/common/extension.h"
#include "extensions/common/switches.h"
#include "extensions/test/extension_test_message_listener.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "url/gurl.h"
#if BUILDFLAG(ENABLE_NACL)
#include "components/nacl/browser/nacl_process_host.h"
#endif
#if BUILDFLAG(IS_MAC)
#include "base/apple/scoped_nsautorelease_pool.h"
#endif
using extensions::Extension;
namespace {
class BackgroundContentsCreationObserver
: public BackgroundContentsServiceObserver {
public:
explicit BackgroundContentsCreationObserver(Profile* profile) {
observation_.Observe(
BackgroundContentsServiceFactory::GetForProfile(profile));
}
BackgroundContentsCreationObserver(
const BackgroundContentsCreationObserver&) = delete;
BackgroundContentsCreationObserver& operator=(
const BackgroundContentsCreationObserver&) = delete;
~BackgroundContentsCreationObserver() override = default;
void OnBackgroundContentsOpened(
const BackgroundContentsOpenedDetails& details) override {
++opens_;
}
int opens() const { return opens_; }
private:
// The number of background contents that have been opened since creation.
int opens_ = 0;
base::ScopedObservation<BackgroundContentsService,
BackgroundContentsServiceObserver>
observation_{this};
};
class BackgroundContentsClosedObserver
: protected BackgroundContentsServiceObserver {
public:
explicit BackgroundContentsClosedObserver(Profile* profile) {
observation_.Observe(
BackgroundContentsServiceFactory::GetForProfile(profile));
}
BackgroundContentsClosedObserver(const BackgroundContentsClosedObserver&) =
delete;
BackgroundContentsClosedObserver& operator=(
const BackgroundContentsClosedObserver&) = delete;
~BackgroundContentsClosedObserver() override = default;
void Wait() { run_loop_.Run(); }
protected:
void OnBackgroundContentsClosed() override { run_loop_.Quit(); }
private:
base::RunLoop run_loop_;
base::ScopedObservation<BackgroundContentsService,
BackgroundContentsServiceObserver>
observation_{this};
};
} // namespace
class AppBackgroundPageApiTest : public extensions::ExtensionApiTest {
public:
void SetUpCommandLine(base::CommandLine* command_line) override {
extensions::ExtensionApiTest::SetUpCommandLine(command_line);
command_line->AppendSwitch(embedder_support::kDisablePopupBlocking);
command_line->AppendSwitch(extensions::switches::kAllowHTTPBackgroundPage);
}
void SetUpOnMainThread() override {
extensions::ExtensionApiTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(StartEmbeddedTestServer());
}
bool CreateApp(const std::string& app_manifest,
base::FilePath* app_dir) {
base::ScopedAllowBlockingForTesting allow_blocking;
if (!app_dir_.CreateUniqueTempDir()) {
LOG(ERROR) << "Unable to create a temporary directory.";
return false;
}
base::FilePath manifest_path =
app_dir_.GetPath().AppendASCII("manifest.json");
if (!base::WriteFile(manifest_path, app_manifest)) {
LOG(ERROR) << "Unable to write manifest to file.";
return false;
}
*app_dir = app_dir_.GetPath();
return true;
}
bool VerifyBackgroundMode(bool expected_background_mode) {
#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
BackgroundModeManager* manager =
g_browser_process->background_mode_manager();
if (manager && manager->IsBackgroundModePrefEnabled()) {
return manager->IsBackgroundModeActive() == expected_background_mode;
}
#endif
// If background mode is disabled on this platform (e.g. cros), then skip
// this check.
DLOG(WARNING) << "Skipping check - background mode disabled";
return true;
}
void UnloadExtensionViaTask(const std::string& id) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&AppBackgroundPageApiTest::UnloadExtension,
base::Unretained(this), id));
}
private:
base::ScopedTempDir app_dir_;
};
namespace {
// Fixture to assist in testing v2 app background pages containing
// Native Client embeds.
class AppBackgroundPageNaClTest : public AppBackgroundPageApiTest {
public:
AppBackgroundPageNaClTest() : extension_(nullptr) {
feature_list_.InitAndEnableFeature(kNaclAllow);
}
~AppBackgroundPageNaClTest() override = default;
void SetUpOnMainThread() override {
AppBackgroundPageApiTest::SetUpOnMainThread();
extensions::ProcessManager::SetEventPageIdleTimeForTesting(1000);
extensions::ProcessManager::SetEventPageSuspendingTimeForTesting(1000);
}
const Extension* extension() { return extension_; }
protected:
void LaunchTestingApp() {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath app_dir;
base::PathService::Get(chrome::DIR_GEN_TEST_DATA, &app_dir);
app_dir = app_dir.AppendASCII(
"ppapi/tests/extensions/background_keepalive/newlib");
extension_ = LoadExtension(app_dir);
ASSERT_TRUE(extension_);
}
private:
raw_ptr<const Extension> extension_;
base::test::ScopedFeatureList feature_list_;
};
} // namespace
// This test is meaningless if background mode is not enabled.
#if BUILDFLAG(ENABLE_BACKGROUND_MODE)
IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, Basic) {
std::string app_manifest = base::StringPrintf(
"{"
" \"name\": \"App\","
" \"version\": \"0.1\","
" \"manifest_version\": 2,"
" \"app\": {"
" \"urls\": ["
" \"http://a.com/\""
" ],"
" \"launch\": {"
" \"web_url\": \"http://a.com:%u/\""
" }"
" },"
" \"permissions\": [\"background\"]"
"}",
embedded_test_server()->port());
base::FilePath app_dir;
BackgroundContentsClosedObserver closed_observer(browser()->profile());
ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
ASSERT_TRUE(LoadExtension(app_dir));
// Background mode should not be active until a background page is created.
ASSERT_TRUE(VerifyBackgroundMode(false));
ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_;
// The test closes the background contents, so we should fall back to no
// background mode at the end.
closed_observer.Wait();
EXPECT_TRUE(VerifyBackgroundMode(false));
}
#endif // BUILDFLAG(ENABLE_BACKGROUND_MODE)
IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, LacksPermission) {
std::string app_manifest = base::StringPrintf(
"{"
" \"name\": \"App\","
" \"version\": \"0.1\","
" \"manifest_version\": 2,"
" \"app\": {"
" \"urls\": ["
" \"http://a.com/\""
" ],"
" \"launch\": {"
" \"web_url\": \"http://a.com:%u/\""
" }"
" }"
"}",
embedded_test_server()->port());
base::FilePath app_dir;
ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
ASSERT_TRUE(LoadExtension(app_dir));
ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission"))
<< message_;
ASSERT_TRUE(VerifyBackgroundMode(false));
}
IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, ManifestBackgroundPage) {
std::string app_manifest = base::StringPrintf(
"{"
" \"name\": \"App\","
" \"version\": \"0.1\","
" \"manifest_version\": 2,"
" \"app\": {"
" \"urls\": ["
" \"http://a.com/\""
" ],"
" \"launch\": {"
" \"web_url\": \"http://a.com:%u/\""
" }"
" },"
" \"permissions\": [\"background\"],"
" \"background\": {"
" \"page\": \"http://a.com:%u/test.html\""
" }"
"}",
embedded_test_server()->port(),
embedded_test_server()->port());
BackgroundContentsTestWaiter background_waiter(profile());
base::FilePath app_dir;
ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
// Background mode should not be active now because no background app was
// loaded.
ASSERT_TRUE(LoadExtension(app_dir));
// Background mode be active now because a background page was created when
// the app was loaded.
ASSERT_TRUE(VerifyBackgroundMode(true));
// Wait for and then verify that the background contents exist.
const Extension* extension = GetSingleLoadedExtension();
background_waiter.WaitForBackgroundContents(extension->id());
BackgroundContents* background_contents =
BackgroundContentsServiceFactory::GetForProfile(browser()->profile())
->GetAppBackgroundContents((extension->id()));
ASSERT_TRUE(background_contents);
// Verify that window.opener in the background contents is not set when
// creating the background page through the manifest (not through
// window.open).
EXPECT_FALSE(background_contents->web_contents()->GetOpener());
EXPECT_EQ(true, content::EvalJs(background_contents->web_contents(),
"window.opener == null;"));
UnloadExtension(extension->id());
}
IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsBackgroundPage) {
// Keep the task manager up through this test to verify that a crash doesn't
// happen when window.open creates a background page that switches
// RenderViewHosts. See http://crbug.com/165138.
chrome::ShowTaskManager(browser());
BackgroundContentsCreationObserver creation_observer(browser()->profile());
std::string app_manifest = base::StringPrintf(
"{"
" \"name\": \"App\","
" \"version\": \"0.1\","
" \"manifest_version\": 2,"
" \"app\": {"
" \"urls\": ["
" \"http://a.com/\""
" ],"
" \"launch\": {"
" \"web_url\": \"http://a.com:%u/test.html\""
" }"
" },"
" \"permissions\": [\"background\"],"
" \"background\": {"
" \"allow_js_access\": false"
" }"
"}",
embedded_test_server()->port());
base::FilePath app_dir;
ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
ASSERT_TRUE(LoadExtension(app_dir));
// There isn't a background page loaded initially.
const Extension* extension = GetSingleLoadedExtension();
ASSERT_FALSE(
BackgroundContentsServiceFactory::GetForProfile(browser()->profile())
->GetAppBackgroundContents(extension->id()));
BackgroundContentsTestWaiter background_waiter(profile());
// The test makes sure that window.open returns null.
ASSERT_TRUE(RunExtensionTest("app_background_page/no_js")) << message_;
background_waiter.WaitForBackgroundContents(extension->id());
// And after it runs there should be a background page.
BackgroundContents* background_contents =
BackgroundContentsServiceFactory::GetForProfile(browser()->profile())
->GetAppBackgroundContents((extension->id()));
ASSERT_TRUE(background_contents);
// Verify that window.opener in the background contents is not set when
// allow_js_access=false.
EXPECT_FALSE(background_contents->web_contents()->GetOpener());
EXPECT_EQ(true, content::EvalJs(background_contents->web_contents(),
"window.opener == null;"));
// Verify multiple BackgroundContents don't get opened despite multiple
// window.open calls.
EXPECT_EQ(1, creation_observer.opens());
UnloadExtension(extension->id());
}
IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsManifestBackgroundPage) {
std::string app_manifest = base::StringPrintf(
"{"
" \"name\": \"App\","
" \"version\": \"0.1\","
" \"manifest_version\": 2,"
" \"app\": {"
" \"urls\": ["
" \"http://a.com/\""
" ],"
" \"launch\": {"
" \"web_url\": \"http://a.com:%u/\""
" }"
" },"
" \"permissions\": [\"background\"],"
" \"background\": {"
" \"page\": \"http://a.com:%u/bg.html\","
" \"allow_js_access\": false"
" }"
"}",
embedded_test_server()->port(),
embedded_test_server()->port());
base::FilePath app_dir;
ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
ASSERT_TRUE(LoadExtension(app_dir));
// The background page should load.
BackgroundContentsTestWaiter background_waiter(profile());
const Extension* extension = GetSingleLoadedExtension();
background_waiter.WaitForBackgroundContents(extension->id());
BackgroundContents* background_contents =
BackgroundContentsServiceFactory::GetForProfile(browser()->profile())
->GetAppBackgroundContents((extension->id()));
ASSERT_TRUE(background_contents);
// Verify that window.opener in the background contents is not set when
// creating the background page through the manifest (not through
// window.open).
EXPECT_FALSE(background_contents->web_contents()->GetOpener());
EXPECT_EQ(true, content::EvalJs(background_contents->web_contents(),
"window.opener == null;"));
// window.open should return null.
ASSERT_TRUE(RunExtensionTest("app_background_page/no_js_manifest")) <<
message_;
// Verify that window.opener in the background contents is still not set.
EXPECT_FALSE(background_contents->web_contents()->GetOpener());
EXPECT_EQ(true, content::EvalJs(background_contents->web_contents(),
"window.opener == null;"));
UnloadExtension(extension->id());
}
IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoBackgroundPages) {
std::string app_manifest = base::StringPrintf(
"{"
" \"name\": \"App\","
" \"version\": \"0.1\","
" \"manifest_version\": 2,"
" \"app\": {"
" \"urls\": ["
" \"http://a.com/\""
" ],"
" \"launch\": {"
" \"web_url\": \"http://a.com:%u/\""
" }"
" },"
" \"permissions\": [\"background\"]"
"}",
embedded_test_server()->port());
base::FilePath app_dir;
ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
ASSERT_TRUE(LoadExtension(app_dir));
const Extension* extension = GetSingleLoadedExtension();
ASSERT_TRUE(RunExtensionTest("app_background_page/two_pages")) << message_;
UnloadExtension(extension->id());
}
IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoPagesWithManifest) {
std::string app_manifest = base::StringPrintf(
"{"
" \"name\": \"App\","
" \"version\": \"0.1\","
" \"manifest_version\": 2,"
" \"app\": {"
" \"urls\": ["
" \"http://a.com/\""
" ],"
" \"launch\": {"
" \"web_url\": \"http://a.com:%u/\""
" }"
" },"
" \"background\": {"
" \"page\": \"http://a.com:%u/bg.html\""
" },"
" \"permissions\": [\"background\"]"
"}",
embedded_test_server()->port(),
embedded_test_server()->port());
base::FilePath app_dir;
ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
ASSERT_TRUE(LoadExtension(app_dir));
const Extension* extension = GetSingleLoadedExtension();
ASSERT_TRUE(RunExtensionTest("app_background_page/two_with_manifest")) <<
message_;
UnloadExtension(extension->id());
}
IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenPopupFromBGPage) {
std::string app_manifest = base::StringPrintf(
"{"
" \"name\": \"App\","
" \"version\": \"0.1\","
" \"manifest_version\": 2,"
" \"app\": {"
" \"urls\": ["
" \"http://a.com/\""
" ],"
" \"launch\": {"
" \"web_url\": \"http://a.com:%u/\""
" }"
" },"
" \"background\": { \"page\": \"http://a.com:%u/extensions/api_test/"
"app_background_page/bg_open/bg_open_bg.html\" },"
" \"permissions\": [\"background\"]"
"}",
embedded_test_server()->port(),
embedded_test_server()->port());
base::FilePath app_dir;
ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
ASSERT_TRUE(LoadExtension(app_dir));
// The background page should load.
BackgroundContentsTestWaiter background_waiter(profile());
const Extension* extension = GetSingleLoadedExtension();
background_waiter.WaitForBackgroundContents(extension->id());
ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_;
}
// Partly a regression test for crbug.com/756465. Namely, that window.open
// correctly matches an app URL with a path component.
// Flaky on Chrome OS https://crbug.com/1462141.
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_OpenThenClose DISABLED_OpenThenClose
#else
#define MAYBE_OpenThenClose OpenThenClose
#endif
IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, MAYBE_OpenThenClose) {
std::string app_manifest = base::StringPrintf(
"{"
" \"name\": \"App\","
" \"version\": \"0.1\","
" \"manifest_version\": 2,"
" \"app\": {"
" \"urls\": ["
" \"http://a.com/extensions/api_test\""
" ],"
" \"launch\": {"
" \"web_url\": \"http://a.com:%u/extensions/api_test\""
" }"
" },"
" \"permissions\": [\"background\"]"
"}",
embedded_test_server()->port());
base::FilePath app_dir;
ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
ASSERT_TRUE(LoadExtension(app_dir));
// There isn't a background page loaded initially.
const Extension* extension = GetSingleLoadedExtension();
ASSERT_FALSE(
BackgroundContentsServiceFactory::GetForProfile(browser()->profile())
->GetAppBackgroundContents(extension->id()));
// Background mode should not be active until a background page is created.
ASSERT_TRUE(VerifyBackgroundMode(false));
BackgroundContentsTestWaiter background_waiter(profile());
ASSERT_TRUE(RunExtensionTest("app_background_page/basic_open")) << message_;
// Background mode should be active now because a background page was created.
ASSERT_TRUE(VerifyBackgroundMode(true));
background_waiter.WaitForBackgroundContents(extension->id());
// Verify that the background contents exist.
BackgroundContents* background_contents =
BackgroundContentsServiceFactory::GetForProfile(browser()->profile())
->GetAppBackgroundContents((extension->id()));
ASSERT_TRUE(background_contents);
// Verify that window.opener in the background contents is set.
content::RenderFrameHost* background_opener =
background_contents->web_contents()->GetOpener();
ASSERT_TRUE(background_opener);
std::string window_opener_href =
content::EvalJs(background_contents->web_contents(),
"window.opener.location.href;")
.ExtractString();
EXPECT_EQ(window_opener_href,
background_opener->GetLastCommittedURL().spec());
// Now close the BackgroundContents.
ASSERT_TRUE(RunExtensionTest("app_background_page/basic_close")) << message_;
// Background mode should no longer be active.
ASSERT_TRUE(VerifyBackgroundMode(false));
ASSERT_FALSE(
BackgroundContentsServiceFactory::GetForProfile(browser()->profile())
->GetAppBackgroundContents(extension->id()));
}
IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, UnloadExtensionWhileHidden) {
std::string app_manifest = base::StringPrintf(
"{"
" \"name\": \"App\","
" \"version\": \"0.1\","
" \"manifest_version\": 2,"
" \"app\": {"
" \"urls\": ["
" \"http://a.com/\""
" ],"
" \"launch\": {"
" \"web_url\": \"http://a.com:%u/\""
" }"
" },"
" \"permissions\": [\"background\"],"
" \"background\": {"
" \"page\": \"http://a.com:%u/test.html\""
" }"
"}",
embedded_test_server()->port(),
embedded_test_server()->port());
base::FilePath app_dir;
ASSERT_TRUE(CreateApp(app_manifest, &app_dir));
auto background_waiter =
std::make_unique<BackgroundContentsTestWaiter>(profile());
// Background mode should not be active now because no background app was
// loaded.
ASSERT_TRUE(LoadExtension(app_dir));
// Background mode be active now because a background page was created when
// the app was loaded.
ASSERT_TRUE(VerifyBackgroundMode(true));
const Extension* extension = GetSingleLoadedExtension();
background_waiter->WaitForBackgroundContents(extension->id());
ASSERT_TRUE(
BackgroundContentsServiceFactory::GetForProfile(browser()->profile())
->GetAppBackgroundContents(extension->id()));
// We need to ensure `background_waiter` gets freed before the call to
// UnloadExtensionViaTask(), otherwise we'll get a dangling raw_ptr warning.
background_waiter.reset();
// Close all browsers - app should continue running.
set_exit_when_last_browser_closes(false);
CloseBrowserSynchronously(browser());
// Post a task to unload the extension - this should cause Chrome to exit
// cleanly (not crash).
UnloadExtensionViaTask(extension->id());
content::RunAllPendingInMessageLoop();
ASSERT_TRUE(VerifyBackgroundMode(false));
}
#if BUILDFLAG(ENABLE_NACL)
// Verify that active NaCl embeds raise the keepalive count.
IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest, BackgroundKeepaliveActive) {
extensions::ProcessManager* manager =
extensions::ProcessManager::Get(browser()->profile());
ExtensionTestMessageListener ready_listener("ready",
ReplyBehavior::kWillReply);
LaunchTestingApp();
EXPECT_TRUE(ready_listener.WaitUntilSatisfied());
const auto api_activity = std::make_pair(extensions::Activity::API_FUNCTION,
std::string("test.sendMessage"));
const auto pepper_api_activity =
std::make_pair(extensions::Activity::PEPPER_API, std::string());
// When the app calls chrome.test.sendMessage() the keepalive count stays
// incremented until the call completes (i.e. until we call Reply() below).
// So between WaitUntilSatisfied() and Reply(), we know that the count must
// be in the incremented state, and in this case that is the only
// contributor to the keepalive count.
EXPECT_EQ(1, manager->GetLazyKeepaliveCount(extension()));
extensions::ProcessManager::ActivitiesMultiset activities =
manager->GetLazyKeepaliveActivities(extension());
EXPECT_THAT(activities, testing::UnorderedElementsAre(api_activity));
ExtensionTestMessageListener created1_listener("created_module:1",
ReplyBehavior::kWillReply);
ready_listener.Reply("create_module");
EXPECT_TRUE(created1_listener.WaitUntilSatisfied());
// Now chrome.test.sendMessage() is incrementing the keepalive count, but
// there is also a Native Client module active, incrementing it again.
EXPECT_EQ(2, manager->GetLazyKeepaliveCount(extension()));
activities = manager->GetLazyKeepaliveActivities(extension());
EXPECT_THAT(activities,
testing::UnorderedElementsAre(api_activity, pepper_api_activity));
ExtensionTestMessageListener created2_listener("created_module:2",
ReplyBehavior::kWillReply);
created1_listener.Reply("create_module");
EXPECT_TRUE(created2_listener.WaitUntilSatisfied());
// Keepalive comes from chrome.test.sendMessage, plus two modules.
EXPECT_EQ(3, manager->GetLazyKeepaliveCount(extension()));
activities = manager->GetLazyKeepaliveActivities(extension());
EXPECT_EQ(3u, activities.size());
EXPECT_THAT(activities,
testing::UnorderedElementsAre(api_activity, pepper_api_activity,
pepper_api_activity));
// Tear-down both modules.
ExtensionTestMessageListener destroyed1_listener("destroyed_module",
ReplyBehavior::kWillReply);
created2_listener.Reply("destroy_module");
EXPECT_TRUE(destroyed1_listener.WaitUntilSatisfied());
ExtensionTestMessageListener destroyed2_listener("destroyed_module");
destroyed1_listener.Reply("destroy_module");
EXPECT_TRUE(destroyed2_listener.WaitUntilSatisfied());
// Both modules are gone, and no sendMessage API reply is pending (since the
// last listener has the |will_reply| flag set to |false|).
EXPECT_EQ(0, manager->GetLazyKeepaliveCount(extension()));
activities = manager->GetLazyKeepaliveActivities(extension());
EXPECT_TRUE(activities.empty());
}
#endif // BUILDFLAG(ENABLE_NACL)
|