1 // Copyright (c) 2011 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. 4 5 #include "chrome/browser/extensions/extension_apitest.h" 6 #include "chrome/browser/extensions/extension_service.h" 7 #include "chrome/browser/extensions/extension_test_message_listener.h" 8 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/ui/browser.h" 10 #include "chrome/common/extensions/extension.h" 11 #include "chrome/test/ui_test_utils.h" 12 13 const std::string kAllUrlsTarget = 14 "files/extensions/api_test/all_urls/index.html"; 15 16 typedef ExtensionApiTest AllUrlsApiTest; 17 18 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, WhitelistedExtension) { 19 // First setup the two extensions. 20 FilePath extension_dir1 = test_data_dir_.AppendASCII("all_urls") 21 .AppendASCII("content_script"); 22 FilePath extension_dir2 = test_data_dir_.AppendASCII("all_urls") 23 .AppendASCII("execute_script"); 24 25 // Then add the two extensions to the whitelist. 26 Extension::ScriptingWhitelist whitelist; 27 whitelist.push_back(Extension::GenerateIdForPath(extension_dir1)); 28 whitelist.push_back(Extension::GenerateIdForPath(extension_dir2)); 29 Extension::SetScriptingWhitelist(whitelist); 30 31 // Then load extensions. 32 ExtensionService* service = browser()->profile()->GetExtensionService(); 33 const size_t size_before = service->extensions()->size(); 34 ASSERT_TRUE(LoadExtension(extension_dir1)); 35 ASSERT_TRUE(LoadExtension(extension_dir2)); 36 EXPECT_EQ(size_before + 2, service->extensions()->size()); 37 38 std::string url; 39 40 // Now verify we run content scripts on chrome://newtab/. 41 url = "chrome://newtab/"; 42 ExtensionTestMessageListener listener1a("content script: " + url, false); 43 ExtensionTestMessageListener listener1b("execute: " + url, false); 44 ui_test_utils::NavigateToURL(browser(), GURL(url)); 45 ASSERT_TRUE(listener1a.WaitUntilSatisfied()); 46 ASSERT_TRUE(listener1b.WaitUntilSatisfied()); 47 48 // Now verify data: urls. 49 url = "data:text/html;charset=utf-8,<html>asdf</html>"; 50 ExtensionTestMessageListener listener2a("content script: " + url, false); 51 ExtensionTestMessageListener listener2b("execute: " + url, false); 52 ui_test_utils::NavigateToURL(browser(), GURL(url)); 53 ASSERT_TRUE(listener2a.WaitUntilSatisfied()); 54 ASSERT_TRUE(listener2b.WaitUntilSatisfied()); 55 56 // Now verify about:version. 57 url = "about:version"; 58 ExtensionTestMessageListener listener3a("content script: " + url, false); 59 ExtensionTestMessageListener listener3b("execute: " + url, false); 60 ui_test_utils::NavigateToURL(browser(), GURL(url)); 61 ASSERT_TRUE(listener3a.WaitUntilSatisfied()); 62 ASSERT_TRUE(listener3b.WaitUntilSatisfied()); 63 64 // Now verify about:blank. 65 url = "about:blank"; 66 ExtensionTestMessageListener listener4a("content script: " + url, false); 67 ExtensionTestMessageListener listener4b("execute: " + url, false); 68 ui_test_utils::NavigateToURL(browser(), GURL(url)); 69 ASSERT_TRUE(listener4a.WaitUntilSatisfied()); 70 ASSERT_TRUE(listener4b.WaitUntilSatisfied()); 71 72 // Now verify we can script a regular http page. 73 ASSERT_TRUE(test_server()->Start()); 74 GURL page_url = test_server()->GetURL(kAllUrlsTarget); 75 ExtensionTestMessageListener listener5a("content script: " + page_url.spec(), 76 false); 77 ExtensionTestMessageListener listener5b("execute: " + page_url.spec(), false); 78 ui_test_utils::NavigateToURL(browser(), page_url); 79 ASSERT_TRUE(listener5a.WaitUntilSatisfied()); 80 ASSERT_TRUE(listener5b.WaitUntilSatisfied()); 81 } 82 83 // Test that an extension NOT whitelisted for scripting can ask for <all_urls> 84 // and run scripts on non-restricted all pages. 85 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, RegularExtensions) { 86 // First load the two extension. 87 FilePath extension_dir1 = test_data_dir_.AppendASCII("all_urls") 88 .AppendASCII("content_script"); 89 FilePath extension_dir2 = test_data_dir_.AppendASCII("all_urls") 90 .AppendASCII("execute_script"); 91 92 93 ExtensionService* service = browser()->profile()->GetExtensionService(); 94 const size_t size_before = service->extensions()->size(); 95 ASSERT_TRUE(LoadExtension(extension_dir1)); 96 ASSERT_TRUE(LoadExtension(extension_dir2)); 97 EXPECT_EQ(size_before + 2, service->extensions()->size()); 98 99 // Now verify we can script a regular http page. 100 ASSERT_TRUE(test_server()->Start()); 101 GURL page_url = test_server()->GetURL(kAllUrlsTarget); 102 ExtensionTestMessageListener listener1a("content script: " + page_url.spec(), 103 false); 104 ExtensionTestMessageListener listener1b("execute: " + page_url.spec(), false); 105 ui_test_utils::NavigateToURL(browser(), page_url); 106 ASSERT_TRUE(listener1a.WaitUntilSatisfied()); 107 ASSERT_TRUE(listener1b.WaitUntilSatisfied()); 108 } 109