| 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 "chrome/browser/download/download_permission_request.h" | 10 #include "chrome/browser/download/download_permission_request.h" |
| 11 #include "chrome/browser/download/download_request_infobar_delegate.h" | |
| 12 #include "chrome/browser/infobars/infobar_service.h" | |
| 13 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 11 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
| 14 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 15 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 18 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
| 19 #include "content/public/browser/navigation_details.h" | 17 #include "content/public/browser/navigation_details.h" |
| 20 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/common/frame_navigate_params.h" | 19 #include "content/public/common/frame_navigate_params.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 21 |
| 22 #if defined(OS_ANDROID) |
| 23 #include "chrome/browser/download/download_request_infobar_delegate.h" |
| 24 #include "chrome/browser/infobars/infobar_service.h" |
| 25 #endif |
| 26 |
| 24 using content::WebContents; | 27 using content::WebContents; |
| 25 | 28 |
| 26 class DownloadRequestLimiterTest; | 29 class DownloadRequestLimiterTest; |
| 27 | 30 |
| 28 class FakePermissionBubbleView : public PermissionBubbleView { | 31 class FakePermissionBubbleView : public PermissionBubbleView { |
| 29 public: | 32 public: |
| 30 class Factory : public base::RefCounted<FakePermissionBubbleView::Factory> { | 33 class Factory : public base::RefCounted<FakePermissionBubbleView::Factory> { |
| 31 public: | 34 public: |
| 32 explicit Factory(DownloadRequestLimiterTest* test) : test_(test) {} | 35 explicit Factory(DownloadRequestLimiterTest* test) : test_(test) {} |
| 33 | 36 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 public: | 78 public: |
| 76 enum TestingAction { | 79 enum TestingAction { |
| 77 ACCEPT, | 80 ACCEPT, |
| 78 CANCEL, | 81 CANCEL, |
| 79 WAIT | 82 WAIT |
| 80 }; | 83 }; |
| 81 | 84 |
| 82 void SetUp() override { | 85 void SetUp() override { |
| 83 ChromeRenderViewHostTestHarness::SetUp(); | 86 ChromeRenderViewHostTestHarness::SetUp(); |
| 84 profile_.reset(new TestingProfile()); | 87 profile_.reset(new TestingProfile()); |
| 85 InfoBarService::CreateForWebContents(web_contents()); | |
| 86 | 88 |
| 87 PermissionBubbleManager::CreateForWebContents(web_contents()); | 89 PermissionBubbleManager::CreateForWebContents(web_contents()); |
| 88 scoped_refptr<FakePermissionBubbleView::Factory> factory = | 90 scoped_refptr<FakePermissionBubbleView::Factory> factory = |
| 89 new FakePermissionBubbleView::Factory(this); | 91 new FakePermissionBubbleView::Factory(this); |
| 90 PermissionBubbleManager::FromWebContents(web_contents())->view_factory_ = | 92 PermissionBubbleManager::FromWebContents(web_contents())->view_factory_ = |
| 91 base::Bind(&FakePermissionBubbleView::Factory::Create, factory); | 93 base::Bind(&FakePermissionBubbleView::Factory::Create, factory); |
| 92 PermissionBubbleManager::FromWebContents(web_contents()) | 94 PermissionBubbleManager::FromWebContents(web_contents()) |
| 93 ->DisplayPendingRequests(nullptr); | 95 ->DisplayPendingRequests(nullptr); |
| 94 | 96 |
| 95 testing_action_ = ACCEPT; | 97 testing_action_ = ACCEPT; |
| 96 ask_allow_count_ = cancel_count_ = continue_count_ = 0; | 98 ask_allow_count_ = cancel_count_ = continue_count_ = 0; |
| 97 download_request_limiter_ = new DownloadRequestLimiter(); | 99 download_request_limiter_ = new DownloadRequestLimiter(); |
| 100 |
| 101 #if defined(OS_ANDROID) |
| 102 InfoBarService::CreateForWebContents(web_contents()); |
| 98 fake_create_callback_ = base::Bind( | 103 fake_create_callback_ = base::Bind( |
| 99 &DownloadRequestLimiterTest::FakeCreate, base::Unretained(this)); | 104 &DownloadRequestLimiterTest::FakeCreate, base::Unretained(this)); |
| 100 DownloadRequestInfoBarDelegate::SetCallbackForTesting( | 105 DownloadRequestInfoBarDelegate::SetCallbackForTesting( |
| 101 &fake_create_callback_); | 106 &fake_create_callback_); |
| 107 #endif |
| 108 |
| 102 content_settings_ = new HostContentSettingsMap(profile_->GetPrefs(), false); | 109 content_settings_ = new HostContentSettingsMap(profile_->GetPrefs(), false); |
| 103 DownloadRequestLimiter::SetContentSettingsForTesting( | 110 DownloadRequestLimiter::SetContentSettingsForTesting( |
| 104 content_settings_.get()); | 111 content_settings_.get()); |
| 105 } | 112 } |
| 106 | 113 |
| 107 int GetAction() { | 114 int GetAction() { |
| 108 return testing_action_; | 115 return testing_action_; |
| 109 } | 116 } |
| 110 | 117 |
| 111 void AskAllow() { | 118 void AskAllow() { |
| 112 ask_allow_count_++; | 119 ask_allow_count_++; |
| 113 } | 120 } |
| 114 | 121 |
| 122 void TearDown() override { |
| 123 content_settings_->ShutdownOnUIThread(); |
| 124 content_settings_ = NULL; |
| 125 #if defined(OS_ANDROID) |
| 126 UnsetDelegate(); |
| 127 #endif |
| 128 ChromeRenderViewHostTestHarness::TearDown(); |
| 129 } |
| 130 |
| 131 #if defined(OS_ANDROID) |
| 115 void FakeCreate( | 132 void FakeCreate( |
| 116 InfoBarService* infobar_service, | 133 InfoBarService* infobar_service, |
| 117 base::WeakPtr<DownloadRequestLimiter::TabDownloadState> host) { | 134 base::WeakPtr<DownloadRequestLimiter::TabDownloadState> host) { |
| 118 AskAllow(); | 135 AskAllow(); |
| 119 switch (testing_action_) { | 136 switch (testing_action_) { |
| 120 case ACCEPT: | 137 case ACCEPT: |
| 121 host->Accept(); | 138 host->Accept(); |
| 122 break; | 139 break; |
| 123 case CANCEL: | 140 case CANCEL: |
| 124 host->Cancel(); | 141 host->Cancel(); |
| 125 break; | 142 break; |
| 126 case WAIT: | 143 case WAIT: |
| 127 break; | 144 break; |
| 128 } | 145 } |
| 129 } | 146 } |
| 130 | 147 |
| 131 void TearDown() override { | |
| 132 content_settings_->ShutdownOnUIThread(); | |
| 133 content_settings_ = NULL; | |
| 134 UnsetDelegate(); | |
| 135 ChromeRenderViewHostTestHarness::TearDown(); | |
| 136 } | |
| 137 | |
| 138 virtual void UnsetDelegate() { | 148 virtual void UnsetDelegate() { |
| 139 DownloadRequestInfoBarDelegate::SetCallbackForTesting(NULL); | 149 DownloadRequestInfoBarDelegate::SetCallbackForTesting(NULL); |
| 140 } | 150 } |
| 151 #endif |
| 141 | 152 |
| 142 void CanDownload() { | 153 void CanDownload() { |
| 143 CanDownloadFor(web_contents()); | 154 CanDownloadFor(web_contents()); |
| 144 } | 155 } |
| 145 | 156 |
| 146 void CanDownloadFor(WebContents* web_contents) { | 157 void CanDownloadFor(WebContents* web_contents) { |
| 147 download_request_limiter_->CanDownloadImpl( | 158 download_request_limiter_->CanDownloadImpl( |
| 148 web_contents, | 159 web_contents, |
| 149 "GET", // request method | 160 "GET", // request method |
| 150 base::Bind(&DownloadRequestLimiterTest::ContinueDownload, | 161 base::Bind(&DownloadRequestLimiterTest::ContinueDownload, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 | 220 |
| 210 // Number of times CancelDownload was invoked. | 221 // Number of times CancelDownload was invoked. |
| 211 int cancel_count_; | 222 int cancel_count_; |
| 212 | 223 |
| 213 // Number of times ShouldAllowDownload was invoked. | 224 // Number of times ShouldAllowDownload was invoked. |
| 214 int ask_allow_count_; | 225 int ask_allow_count_; |
| 215 | 226 |
| 216 scoped_refptr<HostContentSettingsMap> content_settings_; | 227 scoped_refptr<HostContentSettingsMap> content_settings_; |
| 217 | 228 |
| 218 private: | 229 private: |
| 230 #if defined(OS_ANDROID) |
| 219 DownloadRequestInfoBarDelegate::FakeCreateCallback fake_create_callback_; | 231 DownloadRequestInfoBarDelegate::FakeCreateCallback fake_create_callback_; |
| 232 #endif |
| 233 |
| 220 scoped_ptr<TestingProfile> profile_; | 234 scoped_ptr<TestingProfile> profile_; |
| 221 }; | 235 }; |
| 222 | 236 |
| 223 void FakePermissionBubbleView::Show( | 237 void FakePermissionBubbleView::Show( |
| 224 const std::vector<PermissionBubbleRequest*>& requests, | 238 const std::vector<PermissionBubbleRequest*>& requests, |
| 225 const std::vector<bool>& accept_state) { | 239 const std::vector<bool>& accept_state) { |
| 226 test_->AskAllow(); | 240 test_->AskAllow(); |
| 227 int action = test_->GetAction(); | 241 int action = test_->GetAction(); |
| 228 if (action == DownloadRequestLimiterTest::ACCEPT) { | 242 if (action == DownloadRequestLimiterTest::ACCEPT) { |
| 229 delegate_->Accept(); | 243 delegate_->Accept(); |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 BubbleManagerDocumentLoadCompleted(); | 424 BubbleManagerDocumentLoadCompleted(); |
| 411 base::RunLoop().RunUntilIdle(); | 425 base::RunLoop().RunUntilIdle(); |
| 412 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 426 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 413 download_request_limiter_->GetDownloadStatus(web_contents())); | 427 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 414 CanDownload(); | 428 CanDownload(); |
| 415 ExpectAndResetCounts(0, 1, 0, __LINE__); | 429 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 416 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 430 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 417 download_request_limiter_->GetDownloadStatus(web_contents())); | 431 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 418 } | 432 } |
| 419 | 433 |
| 434 #if defined(OS_ANDROID) |
| 420 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_RawWebContents) { | 435 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_RawWebContents) { |
| 421 scoped_ptr<WebContents> web_contents(CreateTestWebContents()); | 436 scoped_ptr<WebContents> web_contents(CreateTestWebContents()); |
| 422 | 437 |
| 423 // DownloadRequestLimiter won't try to make a permission bubble if there's | 438 // DownloadRequestLimiter won't try to make a permission bubble if there's |
| 424 // no permission bubble manager, so don't put one on the test WebContents. | 439 // no permission bubble manager, so don't put one on the test WebContents. |
| 425 | 440 |
| 426 // DownloadRequestLimiter won't try to make an infobar if it doesn't have an | 441 // DownloadRequestLimiter won't try to make an infobar if it doesn't have an |
| 427 // InfoBarService, and we want to test that it will Cancel() instead of | 442 // InfoBarService, and we want to test that it will Cancel() instead of |
| 428 // prompting when it doesn't have a InfoBarService, so unset the delegate. | 443 // prompting when it doesn't have a InfoBarService, so unset the delegate. |
| 429 UnsetDelegate(); | 444 UnsetDelegate(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 447 EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 462 EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 448 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 463 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 449 OnUserGestureFor(web_contents.get()); | 464 OnUserGestureFor(web_contents.get()); |
| 450 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 465 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 451 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 466 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 452 CanDownloadFor(web_contents.get()); | 467 CanDownloadFor(web_contents.get()); |
| 453 ExpectAndResetCounts(1, 0, 0, __LINE__); | 468 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 454 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 469 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 455 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 470 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 456 } | 471 } |
| 472 #endif |
| 457 | 473 |
| 458 TEST_F(DownloadRequestLimiterTest, | 474 TEST_F(DownloadRequestLimiterTest, |
| 459 DownloadRequestLimiter_SetHostContentSetting) { | 475 DownloadRequestLimiter_SetHostContentSetting) { |
| 460 NavigateAndCommit(GURL("http://foo.com/bar")); | 476 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 461 BubbleManagerDocumentLoadCompleted(); | 477 BubbleManagerDocumentLoadCompleted(); |
| 462 SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW); | 478 SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW); |
| 463 | 479 |
| 464 CanDownload(); | 480 CanDownload(); |
| 465 ExpectAndResetCounts(1, 0, 0, __LINE__); | 481 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 466 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 482 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 467 download_request_limiter_->GetDownloadStatus(web_contents())); | 483 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 468 | 484 |
| 469 CanDownload(); | 485 CanDownload(); |
| 470 ExpectAndResetCounts(1, 0, 0, __LINE__); | 486 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 471 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 487 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 472 download_request_limiter_->GetDownloadStatus(web_contents())); | 488 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 473 | 489 |
| 474 SetHostContentSetting(web_contents(), CONTENT_SETTING_BLOCK); | 490 SetHostContentSetting(web_contents(), CONTENT_SETTING_BLOCK); |
| 475 | 491 |
| 476 CanDownload(); | 492 CanDownload(); |
| 477 ExpectAndResetCounts(0, 1, 0, __LINE__); | 493 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 478 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 494 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 479 download_request_limiter_->GetDownloadStatus(web_contents())); | 495 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 480 | 496 |
| 481 CanDownload(); | 497 CanDownload(); |
| 482 ExpectAndResetCounts(0, 1, 0, __LINE__); | 498 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 483 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 499 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 484 download_request_limiter_->GetDownloadStatus(web_contents())); | 500 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 485 } | 501 } |
| OLD | NEW |