Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_nacl_browsertest.cc
blob2f4652bed9695eec8e4e854a9c3a03b48bbdee24
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
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "base/files/file_path.h"
7 #include "base/path_service.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/extensions/crx_installer.h"
11 #include "chrome/browser/extensions/extension_browsertest.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_system.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "content/public/browser/plugin_service.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/webplugininfo.h"
24 #include "content/public/test/browser_test_utils.h"
25 #include "net/dns/mock_host_resolver.h"
27 using content::PluginService;
28 using content::WebContents;
29 using extensions::Extension;
30 using extensions::Manifest;
32 namespace {
34 const char* kExtensionId = "bjjcibdiodkkeanflmiijlcfieiemced";
36 // This class tests that the Native Client plugin is blocked unless the
37 // .nexe is part of an extension from the Chrome Webstore.
38 class NaClExtensionTest : public ExtensionBrowserTest {
39 public:
40 NaClExtensionTest() {}
42 protected:
43 enum InstallType {
44 INSTALL_TYPE_COMPONENT,
45 INSTALL_TYPE_UNPACKED,
46 INSTALL_TYPE_FROM_WEBSTORE,
47 INSTALL_TYPE_NON_WEBSTORE,
49 enum PluginType {
50 PLUGIN_TYPE_NONE = 0,
51 PLUGIN_TYPE_EMBED = 1,
52 PLUGIN_TYPE_CONTENT_HANDLER = 2,
53 PLUGIN_TYPE_ALL = PLUGIN_TYPE_EMBED |
54 PLUGIN_TYPE_CONTENT_HANDLER,
58 const Extension* InstallExtension(const base::FilePath& file_path,
59 InstallType install_type) {
60 ExtensionService* service = extensions::ExtensionSystem::Get(
61 browser()->profile())->extension_service();
62 const Extension* extension = NULL;
63 switch (install_type) {
64 case INSTALL_TYPE_COMPONENT:
65 if (LoadExtensionAsComponent(file_path)) {
66 extension = service->GetExtensionById(kExtensionId, false);
68 break;
70 case INSTALL_TYPE_UNPACKED:
71 // Install the extension from a folder so it's unpacked.
72 if (LoadExtension(file_path)) {
73 extension = service->GetExtensionById(kExtensionId, false);
75 break;
77 case INSTALL_TYPE_FROM_WEBSTORE:
78 // Install native_client.crx from the webstore.
79 if (InstallExtensionFromWebstore(file_path, 1)) {
80 extension = service->GetExtensionById(last_loaded_extension_id(),
81 false);
83 break;
85 case INSTALL_TYPE_NON_WEBSTORE:
86 // Install native_client.crx but not from the webstore.
87 if (ExtensionBrowserTest::InstallExtension(file_path, 1)) {
88 extension = service->GetExtensionById(last_loaded_extension_id(),
89 false);
91 break;
93 return extension;
96 const Extension* InstallExtension(InstallType install_type) {
97 base::FilePath file_path = test_data_dir_.AppendASCII("native_client");
98 return InstallExtension(file_path, install_type);
101 const Extension* InstallHostedApp() {
102 base::FilePath file_path = test_data_dir_.AppendASCII(
103 "native_client_hosted_app");
104 return InstallExtension(file_path, INSTALL_TYPE_FROM_WEBSTORE);
107 bool IsNaClPluginLoaded() {
108 base::FilePath path;
109 if (PathService::Get(chrome::FILE_NACL_PLUGIN, &path)) {
110 content::WebPluginInfo info;
111 return PluginService::GetInstance()->GetPluginInfoByPath(path, &info);
113 return false;
116 void CheckPluginsCreated(const GURL& url, PluginType expected_to_succeed) {
117 ui_test_utils::NavigateToURL(browser(), url);
118 // Don't run tests if the NaCl plugin isn't loaded.
119 if (!IsNaClPluginLoaded())
120 return;
122 bool embedded_plugin_created = false;
123 bool content_handler_plugin_created = false;
124 WebContents* web_contents =
125 browser()->tab_strip_model()->GetActiveWebContents();
126 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
127 web_contents,
128 "window.domAutomationController.send(EmbeddedPluginCreated());",
129 &embedded_plugin_created));
130 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
131 web_contents,
132 "window.domAutomationController.send(ContentHandlerPluginCreated());",
133 &content_handler_plugin_created));
135 EXPECT_EQ(embedded_plugin_created,
136 (expected_to_succeed & PLUGIN_TYPE_EMBED) != 0);
137 EXPECT_EQ(content_handler_plugin_created,
138 (expected_to_succeed & PLUGIN_TYPE_CONTENT_HANDLER) != 0);
141 void CheckPluginsCreated(const Extension* extension,
142 PluginType expected_to_succeed) {
143 CheckPluginsCreated(extension->GetResourceURL("test.html"),
144 expected_to_succeed);
149 // Test that the NaCl plugin isn't blocked for Webstore extensions.
150 // Disabled: http://crbug.com/319892
151 IN_PROC_BROWSER_TEST_F(NaClExtensionTest, DISABLED_WebStoreExtension) {
152 ASSERT_TRUE(test_server()->Start());
154 const Extension* extension = InstallExtension(INSTALL_TYPE_FROM_WEBSTORE);
155 ASSERT_TRUE(extension);
156 CheckPluginsCreated(extension, PLUGIN_TYPE_ALL);
159 // Test that the NaCl plugin is blocked for non-Webstore extensions.
160 // Disabled: http://crbug.com/319892
161 IN_PROC_BROWSER_TEST_F(NaClExtensionTest, DISABLED_NonWebStoreExtension) {
162 ASSERT_TRUE(test_server()->Start());
164 const Extension* extension = InstallExtension(INSTALL_TYPE_NON_WEBSTORE);
165 ASSERT_TRUE(extension);
166 CheckPluginsCreated(extension, PLUGIN_TYPE_NONE);
169 // Test that the NaCl plugin isn't blocked for component extensions.
170 // Disabled: http://crbug.com/319892
171 IN_PROC_BROWSER_TEST_F(NaClExtensionTest, DISABLED_ComponentExtension) {
172 ASSERT_TRUE(test_server()->Start());
174 const Extension* extension = InstallExtension(INSTALL_TYPE_COMPONENT);
175 ASSERT_TRUE(extension);
176 ASSERT_EQ(extension->location(), Manifest::COMPONENT);
177 CheckPluginsCreated(extension, PLUGIN_TYPE_ALL);
180 // Test that the NaCl plugin isn't blocked for unpacked extensions.
181 // Disabled: http://crbug.com/319892
182 IN_PROC_BROWSER_TEST_F(NaClExtensionTest, DISABLED_UnpackedExtension) {
183 ASSERT_TRUE(test_server()->Start());
185 const Extension* extension = InstallExtension(INSTALL_TYPE_UNPACKED);
186 ASSERT_TRUE(extension);
187 ASSERT_EQ(extension->location(), Manifest::UNPACKED);
188 CheckPluginsCreated(extension, PLUGIN_TYPE_ALL);
191 // Test that the NaCl plugin is blocked for non chrome-extension urls, except
192 // if it's a content (MIME type) handler.
193 // Disabled: http://crbug.com/319892
194 IN_PROC_BROWSER_TEST_F(NaClExtensionTest, DISABLED_NonExtensionScheme) {
195 ASSERT_TRUE(test_server()->Start());
197 const Extension* extension = InstallExtension(INSTALL_TYPE_FROM_WEBSTORE);
198 ASSERT_TRUE(extension);
199 CheckPluginsCreated(
200 test_server()->GetURL("files/extensions/native_client/test.html"),
201 PLUGIN_TYPE_CONTENT_HANDLER);
204 // Test that NaCl plugin isn't blocked for hosted app URLs.
205 IN_PROC_BROWSER_TEST_F(NaClExtensionTest, HostedApp) {
206 host_resolver()->AddRule("*", "127.0.0.1");
207 ASSERT_TRUE(test_server()->Start());
209 GURL url = test_server()->GetURL("files/extensions/native_client/test.html");
210 GURL::Replacements replace_host;
211 std::string host_str("localhost");
212 replace_host.SetHostStr(host_str);
213 replace_host.ClearPort();
214 url = url.ReplaceComponents(replace_host);
216 const Extension* extension = InstallHostedApp();
217 ASSERT_TRUE(extension);
218 CheckPluginsCreated(url, PLUGIN_TYPE_ALL);
221 } // namespace