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/test/browser_test_utils.h"
24 #include "net/dns/mock_host_resolver.h"
25 #include "webkit/plugins/webplugininfo.h"
27 using content::PluginService
;
28 using content::WebContents
;
29 using extensions::Extension
;
30 using extensions::Manifest
;
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
{
40 NaClExtensionTest() {}
44 INSTALL_TYPE_COMPONENT
,
45 INSTALL_TYPE_UNPACKED
,
46 INSTALL_TYPE_FROM_WEBSTORE
,
47 INSTALL_TYPE_NON_WEBSTORE
,
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);
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);
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_
,
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_
,
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() {
109 if (PathService::Get(chrome::FILE_NACL_PLUGIN
, &path
)) {
110 webkit::WebPluginInfo info
;
111 return PluginService::GetInstance()->GetPluginInfoByPath(path
, &info
);
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())
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(
128 "window.domAutomationController.send(EmbeddedPluginCreated());",
129 &embedded_plugin_created
));
130 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
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 IN_PROC_BROWSER_TEST_F(NaClExtensionTest
, WebStoreExtension
) {
151 ASSERT_TRUE(test_server()->Start());
153 const Extension
* extension
= InstallExtension(INSTALL_TYPE_FROM_WEBSTORE
);
154 ASSERT_TRUE(extension
);
155 CheckPluginsCreated(extension
, PLUGIN_TYPE_ALL
);
158 // Test that the NaCl plugin is blocked for non-Webstore extensions.
159 IN_PROC_BROWSER_TEST_F(NaClExtensionTest
, NonWebStoreExtension
) {
160 ASSERT_TRUE(test_server()->Start());
162 const Extension
* extension
= InstallExtension(INSTALL_TYPE_NON_WEBSTORE
);
163 ASSERT_TRUE(extension
);
164 CheckPluginsCreated(extension
, PLUGIN_TYPE_NONE
);
167 // Test that the NaCl plugin isn't blocked for component extensions.
168 IN_PROC_BROWSER_TEST_F(NaClExtensionTest
, ComponentExtension
) {
169 ASSERT_TRUE(test_server()->Start());
171 const Extension
* extension
= InstallExtension(INSTALL_TYPE_COMPONENT
);
172 ASSERT_TRUE(extension
);
173 ASSERT_EQ(extension
->location(), Manifest::COMPONENT
);
174 CheckPluginsCreated(extension
, PLUGIN_TYPE_ALL
);
177 // Test that the NaCl plugin isn't blocked for unpacked extensions.
178 IN_PROC_BROWSER_TEST_F(NaClExtensionTest
, UnpackedExtension
) {
179 ASSERT_TRUE(test_server()->Start());
181 const Extension
* extension
= InstallExtension(INSTALL_TYPE_UNPACKED
);
182 ASSERT_TRUE(extension
);
183 ASSERT_EQ(extension
->location(), Manifest::UNPACKED
);
184 CheckPluginsCreated(extension
, PLUGIN_TYPE_ALL
);
187 // Test that the NaCl plugin is blocked for non chrome-extension urls, except
188 // if it's a content (MIME type) handler.
189 IN_PROC_BROWSER_TEST_F(NaClExtensionTest
, NonExtensionScheme
) {
190 ASSERT_TRUE(test_server()->Start());
192 const Extension
* extension
= InstallExtension(INSTALL_TYPE_FROM_WEBSTORE
);
193 ASSERT_TRUE(extension
);
195 test_server()->GetURL("files/extensions/native_client/test.html"),
196 PLUGIN_TYPE_CONTENT_HANDLER
);
199 // Test that NaCl plugin isn't blocked for hosted app URLs.
200 IN_PROC_BROWSER_TEST_F(NaClExtensionTest
, HostedApp
) {
201 host_resolver()->AddRule("*", "127.0.0.1");
202 ASSERT_TRUE(test_server()->Start());
204 GURL url
= test_server()->GetURL("files/extensions/native_client/test.html");
205 GURL::Replacements replace_host
;
206 std::string
host_str("localhost");
207 replace_host
.SetHostStr(host_str
);
208 replace_host
.ClearPort();
209 url
= url
.ReplaceComponents(replace_host
);
211 const Extension
* extension
= InstallHostedApp();
212 ASSERT_TRUE(extension
);
213 CheckPluginsCreated(url
, PLUGIN_TYPE_ALL
);