Chromium Code Reviews| 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/browser/content_settings/host_content_settings_map_factory.h" | 12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 16 #include "components/content_settings/core/browser/host_content_settings_map.h" | 16 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 17 #include "content/public/browser/navigation_controller.h" | 17 #include "content/public/browser/navigation_controller.h" |
| 18 #include "content/public/browser/navigation_details.h" | 18 #include "content/public/browser/navigation_details.h" |
| 19 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
| 20 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
| 21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 22 #include "content/public/common/frame_navigate_params.h" | 22 #include "content/public/common/frame_navigate_params.h" |
| 23 #include "content/public/test/web_contents_tester.h" | 23 #include "content/public/test/web_contents_tester.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 25 |
| 26 #if defined(OS_ANDROID) | |
| 27 #include "chrome/browser/download/download_request_infobar_delegate_android.h" | |
| 28 #include "chrome/browser/infobars/infobar_service.h" | |
| 29 #else | |
| 30 #include "chrome/browser/download/download_permission_request.h" | 26 #include "chrome/browser/download/download_permission_request.h" |
| 31 #include "chrome/browser/permissions/permission_request_manager.h" | 27 #include "chrome/browser/permissions/permission_request_manager.h" |
| 32 #include "chrome/browser/ui/permission_bubble/mock_permission_prompt_factory.h" | 28 #include "chrome/browser/ui/permission_bubble/mock_permission_prompt_factory.h" |
| 33 #endif | |
| 34 | 29 |
| 35 using content::WebContents; | 30 using content::WebContents; |
| 36 | 31 |
| 37 namespace { | 32 namespace { |
| 38 enum TestingAction { | 33 enum TestingAction { |
| 39 ACCEPT, | 34 ACCEPT, |
| 40 CANCEL, | 35 CANCEL, |
| 41 WAIT | 36 WAIT |
| 42 }; | 37 }; |
| 43 | 38 |
| 44 #if defined(OS_ANDROID) | |
| 45 class TestingDelegate { | 39 class TestingDelegate { |
| 46 public: | 40 public: |
| 47 void SetUp(WebContents* web_contents) { | 41 void SetUp(WebContents* web_contents) { |
| 48 InfoBarService::CreateForWebContents(web_contents); | |
| 49 fake_create_callback_ = | |
| 50 base::Bind(&TestingDelegate::FakeCreate, base::Unretained(this)); | |
| 51 DownloadRequestInfoBarDelegateAndroid::SetCallbackForTesting( | |
| 52 &fake_create_callback_); | |
| 53 ResetCounts(); | |
|
raymes
2017/06/20 04:13:46
I think we should just put if (PermissionRequestMa
| |
| 54 } | |
| 55 | |
| 56 void TearDown() { UnsetInfobarDelegate(); } | |
| 57 | |
| 58 void LoadCompleted(WebContents* /*web_contents*/) { | |
| 59 // No action needed on OS_ANDROID. | |
| 60 } | |
| 61 | |
| 62 void ResetCounts() { ask_allow_count_ = 0; } | |
| 63 | |
| 64 int AllowCount() { return ask_allow_count_; } | |
| 65 | |
| 66 void UpdateExpectations(TestingAction action) { testing_action_ = action; } | |
| 67 | |
| 68 void FakeCreate( | |
| 69 InfoBarService* infobar_service, | |
| 70 base::WeakPtr<DownloadRequestLimiter::TabDownloadState> host) { | |
| 71 ask_allow_count_++; | |
| 72 switch (testing_action_) { | |
| 73 case ACCEPT: | |
| 74 host->Accept(); | |
| 75 break; | |
| 76 case CANCEL: | |
| 77 host->Cancel(); | |
| 78 break; | |
| 79 case WAIT: | |
| 80 break; | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void UnsetInfobarDelegate() { | |
| 85 DownloadRequestInfoBarDelegateAndroid::SetCallbackForTesting(nullptr); | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 // Number of times ShouldAllowDownload was invoked. | |
| 90 int ask_allow_count_; | |
| 91 | |
| 92 // The action that FakeCreate() should take. | |
| 93 TestingAction testing_action_; | |
| 94 | |
| 95 DownloadRequestInfoBarDelegateAndroid::FakeCreateCallback | |
| 96 fake_create_callback_; | |
| 97 }; | |
| 98 #else | |
| 99 class TestingDelegate { | |
| 100 public: | |
| 101 void SetUp(WebContents* web_contents) { | |
| 102 PermissionRequestManager::CreateForWebContents(web_contents); | 42 PermissionRequestManager::CreateForWebContents(web_contents); |
| 103 mock_permission_prompt_factory_.reset(new MockPermissionPromptFactory( | 43 mock_permission_prompt_factory_.reset(new MockPermissionPromptFactory( |
| 104 PermissionRequestManager::FromWebContents(web_contents))); | 44 PermissionRequestManager::FromWebContents(web_contents))); |
| 105 PermissionRequestManager::FromWebContents(web_contents) | 45 PermissionRequestManager::FromWebContents(web_contents) |
| 106 ->DisplayPendingRequests(); | 46 ->DisplayPendingRequests(); |
| 107 } | 47 } |
| 108 | 48 |
| 109 void TearDown() { mock_permission_prompt_factory_.reset(); } | 49 void TearDown() { mock_permission_prompt_factory_.reset(); } |
| 110 | 50 |
| 111 void LoadCompleted(WebContents* web_contents) { | 51 void LoadCompleted(WebContents* web_contents) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 130 case WAIT: | 70 case WAIT: |
| 131 response_type = PermissionRequestManager::NONE; | 71 response_type = PermissionRequestManager::NONE; |
| 132 break; | 72 break; |
| 133 } | 73 } |
| 134 mock_permission_prompt_factory_->set_response_type(response_type); | 74 mock_permission_prompt_factory_->set_response_type(response_type); |
| 135 } | 75 } |
| 136 | 76 |
| 137 private: | 77 private: |
| 138 std::unique_ptr<MockPermissionPromptFactory> mock_permission_prompt_factory_; | 78 std::unique_ptr<MockPermissionPromptFactory> mock_permission_prompt_factory_; |
| 139 }; | 79 }; |
| 140 #endif | |
| 141 } // namespace | 80 } // namespace |
| 142 | 81 |
| 143 class DownloadRequestLimiterTest : public ChromeRenderViewHostTestHarness { | 82 class DownloadRequestLimiterTest : public ChromeRenderViewHostTestHarness { |
| 144 public: | 83 public: |
| 145 void SetUp() override { | 84 void SetUp() override { |
| 146 ChromeRenderViewHostTestHarness::SetUp(); | 85 ChromeRenderViewHostTestHarness::SetUp(); |
| 147 testing_delegate_.SetUp(web_contents()); | 86 testing_delegate_.SetUp(web_contents()); |
| 148 | 87 |
| 149 UpdateExpectations(ACCEPT); | 88 UpdateExpectations(ACCEPT); |
| 150 cancel_count_ = continue_count_ = 0; | 89 cancel_count_ = continue_count_ = 0; |
| (...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 564 LoadCompleted(); | 503 LoadCompleted(); |
| 565 base::RunLoop().RunUntilIdle(); | 504 base::RunLoop().RunUntilIdle(); |
| 566 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 505 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 567 download_request_limiter_->GetDownloadStatus(web_contents())); | 506 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 568 CanDownload(); | 507 CanDownload(); |
| 569 ExpectAndResetCounts(0, 1, 0, __LINE__); | 508 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 570 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 509 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 571 download_request_limiter_->GetDownloadStatus(web_contents())); | 510 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 572 } | 511 } |
| 573 | 512 |
| 574 #if defined(OS_ANDROID) | |
| 575 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_RawWebContents) { | 513 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_RawWebContents) { |
| 576 std::unique_ptr<WebContents> web_contents(CreateTestWebContents()); | 514 std::unique_ptr<WebContents> web_contents(CreateTestWebContents()); |
| 577 | 515 |
| 578 GURL url("http://foo.com/bar"); | 516 GURL url("http://foo.com/bar"); |
| 579 web_contents->GetController().LoadURL( | 517 web_contents->GetController().LoadURL( |
| 580 url, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string()); | 518 url, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string()); |
| 581 | 519 |
| 582 // DownloadRequestLimiter won't try to make a permission bubble if there's | 520 // DownloadRequestLimiter won't try to make a permission request if there's |
| 583 // no permission bubble manager, so don't put one on the test WebContents. | 521 // no permission request manager, so don't put one on the test WebContents. |
| 584 | |
| 585 // DownloadRequestLimiter won't try to make an infobar if it doesn't have an | |
| 586 // InfoBarService, and we want to test that it will Cancel() instead of | |
| 587 // prompting when it doesn't have a InfoBarService, so unset the delegate. | |
| 588 testing_delegate_.UnsetInfobarDelegate(); | |
| 589 ExpectAndResetCounts(0, 0, 0, __LINE__); | 522 ExpectAndResetCounts(0, 0, 0, __LINE__); |
| 590 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 523 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 591 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 524 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 592 // You get one freebie. | 525 // You get one freebie. |
| 593 CanDownloadFor(web_contents.get()); | 526 CanDownloadFor(web_contents.get()); |
| 594 ExpectAndResetCounts(1, 0, 0, __LINE__); | 527 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 595 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 528 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 596 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 529 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 597 OnUserInteractionFor(web_contents.get(), blink::WebInputEvent::kTouchStart); | 530 OnUserInteractionFor(web_contents.get(), blink::WebInputEvent::kTouchStart); |
| 598 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 531 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 599 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 532 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 600 CanDownloadFor(web_contents.get()); | 533 CanDownloadFor(web_contents.get()); |
| 601 ExpectAndResetCounts(1, 0, 0, __LINE__); | 534 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 602 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 535 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 603 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 536 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 604 CanDownloadFor(web_contents.get()); | 537 CanDownloadFor(web_contents.get()); |
| 605 ExpectAndResetCounts(0, 1, 0, __LINE__); | 538 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 606 EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 539 EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 607 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 540 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 608 OnUserInteractionFor(web_contents.get(), blink::WebInputEvent::kRawKeyDown); | 541 OnUserInteractionFor(web_contents.get(), blink::WebInputEvent::kRawKeyDown); |
| 609 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 542 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 610 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 543 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 611 CanDownloadFor(web_contents.get()); | 544 CanDownloadFor(web_contents.get()); |
| 612 ExpectAndResetCounts(1, 0, 0, __LINE__); | 545 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 613 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 546 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 614 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 547 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 615 } | 548 } |
| 616 #endif | |
| 617 | 549 |
| 618 TEST_F(DownloadRequestLimiterTest, | 550 TEST_F(DownloadRequestLimiterTest, |
| 619 DownloadRequestLimiter_SetHostContentSetting) { | 551 DownloadRequestLimiter_SetHostContentSetting) { |
| 620 NavigateAndCommit(GURL("http://foo.com/bar")); | 552 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 621 LoadCompleted(); | 553 LoadCompleted(); |
| 622 SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW); | 554 SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW); |
| 623 | 555 |
| 624 CanDownload(); | 556 CanDownload(); |
| 625 ExpectAndResetCounts(1, 0, 0, __LINE__); | 557 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 626 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 558 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 692 // changing the content setting). | 624 // changing the content setting). |
| 693 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); | 625 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); |
| 694 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 626 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 695 download_request_limiter_->GetDownloadStatus(web_contents())); | 627 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 696 UpdateExpectations(WAIT); | 628 UpdateExpectations(WAIT); |
| 697 CanDownload(); | 629 CanDownload(); |
| 698 ExpectAndResetCounts(0, 0, 1, __LINE__); | 630 ExpectAndResetCounts(0, 0, 1, __LINE__); |
| 699 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 631 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 700 download_request_limiter_->GetDownloadStatus(web_contents())); | 632 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 701 } | 633 } |
| OLD | NEW |