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" |
11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
15 #include "components/content_settings/core/browser/host_content_settings_map.h" | 15 #include "components/content_settings/core/browser/host_content_settings_map.h" |
16 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
17 #include "content/public/browser/navigation_details.h" | 17 #include "content/public/browser/navigation_details.h" |
18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
19 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
21 #include "content/public/common/frame_navigate_params.h" | 21 #include "content/public/common/frame_navigate_params.h" |
| 22 #include "content/public/test/web_contents_tester.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 #if defined(OS_ANDROID) | 25 #if defined(OS_ANDROID) |
25 #include "chrome/browser/download/download_request_infobar_delegate_android.h" | 26 #include "chrome/browser/download/download_request_infobar_delegate_android.h" |
26 #include "chrome/browser/infobars/infobar_service.h" | 27 #include "chrome/browser/infobars/infobar_service.h" |
27 #else | 28 #else |
28 #include "chrome/browser/download/download_permission_request.h" | 29 #include "chrome/browser/download/download_permission_request.h" |
29 #include "chrome/browser/ui/website_settings/mock_permission_bubble_factory.h" | 30 #include "chrome/browser/ui/website_settings/mock_permission_bubble_factory.h" |
30 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 31 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
31 #endif | 32 #endif |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 } | 231 } |
231 | 232 |
232 void LoadCompleted() { testing_delegate_.LoadCompleted(web_contents()); } | 233 void LoadCompleted() { testing_delegate_.LoadCompleted(web_contents()); } |
233 | 234 |
234 int AskAllowCount() { return testing_delegate_.AllowCount(); } | 235 int AskAllowCount() { return testing_delegate_.AllowCount(); } |
235 | 236 |
236 void UpdateExpectations(TestingAction action) { | 237 void UpdateExpectations(TestingAction action) { |
237 testing_delegate_.UpdateExpectations(action); | 238 testing_delegate_.UpdateExpectations(action); |
238 } | 239 } |
239 | 240 |
| 241 void NavigateAndCommitWithParams( |
| 242 content::NavigationController::LoadURLParams& params) { |
| 243 controller().LoadURLWithParams(params); |
| 244 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 245 } |
| 246 |
240 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; | 247 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; |
241 | 248 |
242 // Number of times ContinueDownload was invoked. | 249 // Number of times ContinueDownload was invoked. |
243 int continue_count_; | 250 int continue_count_; |
244 | 251 |
245 // Number of times CancelDownload was invoked. | 252 // Number of times CancelDownload was invoked. |
246 int cancel_count_; | 253 int cancel_count_; |
247 | 254 |
248 scoped_refptr<HostContentSettingsMap> content_settings_; | 255 scoped_refptr<HostContentSettingsMap> content_settings_; |
249 TestingDelegate testing_delegate_; | 256 TestingDelegate testing_delegate_; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 OnUserInteraction(blink::WebInputEvent::RawKeyDown); | 322 OnUserInteraction(blink::WebInputEvent::RawKeyDown); |
316 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 323 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
317 download_request_limiter_->GetDownloadStatus(web_contents())); | 324 download_request_limiter_->GetDownloadStatus(web_contents())); |
318 | 325 |
319 // Navigate to a completely different host, which should reset the state. | 326 // Navigate to a completely different host, which should reset the state. |
320 NavigateAndCommit(GURL("http://fooey.com")); | 327 NavigateAndCommit(GURL("http://fooey.com")); |
321 LoadCompleted(); | 328 LoadCompleted(); |
322 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 329 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
323 download_request_limiter_->GetDownloadStatus(web_contents())); | 330 download_request_limiter_->GetDownloadStatus(web_contents())); |
324 | 331 |
325 // Do two downloads, allowing the second so that we end up with allow all. | 332 // Do two downloads, blocking the second so that we end up with downloads not |
| 333 // allowed. |
326 CanDownload(); | 334 CanDownload(); |
327 ExpectAndResetCounts(1, 0, 0, __LINE__); | 335 ExpectAndResetCounts(1, 0, 0, __LINE__); |
328 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 336 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
329 download_request_limiter_->GetDownloadStatus(web_contents())); | 337 download_request_limiter_->GetDownloadStatus(web_contents())); |
330 | 338 |
331 UpdateExpectations(CANCEL); | 339 UpdateExpectations(CANCEL); |
332 CanDownload(); | 340 CanDownload(); |
333 ExpectAndResetCounts(0, 1, 1, __LINE__); | 341 ExpectAndResetCounts(0, 1, 1, __LINE__); |
334 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 342 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
335 download_request_limiter_->GetDownloadStatus(web_contents())); | 343 download_request_limiter_->GetDownloadStatus(web_contents())); |
336 | 344 |
337 // Navigate to a new URL with the same host, which shouldn't reset the allow | 345 // Navigate to a new URL with the same host, which shouldn't reset the allow |
338 // all state. | 346 // all state. |
339 NavigateAndCommit(GURL("http://fooey.com/bar2")); | 347 NavigateAndCommit(GURL("http://fooey.com/bar2")); |
340 LoadCompleted(); | 348 LoadCompleted(); |
341 CanDownload(); | 349 CanDownload(); |
342 ExpectAndResetCounts(0, 1, 0, __LINE__); | 350 ExpectAndResetCounts(0, 1, 0, __LINE__); |
343 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 351 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
344 download_request_limiter_->GetDownloadStatus(web_contents())); | 352 download_request_limiter_->GetDownloadStatus(web_contents())); |
345 } | 353 } |
346 | 354 |
| 355 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_RendererInitiated) { |
| 356 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 357 LoadCompleted(); |
| 358 |
| 359 // Do one download so we end up in PROMPT. |
| 360 CanDownload(); |
| 361 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 362 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 363 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 364 |
| 365 // Set up a renderer-initiated navigation to the same host. |
| 366 content::NavigationController::LoadURLParams load_params( |
| 367 GURL("http://foo.com/bar2")); |
| 368 load_params.is_renderer_initiated = true; |
| 369 load_params.transition_type = ui::PAGE_TRANSITION_GENERATED; |
| 370 load_params.referrer = content::Referrer(); |
| 371 NavigateAndCommitWithParams(load_params); |
| 372 LoadCompleted(); |
| 373 |
| 374 // The state should not be reset. |
| 375 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 376 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 377 |
| 378 // Renderer-initiated nav to a different host shouldn't reset the state. |
| 379 load_params.url = GURL("http://fooey.com/bar"); |
| 380 NavigateAndCommitWithParams(load_params); |
| 381 LoadCompleted(); |
| 382 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 383 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 384 |
| 385 // Set up a subframe. Navigations in the subframe shouldn't reset the state. |
| 386 content::RenderFrameHostTester* rfh_tester = |
| 387 content::RenderFrameHostTester::For(web_contents()->GetMainFrame()); |
| 388 content::RenderFrameHost* subframe = rfh_tester->AppendChild("subframe"); |
| 389 content::RenderFrameHostTester* subframe_tester = |
| 390 content::RenderFrameHostTester::For(subframe); |
| 391 subframe_tester->SimulateNavigationCommit(GURL("http://foo.com")); |
| 392 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 393 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 394 |
| 395 subframe_tester->SimulateNavigationCommit(GURL("http://foobargoo.com")); |
| 396 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 397 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 398 |
| 399 // Set up a blocked state. |
| 400 UpdateExpectations(CANCEL); |
| 401 CanDownload(); |
| 402 ExpectAndResetCounts(0, 1, 1, __LINE__); |
| 403 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 404 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 405 |
| 406 // The state should not be reset on a renderer-initiated load to either the |
| 407 // same host or a different host, in either the main frame or the subframe. |
| 408 load_params.url = GURL("http://fooeybar.com/bar"); |
| 409 NavigateAndCommitWithParams(load_params); |
| 410 LoadCompleted(); |
| 411 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 412 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 413 |
| 414 load_params.url = GURL("http://foo.com/bar"); |
| 415 NavigateAndCommitWithParams(load_params); |
| 416 LoadCompleted(); |
| 417 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 418 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 419 |
| 420 rfh_tester = |
| 421 content::RenderFrameHostTester::For(web_contents()->GetMainFrame()); |
| 422 subframe = rfh_tester->AppendChild("subframe"); |
| 423 subframe_tester = content::RenderFrameHostTester::For(subframe); |
| 424 subframe_tester->SimulateNavigationCommit(GURL("http://foo.com")); |
| 425 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 426 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 427 |
| 428 subframe_tester->SimulateNavigationCommit(GURL("http://foobarfoo.com")); |
| 429 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 430 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 431 |
| 432 // Browser-initiated navigation to a different host, which should reset the |
| 433 // state. |
| 434 NavigateAndCommit(GURL("http://foobar.com")); |
| 435 LoadCompleted(); |
| 436 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 437 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 438 |
| 439 // Set up an allow all state. |
| 440 CanDownload(); |
| 441 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 442 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 443 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 444 |
| 445 UpdateExpectations(ACCEPT); |
| 446 CanDownload(); |
| 447 ExpectAndResetCounts(1, 0, 1, __LINE__); |
| 448 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 449 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 450 |
| 451 // The state should not be reset on a pending renderer-initiated load to |
| 452 // the same host. |
| 453 load_params.url = GURL("http://foobar.com/bar"); |
| 454 NavigateAndCommitWithParams(load_params); |
| 455 LoadCompleted(); |
| 456 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 457 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 458 |
| 459 // The state should not be reset for a subframe nav to the same host. |
| 460 rfh_tester = |
| 461 content::RenderFrameHostTester::For(web_contents()->GetMainFrame()); |
| 462 subframe = rfh_tester->AppendChild("subframe"); |
| 463 subframe_tester = content::RenderFrameHostTester::For(subframe); |
| 464 subframe_tester->SimulateNavigationCommit(GURL("http://foobar.com/bar")); |
| 465 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 466 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 467 |
| 468 subframe_tester->SimulateNavigationCommit(GURL("http://foobarfoo.com/")); |
| 469 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 470 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 471 |
| 472 // But a pending load to a different host in the main frame should reset the |
| 473 // state. |
| 474 load_params.url = GURL("http://foo.com"); |
| 475 NavigateAndCommitWithParams(load_params); |
| 476 LoadCompleted(); |
| 477 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 478 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 479 } |
| 480 |
347 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_ResetOnUserGesture) { | 481 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_ResetOnUserGesture) { |
348 NavigateAndCommit(GURL("http://foo.com/bar")); | 482 NavigateAndCommit(GURL("http://foo.com/bar")); |
349 LoadCompleted(); | 483 LoadCompleted(); |
350 | 484 |
351 // Do one download, which should change to prompt before download. | 485 // Do one download, which should change to prompt before download. |
352 CanDownload(); | 486 CanDownload(); |
353 ExpectAndResetCounts(1, 0, 0, __LINE__); | 487 ExpectAndResetCounts(1, 0, 0, __LINE__); |
354 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 488 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
355 download_request_limiter_->GetDownloadStatus(web_contents())); | 489 download_request_limiter_->GetDownloadStatus(web_contents())); |
356 | 490 |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 // changing the content setting). | 716 // changing the content setting). |
583 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); | 717 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); |
584 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 718 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
585 download_request_limiter_->GetDownloadStatus(web_contents())); | 719 download_request_limiter_->GetDownloadStatus(web_contents())); |
586 UpdateExpectations(WAIT); | 720 UpdateExpectations(WAIT); |
587 CanDownload(); | 721 CanDownload(); |
588 ExpectAndResetCounts(0, 0, 1, __LINE__); | 722 ExpectAndResetCounts(0, 0, 1, __LINE__); |
589 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 723 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
590 download_request_limiter_->GetDownloadStatus(web_contents())); | 724 download_request_limiter_->GetDownloadStatus(web_contents())); |
591 } | 725 } |
OLD | NEW |