| 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/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "chrome/browser/content_settings/host_content_settings_map.h" | 9 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 10 #include "chrome/browser/download/download_request_infobar_delegate.h" | 10 #include "chrome/browser/download/download_request_infobar_delegate.h" |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 174 |
| 175 void SetHostContentSetting(WebContents* contents, ContentSetting setting) { | 175 void SetHostContentSetting(WebContents* contents, ContentSetting setting) { |
| 176 content_settings_->SetContentSetting( | 176 content_settings_->SetContentSetting( |
| 177 ContentSettingsPattern::FromURL(contents->GetURL()), | 177 ContentSettingsPattern::FromURL(contents->GetURL()), |
| 178 ContentSettingsPattern::Wildcard(), | 178 ContentSettingsPattern::Wildcard(), |
| 179 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, | 179 CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, |
| 180 std::string(), | 180 std::string(), |
| 181 setting); | 181 setting); |
| 182 } | 182 } |
| 183 | 183 |
| 184 void BubbleManagerDocumentLoadCompleted() { |
| 185 PermissionBubbleManager::FromWebContents(web_contents())-> |
| 186 DocumentOnLoadCompletedInMainFrame(); |
| 187 } |
| 188 |
| 184 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; | 189 scoped_refptr<DownloadRequestLimiter> download_request_limiter_; |
| 185 | 190 |
| 186 // The action that FakeCreate() should take. | 191 // The action that FakeCreate() should take. |
| 187 TestingAction testing_action_; | 192 TestingAction testing_action_; |
| 188 | 193 |
| 189 // Number of times ContinueDownload was invoked. | 194 // Number of times ContinueDownload was invoked. |
| 190 int continue_count_; | 195 int continue_count_; |
| 191 | 196 |
| 192 // Number of times CancelDownload was invoked. | 197 // Number of times CancelDownload was invoked. |
| 193 int cancel_count_; | 198 int cancel_count_; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 215 delegate_->Deny(); | 220 delegate_->Deny(); |
| 216 } else if (action == DownloadRequestLimiterTest::WAIT) { | 221 } else if (action == DownloadRequestLimiterTest::WAIT) { |
| 217 // do nothing. | 222 // do nothing. |
| 218 } else { | 223 } else { |
| 219 delegate_->Closing(); | 224 delegate_->Closing(); |
| 220 } | 225 } |
| 221 } | 226 } |
| 222 | 227 |
| 223 TEST_F(DownloadRequestLimiterTest, | 228 TEST_F(DownloadRequestLimiterTest, |
| 224 DownloadRequestLimiter_Allow) { | 229 DownloadRequestLimiter_Allow) { |
| 230 BubbleManagerDocumentLoadCompleted(); |
| 231 |
| 225 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. | 232 // All tabs should initially start at ALLOW_ONE_DOWNLOAD. |
| 226 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 233 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 227 download_request_limiter_->GetDownloadStatus(web_contents())); | 234 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 228 | 235 |
| 229 // Ask if the tab can do a download. This moves to PROMPT_BEFORE_DOWNLOAD. | 236 // Ask if the tab can do a download. This moves to PROMPT_BEFORE_DOWNLOAD. |
| 230 CanDownload(); | 237 CanDownload(); |
| 231 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 238 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 232 download_request_limiter_->GetDownloadStatus(web_contents())); | 239 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 233 // We should have been told we can download. | 240 // We should have been told we can download. |
| 234 ExpectAndResetCounts(1, 0, 0, __LINE__); | 241 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 247 // The state is at allow_all, which means the delegate shouldn't be asked. | 254 // The state is at allow_all, which means the delegate shouldn't be asked. |
| 248 // We should have been told we can download. | 255 // We should have been told we can download. |
| 249 ExpectAndResetCounts(1, 0, 0, __LINE__); | 256 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 250 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 257 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 251 download_request_limiter_->GetDownloadStatus(web_contents())); | 258 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 252 } | 259 } |
| 253 | 260 |
| 254 TEST_F(DownloadRequestLimiterTest, | 261 TEST_F(DownloadRequestLimiterTest, |
| 255 DownloadRequestLimiter_ResetOnNavigation) { | 262 DownloadRequestLimiter_ResetOnNavigation) { |
| 256 NavigateAndCommit(GURL("http://foo.com/bar")); | 263 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 264 BubbleManagerDocumentLoadCompleted(); |
| 257 | 265 |
| 258 // Do two downloads, allowing the second so that we end up with allow all. | 266 // Do two downloads, allowing the second so that we end up with allow all. |
| 259 CanDownload(); | 267 CanDownload(); |
| 260 ExpectAndResetCounts(1, 0, 0, __LINE__); | 268 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 261 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 269 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 262 download_request_limiter_->GetDownloadStatus(web_contents())); | 270 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 263 | 271 |
| 264 testing_action_ = ACCEPT; | 272 testing_action_ = ACCEPT; |
| 265 CanDownload(); | 273 CanDownload(); |
| 266 ExpectAndResetCounts(1, 0, 1, __LINE__); | 274 ExpectAndResetCounts(1, 0, 1, __LINE__); |
| 267 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 275 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 268 download_request_limiter_->GetDownloadStatus(web_contents())); | 276 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 269 | 277 |
| 270 // Navigate to a new URL with the same host, which shouldn't reset the allow | 278 // Navigate to a new URL with the same host, which shouldn't reset the allow |
| 271 // all state. | 279 // all state. |
| 272 NavigateAndCommit(GURL("http://foo.com/bar2")); | 280 NavigateAndCommit(GURL("http://foo.com/bar2")); |
| 281 BubbleManagerDocumentLoadCompleted(); |
| 273 CanDownload(); | 282 CanDownload(); |
| 274 ExpectAndResetCounts(1, 0, 0, __LINE__); | 283 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 275 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 284 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 276 download_request_limiter_->GetDownloadStatus(web_contents())); | 285 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 277 | 286 |
| 278 // Do a user gesture, because we're at allow all, this shouldn't change the | 287 // Do a user gesture, because we're at allow all, this shouldn't change the |
| 279 // state. | 288 // state. |
| 280 OnUserGesture(); | 289 OnUserGesture(); |
| 281 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, | 290 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ALL_DOWNLOADS, |
| 282 download_request_limiter_->GetDownloadStatus(web_contents())); | 291 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 283 | 292 |
| 284 // Navigate to a completely different host, which should reset the state. | 293 // Navigate to a completely different host, which should reset the state. |
| 285 NavigateAndCommit(GURL("http://fooey.com")); | 294 NavigateAndCommit(GURL("http://fooey.com")); |
| 295 BubbleManagerDocumentLoadCompleted(); |
| 286 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 296 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 287 download_request_limiter_->GetDownloadStatus(web_contents())); | 297 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 288 | 298 |
| 289 // Do two downloads, allowing the second so that we end up with allow all. | 299 // Do two downloads, allowing the second so that we end up with allow all. |
| 290 CanDownload(); | 300 CanDownload(); |
| 291 ExpectAndResetCounts(1, 0, 0, __LINE__); | 301 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 292 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 302 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 293 download_request_limiter_->GetDownloadStatus(web_contents())); | 303 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 294 | 304 |
| 295 testing_action_ = CANCEL; | 305 testing_action_ = CANCEL; |
| 296 CanDownload(); | 306 CanDownload(); |
| 297 ExpectAndResetCounts(0, 1, 1, __LINE__); | 307 ExpectAndResetCounts(0, 1, 1, __LINE__); |
| 298 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 308 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 299 download_request_limiter_->GetDownloadStatus(web_contents())); | 309 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 300 | 310 |
| 301 // Navigate to a new URL with the same host, which shouldn't reset the allow | 311 // Navigate to a new URL with the same host, which shouldn't reset the allow |
| 302 // all state. | 312 // all state. |
| 303 NavigateAndCommit(GURL("http://fooey.com/bar2")); | 313 NavigateAndCommit(GURL("http://fooey.com/bar2")); |
| 314 BubbleManagerDocumentLoadCompleted(); |
| 304 CanDownload(); | 315 CanDownload(); |
| 305 ExpectAndResetCounts(0, 1, 0, __LINE__); | 316 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 306 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 317 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 307 download_request_limiter_->GetDownloadStatus(web_contents())); | 318 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 308 } | 319 } |
| 309 | 320 |
| 310 TEST_F(DownloadRequestLimiterTest, | 321 TEST_F(DownloadRequestLimiterTest, |
| 311 DownloadRequestLimiter_ResetOnUserGesture) { | 322 DownloadRequestLimiter_ResetOnUserGesture) { |
| 312 NavigateAndCommit(GURL("http://foo.com/bar")); | 323 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 324 BubbleManagerDocumentLoadCompleted(); |
| 313 | 325 |
| 314 // Do one download, which should change to prompt before download. | 326 // Do one download, which should change to prompt before download. |
| 315 CanDownload(); | 327 CanDownload(); |
| 316 ExpectAndResetCounts(1, 0, 0, __LINE__); | 328 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 317 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 329 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 318 download_request_limiter_->GetDownloadStatus(web_contents())); | 330 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 319 | 331 |
| 320 // Do a user gesture, which should reset back to allow one. | 332 // Do a user gesture, which should reset back to allow one. |
| 321 OnUserGesture(); | 333 OnUserGesture(); |
| 322 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 334 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 343 CanDownload(); | 355 CanDownload(); |
| 344 ExpectAndResetCounts(0, 1, 0, __LINE__); | 356 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 345 // And the state shouldn't have changed. | 357 // And the state shouldn't have changed. |
| 346 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, | 358 ASSERT_EQ(DownloadRequestLimiter::DOWNLOADS_NOT_ALLOWED, |
| 347 download_request_limiter_->GetDownloadStatus(web_contents())); | 359 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 348 } | 360 } |
| 349 | 361 |
| 350 TEST_F(DownloadRequestLimiterTest, | 362 TEST_F(DownloadRequestLimiterTest, |
| 351 DownloadRequestLimiter_ResetOnReload) { | 363 DownloadRequestLimiter_ResetOnReload) { |
| 352 NavigateAndCommit(GURL("http://foo.com/bar")); | 364 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 365 BubbleManagerDocumentLoadCompleted(); |
| 353 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, | 366 ASSERT_EQ(DownloadRequestLimiter::ALLOW_ONE_DOWNLOAD, |
| 354 download_request_limiter_->GetDownloadStatus(web_contents())); | 367 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 355 | 368 |
| 356 // If the user refreshes the page without responding to the infobar, pretend | 369 // If the user refreshes the page without responding to the infobar, pretend |
| 357 // like the refresh is the initial load: they get 1 free download (probably | 370 // like the refresh is the initial load: they get 1 free download (probably |
| 358 // the same as the actual initial load), then an infobar. | 371 // the same as the actual initial load), then an infobar. |
| 359 testing_action_ = WAIT; | 372 testing_action_ = WAIT; |
| 360 | 373 |
| 361 CanDownload(); | 374 CanDownload(); |
| 362 ExpectAndResetCounts(1, 0, 0, __LINE__); | 375 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 443 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 431 CanDownloadFor(web_contents.get()); | 444 CanDownloadFor(web_contents.get()); |
| 432 ExpectAndResetCounts(1, 0, 0, __LINE__); | 445 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 433 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 446 EXPECT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 434 download_request_limiter_->GetDownloadStatus(web_contents.get())); | 447 download_request_limiter_->GetDownloadStatus(web_contents.get())); |
| 435 } | 448 } |
| 436 | 449 |
| 437 TEST_F(DownloadRequestLimiterTest, | 450 TEST_F(DownloadRequestLimiterTest, |
| 438 DownloadRequestLimiter_SetHostContentSetting) { | 451 DownloadRequestLimiter_SetHostContentSetting) { |
| 439 NavigateAndCommit(GURL("http://foo.com/bar")); | 452 NavigateAndCommit(GURL("http://foo.com/bar")); |
| 453 BubbleManagerDocumentLoadCompleted(); |
| 440 SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW); | 454 SetHostContentSetting(web_contents(), CONTENT_SETTING_ALLOW); |
| 441 | 455 |
| 442 CanDownload(); | 456 CanDownload(); |
| 443 ExpectAndResetCounts(1, 0, 0, __LINE__); | 457 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 444 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 458 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 445 download_request_limiter_->GetDownloadStatus(web_contents())); | 459 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 446 | 460 |
| 447 CanDownload(); | 461 CanDownload(); |
| 448 ExpectAndResetCounts(1, 0, 0, __LINE__); | 462 ExpectAndResetCounts(1, 0, 0, __LINE__); |
| 449 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 463 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 450 download_request_limiter_->GetDownloadStatus(web_contents())); | 464 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 451 | 465 |
| 452 SetHostContentSetting(web_contents(), CONTENT_SETTING_BLOCK); | 466 SetHostContentSetting(web_contents(), CONTENT_SETTING_BLOCK); |
| 453 | 467 |
| 454 CanDownload(); | 468 CanDownload(); |
| 455 ExpectAndResetCounts(0, 1, 0, __LINE__); | 469 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 456 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 470 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 457 download_request_limiter_->GetDownloadStatus(web_contents())); | 471 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 458 | 472 |
| 459 CanDownload(); | 473 CanDownload(); |
| 460 ExpectAndResetCounts(0, 1, 0, __LINE__); | 474 ExpectAndResetCounts(0, 1, 0, __LINE__); |
| 461 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, | 475 ASSERT_EQ(DownloadRequestLimiter::PROMPT_BEFORE_DOWNLOAD, |
| 462 download_request_limiter_->GetDownloadStatus(web_contents())); | 476 download_request_limiter_->GetDownloadStatus(web_contents())); |
| 463 } | 477 } |
| OLD | NEW |