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 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 int expect_cancels, | 191 int expect_cancels, |
189 int expect_asks, | 192 int expect_asks, |
190 int line) { | 193 int line) { |
191 EXPECT_EQ(expect_continues, continue_count_) << "line " << line; | 194 EXPECT_EQ(expect_continues, continue_count_) << "line " << line; |
192 EXPECT_EQ(expect_cancels, cancel_count_) << "line " << line; | 195 EXPECT_EQ(expect_cancels, cancel_count_) << "line " << line; |
193 EXPECT_EQ(expect_asks, AskAllowCount()) << "line " << line; | 196 EXPECT_EQ(expect_asks, AskAllowCount()) << "line " << line; |
194 continue_count_ = cancel_count_ = 0; | 197 continue_count_ = cancel_count_ = 0; |
195 testing_delegate_.ResetCounts(); | 198 testing_delegate_.ResetCounts(); |
196 } | 199 } |
197 | 200 |
201 void UpdateContentSettings(WebContents* web_contents, | |
202 ContentSetting setting) { | |
203 // Ensure a download state exists. | |
204 download_request_limiter_->GetDownloadState(web_contents, NULL, true); | |
asanka
2016/02/08 15:02:14
NULL -> nullptr
dominickn
2016/02/11 01:25:53
Done. Also updated other uses in this file.
| |
205 SetHostContentSetting(web_contents, setting); | |
206 | |
207 content::NotificationService::current()->Notify( | |
208 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED, | |
209 content::Source<WebContents>(web_contents), | |
210 content::NotificationService::NoDetails()); | |
211 } | |
212 | |
198 protected: | 213 protected: |
199 void ContinueDownload(bool allow) { | 214 void ContinueDownload(bool allow) { |
200 if (allow) { | 215 if (allow) { |
201 continue_count_++; | 216 continue_count_++; |
202 } else { | 217 } else { |
203 cancel_count_++; | 218 cancel_count_++; |
204 } | 219 } |
205 } | 220 } |
206 | 221 |
207 void SetHostContentSetting(WebContents* contents, ContentSetting setting) { | 222 void SetHostContentSetting(WebContents* contents, ContentSetting setting) { |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
478 CanDownload(); | 493 CanDownload(); |
479 ExpectAndResetCounts(0, 1, 0, __LINE__); | 494 ExpectAndResetCounts(0, 1, 0, __LINE__); |
480 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 495 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
481 download_request_limiter_->GetDownloadStatus(web_contents())); | 496 download_request_limiter_->GetDownloadStatus(web_contents())); |
482 | 497 |
483 CanDownload(); | 498 CanDownload(); |
484 ExpectAndResetCounts(0, 1, 0, __LINE__); | 499 ExpectAndResetCounts(0, 1, 0, __LINE__); |
485 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 500 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
486 download_request_limiter_->GetDownloadStatus(web_contents())); | 501 download_request_limiter_->GetDownloadStatus(web_contents())); |
487 } | 502 } |
503 | |
504 TEST_F(DownloadRequestLimiterTest, | |
505 DownloadRequestLimiter_ContentSettingChanged) { | |
asanka
2016/02/08 15:02:14
The current behavior doesn't reset the download co
dominickn
2016/02/11 01:25:53
Not sure I understood this 100%, but I added a bun
| |
506 NavigateAndCommit(GURL("http://foo.com/bar")); | |
507 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | |
508 download_request_limiter_->GetDownloadStatus(web_contents())); | |
509 | |
510 // Set the content setting to allow and send the notification. Ensure that the | |
511 // limiter states update to match. | |
512 UpdateContentSettings(web_contents(), CONTENT_SETTING_ALLOW); | |
513 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | |
514 download_request_limiter_->GetDownloadStatus(web_contents())); | |
515 | |
516 // Ask to download and assert we are still in allow. | |
517 CanDownload(); | |
518 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | |
519 download_request_limiter_->GetDownloadStatus(web_contents())); | |
520 | |
521 // Set the content setting to block and send the notification. Ensure that the | |
522 // limiter states updates to match. | |
523 UpdateContentSettings(web_contents(), CONTENT_SETTING_BLOCK); | |
524 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | |
525 download_request_limiter_->GetDownloadStatus(web_contents())); | |
526 | |
527 // Check that block to allow works as expected. | |
528 UpdateContentSettings(web_contents(), CONTENT_SETTING_ALLOW); | |
529 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | |
530 download_request_limiter_->GetDownloadStatus(web_contents())); | |
531 | |
532 // Reset to ask. | |
533 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); | |
534 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | |
535 download_request_limiter_->GetDownloadStatus(web_contents())); | |
536 } | |
OLD | NEW |