Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(946)

Side by Side Diff: chrome/browser/extensions/content_script_apitest.cc

Issue 2978953002: [Extensions] Don't allow content scripts on the New Tab Page (Closed)
Patch Set: Remove logging! Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/renderer/extensions/renderer_permissions_policy_delegate.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "chrome/browser/extensions/api/permissions/permissions_api.h" 15 #include "chrome/browser/extensions/api/permissions/permissions_api.h"
16 #include "chrome/browser/extensions/extension_apitest.h" 16 #include "chrome/browser/extensions/extension_apitest.h"
17 #include "chrome/browser/extensions/extension_management_test_util.h" 17 #include "chrome/browser/extensions/extension_management_test_util.h"
18 #include "chrome/browser/extensions/extension_service.h" 18 #include "chrome/browser/extensions/extension_service.h"
19 #include "chrome/browser/extensions/extension_with_management_policy_apitest.h" 19 #include "chrome/browser/extensions/extension_with_management_policy_apitest.h"
20 #include "chrome/browser/extensions/test_extension_dir.h" 20 #include "chrome/browser/extensions/test_extension_dir.h"
21 #include "chrome/browser/search/search.h"
21 #include "chrome/browser/ui/browser.h" 22 #include "chrome/browser/ui/browser.h"
22 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h" 23 #include "chrome/browser/ui/javascript_dialogs/javascript_dialog_tab_helper.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h" 24 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/common/chrome_switches.h" 25 #include "chrome/common/chrome_switches.h"
25 #include "chrome/test/base/ui_test_utils.h" 26 #include "chrome/test/base/ui_test_utils.h"
26 #include "content/public/browser/javascript_dialog_manager.h" 27 #include "content/public/browser/javascript_dialog_manager.h"
27 #include "content/public/browser/render_frame_host.h" 28 #include "content/public/browser/render_frame_host.h"
28 #include "content/public/browser/web_contents.h" 29 #include "content/public/browser/web_contents.h"
29 #include "content/public/browser/web_contents_delegate.h" 30 #include "content/public/browser/web_contents_delegate.h"
30 #include "content/public/test/browser_test_utils.h" 31 #include "content/public/test/browser_test_utils.h"
(...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 base::FilePath data_dir = test_data_dir_.AppendASCII("content_scripts"); 616 base::FilePath data_dir = test_data_dir_.AppendASCII("content_scripts");
616 ExtensionTestMessageListener iframe_loaded_listener("iframe loaded", false); 617 ExtensionTestMessageListener iframe_loaded_listener("iframe loaded", false);
617 ExtensionTestMessageListener content_script_listener("script injected", 618 ExtensionTestMessageListener content_script_listener("script injected",
618 false); 619 false);
619 LoadExtension(data_dir.AppendASCII("script_a_com")); 620 LoadExtension(data_dir.AppendASCII("script_a_com"));
620 LoadExtension(data_dir.AppendASCII("background_page_iframe")); 621 LoadExtension(data_dir.AppendASCII("background_page_iframe"));
621 iframe_loaded_listener.WaitUntilSatisfied(); 622 iframe_loaded_listener.WaitUntilSatisfied();
622 EXPECT_FALSE(content_script_listener.was_satisfied()); 623 EXPECT_FALSE(content_script_listener.was_satisfied());
623 } 624 }
624 625
626 IN_PROC_BROWSER_TEST_P(ContentScriptApiTest, CannotScriptTheNewTabPage) {
627 ASSERT_TRUE(StartEmbeddedTestServer());
628
629 ExtensionTestMessageListener test_listener("ready", true);
630 LoadExtension(test_data_dir_.AppendASCII("content_scripts/ntp"));
631 ASSERT_TRUE(test_listener.WaitUntilSatisfied());
632
633 auto did_script_inject = [](content::WebContents* web_contents) {
634 bool did_inject = false;
635 EXPECT_TRUE(content::ExecuteScriptAndExtractBool(
636 web_contents,
637 "domAutomationController.send(document.title === 'injected');",
638 &did_inject));
639 return did_inject;
640 };
641
642 // First, test the executeScript() method.
643 ResultCatcher catcher;
644 test_listener.Reply(std::string());
645 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
646 EXPECT_EQ(GURL("chrome://newtab"), browser()
647 ->tab_strip_model()
648 ->GetActiveWebContents()
649 ->GetLastCommittedURL());
650 EXPECT_FALSE(
651 did_script_inject(browser()->tab_strip_model()->GetActiveWebContents()));
652
653 // Next, check content script injection.
654 ui_test_utils::NavigateToURL(browser(), search::GetNewTabPageURL(profile()));
655 EXPECT_FALSE(
656 did_script_inject(browser()->tab_strip_model()->GetActiveWebContents()));
657
658 // The extension should inject on "normal" urls.
659 GURL unprotected_url = embedded_test_server()->GetURL(
660 "example.com", "/extensions/test_file.html");
661 ui_test_utils::NavigateToURL(browser(), unprotected_url);
662 EXPECT_TRUE(
663 did_script_inject(browser()->tab_strip_model()->GetActiveWebContents()));
664 }
665
625 INSTANTIATE_TEST_CASE_P( 666 INSTANTIATE_TEST_CASE_P(
626 ContentScriptApiTests, 667 ContentScriptApiTests,
627 ContentScriptApiTest, 668 ContentScriptApiTest,
628 testing::Values(TestConfig::kDefault, 669 testing::Values(TestConfig::kDefault,
629 TestConfig::kYieldBetweenContentScriptRunsEnabled)); 670 TestConfig::kYieldBetweenContentScriptRunsEnabled));
630 671
631 } // namespace extensions 672 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/renderer/extensions/renderer_permissions_policy_delegate.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698