Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(613)

Side by Side Diff: chrome/browser/permissions/permission_decision_auto_blocker_unittest.cc

Issue 2675483002: Replace PermissionType in chrome/ with ContentSettingsType (Closed)
Patch Set: rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/test/scoped_feature_list.h" 11 #include "base/test/scoped_feature_list.h"
12 #include "base/test/simple_test_clock.h" 12 #include "base/test/simple_test_clock.h"
13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 13 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
14 #include "chrome/browser/permissions/permission_util.h" 14 #include "chrome/browser/permissions/permission_util.h"
15 #include "chrome/common/chrome_features.h" 15 #include "chrome/common/chrome_features.h"
16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
17 #include "chrome/test/base/testing_profile.h" 17 #include "chrome/test/base/testing_profile.h"
18 #include "components/content_settings/core/browser/host_content_settings_map.h" 18 #include "components/content_settings/core/browser/host_content_settings_map.h"
19 #include "components/safe_browsing_db/test_database_manager.h" 19 #include "components/safe_browsing_db/test_database_manager.h"
20 #include "content/public/browser/permission_type.h"
21 20
22 namespace { 21 namespace {
23 22
24 bool FilterGoogle(const GURL& url) { 23 bool FilterGoogle(const GURL& url) {
25 return url == "https://www.google.com/"; 24 return url == "https://www.google.com/";
26 } 25 }
27 26
28 bool FilterAll(const GURL& url) { 27 bool FilterAll(const GURL& url) {
29 return true; 28 return true;
30 } 29 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 callback_was_run_ = false; 97 callback_was_run_ = false;
99 } 98 }
100 99
101 void SetSafeBrowsingDatabaseManagerAndTimeoutForTesting( 100 void SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(
102 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager, 101 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager,
103 int timeout) { 102 int timeout) {
104 autoblocker_->SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 103 autoblocker_->SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
105 timeout); 104 timeout);
106 } 105 }
107 106
108 void UpdateEmbargoedStatus(content::PermissionType permission, 107 void UpdateEmbargoedStatus(ContentSettingsType permission, const GURL& url) {
109 const GURL& url) {
110 base::RunLoop run_loop; 108 base::RunLoop run_loop;
111 autoblocker_->UpdateEmbargoedStatus( 109 autoblocker_->UpdateEmbargoedStatus(
112 permission, url, nullptr, 110 permission, url, nullptr,
113 base::Bind(&PermissionDecisionAutoBlockerUnitTest::SetLastEmbargoStatus, 111 base::Bind(&PermissionDecisionAutoBlockerUnitTest::SetLastEmbargoStatus,
114 base::Unretained(this), run_loop.QuitClosure())); 112 base::Unretained(this), run_loop.QuitClosure()));
115 run_loop.Run(); 113 run_loop.Run();
116 } 114 }
117 115
118 // Manually placing an (origin, permission) pair under embargo for 116 // Manually placing an (origin, permission) pair under embargo for
119 // blacklisting. To embargo on dismissals, RecordDismissAndEmbargo can be 117 // blacklisting. To embargo on dismissals, RecordDismissAndEmbargo can be
120 // used. 118 // used.
121 void PlaceUnderBlacklistEmbargo(content::PermissionType permission, 119 void PlaceUnderBlacklistEmbargo(ContentSettingsType permission,
122 const GURL& url) { 120 const GURL& url) {
123 autoblocker_->PlaceUnderEmbargo( 121 autoblocker_->PlaceUnderEmbargo(
124 permission, url, 122 permission, url,
125 PermissionDecisionAutoBlocker::kPermissionBlacklistEmbargoKey); 123 PermissionDecisionAutoBlocker::kPermissionBlacklistEmbargoKey);
126 } 124 }
127 125
128 PermissionDecisionAutoBlocker* autoblocker() { return autoblocker_; } 126 PermissionDecisionAutoBlocker* autoblocker() { return autoblocker_; }
129 127
130 void SetLastEmbargoStatus(base::Closure quit_closure, bool status) { 128 void SetLastEmbargoStatus(base::Closure quit_closure, bool status) {
131 callback_was_run_ = true; 129 callback_was_run_ = true;
(...skipping 25 matching lines...) Expand all
157 bool last_embargoed_status_; 155 bool last_embargoed_status_;
158 bool callback_was_run_; 156 bool callback_was_run_;
159 }; 157 };
160 158
161 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) { 159 TEST_F(PermissionDecisionAutoBlockerUnitTest, RemoveCountsByUrl) {
162 GURL url1("https://www.google.com"); 160 GURL url1("https://www.google.com");
163 GURL url2("https://www.example.com"); 161 GURL url2("https://www.example.com");
164 162
165 // Record some dismissals. 163 // Record some dismissals.
166 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 164 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
167 url1, content::PermissionType::GEOLOCATION)); 165 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION));
168 EXPECT_EQ(1, autoblocker()->GetDismissCount( 166 EXPECT_EQ(1, autoblocker()->GetDismissCount(
169 url1, content::PermissionType::GEOLOCATION)); 167 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION));
170 168
171 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 169 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
172 url1, content::PermissionType::GEOLOCATION)); 170 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION));
173 EXPECT_EQ(2, autoblocker()->GetDismissCount( 171 EXPECT_EQ(2, autoblocker()->GetDismissCount(
174 url1, content::PermissionType::GEOLOCATION)); 172 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION));
175 173
176 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( 174 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
177 url1, content::PermissionType::GEOLOCATION)); 175 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION));
178 EXPECT_EQ(3, autoblocker()->GetDismissCount( 176 EXPECT_EQ(3, autoblocker()->GetDismissCount(
179 url1, content::PermissionType::GEOLOCATION)); 177 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION));
180 178
181 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 179 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
182 url2, content::PermissionType::GEOLOCATION)); 180 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION));
183 EXPECT_EQ(1, autoblocker()->GetDismissCount( 181 EXPECT_EQ(1, autoblocker()->GetDismissCount(
184 url2, content::PermissionType::GEOLOCATION)); 182 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION));
185 183
186 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 184 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
187 url1, content::PermissionType::NOTIFICATIONS)); 185 url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
188 EXPECT_EQ(1, autoblocker()->GetDismissCount( 186 EXPECT_EQ(1, autoblocker()->GetDismissCount(
189 url1, content::PermissionType::NOTIFICATIONS)); 187 url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
190 188
191 // Record some ignores. 189 // Record some ignores.
192 EXPECT_EQ(1, autoblocker()->RecordIgnore( 190 EXPECT_EQ(1, autoblocker()->RecordIgnore(
193 url1, content::PermissionType::MIDI_SYSEX)); 191 url1, CONTENT_SETTINGS_TYPE_MIDI_SYSEX));
194 EXPECT_EQ(1, autoblocker()->RecordIgnore( 192 EXPECT_EQ(1, autoblocker()->RecordIgnore(
195 url1, content::PermissionType::DURABLE_STORAGE)); 193 url1, CONTENT_SETTINGS_TYPE_DURABLE_STORAGE));
196 EXPECT_EQ(1, autoblocker()->RecordIgnore( 194 EXPECT_EQ(1, autoblocker()->RecordIgnore(
197 url2, content::PermissionType::GEOLOCATION)); 195 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION));
198 EXPECT_EQ(2, autoblocker()->RecordIgnore( 196 EXPECT_EQ(2, autoblocker()->RecordIgnore(
199 url2, content::PermissionType::GEOLOCATION)); 197 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION));
200 198
201 autoblocker()->RemoveCountsByUrl(base::Bind(&FilterGoogle)); 199 autoblocker()->RemoveCountsByUrl(base::Bind(&FilterGoogle));
202 200
203 // Expect that url1's actions are gone, but url2's remain. 201 // Expect that url1's actions are gone, but url2's remain.
204 EXPECT_EQ(0, autoblocker()->GetDismissCount( 202 EXPECT_EQ(0, autoblocker()->GetDismissCount(
205 url1, content::PermissionType::GEOLOCATION)); 203 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION));
206 EXPECT_EQ(0, autoblocker()->GetDismissCount( 204 EXPECT_EQ(0, autoblocker()->GetDismissCount(
207 url1, content::PermissionType::NOTIFICATIONS)); 205 url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
208 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 206 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
209 url1, content::PermissionType::MIDI_SYSEX)); 207 url1, CONTENT_SETTINGS_TYPE_MIDI_SYSEX));
210 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 208 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
211 url1, content::PermissionType::DURABLE_STORAGE)); 209 url1, CONTENT_SETTINGS_TYPE_DURABLE_STORAGE));
212 210
213 EXPECT_EQ(1, autoblocker()->GetDismissCount( 211 EXPECT_EQ(1, autoblocker()->GetDismissCount(
214 url2, content::PermissionType::GEOLOCATION)); 212 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION));
215 EXPECT_EQ(2, autoblocker()->GetIgnoreCount( 213 EXPECT_EQ(2, autoblocker()->GetIgnoreCount(
216 url2, content::PermissionType::GEOLOCATION)); 214 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION));
217 215
218 // Add some more actions. 216 // Add some more actions.
219 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 217 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
220 url1, content::PermissionType::GEOLOCATION)); 218 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION));
221 EXPECT_EQ(1, autoblocker()->GetDismissCount( 219 EXPECT_EQ(1, autoblocker()->GetDismissCount(
222 url1, content::PermissionType::GEOLOCATION)); 220 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION));
223 221
224 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 222 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
225 url1, content::PermissionType::NOTIFICATIONS)); 223 url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
226 EXPECT_EQ(1, autoblocker()->GetDismissCount( 224 EXPECT_EQ(1, autoblocker()->GetDismissCount(
227 url1, content::PermissionType::NOTIFICATIONS)); 225 url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
228 226
229 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 227 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
230 url2, content::PermissionType::GEOLOCATION)); 228 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION));
231 EXPECT_EQ(2, autoblocker()->GetDismissCount( 229 EXPECT_EQ(2, autoblocker()->GetDismissCount(
232 url2, content::PermissionType::GEOLOCATION)); 230 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION));
233 231
234 EXPECT_EQ(1, autoblocker()->RecordIgnore( 232 EXPECT_EQ(1, autoblocker()->RecordIgnore(
235 url1, content::PermissionType::GEOLOCATION)); 233 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION));
236 EXPECT_EQ(1, autoblocker()->RecordIgnore( 234 EXPECT_EQ(1, autoblocker()->RecordIgnore(
237 url1, content::PermissionType::NOTIFICATIONS)); 235 url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
238 EXPECT_EQ(1, autoblocker()->RecordIgnore( 236 EXPECT_EQ(1, autoblocker()->RecordIgnore(
239 url1, content::PermissionType::DURABLE_STORAGE)); 237 url1, CONTENT_SETTINGS_TYPE_DURABLE_STORAGE));
240 EXPECT_EQ(1, autoblocker()->RecordIgnore( 238 EXPECT_EQ(1, autoblocker()->RecordIgnore(
241 url2, content::PermissionType::MIDI_SYSEX)); 239 url2, CONTENT_SETTINGS_TYPE_MIDI_SYSEX));
242 240
243 // Remove everything and expect that it's all gone. 241 // Remove everything and expect that it's all gone.
244 autoblocker()->RemoveCountsByUrl(base::Bind(&FilterAll)); 242 autoblocker()->RemoveCountsByUrl(base::Bind(&FilterAll));
245 243
246 EXPECT_EQ(0, autoblocker()->GetDismissCount( 244 EXPECT_EQ(0, autoblocker()->GetDismissCount(
247 url1, content::PermissionType::GEOLOCATION)); 245 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION));
248 EXPECT_EQ(0, autoblocker()->GetDismissCount( 246 EXPECT_EQ(0, autoblocker()->GetDismissCount(
249 url1, content::PermissionType::NOTIFICATIONS)); 247 url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
250 EXPECT_EQ(0, autoblocker()->GetDismissCount( 248 EXPECT_EQ(0, autoblocker()->GetDismissCount(
251 url2, content::PermissionType::GEOLOCATION)); 249 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION));
252 250
253 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 251 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
254 url1, content::PermissionType::GEOLOCATION)); 252 url1, CONTENT_SETTINGS_TYPE_GEOLOCATION));
255 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 253 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
256 url1, content::PermissionType::NOTIFICATIONS)); 254 url1, CONTENT_SETTINGS_TYPE_NOTIFICATIONS));
257 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 255 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
258 url2, content::PermissionType::GEOLOCATION)); 256 url2, CONTENT_SETTINGS_TYPE_GEOLOCATION));
259 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 257 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
260 url2, content::PermissionType::DURABLE_STORAGE)); 258 url2, CONTENT_SETTINGS_TYPE_DURABLE_STORAGE));
261 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 259 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
262 url2, content::PermissionType::MIDI_SYSEX)); 260 url2, CONTENT_SETTINGS_TYPE_MIDI_SYSEX));
263 } 261 }
264 262
265 // Test that an origin that has been blacklisted for a permission is embargoed. 263 // Test that an origin that has been blacklisted for a permission is embargoed.
266 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) { 264 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestUpdateEmbargoBlacklist) {
267 GURL url("https://www.google.com"); 265 GURL url("https://www.google.com");
268 266
269 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = 267 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
270 new MockSafeBrowsingDatabaseManager(true /* perform_callback */, 268 new MockSafeBrowsingDatabaseManager(true /* perform_callback */,
271 true /* enabled */); 269 true /* enabled */);
272 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; 270 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
273 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 271 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
274 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 272 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
275 2000 /* timeout in ms */); 273 2000 /* timeout in ms */);
276 274
277 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 275 UpdateEmbargoedStatus(CONTENT_SETTINGS_TYPE_GEOLOCATION, url);
278 EXPECT_TRUE(callback_was_run()); 276 EXPECT_TRUE(callback_was_run());
279 EXPECT_TRUE(last_embargoed_status()); 277 EXPECT_TRUE(last_embargoed_status());
280 } 278 }
281 279
282 // Check that IsUnderEmbargo returns the correct value when the embargo is set 280 // Check that IsUnderEmbargo returns the correct value when the embargo is set
283 // and expires. 281 // and expires.
284 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) { 282 TEST_F(PermissionDecisionAutoBlockerUnitTest, CheckEmbargoStatus) {
285 GURL url("https://www.google.com"); 283 GURL url("https://www.google.com");
286 clock()->SetNow(base::Time::Now()); 284 clock()->SetNow(base::Time::Now());
287 285
288 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); 286 PlaceUnderBlacklistEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url);
289 EXPECT_TRUE( 287 EXPECT_TRUE(
290 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 288 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
291 289
292 // Check that the origin is not under embargo for a different permission. 290 // Check that the origin is not under embargo for a different permission.
293 EXPECT_FALSE(autoblocker()->IsUnderEmbargo( 291 EXPECT_FALSE(
294 content::PermissionType::NOTIFICATIONS, url)); 292 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, url));
295 293
296 // Confirm embargo status during the embargo period. 294 // Confirm embargo status during the embargo period.
297 clock()->Advance(base::TimeDelta::FromDays(5)); 295 clock()->Advance(base::TimeDelta::FromDays(5));
298 EXPECT_TRUE( 296 EXPECT_TRUE(
299 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 297 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
300 298
301 // Check embargo is lifted on expiry day. A small offset after the exact 299 // Check embargo is lifted on expiry day. A small offset after the exact
302 // embargo expiration date has been added to account for any precision errors 300 // embargo expiration date has been added to account for any precision errors
303 // when removing the date stored as a double from the permission dictionary. 301 // when removing the date stored as a double from the permission dictionary.
304 clock()->Advance(base::TimeDelta::FromHours(3 * 24 + 1)); 302 clock()->Advance(base::TimeDelta::FromHours(3 * 24 + 1));
305 EXPECT_FALSE( 303 EXPECT_FALSE(
306 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 304 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
307 305
308 // Check embargo is lifted well after the expiry day. 306 // Check embargo is lifted well after the expiry day.
309 clock()->Advance(base::TimeDelta::FromDays(1)); 307 clock()->Advance(base::TimeDelta::FromDays(1));
310 EXPECT_FALSE( 308 EXPECT_FALSE(
311 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 309 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
312 310
313 // Place under embargo again and verify the embargo status. 311 // Place under embargo again and verify the embargo status.
314 PlaceUnderBlacklistEmbargo(content::PermissionType::NOTIFICATIONS, url); 312 PlaceUnderBlacklistEmbargo(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, url);
315 clock()->Advance(base::TimeDelta::FromDays(1)); 313 clock()->Advance(base::TimeDelta::FromDays(1));
316 EXPECT_TRUE(autoblocker()->IsUnderEmbargo( 314 EXPECT_TRUE(
317 content::PermissionType::NOTIFICATIONS, url)); 315 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_NOTIFICATIONS, url));
318 } 316 }
319 317
320 // Tests the alternating pattern of the block on multiple dismiss behaviour. On 318 // Tests the alternating pattern of the block on multiple dismiss behaviour. On
321 // N dismissals, the origin to be embargoed for the requested permission and 319 // N dismissals, the origin to be embargoed for the requested permission and
322 // automatically blocked. Each time the embargo is lifted, the site gets another 320 // automatically blocked. Each time the embargo is lifted, the site gets another
323 // chance to request the permission, but if it is again dismissed it is placed 321 // chance to request the permission, but if it is again dismissed it is placed
324 // under embargo again and its permission requests blocked. 322 // under embargo again and its permission requests blocked.
325 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) { 323 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDismissEmbargoBackoff) {
326 GURL url("https://www.google.com"); 324 GURL url("https://www.google.com");
327 clock()->SetNow(base::Time::Now()); 325 clock()->SetNow(base::Time::Now());
328 326
329 // Record some dismisses. 327 // Record some dismisses.
330 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 328 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
331 url, content::PermissionType::GEOLOCATION)); 329 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
332 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 330 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
333 url, content::PermissionType::GEOLOCATION)); 331 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
334 332
335 // A request with < 3 prior dismisses should not be automatically blocked. 333 // A request with < 3 prior dismisses should not be automatically blocked.
336 EXPECT_FALSE( 334 EXPECT_FALSE(
337 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 335 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
338 336
339 // After the 3rd dismiss subsequent permission requests should be autoblocked. 337 // After the 3rd dismiss subsequent permission requests should be autoblocked.
340 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( 338 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
341 url, content::PermissionType::GEOLOCATION)); 339 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
342 EXPECT_TRUE( 340 EXPECT_TRUE(
343 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 341 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
344 342
345 // Accelerate time forward, check that the embargo status is lifted and the 343 // Accelerate time forward, check that the embargo status is lifted and the
346 // request won't be automatically blocked. 344 // request won't be automatically blocked.
347 clock()->Advance(base::TimeDelta::FromDays(8)); 345 clock()->Advance(base::TimeDelta::FromDays(8));
348 EXPECT_FALSE( 346 EXPECT_FALSE(
349 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 347 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
350 348
351 // Record another dismiss, subsequent requests should be autoblocked again. 349 // Record another dismiss, subsequent requests should be autoblocked again.
352 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( 350 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
353 url, content::PermissionType::GEOLOCATION)); 351 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
354 EXPECT_TRUE( 352 EXPECT_TRUE(
355 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 353 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
356 354
357 // Accelerate time again, check embargo is lifted and another permission 355 // Accelerate time again, check embargo is lifted and another permission
358 // request is let through. 356 // request is let through.
359 clock()->Advance(base::TimeDelta::FromDays(8)); 357 clock()->Advance(base::TimeDelta::FromDays(8));
360 EXPECT_FALSE( 358 EXPECT_FALSE(
361 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 359 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
362 } 360 }
363 361
364 // Test the logic for a combination of blacklisting and dismissal embargo. 362 // Test the logic for a combination of blacklisting and dismissal embargo.
365 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) { 363 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestExpiredBlacklistEmbargo) {
366 GURL url("https://www.google.com"); 364 GURL url("https://www.google.com");
367 clock()->SetNow(base::Time::Now()); 365 clock()->SetNow(base::Time::Now());
368 366
369 // Place under blacklist embargo and check the status. 367 // Place under blacklist embargo and check the status.
370 PlaceUnderBlacklistEmbargo(content::PermissionType::GEOLOCATION, url); 368 PlaceUnderBlacklistEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url);
371 clock()->Advance(base::TimeDelta::FromDays(5)); 369 clock()->Advance(base::TimeDelta::FromDays(5));
372 EXPECT_TRUE( 370 EXPECT_TRUE(
373 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 371 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
374 372
375 // Record dismisses to place it under dismissal embargo. 373 // Record dismisses to place it under dismissal embargo.
376 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 374 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
377 url, content::PermissionType::GEOLOCATION)); 375 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
378 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo( 376 EXPECT_FALSE(autoblocker()->RecordDismissAndEmbargo(
379 url, content::PermissionType::GEOLOCATION)); 377 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
380 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo( 378 EXPECT_TRUE(autoblocker()->RecordDismissAndEmbargo(
381 url, content::PermissionType::GEOLOCATION)); 379 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
382 380
383 // Accelerate time to a point where the blacklist embargo should be expired 381 // Accelerate time to a point where the blacklist embargo should be expired
384 // and check that dismissal embargo is still set. 382 // and check that dismissal embargo is still set.
385 clock()->Advance(base::TimeDelta::FromDays(3)); 383 clock()->Advance(base::TimeDelta::FromDays(3));
386 EXPECT_TRUE( 384 EXPECT_TRUE(
387 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 385 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
388 } 386 }
389 387
390 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) { 388 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestSafeBrowsingTimeout) {
391 GURL url("https://www.google.com"); 389 GURL url("https://www.google.com");
392 clock()->SetNow(base::Time::Now()); 390 clock()->SetNow(base::Time::Now());
393 391
394 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = 392 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
395 new MockSafeBrowsingDatabaseManager(false /* perform_callback */, 393 new MockSafeBrowsingDatabaseManager(false /* perform_callback */,
396 true /* enabled */); 394 true /* enabled */);
397 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; 395 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
398 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 396 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
399 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 397 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
400 0 /* timeout in ms */); 398 0 /* timeout in ms */);
401 399
402 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 400 UpdateEmbargoedStatus(CONTENT_SETTINGS_TYPE_GEOLOCATION, url);
403 EXPECT_TRUE(callback_was_run()); 401 EXPECT_TRUE(callback_was_run());
404 EXPECT_FALSE(last_embargoed_status()); 402 EXPECT_FALSE(last_embargoed_status());
405 EXPECT_FALSE( 403 EXPECT_FALSE(
406 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 404 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
407 db_manager->SetPerformCallback(true); 405 db_manager->SetPerformCallback(true);
408 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 406 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
409 2000 /* timeout in ms */); 407 2000 /* timeout in ms */);
410 408
411 clock()->Advance(base::TimeDelta::FromDays(1)); 409 clock()->Advance(base::TimeDelta::FromDays(1));
412 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 410 UpdateEmbargoedStatus(CONTENT_SETTINGS_TYPE_GEOLOCATION, url);
413 EXPECT_TRUE(callback_was_run()); 411 EXPECT_TRUE(callback_was_run());
414 EXPECT_TRUE(last_embargoed_status()); 412 EXPECT_TRUE(last_embargoed_status());
415 413
416 clock()->Advance(base::TimeDelta::FromDays(1)); 414 clock()->Advance(base::TimeDelta::FromDays(1));
417 EXPECT_TRUE( 415 EXPECT_TRUE(
418 autoblocker()->IsUnderEmbargo(content::PermissionType::GEOLOCATION, url)); 416 autoblocker()->IsUnderEmbargo(CONTENT_SETTINGS_TYPE_GEOLOCATION, url));
419 } 417 }
420 418
421 // TODO(raymes): See crbug.com/681709. Remove after M60. 419 // TODO(raymes): See crbug.com/681709. Remove after M60.
422 TEST_F(PermissionDecisionAutoBlockerUnitTest, 420 TEST_F(PermissionDecisionAutoBlockerUnitTest,
423 MigrateNoDecisionCountToPermissionAutoBlockerData) { 421 MigrateNoDecisionCountToPermissionAutoBlockerData) {
424 GURL url("https://www.google.com"); 422 GURL url("https://www.google.com");
425 auto* map = HostContentSettingsMapFactory::GetForProfile(profile()); 423 auto* map = HostContentSettingsMapFactory::GetForProfile(profile());
426 424
427 // Write to the old content setting. 425 // Write to the old content setting.
428 base::DictionaryValue permissions_dict; 426 base::DictionaryValue permissions_dict;
429 permissions_dict.SetInteger(GetDismissKey(), 100); 427 permissions_dict.SetInteger(GetDismissKey(), 100);
430 permissions_dict.SetInteger(GetIgnoreKey(), 50); 428 permissions_dict.SetInteger(GetIgnoreKey(), 50);
431 429
432 base::DictionaryValue origin_dict; 430 base::DictionaryValue origin_dict;
433 origin_dict.Set( 431 origin_dict.Set(
434 PermissionUtil::GetPermissionString(content::PermissionType::GEOLOCATION), 432 PermissionUtil::GetPermissionString(CONTENT_SETTINGS_TYPE_GEOLOCATION),
435 permissions_dict.CreateDeepCopy()); 433 permissions_dict.CreateDeepCopy());
436 map->SetWebsiteSettingDefaultScope( 434 map->SetWebsiteSettingDefaultScope(
437 url, GURL(), CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, 435 url, GURL(), CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT,
438 std::string(), origin_dict.CreateDeepCopy()); 436 std::string(), origin_dict.CreateDeepCopy());
439 437
440 // Nothing should be migrated yet, so the current values should be 0. 438 // Nothing should be migrated yet, so the current values should be 0.
441 EXPECT_EQ(0, autoblocker()->GetDismissCount( 439 EXPECT_EQ(0, autoblocker()->GetDismissCount(
442 url, content::PermissionType::GEOLOCATION)); 440 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
443 EXPECT_EQ(0, autoblocker()->GetIgnoreCount( 441 EXPECT_EQ(0, autoblocker()->GetIgnoreCount(
444 url, content::PermissionType::GEOLOCATION)); 442 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
445 443
446 // Trigger pref migration which happens at the creation of the 444 // Trigger pref migration which happens at the creation of the
447 // HostContentSettingsMap. 445 // HostContentSettingsMap.
448 { 446 {
449 scoped_refptr<HostContentSettingsMap> temp_map(new HostContentSettingsMap( 447 scoped_refptr<HostContentSettingsMap> temp_map(new HostContentSettingsMap(
450 profile()->GetPrefs(), false /* is_incognito_profile */, 448 profile()->GetPrefs(), false /* is_incognito_profile */,
451 false /* is_guest_profile */)); 449 false /* is_guest_profile */));
452 temp_map->ShutdownOnUIThread(); 450 temp_map->ShutdownOnUIThread();
453 } 451 }
454 452
455 // The values should now be migrated. 453 // The values should now be migrated.
456 EXPECT_EQ(100, autoblocker()->GetDismissCount( 454 EXPECT_EQ(100, autoblocker()->GetDismissCount(
457 url, content::PermissionType::GEOLOCATION)); 455 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
458 EXPECT_EQ(50, autoblocker()->GetIgnoreCount( 456 EXPECT_EQ(50, autoblocker()->GetIgnoreCount(
459 url, content::PermissionType::GEOLOCATION)); 457 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
460 458
461 // The old pref should be deleted. 459 // The old pref should be deleted.
462 std::unique_ptr<base::DictionaryValue> old_dict = 460 std::unique_ptr<base::DictionaryValue> old_dict =
463 base::DictionaryValue::From(map->GetWebsiteSetting( 461 base::DictionaryValue::From(map->GetWebsiteSetting(
464 url, GURL(), CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, 462 url, GURL(), CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT,
465 std::string(), nullptr)); 463 std::string(), nullptr));
466 EXPECT_EQ(nullptr, old_dict); 464 EXPECT_EQ(nullptr, old_dict);
467 465
468 // Write to the old content setting again, but with different numbers. 466 // Write to the old content setting again, but with different numbers.
469 permissions_dict.SetInteger(GetDismissKey(), 99); 467 permissions_dict.SetInteger(GetDismissKey(), 99);
470 permissions_dict.SetInteger(GetIgnoreKey(), 99); 468 permissions_dict.SetInteger(GetIgnoreKey(), 99);
471 469
472 origin_dict.Set( 470 origin_dict.Set(
473 PermissionUtil::GetPermissionString(content::PermissionType::GEOLOCATION), 471 PermissionUtil::GetPermissionString(CONTENT_SETTINGS_TYPE_GEOLOCATION),
474 permissions_dict.CreateDeepCopy()); 472 permissions_dict.CreateDeepCopy());
475 map->SetWebsiteSettingDefaultScope( 473 map->SetWebsiteSettingDefaultScope(
476 url, GURL(), CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT, 474 url, GURL(), CONTENT_SETTINGS_TYPE_PROMPT_NO_DECISION_COUNT,
477 std::string(), origin_dict.CreateDeepCopy()); 475 std::string(), origin_dict.CreateDeepCopy());
478 476
479 // Ensure that migrating again does nothing. 477 // Ensure that migrating again does nothing.
480 { 478 {
481 scoped_refptr<HostContentSettingsMap> temp_map(new HostContentSettingsMap( 479 scoped_refptr<HostContentSettingsMap> temp_map(new HostContentSettingsMap(
482 profile()->GetPrefs(), false /* is_incognito_profile */, 480 profile()->GetPrefs(), false /* is_incognito_profile */,
483 false /* is_guest_profile */)); 481 false /* is_guest_profile */));
484 temp_map->ShutdownOnUIThread(); 482 temp_map->ShutdownOnUIThread();
485 } 483 }
486 484
487 EXPECT_EQ(100, autoblocker()->GetDismissCount( 485 EXPECT_EQ(100, autoblocker()->GetDismissCount(
488 url, content::PermissionType::GEOLOCATION)); 486 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
489 EXPECT_EQ(50, autoblocker()->GetIgnoreCount( 487 EXPECT_EQ(50, autoblocker()->GetIgnoreCount(
490 url, content::PermissionType::GEOLOCATION)); 488 url, CONTENT_SETTINGS_TYPE_GEOLOCATION));
491 } 489 }
492 490
493 // Test that a blacklisted permission should not be autoblocked if the database 491 // Test that a blacklisted permission should not be autoblocked if the database
494 // manager is disabled. 492 // manager is disabled.
495 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDisabledDatabaseManager) { 493 TEST_F(PermissionDecisionAutoBlockerUnitTest, TestDisabledDatabaseManager) {
496 GURL url("https://www.google.com"); 494 GURL url("https://www.google.com");
497 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager = 495 scoped_refptr<MockSafeBrowsingDatabaseManager> db_manager =
498 new MockSafeBrowsingDatabaseManager(true /* perform_callback */, 496 new MockSafeBrowsingDatabaseManager(true /* perform_callback */,
499 false /* enabled */); 497 false /* enabled */);
500 std::set<std::string> blacklisted_permissions{"GEOLOCATION"}; 498 std::set<std::string> blacklisted_permissions{"GEOLOCATION"};
501 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions); 499 db_manager->BlacklistUrlPermissions(url, blacklisted_permissions);
502 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager, 500 SetSafeBrowsingDatabaseManagerAndTimeoutForTesting(db_manager,
503 2000 /* timeout in ms */); 501 2000 /* timeout in ms */);
504 UpdateEmbargoedStatus(content::PermissionType::GEOLOCATION, url); 502 UpdateEmbargoedStatus(CONTENT_SETTINGS_TYPE_GEOLOCATION, url);
505 EXPECT_TRUE(callback_was_run()); 503 EXPECT_TRUE(callback_was_run());
506 EXPECT_FALSE(last_embargoed_status()); 504 EXPECT_FALSE(last_embargoed_status());
507 } 505 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698