| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/permissions/permission_decision_auto_blocker.h" | 5 #include "chrome/browser/permissions/permission_decision_auto_blocker.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 callback_was_run_ = false; | 99 callback_was_run_ = false; |
| 100 } | 100 } |
| 101 | 101 |
| 102 void SetSafeBrowsingDatabaseManagerAndTimeoutForTesting( | 102 void SetSafeBrowsingDatabaseManagerAndTimeoutForTesting( |
| 103 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager, | 103 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager, |
| 104 int timeout) { | 104 int timeout) { |
| 105 autoblocker_->SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 105 autoblocker_->SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 106 timeout); | 106 timeout); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void UpdateEmbargoedStatus(ContentSettingsType permission, const GURL& url) { | 109 void CheckSafeBrowsingBlacklist(const GURL& url, |
| 110 ContentSettingsType permission) { |
| 110 base::RunLoop run_loop; | 111 base::RunLoop run_loop; |
| 111 autoblocker_->UpdateEmbargoedStatus( | 112 autoblocker_->CheckSafeBrowsingBlacklist( |
| 112 permission, url, nullptr, | 113 nullptr, url, permission, |
| 113 base::Bind(&PermissionDecisionAutoBlockerUnitTest::SetLastEmbargoStatus, | 114 base::Bind(&PermissionDecisionAutoBlockerUnitTest::SetLastEmbargoStatus, |
| 114 base::Unretained(this), run_loop.QuitClosure())); | 115 base::Unretained(this), run_loop.QuitClosure())); |
| 115 run_loop.Run(); | 116 run_loop.Run(); |
| 116 } | 117 } |
| 117 | 118 |
| 118 // Manually placing an (origin, permission) pair under embargo for | 119 // Manually placing an (origin, permission) pair under embargo for |
| 119 // blacklisting. To embargo on dismissals, RecordDismissAndEmbargo can be | 120 // blacklisting. To embargo on dismissals, RecordDismissAndEmbargo can be |
| 120 // used. | 121 // used. |
| 121 void PlaceUnderBlacklistEmbargo(ContentSettingsType permission, | 122 void PlaceUnderBlacklistEmbargo(const GURL& url, |
| 122 const GURL& url) { | 123 ContentSettingsType permission) { |
| 123 autoblocker_->PlaceUnderEmbargo( | 124 autoblocker_->PlaceUnderEmbargo( |
| 124 permission, url, | 125 url, permission, |
| 125 PermissionDecisionAutoBlocker::kPermissionBlacklistEmbargoKey); | 126 PermissionDecisionAutoBlocker::kPermissionBlacklistEmbargoKey); |
| 126 } | 127 } |
| 127 | 128 |
| 128 PermissionDecisionAutoBlocker* autoblocker() { return autoblocker_; } | 129 PermissionDecisionAutoBlocker* autoblocker() { return autoblocker_; } |
| 129 | 130 |
| 130 void SetLastEmbargoStatus(base::Closure quit_closure, bool status) { | 131 void SetLastEmbargoStatus(base::Closure quit_closure, bool status) { |
| 131 callback_was_run_ = true; | 132 callback_was_run_ = true; |
| 132 last_embargoed_status_ = status; | 133 last_embargoed_status_ = status; |
| 133 if (quit_closure) { | 134 if (quit_closure) { |
| 134 quit_closure.Run(); | 135 quit_closure.Run(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 base::HistogramTester histograms; | 269 base::HistogramTester histograms; |
| 269 | 270 |
| 270 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 271 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 271 new MockSafeBrowsingDatabaseManager(true /* perform_callback */, | 272 new MockSafeBrowsingDatabaseManager(true /* perform_callback */, |
| 272 true /* enabled */); | 273 true /* enabled */); |
| 273 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 274 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
| 274 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 275 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 275 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 276 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 276 2000 /* timeout in ms */); | 277 2000 /* timeout in ms */); |
| 277 | 278 |
| 278 UpdateEmbargoedStatus(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 279 CheckSafeBrowsingBlacklist(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 279 EXPECT_TRUE(callback_was_run()); | 280 EXPECT_TRUE(callback_was_run()); |
| 280 EXPECT_TRUE(last_embargoed_status()); | 281 EXPECT_TRUE(last_embargoed_status()); |
| 281 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", | 282 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", |
| 282 SafeBrowsingResponse::BLACKLISTED, 1); | 283 SafeBrowsingResponse::BLACKLISTED, 1); |
| 283 histograms.ExpectTotalCount( | 284 histograms.ExpectTotalCount( |
| 284 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 1); | 285 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 1); |
| 285 } | 286 } |
| 286 | 287 |
| 287 // Test that an origin that is blacklisted for a permission will not be placed | 288 // Test that an origin that is blacklisted for a permission will not be placed |
| 288 // under embargoed for another. | 289 // under embargoed for another. |
| 289 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestRequestNotBlacklisted) { | 290 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestRequestNotBlacklisted) { |
| 290 GURL url("https://www.google.com"); | 291 GURL url("https://www.google.com"); |
| 291 clock()->SetNow(base::Time::Now()); | 292 clock()->SetNow(base::Time::Now()); |
| 292 base::HistogramTester histograms; | 293 base::HistogramTester histograms; |
| 293 | 294 |
| 294 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 295 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 295 new MockSafeBrowsingDatabaseManager(true /* perform_callback */, | 296 new MockSafeBrowsingDatabaseManager(true /* perform_callback */, |
| 296 true /* enabled */); | 297 true /* enabled */); |
| 297 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 298 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
| 298 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 299 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 299 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 300 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 300 0 /* timeout in ms */); | 301 0 /* timeout in ms */); |
| 301 | 302 |
| 302 UpdateEmbargoedStatus(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, url); | 303 CheckSafeBrowsingBlacklist(url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 303 EXPECT_FALSE(last_embargoed_status()); | 304 EXPECT_FALSE(last_embargoed_status()); |
| 304 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", | 305 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", |
| 305 SafeBrowsingResponse::NOT_BLACKLISTED, 1); | 306 SafeBrowsingResponse::NOT_BLACKLISTED, 1); |
| 306 histograms.ExpectTotalCount( | 307 histograms.ExpectTotalCount( |
| 307 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 1); | 308 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 1); |
| 308 } | 309 } |
| 309 | 310 |
| 310 // Check that GetEmbargoResult returns the correct value when the embargo is set | 311 // Check that GetEmbargoResult returns the correct value when the embargo is set |
| 311 // and expires. | 312 // and expires. |
| 312 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) { | 313 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) { |
| 313 GURL url("https://www.google.com"); | 314 GURL url("https://www.google.com"); |
| 314 clock()->SetNow(base::Time::Now()); | 315 clock()->SetNow(base::Time::Now()); |
| 315 | 316 |
| 316 // Check the default state. | 317 // Check the default state. |
| 317 PermissionResult result = | 318 PermissionResult result = |
| 318 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 319 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 319 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); | 320 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
| 320 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); | 321 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 321 | 322 |
| 322 // Place under embargo and verify. | 323 // Place under embargo and verify. |
| 323 PlaceUnderBlacklistEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 324 PlaceUnderBlacklistEmbargo(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 324 result = | 325 result = |
| 325 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 326 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 326 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); | 327 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 327 EXPECT_EQ(PermissionStatusSource::SAFE_BROWSING_BLACKLIST, result.source); | 328 EXPECT_EQ(PermissionStatusSource::SAFE_BROWSING_BLACKLIST, result.source); |
| 328 | 329 |
| 329 // Check that the origin is not under embargo for a different permission. | 330 // Check that the origin is not under embargo for a different permission. |
| 330 result = | 331 result = |
| 331 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, url); | 332 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 332 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); | 333 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
| 333 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); | 334 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 334 | 335 |
| 335 // Confirm embargo status during the embargo period. | 336 // Confirm embargo status during the embargo period. |
| 336 clock()->Advance(base::TimeDelta::FromDays(5)); | 337 clock()->Advance(base::TimeDelta::FromDays(5)); |
| 337 result = | 338 result = |
| 338 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 339 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 339 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); | 340 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 340 EXPECT_EQ(PermissionStatusSource::SAFE_BROWSING_BLACKLIST, result.source); | 341 EXPECT_EQ(PermissionStatusSource::SAFE_BROWSING_BLACKLIST, result.source); |
| 341 | 342 |
| 342 // Check embargo is lifted on expiry day. A small offset after the exact | 343 // Check embargo is lifted on expiry day. A small offset after the exact |
| 343 // embargo expiration date has been added to account for any precision errors | 344 // embargo expiration date has been added to account for any precision errors |
| 344 // when removing the date stored as a double from the permission dictionary. | 345 // when removing the date stored as a double from the permission dictionary. |
| 345 clock()->Advance(base::TimeDelta::FromHours(3 * 24 + 1)); | 346 clock()->Advance(base::TimeDelta::FromHours(3 * 24 + 1)); |
| 346 result = | 347 result = |
| 347 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 348 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 348 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); | 349 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
| 349 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); | 350 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 350 | 351 |
| 351 // Check embargo is lifted well after the expiry day. | 352 // Check embargo is lifted well after the expiry day. |
| 352 clock()->Advance(base::TimeDelta::FromDays(1)); | 353 clock()->Advance(base::TimeDelta::FromDays(1)); |
| 353 result = | 354 result = |
| 354 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 355 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 355 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); | 356 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
| 356 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); | 357 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 357 | 358 |
| 358 // Place under embargo again and verify the embargo status. | 359 // Place under embargo again and verify the embargo status. |
| 359 PlaceUnderBlacklistEmbargo(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, url); | 360 PlaceUnderBlacklistEmbargo(url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 360 clock()->Advance(base::TimeDelta::FromDays(1)); | 361 clock()->Advance(base::TimeDelta::FromDays(1)); |
| 361 result = | 362 result = |
| 362 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, url); | 363 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 363 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); | 364 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 364 EXPECT_EQ(PermissionStatusSource::SAFE_BROWSING_BLACKLIST, result.source); | 365 EXPECT_EQ(PermissionStatusSource::SAFE_BROWSING_BLACKLIST, result.source); |
| 365 } | 366 } |
| 366 | 367 |
| 367 // Tests the alternating pattern of the block on multiple dismiss behaviour. On | 368 // Tests the alternating pattern of the block on multiple dismiss behaviour. On |
| 368 // N dismissals, the origin to be embargoed for the requested permission and | 369 // N dismissals, the origin to be embargoed for the requested permission and |
| 369 // automatically blocked. Each time the embargo is lifted, the site gets another | 370 // automatically blocked. Each time the embargo is lifted, the site gets another |
| 370 // chance to request the permission, but if it is again dismissed it is placed | 371 // chance to request the permission, but if it is again dismissed it is placed |
| 371 // under embargo again and its permission requests blocked. | 372 // under embargo again and its permission requests blocked. |
| 372 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) { | 373 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) { |
| 373 GURL url("https://www.google.com"); | 374 GURL url("https://www.google.com"); |
| 374 clock()->SetNow(base::Time::Now()); | 375 clock()->SetNow(base::Time::Now()); |
| 375 base::HistogramTester histograms; | 376 base::HistogramTester histograms; |
| 376 | 377 |
| 377 // Record some dismisses. | 378 // Record some dismisses. |
| 378 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( | 379 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 379 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); | 380 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 380 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( | 381 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 381 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); | 382 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 382 | 383 |
| 383 // A request with < 3 prior dismisses should not be automatically blocked. | 384 // A request with < 3 prior dismisses should not be automatically blocked. |
| 384 PermissionResult result = | 385 PermissionResult result = |
| 385 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 386 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 386 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); | 387 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
| 387 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); | 388 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 388 | 389 |
| 389 // After the 3rd dismiss subsequent permission requests should be autoblocked. | 390 // After the 3rd dismiss subsequent permission requests should be autoblocked. |
| 390 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( | 391 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( |
| 391 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); | 392 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 392 result = | 393 result = |
| 393 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 394 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 394 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); | 395 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 395 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); | 396 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 396 | 397 |
| 397 histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse", | 398 histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse", |
| 398 0); | 399 0); |
| 399 histograms.ExpectTotalCount( | 400 histograms.ExpectTotalCount( |
| 400 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 0); | 401 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 0); |
| 401 // Accelerate time forward, check that the embargo status is lifted and the | 402 // Accelerate time forward, check that the embargo status is lifted and the |
| 402 // request won't be automatically blocked. | 403 // request won't be automatically blocked. |
| 403 clock()->Advance(base::TimeDelta::FromDays(8)); | 404 clock()->Advance(base::TimeDelta::FromDays(8)); |
| 404 result = | 405 result = |
| 405 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 406 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 406 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); | 407 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
| 407 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); | 408 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 408 | 409 |
| 409 // Record another dismiss, subsequent requests should be autoblocked again. | 410 // Record another dismiss, subsequent requests should be autoblocked again. |
| 410 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( | 411 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( |
| 411 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); | 412 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 412 result = | 413 result = |
| 413 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 414 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 414 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); | 415 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 415 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); | 416 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 416 | 417 |
| 417 // Accelerate time again, check embargo is lifted and another permission | 418 // Accelerate time again, check embargo is lifted and another permission |
| 418 // request is let through. | 419 // request is let through. |
| 419 clock()->Advance(base::TimeDelta::FromDays(8)); | 420 clock()->Advance(base::TimeDelta::FromDays(8)); |
| 420 result = | 421 result = |
| 421 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 422 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 422 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); | 423 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
| 423 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); | 424 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 424 | 425 |
| 425 // Record another dismiss, subsequent requests should be autoblocked again. | 426 // Record another dismiss, subsequent requests should be autoblocked again. |
| 426 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( | 427 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( |
| 427 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); | 428 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 428 result = | 429 result = |
| 429 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 430 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 430 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); | 431 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 431 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); | 432 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 432 histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse", | 433 histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse", |
| 433 0); | 434 0); |
| 434 histograms.ExpectTotalCount( | 435 histograms.ExpectTotalCount( |
| 435 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 0); | 436 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 0); |
| 436 } | 437 } |
| 437 | 438 |
| 438 // Test the logic for a combination of blacklisting and dismissal embargo. | 439 // Test the logic for a combination of blacklisting and dismissal embargo. |
| 439 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) { | 440 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) { |
| 440 GURL url("https://www.google.com"); | 441 GURL url("https://www.google.com"); |
| 441 clock()->SetNow(base::Time::Now()); | 442 clock()->SetNow(base::Time::Now()); |
| 442 | 443 |
| 443 // Place under blacklist embargo and check the status. | 444 // Place under blacklist embargo and check the status. |
| 444 PlaceUnderBlacklistEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 445 PlaceUnderBlacklistEmbargo(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 445 clock()->Advance(base::TimeDelta::FromDays(5)); | 446 clock()->Advance(base::TimeDelta::FromDays(5)); |
| 446 PermissionResult result = | 447 PermissionResult result = |
| 447 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 448 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 448 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); | 449 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 449 EXPECT_EQ(PermissionStatusSource::SAFE_BROWSING_BLACKLIST, result.source); | 450 EXPECT_EQ(PermissionStatusSource::SAFE_BROWSING_BLACKLIST, result.source); |
| 450 | 451 |
| 451 // Record dismisses to place it under dismissal embargo. | 452 // Record dismisses to place it under dismissal embargo. |
| 452 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( | 453 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 453 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); | 454 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 454 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( | 455 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 455 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); | 456 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 456 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( | 457 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( |
| 457 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); | 458 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 458 | 459 |
| 459 // Accelerate time to a point where the blacklist embargo should be expired | 460 // Accelerate time to a point where the blacklist embargo should be expired |
| 460 // and check that dismissal embargo is still set. | 461 // and check that dismissal embargo is still set. |
| 461 clock()->Advance(base::TimeDelta::FromDays(3)); | 462 clock()->Advance(base::TimeDelta::FromDays(3)); |
| 462 result = | 463 result = |
| 463 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 464 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 464 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); | 465 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 465 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); | 466 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 466 } | 467 } |
| 467 | 468 |
| 468 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { | 469 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { |
| 469 GURL url("https://www.google.com"); | 470 GURL url("https://www.google.com"); |
| 470 clock()->SetNow(base::Time::Now()); | 471 clock()->SetNow(base::Time::Now()); |
| 471 base::HistogramTester histograms; | 472 base::HistogramTester histograms; |
| 472 | 473 |
| 473 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 474 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 474 new MockSafeBrowsingDatabaseManager(false /* perform_callback */, | 475 new MockSafeBrowsingDatabaseManager(false /* perform_callback */, |
| 475 true /* enabled */); | 476 true /* enabled */); |
| 476 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 477 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
| 477 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 478 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 478 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 479 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 479 0 /* timeout in ms */); | 480 0 /* timeout in ms */); |
| 480 | 481 |
| 481 UpdateEmbargoedStatus(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 482 CheckSafeBrowsingBlacklist(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 482 EXPECT_TRUE(callback_was_run()); | 483 EXPECT_TRUE(callback_was_run()); |
| 483 EXPECT_FALSE(last_embargoed_status()); | 484 EXPECT_FALSE(last_embargoed_status()); |
| 484 | 485 |
| 485 PermissionResult result = | 486 PermissionResult result = |
| 486 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 487 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 487 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); | 488 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
| 488 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); | 489 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 489 | 490 |
| 490 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", | 491 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", |
| 491 SafeBrowsingResponse::TIMEOUT, 1); | 492 SafeBrowsingResponse::TIMEOUT, 1); |
| 492 histograms.ExpectTotalCount( | 493 histograms.ExpectTotalCount( |
| 493 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 1); | 494 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 1); |
| 494 db_manager->SetPerformCallback(true); | 495 db_manager->SetPerformCallback(true); |
| 495 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 496 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 496 2000 /* timeout in ms */); | 497 2000 /* timeout in ms */); |
| 497 | 498 |
| 498 clock()->Advance(base::TimeDelta::FromDays(1)); | 499 clock()->Advance(base::TimeDelta::FromDays(1)); |
| 499 UpdateEmbargoedStatus(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 500 CheckSafeBrowsingBlacklist(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 500 EXPECT_TRUE(callback_was_run()); | 501 EXPECT_TRUE(callback_was_run()); |
| 501 EXPECT_TRUE(last_embargoed_status()); | 502 EXPECT_TRUE(last_embargoed_status()); |
| 502 histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse", | 503 histograms.ExpectTotalCount("Permissions.AutoBlocker.SafeBrowsingResponse", |
| 503 2); | 504 2); |
| 504 histograms.ExpectTotalCount( | 505 histograms.ExpectTotalCount( |
| 505 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 2); | 506 "Permissions.AutoBlocker.SafeBrowsingResponseTime", 2); |
| 506 histograms.ExpectBucketCount("Permissions.AutoBlocker.SafeBrowsingResponse", | 507 histograms.ExpectBucketCount("Permissions.AutoBlocker.SafeBrowsingResponse", |
| 507 SafeBrowsingResponse::BLACKLISTED, 1); | 508 SafeBrowsingResponse::BLACKLISTED, 1); |
| 508 clock()->Advance(base::TimeDelta::FromDays(1)); | 509 clock()->Advance(base::TimeDelta::FromDays(1)); |
| 509 result = | 510 result = |
| 510 autoblocker()->GetEmbargoResult(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 511 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 511 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); | 512 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 512 EXPECT_EQ(PermissionStatusSource::SAFE_BROWSING_BLACKLIST, result.source); | 513 EXPECT_EQ(PermissionStatusSource::SAFE_BROWSING_BLACKLIST, result.source); |
| 513 } | 514 } |
| 514 | 515 |
| 515 // TODO(raymes): See crbug.com/681709. Remove after M60. | 516 // TODO(raymes): See crbug.com/681709. Remove after M60. |
| 516 TEST_F(PermissionDecisionAutoBlockerUnitTest, | 517 TEST_F(PermissionDecisionAutoBlockerUnitTest, |
| 517 MigrateNoDecisionCountToPermissionAutoBlockerData) { | 518 MigrateNoDecisionCountToPermissionAutoBlockerData) { |
| 518 GURL url("https://www.google.com"); | 519 GURL url("https://www.google.com"); |
| 519 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); | 520 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); |
| 520 | 521 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 588 // manager is disabled. | 589 // manager is disabled. |
| 589 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDisabledDatabaseManager) { | 590 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDisabledDatabaseManager) { |
| 590 GURL url("https://www.google.com"); | 591 GURL url("https://www.google.com"); |
| 591 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 592 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 592 new MockSafeBrowsingDatabaseManager(true /* perform_callback */, | 593 new MockSafeBrowsingDatabaseManager(true /* perform_callback */, |
| 593 false /* enabled */); | 594 false /* enabled */); |
| 594 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 595 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
| 595 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 596 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 596 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 597 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 597 2000 /* timeout in ms */); | 598 2000 /* timeout in ms */); |
| 598 UpdateEmbargoedStatus(CONTENT_SETTINGS_TYPE_GEOLOCATION, url); | 599 CheckSafeBrowsingBlacklist(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 599 EXPECT_TRUE(callback_was_run()); | 600 EXPECT_TRUE(callback_was_run()); |
| 600 EXPECT_FALSE(last_embargoed_status()); | 601 EXPECT_FALSE(last_embargoed_status()); |
| 601 } | 602 } |
| 602 | 603 |
| 603 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingResponse) { | 604 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingResponse) { |
| 604 GURL url("https://www.google.com"); | 605 GURL url("https://www.google.com"); |
| 605 clock()->SetNow(base::Time::Now()); | 606 clock()->SetNow(base::Time::Now()); |
| 606 base::HistogramTester histograms; | 607 base::HistogramTester histograms; |
| 607 | 608 |
| 608 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = | 609 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = |
| 609 new MockSafeBrowsingDatabaseManager(true /* perform_callback */, | 610 new MockSafeBrowsingDatabaseManager(true /* perform_callback */, |
| 610 true /* enabled */); | 611 true /* enabled */); |
| 611 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; | 612 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; |
| 612 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 613 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 613 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 614 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 614 0 /* timeout in ms */); | 615 0 /* timeout in ms */); |
| 615 | 616 |
| 616 UpdateEmbargoedStatus(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, url); | 617 CheckSafeBrowsingBlacklist(url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 617 EXPECT_FALSE(last_embargoed_status()); | 618 EXPECT_FALSE(last_embargoed_status()); |
| 618 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", | 619 histograms.ExpectUniqueSample("Permissions.AutoBlocker.SafeBrowsingResponse", |
| 619 SafeBrowsingResponse::NOT_BLACKLISTED, 1); | 620 SafeBrowsingResponse::NOT_BLACKLISTED, 1); |
| 620 } | 621 } |
| OLD | NEW |