1 // Copyright (c) 2012 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.
5 #include "base/location.h"
6 #include "base/path_service.h"
7 #include "base/single_thread_task_runner.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/background/background_contents_service.h"
12 #include "chrome/browser/background/background_contents_service_factory.h"
13 #include "chrome/browser/background/background_mode_manager.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/extensions/extension_apitest.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/task_manager/task_manager_browsertest_util.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/browser_dialogs.h"
21 #include "chrome/browser/ui/browser_window.h"
22 #include "chrome/browser/ui/extensions/application_launch.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/test/test_notification_tracker.h"
27 #include "content/public/test/test_utils.h"
28 #include "extensions/browser/process_manager.h"
29 #include "extensions/common/extension.h"
30 #include "extensions/common/switches.h"
31 #include "extensions/test/extension_test_message_listener.h"
32 #include "net/dns/mock_host_resolver.h"
33 #include "net/test/embedded_test_server/embedded_test_server.h"
35 #if !defined(DISABLE_NACL)
36 #include "components/nacl/browser/nacl_process_host.h"
39 #if defined(OS_MACOSX)
40 #include "base/mac/scoped_nsautorelease_pool.h"
43 using base::ASCIIToUTF16
;
44 using extensions::Extension
;
46 class AppBackgroundPageApiTest
: public ExtensionApiTest
{
48 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
49 ExtensionApiTest::SetUpCommandLine(command_line
);
50 command_line
->AppendSwitch(switches::kDisablePopupBlocking
);
51 command_line
->AppendSwitch(extensions::switches::kAllowHTTPBackgroundPage
);
54 bool CreateApp(const std::string
& app_manifest
,
55 base::FilePath
* app_dir
) {
56 if (!app_dir_
.CreateUniqueTempDir()) {
57 LOG(ERROR
) << "Unable to create a temporary directory.";
60 base::FilePath manifest_path
= app_dir_
.path().AppendASCII("manifest.json");
61 int bytes_written
= base::WriteFile(manifest_path
,
64 if (bytes_written
!= static_cast<int>(app_manifest
.size())) {
65 LOG(ERROR
) << "Unable to write complete manifest to file. Return code="
69 *app_dir
= app_dir_
.path();
73 bool WaitForBackgroundMode(bool expected_background_mode
) {
74 #if defined(OS_CHROMEOS)
75 // BackgroundMode is not supported on chromeos, so we should test the
76 // behavior of BackgroundContents, but not the background mode state itself.
79 BackgroundModeManager
* manager
=
80 g_browser_process
->background_mode_manager();
81 // If background mode is disabled on this platform (e.g. cros), then skip
83 if (!manager
|| !manager
->IsBackgroundModePrefEnabled()) {
84 DLOG(WARNING
) << "Skipping check - background mode disabled";
87 if (manager
->IsBackgroundModeActive() == expected_background_mode
)
90 // We are not currently in the expected state - wait for the state to
92 content::WindowedNotificationObserver
watcher(
93 chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED
,
94 content::NotificationService::AllSources());
96 return manager
->IsBackgroundModeActive() == expected_background_mode
;
100 void UnloadExtensionViaTask(const std::string
& id
) {
101 base::ThreadTaskRunnerHandle::Get()->PostTask(
103 base::Bind(&AppBackgroundPageApiTest::UnloadExtension
, this, id
));
107 base::ScopedTempDir app_dir_
;
112 // Fixture to assist in testing v2 app background pages containing
113 // Native Client embeds.
114 class AppBackgroundPageNaClTest
: public AppBackgroundPageApiTest
{
116 AppBackgroundPageNaClTest()
117 : extension_(NULL
) {}
118 ~AppBackgroundPageNaClTest() override
{}
120 void SetUpOnMainThread() override
{
121 AppBackgroundPageApiTest::SetUpOnMainThread();
122 #if !defined(DISABLE_NACL)
123 nacl::NaClProcessHost::SetPpapiKeepAliveThrottleForTesting(50);
125 extensions::ProcessManager::SetEventPageIdleTimeForTesting(1000);
126 extensions::ProcessManager::SetEventPageSuspendingTimeForTesting(1000);
129 const Extension
* extension() { return extension_
; }
132 void LaunchTestingApp() {
133 base::FilePath app_dir
;
134 PathService::Get(chrome::DIR_GEN_TEST_DATA
, &app_dir
);
135 app_dir
= app_dir
.AppendASCII(
136 "ppapi/tests/extensions/background_keepalive/newlib");
137 extension_
= LoadExtension(app_dir
);
138 ASSERT_TRUE(extension_
);
142 const Extension
* extension_
;
145 // Produces an extensions::ProcessManager::ImpulseCallbackForTesting callback
146 // that will match a specified goal and can be waited on.
147 class ImpulseCallbackCounter
{
149 explicit ImpulseCallbackCounter(extensions::ProcessManager
* manager
,
150 const std::string
& extension_id
)
154 extension_id_(extension_id
) {
157 extensions::ProcessManager::ImpulseCallbackForTesting
158 SetGoalAndGetCallback(int goal
) {
161 message_loop_runner_
= new content::MessageLoopRunner();
162 return base::Bind(&ImpulseCallbackCounter::ImpulseCallback
,
163 base::Unretained(this),
164 message_loop_runner_
->QuitClosure(),
169 message_loop_runner_
->Run();
172 void ImpulseCallback(
173 const base::Closure
& quit_callback
,
174 const std::string
& extension_id_from_test
,
175 const std::string
& extension_id_from_manager
) {
176 if (extension_id_from_test
== extension_id_from_manager
) {
177 if (++observed_
>= goal_
) {
178 // Clear callback to free reference to message loop.
179 manager_
->SetKeepaliveImpulseCallbackForTesting(
180 extensions::ProcessManager::ImpulseCallbackForTesting());
181 manager_
->SetKeepaliveImpulseDecrementCallbackForTesting(
182 extensions::ProcessManager::ImpulseCallbackForTesting());
190 extensions::ProcessManager
* manager_
;
191 const std::string extension_id_
;
192 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
197 // Disable on Mac only. http://crbug.com/95139
198 #if defined(OS_MACOSX)
199 #define MAYBE_Basic DISABLED_Basic
201 #define MAYBE_Basic Basic
204 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest
, MAYBE_Basic
) {
205 host_resolver()->AddRule("a.com", "127.0.0.1");
206 ASSERT_TRUE(StartEmbeddedTestServer());
208 std::string app_manifest
= base::StringPrintf(
210 " \"name\": \"App\","
211 " \"version\": \"0.1\","
212 " \"manifest_version\": 2,"
218 " \"web_url\": \"http://a.com:%u/\""
221 " \"permissions\": [\"background\"]"
223 embedded_test_server()->port());
225 base::FilePath app_dir
;
226 ASSERT_TRUE(CreateApp(app_manifest
, &app_dir
));
227 ASSERT_TRUE(LoadExtension(app_dir
));
228 // Background mode should not be active until a background page is created.
229 ASSERT_TRUE(WaitForBackgroundMode(false));
230 ASSERT_TRUE(RunExtensionTest("app_background_page/basic")) << message_
;
231 // The test closes the background contents, so we should fall back to no
232 // background mode at the end.
233 ASSERT_TRUE(WaitForBackgroundMode(false));
236 // Crashy, http://crbug.com/69215.
237 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest
, DISABLED_LacksPermission
) {
238 host_resolver()->AddRule("a.com", "127.0.0.1");
239 ASSERT_TRUE(StartEmbeddedTestServer());
241 std::string app_manifest
= base::StringPrintf(
243 " \"name\": \"App\","
244 " \"version\": \"0.1\","
245 " \"manifest_version\": 2,"
251 " \"web_url\": \"http://a.com:%u/\""
255 embedded_test_server()->port());
257 base::FilePath app_dir
;
258 ASSERT_TRUE(CreateApp(app_manifest
, &app_dir
));
259 ASSERT_TRUE(LoadExtension(app_dir
));
260 ASSERT_TRUE(RunExtensionTest("app_background_page/lacks_permission"))
262 ASSERT_TRUE(WaitForBackgroundMode(false));
265 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest
, ManifestBackgroundPage
) {
266 host_resolver()->AddRule("a.com", "127.0.0.1");
267 ASSERT_TRUE(StartEmbeddedTestServer());
269 std::string app_manifest
= base::StringPrintf(
271 " \"name\": \"App\","
272 " \"version\": \"0.1\","
273 " \"manifest_version\": 2,"
279 " \"web_url\": \"http://a.com:%u/\""
282 " \"permissions\": [\"background\"],"
284 " \"page\": \"http://a.com:%u/test.html\""
287 embedded_test_server()->port(),
288 embedded_test_server()->port());
290 base::FilePath app_dir
;
291 ASSERT_TRUE(CreateApp(app_manifest
, &app_dir
));
292 // Background mode should not be active now because no background app was
294 ASSERT_TRUE(LoadExtension(app_dir
));
295 // Background mode be active now because a background page was created when
296 // the app was loaded.
297 ASSERT_TRUE(WaitForBackgroundMode(true));
299 const Extension
* extension
= GetSingleLoadedExtension();
301 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
302 GetAppBackgroundContents(ASCIIToUTF16(extension
->id())));
303 UnloadExtension(extension
->id());
306 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest
, NoJsBackgroundPage
) {
307 // Keep the task manager up through this test to verify that a crash doesn't
308 // happen when window.open creates a background page that switches
309 // RenderViewHosts. See http://crbug.com/165138.
310 // This test is for the old implementation of the task manager. We must
311 // explicitly disable the new one.
312 task_manager::browsertest_util::EnableOldTaskManager();
313 chrome::ShowTaskManager(browser());
315 // Make sure that no BackgroundContentses get deleted (a signal that repeated
316 // window.open calls recreate instances, instead of being no-ops).
317 content::TestNotificationTracker background_deleted_tracker
;
318 background_deleted_tracker
.ListenFor(
319 chrome::NOTIFICATION_BACKGROUND_CONTENTS_DELETED
,
320 content::Source
<Profile
>(browser()->profile()));
322 host_resolver()->AddRule("a.com", "127.0.0.1");
323 ASSERT_TRUE(StartEmbeddedTestServer());
325 std::string app_manifest
= base::StringPrintf(
327 " \"name\": \"App\","
328 " \"version\": \"0.1\","
329 " \"manifest_version\": 2,"
335 " \"web_url\": \"http://a.com:%u/test.html\""
338 " \"permissions\": [\"background\"],"
340 " \"allow_js_access\": false"
343 embedded_test_server()->port());
345 base::FilePath app_dir
;
346 ASSERT_TRUE(CreateApp(app_manifest
, &app_dir
));
347 ASSERT_TRUE(LoadExtension(app_dir
));
349 // There isn't a background page loaded initially.
350 const Extension
* extension
= GetSingleLoadedExtension();
352 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
353 GetAppBackgroundContents(ASCIIToUTF16(extension
->id())));
354 // The test makes sure that window.open returns null.
355 ASSERT_TRUE(RunExtensionTest("app_background_page/no_js")) << message_
;
356 // And after it runs there should be a background page.
358 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
359 GetAppBackgroundContents(ASCIIToUTF16(extension
->id())));
361 EXPECT_EQ(0u, background_deleted_tracker
.size());
362 UnloadExtension(extension
->id());
365 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest
, NoJsManifestBackgroundPage
) {
366 host_resolver()->AddRule("a.com", "127.0.0.1");
367 ASSERT_TRUE(StartEmbeddedTestServer());
369 std::string app_manifest
= base::StringPrintf(
371 " \"name\": \"App\","
372 " \"version\": \"0.1\","
373 " \"manifest_version\": 2,"
379 " \"web_url\": \"http://a.com:%u/\""
382 " \"permissions\": [\"background\"],"
384 " \"page\": \"http://a.com:%u/bg.html\","
385 " \"allow_js_access\": false"
388 embedded_test_server()->port(),
389 embedded_test_server()->port());
391 base::FilePath app_dir
;
392 ASSERT_TRUE(CreateApp(app_manifest
, &app_dir
));
393 ASSERT_TRUE(LoadExtension(app_dir
));
395 // The background page should load, but window.open should return null.
396 const Extension
* extension
= GetSingleLoadedExtension();
398 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
399 GetAppBackgroundContents(ASCIIToUTF16(extension
->id())));
400 ASSERT_TRUE(RunExtensionTest("app_background_page/no_js_manifest")) <<
402 UnloadExtension(extension
->id());
405 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest
, OpenTwoBackgroundPages
) {
406 host_resolver()->AddRule("a.com", "127.0.0.1");
407 ASSERT_TRUE(StartEmbeddedTestServer());
409 std::string app_manifest
= base::StringPrintf(
411 " \"name\": \"App\","
412 " \"version\": \"0.1\","
413 " \"manifest_version\": 2,"
419 " \"web_url\": \"http://a.com:%u/\""
422 " \"permissions\": [\"background\"]"
424 embedded_test_server()->port());
426 base::FilePath app_dir
;
427 ASSERT_TRUE(CreateApp(app_manifest
, &app_dir
));
428 ASSERT_TRUE(LoadExtension(app_dir
));
429 const Extension
* extension
= GetSingleLoadedExtension();
430 ASSERT_TRUE(RunExtensionTest("app_background_page/two_pages")) << message_
;
431 UnloadExtension(extension
->id());
434 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest
, OpenTwoPagesWithManifest
) {
435 host_resolver()->AddRule("a.com", "127.0.0.1");
436 ASSERT_TRUE(StartEmbeddedTestServer());
438 std::string app_manifest
= base::StringPrintf(
440 " \"name\": \"App\","
441 " \"version\": \"0.1\","
442 " \"manifest_version\": 2,"
448 " \"web_url\": \"http://a.com:%u/\""
452 " \"page\": \"http://a.com:%u/bg.html\""
454 " \"permissions\": [\"background\"]"
456 embedded_test_server()->port(),
457 embedded_test_server()->port());
459 base::FilePath app_dir
;
460 ASSERT_TRUE(CreateApp(app_manifest
, &app_dir
));
461 ASSERT_TRUE(LoadExtension(app_dir
));
462 const Extension
* extension
= GetSingleLoadedExtension();
463 ASSERT_TRUE(RunExtensionTest("app_background_page/two_with_manifest")) <<
465 UnloadExtension(extension
->id());
468 // Times out occasionally -- see crbug.com/108493
469 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest
, DISABLED_OpenPopupFromBGPage
) {
470 host_resolver()->AddRule("a.com", "127.0.0.1");
471 ASSERT_TRUE(StartEmbeddedTestServer());
473 std::string app_manifest
= base::StringPrintf(
475 " \"name\": \"App\","
476 " \"version\": \"0.1\","
477 " \"manifest_version\": 2,"
483 " \"web_url\": \"http://a.com:%u/\""
486 " \"background\": { \"page\": \"http://a.com:%u/extensions/api_test/"
487 "app_background_page/bg_open/bg_open_bg.html\" },"
488 " \"permissions\": [\"background\"]"
490 embedded_test_server()->port(),
491 embedded_test_server()->port());
493 base::FilePath app_dir
;
494 ASSERT_TRUE(CreateApp(app_manifest
, &app_dir
));
495 ASSERT_TRUE(LoadExtension(app_dir
));
496 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_
;
499 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest
, DISABLED_OpenThenClose
) {
500 host_resolver()->AddRule("a.com", "127.0.0.1");
501 ASSERT_TRUE(StartEmbeddedTestServer());
503 std::string app_manifest
= base::StringPrintf(
505 " \"name\": \"App\","
506 " \"version\": \"0.1\","
507 " \"manifest_version\": 2,"
513 " \"web_url\": \"http://a.com:%u/\""
516 " \"permissions\": [\"background\"]"
518 embedded_test_server()->port());
520 base::FilePath app_dir
;
521 ASSERT_TRUE(CreateApp(app_manifest
, &app_dir
));
522 ASSERT_TRUE(LoadExtension(app_dir
));
523 // There isn't a background page loaded initially.
524 const Extension
* extension
= GetSingleLoadedExtension();
526 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
527 GetAppBackgroundContents(ASCIIToUTF16(extension
->id())));
528 // Background mode should not be active until a background page is created.
529 ASSERT_TRUE(WaitForBackgroundMode(false));
530 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_open")) << message_
;
531 // Background mode should be active now because a background page was created.
532 ASSERT_TRUE(WaitForBackgroundMode(true));
534 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
535 GetAppBackgroundContents(ASCIIToUTF16(extension
->id())));
536 // Now close the BackgroundContents.
537 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_close")) << message_
;
538 // Background mode should no longer be active.
539 ASSERT_TRUE(WaitForBackgroundMode(false));
541 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
542 GetAppBackgroundContents(ASCIIToUTF16(extension
->id())));
545 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest
, UnloadExtensionWhileHidden
) {
546 host_resolver()->AddRule("a.com", "127.0.0.1");
547 ASSERT_TRUE(StartEmbeddedTestServer());
549 std::string app_manifest
= base::StringPrintf(
551 " \"name\": \"App\","
552 " \"version\": \"0.1\","
553 " \"manifest_version\": 2,"
559 " \"web_url\": \"http://a.com:%u/\""
562 " \"permissions\": [\"background\"],"
564 " \"page\": \"http://a.com:%u/test.html\""
567 embedded_test_server()->port(),
568 embedded_test_server()->port());
570 base::FilePath app_dir
;
571 ASSERT_TRUE(CreateApp(app_manifest
, &app_dir
));
572 // Background mode should not be active now because no background app was
574 ASSERT_TRUE(LoadExtension(app_dir
));
575 // Background mode be active now because a background page was created when
576 // the app was loaded.
577 ASSERT_TRUE(WaitForBackgroundMode(true));
579 const Extension
* extension
= GetSingleLoadedExtension();
581 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())->
582 GetAppBackgroundContents(ASCIIToUTF16(extension
->id())));
584 // Close all browsers - app should continue running.
585 set_exit_when_last_browser_closes(false);
586 CloseBrowserSynchronously(browser());
588 // Post a task to unload the extension - this should cause Chrome to exit
589 // cleanly (not crash).
590 UnloadExtensionViaTask(extension
->id());
591 content::RunAllPendingInMessageLoop();
592 ASSERT_TRUE(WaitForBackgroundMode(false));
595 // Verify active NaCl embeds cause many keepalive impulses to be sent.
596 // Disabled on Windows due to flakiness: http://crbug.com/346278
598 #define MAYBE_BackgroundKeepaliveActive DISABLED_BackgroundKeepaliveActive
600 // Disabling other platforms too since the test started failing
601 // consistently. http://crbug.com/490440
602 #define MAYBE_BackgroundKeepaliveActive DISABLED_BackgroundKeepaliveActive
604 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest
,
605 MAYBE_BackgroundKeepaliveActive
) {
606 #if !defined(DISABLE_NACL)
607 ExtensionTestMessageListener
nacl_modules_loaded("nacl_modules_loaded", true);
609 extensions::ProcessManager
* manager
=
610 extensions::ProcessManager::Get(browser()->profile());
611 ImpulseCallbackCounter
active_impulse_counter(manager
, extension()->id());
612 EXPECT_TRUE(nacl_modules_loaded
.WaitUntilSatisfied());
614 // Target .5 seconds: .5 seconds / 50ms throttle * 2 embeds == 20 impulses.
615 manager
->SetKeepaliveImpulseCallbackForTesting(
616 active_impulse_counter
.SetGoalAndGetCallback(20));
617 active_impulse_counter
.Wait();
621 // Verify that nacl modules that go idle will not send keepalive impulses.
622 // Disabled on windows due to Win XP failures:
623 // DesktopWindowTreeHostWin::HandleCreate not implemented. crbug.com/331954
625 #define MAYBE_BackgroundKeepaliveIdle DISABLED_BackgroundKeepaliveIdle
627 // ASAN errors appearing: https://crbug.com/332440
628 #define MAYBE_BackgroundKeepaliveIdle DISABLED_BackgroundKeepaliveIdle
630 IN_PROC_BROWSER_TEST_F(AppBackgroundPageNaClTest
,
631 MAYBE_BackgroundKeepaliveIdle
) {
632 #if !defined(DISABLE_NACL)
633 ExtensionTestMessageListener
nacl_modules_loaded("nacl_modules_loaded", true);
635 extensions::ProcessManager
* manager
=
636 extensions::ProcessManager::Get(browser()->profile());
637 ImpulseCallbackCounter
idle_impulse_counter(manager
, extension()->id());
638 EXPECT_TRUE(nacl_modules_loaded
.WaitUntilSatisfied());
640 manager
->SetKeepaliveImpulseDecrementCallbackForTesting(
641 idle_impulse_counter
.SetGoalAndGetCallback(1));
642 nacl_modules_loaded
.Reply("be idle");
643 idle_impulse_counter
.Wait();