| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 // listening for an extension event. | 414 // listening for an extension event. |
| 415 ui_test_utils::NavigateToURL( | 415 ui_test_utils::NavigateToURL( |
| 416 browser(), | 416 browser(), |
| 417 embedded_test_server()->GetURL( | 417 embedded_test_server()->GetURL( |
| 418 "/extensions/api_test/content_scripts/extension_api/events.html")); | 418 "/extensions/api_test/content_scripts/extension_api/events.html")); |
| 419 | 419 |
| 420 // Navigate to an extension page that will fire the event events.js is | 420 // Navigate to an extension page that will fire the event events.js is |
| 421 // listening for. | 421 // listening for. |
| 422 ui_test_utils::NavigateToURLWithDisposition( | 422 ui_test_utils::NavigateToURLWithDisposition( |
| 423 browser(), extension->GetResourceURL("fire_event.html"), | 423 browser(), extension->GetResourceURL("fire_event.html"), |
| 424 NEW_FOREGROUND_TAB, ui_test_utils::BROWSER_TEST_NONE); | 424 WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 425 ui_test_utils::BROWSER_TEST_NONE); |
| 425 EXPECT_TRUE(catcher.GetNextResult()); | 426 EXPECT_TRUE(catcher.GetNextResult()); |
| 426 } | 427 } |
| 427 | 428 |
| 428 // Flaky on Windows. http://crbug.com/248418 | 429 // Flaky on Windows. http://crbug.com/248418 |
| 429 #if defined(OS_WIN) | 430 #if defined(OS_WIN) |
| 430 #define MAYBE_ContentScriptPermissionsApi DISABLED_ContentScriptPermissionsApi | 431 #define MAYBE_ContentScriptPermissionsApi DISABLED_ContentScriptPermissionsApi |
| 431 #else | 432 #else |
| 432 #define MAYBE_ContentScriptPermissionsApi ContentScriptPermissionsApi | 433 #define MAYBE_ContentScriptPermissionsApi ContentScriptPermissionsApi |
| 433 #endif | 434 #endif |
| 434 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_ContentScriptPermissionsApi) { | 435 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_ContentScriptPermissionsApi) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 browser()->tab_strip_model()->GetActiveWebContents(); | 468 browser()->tab_strip_model()->GetActiveWebContents(); |
| 468 DialogHelper dialog_helper(web_contents); | 469 DialogHelper dialog_helper(web_contents); |
| 469 base::RunLoop run_loop; | 470 base::RunLoop run_loop; |
| 470 dialog_helper.set_quit_closure(run_loop.QuitClosure()); | 471 dialog_helper.set_quit_closure(run_loop.QuitClosure()); |
| 471 | 472 |
| 472 ExtensionTestMessageListener listener("done", false); | 473 ExtensionTestMessageListener listener("done", false); |
| 473 listener.set_extension_id(ext2->id()); | 474 listener.set_extension_id(ext2->id()); |
| 474 | 475 |
| 475 // Navigate! Both extensions will try to inject. | 476 // Navigate! Both extensions will try to inject. |
| 476 ui_test_utils::NavigateToURLWithDisposition( | 477 ui_test_utils::NavigateToURLWithDisposition( |
| 477 browser(), | 478 browser(), embedded_test_server()->GetURL("/empty.html"), |
| 478 embedded_test_server()->GetURL("/empty.html"), | 479 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE); |
| 479 CURRENT_TAB, | |
| 480 ui_test_utils::BROWSER_TEST_NONE); | |
| 481 | 480 |
| 482 run_loop.Run(); | 481 run_loop.Run(); |
| 483 // Right now, the alert dialog is showing and blocking injection of anything | 482 // Right now, the alert dialog is showing and blocking injection of anything |
| 484 // after it, so the listener shouldn't be satisfied. | 483 // after it, so the listener shouldn't be satisfied. |
| 485 EXPECT_FALSE(listener.was_satisfied()); | 484 EXPECT_FALSE(listener.was_satisfied()); |
| 486 EXPECT_EQ(1u, dialog_helper.dialog_count()); | 485 EXPECT_EQ(1u, dialog_helper.dialog_count()); |
| 487 dialog_helper.CloseDialogs(); | 486 dialog_helper.CloseDialogs(); |
| 488 | 487 |
| 489 // After closing the dialog, the rest of the scripts should be able to | 488 // After closing the dialog, the rest of the scripts should be able to |
| 490 // inject. | 489 // inject. |
| 491 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 490 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 492 } | 491 } |
| 493 | 492 |
| 494 // Test that closing a tab with a blocking script results in no further scripts | 493 // Test that closing a tab with a blocking script results in no further scripts |
| 495 // running (and we don't crash). | 494 // running (and we don't crash). |
| 496 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptBlockingScriptTabClosed) { | 495 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentScriptBlockingScriptTabClosed) { |
| 497 ASSERT_TRUE(StartEmbeddedTestServer()); | 496 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 498 | 497 |
| 499 // We're going to close a tab in this test, so make a new one (to ensure | 498 // We're going to close a tab in this test, so make a new one (to ensure |
| 500 // we don't close the browser). | 499 // we don't close the browser). |
| 501 ui_test_utils::NavigateToURLWithDisposition( | 500 ui_test_utils::NavigateToURLWithDisposition( |
| 502 browser(), | 501 browser(), embedded_test_server()->GetURL("/empty.html"), |
| 503 embedded_test_server()->GetURL("/empty.html"), | 502 WindowOpenDisposition::NEW_FOREGROUND_TAB, |
| 504 NEW_FOREGROUND_TAB, | |
| 505 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 503 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 506 | 504 |
| 507 // Set up the same as the previous test case. | 505 // Set up the same as the previous test case. |
| 508 TestExtensionDir ext_dir1; | 506 TestExtensionDir ext_dir1; |
| 509 ext_dir1.WriteManifest( | 507 ext_dir1.WriteManifest( |
| 510 base::StringPrintf(kManifest, "ext1", "document_start")); | 508 base::StringPrintf(kManifest, "ext1", "document_start")); |
| 511 ext_dir1.WriteFile(FILE_PATH_LITERAL("script.js"), kBlockingScript); | 509 ext_dir1.WriteFile(FILE_PATH_LITERAL("script.js"), kBlockingScript); |
| 512 const Extension* ext1 = LoadExtension(ext_dir1.unpacked_path()); | 510 const Extension* ext1 = LoadExtension(ext_dir1.unpacked_path()); |
| 513 ASSERT_TRUE(ext1); | 511 ASSERT_TRUE(ext1); |
| 514 | 512 |
| 515 TestExtensionDir ext_dir2; | 513 TestExtensionDir ext_dir2; |
| 516 ext_dir2.WriteManifest(base::StringPrintf(kManifest, "ext2", "document_end")); | 514 ext_dir2.WriteManifest(base::StringPrintf(kManifest, "ext2", "document_end")); |
| 517 ext_dir2.WriteFile(FILE_PATH_LITERAL("script.js"), kNonBlockingScript); | 515 ext_dir2.WriteFile(FILE_PATH_LITERAL("script.js"), kNonBlockingScript); |
| 518 const Extension* ext2 = LoadExtension(ext_dir2.unpacked_path()); | 516 const Extension* ext2 = LoadExtension(ext_dir2.unpacked_path()); |
| 519 ASSERT_TRUE(ext2); | 517 ASSERT_TRUE(ext2); |
| 520 | 518 |
| 521 content::WebContents* web_contents = | 519 content::WebContents* web_contents = |
| 522 browser()->tab_strip_model()->GetActiveWebContents(); | 520 browser()->tab_strip_model()->GetActiveWebContents(); |
| 523 DialogHelper dialog_helper(web_contents); | 521 DialogHelper dialog_helper(web_contents); |
| 524 base::RunLoop run_loop; | 522 base::RunLoop run_loop; |
| 525 dialog_helper.set_quit_closure(run_loop.QuitClosure()); | 523 dialog_helper.set_quit_closure(run_loop.QuitClosure()); |
| 526 | 524 |
| 527 ExtensionTestMessageListener listener("done", false); | 525 ExtensionTestMessageListener listener("done", false); |
| 528 listener.set_extension_id(ext2->id()); | 526 listener.set_extension_id(ext2->id()); |
| 529 | 527 |
| 530 // Navitate! | 528 // Navitate! |
| 531 ui_test_utils::NavigateToURLWithDisposition( | 529 ui_test_utils::NavigateToURLWithDisposition( |
| 532 browser(), | 530 browser(), embedded_test_server()->GetURL("/empty.html"), |
| 533 embedded_test_server()->GetURL("/empty.html"), | 531 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE); |
| 534 CURRENT_TAB, | |
| 535 ui_test_utils::BROWSER_TEST_NONE); | |
| 536 | 532 |
| 537 // Now, instead of closing the dialog, just close the tab. Later scripts | 533 // Now, instead of closing the dialog, just close the tab. Later scripts |
| 538 // should never get a chance to run (and we shouldn't crash). | 534 // should never get a chance to run (and we shouldn't crash). |
| 539 run_loop.Run(); | 535 run_loop.Run(); |
| 540 EXPECT_FALSE(listener.was_satisfied()); | 536 EXPECT_FALSE(listener.was_satisfied()); |
| 541 EXPECT_TRUE(browser()->tab_strip_model()->CloseWebContentsAt( | 537 EXPECT_TRUE(browser()->tab_strip_model()->CloseWebContentsAt( |
| 542 browser()->tab_strip_model()->active_index(), 0)); | 538 browser()->tab_strip_model()->active_index(), 0)); |
| 543 EXPECT_FALSE(listener.was_satisfied()); | 539 EXPECT_FALSE(listener.was_satisfied()); |
| 544 } | 540 } |
| 545 | 541 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 559 ASSERT_TRUE(ext1); | 555 ASSERT_TRUE(ext1); |
| 560 | 556 |
| 561 content::WebContents* web_contents = | 557 content::WebContents* web_contents = |
| 562 browser()->tab_strip_model()->GetActiveWebContents(); | 558 browser()->tab_strip_model()->GetActiveWebContents(); |
| 563 DialogHelper dialog_helper(web_contents); | 559 DialogHelper dialog_helper(web_contents); |
| 564 base::RunLoop run_loop; | 560 base::RunLoop run_loop; |
| 565 dialog_helper.set_quit_closure(run_loop.QuitClosure()); | 561 dialog_helper.set_quit_closure(run_loop.QuitClosure()); |
| 566 | 562 |
| 567 // Navigate! | 563 // Navigate! |
| 568 ui_test_utils::NavigateToURLWithDisposition( | 564 ui_test_utils::NavigateToURLWithDisposition( |
| 569 browser(), | 565 browser(), embedded_test_server()->GetURL("/empty.html"), |
| 570 embedded_test_server()->GetURL("/empty.html"), | 566 WindowOpenDisposition::CURRENT_TAB, ui_test_utils::BROWSER_TEST_NONE); |
| 571 CURRENT_TAB, | |
| 572 ui_test_utils::BROWSER_TEST_NONE); | |
| 573 | 567 |
| 574 run_loop.Run(); | 568 run_loop.Run(); |
| 575 | 569 |
| 576 // The extension will have injected at idle, but it should only inject once. | 570 // The extension will have injected at idle, but it should only inject once. |
| 577 EXPECT_EQ(1u, dialog_helper.dialog_count()); | 571 EXPECT_EQ(1u, dialog_helper.dialog_count()); |
| 578 dialog_helper.CloseDialogs(); | 572 dialog_helper.CloseDialogs(); |
| 579 EXPECT_TRUE(RunAllPending(web_contents)); | 573 EXPECT_TRUE(RunAllPending(web_contents)); |
| 580 EXPECT_EQ(1u, dialog_helper.dialog_count()); | 574 EXPECT_EQ(1u, dialog_helper.dialog_count()); |
| 581 } | 575 } |
| 582 | 576 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 605 content::WebContents* tab_contents = | 599 content::WebContents* tab_contents = |
| 606 browser()->tab_strip_model()->GetActiveWebContents(); | 600 browser()->tab_strip_model()->GetActiveWebContents(); |
| 607 | 601 |
| 608 EXPECT_EQ(new_tab_override->GetResourceURL("newtab.html"), | 602 EXPECT_EQ(new_tab_override->GetResourceURL("newtab.html"), |
| 609 tab_contents->GetMainFrame()->GetLastCommittedURL()); | 603 tab_contents->GetMainFrame()->GetLastCommittedURL()); |
| 610 EXPECT_FALSE(listener.was_satisfied()); | 604 EXPECT_FALSE(listener.was_satisfied()); |
| 611 listener.Reset(); | 605 listener.Reset(); |
| 612 | 606 |
| 613 ui_test_utils::NavigateToURLWithDisposition( | 607 ui_test_utils::NavigateToURLWithDisposition( |
| 614 browser(), embedded_test_server()->GetURL("/empty.html"), | 608 browser(), embedded_test_server()->GetURL("/empty.html"), |
| 615 CURRENT_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 609 WindowOpenDisposition::CURRENT_TAB, |
| 610 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 616 base::RunLoop().RunUntilIdle(); | 611 base::RunLoop().RunUntilIdle(); |
| 617 EXPECT_TRUE(listener.was_satisfied()); | 612 EXPECT_TRUE(listener.was_satisfied()); |
| 618 } | 613 } |
| 619 | 614 |
| 620 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, | 615 IN_PROC_BROWSER_TEST_F(ExtensionApiTest, |
| 621 DontInjectContentScriptsInBackgroundPages) { | 616 DontInjectContentScriptsInBackgroundPages) { |
| 622 host_resolver()->AddRule("a.com", "127.0.0.1"); | 617 host_resolver()->AddRule("a.com", "127.0.0.1"); |
| 623 ASSERT_TRUE(StartEmbeddedTestServer()); | 618 ASSERT_TRUE(StartEmbeddedTestServer()); |
| 624 // Load two extensions, one with an iframe to a.com in its background page, | 619 // Load two extensions, one with an iframe to a.com in its background page, |
| 625 // the other, a content script for a.com. The latter should never be able to | 620 // the other, a content script for a.com. The latter should never be able to |
| 626 // inject the script, because scripts aren't allowed to run on foreign | 621 // inject the script, because scripts aren't allowed to run on foreign |
| 627 // extensions' pages. | 622 // extensions' pages. |
| 628 base::FilePath data_dir = test_data_dir_.AppendASCII("content_scripts"); | 623 base::FilePath data_dir = test_data_dir_.AppendASCII("content_scripts"); |
| 629 ExtensionTestMessageListener iframe_loaded_listener("iframe loaded", false); | 624 ExtensionTestMessageListener iframe_loaded_listener("iframe loaded", false); |
| 630 ExtensionTestMessageListener content_script_listener("script injected", | 625 ExtensionTestMessageListener content_script_listener("script injected", |
| 631 false); | 626 false); |
| 632 LoadExtension(data_dir.AppendASCII("script_a_com")); | 627 LoadExtension(data_dir.AppendASCII("script_a_com")); |
| 633 LoadExtension(data_dir.AppendASCII("background_page_iframe")); | 628 LoadExtension(data_dir.AppendASCII("background_page_iframe")); |
| 634 iframe_loaded_listener.WaitUntilSatisfied(); | 629 iframe_loaded_listener.WaitUntilSatisfied(); |
| 635 EXPECT_FALSE(content_script_listener.was_satisfied()); | 630 EXPECT_FALSE(content_script_listener.was_satisfied()); |
| 636 } | 631 } |
| 637 | 632 |
| 638 } // namespace extensions | 633 } // namespace extensions |
| OLD | NEW |