OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/command_line.h" | 5 #include "base/command_line.h" |
6 #include "chrome/browser/extensions/extension_apitest.h" | 6 #include "chrome/browser/extensions/extension_apitest.h" |
| 7 #include "chrome/browser/extensions/extension_service.h" |
7 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
8 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/test/base/test_switches.h" | 10 #include "chrome/test/base/test_switches.h" |
10 #include "chrome/test/base/ui_test_utils.h" | 11 #include "chrome/test/base/ui_test_utils.h" |
11 #include "components/crx_file/id_util.h" | 12 #include "components/crx_file/id_util.h" |
12 #include "extensions/browser/extension_registry.h" | 13 #include "extensions/browser/extension_registry.h" |
13 #include "extensions/common/extension.h" | 14 #include "extensions/common/extension.h" |
14 #include "extensions/common/extensions_client.h" | 15 #include "extensions/common/extensions_client.h" |
15 #include "extensions/test/extension_test_message_listener.h" | 16 #include "extensions/test/extension_test_message_listener.h" |
16 #include "net/test/embedded_test_server/embedded_test_server.h" | 17 #include "net/test/embedded_test_server/embedded_test_server.h" |
17 | 18 |
| 19 namespace extensions { |
| 20 |
| 21 namespace { |
18 const std::string kAllUrlsTarget = "/extensions/api_test/all_urls/index.html"; | 22 const std::string kAllUrlsTarget = "/extensions/api_test/all_urls/index.html"; |
| 23 } |
19 | 24 |
20 typedef ExtensionApiTest AllUrlsApiTest; | 25 class AllUrlsApiTest : public ExtensionApiTest { |
| 26 protected: |
| 27 AllUrlsApiTest() {} |
| 28 ~AllUrlsApiTest() override {} |
| 29 |
| 30 const Extension* content_script() const { return content_script_.get(); } |
| 31 const Extension* execute_script() const { return execute_script_.get(); } |
| 32 |
| 33 void WhitelistExtensions() { |
| 34 ExtensionsClient::ScriptingWhitelist whitelist; |
| 35 whitelist.push_back(content_script_->id()); |
| 36 whitelist.push_back(execute_script_->id()); |
| 37 ExtensionsClient::Get()->SetScriptingWhitelist(whitelist); |
| 38 // Extensions will have certain permissions withheld at initialization if |
| 39 // they aren't whitelisted, so we need to reload them. |
| 40 extension_service()->ReloadExtension(content_script_->id()); |
| 41 extension_service()->ReloadExtension(execute_script_->id()); |
| 42 } |
| 43 |
| 44 private: |
| 45 void SetUpOnMainThread() override { |
| 46 ExtensionApiTest::SetUpOnMainThread(); |
| 47 base::FilePath data_dir = test_data_dir_.AppendASCII("all_urls"); |
| 48 content_script_ = LoadExtension(data_dir.AppendASCII("content_script")); |
| 49 ASSERT_TRUE(content_script_); |
| 50 execute_script_ = LoadExtension(data_dir.AppendASCII("execute_script")); |
| 51 ASSERT_TRUE(execute_script_); |
| 52 } |
| 53 |
| 54 scoped_refptr<const Extension> content_script_; |
| 55 scoped_refptr<const Extension> execute_script_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(AllUrlsApiTest); |
| 58 }; |
21 | 59 |
22 #if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_CHROMEOS) | 60 #if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_CHROMEOS) |
23 // http://crbug.com/174341 | 61 // http://crbug.com/174341 |
24 #define MAYBE_WhitelistedExtension DISABLED_WhitelistedExtension | 62 #define MAYBE_WhitelistedExtension DISABLED_WhitelistedExtension |
25 #else | 63 #else |
26 #define MAYBE_WhitelistedExtension WhitelistedExtension | 64 #define MAYBE_WhitelistedExtension WhitelistedExtension |
27 #endif | 65 #endif |
28 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, MAYBE_WhitelistedExtension) { | 66 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, MAYBE_WhitelistedExtension) { |
29 #if defined(OS_WIN) && defined(USE_ASH) | 67 #if defined(OS_WIN) && defined(USE_ASH) |
30 // Disable this test in Metro+Ash for now (http://crbug.com/262796). | 68 // Disable this test in Metro+Ash for now (http://crbug.com/262796). |
31 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 69 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
32 switches::kAshBrowserTests)) | 70 switches::kAshBrowserTests)) |
33 return; | 71 return; |
34 #endif | 72 #endif |
35 | 73 |
36 // First setup the two extensions. | 74 WhitelistExtensions(); |
37 base::FilePath extension_dir1 = test_data_dir_.AppendASCII("all_urls") | |
38 .AppendASCII("content_script"); | |
39 base::FilePath extension_dir2 = test_data_dir_.AppendASCII("all_urls") | |
40 .AppendASCII("execute_script"); | |
41 | |
42 // Then add the two extensions to the whitelist. | |
43 extensions::ExtensionsClient::ScriptingWhitelist whitelist; | |
44 whitelist.push_back(crx_file::id_util::GenerateIdForPath(extension_dir1)); | |
45 whitelist.push_back(crx_file::id_util::GenerateIdForPath(extension_dir2)); | |
46 extensions::ExtensionsClient::Get()->SetScriptingWhitelist(whitelist); | |
47 | |
48 // Then load extensions. | |
49 extensions::ExtensionRegistry* registry = | |
50 extensions::ExtensionRegistry::Get(browser()->profile()); | |
51 const size_t size_before = registry->enabled_extensions().size(); | |
52 ASSERT_TRUE(LoadExtension(extension_dir1)); | |
53 ASSERT_TRUE(LoadExtension(extension_dir2)); | |
54 EXPECT_EQ(size_before + 2, registry->enabled_extensions().size()); | |
55 | 75 |
56 std::string url; | 76 std::string url; |
57 | 77 |
58 // Now verify we run content scripts on chrome://newtab/. | 78 // Now verify we run content scripts on chrome://newtab/. |
59 url = "chrome://newtab/"; | 79 url = "chrome://newtab/"; |
60 ExtensionTestMessageListener listener1a("content script: " + url, false); | 80 ExtensionTestMessageListener listener1a("content script: " + url, false); |
61 ExtensionTestMessageListener listener1b("execute: " + url, false); | 81 ExtensionTestMessageListener listener1b("execute: " + url, false); |
62 ui_test_utils::NavigateToURL(browser(), GURL(url)); | 82 ui_test_utils::NavigateToURL(browser(), GURL(url)); |
63 ASSERT_TRUE(listener1a.WaitUntilSatisfied()); | 83 ASSERT_TRUE(listener1a.WaitUntilSatisfied()); |
64 ASSERT_TRUE(listener1b.WaitUntilSatisfied()); | 84 ASSERT_TRUE(listener1b.WaitUntilSatisfied()); |
(...skipping 29 matching lines...) Expand all Loading... |
94 false); | 114 false); |
95 ExtensionTestMessageListener listener5b("execute: " + page_url.spec(), false); | 115 ExtensionTestMessageListener listener5b("execute: " + page_url.spec(), false); |
96 ui_test_utils::NavigateToURL(browser(), page_url); | 116 ui_test_utils::NavigateToURL(browser(), page_url); |
97 ASSERT_TRUE(listener5a.WaitUntilSatisfied()); | 117 ASSERT_TRUE(listener5a.WaitUntilSatisfied()); |
98 ASSERT_TRUE(listener5b.WaitUntilSatisfied()); | 118 ASSERT_TRUE(listener5b.WaitUntilSatisfied()); |
99 } | 119 } |
100 | 120 |
101 // Test that an extension NOT whitelisted for scripting can ask for <all_urls> | 121 // Test that an extension NOT whitelisted for scripting can ask for <all_urls> |
102 // and run scripts on non-restricted all pages. | 122 // and run scripts on non-restricted all pages. |
103 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, RegularExtensions) { | 123 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, RegularExtensions) { |
104 // First load the two extensions. | |
105 base::FilePath extension_dir1 = test_data_dir_.AppendASCII("all_urls") | |
106 .AppendASCII("content_script"); | |
107 base::FilePath extension_dir2 = test_data_dir_.AppendASCII("all_urls") | |
108 .AppendASCII("execute_script"); | |
109 | |
110 extensions::ExtensionRegistry* registry = | |
111 extensions::ExtensionRegistry::Get(browser()->profile()); | |
112 const size_t size_before = registry->enabled_extensions().size(); | |
113 ASSERT_TRUE(LoadExtension(extension_dir1)); | |
114 ASSERT_TRUE(LoadExtension(extension_dir2)); | |
115 EXPECT_EQ(size_before + 2, registry->enabled_extensions().size()); | |
116 | |
117 // Now verify we can script a regular http page. | 124 // Now verify we can script a regular http page. |
118 ASSERT_TRUE(StartEmbeddedTestServer()); | 125 ASSERT_TRUE(StartEmbeddedTestServer()); |
119 GURL page_url = embedded_test_server()->GetURL(kAllUrlsTarget); | 126 GURL page_url = embedded_test_server()->GetURL(kAllUrlsTarget); |
120 ExtensionTestMessageListener listener1a("content script: " + page_url.spec(), | 127 ExtensionTestMessageListener listener1a("content script: " + page_url.spec(), |
121 false); | 128 false); |
122 ExtensionTestMessageListener listener1b("execute: " + page_url.spec(), false); | 129 ExtensionTestMessageListener listener1b("execute: " + page_url.spec(), false); |
123 ui_test_utils::NavigateToURL(browser(), page_url); | 130 ui_test_utils::NavigateToURL(browser(), page_url); |
124 ASSERT_TRUE(listener1a.WaitUntilSatisfied()); | 131 ASSERT_TRUE(listener1a.WaitUntilSatisfied()); |
125 ASSERT_TRUE(listener1b.WaitUntilSatisfied()); | 132 ASSERT_TRUE(listener1b.WaitUntilSatisfied()); |
126 } | 133 } |
| 134 |
| 135 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest, |
| 136 WhitelistedExtensionRunsOnExtensionPages) { |
| 137 WhitelistExtensions(); |
| 138 const Extension* bystander = |
| 139 LoadExtension(test_data_dir_.AppendASCII("all_urls") |
| 140 .AppendASCII("bystander")); |
| 141 ASSERT_TRUE(bystander); |
| 142 |
| 143 // TODO(devlin): This test should probably go in the WhitelistedExtension test |
| 144 // above, but that one has a bunch of disableds, so it wouldn't be very |
| 145 // useful. |
| 146 GURL url(bystander->GetResourceURL("page.html")); |
| 147 ExtensionTestMessageListener listenerA( |
| 148 "content script: " + url.spec(), false); |
| 149 ExtensionTestMessageListener listenerB("execute: " + url.spec(), false); |
| 150 ui_test_utils::NavigateToURL(browser(), GURL(url)); |
| 151 ASSERT_TRUE(listenerA.WaitUntilSatisfied()); |
| 152 ASSERT_TRUE(listenerB.WaitUntilSatisfied()); |
| 153 } |
| 154 |
| 155 } // namespace extensions |
OLD | NEW |