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.
5 #include "base/command_line.h"
6 #include "chrome/browser/extensions/extension_apitest.h"
7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/test/base/test_switches.h"
11 #include "chrome/test/base/ui_test_utils.h"
12 #include "components/crx_file/id_util.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/common/extension.h"
15 #include "extensions/common/extensions_client.h"
16 #include "extensions/test/extension_test_message_listener.h"
17 #include "net/test/embedded_test_server/embedded_test_server.h"
19 namespace extensions
{
22 const std::string kAllUrlsTarget
= "/extensions/api_test/all_urls/index.html";
25 class AllUrlsApiTest
: public ExtensionApiTest
{
28 ~AllUrlsApiTest() override
{}
30 const Extension
* content_script() const { return content_script_
.get(); }
31 const Extension
* execute_script() const { return execute_script_
.get(); }
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());
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_
);
54 scoped_refptr
<const Extension
> content_script_
;
55 scoped_refptr
<const Extension
> execute_script_
;
57 DISALLOW_COPY_AND_ASSIGN(AllUrlsApiTest
);
60 #if (defined(OS_WIN) && !defined(NDEBUG)) || defined(OS_CHROMEOS) || \
61 (defined(OS_MACOSX) && defined(ADDRESS_SANITIZER))
62 // http://crbug.com/174341
63 #define MAYBE_WhitelistedExtension DISABLED_WhitelistedExtension
65 #define MAYBE_WhitelistedExtension WhitelistedExtension
67 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest
, MAYBE_WhitelistedExtension
) {
68 #if defined(OS_WIN) && defined(USE_ASH)
69 // Disable this test in Metro+Ash for now (http://crbug.com/262796).
70 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
71 switches::kAshBrowserTests
))
75 WhitelistExtensions();
79 // Now verify we run content scripts on chrome://newtab/.
80 url
= "chrome://newtab/";
81 ExtensionTestMessageListener
listener1a("content script: " + url
, false);
82 ExtensionTestMessageListener
listener1b("execute: " + url
, false);
83 ui_test_utils::NavigateToURL(browser(), GURL(url
));
84 ASSERT_TRUE(listener1a
.WaitUntilSatisfied());
85 ASSERT_TRUE(listener1b
.WaitUntilSatisfied());
87 // Now verify data: urls.
88 url
= "data:text/html;charset=utf-8,<html>asdf</html>";
89 ExtensionTestMessageListener
listener2a("content script: " + url
, false);
90 ExtensionTestMessageListener
listener2b("execute: " + url
, false);
91 ui_test_utils::NavigateToURL(browser(), GURL(url
));
92 ASSERT_TRUE(listener2a
.WaitUntilSatisfied());
93 ASSERT_TRUE(listener2b
.WaitUntilSatisfied());
95 // Now verify chrome://version/.
96 url
= "chrome://version/";
97 ExtensionTestMessageListener
listener3a("content script: " + url
, false);
98 ExtensionTestMessageListener
listener3b("execute: " + url
, false);
99 ui_test_utils::NavigateToURL(browser(), GURL(url
));
100 ASSERT_TRUE(listener3a
.WaitUntilSatisfied());
101 ASSERT_TRUE(listener3b
.WaitUntilSatisfied());
103 // Now verify about:blank.
105 ExtensionTestMessageListener
listener4a("content script: " + url
, false);
106 ExtensionTestMessageListener
listener4b("execute: " + url
, false);
107 ui_test_utils::NavigateToURL(browser(), GURL(url
));
108 ASSERT_TRUE(listener4a
.WaitUntilSatisfied());
109 ASSERT_TRUE(listener4b
.WaitUntilSatisfied());
111 // Now verify we can script a regular http page.
112 ASSERT_TRUE(StartEmbeddedTestServer());
113 GURL page_url
= embedded_test_server()->GetURL(kAllUrlsTarget
);
114 ExtensionTestMessageListener
listener5a("content script: " + page_url
.spec(),
116 ExtensionTestMessageListener
listener5b("execute: " + page_url
.spec(), false);
117 ui_test_utils::NavigateToURL(browser(), page_url
);
118 ASSERT_TRUE(listener5a
.WaitUntilSatisfied());
119 ASSERT_TRUE(listener5b
.WaitUntilSatisfied());
122 // Test that an extension NOT whitelisted for scripting can ask for <all_urls>
123 // and run scripts on non-restricted all pages.
124 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest
, RegularExtensions
) {
125 // Now verify we can script a regular http page.
126 ASSERT_TRUE(StartEmbeddedTestServer());
127 GURL page_url
= embedded_test_server()->GetURL(kAllUrlsTarget
);
128 ExtensionTestMessageListener
listener1a("content script: " + page_url
.spec(),
130 ExtensionTestMessageListener
listener1b("execute: " + page_url
.spec(), false);
131 ui_test_utils::NavigateToURL(browser(), page_url
);
132 ASSERT_TRUE(listener1a
.WaitUntilSatisfied());
133 ASSERT_TRUE(listener1b
.WaitUntilSatisfied());
136 // Disabled because sometimes bystander doesn't load.
137 // TODO(devlin): Why?
138 IN_PROC_BROWSER_TEST_F(AllUrlsApiTest
,
139 DISABLED_WhitelistedExtensionRunsOnExtensionPages
) {
140 WhitelistExtensions();
141 const Extension
* bystander
=
142 LoadExtension(test_data_dir_
.AppendASCII("all_urls")
143 .AppendASCII("bystander"));
144 ASSERT_TRUE(bystander
);
146 // TODO(devlin): This test should probably go in the WhitelistedExtension test
147 // above, but that one has a bunch of disableds, so it wouldn't be very
149 GURL
url(bystander
->GetResourceURL("page.html"));
150 ExtensionTestMessageListener
listenerA(
151 "content script: " + url
.spec(), false);
152 ExtensionTestMessageListener
listenerB("execute: " + url
.spec(), false);
153 ui_test_utils::NavigateToURL(browser(), GURL(url
));
154 ASSERT_TRUE(listenerA
.WaitUntilSatisfied());
155 ASSERT_TRUE(listenerB
.WaitUntilSatisfied());
158 } // namespace extensions