| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/content_settings/host_content_settings_map.h" | 5 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 6 #include "chrome/browser/extensions/extension_browsertest.h" | 6 #include "chrome/browser/extensions/extension_browsertest.h" |
| 7 #include "chrome/browser/extensions/extension_service.h" | 7 #include "chrome/browser/extensions/extension_service.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/browser_commands.h" | 10 #include "chrome/browser/ui/browser_commands.h" |
| 11 #include "chrome/browser/ui/browser_tabstrip.h" | 11 #include "chrome/browser/ui/browser_tabstrip.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 12 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 13 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
| 14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 15 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
| 16 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
| 17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/test/browser_test_utils.h" |
| 18 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
| 19 | 20 |
| 20 using content::NavigationController; | 21 using content::NavigationController; |
| 21 using content::WebContents; | 22 using content::WebContents; |
| 22 using extensions::Extension; | 23 using extensions::Extension; |
| 23 | 24 |
| 24 // Tests that a renderer's plugin list is properly updated when we load and | 25 // Tests that a renderer's plugin list is properly updated when we load and |
| 25 // unload an extension that contains a plugin. | 26 // unload an extension that contains a plugin. |
| 26 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, PluginLoadUnload) { | 27 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, PluginLoadUnload) { |
| 27 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysAuthorize, | 28 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysAuthorize, |
| 28 true); | 29 true); |
| 29 | 30 |
| 30 FilePath extension_dir = | 31 FilePath extension_dir = |
| 31 test_data_dir_.AppendASCII("uitest").AppendASCII("plugins"); | 32 test_data_dir_.AppendASCII("uitest").AppendASCII("plugins"); |
| 32 | 33 |
| 33 ui_test_utils::NavigateToURL(browser(), | 34 ui_test_utils::NavigateToURL(browser(), |
| 34 net::FilePathToFileURL(extension_dir.AppendASCII("test.html"))); | 35 net::FilePathToFileURL(extension_dir.AppendASCII("test.html"))); |
| 35 WebContents* tab = chrome::GetActiveWebContents(browser()); | 36 WebContents* tab = chrome::GetActiveWebContents(browser()); |
| 36 | 37 |
| 37 // With no extensions, the plugin should not be loaded. | 38 // With no extensions, the plugin should not be loaded. |
| 38 bool result = false; | 39 bool result = false; |
| 39 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 40 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 40 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); | 41 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); |
| 41 EXPECT_FALSE(result); | 42 EXPECT_FALSE(result); |
| 42 | 43 |
| 43 ExtensionService* service = browser()->profile()->GetExtensionService(); | 44 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 44 service->set_show_extensions_prompts(false); | 45 service->set_show_extensions_prompts(false); |
| 45 const size_t size_before = service->extensions()->size(); | 46 const size_t size_before = service->extensions()->size(); |
| 46 const Extension* extension = LoadExtension(extension_dir); | 47 const Extension* extension = LoadExtension(extension_dir); |
| 47 ASSERT_TRUE(extension); | 48 ASSERT_TRUE(extension); |
| 48 EXPECT_EQ(size_before + 1, service->extensions()->size()); | 49 EXPECT_EQ(size_before + 1, service->extensions()->size()); |
| 49 // Now the plugin should be in the cache. | 50 // Now the plugin should be in the cache. |
| 50 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 51 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 51 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); | 52 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); |
| 52 // We don't allow extension plugins to run on ChromeOS. | 53 // We don't allow extension plugins to run on ChromeOS. |
| 53 #if defined(OS_CHROMEOS) | 54 #if defined(OS_CHROMEOS) |
| 54 EXPECT_FALSE(result); | 55 EXPECT_FALSE(result); |
| 55 #else | 56 #else |
| 56 EXPECT_TRUE(result); | 57 EXPECT_TRUE(result); |
| 57 #endif | 58 #endif |
| 58 | 59 |
| 59 EXPECT_EQ(size_before + 1, service->extensions()->size()); | 60 EXPECT_EQ(size_before + 1, service->extensions()->size()); |
| 60 UnloadExtension(extension->id()); | 61 UnloadExtension(extension->id()); |
| 61 EXPECT_EQ(size_before, service->extensions()->size()); | 62 EXPECT_EQ(size_before, service->extensions()->size()); |
| 62 | 63 |
| 63 // Now the plugin should be unloaded, and the page should be broken. | 64 // Now the plugin should be unloaded, and the page should be broken. |
| 64 | 65 |
| 65 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 66 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 66 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); | 67 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); |
| 67 EXPECT_FALSE(result); | 68 EXPECT_FALSE(result); |
| 68 | 69 |
| 69 // If we reload the extension and page, it should work again. | 70 // If we reload the extension and page, it should work again. |
| 70 | 71 |
| 71 ASSERT_TRUE(LoadExtension(extension_dir)); | 72 ASSERT_TRUE(LoadExtension(extension_dir)); |
| 72 EXPECT_EQ(size_before + 1, service->extensions()->size()); | 73 EXPECT_EQ(size_before + 1, service->extensions()->size()); |
| 73 { | 74 { |
| 74 content::WindowedNotificationObserver observer( | 75 content::WindowedNotificationObserver observer( |
| 75 content::NOTIFICATION_LOAD_STOP, | 76 content::NOTIFICATION_LOAD_STOP, |
| 76 content::Source<NavigationController>( | 77 content::Source<NavigationController>( |
| 77 &chrome::GetActiveWebContents(browser())->GetController())); | 78 &chrome::GetActiveWebContents(browser())->GetController())); |
| 78 chrome::Reload(browser(), CURRENT_TAB); | 79 chrome::Reload(browser(), CURRENT_TAB); |
| 79 observer.Wait(); | 80 observer.Wait(); |
| 80 } | 81 } |
| 81 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 82 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 82 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); | 83 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); |
| 83 // We don't allow extension plugins to run on ChromeOS. | 84 // We don't allow extension plugins to run on ChromeOS. |
| 84 #if defined(OS_CHROMEOS) | 85 #if defined(OS_CHROMEOS) |
| 85 EXPECT_FALSE(result); | 86 EXPECT_FALSE(result); |
| 86 #else | 87 #else |
| 87 EXPECT_TRUE(result); | 88 EXPECT_TRUE(result); |
| 88 #endif | 89 #endif |
| 89 } | 90 } |
| 90 | 91 |
| 91 // Tests that private extension plugins are only visible to the extension. | 92 // Tests that private extension plugins are only visible to the extension. |
| 92 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, PluginPrivate) { | 93 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, PluginPrivate) { |
| 93 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysAuthorize, | 94 browser()->profile()->GetPrefs()->SetBoolean(prefs::kPluginsAlwaysAuthorize, |
| 94 true); | 95 true); |
| 95 | 96 |
| 96 FilePath extension_dir = | 97 FilePath extension_dir = |
| 97 test_data_dir_.AppendASCII("uitest").AppendASCII("plugins_private"); | 98 test_data_dir_.AppendASCII("uitest").AppendASCII("plugins_private"); |
| 98 | 99 |
| 99 ExtensionService* service = browser()->profile()->GetExtensionService(); | 100 ExtensionService* service = browser()->profile()->GetExtensionService(); |
| 100 service->set_show_extensions_prompts(false); | 101 service->set_show_extensions_prompts(false); |
| 101 const size_t size_before = service->extensions()->size(); | 102 const size_t size_before = service->extensions()->size(); |
| 102 const Extension* extension = LoadExtension(extension_dir); | 103 const Extension* extension = LoadExtension(extension_dir); |
| 103 ASSERT_TRUE(extension); | 104 ASSERT_TRUE(extension); |
| 104 EXPECT_EQ(size_before + 1, service->extensions()->size()); | 105 EXPECT_EQ(size_before + 1, service->extensions()->size()); |
| 105 | 106 |
| 106 // Load the test page through the extension URL, and the plugin should work. | 107 // Load the test page through the extension URL, and the plugin should work. |
| 107 ui_test_utils::NavigateToURL(browser(), | 108 ui_test_utils::NavigateToURL(browser(), |
| 108 extension->GetResourceURL("test.html")); | 109 extension->GetResourceURL("test.html")); |
| 109 WebContents* tab = chrome::GetActiveWebContents(browser()); | 110 WebContents* tab = chrome::GetActiveWebContents(browser()); |
| 110 bool result = false; | 111 bool result = false; |
| 111 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 112 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 112 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); | 113 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); |
| 113 // We don't allow extension plugins to run on ChromeOS. | 114 // We don't allow extension plugins to run on ChromeOS. |
| 114 #if defined(OS_CHROMEOS) | 115 #if defined(OS_CHROMEOS) |
| 115 EXPECT_FALSE(result); | 116 EXPECT_FALSE(result); |
| 116 #else | 117 #else |
| 117 // TODO(bauerb): This might wrongly fail if the plug-in takes too long | 118 // TODO(bauerb): This might wrongly fail if the plug-in takes too long |
| 118 // to load. Extension-private plug-ins don't appear in navigator.plugins, so | 119 // to load. Extension-private plug-ins don't appear in navigator.plugins, so |
| 119 // we can't check for the plug-in in Javascript. | 120 // we can't check for the plug-in in Javascript. |
| 120 EXPECT_TRUE(result); | 121 EXPECT_TRUE(result); |
| 121 #endif | 122 #endif |
| 122 | 123 |
| 123 // Regression test for http://crbug.com/131811: The plug-in should be | 124 // Regression test for http://crbug.com/131811: The plug-in should be |
| 124 // whitelisted for the extension (and only for the extension), so it should be | 125 // whitelisted for the extension (and only for the extension), so it should be |
| 125 // loaded even if content settings are set to block plug-ins. | 126 // loaded even if content settings are set to block plug-ins. |
| 126 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting( | 127 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting( |
| 127 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK); | 128 CONTENT_SETTINGS_TYPE_PLUGINS, CONTENT_SETTING_BLOCK); |
| 128 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 129 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 129 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); | 130 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); |
| 130 #if defined(OS_CHROMEOS) | 131 #if defined(OS_CHROMEOS) |
| 131 EXPECT_FALSE(result); | 132 EXPECT_FALSE(result); |
| 132 #else | 133 #else |
| 133 EXPECT_TRUE(result); | 134 EXPECT_TRUE(result); |
| 134 #endif | 135 #endif |
| 135 | 136 |
| 136 // Now load it through a file URL. The plugin should not load. | 137 // Now load it through a file URL. The plugin should not load. |
| 137 ui_test_utils::NavigateToURL(browser(), | 138 ui_test_utils::NavigateToURL(browser(), |
| 138 net::FilePathToFileURL(extension_dir.AppendASCII("test.html"))); | 139 net::FilePathToFileURL(extension_dir.AppendASCII("test.html"))); |
| 139 ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool( | 140 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
| 140 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); | 141 tab->GetRenderViewHost(), L"", L"testPluginWorks()", &result)); |
| 141 EXPECT_FALSE(result); | 142 EXPECT_FALSE(result); |
| 142 } | 143 } |
| OLD | NEW |