| 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 "chrome/browser/download/download_request_limiter.h" | 5 #include "chrome/browser/download/download_request_limiter.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 } | 232 } |
| 233 | 233 |
| 234 void LoadCompleted() { testing_delegate_.LoadCompleted(web_contents()); } | 234 void LoadCompleted() { testing_delegate_.LoadCompleted(web_contents()); } |
| 235 | 235 |
| 236 int AskAllowCount() { return testing_delegate_.AllowCount(); } | 236 int AskAllowCount() { return testing_delegate_.AllowCount(); } |
| 237 | 237 |
| 238 void UpdateExpectations(TestingAction action) { | 238 void UpdateExpectations(TestingAction action) { |
| 239 testing_delegate_.UpdateExpectations(action); | 239 testing_delegate_.UpdateExpectations(action); |
| 240 } | 240 } |
| 241 | 241 |
| 242 void NavigateAndCommitWithParams( | |
| 243 content::NavigationController::LoadURLParams& params) { | |
| 244 controller().LoadURLWithParams(params); | |
| 245 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); | |
| 246 } | |
| 247 | |
| 248 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; | 242 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; |
| 249 | 243 |
| 250 // Number of times ContinueDownload was invoked. | 244 // Number of times ContinueDownload was invoked. |
| 251 int continue_count_; | 245 int continue_count_; |
| 252 | 246 |
| 253 // Number of times CancelDownload was invoked. | 247 // Number of times CancelDownload was invoked. |
| 254 int cancel_count_; | 248 int cancel_count_; |
| 255 | 249 |
| 256 scoped_refptr<HostContentSettingsMap> content_settings_; | 250 scoped_refptr<HostContentSettingsMap> content_settings_; |
| 257 TestingDelegate testing_delegate_; | 251 TestingDelegate testing_delegate_; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 NavigateAndCommit(GURL("http://foo.com/bar")); | 351 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 358 LoadCompleted(); | 352 LoadCompleted(); |
| 359 | 353 |
| 360 // Do one download so we end up in PROMPT. | 354 // Do one download so we end up in PROMPT. |
| 361 CanDownload(); | 355 CanDownload(); |
| 362 ExpectAndResetCounts(1, 0, 0, __LINE__); | 356 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 363 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 357 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 364 download_request_limiter_->GetDownloadStatus(web_contents())); | 358 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 365 | 359 |
| 366 // Set up a renderer-initiated navigation to the same host. | 360 // Set up a renderer-initiated navigation to the same host. |
| 367 content::NavigationController::LoadURLParams load_params( | 361 content::RenderFrameHostTester* rfh_tester = |
| 368 GURL("http://foo.com/bar2")); | 362 content::RenderFrameHostTester::For(web_contents()->GetMainFrame()); |
| 369 load_params.is_renderer_initiated = true; | 363 rfh_tester->NavigateAndCommitRendererInitiated(1, true, |
| 370 load_params.transition_type = ui::PAGE_TRANSITION_GENERATED; | 364 GURL("http://foo.com/bar2")); |
| 371 load_params.referrer = content::Referrer(); | |
| 372 NavigateAndCommitWithParams(load_params); | |
| 373 LoadCompleted(); | 365 LoadCompleted(); |
| 374 | 366 |
| 375 // The state should not be reset. | 367 // The state should not be reset. |
| 376 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 368 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 377 download_request_limiter_->GetDownloadStatus(web_contents())); | 369 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 378 | 370 |
| 379 // Renderer-initiated nav to a different host shouldn't reset the state. | 371 // Renderer-initiated nav to a different host shouldn't reset the state. |
| 380 load_params.url = GURL("http://fooey.com/bar"); | 372 rfh_tester->NavigateAndCommitRendererInitiated(2, true, |
| 381 NavigateAndCommitWithParams(load_params); | 373 GURL("http://fooey.com/bar")); |
| 382 LoadCompleted(); | 374 LoadCompleted(); |
| 383 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 375 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 384 download_request_limiter_->GetDownloadStatus(web_contents())); | 376 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 385 | 377 |
| 386 // Set up a subframe. Navigations in the subframe shouldn't reset the state. | 378 // Set up a subframe. Navigations in the subframe shouldn't reset the state. |
| 387 content::RenderFrameHostTester* rfh_tester = | |
| 388 content::RenderFrameHostTester::For(web_contents()->GetMainFrame()); | |
| 389 content::RenderFrameHost* subframe = rfh_tester->AppendChild("subframe"); | 379 content::RenderFrameHost* subframe = rfh_tester->AppendChild("subframe"); |
| 390 content::RenderFrameHostTester* subframe_tester = | 380 content::RenderFrameHostTester* subframe_tester = |
| 391 content::RenderFrameHostTester::For(subframe); | 381 content::RenderFrameHostTester::For(subframe); |
| 392 subframe_tester->SimulateNavigationCommit(GURL("http://foo.com")); | 382 subframe_tester->SimulateNavigationCommit(GURL("http://foo.com")); |
| 393 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 383 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 394 download_request_limiter_->GetDownloadStatus(web_contents())); | 384 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 395 | 385 |
| 396 subframe_tester->SimulateNavigationCommit(GURL("http://foobargoo.com")); | 386 subframe_tester->SimulateNavigationCommit(GURL("http://foobargoo.com")); |
| 397 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 387 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 398 download_request_limiter_->GetDownloadStatus(web_contents())); | 388 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 399 | 389 |
| 400 // Set up a blocked state. | 390 // Set up a blocked state. |
| 401 UpdateExpectations(CANCEL); | 391 UpdateExpectations(CANCEL); |
| 402 CanDownload(); | 392 CanDownload(); |
| 403 ExpectAndResetCounts(0, 1, 1, __LINE__); | 393 ExpectAndResetCounts(0, 1, 1, __LINE__); |
| 404 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 394 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 405 download_request_limiter_->GetDownloadStatus(web_contents())); | 395 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 406 | 396 |
| 407 // The state should not be reset on a renderer-initiated load to either the | 397 // The state should not be reset on a renderer-initiated load to either the |
| 408 // same host or a different host, in either the main frame or the subframe. | 398 // same host or a different host, in either the main frame or the subframe. |
| 409 load_params.url = GURL("http://fooeybar.com/bar"); | 399 rfh_tester->NavigateAndCommitRendererInitiated( |
| 410 NavigateAndCommitWithParams(load_params); | 400 3, true, GURL("http://fooeybar.com/bar")); |
| 411 LoadCompleted(); | 401 LoadCompleted(); |
| 412 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 402 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 413 download_request_limiter_->GetDownloadStatus(web_contents())); | 403 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 414 | 404 |
| 415 load_params.url = GURL("http://foo.com/bar"); | 405 rfh_tester->NavigateAndCommitRendererInitiated(4, true, |
| 416 NavigateAndCommitWithParams(load_params); | 406 GURL("http://foo.com/bar")); |
| 417 LoadCompleted(); | 407 LoadCompleted(); |
| 418 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 408 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 419 download_request_limiter_->GetDownloadStatus(web_contents())); | 409 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 420 | 410 |
| 421 rfh_tester = | 411 rfh_tester = |
| 422 content::RenderFrameHostTester::For(web_contents()->GetMainFrame()); | 412 content::RenderFrameHostTester::For(web_contents()->GetMainFrame()); |
| 423 subframe = rfh_tester->AppendChild("subframe"); | 413 subframe = rfh_tester->AppendChild("subframe"); |
| 424 subframe_tester = content::RenderFrameHostTester::For(subframe); | 414 subframe_tester = content::RenderFrameHostTester::For(subframe); |
| 425 subframe_tester->SimulateNavigationCommit(GURL("http://foo.com")); | 415 subframe_tester->SimulateNavigationCommit(GURL("http://foo.com")); |
| 426 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 416 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 444 download_request_limiter_->GetDownloadStatus(web_contents())); | 434 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 445 | 435 |
| 446 UpdateExpectations(ACCEPT); | 436 UpdateExpectations(ACCEPT); |
| 447 CanDownload(); | 437 CanDownload(); |
| 448 ExpectAndResetCounts(1, 0, 1, __LINE__); | 438 ExpectAndResetCounts(1, 0, 1, __LINE__); |
| 449 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 439 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 450 download_request_limiter_->GetDownloadStatus(web_contents())); | 440 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 451 | 441 |
| 452 // The state should not be reset on a pending renderer-initiated load to | 442 // The state should not be reset on a pending renderer-initiated load to |
| 453 // the same host. | 443 // the same host. |
| 454 load_params.url = GURL("http://foobar.com/bar"); | 444 rfh_tester = |
| 455 NavigateAndCommitWithParams(load_params); | 445 content::RenderFrameHostTester::For(web_contents()->GetMainFrame()); |
| 446 rfh_tester->NavigateAndCommitRendererInitiated(5, true, |
| 447 GURL("http://foobar.com/bar")); |
| 456 LoadCompleted(); | 448 LoadCompleted(); |
| 457 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 449 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 458 download_request_limiter_->GetDownloadStatus(web_contents())); | 450 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 459 | 451 |
| 460 // The state should not be reset for a subframe nav to the same host. | 452 // The state should not be reset for a subframe nav to the same host. |
| 461 rfh_tester = | 453 rfh_tester = |
| 462 content::RenderFrameHostTester::For(web_contents()->GetMainFrame()); | 454 content::RenderFrameHostTester::For(web_contents()->GetMainFrame()); |
| 463 subframe = rfh_tester->AppendChild("subframe"); | 455 subframe = rfh_tester->AppendChild("subframe"); |
| 464 subframe_tester = content::RenderFrameHostTester::For(subframe); | 456 subframe_tester = content::RenderFrameHostTester::For(subframe); |
| 465 subframe_tester->SimulateNavigationCommit(GURL("http://foobar.com/bar")); | 457 subframe_tester->SimulateNavigationCommit(GURL("http://foobar.com/bar")); |
| 466 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 458 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 467 download_request_limiter_->GetDownloadStatus(web_contents())); | 459 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 468 | 460 |
| 469 subframe_tester->SimulateNavigationCommit(GURL("http://foobarfoo.com/")); | 461 subframe_tester->SimulateNavigationCommit(GURL("http://foobarfoo.com/")); |
| 470 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 462 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 471 download_request_limiter_->GetDownloadStatus(web_contents())); | 463 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 472 | 464 |
| 473 // But a pending load to a different host in the main frame should reset the | 465 // But a pending load to a different host in the main frame should reset the |
| 474 // state. | 466 // state. |
| 475 load_params.url = GURL("http://foo.com"); | 467 rfh_tester->NavigateAndCommitRendererInitiated(6, true, |
| 476 NavigateAndCommitWithParams(load_params); | 468 GURL("http://foo.com")); |
| 477 LoadCompleted(); | 469 LoadCompleted(); |
| 478 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 470 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 479 download_request_limiter_->GetDownloadStatus(web_contents())); | 471 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 480 } | 472 } |
| 481 | 473 |
| 482 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_ResetOnUserGesture) { | 474 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_ResetOnUserGesture) { |
| 483 NavigateAndCommit(GURL("http://foo.com/bar")); | 475 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 484 LoadCompleted(); | 476 LoadCompleted(); |
| 485 | 477 |
| 486 // Do one download, which should change to prompt before download. | 478 // Do one download, which should change to prompt before download. |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 717 // changing the content setting). | 709 // changing the content setting). |
| 718 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); | 710 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); |
| 719 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 711 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 720 download_request_limiter_->GetDownloadStatus(web_contents())); | 712 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 721 UpdateExpectations(WAIT); | 713 UpdateExpectations(WAIT); |
| 722 CanDownload(); | 714 CanDownload(); |
| 723 ExpectAndResetCounts(0, 0, 1, __LINE__); | 715 ExpectAndResetCounts(0, 0, 1, __LINE__); |
| 724 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 716 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 725 download_request_limiter_->GetDownloadStatus(web_contents())); | 717 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 726 } | 718 } |
| OLD | NEW |