Home | History | Annotate | Download | only in plugins
      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.
      4 
      5 #include "chrome/browser/plugins/plugin_prefs.h"
      6 
      7 #include "base/at_exit.h"
      8 #include "base/bind.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"
     21 
     22 using content::BrowserThread;
     23 using content::PluginService;
     24 
     25 namespace {
     26 
     27 void CanEnablePluginCallback(const base::Closure& quit_closure,
     28                              bool expected_can_change,
     29                              bool did_change) {
     30   EXPECT_EQ(expected_can_change, did_change);
     31   quit_closure.Run();
     32 }
     33 
     34 base::FilePath GetComponentUpdatedPepperFlashPath(
     35     const base::FilePath::StringType& version) {
     36   base::FilePath path;
     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);
     41   return path;
     42 }
     43 
     44 base::FilePath GetBundledPepperFlashPath() {
     45   base::FilePath path;
     46   EXPECT_TRUE(PathService::Get(chrome::FILE_PEPPER_FLASH_PLUGIN, &path));
     47   return path;
     48 }
     49 
     50 void GotPlugins(const base::Closure& quit_closure,
     51                 const std::vector<content::WebPluginInfo>& plugins) {
     52   quit_closure.Run();
     53 }
     54 
     55 }  // namespace
     56 
     57 class PluginPrefsTest : public ::testing::Test {
     58  public:
     59   virtual void SetUp() OVERRIDE {
     60     plugin_prefs_ = new PluginPrefs();
     61   }
     62 
     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);
     69   }
     70 
     71  protected:
     72   void EnablePluginSynchronously(bool enabled,
     73                                  const base::FilePath& path,
     74                                  bool expected_can_change) {
     75     base::RunLoop run_loop;
     76     plugin_prefs_->EnablePlugin(
     77         enabled, path,
     78         base::Bind(&CanEnablePluginCallback, run_loop.QuitClosure(),
     79                    expected_can_change));
     80     run_loop.Run();
     81   }
     82 
     83   scoped_refptr<PluginPrefs> plugin_prefs_;
     84 };
     85 
     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,
     91                                   std::set<string16>(),
     92                                   std::set<string16>());
     93 
     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")));
    101 }
    102 
    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>(),
    109                                   enabled_plugins);
    110 
    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")));
    117 }
    118 
    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"));
    128 
    129   std::set<string16> disabled_plugins;
    130   std::set<string16> disabled_plugins_exceptions;
    131   std::set<string16> enabled_plugins;
    132 
    133   disabled_plugins.insert(kEnabled);
    134   disabled_plugins_exceptions.insert(kEnabled);
    135   enabled_plugins.insert(kEnabled);
    136 
    137   disabled_plugins_exceptions.insert(kException);
    138 
    139   disabled_plugins.insert(kEnabled2);
    140   enabled_plugins.insert(kEnabled2);
    141 
    142   disabled_plugins.insert(kException2);
    143   disabled_plugins_exceptions.insert(kException2);
    144 
    145   disabled_plugins_exceptions.insert(kEnabled3);
    146   enabled_plugins.insert(kEnabled3);
    147 
    148   SetPolicyEnforcedPluginPatterns(disabled_plugins,
    149                                   disabled_plugins_exceptions,
    150                                   enabled_plugins);
    151 
    152   EXPECT_EQ(PluginPrefs::NO_POLICY, plugin_prefs_->PolicyStatusForPlugin(k42));
    153 
    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));
    160 
    161   EXPECT_EQ(PluginPrefs::NO_POLICY,
    162             plugin_prefs_->PolicyStatusForPlugin(kException));
    163   EXPECT_EQ(PluginPrefs::NO_POLICY,
    164             plugin_prefs_->PolicyStatusForPlugin(kException2));
    165 
    166   disabled_plugins.clear();
    167   disabled_plugins_exceptions.clear();
    168   enabled_plugins.clear();
    169 
    170   disabled_plugins.insert(ASCIIToUTF16("*"));
    171   disabled_plugins_exceptions.insert(ASCIIToUTF16("*Google*"));
    172   enabled_plugins.insert(kGoogleEarth);
    173 
    174   SetPolicyEnforcedPluginPatterns(disabled_plugins,
    175                                   disabled_plugins_exceptions,
    176                                   enabled_plugins);
    177 
    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));
    184 }
    185 
    186 // Linux Aura doesn't support NPAPI.
    187 #if !(defined(OS_LINUX) && defined(USE_AURA))
    188 
    189 TEST_F(PluginPrefsTest, UnifiedPepperFlashState) {
    190   base::ShadowingAtExitManager at_exit_manager_;  // Destroys the PluginService.
    191 
    192   base::MessageLoop message_loop;
    193   content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
    194   PluginService::GetInstance()->Init();
    195   PluginService::GetInstance()->DisablePluginsDiscoveryForTesting();
    196 
    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"),
    203       ASCIIToUTF16(""));
    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"),
    208       ASCIIToUTF16(""));
    209   content::WebPluginInfo bundled_plugin(ASCIIToUTF16("Pepper Flash"),
    210                                         GetBundledPepperFlashPath(),
    211                                         ASCIIToUTF16("11.3.31.229"),
    212                                         ASCIIToUTF16(""));
    213 
    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);
    219 
    220 #if !defined(OS_WIN)
    221     // Can't go out of process in unit tests.
    222     content::RenderProcessHost::SetRunRendererInProcess(true);
    223 #endif
    224   scoped_refptr<content::MessageLoopRunner> runner =
    225       new content::MessageLoopRunner;
    226   PluginService::GetInstance()->GetPlugins(
    227       base::Bind(&GotPlugins, runner->QuitClosure()));
    228   runner->Run();
    229 #if !defined(OS_WIN)
    230     content::RenderProcessHost::SetRunRendererInProcess(false);
    231 #endif
    232 
    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));
    238 
    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));
    243 
    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));
    248 
    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>());
    254 
    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));
    259 
    260   EnablePluginSynchronously(false, bundled_plugin.path, true);
    261   EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
    262 
    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));
    270 
    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));
    275 }
    276 
    277 #endif
    278