Home | History | Annotate | Download | only in content_settings
      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 "base/prefs/pref_service.h"
      6 #include "base/strings/utf_string_conversions.h"
      7 #include "chrome/browser/browser_process.h"
      8 #include "chrome/browser/chrome_notification_types.h"
      9 #include "chrome/browser/content_settings/cookie_settings.h"
     10 #include "chrome/browser/content_settings/host_content_settings_map.h"
     11 #include "chrome/browser/extensions/api/content_settings/content_settings_api.h"
     12 #include "chrome/browser/extensions/extension_apitest.h"
     13 #include "chrome/browser/profiles/profile.h"
     14 #include "chrome/browser/ui/browser.h"
     15 #include "chrome/common/chrome_switches.h"
     16 #include "chrome/common/pref_names.h"
     17 #include "content/public/browser/notification_service.h"
     18 #include "content/public/browser/plugin_service.h"
     19 #include "content/public/common/webplugininfo.h"
     20 #include "content/public/test/test_utils.h"
     21 #include "extensions/browser/extension_registry.h"
     22 #include "extensions/browser/test_extension_registry_observer.h"
     23 
     24 namespace {
     25 
     26 void ReleaseBrowserProcessModule() {
     27   g_browser_process->ReleaseModule();
     28 }
     29 
     30 }  // namespace
     31 
     32 namespace extensions {
     33 
     34 class ExtensionContentSettingsApiTest : public ExtensionApiTest {
     35  public:
     36   ExtensionContentSettingsApiTest() : profile_(NULL) {}
     37 
     38   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     39     ExtensionApiTest::SetUpCommandLine(command_line);
     40     command_line->AppendSwitch(switches::kDisablePluginsDiscovery);
     41   }
     42 
     43   virtual void SetUpOnMainThread() OVERRIDE {
     44     ExtensionApiTest::SetUpOnMainThread();
     45 
     46     // The browser might get closed later (and therefore be destroyed), so we
     47     // save the profile.
     48     profile_ = browser()->profile();
     49 
     50     // Closing the last browser window also releases a module reference. Make
     51     // sure it's not the last one, so the message loop doesn't quit
     52     // unexpectedly.
     53     g_browser_process->AddRefModule();
     54   }
     55 
     56   virtual void TearDownOnMainThread() OVERRIDE {
     57     // ReleaseBrowserProcessModule() needs to be called in a message loop, so we
     58     // post a task to do it, then run the message loop.
     59     base::MessageLoop::current()->PostTask(
     60         FROM_HERE, base::Bind(&ReleaseBrowserProcessModule));
     61     content::RunAllPendingInMessageLoop();
     62 
     63     ExtensionApiTest::TearDownOnMainThread();
     64   }
     65 
     66  protected:
     67   void CheckContentSettingsSet() {
     68     HostContentSettingsMap* map =
     69         profile_->GetHostContentSettingsMap();
     70     CookieSettings* cookie_settings =
     71         CookieSettings::Factory::GetForProfile(profile_).get();
     72 
     73     // Check default content settings by using an unknown URL.
     74     GURL example_url("http://www.example.com");
     75     EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(
     76         example_url, example_url));
     77     EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(
     78         example_url, example_url));
     79     EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(example_url));
     80     EXPECT_EQ(CONTENT_SETTING_ALLOW,
     81               map->GetContentSetting(example_url,
     82                                      example_url,
     83                                      CONTENT_SETTINGS_TYPE_IMAGES,
     84                                      std::string()));
     85     EXPECT_EQ(CONTENT_SETTING_BLOCK,
     86               map->GetContentSetting(example_url,
     87                                      example_url,
     88                                      CONTENT_SETTINGS_TYPE_JAVASCRIPT,
     89                                      std::string()));
     90     EXPECT_EQ(CONTENT_SETTING_ALLOW,
     91               map->GetContentSetting(example_url,
     92                                      example_url,
     93                                      CONTENT_SETTINGS_TYPE_PLUGINS,
     94                                      std::string()));
     95     EXPECT_EQ(CONTENT_SETTING_BLOCK,
     96               map->GetContentSetting(example_url,
     97                                      example_url,
     98                                      CONTENT_SETTINGS_TYPE_POPUPS,
     99                                      std::string()));
    100 #if 0
    101     // TODO(bauerb): Enable once geolocation settings are integrated into the
    102     // HostContentSettingsMap.
    103     EXPECT_EQ(CONTENT_SETTING_ALLOW,
    104               map->GetContentSetting(example_url,
    105                                      example_url,
    106                                      CONTENT_SETTINGS_TYPE_GEOLOCATION,
    107                                      std::string()));
    108 #endif
    109     EXPECT_EQ(CONTENT_SETTING_ASK,
    110               map->GetContentSetting(example_url,
    111                                      example_url,
    112                                      CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
    113                                      std::string()));
    114 
    115     // Check content settings for www.google.com
    116     GURL url("http://www.google.com");
    117     EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed(url, url));
    118     EXPECT_EQ(CONTENT_SETTING_ALLOW,
    119               map->GetContentSetting(
    120                   url, url, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
    121     EXPECT_EQ(CONTENT_SETTING_BLOCK,
    122               map->GetContentSetting(
    123                   url, url, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
    124     EXPECT_EQ(CONTENT_SETTING_BLOCK,
    125               map->GetContentSetting(
    126                   url, url, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
    127     EXPECT_EQ(CONTENT_SETTING_ALLOW,
    128               map->GetContentSetting(
    129                   url, url, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
    130 #if 0
    131     EXPECT_EQ(CONTENT_SETTING_BLOCK,
    132               map->GetContentSetting(
    133                   url, url, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
    134 #endif
    135     EXPECT_EQ(
    136         CONTENT_SETTING_BLOCK,
    137         map->GetContentSetting(
    138             url, url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
    139   }
    140 
    141   void CheckContentSettingsDefault() {
    142     HostContentSettingsMap* map =
    143         profile_->GetHostContentSettingsMap();
    144     CookieSettings* cookie_settings =
    145         CookieSettings::Factory::GetForProfile(profile_).get();
    146 
    147     // Check content settings for www.google.com
    148     GURL url("http://www.google.com");
    149     EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(url, url));
    150     EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed(url, url));
    151     EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(url));
    152     EXPECT_EQ(CONTENT_SETTING_ALLOW,
    153               map->GetContentSetting(
    154                   url, url, CONTENT_SETTINGS_TYPE_IMAGES, std::string()));
    155     EXPECT_EQ(CONTENT_SETTING_ALLOW,
    156               map->GetContentSetting(
    157                   url, url, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string()));
    158     EXPECT_EQ(CONTENT_SETTING_ALLOW,
    159               map->GetContentSetting(
    160                   url, url, CONTENT_SETTINGS_TYPE_PLUGINS, std::string()));
    161     EXPECT_EQ(CONTENT_SETTING_BLOCK,
    162               map->GetContentSetting(
    163                   url, url, CONTENT_SETTINGS_TYPE_POPUPS, std::string()));
    164 #if 0
    165     // TODO(bauerb): Enable once geolocation settings are integrated into the
    166     // HostContentSettingsMap.
    167     EXPECT_EQ(CONTENT_SETTING_ALLOW,
    168               map->GetContentSetting(
    169                   url, url, CONTENT_SETTINGS_TYPE_GEOLOCATION, std::string()));
    170 #endif
    171     EXPECT_EQ(
    172         CONTENT_SETTING_ASK,
    173         map->GetContentSetting(
    174             url, url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string()));
    175   }
    176 
    177  private:
    178   Profile* profile_;
    179 };
    180 
    181 // http://crbug.com/177163
    182 #if defined(OS_WIN) && !defined(NDEBUG)
    183 #define MAYBE_Standard DISABLED_Standard
    184 #else
    185 #define MAYBE_Standard Standard
    186 #endif
    187 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest, MAYBE_Standard) {
    188   CheckContentSettingsDefault();
    189 
    190   const char kExtensionPath[] = "content_settings/standard";
    191 
    192   EXPECT_TRUE(RunExtensionSubtest(kExtensionPath, "test.html")) << message_;
    193   CheckContentSettingsSet();
    194 
    195   // The settings should not be reset when the extension is reloaded.
    196   ReloadExtension(last_loaded_extension_id());
    197   CheckContentSettingsSet();
    198 
    199   // Uninstalling and installing the extension (without running the test that
    200   // calls the extension API) should clear the settings.
    201   TestExtensionRegistryObserver observer(ExtensionRegistry::Get(profile()),
    202                                          last_loaded_extension_id());
    203   UninstallExtension(last_loaded_extension_id());
    204   observer.WaitForExtensionUninstalled();
    205   CheckContentSettingsDefault();
    206 
    207   LoadExtension(test_data_dir_.AppendASCII(kExtensionPath));
    208   CheckContentSettingsDefault();
    209 }
    210 
    211 // Flaky on the trybots. See http://crbug.com/96725.
    212 IN_PROC_BROWSER_TEST_F(ExtensionContentSettingsApiTest,
    213                        DISABLED_GetResourceIdentifiers) {
    214   base::FilePath::CharType kFooPath[] =
    215       FILE_PATH_LITERAL("/plugins/foo.plugin");
    216   base::FilePath::CharType kBarPath[] =
    217       FILE_PATH_LITERAL("/plugins/bar.plugin");
    218   const char* kFooName = "Foo Plugin";
    219   const char* kBarName = "Bar Plugin";
    220 
    221   content::PluginService::GetInstance()->RegisterInternalPlugin(
    222       content::WebPluginInfo(base::ASCIIToUTF16(kFooName),
    223                              base::FilePath(kFooPath),
    224                              base::ASCIIToUTF16("1.2.3"),
    225                              base::ASCIIToUTF16("foo")),
    226       false);
    227   content::PluginService::GetInstance()->RegisterInternalPlugin(
    228     content::WebPluginInfo(base::ASCIIToUTF16(kBarName),
    229                            base::FilePath(kBarPath),
    230                            base::ASCIIToUTF16("2.3.4"),
    231                            base::ASCIIToUTF16("bar")),
    232       false);
    233 
    234   EXPECT_TRUE(RunExtensionTest("content_settings/getresourceidentifiers"))
    235       << message_;
    236 }
    237 
    238 }  // namespace extensions
    239