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 "chrome/browser/plugins/plugin_prefs.h"
7 #include "base/at_exit.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h"
11 #include "base/run_loop.h"
12 #include "base/strings/utf_string_conversions.h"
13 #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.h"
19 #include "content/public/test/test_utils.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using content::BrowserThread;
23 using content::PluginService;
27 void CanEnablePluginCallback(const base::Closure& quit_closure,
28 bool expected_can_change,
30 EXPECT_EQ(expected_can_change, did_change);
34 base::FilePath GetComponentUpdatedPepperFlashPath(
35 const base::FilePath::StringType& version) {
37 EXPECT_TRUE(PathService::Get(
38 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &path));
39 path = path.Append(version);
40 path = path.Append(chrome::kPepperFlashPluginFilename);
44 base::FilePath GetBundledPepperFlashPath() {
46 EXPECT_TRUE(PathService::Get(chrome::FILE_PEPPER_FLASH_PLUGIN, &path));
50 void GotPlugins(const base::Closure& quit_closure,
51 const std::vector<content::WebPluginInfo>& plugins) {
57 class PluginPrefsTest : public ::testing::Test {
59 virtual void SetUp() OVERRIDE {
60 plugin_prefs_ = new PluginPrefs();
63 void SetPolicyEnforcedPluginPatterns(
64 const std::set<string16>& disabled,
65 const std::set<string16>& disabled_exceptions,
66 const std::set<string16>& enabled) {
67 plugin_prefs_->SetPolicyEnforcedPluginPatterns(
68 disabled, disabled_exceptions, enabled);
72 void EnablePluginSynchronously(bool enabled,
73 const base::FilePath& path,
74 bool expected_can_change) {
75 base::RunLoop run_loop;
76 plugin_prefs_->EnablePlugin(
78 base::Bind(&CanEnablePluginCallback, run_loop.QuitClosure(),
79 expected_can_change));
83 scoped_refptr<PluginPrefs> plugin_prefs_;
86 TEST_F(PluginPrefsTest, DisabledByPolicy) {
87 std::set<string16> disabled_plugins;
88 disabled_plugins.insert(ASCIIToUTF16("Disable this!"));
89 disabled_plugins.insert(ASCIIToUTF16("*Google*"));
90 SetPolicyEnforcedPluginPatterns(disabled_plugins,
92 std::set<string16>());
94 EXPECT_EQ(PluginPrefs::NO_POLICY,
95 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("42")));
96 EXPECT_EQ(PluginPrefs::POLICY_DISABLED,
97 plugin_prefs_->PolicyStatusForPlugin(
98 ASCIIToUTF16("Disable this!")));
99 EXPECT_EQ(PluginPrefs::POLICY_DISABLED,
100 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("Google Earth")));
103 TEST_F(PluginPrefsTest, EnabledByPolicy) {
104 std::set<string16> enabled_plugins;
105 enabled_plugins.insert(ASCIIToUTF16("Enable that!"));
106 enabled_plugins.insert(ASCIIToUTF16("PDF*"));
107 SetPolicyEnforcedPluginPatterns(std::set<string16>(),
108 std::set<string16>(),
111 EXPECT_EQ(PluginPrefs::NO_POLICY,
112 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("42")));
113 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
114 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("Enable that!")));
115 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
116 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("PDF Reader")));
119 TEST_F(PluginPrefsTest, EnabledAndDisabledByPolicy) {
120 const string16 k42(ASCIIToUTF16("42"));
121 const string16 kEnabled(ASCIIToUTF16("Enabled"));
122 const string16 kEnabled2(ASCIIToUTF16("Enabled 2"));
123 const string16 kEnabled3(ASCIIToUTF16("Enabled 3"));
124 const string16 kException(ASCIIToUTF16("Exception"));
125 const string16 kException2(ASCIIToUTF16("Exception 2"));
126 const string16 kGoogleMars(ASCIIToUTF16("Google Mars"));
127 const string16 kGoogleEarth(ASCIIToUTF16("Google Earth"));
129 std::set<string16> disabled_plugins;
130 std::set<string16> disabled_plugins_exceptions;
131 std::set<string16> enabled_plugins;
133 disabled_plugins.insert(kEnabled);
134 disabled_plugins_exceptions.insert(kEnabled);
135 enabled_plugins.insert(kEnabled);
137 disabled_plugins_exceptions.insert(kException);
139 disabled_plugins.insert(kEnabled2);
140 enabled_plugins.insert(kEnabled2);
142 disabled_plugins.insert(kException2);
143 disabled_plugins_exceptions.insert(kException2);
145 disabled_plugins_exceptions.insert(kEnabled3);
146 enabled_plugins.insert(kEnabled3);
148 SetPolicyEnforcedPluginPatterns(disabled_plugins,
149 disabled_plugins_exceptions,
152 EXPECT_EQ(PluginPrefs::NO_POLICY, plugin_prefs_->PolicyStatusForPlugin(k42));
154 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
155 plugin_prefs_->PolicyStatusForPlugin(kEnabled));
156 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
157 plugin_prefs_->PolicyStatusForPlugin(kEnabled2));
158 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
159 plugin_prefs_->PolicyStatusForPlugin(kEnabled3));
161 EXPECT_EQ(PluginPrefs::NO_POLICY,
162 plugin_prefs_->PolicyStatusForPlugin(kException));
163 EXPECT_EQ(PluginPrefs::NO_POLICY,
164 plugin_prefs_->PolicyStatusForPlugin(kException2));
166 disabled_plugins.clear();
167 disabled_plugins_exceptions.clear();
168 enabled_plugins.clear();
170 disabled_plugins.insert(ASCIIToUTF16("*"));
171 disabled_plugins_exceptions.insert(ASCIIToUTF16("*Google*"));
172 enabled_plugins.insert(kGoogleEarth);
174 SetPolicyEnforcedPluginPatterns(disabled_plugins,
175 disabled_plugins_exceptions,
178 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
179 plugin_prefs_->PolicyStatusForPlugin(kGoogleEarth));
180 EXPECT_EQ(PluginPrefs::NO_POLICY,
181 plugin_prefs_->PolicyStatusForPlugin(kGoogleMars));
182 EXPECT_EQ(PluginPrefs::POLICY_DISABLED,
183 plugin_prefs_->PolicyStatusForPlugin(k42));
186 // Linux Aura doesn't support NPAPI.
187 #if !(defined(OS_LINUX) && defined(USE_AURA))
189 TEST_F(PluginPrefsTest, UnifiedPepperFlashState) {
190 base::ShadowingAtExitManager at_exit_manager_; // Destroys the PluginService.
192 base::MessageLoop message_loop;
193 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
194 PluginService::GetInstance()->Init();
195 PluginService::GetInstance()->DisablePluginsDiscoveryForTesting();
197 string16 component_updated_plugin_name(
198 ASCIIToUTF16("Component-updated Pepper Flash"));
199 content::WebPluginInfo component_updated_plugin_1(
200 component_updated_plugin_name,
201 GetComponentUpdatedPepperFlashPath(FILE_PATH_LITERAL("11.3.31.227")),
202 ASCIIToUTF16("11.3.31.227"),
204 content::WebPluginInfo component_updated_plugin_2(
205 component_updated_plugin_name,
206 GetComponentUpdatedPepperFlashPath(FILE_PATH_LITERAL("11.3.31.228")),
207 ASCIIToUTF16("11.3.31.228"),
209 content::WebPluginInfo bundled_plugin(ASCIIToUTF16("Pepper Flash"),
210 GetBundledPepperFlashPath(),
211 ASCIIToUTF16("11.3.31.229"),
214 PluginService::GetInstance()->RegisterInternalPlugin(
215 component_updated_plugin_1, false);
216 PluginService::GetInstance()->RegisterInternalPlugin(
217 component_updated_plugin_2, false);
218 PluginService::GetInstance()->RegisterInternalPlugin(bundled_plugin, false);
221 // Can't go out of process in unit tests.
222 content::RenderProcessHost::SetRunRendererInProcess(true);
224 scoped_refptr<content::MessageLoopRunner> runner =
225 new content::MessageLoopRunner;
226 PluginService::GetInstance()->GetPlugins(
227 base::Bind(&GotPlugins, runner->QuitClosure()));
230 content::RenderProcessHost::SetRunRendererInProcess(false);
233 // Set the state of any of the three plugins will affect the others.
234 EnablePluginSynchronously(true, component_updated_plugin_1.path, true);
235 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
236 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
237 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
239 EnablePluginSynchronously(false, bundled_plugin.path, true);
240 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
241 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
242 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
244 EnablePluginSynchronously(true, component_updated_plugin_2.path, true);
245 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
246 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
247 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
249 std::set<string16> disabled_plugins;
250 disabled_plugins.insert(component_updated_plugin_name);
251 SetPolicyEnforcedPluginPatterns(disabled_plugins,
252 std::set<string16>(),
253 std::set<string16>());
255 // Policy settings should be respected.
256 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
257 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
258 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
260 EnablePluginSynchronously(false, bundled_plugin.path, true);
261 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
263 // Trying to change the state of a policy-enforced plugin should not take
264 // effect. And it shouldn't change the state of other plugins either, even if
265 // they are not restricted by any policy.
266 EnablePluginSynchronously(true, component_updated_plugin_1.path, false);
267 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
268 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
269 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
271 EnablePluginSynchronously(true, bundled_plugin.path, true);
272 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
273 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
274 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin));