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/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
13 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
14 #include "components/content_settings/core/browser/host_content_settings_map.h" | 15 #include "components/content_settings/core/browser/host_content_settings_map.h" |
15 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
16 #include "content/public/browser/navigation_details.h" | 17 #include "content/public/browser/navigation_details.h" |
| 18 #include "content/public/browser/notification_service.h" |
| 19 #include "content/public/browser/notification_source.h" |
17 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
18 #include "content/public/common/frame_navigate_params.h" | 21 #include "content/public/common/frame_navigate_params.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
20 | 23 |
21 #if defined(OS_ANDROID) | 24 #if defined(OS_ANDROID) |
22 #include "chrome/browser/download/download_request_infobar_delegate_android.h" | 25 #include "chrome/browser/download/download_request_infobar_delegate_android.h" |
23 #include "chrome/browser/infobars/infobar_service.h" | 26 #include "chrome/browser/infobars/infobar_service.h" |
24 #else | 27 #else |
25 #include "chrome/browser/download/download_permission_request.h" | 28 #include "chrome/browser/download/download_permission_request.h" |
26 #include "chrome/browser/ui/website_settings/mock_permission_bubble_factory.h" | 29 #include "chrome/browser/ui/website_settings/mock_permission_bubble_factory.h" |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 | 150 |
148 content_settings_ = new HostContentSettingsMap( | 151 content_settings_ = new HostContentSettingsMap( |
149 profile_->GetPrefs(), false /* incognito_profile */, | 152 profile_->GetPrefs(), false /* incognito_profile */, |
150 false /* guest_profile */); | 153 false /* guest_profile */); |
151 DownloadRequestLimiter::SetContentSettingsForTesting( | 154 DownloadRequestLimiter::SetContentSettingsForTesting( |
152 content_settings_.get()); | 155 content_settings_.get()); |
153 } | 156 } |
154 | 157 |
155 void TearDown() override { | 158 void TearDown() override { |
156 content_settings_->ShutdownOnUIThread(); | 159 content_settings_->ShutdownOnUIThread(); |
157 content_settings_ = NULL; | 160 content_settings_ = nullptr; |
158 testing_delegate_.TearDown(); | 161 testing_delegate_.TearDown(); |
159 ChromeRenderViewHostTestHarness::TearDown(); | 162 ChromeRenderViewHostTestHarness::TearDown(); |
160 } | 163 } |
161 | 164 |
162 void CanDownload() { | 165 void CanDownload() { |
163 CanDownloadFor(web_contents()); | 166 CanDownloadFor(web_contents()); |
164 } | 167 } |
165 | 168 |
166 void CanDownloadFor(WebContents* web_contents) { | 169 void CanDownloadFor(WebContents* web_contents) { |
167 download_request_limiter_->CanDownloadImpl( | 170 download_request_limiter_->CanDownloadImpl( |
168 web_contents, | 171 web_contents, |
169 "GET", // request method | 172 "GET", // request method |
170 base::Bind(&DownloadRequestLimiterTest::ContinueDownload, | 173 base::Bind(&DownloadRequestLimiterTest::ContinueDownload, |
171 base::Unretained(this))); | 174 base::Unretained(this))); |
172 base::RunLoop().RunUntilIdle(); | 175 base::RunLoop().RunUntilIdle(); |
173 } | 176 } |
174 | 177 |
175 void OnUserGesture() { | 178 void OnUserGesture() { |
176 OnUserGestureFor(web_contents()); | 179 OnUserGestureFor(web_contents()); |
177 } | 180 } |
178 | 181 |
179 void OnUserGestureFor(WebContents* web_contents) { | 182 void OnUserGestureFor(WebContents* web_contents) { |
180 DownloadRequestLimiter::TabDownloadState* state = | 183 DownloadRequestLimiter::TabDownloadState* state = |
181 download_request_limiter_->GetDownloadState(web_contents, NULL, false); | 184 download_request_limiter_->GetDownloadState(web_contents, nullptr, |
| 185 false); |
182 if (state) | 186 if (state) |
183 state->DidGetUserGesture(); | 187 state->DidGetUserGesture(); |
184 } | 188 } |
185 | 189 |
186 void ExpectAndResetCounts( | 190 void ExpectAndResetCounts( |
187 int expect_continues, | 191 int expect_continues, |
188 int expect_cancels, | 192 int expect_cancels, |
189 int expect_asks, | 193 int expect_asks, |
190 int line) { | 194 int line) { |
191 EXPECT_EQ(expect_continues, continue_count_) << "line " << line; | 195 EXPECT_EQ(expect_continues, continue_count_) << "line " << line; |
192 EXPECT_EQ(expect_cancels, cancel_count_) << "line " << line; | 196 EXPECT_EQ(expect_cancels, cancel_count_) << "line " << line; |
193 EXPECT_EQ(expect_asks, AskAllowCount()) << "line " << line; | 197 EXPECT_EQ(expect_asks, AskAllowCount()) << "line " << line; |
194 continue_count_ = cancel_count_ = 0; | 198 continue_count_ = cancel_count_ = 0; |
195 testing_delegate_.ResetCounts(); | 199 testing_delegate_.ResetCounts(); |
196 } | 200 } |
197 | 201 |
| 202 void UpdateContentSettings(WebContents* web_contents, |
| 203 ContentSetting setting) { |
| 204 // Ensure a download state exists. |
| 205 download_request_limiter_->GetDownloadState(web_contents, nullptr, true); |
| 206 SetHostContentSetting(web_contents, setting); |
| 207 |
| 208 // Manually send the update notification. In the browser, this is sent from |
| 209 // ContentSettingRPHBubbleModel. |
| 210 content::NotificationService::current()->Notify( |
| 211 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED, |
| 212 content::Source<WebContents>(web_contents), |
| 213 content::NotificationService::NoDetails()); |
| 214 } |
| 215 |
198 protected: | 216 protected: |
199 void ContinueDownload(bool allow) { | 217 void ContinueDownload(bool allow) { |
200 if (allow) { | 218 if (allow) { |
201 continue_count_++; | 219 continue_count_++; |
202 } else { | 220 } else { |
203 cancel_count_++; | 221 cancel_count_++; |
204 } | 222 } |
205 } | 223 } |
206 | 224 |
207 void SetHostContentSetting(WebContents* contents, ContentSetting setting) { | 225 void SetHostContentSetting(WebContents* contents, ContentSetting setting) { |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 CanDownload(); | 496 CanDownload(); |
479 ExpectAndResetCounts(0, 1, 0, __LINE__); | 497 ExpectAndResetCounts(0, 1, 0, __LINE__); |
480 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 498 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
481 download_request_limiter_->GetDownloadStatus(web_contents())); | 499 download_request_limiter_->GetDownloadStatus(web_contents())); |
482 | 500 |
483 CanDownload(); | 501 CanDownload(); |
484 ExpectAndResetCounts(0, 1, 0, __LINE__); | 502 ExpectAndResetCounts(0, 1, 0, __LINE__); |
485 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 503 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
486 download_request_limiter_->GetDownloadStatus(web_contents())); | 504 download_request_limiter_->GetDownloadStatus(web_contents())); |
487 } | 505 } |
| 506 |
| 507 TEST_F(DownloadRequestLimiterTest, |
| 508 DownloadRequestLimiter_ContentSettingChanged) { |
| 509 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 510 LoadCompleted(); |
| 511 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 512 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 513 |
| 514 CanDownload(); |
| 515 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 516 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 517 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 518 |
| 519 // Simulate an accidental deny. |
| 520 UpdateExpectations(CANCEL); |
| 521 CanDownload(); |
| 522 ExpectAndResetCounts(0, 1, 1, __LINE__); |
| 523 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 524 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 525 |
| 526 // Set the content setting to allow and send the notification. Ensure that the |
| 527 // limiter states update to match. |
| 528 UpdateContentSettings(web_contents(), CONTENT_SETTING_ALLOW); |
| 529 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 530 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 531 |
| 532 // Ask to download, and assert that it succeeded and we are still in allow. |
| 533 CanDownload(); |
| 534 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 535 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 536 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 537 |
| 538 // Set the content setting to block and send the notification. Ensure that the |
| 539 // limiter states updates to match. |
| 540 UpdateContentSettings(web_contents(), CONTENT_SETTING_BLOCK); |
| 541 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 542 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 543 |
| 544 // Ensure downloads are blocked. |
| 545 CanDownload(); |
| 546 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 547 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 548 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 549 |
| 550 // Reset to ask. Verify that the download counts have not changed on the |
| 551 // content settings change (ensuring there is no "free" download after |
| 552 // changing the content setting). |
| 553 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); |
| 554 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 555 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 556 UpdateExpectations(WAIT); |
| 557 CanDownload(); |
| 558 ExpectAndResetCounts(0, 0, 1, __LINE__); |
| 559 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 560 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 561 } |
OLD | NEW |