Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(657)

Side by Side Diff: chrome/renderer/content_settings_observer_browsertest.cc

Issue 1835753002: Reset content settings caches during provisional load start instead of commit. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move things under DidCreateNewDocument instead of DidStartProvisionalLoad Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "chrome/common/render_messages.h" 7 #include "chrome/common/render_messages.h"
8 #include "chrome/renderer/content_settings_observer.h" 8 #include "chrome/renderer/content_settings_observer.h"
9 #include "chrome/test/base/chrome_render_view_test.h" 9 #include "chrome/test/base/chrome_render_view_test.h"
10 #include "components/content_settings/content/common/content_settings_messages.h " 10 #include "components/content_settings/content/common/content_settings_messages.h "
11 #include "components/content_settings/core/common/content_settings.h" 11 #include "components/content_settings/core/common/content_settings.h"
12 #include "content/public/renderer/render_view.h" 12 #include "content/public/renderer/render_view.h"
13 #include "ipc/ipc_message_macros.h" 13 #include "ipc/ipc_message_macros.h"
14 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/public/web/WebFrameContentDumper.h"
16 #include "third_party/WebKit/public/web/WebView.h" 17 #include "third_party/WebKit/public/web/WebView.h"
17 18
18 using testing::_; 19 using testing::_;
19 using testing::DeleteArg; 20 using testing::DeleteArg;
20 21
21 namespace { 22 namespace {
22 23
23 class MockContentSettingsObserver : public ContentSettingsObserver { 24 class MockContentSettingsObserver : public ContentSettingsObserver {
24 public: 25 public:
25 explicit MockContentSettingsObserver(content::RenderFrame* render_frame); 26 explicit MockContentSettingsObserver(content::RenderFrame* render_frame);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 82
82 // Accessing localStorage from the same origin again shouldn't result in a 83 // Accessing localStorage from the same origin again shouldn't result in a
83 // new IPC. 84 // new IPC.
84 observer.allowStorage(true); 85 observer.allowStorage(true);
85 ::testing::Mock::VerifyAndClearExpectations(&observer); 86 ::testing::Mock::VerifyAndClearExpectations(&observer);
86 } 87 }
87 88
88 // Regression test for http://crbug.com/35011 89 // Regression test for http://crbug.com/35011
89 TEST_F(ChromeRenderViewTest, JSBlockSentAfterPageLoad) { 90 TEST_F(ChromeRenderViewTest, JSBlockSentAfterPageLoad) {
90 // 1. Load page with JS. 91 // 1. Load page with JS.
91 std::string html = "<html>" 92 const char kHtml[] =
92 "<head>" 93 "<html>"
93 "<script>document.createElement('div');</script>" 94 "<head>"
94 "</head>" 95 "<script>document.createElement('div');</script>"
95 "<body>" 96 "</head>"
96 "</body>" 97 "<body>"
97 "</html>"; 98 "</body>"
99 "</html>";
98 render_thread_->sink().ClearMessages(); 100 render_thread_->sink().ClearMessages();
99 LoadHTML(html.c_str()); 101 LoadHTML(kHtml);
100 102
101 // 2. Block JavaScript. 103 // 2. Block JavaScript.
102 RendererContentSettingRules content_setting_rules; 104 RendererContentSettingRules content_setting_rules;
103 ContentSettingsForOneType& script_setting_rules = 105 ContentSettingsForOneType& script_setting_rules =
104 content_setting_rules.script_rules; 106 content_setting_rules.script_rules;
105 script_setting_rules.push_back( 107 script_setting_rules.push_back(
106 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), 108 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
107 ContentSettingsPattern::Wildcard(), 109 ContentSettingsPattern::Wildcard(),
108 CONTENT_SETTING_BLOCK, 110 CONTENT_SETTING_BLOCK,
109 std::string(), 111 std::string(),
110 false)); 112 false));
111 ContentSettingsObserver* observer = ContentSettingsObserver::Get( 113 ContentSettingsObserver* observer = ContentSettingsObserver::Get(
112 view_->GetMainRenderFrame()); 114 view_->GetMainRenderFrame());
113 observer->SetContentSettingRules(&content_setting_rules); 115 observer->SetContentSettingRules(&content_setting_rules);
114 116
115 // Make sure no pending messages are in the queue. 117 // Make sure no pending messages are in the queue.
116 ProcessPendingMessages(); 118 ProcessPendingMessages();
117 render_thread_->sink().ClearMessages(); 119 render_thread_->sink().ClearMessages();
118 120
119 // 3. Reload page. 121 // 3. Reload page.
120 std::string url_str = "data:text/html;charset=utf-8,"; 122 std::string url_str = "data:text/html;charset=utf-8,";
121 url_str.append(html); 123 url_str.append(kHtml);
122 GURL url(url_str); 124 GURL url(url_str);
123 Reload(url); 125 Reload(url);
124 ProcessPendingMessages(); 126 ProcessPendingMessages();
125 127
126 // 4. Verify that the notification that javascript was blocked is sent after 128 // 4. Verify that the notification that javascript was blocked is sent after
127 // the navigation notification is sent. 129 // the navigation notification is sent.
128 int navigation_index = -1; 130 int navigation_index = -1;
129 int block_index = -1; 131 int block_index = -1;
130 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { 132 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) {
131 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); 133 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i);
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 ContentSettingsPattern::Wildcard(), 264 ContentSettingsPattern::Wildcard(),
263 CONTENT_SETTING_BLOCK, 265 CONTENT_SETTING_BLOCK,
264 std::string(), 266 std::string(),
265 false)); 267 false));
266 268
267 ContentSettingsObserver* observer = 269 ContentSettingsObserver* observer =
268 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); 270 ContentSettingsObserver::Get(view_->GetMainRenderFrame());
269 observer->SetContentSettingRules(&content_setting_rules); 271 observer->SetContentSettingRules(&content_setting_rules);
270 272
271 // Load a page which contains a script. 273 // Load a page which contains a script.
272 std::string html = "<html>" 274 const char kHtml[] =
273 "<head>" 275 "<html>"
274 "<script src='data:foo'></script>" 276 "<head>"
275 "</head>" 277 "<script src='data:foo'></script>"
276 "<body>" 278 "</head>"
277 "</body>" 279 "<body>"
278 "</html>"; 280 "</body>"
279 LoadHTML(html.c_str()); 281 "</html>";
282 LoadHTML(kHtml);
280 283
281 // Verify that the script was blocked. 284 // Verify that the script was blocked.
282 bool was_blocked = false; 285 bool was_blocked = false;
283 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { 286 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) {
284 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); 287 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i);
285 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) 288 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID)
286 was_blocked = true; 289 was_blocked = true;
287 } 290 }
288 EXPECT_TRUE(was_blocked); 291 EXPECT_TRUE(was_blocked);
289 } 292 }
290 293
291 TEST_F(ChromeRenderViewTest, ContentSettingsAllowScripts) { 294 TEST_F(ChromeRenderViewTest, ContentSettingsAllowScripts) {
292 // Set the content settings for scripts. 295 // Set the content settings for scripts.
293 RendererContentSettingRules content_setting_rules; 296 RendererContentSettingRules content_setting_rules;
294 ContentSettingsForOneType& script_setting_rules = 297 ContentSettingsForOneType& script_setting_rules =
295 content_setting_rules.script_rules; 298 content_setting_rules.script_rules;
296 script_setting_rules.push_back( 299 script_setting_rules.push_back(
297 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), 300 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
298 ContentSettingsPattern::Wildcard(), 301 ContentSettingsPattern::Wildcard(),
299 CONTENT_SETTING_ALLOW, 302 CONTENT_SETTING_ALLOW,
300 std::string(), 303 std::string(),
301 false)); 304 false));
302 305
303 ContentSettingsObserver* observer = 306 ContentSettingsObserver* observer =
304 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); 307 ContentSettingsObserver::Get(view_->GetMainRenderFrame());
305 observer->SetContentSettingRules(&content_setting_rules); 308 observer->SetContentSettingRules(&content_setting_rules);
306 309
307 // Load a page which contains a script. 310 // Load a page which contains a script.
308 std::string html = "<html>" 311 const char kHtml[] =
309 "<head>" 312 "<html>"
310 "<script src='data:foo'></script>" 313 "<head>"
311 "</head>" 314 "<script src='data:foo'></script>"
312 "<body>" 315 "</head>"
313 "</body>" 316 "<body>"
314 "</html>"; 317 "</body>"
315 LoadHTML(html.c_str()); 318 "</html>";
319 LoadHTML(kHtml);
316 320
317 // Verify that the script was not blocked. 321 // Verify that the script was not blocked.
318 bool was_blocked = false; 322 bool was_blocked = false;
319 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { 323 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) {
320 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); 324 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i);
321 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) 325 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID)
322 was_blocked = true; 326 was_blocked = true;
323 } 327 }
324 EXPECT_FALSE(was_blocked); 328 EXPECT_FALSE(was_blocked);
325 } 329 }
326 330
331 // Regression test for crbug.com/232410: Load a page with JS blocked. Then,
332 // allow JS and reload the page. In each case, only one of noscript or script
333 // tags should be enabled, but never both.
334 TEST_F(ChromeRenderViewTest, ContentSettingsNoscriptTag) {
335 // 1. Block JavaScript.
336 RendererContentSettingRules content_setting_rules;
337 ContentSettingsForOneType& script_setting_rules =
338 content_setting_rules.script_rules;
339 script_setting_rules.push_back(ContentSettingPatternSource(
340 ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(),
341 CONTENT_SETTING_BLOCK, std::string(), false));
342
343 ContentSettingsObserver* observer =
344 ContentSettingsObserver::Get(view_->GetMainRenderFrame());
345 observer->SetContentSettingRules(&content_setting_rules);
346
347 // 2. Load a page which contains a noscript tag and a script tag. Note that
348 // the page doesn't have a body tag.
349 const char kHtml[] =
350 "<html>"
351 "<noscript>JS_DISABLED</noscript>"
352 "<script>document.write('JS_ENABLED');</script>"
353 "</html>";
354 LoadHTML(kHtml);
355 EXPECT_EQ("JS_DISABLED", blink::WebFrameContentDumper::dumpFrameTreeAsText(
356 GetMainFrame(), 1024)
357 .utf8());
358
359 // 3. Allow JavaScript.
360 script_setting_rules.clear();
361 script_setting_rules.push_back(ContentSettingPatternSource(
362 ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(),
363 CONTENT_SETTING_ALLOW, std::string(), false));
364 observer->SetContentSettingRules(&content_setting_rules);
365
366 // 4. Reload the page.
367 std::string url_str = "data:text/html;charset=utf-8,";
368 url_str.append(kHtml);
369 GURL url(url_str);
370 Reload(url);
371 EXPECT_EQ("JS_ENABLED", blink::WebFrameContentDumper::dumpFrameTreeAsText(
372 GetMainFrame(), 1024)
373 .utf8());
374 }
375
376 // Checks that same page navigations don't update content settings for the page.
377 TEST_F(ChromeRenderViewTest, ContentSettingsSamePageNavigation) {
378 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame());
379 // Load a page which contains a script.
380 const char kHtml[] =
381 "<html>"
382 "<head>"
383 "<script src='data:foo'></script>"
384 "</head>"
385 "<body>"
386 "</body>"
387 "</html>";
388 LoadHTML(kHtml);
389
390 // Verify that the script was not blocked.
391 bool was_blocked = false;
392 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) {
393 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i);
394 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID)
395 was_blocked = true;
396 }
397 EXPECT_FALSE(was_blocked);
398
399 // Block JavaScript.
400 RendererContentSettingRules content_setting_rules;
401 ContentSettingsForOneType& script_setting_rules =
402 content_setting_rules.script_rules;
403 script_setting_rules.push_back(ContentSettingPatternSource(
404 ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(),
405 CONTENT_SETTING_BLOCK, std::string(), false));
406
407 ContentSettingsObserver* observer =
408 ContentSettingsObserver::Get(view_->GetMainRenderFrame());
409 observer->SetContentSettingRules(&content_setting_rules);
410
411 // The page shouldn't see the change to script blocking setting after a
412 // same page navigation.
413 DidNavigateWithinPage(GetMainFrame(), true);
414 EXPECT_TRUE(observer->allowScript(true));
415 }
416
327 TEST_F(ChromeRenderViewTest, ContentSettingsInterstitialPages) { 417 TEST_F(ChromeRenderViewTest, ContentSettingsInterstitialPages) {
328 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame()); 418 MockContentSettingsObserver mock_observer(view_->GetMainRenderFrame());
329 // Block scripts. 419 // Block scripts.
330 RendererContentSettingRules content_setting_rules; 420 RendererContentSettingRules content_setting_rules;
331 ContentSettingsForOneType& script_setting_rules = 421 ContentSettingsForOneType& script_setting_rules =
332 content_setting_rules.script_rules; 422 content_setting_rules.script_rules;
333 script_setting_rules.push_back( 423 script_setting_rules.push_back(
334 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), 424 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
335 ContentSettingsPattern::Wildcard(), 425 ContentSettingsPattern::Wildcard(),
336 CONTENT_SETTING_BLOCK, 426 CONTENT_SETTING_BLOCK,
337 std::string(), 427 std::string(),
338 false)); 428 false));
339 // Block images. 429 // Block images.
340 ContentSettingsForOneType& image_setting_rules = 430 ContentSettingsForOneType& image_setting_rules =
341 content_setting_rules.image_rules; 431 content_setting_rules.image_rules;
342 image_setting_rules.push_back( 432 image_setting_rules.push_back(
343 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(), 433 ContentSettingPatternSource(ContentSettingsPattern::Wildcard(),
344 ContentSettingsPattern::Wildcard(), 434 ContentSettingsPattern::Wildcard(),
345 CONTENT_SETTING_BLOCK, 435 CONTENT_SETTING_BLOCK,
346 std::string(), 436 std::string(),
347 false)); 437 false));
348 438
349 ContentSettingsObserver* observer = 439 ContentSettingsObserver* observer =
350 ContentSettingsObserver::Get(view_->GetMainRenderFrame()); 440 ContentSettingsObserver::Get(view_->GetMainRenderFrame());
351 observer->SetContentSettingRules(&content_setting_rules); 441 observer->SetContentSettingRules(&content_setting_rules);
352 observer->OnSetAsInterstitial(); 442 observer->OnSetAsInterstitial();
353 443
354 // Load a page which contains a script. 444 // Load a page which contains a script.
355 std::string html = "<html>" 445 const char kHtml[] =
356 "<head>" 446 "<html>"
357 "<script src='data:foo'></script>" 447 "<head>"
358 "</head>" 448 "<script src='data:foo'></script>"
359 "<body>" 449 "</head>"
360 "</body>" 450 "<body>"
361 "</html>"; 451 "</body>"
362 LoadHTML(html.c_str()); 452 "</html>";
453 LoadHTML(kHtml);
363 454
364 // Verify that the script was allowed. 455 // Verify that the script was allowed.
365 bool was_blocked = false; 456 bool was_blocked = false;
366 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) { 457 for (size_t i = 0; i < render_thread_->sink().message_count(); ++i) {
367 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i); 458 const IPC::Message* msg = render_thread_->sink().GetMessageAt(i);
368 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID) 459 if (msg->type() == ChromeViewHostMsg_ContentBlocked::ID)
369 was_blocked = true; 460 was_blocked = true;
370 } 461 }
371 EXPECT_FALSE(was_blocked); 462 EXPECT_FALSE(was_blocked);
372 463
373 // Verify that images are allowed. 464 // Verify that images are allowed.
374 EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, 465 EXPECT_CALL(mock_observer, OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES,
375 base::string16())).Times(0); 466 base::string16())).Times(0);
376 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_)); 467 EXPECT_TRUE(observer->allowImage(true, mock_observer.image_url_));
377 ::testing::Mock::VerifyAndClearExpectations(&observer); 468 ::testing::Mock::VerifyAndClearExpectations(&observer);
378 } 469 }
OLDNEW
« chrome/renderer/content_settings_observer.cc ('K') | « chrome/renderer/content_settings_observer.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698