| 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 12 matching lines...) Expand all Loading... |
| 137 } | 142 } |
| 138 EXPECT_NE(-1, navigation_index); | 143 EXPECT_NE(-1, navigation_index); |
| 139 EXPECT_NE(-1, block_index); | 144 EXPECT_NE(-1, block_index); |
| 140 EXPECT_LT(navigation_index, block_index); | 145 EXPECT_LT(navigation_index, block_index); |
| 141 } | 146 } |
| 142 | 147 |
| 143 TEST_F(ChromeRenderViewTest, PluginsTemporarilyAllowed) { | 148 TEST_F(ChromeRenderViewTest, PluginsTemporarilyAllowed) { |
| 144 // Load some HTML. | 149 // Load some HTML. |
| 145 LoadHTML("<html>Foo</html>"); | 150 LoadHTML("<html>Foo</html>"); |
| 146 | 151 |
| 147 // Block plugins. | |
| 148 ContentSettings settings; | |
| 149 for (int i = 0; i < CONTENT_SETTINGS_NUM_TYPES; ++i) | |
| 150 settings.settings[i] = CONTENT_SETTING_ALLOW; | |
| 151 settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS] = CONTENT_SETTING_BLOCK; | |
| 152 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 152 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 153 observer->SetContentSettings(settings); | |
| 154 observer->SetDefaultContentSettings(&settings); | |
| 155 EXPECT_FALSE(observer->plugins_temporarily_allowed()); | 153 EXPECT_FALSE(observer->plugins_temporarily_allowed()); |
| 156 | 154 |
| 157 // Temporarily allow plugins. | 155 // Temporarily allow plugins. |
| 158 OnMessageReceived(ChromeViewMsg_LoadBlockedPlugins(MSG_ROUTING_NONE)); | 156 OnMessageReceived(ChromeViewMsg_LoadBlockedPlugins(MSG_ROUTING_NONE)); |
| 159 EXPECT_TRUE(observer->plugins_temporarily_allowed()); | 157 EXPECT_TRUE(observer->plugins_temporarily_allowed()); |
| 160 | 158 |
| 161 // Simulate a navigation within the page. | 159 // Simulate a navigation within the page. |
| 162 DidNavigateWithinPage(GetMainFrame(), true); | 160 DidNavigateWithinPage(GetMainFrame(), true); |
| 163 EXPECT_TRUE(observer->plugins_temporarily_allowed()); | 161 EXPECT_TRUE(observer->plugins_temporarily_allowed()); |
| 164 | 162 |
| 165 // Navigate to a different page. | 163 // Navigate to a different page. |
| 166 LoadHTML("<html>Bar</html>"); | 164 LoadHTML("<html>Bar</html>"); |
| 167 EXPECT_FALSE(observer->plugins_temporarily_allowed()); | 165 EXPECT_FALSE(observer->plugins_temporarily_allowed()); |
| 168 } | 166 } |
| 169 | 167 |
| 170 TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) { | 168 TEST_F(ChromeRenderViewTest, ImagesBlockedByDefault) { |
| 171 MockContentSettingsObserver mock_observer(view_); | 169 MockContentSettingsObserver mock_observer(view_); |
| 172 | 170 |
| 173 // Load some HTML. | 171 // Load some HTML. |
| 174 LoadHTML("<html>Foo</html>"); | 172 LoadHTML("<html>Foo</html>"); |
| 175 | 173 |
| 176 // Set the default image blocking setting. | 174 // Set the default image blocking setting. |
| 177 ContentSettingsForOneType image_setting_rules; | 175 RendererContentSettingRules content_setting_rules; |
| 176 ContentSettingsForOneType& image_setting_rules = |
| 177 content_setting_rules.image_rules; |
| 178 image_setting_rules.push_back( | 178 image_setting_rules.push_back( |
| 179 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 179 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 180 ContentSettingsPattern::Wildcard(), | 180 ContentSettingsPattern::Wildcard(), |
| 181 CONTENT_SETTING_BLOCK, | 181 CONTENT_SETTING_BLOCK, |
| 182 "", | 182 "", |
| 183 false)); | 183 false)); |
| 184 | 184 |
| 185 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 185 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 186 observer->SetImageSettingRules(&image_setting_rules); | 186 observer->SetContentSettingRules(&content_setting_rules); |
| 187 EXPECT_CALL(mock_observer, | 187 EXPECT_CALL(mock_observer, |
| 188 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 188 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
| 189 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), | 189 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
| 190 true, mock_observer.image_url_)); | 190 true, mock_observer.image_url_)); |
| 191 ::testing::Mock::VerifyAndClearExpectations(&observer); | 191 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 192 | 192 |
| 193 // Create an exception which allows the image. | 193 // Create an exception which allows the image. |
| 194 image_setting_rules.insert( | 194 image_setting_rules.insert( |
| 195 image_setting_rules.begin(), | 195 image_setting_rules.begin(), |
| 196 ContentSettingPatternSource( | 196 ContentSettingPatternSource( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 208 ::testing::Mock::VerifyAndClearExpectations(&observer); | 208 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 209 } | 209 } |
| 210 | 210 |
| 211 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { | 211 TEST_F(ChromeRenderViewTest, ImagesAllowedByDefault) { |
| 212 MockContentSettingsObserver mock_observer(view_); | 212 MockContentSettingsObserver mock_observer(view_); |
| 213 | 213 |
| 214 // Load some HTML. | 214 // Load some HTML. |
| 215 LoadHTML("<html>Foo</html>"); | 215 LoadHTML("<html>Foo</html>"); |
| 216 | 216 |
| 217 // Set the default image blocking setting. | 217 // Set the default image blocking setting. |
| 218 ContentSettingsForOneType image_setting_rules; | 218 RendererContentSettingRules content_setting_rules; |
| 219 ContentSettingsForOneType& image_setting_rules = |
| 220 content_setting_rules.image_rules; |
| 219 image_setting_rules.push_back( | 221 image_setting_rules.push_back( |
| 220 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), | 222 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), |
| 221 ContentSettingsPattern::Wildcard(), | 223 ContentSettingsPattern::Wildcard(), |
| 222 CONTENT_SETTING_ALLOW, | 224 CONTENT_SETTING_ALLOW, |
| 223 "", | 225 "", |
| 224 false)); | 226 false)); |
| 225 | 227 |
| 226 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); | 228 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 227 observer->SetImageSettingRules(&image_setting_rules); | 229 observer->SetContentSettingRules(&content_setting_rules); |
| 228 EXPECT_CALL( | 230 EXPECT_CALL( |
| 229 mock_observer, | 231 mock_observer, |
| 230 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); | 232 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())).Times(0); |
| 231 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, | 233 EXPECT_TRUE(observer->AllowImage(GetMainFrame(), true, |
| 232 mock_observer.image_url_)); | 234 mock_observer.image_url_)); |
| 233 ::testing::Mock::VerifyAndClearExpectations(&observer); | 235 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 234 | 236 |
| 235 // Create an exception which blocks the image. | 237 // Create an exception which blocks the image. |
| 236 image_setting_rules.insert( | 238 image_setting_rules.insert( |
| 237 image_setting_rules.begin(), | 239 image_setting_rules.begin(), |
| 238 ContentSettingPatternSource( | 240 ContentSettingPatternSource( |
| 239 ContentSettingsPattern::Wildcard(), | 241 ContentSettingsPattern::Wildcard(), |
| 240 ContentSettingsPattern::FromString(mock_observer.image_origin_), | 242 ContentSettingsPattern::FromString(mock_observer.image_origin_), |
| 241 CONTENT_SETTING_BLOCK, | 243 CONTENT_SETTING_BLOCK, |
| 242 "", | 244 "", |
| 243 false)); | 245 false)); |
| 244 EXPECT_CALL(mock_observer, | 246 EXPECT_CALL(mock_observer, |
| 245 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); | 247 OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, std::string())); |
| 246 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), | 248 EXPECT_FALSE(observer->AllowImage(GetMainFrame(), |
| 247 true, mock_observer.image_url_)); | 249 true, mock_observer.image_url_)); |
| 248 ::testing::Mock::VerifyAndClearExpectations(&observer); | 250 ::testing::Mock::VerifyAndClearExpectations(&observer); |
| 249 } | 251 } |
| 252 |
| 253 TEST_F(ChromeRenderViewTest, ContentSettingsBlockScripts) { |
| 254 // Set the content settings for scripts. |
| 255 RendererContentSettingRules content_setting_rules; |
| 256 ContentSettingsForOneType& script_setting_rules = |
| 257 content_setting_rules.script_rules; |
| 258 script_setting_rules.push_back( |
| 259 ContentSettingPatternSource( |
| 260 ContentSettingsPattern::Wildcard(), |
| 261 ContentSettingsPattern::Wildcard(), |
| 262 CONTENT_SETTING_BLOCK, |
| 263 "", |
| 264 false)); |
| 265 |
| 266 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 267 observer->SetContentSettingRules(&content_setting_rules); |
| 268 |
| 269 // Load a page which contains a script. |
| 270 std::string html = "<html>" |
| 271 "<head>" |
| 272 "<script src='data:foo'></script>" |
| 273 "</head>" |
| 274 "<body>" |
| 275 "</body>" |
| 276 "</html>"; |
| 277 LoadHTML(html.c_str()); |
| 278 |
| 279 // Verify that the script was blocked. |
| 280 bool was_blocked = false; |
| 281 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 282 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 283 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 284 was_blocked = true; |
| 285 } |
| 286 EXPECT_TRUE(was_blocked); |
| 287 } |
| 288 |
| 289 TEST_F(ChromeRenderViewTest, ContentSettingsAllowScripts) { |
| 290 // Set the content settings for scripts. |
| 291 RendererContentSettingRules content_setting_rules; |
| 292 ContentSettingsForOneType& script_setting_rules = |
| 293 content_setting_rules.script_rules; |
| 294 script_setting_rules.push_back( |
| 295 ContentSettingPatternSource( |
| 296 ContentSettingsPattern::Wildcard(), |
| 297 ContentSettingsPattern::Wildcard(), |
| 298 CONTENT_SETTING_ALLOW, |
| 299 "", |
| 300 false)); |
| 301 |
| 302 ContentSettingsObserver* observer = ContentSettingsObserver::Get(view_); |
| 303 observer->SetContentSettingRules(&content_setting_rules); |
| 304 |
| 305 // Load a page which contains a script. |
| 306 std::string html = "<html>" |
| 307 "<head>" |
| 308 "<script src='data:foo'></script>" |
| 309 "</head>" |
| 310 "<body>" |
| 311 "</body>" |
| 312 "</html>"; |
| 313 LoadHTML(html.c_str()); |
| 314 |
| 315 // Verify that the script was not blocked. |
| 316 bool was_blocked = false; |
| 317 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { |
| 318 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); |
| 319 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) |
| 320 was_blocked = true; |
| 321 } |
| 322 EXPECT_FALSE(was_blocked); |
| 323 } |
| OLD | NEW |