| 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" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 14 #include "chrome/test/base/testing_profile.h" | |
| 15 #include "components/content_settings/core/browser/host_content_settings_map.h" | 16 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 16 #include "content/public/browser/navigation_controller.h" | 17 #include "content/public/browser/navigation_controller.h" |
| 17 #include "content/public/browser/navigation_details.h" | 18 #include "content/public/browser/navigation_details.h" |
| 18 #include "content/public/browser/notification_service.h" | 19 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
| 20 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/common/frame_navigate_params.h" | 22 #include "content/public/common/frame_navigate_params.h" |
| 22 #include "content/public/test/web_contents_tester.h" | 23 #include "content/public/test/web_contents_tester.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 private: | 137 private: |
| 137 std::unique_ptr<MockPermissionPromptFactory> mock_permission_prompt_factory_; | 138 std::unique_ptr<MockPermissionPromptFactory> mock_permission_prompt_factory_; |
| 138 }; | 139 }; |
| 139 #endif | 140 #endif |
| 140 } // namespace | 141 } // namespace |
| 141 | 142 |
| 142 class DownloadRequestLimiterTest : public ChromeRenderViewHostTestHarness { | 143 class DownloadRequestLimiterTest : public ChromeRenderViewHostTestHarness { |
| 143 public: | 144 public: |
| 144 void SetUp() override { | 145 void SetUp() override { |
| 145 ChromeRenderViewHostTestHarness::SetUp(); | 146 ChromeRenderViewHostTestHarness::SetUp(); |
| 146 profile_.reset(new TestingProfile()); | |
| 147 testing_delegate_.SetUp(web_contents()); | 147 testing_delegate_.SetUp(web_contents()); |
| 148 | 148 |
| 149 UpdateExpectations(ACCEPT); | 149 UpdateExpectations(ACCEPT); |
| 150 cancel_count_ = continue_count_ = 0; | 150 cancel_count_ = continue_count_ = 0; |
| 151 download_request_limiter_ = new DownloadRequestLimiter(); | 151 download_request_limiter_ = new DownloadRequestLimiter(); |
| 152 | |
| 153 content_settings_ = new HostContentSettingsMap( | |
| 154 profile_->GetPrefs(), false /* incognito_profile */, | |
| 155 false /* guest_profile */); | |
| 156 DownloadRequestLimiter::SetContentSettingsForTesting( | |
| 157 content_settings_.get()); | |
| 158 } | 152 } |
| 159 | 153 |
| 160 void TearDown() override { | 154 void TearDown() override { |
| 161 content_settings_->ShutdownOnUIThread(); | |
| 162 content_settings_ = nullptr; | |
| 163 testing_delegate_.TearDown(); | 155 testing_delegate_.TearDown(); |
| 164 | 156 |
| 165 // |profile_| must be teared down before | |
| 166 // |RenderViewHostTestHarness::thread_bundle_|. | |
| 167 profile_.reset(); | |
| 168 | |
| 169 ChromeRenderViewHostTestHarness::TearDown(); | 157 ChromeRenderViewHostTestHarness::TearDown(); |
| 170 } | 158 } |
| 171 | 159 |
| 172 void CanDownload() { | 160 void CanDownload() { |
| 173 CanDownloadFor(web_contents()); | 161 CanDownloadFor(web_contents()); |
| 174 } | 162 } |
| 175 | 163 |
| 176 void CanDownloadFor(WebContents* web_contents) { | 164 void CanDownloadFor(WebContents* web_contents) { |
| 177 download_request_limiter_->CanDownloadImpl( | 165 download_request_limiter_->CanDownloadImpl( |
| 178 web_contents, | 166 web_contents, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 205 EXPECT_EQ(expect_asks, AskAllowCount()) << "line " << line; | 193 EXPECT_EQ(expect_asks, AskAllowCount()) << "line " << line; |
| 206 continue_count_ = cancel_count_ = 0; | 194 continue_count_ = cancel_count_ = 0; |
| 207 testing_delegate_.ResetCounts(); | 195 testing_delegate_.ResetCounts(); |
| 208 } | 196 } |
| 209 | 197 |
| 210 void UpdateContentSettings(WebContents* web_contents, | 198 void UpdateContentSettings(WebContents* web_contents, |
| 211 ContentSetting setting) { | 199 ContentSetting setting) { |
| 212 // Ensure a download state exists. | 200 // Ensure a download state exists. |
| 213 download_request_limiter_->GetDownloadState(web_contents, nullptr, true); | 201 download_request_limiter_->GetDownloadState(web_contents, nullptr, true); |
| 214 SetHostContentSetting(web_contents, setting); | 202 SetHostContentSetting(web_contents, setting); |
| 215 | |
| 216 // Manually send the update notification. In the browser, this is sent from | |
| 217 // ContentSettingRPHBubbleModel. | |
| 218 content::NotificationService::current()->Notify( | |
| 219 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED, | |
| 220 content::Source<WebContents>(web_contents), | |
| 221 content::NotificationService::NoDetails()); | |
| 222 } | 203 } |
| 223 | 204 |
| 224 protected: | 205 protected: |
| 225 void ContinueDownload(bool allow) { | 206 void ContinueDownload(bool allow) { |
| 226 if (allow) { | 207 if (allow) { |
| 227 continue_count_++; | 208 continue_count_++; |
| 228 } else { | 209 } else { |
| 229 cancel_count_++; | 210 cancel_count_++; |
| 230 } | 211 } |
| 231 } | 212 } |
| 232 | 213 |
| 233 void SetHostContentSetting(WebContents* contents, ContentSetting setting) { | 214 void SetHostContentSetting(WebContents* contents, ContentSetting setting) { |
| 234 content_settings_->SetContentSettingDefaultScope( | 215 HostContentSettingsMapFactory::GetForProfile( |
| 235 contents->GetURL(), GURL(), CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, | 216 Profile::FromBrowserContext(contents->GetBrowserContext())) |
| 236 std::string(), setting); | 217 ->SetContentSettingDefaultScope( |
| 218 contents->GetURL(), GURL(), |
| 219 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, std::string(), setting); |
| 237 } | 220 } |
| 238 | 221 |
| 239 void LoadCompleted() { testing_delegate_.LoadCompleted(web_contents()); } | 222 void LoadCompleted() { testing_delegate_.LoadCompleted(web_contents()); } |
| 240 | 223 |
| 241 int AskAllowCount() { return testing_delegate_.AllowCount(); } | 224 int AskAllowCount() { return testing_delegate_.AllowCount(); } |
| 242 | 225 |
| 243 void UpdateExpectations(TestingAction action) { | 226 void UpdateExpectations(TestingAction action) { |
| 244 testing_delegate_.UpdateExpectations(action); | 227 testing_delegate_.UpdateExpectations(action); |
| 245 } | 228 } |
| 246 | 229 |
| 247 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; | 230 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; |
| 248 | 231 |
| 249 // Number of times ContinueDownload was invoked. | 232 // Number of times ContinueDownload was invoked. |
| 250 int continue_count_; | 233 int continue_count_; |
| 251 | 234 |
| 252 // Number of times CancelDownload was invoked. | 235 // Number of times CancelDownload was invoked. |
| 253 int cancel_count_; | 236 int cancel_count_; |
| 254 | 237 |
| 255 scoped_refptr<HostContentSettingsMap> content_settings_; | |
| 256 TestingDelegate testing_delegate_; | 238 TestingDelegate testing_delegate_; |
| 257 | |
| 258 private: | |
| 259 std::unique_ptr<TestingProfile> profile_; | |
| 260 }; | 239 }; |
| 261 | 240 |
| 262 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_Allow) { | 241 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_Allow) { |
| 263 NavigateAndCommit(GURL("http://foo.com/bar")); | 242 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 264 LoadCompleted(); | 243 LoadCompleted(); |
| 265 | 244 |
| 266 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. | 245 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. |
| 267 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 246 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 268 download_request_limiter_->GetDownloadStatus(web_contents())); | 247 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 269 | 248 |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 643 LoadCompleted(); | 622 LoadCompleted(); |
| 644 SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW); | 623 SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW); |
| 645 | 624 |
| 646 CanDownload(); | 625 CanDownload(); |
| 647 ExpectAndResetCounts(1, 0, 0, __LINE__); | 626 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 648 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 627 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 649 download_request_limiter_->GetDownloadStatus(web_contents())); | 628 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 650 | 629 |
| 651 CanDownload(); | 630 CanDownload(); |
| 652 ExpectAndResetCounts(1, 0, 0, __LINE__); | 631 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 653 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 632 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 654 download_request_limiter_->GetDownloadStatus(web_contents())); | 633 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 655 | 634 |
| 656 SetHostContentSetting(web_contents(), CONTENT_SETTING_BLOCK); | 635 SetHostContentSetting(web_contents(), CONTENT_SETTING_BLOCK); |
| 657 | 636 |
| 658 CanDownload(); | 637 CanDownload(); |
| 659 ExpectAndResetCounts(0, 1, 0, __LINE__); | 638 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 660 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 639 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 661 download_request_limiter_->GetDownloadStatus(web_contents())); | 640 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 662 | 641 |
| 663 CanDownload(); | 642 CanDownload(); |
| 664 ExpectAndResetCounts(0, 1, 0, __LINE__); | 643 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 665 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 644 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 666 download_request_limiter_->GetDownloadStatus(web_contents())); | 645 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 667 } | 646 } |
| 668 | 647 |
| 669 TEST_F(DownloadRequestLimiterTest, | 648 TEST_F(DownloadRequestLimiterTest, |
| 670 DownloadRequestLimiter_ContentSettingChanged) { | 649 DownloadRequestLimiter_ContentSettingChanged) { |
| 671 NavigateAndCommit(GURL("http://foo.com/bar")); | 650 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 672 LoadCompleted(); | 651 LoadCompleted(); |
| 673 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 652 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 674 download_request_limiter_->GetDownloadStatus(web_contents())); | 653 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 675 | 654 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 714 // changing the content setting). | 693 // changing the content setting). |
| 715 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); | 694 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); |
| 716 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 695 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 717 download_request_limiter_->GetDownloadStatus(web_contents())); | 696 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 718 UpdateExpectations(WAIT); | 697 UpdateExpectations(WAIT); |
| 719 CanDownload(); | 698 CanDownload(); |
| 720 ExpectAndResetCounts(0, 0, 1, __LINE__); | 699 ExpectAndResetCounts(0, 0, 1, __LINE__); |
| 721 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 700 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 722 download_request_limiter_->GetDownloadStatus(web_contents())); | 701 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 723 } | 702 } |
| OLD | NEW |