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