| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/common/content_settings.h" | 5 #include "chrome/common/content_settings.h" |
| 6 #include "chrome/common/render_messages.h" | 6 #include "chrome/common/render_messages.h" |
| 7 #include "chrome/renderer/content_settings_observer.h" | 7 #include "chrome/renderer/content_settings_observer.h" |
| 8 #include "chrome/test/base/chrome_render_view_test.h" | 8 #include "chrome/test/base/chrome_render_view_test.h" |
| 9 #include "content/public/renderer/render_view.h" | 9 #include "content/public/renderer/render_view.h" |
| 10 #include "ipc/ipc_message_macros.h" | 10 #include "ipc/ipc_message_macros.h" |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 "<head>" | 98 "<head>" |
| 99 "<script>document.createElement('div');</script>" | 99 "<script>document.createElement('div');</script>" |
| 100 "</head>" | 100 "</head>" |
| 101 "<body>" | 101 "<body>" |
| 102 "</body>" | 102 "</body>" |
| 103 "</html>"; | 103 "</html>"; |
| 104 render_thread_->sink().ClearMessages(); | 104 render_thread_->sink().ClearMessages(); |
| 105 LoadHTML(html.c_str()); | 105 LoadHTML(html.c_str()); |
| 106 | 106 |
| 107 // 2. Block JavaScript. | 107 // 2. Block JavaScript. |
| 108 ContentSettings settings; | 108 RendererContentSettingRules content_setting_rules; |
| 109 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) | 109 ContentSettingsForOneType& script_setting_rules = |
| 110 settings.settings[i] = CONTENT_SETTING_ALLOW; | 110 content_setting_rules.script_rules; |
| 111 settings.settings[CONTENT_SETTINGS_TYPE_JAVASCRIPT] = CONTENT_SETTING_BLOCK; | 111 script_setting_rules.push_back( |
| 112 ContentSettingPatternSource( |
| 113 ContentSettingsPattern::Wildcard(), |
| 114 ContentSettingsPattern::Wildcard(), |
| 115 CONTENT_SETTING_BLOCK, |
| 116 "", |
| 117 false)); |
| 112 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 118 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 113 observer->SetContentSettings(settings); | 119 observer->SetContentSettingRules(&content_setting_rules); |
| 114 observer->SetDefaultContentSettings(&settings); | |
| 115 | 120 |
| 116 // Make sure no pending messages are in the queue. | 121 // Make sure no pending messages are in the queue. |
| 117 ProcessPendingMessages(); | 122 ProcessPendingMessages(); |
| 118 render_thread_->sink().ClearMessages(); | 123 render_thread_->sink().ClearMessages(); |
| 119 | 124 |
| 120 // 3. Reload page. | 125 // 3. Reload page. |
| 121 std::string url_str = "data:text/html;charset=utf-8,"; | 126 std::string url_str = "data:text/html;charset=utf-8,"; |
| 122 url_str.append(html); | 127 url_str.append(html); |
| 123 GURL url(url_str); | 128 GURL url(url_str); |
| 124 Reload(url); | 129 Reload(url); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 EXPECT_FALSE(observer->plugins_temporarily_allowed()); | 172 EXPECT_FALSE(observer->plugins_temporarily_allowed()); |
| 168 } | 173 } |
| 169 | 174 |
| 170 TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) { | 175 TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) { |
| 171 MockContentSettingsObserver mock_observer(view_); | 176 MockContentSettingsObserver mock_observer(view_); |
| 172 | 177 |
| 173 // Load some HTML. | 178 // Load some HTML. |
| 174 LoadHTML("<html>Foo</html>"); | 179 LoadHTML("<html>Foo</html>"); |
| 175 | 180 |
| 176 // Set the default image blocking setting. | 181 // Set the default image blocking setting. |
| 177 ContentSettingsForOneType image_setting_rules; | 182 RendererContentSettingRules content_setting_rules; |
| 183 ContentSettingsForOneType& image_setting_rules = |
| 184 content_setting_rules.image_rules; |
| 178 image_setting_rules.push_back( | 185 image_setting_rules.push_back( |
| 179 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 186 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 180 ContentSettingsPattern::Wildcard(), | 187 ContentSettingsPattern::Wildcard(), |
| 181 CONTENT_SETTING_BLOCK, | 188 CONTENT_SETTING_BLOCK, |
| 182 "", | 189 "", |
| 183 false)); | 190 false)); |
| 184 | 191 |
| 185 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 192 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 186 observer->SetImageSettingRules(&image_setting_rules); | 193 observer->SetContentSettingRules(&content_setting_rules); |
| 187 EXPECT_CALL(mock_observer, | 194 EXPECT_CALL(mock_observer, |
| 188 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 195 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
| 189 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), | 196 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
| 190 true, mock_observer.image_url_)); | 197 true, mock_observer.image_url_)); |
| 191 ::testing::Mock::VerifyAndClearExpectations(&observer); | 198 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 192 | 199 |
| 193 // Create an exception which allows the image. | 200 // Create an exception which allows the image. |
| 194 image_setting_rules.insert( | 201 image_setting_rules.insert( |
| 195 image_setting_rules.begin(), | 202 image_setting_rules.begin(), |
| 196 ContentSettingPatternSource( | 203 ContentSettingPatternSource( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 208 ::testing::Mock::VerifyAndClearExpectations(&observer); | 215 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 209 } | 216 } |
| 210 | 217 |
| 211 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { | 218 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { |
| 212 MockContentSettingsObserver mock_observer(view_); | 219 MockContentSettingsObserver mock_observer(view_); |
| 213 | 220 |
| 214 // Load some HTML. | 221 // Load some HTML. |
| 215 LoadHTML("<html>Foo</html>"); | 222 LoadHTML("<html>Foo</html>"); |
| 216 | 223 |
| 217 // Set the default image blocking setting. | 224 // Set the default image blocking setting. |
| 218 ContentSettingsForOneType image_setting_rules; | 225 RendererContentSettingRules content_setting_rules; |
| 226 ContentSettingsForOneType& image_setting_rules = |
| 227 content_setting_rules.image_rules; |
| 219 image_setting_rules.push_back( | 228 image_setting_rules.push_back( |
| 220 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 229 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 221 ContentSettingsPattern::Wildcard(), | 230 ContentSettingsPattern::Wildcard(), |
| 222 CONTENT_SETTING_ALLOW, | 231 CONTENT_SETTING_ALLOW, |
| 223 "", | 232 "", |
| 224 false)); | 233 false)); |
| 225 | 234 |
| 226 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 235 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 227 observer->SetImageSettingRules(&image_setting_rules); | 236 observer->SetContentSettingRules(&content_setting_rules); |
| 228 EXPECT_CALL( | 237 EXPECT_CALL( |
| 229 mock_observer, | 238 mock_observer, |
| 230 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); | 239 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); |
| 231 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, | 240 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
| 232 mock_observer.image_url_)); | 241 mock_observer.image_url_)); |
| 233 ::testing::Mock::VerifyAndClearExpectations(&observer); | 242 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 234 | 243 |
| 235 // Create an exception which blocks the image. | 244 // Create an exception which blocks the image. |
| 236 image_setting_rules.insert( | 245 image_setting_rules.insert( |
| 237 image_setting_rules.begin(), | 246 image_setting_rules.begin(), |
| 238 ContentSettingPatternSource( | 247 ContentSettingPatternSource( |
| 239 ContentSettingsPattern::Wildcard(), | 248 ContentSettingsPattern::Wildcard(), |
| 240 ContentSettingsPattern::FromString(mock_observer.image_origin_), | 249 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
| 241 CONTENT_SETTING_BLOCK, | 250 CONTENT_SETTING_BLOCK, |
| 242 "", | 251 "", |
| 243 false)); | 252 false)); |
| 244 EXPECT_CALL(mock_observer, | 253 EXPECT_CALL(mock_observer, |
| 245 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 254 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
| 246 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), | 255 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
| 247 true, mock_observer.image_url_)); | 256 true, mock_observer.image_url_)); |
| 248 ::testing::Mock::VerifyAndClearExpectations(&observer); | 257 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 249 } | 258 } |
| 259 |
| 260 TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) { |
| 261 // Set the content settings for scripts. |
| 262 RendererContentSettingRules content_setting_rules; |
| 263 ContentSettingsForOneType& script_setting_rules = |
| 264 content_setting_rules.script_rules; |
| 265 script_setting_rules.push_back( |
| 266 ContentSettingPatternSource( |
| 267 ContentSettingsPattern::Wildcard(), |
| 268 ContentSettingsPattern::Wildcard(), |
| 269 CONTENT_SETTING_BLOCK, |
| 270 "", |
| 271 false)); |
| 272 |
| 273 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 274 observer->SetContentSettingRules(&content_setting_rules); |
| 275 |
| 276 // Load a page which contains a script. |
| 277 std::string html = "<html>" |
| 278 "<head>" |
| 279 "<script src='data:foo'></script>" |
| 280 "</head>" |
| 281 "<body>" |
| 282 "</body>" |
| 283 "</html>"; |
| 284 LoadHTML(html.c_str()); |
| 285 |
| 286 // Verify that the script was blocked. |
| 287 bool was_blocked = false; |
| 288 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 289 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 290 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 291 was_blocked = true; |
| 292 } |
| 293 EXPECT_TRUE(was_blocked); |
| 294 } |
| 295 |
| 296 TEST_F(ChromeRenderViewTest, ContentSettingsAllowScripts) { |
| 297 // Set the content settings for scripts. |
| 298 RendererContentSettingRules content_setting_rules; |
| 299 ContentSettingsForOneType& script_setting_rules = |
| 300 content_setting_rules.script_rules; |
| 301 script_setting_rules.push_back( |
| 302 ContentSettingPatternSource( |
| 303 ContentSettingsPattern::Wildcard(), |
| 304 ContentSettingsPattern::Wildcard(), |
| 305 CONTENT_SETTING_ALLOW, |
| 306 "", |
| 307 false)); |
| 308 |
| 309 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 310 observer->SetContentSettingRules(&content_setting_rules); |
| 311 |
| 312 // Load a page which contains a script. |
| 313 std::string html = "<html>" |
| 314 "<head>" |
| 315 "<script src='data:foo'></script>" |
| 316 "</head>" |
| 317 "<body>" |
| 318 "</body>" |
| 319 "</html>"; |
| 320 LoadHTML(html.c_str()); |
| 321 |
| 322 // Verify that the script was not blocked. |
| 323 bool was_blocked = false; |
| 324 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 325 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 326 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 327 was_blocked = true; |
| 328 } |
| 329 EXPECT_FALSE(was_blocked); |
| 330 } |
| OLD | NEW |