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 base::ASCIIToUTF16
;
23 using content::BrowserThread
;
24 using content::PluginService
;
28 void CanEnablePluginCallback(const base::Closure
& quit_closure
,
29 bool expected_can_change
,
31 EXPECT_EQ(expected_can_change
, did_change
);
35 #if !(defined(OS_LINUX) && defined(USE_AURA))
36 base::FilePath
GetComponentUpdatedPepperFlashPath(
37 const base::FilePath::StringType
& version
) {
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
);
46 base::FilePath
GetBundledPepperFlashPath() {
48 EXPECT_TRUE(PathService::Get(chrome::FILE_PEPPER_FLASH_PLUGIN
, &path
));
51 #endif // !(defined(OS_LINUX) && defined(USE_AURA))
53 void GotPlugins(const base::Closure
& quit_closure
,
54 const std::vector
<content::WebPluginInfo
>& plugins
) {
60 class PluginPrefsTest
: public ::testing::Test
{
62 virtual void SetUp() OVERRIDE
{
63 plugin_prefs_
= new PluginPrefs();
66 void SetPolicyEnforcedPluginPatterns(
67 const std::set
<base::string16
>& disabled
,
68 const std::set
<base::string16
>& disabled_exceptions
,
69 const std::set
<base::string16
>& enabled
) {
70 plugin_prefs_
->SetPolicyEnforcedPluginPatterns(
71 disabled
, disabled_exceptions
, enabled
);
75 void EnablePluginSynchronously(bool enabled
,
76 const base::FilePath
& path
,
77 bool expected_can_change
) {
78 base::RunLoop run_loop
;
79 plugin_prefs_
->EnablePlugin(
81 base::Bind(&CanEnablePluginCallback
, run_loop
.QuitClosure(),
82 expected_can_change
));
86 void RefreshPluginsSynchronously() {
87 PluginService::GetInstance()->RefreshPlugins();
89 // Can't go out of process in unit tests.
90 content::RenderProcessHost::SetRunRendererInProcess(true);
92 scoped_refptr
<content::MessageLoopRunner
> runner
=
93 new content::MessageLoopRunner
;
94 PluginService::GetInstance()->GetPlugins(
95 base::Bind(&GotPlugins
, runner
->QuitClosure()));
98 content::RenderProcessHost::SetRunRendererInProcess(false);
102 scoped_refptr
<PluginPrefs
> plugin_prefs_
;
105 TEST_F(PluginPrefsTest
, DisabledByPolicy
) {
106 std::set
<base::string16
> disabled_plugins
;
107 disabled_plugins
.insert(ASCIIToUTF16("Disable this!"));
108 disabled_plugins
.insert(ASCIIToUTF16("*Google*"));
109 SetPolicyEnforcedPluginPatterns(disabled_plugins
,
110 std::set
<base::string16
>(),
111 std::set
<base::string16
>());
113 EXPECT_EQ(PluginPrefs::NO_POLICY
,
114 plugin_prefs_
->PolicyStatusForPlugin(ASCIIToUTF16("42")));
115 EXPECT_EQ(PluginPrefs::POLICY_DISABLED
,
116 plugin_prefs_
->PolicyStatusForPlugin(
117 ASCIIToUTF16("Disable this!")));
118 EXPECT_EQ(PluginPrefs::POLICY_DISABLED
,
119 plugin_prefs_
->PolicyStatusForPlugin(ASCIIToUTF16("Google Earth")));
122 TEST_F(PluginPrefsTest
, EnabledByPolicy
) {
123 std::set
<base::string16
> enabled_plugins
;
124 enabled_plugins
.insert(ASCIIToUTF16("Enable that!"));
125 enabled_plugins
.insert(ASCIIToUTF16("PDF*"));
126 SetPolicyEnforcedPluginPatterns(std::set
<base::string16
>(),
127 std::set
<base::string16
>(),
130 EXPECT_EQ(PluginPrefs::NO_POLICY
,
131 plugin_prefs_
->PolicyStatusForPlugin(ASCIIToUTF16("42")));
132 EXPECT_EQ(PluginPrefs::POLICY_ENABLED
,
133 plugin_prefs_
->PolicyStatusForPlugin(ASCIIToUTF16("Enable that!")));
134 EXPECT_EQ(PluginPrefs::POLICY_ENABLED
,
135 plugin_prefs_
->PolicyStatusForPlugin(ASCIIToUTF16("PDF Reader")));
138 TEST_F(PluginPrefsTest
, EnabledAndDisabledByPolicy
) {
139 const base::string16
k42(ASCIIToUTF16("42"));
140 const base::string16
kEnabled(ASCIIToUTF16("Enabled"));
141 const base::string16
kEnabled2(ASCIIToUTF16("Enabled 2"));
142 const base::string16
kEnabled3(ASCIIToUTF16("Enabled 3"));
143 const base::string16
kException(ASCIIToUTF16("Exception"));
144 const base::string16
kException2(ASCIIToUTF16("Exception 2"));
145 const base::string16
kGoogleMars(ASCIIToUTF16("Google Mars"));
146 const base::string16
kGoogleEarth(ASCIIToUTF16("Google Earth"));
148 std::set
<base::string16
> disabled_plugins
;
149 std::set
<base::string16
> disabled_plugins_exceptions
;
150 std::set
<base::string16
> enabled_plugins
;
152 disabled_plugins
.insert(kEnabled
);
153 disabled_plugins_exceptions
.insert(kEnabled
);
154 enabled_plugins
.insert(kEnabled
);
156 disabled_plugins_exceptions
.insert(kException
);
158 disabled_plugins
.insert(kEnabled2
);
159 enabled_plugins
.insert(kEnabled2
);
161 disabled_plugins
.insert(kException2
);
162 disabled_plugins_exceptions
.insert(kException2
);
164 disabled_plugins_exceptions
.insert(kEnabled3
);
165 enabled_plugins
.insert(kEnabled3
);
167 SetPolicyEnforcedPluginPatterns(disabled_plugins
,
168 disabled_plugins_exceptions
,
171 EXPECT_EQ(PluginPrefs::NO_POLICY
, plugin_prefs_
->PolicyStatusForPlugin(k42
));
173 EXPECT_EQ(PluginPrefs::POLICY_ENABLED
,
174 plugin_prefs_
->PolicyStatusForPlugin(kEnabled
));
175 EXPECT_EQ(PluginPrefs::POLICY_ENABLED
,
176 plugin_prefs_
->PolicyStatusForPlugin(kEnabled2
));
177 EXPECT_EQ(PluginPrefs::POLICY_ENABLED
,
178 plugin_prefs_
->PolicyStatusForPlugin(kEnabled3
));
180 EXPECT_EQ(PluginPrefs::NO_POLICY
,
181 plugin_prefs_
->PolicyStatusForPlugin(kException
));
182 EXPECT_EQ(PluginPrefs::NO_POLICY
,
183 plugin_prefs_
->PolicyStatusForPlugin(kException2
));
185 disabled_plugins
.clear();
186 disabled_plugins_exceptions
.clear();
187 enabled_plugins
.clear();
189 disabled_plugins
.insert(ASCIIToUTF16("*"));
190 disabled_plugins_exceptions
.insert(ASCIIToUTF16("*Google*"));
191 enabled_plugins
.insert(kGoogleEarth
);
193 SetPolicyEnforcedPluginPatterns(disabled_plugins
,
194 disabled_plugins_exceptions
,
197 EXPECT_EQ(PluginPrefs::POLICY_ENABLED
,
198 plugin_prefs_
->PolicyStatusForPlugin(kGoogleEarth
));
199 EXPECT_EQ(PluginPrefs::NO_POLICY
,
200 plugin_prefs_
->PolicyStatusForPlugin(kGoogleMars
));
201 EXPECT_EQ(PluginPrefs::POLICY_DISABLED
,
202 plugin_prefs_
->PolicyStatusForPlugin(k42
));
205 // Linux Aura doesn't support NPAPI.
206 #if !(defined(OS_LINUX) && defined(USE_AURA))
208 TEST_F(PluginPrefsTest
, UnifiedPepperFlashState
) {
209 base::ShadowingAtExitManager at_exit_manager_
; // Destroys the PluginService.
211 base::MessageLoop message_loop
;
212 content::TestBrowserThread
ui_thread(BrowserThread::UI
, &message_loop
);
213 PluginService::GetInstance()->Init();
214 PluginService::GetInstance()->DisablePluginsDiscoveryForTesting();
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"),
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"),
228 content::WebPluginInfo
bundled_plugin(ASCIIToUTF16("Pepper Flash"),
229 GetBundledPepperFlashPath(),
230 ASCIIToUTF16("11.3.31.229"),
233 PluginService::GetInstance()->RegisterInternalPlugin(
234 component_updated_plugin_1
, false);
235 PluginService::GetInstance()->RegisterInternalPlugin(
236 component_updated_plugin_2
, false);
237 PluginService::GetInstance()->RegisterInternalPlugin(bundled_plugin
, false);
239 RefreshPluginsSynchronously();
241 // Set the state of any of the three plugins will affect the others.
242 EnablePluginSynchronously(true, component_updated_plugin_1
.path
, true);
243 EXPECT_TRUE(plugin_prefs_
->IsPluginEnabled(component_updated_plugin_1
));
244 EXPECT_TRUE(plugin_prefs_
->IsPluginEnabled(component_updated_plugin_2
));
245 EXPECT_TRUE(plugin_prefs_
->IsPluginEnabled(bundled_plugin
));
247 EnablePluginSynchronously(false, bundled_plugin
.path
, true);
248 EXPECT_FALSE(plugin_prefs_
->IsPluginEnabled(component_updated_plugin_1
));
249 EXPECT_FALSE(plugin_prefs_
->IsPluginEnabled(component_updated_plugin_2
));
250 EXPECT_FALSE(plugin_prefs_
->IsPluginEnabled(bundled_plugin
));
252 EnablePluginSynchronously(true, component_updated_plugin_2
.path
, true);
253 EXPECT_TRUE(plugin_prefs_
->IsPluginEnabled(component_updated_plugin_1
));
254 EXPECT_TRUE(plugin_prefs_
->IsPluginEnabled(component_updated_plugin_2
));
255 EXPECT_TRUE(plugin_prefs_
->IsPluginEnabled(bundled_plugin
));
257 std::set
<base::string16
> disabled_plugins
;
258 disabled_plugins
.insert(component_updated_plugin_name
);
259 SetPolicyEnforcedPluginPatterns(disabled_plugins
,
260 std::set
<base::string16
>(),
261 std::set
<base::string16
>());
263 // Policy settings should be respected.
264 EXPECT_FALSE(plugin_prefs_
->IsPluginEnabled(component_updated_plugin_1
));
265 EXPECT_FALSE(plugin_prefs_
->IsPluginEnabled(component_updated_plugin_2
));
266 EXPECT_TRUE(plugin_prefs_
->IsPluginEnabled(bundled_plugin
));
268 EnablePluginSynchronously(false, bundled_plugin
.path
, true);
269 EXPECT_FALSE(plugin_prefs_
->IsPluginEnabled(bundled_plugin
));
271 // Trying to change the state of a policy-enforced plugin should not take
272 // effect. And it shouldn't change the state of other plugins either, even if
273 // they are not restricted by any policy.
274 EnablePluginSynchronously(true, component_updated_plugin_1
.path
, false);
275 EXPECT_FALSE(plugin_prefs_
->IsPluginEnabled(component_updated_plugin_1
));
276 EXPECT_FALSE(plugin_prefs_
->IsPluginEnabled(component_updated_plugin_2
));
277 EXPECT_FALSE(plugin_prefs_
->IsPluginEnabled(bundled_plugin
));
279 EnablePluginSynchronously(true, bundled_plugin
.path
, true);
280 EXPECT_FALSE(plugin_prefs_
->IsPluginEnabled(component_updated_plugin_1
));
281 EXPECT_FALSE(plugin_prefs_
->IsPluginEnabled(component_updated_plugin_2
));
282 EXPECT_TRUE(plugin_prefs_
->IsPluginEnabled(bundled_plugin
));