OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/location.h" | 5 #include "base/location.h" |
6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
7 #include "base/single_thread_task_runner.h" | 7 #include "base/single_thread_task_runner.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "chrome/browser/background/background_contents_service.h" | 13 #include "chrome/browser/background/background_contents_service.h" |
14 #include "chrome/browser/background/background_contents_service_factory.h" | 14 #include "chrome/browser/background/background_contents_service_factory.h" |
15 #include "chrome/browser/background/background_mode_manager.h" | 15 #include "chrome/browser/background/background_mode_manager.h" |
16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
17 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
18 #include "chrome/browser/extensions/extension_apitest.h" | 18 #include "chrome/browser/extensions/extension_apitest.h" |
19 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
22 #include "chrome/browser/ui/browser_dialogs.h" | 22 #include "chrome/browser/ui/browser_dialogs.h" |
23 #include "chrome/browser/ui/browser_window.h" | 23 #include "chrome/browser/ui/browser_window.h" |
24 #include "chrome/browser/ui/extensions/application_launch.h" | 24 #include "chrome/browser/ui/extensions/application_launch.h" |
25 #include "chrome/common/chrome_paths.h" | 25 #include "chrome/common/chrome_paths.h" |
26 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
27 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
| 28 #include "content/public/browser/render_frame_host.h" |
| 29 #include "content/public/test/browser_test_utils.h" |
28 #include "content/public/test/test_notification_tracker.h" | 30 #include "content/public/test/test_notification_tracker.h" |
29 #include "content/public/test/test_utils.h" | 31 #include "content/public/test/test_utils.h" |
30 #include "extensions/browser/process_manager.h" | 32 #include "extensions/browser/process_manager.h" |
31 #include "extensions/common/extension.h" | 33 #include "extensions/common/extension.h" |
32 #include "extensions/common/switches.h" | 34 #include "extensions/common/switches.h" |
33 #include "extensions/test/extension_test_message_listener.h" | 35 #include "extensions/test/extension_test_message_listener.h" |
34 #include "net/dns/mock_host_resolver.h" | 36 #include "net/dns/mock_host_resolver.h" |
35 #include "net/test/embedded_test_server/embedded_test_server.h" | 37 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 38 #include "url/gurl.h" |
36 | 39 |
37 #if !defined(DISABLE_NACL) | 40 #if !defined(DISABLE_NACL) |
38 #include "components/nacl/browser/nacl_process_host.h" | 41 #include "components/nacl/browser/nacl_process_host.h" |
39 #endif | 42 #endif |
40 | 43 |
41 #if defined(OS_MACOSX) | 44 #if defined(OS_MACOSX) |
42 #include "base/mac/scoped_nsautorelease_pool.h" | 45 #include "base/mac/scoped_nsautorelease_pool.h" |
43 #endif | 46 #endif |
44 | 47 |
45 using base::ASCIIToUTF16; | 48 using base::ASCIIToUTF16; |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 | 241 |
239 base::FilePath app_dir; | 242 base::FilePath app_dir; |
240 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 243 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
241 // Background mode should not be active now because no background app was | 244 // Background mode should not be active now because no background app was |
242 // loaded. | 245 // loaded. |
243 ASSERT_TRUE(LoadExtension(app_dir)); | 246 ASSERT_TRUE(LoadExtension(app_dir)); |
244 // Background mode be active now because a background page was created when | 247 // Background mode be active now because a background page was created when |
245 // the app was loaded. | 248 // the app was loaded. |
246 ASSERT_TRUE(WaitForBackgroundMode(true)); | 249 ASSERT_TRUE(WaitForBackgroundMode(true)); |
247 | 250 |
| 251 // Verify that the background contents exist. |
248 const Extension* extension = GetSingleLoadedExtension(); | 252 const Extension* extension = GetSingleLoadedExtension(); |
249 ASSERT_TRUE( | 253 BackgroundContents* background_contents = |
250 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 254 BackgroundContentsServiceFactory::GetForProfile(browser()->profile()) |
251 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 255 ->GetAppBackgroundContents(ASCIIToUTF16(extension->id())); |
| 256 ASSERT_TRUE(background_contents); |
| 257 |
| 258 // Verify that window.opener in the background contents is not set when |
| 259 // creating the background page through the manifest (not through |
| 260 // window.open). |
| 261 EXPECT_FALSE(background_contents->web_contents()->GetOpener()); |
| 262 bool window_opener_null_in_js; |
| 263 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 264 background_contents->web_contents(), |
| 265 "domAutomationController.send(window.opener == null);", |
| 266 &window_opener_null_in_js)); |
| 267 EXPECT_TRUE(window_opener_null_in_js); |
| 268 |
252 UnloadExtension(extension->id()); | 269 UnloadExtension(extension->id()); |
253 } | 270 } |
254 | 271 |
255 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsBackgroundPage) { | 272 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsBackgroundPage) { |
256 // Keep the task manager up through this test to verify that a crash doesn't | 273 // Keep the task manager up through this test to verify that a crash doesn't |
257 // happen when window.open creates a background page that switches | 274 // happen when window.open creates a background page that switches |
258 // RenderViewHosts. See http://crbug.com/165138. | 275 // RenderViewHosts. See http://crbug.com/165138. |
259 chrome::ShowTaskManager(browser()); | 276 chrome::ShowTaskManager(browser()); |
260 | 277 |
261 // Make sure that no BackgroundContentses get deleted (a signal that repeated | 278 // Make sure that no BackgroundContentses get deleted (a signal that repeated |
(...skipping 27 matching lines...) Expand all Loading... |
289 ASSERT_TRUE(LoadExtension(app_dir)); | 306 ASSERT_TRUE(LoadExtension(app_dir)); |
290 | 307 |
291 // There isn't a background page loaded initially. | 308 // There isn't a background page loaded initially. |
292 const Extension* extension = GetSingleLoadedExtension(); | 309 const Extension* extension = GetSingleLoadedExtension(); |
293 ASSERT_FALSE( | 310 ASSERT_FALSE( |
294 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 311 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
295 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 312 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
296 // The test makes sure that window.open returns null. | 313 // The test makes sure that window.open returns null. |
297 ASSERT_TRUE(RunExtensionTest("app_background_page/no_js")) << message_; | 314 ASSERT_TRUE(RunExtensionTest("app_background_page/no_js")) << message_; |
298 // And after it runs there should be a background page. | 315 // And after it runs there should be a background page. |
299 ASSERT_TRUE( | 316 BackgroundContents* background_contents = |
300 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 317 BackgroundContentsServiceFactory::GetForProfile(browser()->profile()) |
301 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 318 ->GetAppBackgroundContents(ASCIIToUTF16(extension->id())); |
| 319 ASSERT_TRUE(background_contents); |
| 320 |
| 321 // Verify that window.opener in the background contents is not set when |
| 322 // allow_js_access=false. |
| 323 EXPECT_FALSE(background_contents->web_contents()->GetOpener()); |
| 324 bool window_opener_null_in_js; |
| 325 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 326 background_contents->web_contents(), |
| 327 "domAutomationController.send(window.opener == null);", |
| 328 &window_opener_null_in_js)); |
| 329 EXPECT_TRUE(window_opener_null_in_js); |
302 | 330 |
303 EXPECT_EQ(0u, background_deleted_tracker.size()); | 331 EXPECT_EQ(0u, background_deleted_tracker.size()); |
304 UnloadExtension(extension->id()); | 332 UnloadExtension(extension->id()); |
305 } | 333 } |
306 | 334 |
307 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsManifestBackgroundPage) { | 335 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, NoJsManifestBackgroundPage) { |
308 std::string app_manifest = base::StringPrintf( | 336 std::string app_manifest = base::StringPrintf( |
309 "{" | 337 "{" |
310 " \"name\": \"App\"," | 338 " \"name\": \"App\"," |
311 " \"version\": \"0.1\"," | 339 " \"version\": \"0.1\"," |
(...skipping 12 matching lines...) Expand all Loading... |
324 " \"allow_js_access\": false" | 352 " \"allow_js_access\": false" |
325 " }" | 353 " }" |
326 "}", | 354 "}", |
327 embedded_test_server()->port(), | 355 embedded_test_server()->port(), |
328 embedded_test_server()->port()); | 356 embedded_test_server()->port()); |
329 | 357 |
330 base::FilePath app_dir; | 358 base::FilePath app_dir; |
331 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 359 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
332 ASSERT_TRUE(LoadExtension(app_dir)); | 360 ASSERT_TRUE(LoadExtension(app_dir)); |
333 | 361 |
334 // The background page should load, but window.open should return null. | 362 // The background page should load. |
335 const Extension* extension = GetSingleLoadedExtension(); | 363 const Extension* extension = GetSingleLoadedExtension(); |
336 ASSERT_TRUE( | 364 BackgroundContents* background_contents = |
337 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 365 BackgroundContentsServiceFactory::GetForProfile(browser()->profile()) |
338 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 366 ->GetAppBackgroundContents(ASCIIToUTF16(extension->id())); |
| 367 ASSERT_TRUE(background_contents); |
| 368 |
| 369 // Verify that window.opener in the background contents is not set when |
| 370 // creating the background page through the manifest (not through |
| 371 // window.open). |
| 372 EXPECT_FALSE(background_contents->web_contents()->GetOpener()); |
| 373 bool window_opener_null_in_js; |
| 374 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 375 background_contents->web_contents(), |
| 376 "domAutomationController.send(window.opener == null);", |
| 377 &window_opener_null_in_js)); |
| 378 EXPECT_TRUE(window_opener_null_in_js); |
| 379 |
| 380 // window.open should return null. |
339 ASSERT_TRUE(RunExtensionTest("app_background_page/no_js_manifest")) << | 381 ASSERT_TRUE(RunExtensionTest("app_background_page/no_js_manifest")) << |
340 message_; | 382 message_; |
| 383 |
| 384 // Verify that window.opener in the background contents is still not set. |
| 385 EXPECT_FALSE(background_contents->web_contents()->GetOpener()); |
| 386 EXPECT_TRUE(content::ExecuteScriptAndExtractBool( |
| 387 background_contents->web_contents(), |
| 388 "domAutomationController.send(window.opener == null);", |
| 389 &window_opener_null_in_js)); |
| 390 EXPECT_TRUE(window_opener_null_in_js); |
| 391 |
341 UnloadExtension(extension->id()); | 392 UnloadExtension(extension->id()); |
342 } | 393 } |
343 | 394 |
344 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoBackgroundPages) { | 395 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenTwoBackgroundPages) { |
345 std::string app_manifest = base::StringPrintf( | 396 std::string app_manifest = base::StringPrintf( |
346 "{" | 397 "{" |
347 " \"name\": \"App\"," | 398 " \"name\": \"App\"," |
348 " \"version\": \"0.1\"," | 399 " \"version\": \"0.1\"," |
349 " \"manifest_version\": 2," | 400 " \"manifest_version\": 2," |
350 " \"app\": {" | 401 " \"app\": {" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 "}", | 470 "}", |
420 embedded_test_server()->port(), | 471 embedded_test_server()->port(), |
421 embedded_test_server()->port()); | 472 embedded_test_server()->port()); |
422 | 473 |
423 base::FilePath app_dir; | 474 base::FilePath app_dir; |
424 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); | 475 ASSERT_TRUE(CreateApp(app_manifest, &app_dir)); |
425 ASSERT_TRUE(LoadExtension(app_dir)); | 476 ASSERT_TRUE(LoadExtension(app_dir)); |
426 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_; | 477 ASSERT_TRUE(RunExtensionTest("app_background_page/bg_open")) << message_; |
427 } | 478 } |
428 | 479 |
429 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, DISABLED_OpenThenClose) { | 480 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, OpenThenClose) { |
430 std::string app_manifest = base::StringPrintf( | 481 std::string app_manifest = base::StringPrintf( |
431 "{" | 482 "{" |
432 " \"name\": \"App\"," | 483 " \"name\": \"App\"," |
433 " \"version\": \"0.1\"," | 484 " \"version\": \"0.1\"," |
434 " \"manifest_version\": 2," | 485 " \"manifest_version\": 2," |
435 " \"app\": {" | 486 " \"app\": {" |
436 " \"urls\": [" | 487 " \"urls\": [" |
437 " \"http://a.com/\"" | 488 " \"http://a.com/\"" |
438 " ]," | 489 " ]," |
439 " \"launch\": {" | 490 " \"launch\": {" |
(...skipping 10 matching lines...) Expand all Loading... |
450 // There isn't a background page loaded initially. | 501 // There isn't a background page loaded initially. |
451 const Extension* extension = GetSingleLoadedExtension(); | 502 const Extension* extension = GetSingleLoadedExtension(); |
452 ASSERT_FALSE( | 503 ASSERT_FALSE( |
453 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 504 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
454 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 505 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
455 // Background mode should not be active until a background page is created. | 506 // Background mode should not be active until a background page is created. |
456 ASSERT_TRUE(WaitForBackgroundMode(false)); | 507 ASSERT_TRUE(WaitForBackgroundMode(false)); |
457 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_open")) << message_; | 508 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_open")) << message_; |
458 // Background mode should be active now because a background page was created. | 509 // Background mode should be active now because a background page was created. |
459 ASSERT_TRUE(WaitForBackgroundMode(true)); | 510 ASSERT_TRUE(WaitForBackgroundMode(true)); |
460 ASSERT_TRUE( | 511 |
461 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 512 // Verify that the background contents exist. |
462 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 513 BackgroundContents* background_contents = |
| 514 BackgroundContentsServiceFactory::GetForProfile(browser()->profile()) |
| 515 ->GetAppBackgroundContents(ASCIIToUTF16(extension->id())); |
| 516 ASSERT_TRUE(background_contents); |
| 517 |
| 518 // Verify that window.opener in the background contents is set. |
| 519 content::RenderFrameHost* background_opener = |
| 520 background_contents->web_contents()->GetOpener(); |
| 521 ASSERT_TRUE(background_opener); |
| 522 std::string window_opener_href; |
| 523 EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 524 background_contents->web_contents(), |
| 525 "domAutomationController.send(window.opener.location.href);", |
| 526 &window_opener_href)); |
| 527 EXPECT_EQ(window_opener_href, |
| 528 background_opener->GetLastCommittedURL().spec()); |
| 529 |
463 // Now close the BackgroundContents. | 530 // Now close the BackgroundContents. |
464 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_close")) << message_; | 531 ASSERT_TRUE(RunExtensionTest("app_background_page/basic_close")) << message_; |
| 532 |
465 // Background mode should no longer be active. | 533 // Background mode should no longer be active. |
466 ASSERT_TRUE(WaitForBackgroundMode(false)); | 534 ASSERT_TRUE(WaitForBackgroundMode(false)); |
467 ASSERT_FALSE( | 535 ASSERT_FALSE( |
468 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> | 536 BackgroundContentsServiceFactory::GetForProfile(browser()->profile())-> |
469 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); | 537 GetAppBackgroundContents(ASCIIToUTF16(extension->id()))); |
470 } | 538 } |
471 | 539 |
472 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, UnloadExtensionWhileHidden) { | 540 IN_PROC_BROWSER_TEST_F(AppBackgroundPageApiTest, UnloadExtensionWhileHidden) { |
473 std::string app_manifest = base::StringPrintf( | 541 std::string app_manifest = base::StringPrintf( |
474 "{" | 542 "{" |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 ExtensionTestMessageListener ready_listener("ready", true); | 637 ExtensionTestMessageListener ready_listener("ready", true); |
570 LaunchTestingApp(); | 638 LaunchTestingApp(); |
571 EXPECT_TRUE(ready_listener.WaitUntilSatisfied()); | 639 EXPECT_TRUE(ready_listener.WaitUntilSatisfied()); |
572 | 640 |
573 ExtensionTestMessageListener created_listener("created_module:1", false); | 641 ExtensionTestMessageListener created_listener("created_module:1", false); |
574 ready_listener.Reply("create_module_without_hack"); | 642 ready_listener.Reply("create_module_without_hack"); |
575 EXPECT_TRUE(created_listener.WaitUntilSatisfied()); | 643 EXPECT_TRUE(created_listener.WaitUntilSatisfied()); |
576 } | 644 } |
577 | 645 |
578 #endif // !defined(DISABLE_NACL) | 646 #endif // !defined(DISABLE_NACL) |
OLD | NEW |