| 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/plugins/plugin_prefs.h" | 5 #include "chrome/browser/plugins/plugin_prefs.h" |
| 6 | 6 |
| 7 #include "base/at_exit.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/path_service.h" | |
| 10 #include "base/run_loop.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 12 #include "build/build_config.h" | |
| 13 #include "chrome/common/chrome_constants.h" | 8 #include "chrome/common/chrome_constants.h" |
| 14 #include "chrome/common/chrome_paths.h" | |
| 15 #include "content/public/browser/plugin_service.h" | |
| 16 #include "content/public/browser/render_process_host.h" | |
| 17 #include "content/public/common/webplugininfo.h" | |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 9 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "content/public/test/test_utils.h" | 10 #include "content/public/test/test_utils.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 12 |
| 22 using base::ASCIIToUTF16; | |
| 23 using content::BrowserThread; | |
| 24 using content::PluginService; | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 void CanEnablePluginCallback(const base::Closure& quit_closure, | |
| 29 bool expected_can_change, | |
| 30 bool did_change) { | |
| 31 EXPECT_EQ(expected_can_change, did_change); | |
| 32 quit_closure.Run(); | |
| 33 } | |
| 34 | |
| 35 #if !(defined(OS_LINUX) && defined(USE_AURA)) | |
| 36 base::FilePath GetComponentUpdatedPepperFlashPath( | |
| 37 const base::FilePath::StringType& version) { | |
| 38 base::FilePath path; | |
| 39 EXPECT_TRUE(PathService::Get( | |
| 40 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &path)); | |
| 41 path = path.Append(version); | |
| 42 path = path.Append(chrome::kPepperFlashPluginFilename); | |
| 43 return path; | |
| 44 } | |
| 45 | |
| 46 base::FilePath GetBundledPepperFlashPath() { | |
| 47 base::FilePath path; | |
| 48 EXPECT_TRUE(PathService::Get(chrome::FILE_PEPPER_FLASH_PLUGIN, &path)); | |
| 49 return path; | |
| 50 } | |
| 51 #endif // !(defined(OS_LINUX) && defined(USE_AURA)) | |
| 52 | |
| 53 void GotPlugins(const base::Closure& quit_closure, | |
| 54 const std::vector<content::WebPluginInfo>& plugins) { | |
| 55 quit_closure.Run(); | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 class PluginPrefsTest : public ::testing::Test { | 13 class PluginPrefsTest : public ::testing::Test { |
| 61 public: | 14 public: |
| 62 void SetUp() override { plugin_prefs_ = new PluginPrefs(); } | 15 void SetUp() override { plugin_prefs_ = new PluginPrefs(); } |
| 63 | 16 |
| 64 void SetPolicyEnforcedPluginPatterns( | |
| 65 const std::set<base::string16>& disabled, | |
| 66 const std::set<base::string16>& disabled_exceptions, | |
| 67 const std::set<base::string16>& enabled) { | |
| 68 plugin_prefs_->SetPolicyEnforcedPluginPatternsForTests( | |
| 69 disabled, disabled_exceptions, enabled); | |
| 70 } | |
| 71 | |
| 72 void SetAlwaysOpenPdfExternally(bool value) { | 17 void SetAlwaysOpenPdfExternally(bool value) { |
| 73 plugin_prefs_->SetAlwaysOpenPdfExternallyForTests(value); | 18 plugin_prefs_->SetAlwaysOpenPdfExternallyForTests(value); |
| 74 } | 19 } |
| 75 | 20 |
| 76 protected: | 21 protected: |
| 77 void EnablePluginSynchronously(bool enabled, | |
| 78 const base::FilePath& path, | |
| 79 bool expected_can_change) { | |
| 80 base::RunLoop run_loop; | |
| 81 plugin_prefs_->EnablePlugin( | |
| 82 enabled, path, | |
| 83 base::Bind(&CanEnablePluginCallback, run_loop.QuitClosure(), | |
| 84 expected_can_change)); | |
| 85 run_loop.Run(); | |
| 86 } | |
| 87 | |
| 88 void RefreshPluginsSynchronously() { | |
| 89 PluginService::GetInstance()->RefreshPlugins(); | |
| 90 #if !defined(OS_WIN) | |
| 91 // Can't go out of process in unit tests. | |
| 92 content::RenderProcessHost::SetRunRendererInProcess(true); | |
| 93 #endif | |
| 94 scoped_refptr<content::MessageLoopRunner> runner = | |
| 95 new content::MessageLoopRunner; | |
| 96 PluginService::GetInstance()->GetPlugins( | |
| 97 base::Bind(&GotPlugins, runner->QuitClosure())); | |
| 98 runner->Run(); | |
| 99 #if !defined(OS_WIN) | |
| 100 content::RenderProcessHost::SetRunRendererInProcess(false); | |
| 101 #endif | |
| 102 } | |
| 103 | |
| 104 scoped_refptr<PluginPrefs> plugin_prefs_; | 22 scoped_refptr<PluginPrefs> plugin_prefs_; |
| 105 }; | 23 }; |
| 106 | 24 |
| 107 TEST_F(PluginPrefsTest, DisabledByPolicy) { | |
| 108 std::set<base::string16> disabled_plugins; | |
| 109 disabled_plugins.insert(ASCIIToUTF16("Disable this!")); | |
| 110 disabled_plugins.insert(ASCIIToUTF16("*Google*")); | |
| 111 SetPolicyEnforcedPluginPatterns(disabled_plugins, | |
| 112 std::set<base::string16>(), | |
| 113 std::set<base::string16>()); | |
| 114 | |
| 115 EXPECT_EQ(PluginPrefs::NO_POLICY, | |
| 116 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("42"))); | |
| 117 EXPECT_EQ(PluginPrefs::POLICY_DISABLED, | |
| 118 plugin_prefs_->PolicyStatusForPlugin( | |
| 119 ASCIIToUTF16("Disable this!"))); | |
| 120 EXPECT_EQ(PluginPrefs::POLICY_DISABLED, | |
| 121 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("Google Earth"))); | |
| 122 } | |
| 123 | |
| 124 TEST_F(PluginPrefsTest, EnabledByPolicy) { | |
| 125 std::set<base::string16> enabled_plugins; | |
| 126 enabled_plugins.insert(ASCIIToUTF16("Enable that!")); | |
| 127 enabled_plugins.insert(ASCIIToUTF16("PDF*")); | |
| 128 SetPolicyEnforcedPluginPatterns(std::set<base::string16>(), | |
| 129 std::set<base::string16>(), | |
| 130 enabled_plugins); | |
| 131 | |
| 132 EXPECT_EQ(PluginPrefs::NO_POLICY, | |
| 133 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("42"))); | |
| 134 EXPECT_EQ(PluginPrefs::POLICY_ENABLED, | |
| 135 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("Enable that!"))); | |
| 136 EXPECT_EQ(PluginPrefs::POLICY_ENABLED, | |
| 137 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("PDF Reader"))); | |
| 138 } | |
| 139 | |
| 140 TEST_F(PluginPrefsTest, EnabledAndDisabledByPolicy) { | |
| 141 const base::string16 k42(ASCIIToUTF16("42")); | |
| 142 const base::string16 kEnabled(ASCIIToUTF16("Enabled")); | |
| 143 const base::string16 kEnabled2(ASCIIToUTF16("Enabled 2")); | |
| 144 const base::string16 kEnabled3(ASCIIToUTF16("Enabled 3")); | |
| 145 const base::string16 kException(ASCIIToUTF16("Exception")); | |
| 146 const base::string16 kException2(ASCIIToUTF16("Exception 2")); | |
| 147 const base::string16 kGoogleMars(ASCIIToUTF16("Google Mars")); | |
| 148 const base::string16 kGoogleEarth(ASCIIToUTF16("Google Earth")); | |
| 149 | |
| 150 std::set<base::string16> disabled_plugins; | |
| 151 std::set<base::string16> disabled_plugins_exceptions; | |
| 152 std::set<base::string16> enabled_plugins; | |
| 153 | |
| 154 disabled_plugins.insert(kEnabled); | |
| 155 disabled_plugins_exceptions.insert(kEnabled); | |
| 156 enabled_plugins.insert(kEnabled); | |
| 157 | |
| 158 disabled_plugins_exceptions.insert(kException); | |
| 159 | |
| 160 disabled_plugins.insert(kEnabled2); | |
| 161 enabled_plugins.insert(kEnabled2); | |
| 162 | |
| 163 disabled_plugins.insert(kException2); | |
| 164 disabled_plugins_exceptions.insert(kException2); | |
| 165 | |
| 166 disabled_plugins_exceptions.insert(kEnabled3); | |
| 167 enabled_plugins.insert(kEnabled3); | |
| 168 | |
| 169 SetPolicyEnforcedPluginPatterns(disabled_plugins, | |
| 170 disabled_plugins_exceptions, | |
| 171 enabled_plugins); | |
| 172 | |
| 173 EXPECT_EQ(PluginPrefs::NO_POLICY, plugin_prefs_->PolicyStatusForPlugin(k42)); | |
| 174 | |
| 175 EXPECT_EQ(PluginPrefs::POLICY_ENABLED, | |
| 176 plugin_prefs_->PolicyStatusForPlugin(kEnabled)); | |
| 177 EXPECT_EQ(PluginPrefs::POLICY_ENABLED, | |
| 178 plugin_prefs_->PolicyStatusForPlugin(kEnabled2)); | |
| 179 EXPECT_EQ(PluginPrefs::POLICY_ENABLED, | |
| 180 plugin_prefs_->PolicyStatusForPlugin(kEnabled3)); | |
| 181 | |
| 182 EXPECT_EQ(PluginPrefs::NO_POLICY, | |
| 183 plugin_prefs_->PolicyStatusForPlugin(kException)); | |
| 184 EXPECT_EQ(PluginPrefs::NO_POLICY, | |
| 185 plugin_prefs_->PolicyStatusForPlugin(kException2)); | |
| 186 | |
| 187 disabled_plugins.clear(); | |
| 188 disabled_plugins_exceptions.clear(); | |
| 189 enabled_plugins.clear(); | |
| 190 | |
| 191 disabled_plugins.insert(ASCIIToUTF16("*")); | |
| 192 disabled_plugins_exceptions.insert(ASCIIToUTF16("*Google*")); | |
| 193 enabled_plugins.insert(kGoogleEarth); | |
| 194 | |
| 195 SetPolicyEnforcedPluginPatterns(disabled_plugins, | |
| 196 disabled_plugins_exceptions, | |
| 197 enabled_plugins); | |
| 198 | |
| 199 EXPECT_EQ(PluginPrefs::POLICY_ENABLED, | |
| 200 plugin_prefs_->PolicyStatusForPlugin(kGoogleEarth)); | |
| 201 EXPECT_EQ(PluginPrefs::NO_POLICY, | |
| 202 plugin_prefs_->PolicyStatusForPlugin(kGoogleMars)); | |
| 203 EXPECT_EQ(PluginPrefs::POLICY_DISABLED, | |
| 204 plugin_prefs_->PolicyStatusForPlugin(k42)); | |
| 205 } | |
| 206 | |
| 207 // Linux Aura doesn't support NPAPI. | |
| 208 #if !(defined(OS_LINUX) && defined(USE_AURA)) | |
| 209 | |
| 210 TEST_F(PluginPrefsTest, UnifiedPepperFlashState) { | |
| 211 content::TestBrowserThreadBundle browser_threads; | |
| 212 base::ShadowingAtExitManager at_exit_manager_; // Destroys the PluginService. | |
| 213 | |
| 214 PluginService::GetInstance()->Init(); | |
| 215 | |
| 216 base::string16 component_updated_plugin_name( | |
| 217 ASCIIToUTF16("Component-updated Pepper Flash")); | |
| 218 content::WebPluginInfo component_updated_plugin_1( | |
| 219 component_updated_plugin_name, | |
| 220 GetComponentUpdatedPepperFlashPath(FILE_PATH_LITERAL("11.3.31.227")), | |
| 221 ASCIIToUTF16("11.3.31.227"), | |
| 222 ASCIIToUTF16("")); | |
| 223 content::WebPluginInfo component_updated_plugin_2( | |
| 224 component_updated_plugin_name, | |
| 225 GetComponentUpdatedPepperFlashPath(FILE_PATH_LITERAL("11.3.31.228")), | |
| 226 ASCIIToUTF16("11.3.31.228"), | |
| 227 ASCIIToUTF16("")); | |
| 228 content::WebPluginInfo bundled_plugin(ASCIIToUTF16("Pepper Flash"), | |
| 229 GetBundledPepperFlashPath(), | |
| 230 ASCIIToUTF16("11.3.31.229"), | |
| 231 ASCIIToUTF16("")); | |
| 232 component_updated_plugin_1.type = | |
| 233 content::WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS; | |
| 234 component_updated_plugin_2.type = | |
| 235 content::WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS; | |
| 236 bundled_plugin.type = | |
| 237 content::WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS; | |
| 238 | |
| 239 PluginService::GetInstance()->RegisterInternalPlugin( | |
| 240 component_updated_plugin_1, false); | |
| 241 PluginService::GetInstance()->RegisterInternalPlugin( | |
| 242 component_updated_plugin_2, false); | |
| 243 PluginService::GetInstance()->RegisterInternalPlugin(bundled_plugin, false); | |
| 244 | |
| 245 RefreshPluginsSynchronously(); | |
| 246 | |
| 247 // Set the state of any of the three plugins will affect the others. | |
| 248 EnablePluginSynchronously(true, component_updated_plugin_1.path, true); | |
| 249 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1)); | |
| 250 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2)); | |
| 251 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
| 252 | |
| 253 EnablePluginSynchronously(false, bundled_plugin.path, true); | |
| 254 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1)); | |
| 255 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2)); | |
| 256 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
| 257 | |
| 258 EnablePluginSynchronously(true, component_updated_plugin_2.path, true); | |
| 259 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1)); | |
| 260 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2)); | |
| 261 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
| 262 | |
| 263 std::set<base::string16> disabled_plugins; | |
| 264 disabled_plugins.insert(component_updated_plugin_name); | |
| 265 SetPolicyEnforcedPluginPatterns(disabled_plugins, | |
| 266 std::set<base::string16>(), | |
| 267 std::set<base::string16>()); | |
| 268 | |
| 269 // Policy settings should be respected. | |
| 270 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1)); | |
| 271 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2)); | |
| 272 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
| 273 | |
| 274 EnablePluginSynchronously(false, bundled_plugin.path, true); | |
| 275 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
| 276 | |
| 277 // Trying to change the state of a policy-enforced plugin should not take | |
| 278 // effect. And it shouldn't change the state of other plugins either, even if | |
| 279 // they are not restricted by any policy. | |
| 280 EnablePluginSynchronously(true, component_updated_plugin_1.path, false); | |
| 281 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1)); | |
| 282 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2)); | |
| 283 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
| 284 | |
| 285 EnablePluginSynchronously(true, bundled_plugin.path, true); | |
| 286 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1)); | |
| 287 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2)); | |
| 288 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
| 289 } | |
| 290 | |
| 291 #endif | |
| 292 | |
| 293 TEST_F(PluginPrefsTest, AlwaysOpenPdfExternally) { | 25 TEST_F(PluginPrefsTest, AlwaysOpenPdfExternally) { |
| 294 EXPECT_EQ(PluginPrefs::NO_POLICY, | 26 EXPECT_EQ(PluginPrefs::NO_POLICY, |
| 295 plugin_prefs_->PolicyStatusForPlugin( | 27 plugin_prefs_->PolicyStatusForPlugin( |
| 296 base::ASCIIToUTF16(ChromeContentClient::kPDFPluginName))); | 28 base::ASCIIToUTF16(ChromeContentClient::kPDFPluginName))); |
| 297 | 29 |
| 298 SetAlwaysOpenPdfExternally(true); | 30 SetAlwaysOpenPdfExternally(true); |
| 299 | 31 |
| 300 EXPECT_EQ(PluginPrefs::POLICY_DISABLED, | 32 EXPECT_EQ(PluginPrefs::POLICY_DISABLED, |
| 301 plugin_prefs_->PolicyStatusForPlugin( | 33 plugin_prefs_->PolicyStatusForPlugin( |
| 302 base::ASCIIToUTF16(ChromeContentClient::kPDFPluginName))); | 34 base::ASCIIToUTF16(ChromeContentClient::kPDFPluginName))); |
| 303 } | 35 } |
| OLD | NEW |