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