| 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" |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 168 |
| 169 void CanDownloadFor(WebContents* web_contents) { | 169 void CanDownloadFor(WebContents* web_contents) { |
| 170 download_request_limiter_->CanDownloadImpl( | 170 download_request_limiter_->CanDownloadImpl( |
| 171 web_contents, | 171 web_contents, |
| 172 "GET", // request method | 172 "GET", // request method |
| 173 base::Bind(&DownloadRequestLimiterTest::ContinueDownload, | 173 base::Bind(&DownloadRequestLimiterTest::ContinueDownload, |
| 174 base::Unretained(this))); | 174 base::Unretained(this))); |
| 175 base::RunLoop().RunUntilIdle(); | 175 base::RunLoop().RunUntilIdle(); |
| 176 } | 176 } |
| 177 | 177 |
| 178 void OnUserGesture() { | 178 void OnUserInteraction(blink::WebInputEvent::Type type) { |
| 179 OnUserGestureFor(web_contents()); | 179 OnUserInteractionFor(web_contents(), type); |
| 180 } | 180 } |
| 181 | 181 |
| 182 void OnUserGestureFor(WebContents* web_contents) { | 182 void OnUserInteractionFor(WebContents* web_contents, |
| 183 blink::WebInputEvent::Type type) { |
| 183 DownloadRequestLimiter::TabDownloadState* state = | 184 DownloadRequestLimiter::TabDownloadState* state = |
| 184 download_request_limiter_->GetDownloadState(web_contents, nullptr, | 185 download_request_limiter_->GetDownloadState(web_contents, nullptr, |
| 185 false); | 186 false); |
| 186 if (state) | 187 if (state) |
| 187 state->DidGetUserGesture(); | 188 state->DidGetUserInteraction(type); |
| 188 } | 189 } |
| 189 | 190 |
| 190 void ExpectAndResetCounts( | 191 void ExpectAndResetCounts( |
| 191 int expect_continues, | 192 int expect_continues, |
| 192 int expect_cancels, | 193 int expect_cancels, |
| 193 int expect_asks, | 194 int expect_asks, |
| 194 int line) { | 195 int line) { |
| 195 EXPECT_EQ(expect_continues, continue_count_) << "line " << line; | 196 EXPECT_EQ(expect_continues, continue_count_) << "line " << line; |
| 196 EXPECT_EQ(expect_cancels, cancel_count_) << "line " << line; | 197 EXPECT_EQ(expect_cancels, cancel_count_) << "line " << line; |
| 197 EXPECT_EQ(expect_asks, AskAllowCount()) << "line " << line; | 198 EXPECT_EQ(expect_asks, AskAllowCount()) << "line " << line; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 // all state. | 307 // all state. |
| 307 NavigateAndCommit(GURL("http://foo.com/bar2")); | 308 NavigateAndCommit(GURL("http://foo.com/bar2")); |
| 308 LoadCompleted(); | 309 LoadCompleted(); |
| 309 CanDownload(); | 310 CanDownload(); |
| 310 ExpectAndResetCounts(1, 0, 0, __LINE__); | 311 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 311 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 312 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 312 download_request_limiter_->GetDownloadStatus(web_contents())); | 313 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 313 | 314 |
| 314 // Do a user gesture, because we're at allow all, this shouldn't change the | 315 // Do a user gesture, because we're at allow all, this shouldn't change the |
| 315 // state. | 316 // state. |
| 316 OnUserGesture(); | 317 OnUserInteraction(blink::WebInputEvent::RawKeyDown); |
| 317 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 318 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 318 download_request_limiter_->GetDownloadStatus(web_contents())); | 319 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 319 | 320 |
| 320 // Navigate to a completely different host, which should reset the state. | 321 // Navigate to a completely different host, which should reset the state. |
| 321 NavigateAndCommit(GURL("http://fooey.com")); | 322 NavigateAndCommit(GURL("http://fooey.com")); |
| 322 LoadCompleted(); | 323 LoadCompleted(); |
| 323 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 324 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 324 download_request_limiter_->GetDownloadStatus(web_contents())); | 325 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 325 | 326 |
| 326 // Do two downloads, allowing the second so that we end up with allow all. | 327 // Do two downloads, allowing the second so that we end up with allow all. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 348 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_ResetOnUserGesture) { | 349 TEST_F(DownloadRequestLimiterTest, DownloadRequestLimiter_ResetOnUserGesture) { |
| 349 NavigateAndCommit(GURL("http://foo.com/bar")); | 350 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 350 LoadCompleted(); | 351 LoadCompleted(); |
| 351 | 352 |
| 352 // Do one download, which should change to prompt before download. | 353 // Do one download, which should change to prompt before download. |
| 353 CanDownload(); | 354 CanDownload(); |
| 354 ExpectAndResetCounts(1, 0, 0, __LINE__); | 355 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 355 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 356 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 356 download_request_limiter_->GetDownloadStatus(web_contents())); | 357 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 357 | 358 |
| 358 // Do a user gesture, which should reset back to allow one. | 359 // Do a user gesture with mouse scroll, which should be ignored. |
| 359 OnUserGesture(); | 360 OnUserInteraction(blink::WebInputEvent::MouseWheel); |
| 361 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 362 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 363 // Do a user gesture with mouse click, which should reset back to allow one. |
| 364 OnUserInteraction(blink::WebInputEvent::MouseDown); |
| 360 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 365 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 361 download_request_limiter_->GetDownloadStatus(web_contents())); | 366 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 362 | 367 |
| 368 // Do one download, which should change to prompt before download. |
| 369 CanDownload(); |
| 370 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 371 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 372 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 373 |
| 374 // Do a user gesture with gesture tap, which should reset back to allow one. |
| 375 OnUserInteraction(blink::WebInputEvent::GestureTapDown); |
| 376 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 377 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 378 |
| 379 // Do one download, which should change to prompt before download. |
| 380 CanDownload(); |
| 381 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 382 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 383 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 384 |
| 385 // Do a user gesture with keyboard down, which should reset back to allow one. |
| 386 OnUserInteraction(blink::WebInputEvent::RawKeyDown); |
| 387 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 388 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 389 |
| 363 // Ask twice, which triggers calling the delegate. Don't allow the download | 390 // Ask twice, which triggers calling the delegate. Don't allow the download |
| 364 // so that we end up with not allowed. | 391 // so that we end up with not allowed. |
| 365 CanDownload(); | 392 CanDownload(); |
| 366 ExpectAndResetCounts(1, 0, 0, __LINE__); | 393 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 367 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 394 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 368 download_request_limiter_->GetDownloadStatus(web_contents())); | 395 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 369 | 396 |
| 370 UpdateExpectations(CANCEL); | 397 UpdateExpectations(CANCEL); |
| 371 CanDownload(); | 398 CanDownload(); |
| 372 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 399 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 373 download_request_limiter_->GetDownloadStatus(web_contents())); | 400 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 374 ExpectAndResetCounts(0, 1, 1, __LINE__); | 401 ExpectAndResetCounts(0, 1, 1, __LINE__); |
| 375 | 402 |
| 376 // A user gesture now should NOT change the state. | 403 // A user gesture now should NOT change the state. |
| 377 OnUserGesture(); | 404 OnUserInteraction(blink::WebInputEvent::MouseDown); |
| 378 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 405 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 379 download_request_limiter_->GetDownloadStatus(web_contents())); | 406 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 380 // And make sure we really can't download. | 407 // And make sure we really can't download. |
| 381 CanDownload(); | 408 CanDownload(); |
| 382 ExpectAndResetCounts(0, 1, 0, __LINE__); | 409 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 383 // And the state shouldn't have changed. | 410 // And the state shouldn't have changed. |
| 384 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 411 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 385 download_request_limiter_->GetDownloadStatus(web_contents())); | 412 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 386 } | 413 } |
| 387 | 414 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 // prompting when it doesn't have a InfoBarService, so unset the delegate. | 474 // prompting when it doesn't have a InfoBarService, so unset the delegate. |
| 448 testing_delegate_.UnsetInfobarDelegate(); | 475 testing_delegate_.UnsetInfobarDelegate(); |
| 449 ExpectAndResetCounts(0, 0, 0, __LINE__); | 476 ExpectAndResetCounts(0, 0, 0, __LINE__); |
| 450 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 477 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 451 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 478 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 452 // You get one freebie. | 479 // You get one freebie. |
| 453 CanDownloadFor(web_contents.get()); | 480 CanDownloadFor(web_contents.get()); |
| 454 ExpectAndResetCounts(1, 0, 0, __LINE__); | 481 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 455 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 482 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 456 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 483 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 457 OnUserGestureFor(web_contents.get()); | 484 OnUserInteractionFor(web_contents.get(), |
| 485 blink::WebInputEvent::GestureTapDown); |
| 458 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 486 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 459 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 487 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 460 CanDownloadFor(web_contents.get()); | 488 CanDownloadFor(web_contents.get()); |
| 461 ExpectAndResetCounts(1, 0, 0, __LINE__); | 489 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 462 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 490 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 463 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 491 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 464 CanDownloadFor(web_contents.get()); | 492 CanDownloadFor(web_contents.get()); |
| 465 ExpectAndResetCounts(0, 1, 0, __LINE__); | 493 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 466 EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 494 EXPECT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 467 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 495 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 468 OnUserGestureFor(web_contents.get()); | 496 OnUserInteractionFor(web_contents.get(), blink::WebInputEvent::RawKeyDown); |
| 469 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 497 EXPECT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 470 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 498 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 471 CanDownloadFor(web_contents.get()); | 499 CanDownloadFor(web_contents.get()); |
| 472 ExpectAndResetCounts(1, 0, 0, __LINE__); | 500 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 473 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 501 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 474 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 502 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 475 } | 503 } |
| 476 #endif | 504 #endif |
| 477 | 505 |
| 478 TEST_F(DownloadRequestLimiterTest, | 506 TEST_F(DownloadRequestLimiterTest, |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 // changing the content setting). | 580 // changing the content setting). |
| 553 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); | 581 UpdateContentSettings(web_contents(), CONTENT_SETTING_ASK); |
| 554 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 582 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 555 download_request_limiter_->GetDownloadStatus(web_contents())); | 583 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 556 UpdateExpectations(WAIT); | 584 UpdateExpectations(WAIT); |
| 557 CanDownload(); | 585 CanDownload(); |
| 558 ExpectAndResetCounts(0, 0, 1, __LINE__); | 586 ExpectAndResetCounts(0, 0, 1, __LINE__); |
| 559 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 587 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 560 download_request_limiter_->GetDownloadStatus(web_contents())); | 588 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 561 } | 589 } |
| OLD | NEW |