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/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
14 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
15 #include "components/content_settings/core/browser/host_content_settings_map.h" | 15 #include "components/content_settings/core/browser/host_content_settings_map.h" |
16 #include "content/public/browser/navigation_controller.h" | 16 #include "content/public/browser/navigation_controller.h" |
17 #include "content/public/browser/navigation_details.h" | 17 #include "content/public/browser/navigation_details.h" |
18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
19 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
21 #include "content/public/common/frame_navigate_params.h" | 21 #include "content/public/common/frame_navigate_params.h" |
| 22 #include "content/public/test/web_contents_tester.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 #if defined(OS_ANDROID) | 25 #if defined(OS_ANDROID) |
25 #include "chrome/browser/download/download_request_infobar_delegate_android.h" | 26 #include "chrome/browser/download/download_request_infobar_delegate_android.h" |
26 #include "chrome/browser/infobars/infobar_service.h" | 27 #include "chrome/browser/infobars/infobar_service.h" |
27 #else | 28 #else |
28 #include "chrome/browser/download/download_permission_request.h" | 29 #include "chrome/browser/download/download_permission_request.h" |
29 #include "chrome/browser/ui/website_settings/mock_permission_bubble_factory.h" | 30 #include "chrome/browser/ui/website_settings/mock_permission_bubble_factory.h" |
30 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 31 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
31 #endif | 32 #endif |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 } | 231 } |
231 | 232 |
232 void LoadCompleted() { testing_delegate_.LoadCompleted(web_contents()); } | 233 void LoadCompleted() { testing_delegate_.LoadCompleted(web_contents()); } |
233 | 234 |
234 int AskAllowCount() { return testing_delegate_.AllowCount(); } | 235 int AskAllowCount() { return testing_delegate_.AllowCount(); } |
235 | 236 |
236 void UpdateExpectations(TestingAction action) { | 237 void UpdateExpectations(TestingAction action) { |
237 testing_delegate_.UpdateExpectations(action); | 238 testing_delegate_.UpdateExpectations(action); |
238 } | 239 } |
239 | 240 |
| 241 void NavigateAndCommitWithParams( |
| 242 content::NavigationController::LoadURLParams& params) { |
| 243 controller().LoadURLWithParams(params); |
| 244 content::WebContentsTester::For(web_contents())->CommitPendingNavigation(); |
| 245 } |
| 246 |
240 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; | 247 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; |
241 | 248 |
242 // Number of times ContinueDownload was invoked. | 249 // Number of times ContinueDownload was invoked. |
243 int continue_count_; | 250 int continue_count_; |
244 | 251 |
245 // Number of times CancelDownload was invoked. | 252 // Number of times CancelDownload was invoked. |
246 int cancel_count_; | 253 int cancel_count_; |
247 | 254 |
248 scoped_refptr<HostContentSettingsMap> content_settings_; | 255 scoped_refptr<HostContentSettingsMap> content_settings_; |
249 TestingDelegate testing_delegate_; | 256 TestingDelegate testing_delegate_; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 OnUserInteraction(blink::WebInputEvent::RawKeyDown); | 322 OnUserInteraction(blink::WebInputEvent::RawKeyDown); |
316 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 323 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
317 download_request_limiter_->GetDownloadStatus(web_contents())); | 324 download_request_limiter_->GetDownloadStatus(web_contents())); |
318 | 325 |
319 // Navigate to a completely different host, which should reset the state. | 326 // Navigate to a completely different host, which should reset the state. |
320 NavigateAndCommit(GURL("http://fooey.com")); | 327 NavigateAndCommit(GURL("http://fooey.com")); |
321 LoadCompleted(); | 328 LoadCompleted(); |
322 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 329 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
323 download_request_limiter_->GetDownloadStatus(web_contents())); | 330 download_request_limiter_->GetDownloadStatus(web_contents())); |
324 | 331 |
325 // Do two downloads, allowing the second so that we end up with allow all. | 332 // Do two downloads, blocking the second so that we end up with downloads not |
| 333 // allowed. |
326 CanDownload(); | 334 CanDownload(); |
327 ExpectAndResetCounts(1, 0, 0, __LINE__); | 335 ExpectAndResetCounts(1, 0, 0, __LINE__); |
328 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 336 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
329 download_request_limiter_->GetDownloadStatus(web_contents())); | 337 download_request_limiter_->GetDownloadStatus(web_contents())); |
330 | 338 |
331 UpdateExpectations(CANCEL); | 339 UpdateExpectations(CANCEL); |
332 CanDownload(); | 340 CanDownload(); |
333 ExpectAndResetCounts(0, 1, 1, __LINE__); | 341 ExpectAndResetCounts(0, 1, 1, __LINE__); |
334 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 342 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
335 download_request_limiter_->GetDownloadStatus(web_contents())); | 343 download_request_limiter_->GetDownloadStatus(web_contents())); |
336 | 344 |
337 // Navigate to a new URL with the same host, which shouldn't reset the allow | 345 // Navigate to a new URL with the same host, which shouldn't reset the allow |
338 // all state. | 346 // all state. |
339 NavigateAndCommit(GURL("http://fooey.com/bar2")); | 347 NavigateAndCommit(GURL("http://fooey.com/bar2")); |
340 LoadCompleted(); | 348 LoadCompleted(); |
341 CanDownload(); | 349 CanDownload(); |
342 ExpectAndResetCounts(0, 1, 0, __LINE__); | 350 ExpectAndResetCounts(0, 1, 0, __LINE__); |
343 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 351 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
344 download_request_limiter_->GetDownloadStatus(web_contents())); | 352 download_request_limiter_->GetDownloadStatus(web_contents())); |
345 } | 353 } |
346 | 354 |
| 355 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_RendererInitiated) { |
| 356 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 357 LoadCompleted(); |
| 358 |
| 359 // Do one download so we end up in PROMPT. |
| 360 CanDownload(); |
| 361 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 362 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 363 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 364 |
| 365 // Set up a renderer-initiated navigation to the same host. |
| 366 content::NavigationController::LoadURLParams load_params( |
| 367 GURL("http://foo.com/bar2")); |
| 368 load_params.is_renderer_initiated = true; |
| 369 load_params.transition_type = ui::PAGE_TRANSITION_GENERATED; |
| 370 load_params.referrer = content::Referrer(); |
| 371 NavigateAndCommitWithParams(load_params); |
| 372 LoadCompleted(); |
| 373 |
| 374 // The state should not be reset. |
| 375 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 376 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 377 |
| 378 // Renderer-initiated nav to a different host shouldn't reset the state. |
| 379 load_params.url = GURL("http://fooey.com/bar"); |
| 380 NavigateAndCommitWithParams(load_params); |
| 381 LoadCompleted(); |
| 382 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 383 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 384 |
| 385 // Set up a blocked state. |
| 386 UpdateExpectations(CANCEL); |
| 387 CanDownload(); |
| 388 ExpectAndResetCounts(0, 1, 1, __LINE__); |
| 389 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 390 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 391 |
| 392 // The state should not be reset on a renderer-initiated load to either the |
| 393 // same host or a different host. |
| 394 load_params.url = GURL("http://fooeybar.com/bar"); |
| 395 NavigateAndCommitWithParams(load_params); |
| 396 LoadCompleted(); |
| 397 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 398 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 399 |
| 400 load_params.url = GURL("http://foo.com/bar"); |
| 401 NavigateAndCommitWithParams(load_params); |
| 402 LoadCompleted(); |
| 403 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 404 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 405 |
| 406 // Browser-initiated navigation to a different host, which should reset the |
| 407 // state. |
| 408 NavigateAndCommit(GURL("http://foobar.com")); |
| 409 LoadCompleted(); |
| 410 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 411 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 412 |
| 413 // Set up an allow all state. |
| 414 CanDownload(); |
| 415 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 416 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 417 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 418 |
| 419 UpdateExpectations(ACCEPT); |
| 420 CanDownload(); |
| 421 ExpectAndResetCounts(1, 0, 1, __LINE__); |
| 422 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 423 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 424 |
| 425 // The state should not be reset on a pending renderer-initiated load to |
| 426 // the same host. |
| 427 load_params.url = GURL("http://foobar.com/bar"); |
| 428 NavigateAndCommitWithParams(load_params); |
| 429 LoadCompleted(); |
| 430 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 431 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 432 |
| 433 // But a pending load to a different host should reset the state. |
| 434 load_params.url = GURL("http://foo.com"); |
| 435 NavigateAndCommitWithParams(load_params); |
| 436 LoadCompleted(); |
| 437 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 438 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 439 } |
| 440 |
347 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_ResetOnUserGesture) { | 441 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_ResetOnUserGesture) { |
348 NavigateAndCommit(GURL("http://foo.com/bar")); | 442 NavigateAndCommit(GURL("http://foo.com/bar")); |
349 LoadCompleted(); | 443 LoadCompleted(); |
350 | 444 |
351 // Do one download, which should change to prompt before download. | 445 // Do one download, which should change to prompt before download. |
352 CanDownload(); | 446 CanDownload(); |
353 ExpectAndResetCounts(1, 0, 0, __LINE__); | 447 ExpectAndResetCounts(1, 0, 0, __LINE__); |
354 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 448 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
355 download_request_limiter_->GetDownloadStatus(web_contents())); | 449 download_request_limiter_->GetDownloadStatus(web_contents())); |
356 | 450 |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 // changing the content setting). | 676 // changing the content setting). |
583 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); | 677 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); |
584 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 678 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
585 download_request_limiter_->GetDownloadStatus(web_contents())); | 679 download_request_limiter_->GetDownloadStatus(web_contents())); |
586 UpdateExpectations(WAIT); | 680 UpdateExpectations(WAIT); |
587 CanDownload(); | 681 CanDownload(); |
588 ExpectAndResetCounts(0, 0, 1, __LINE__); | 682 ExpectAndResetCounts(0, 0, 1, __LINE__); |
589 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 683 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
590 download_request_limiter_->GetDownloadStatus(web_contents())); | 684 download_request_limiter_->GetDownloadStatus(web_contents())); |
591 } | 685 } |
OLD | NEW |