| 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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 } | 152 } |
| 153 | 153 |
| 154 private: | 154 private: |
| 155 PermissionDecisionAutoBlocker* autoblocker_; | 155 PermissionDecisionAutoBlocker* autoblocker_; |
| 156 base::test::ScopedFeatureList feature_list_; | 156 base::test::ScopedFeatureList feature_list_; |
| 157 base::SimpleTestClock* clock_; | 157 base::SimpleTestClock* clock_; |
| 158 bool last_embargoed_status_; | 158 bool last_embargoed_status_; |
| 159 bool callback_was_run_; | 159 bool callback_was_run_; |
| 160 }; | 160 }; |
| 161 | 161 |
| 162 // Check removing the the embargo for a single permission on a site works, and |
| 163 // that it doesn't interfere with other embargoed permissions or the same |
| 164 // permission embargoed on other sites. |
| 165 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveEmbargoByUrl) { |
| 166 GURL url1("https://www.google.com"); |
| 167 GURL url2("https://www.example.com"); |
| 168 |
| 169 // Record dismissals for location and notifications in |url1|. |
| 170 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 171 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 172 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 173 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 174 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( |
| 175 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 176 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 177 url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS)); |
| 178 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 179 url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS)); |
| 180 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( |
| 181 url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS)); |
| 182 // Record dismissals for location in |url2|. |
| 183 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 184 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 185 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 186 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 187 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( |
| 188 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 189 |
| 190 // Verify all dismissals recorded above resulted in embargo. |
| 191 PermissionResult result = |
| 192 autoblocker()->GetEmbargoResult(url1, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 193 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 194 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 195 result = autoblocker()->GetEmbargoResult(url1, |
| 196 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 197 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 198 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 199 result = |
| 200 autoblocker()->GetEmbargoResult(url2, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 201 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 202 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 203 |
| 204 // Remove the embargo on notifications. Verify it is no longer under embargo, |
| 205 // but location still is. |
| 206 autoblocker()->RemoveEmbargoByUrl(url1, url1, |
| 207 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 208 result = |
| 209 autoblocker()->GetEmbargoResult(url1, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 210 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 211 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 212 // Notification's default setting is |CONTENT_SETTING_ASK|. |
| 213 result = autoblocker()->GetEmbargoResult(url1, |
| 214 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 215 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
| 216 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 217 // Verify |url2|'s embargo is still intact as well. |
| 218 result = |
| 219 autoblocker()->GetEmbargoResult(url2, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 220 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 221 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 222 } |
| 223 |
| 224 // Test that removing embargo from blacklisted permissions also works. |
| 225 TEST_F(PermissionDecisionAutoBlockerUnitTest, |
| 226 RemoveEmbargoByUrlForBlacklistedPermission) { |
| 227 GURL url("https://www.example.com"); |
| 228 |
| 229 // Place under embargo and verify. |
| 230 PlaceUnderBlacklistEmbargo(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 231 PermissionResult result = |
| 232 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 233 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 234 EXPECT_EQ(PermissionStatusSource::SAFE_BROWSING_BLACKLIST, result.source); |
| 235 |
| 236 // Remove embargo and verify. |
| 237 autoblocker()->RemoveEmbargoByUrl(url, url, |
| 238 CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 239 result = |
| 240 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 241 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
| 242 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 243 } |
| 244 |
| 245 // Test it still only takes one more dismissal to re-trigger embargo after |
| 246 // removing the embargo status for a site. |
| 247 TEST_F(PermissionDecisionAutoBlockerUnitTest, |
| 248 DismissAfterRemovingEmbargoByURL) { |
| 249 GURL url("https://www.example.com"); |
| 250 |
| 251 // Record dismissals for location. |
| 252 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 253 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 254 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 255 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 256 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( |
| 257 url, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 258 |
| 259 // Verify location is under embargo. |
| 260 PermissionResult result = |
| 261 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 262 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 263 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 264 |
| 265 // Remove embargo and verify this is true. |
| 266 autoblocker()->RemoveEmbargoByUrl(url, url, |
| 267 CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 268 result = |
| 269 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 270 EXPECT_EQ(CONTENT_SETTING_ASK, result.content_setting); |
| 271 EXPECT_EQ(PermissionStatusSource::UNSPECIFIED, result.source); |
| 272 |
| 273 // Record another dismissal and verify location is under embargo again. |
| 274 autoblocker()->RecordDismissAndEmbargo(url, |
| 275 CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 276 result = |
| 277 autoblocker()->GetEmbargoResult(url, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 278 EXPECT_EQ(CONTENT_SETTING_BLOCK, result.content_setting); |
| 279 EXPECT_EQ(PermissionStatusSource::MULTIPLE_DISMISSALS, result.source); |
| 280 } |
| 281 |
| 162 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) { | 282 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) { |
| 163 GURL url1("https://www.google.com"); | 283 GURL url1("https://www.google.com"); |
| 164 GURL url2("https://www.example.com"); | 284 GURL url2("https://www.example.com"); |
| 165 | 285 |
| 166 // Record some dismissals. | 286 // Record some dismissals. |
| 167 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( | 287 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( |
| 168 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION)); | 288 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 169 EXPECT_EQ(1, autoblocker()->GetDismissCount( | 289 EXPECT_EQ(1, autoblocker()->GetDismissCount( |
| 170 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION)); | 290 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION)); |
| 171 | 291 |
| (...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); | 772 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); |
| 653 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, | 773 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, |
| 654 0 /* timeout in ms */); | 774 0 /* timeout in ms */); |
| 655 | 775 |
| 656 CheckSafeBrowsingBlacklist(url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | 776 CheckSafeBrowsingBlacklist(url, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); |
| 657 EXPECT_FALSE(last_embargoed_status()); | 777 EXPECT_FALSE(last_embargoed_status()); |
| 658 histograms.ExpectUniqueSample( | 778 histograms.ExpectUniqueSample( |
| 659 "Permissions.AutoBlocker.SafeBrowsingResponse", | 779 "Permissions.AutoBlocker.SafeBrowsingResponse", |
| 660 static_cast<int>(SafeBrowsingResponse::NOT_BLACKLISTED), 1); | 780 static_cast<int>(SafeBrowsingResponse::NOT_BLACKLISTED), 1); |
| 661 } | 781 } |
| OLD | NEW |