1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/auto_reset.h"
6 #include "base/command_line.h"
7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/prefs/scoped_user_pref_update.h"
12 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
13 #include "chrome/browser/content_settings/cookie_settings_factory.h"
14 #include "chrome/browser/content_settings/mock_settings_observer.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/common/url_constants.h"
17 #include "chrome/test/base/testing_pref_service_syncable.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "components/content_settings/core/browser/content_settings_details.h"
20 #include "components/content_settings/core/browser/cookie_settings.h"
21 #include "components/content_settings/core/browser/host_content_settings_map.h"
22 #include "components/content_settings/core/browser/website_settings_info.h"
23 #include "components/content_settings/core/browser/website_settings_registry.h"
24 #include "content/public/test/test_browser_thread.h"
25 #include "net/base/static_cookie_policy.h"
26 #include "testing/gtest/include/gtest/gtest.h"
29 using content::BrowserThread
;
33 class HostContentSettingsMapTest
: public testing::Test
{
35 HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI
, &message_loop_
) {
39 const std::string
& GetPrefName(ContentSettingsType type
) {
40 return content_settings::WebsiteSettingsRegistry::GetInstance()
45 base::MessageLoop message_loop_
;
46 content::TestBrowserThread ui_thread_
;
49 TEST_F(HostContentSettingsMapTest
, DefaultValues
) {
50 TestingProfile profile
;
51 HostContentSettingsMap
* host_content_settings_map
=
52 profile
.GetHostContentSettingsMap();
54 // Check setting defaults.
55 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
56 host_content_settings_map
->GetDefaultContentSetting(
57 CONTENT_SETTINGS_TYPE_JAVASCRIPT
, NULL
));
58 host_content_settings_map
->SetDefaultContentSetting(
59 CONTENT_SETTINGS_TYPE_IMAGES
, CONTENT_SETTING_BLOCK
);
60 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
61 host_content_settings_map
->GetDefaultContentSetting(
62 CONTENT_SETTINGS_TYPE_IMAGES
, NULL
));
63 EXPECT_EQ(CONTENT_SETTING_ALLOW
, host_content_settings_map
->GetContentSetting(
64 GURL(chrome::kChromeUINewTabURL
),
65 GURL(chrome::kChromeUINewTabURL
),
66 CONTENT_SETTINGS_TYPE_IMAGES
,
69 host_content_settings_map
->SetDefaultContentSetting(
70 CONTENT_SETTINGS_TYPE_PLUGINS
, CONTENT_SETTING_ALLOW
);
71 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
72 host_content_settings_map
->GetDefaultContentSetting(
73 CONTENT_SETTINGS_TYPE_PLUGINS
, NULL
));
74 host_content_settings_map
->SetDefaultContentSetting(
75 CONTENT_SETTINGS_TYPE_PLUGINS
, CONTENT_SETTING_BLOCK
);
76 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
77 host_content_settings_map
->GetDefaultContentSetting(
78 CONTENT_SETTINGS_TYPE_PLUGINS
, NULL
));
79 host_content_settings_map
->SetDefaultContentSetting(
80 CONTENT_SETTINGS_TYPE_PLUGINS
, CONTENT_SETTING_DETECT_IMPORTANT_CONTENT
);
81 EXPECT_EQ(CONTENT_SETTING_DETECT_IMPORTANT_CONTENT
,
82 host_content_settings_map
->GetDefaultContentSetting(
83 CONTENT_SETTINGS_TYPE_PLUGINS
, NULL
));
85 host_content_settings_map
->SetDefaultContentSetting(
86 CONTENT_SETTINGS_TYPE_POPUPS
, CONTENT_SETTING_ALLOW
);
87 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
88 host_content_settings_map
->GetDefaultContentSetting(
89 CONTENT_SETTINGS_TYPE_POPUPS
, NULL
));
92 TEST_F(HostContentSettingsMapTest
, IndividualSettings
) {
93 TestingProfile profile
;
94 HostContentSettingsMap
* host_content_settings_map
=
95 profile
.GetHostContentSettingsMap();
97 // Check returning individual settings.
98 GURL
host("http://example.com/");
99 ContentSettingsPattern pattern
=
100 ContentSettingsPattern::FromString("[*.]example.com");
101 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
102 host_content_settings_map
->GetContentSetting(
103 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
104 host_content_settings_map
->SetContentSetting(
106 ContentSettingsPattern::Wildcard(),
107 CONTENT_SETTINGS_TYPE_IMAGES
,
109 CONTENT_SETTING_DEFAULT
);
110 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
111 host_content_settings_map
->GetContentSetting(
112 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
113 host_content_settings_map
->SetContentSetting(
115 ContentSettingsPattern::Wildcard(),
116 CONTENT_SETTINGS_TYPE_IMAGES
,
118 CONTENT_SETTING_BLOCK
);
119 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
120 host_content_settings_map
->GetContentSetting(
121 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
122 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
123 host_content_settings_map
->GetContentSetting(
124 host
, host
, CONTENT_SETTINGS_TYPE_PLUGINS
, std::string()));
126 // Check returning all settings for a host.
127 host_content_settings_map
->SetContentSetting(
129 ContentSettingsPattern::Wildcard(),
130 CONTENT_SETTINGS_TYPE_IMAGES
,
132 CONTENT_SETTING_DEFAULT
);
133 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
134 host_content_settings_map
->GetContentSetting(
135 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
136 host_content_settings_map
->SetContentSetting(
138 ContentSettingsPattern::Wildcard(),
139 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
141 CONTENT_SETTING_BLOCK
);
142 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
143 host_content_settings_map
->GetContentSetting(
144 host
, host
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
145 host_content_settings_map
->SetContentSetting(
147 ContentSettingsPattern::Wildcard(),
148 CONTENT_SETTINGS_TYPE_PLUGINS
,
150 CONTENT_SETTING_ALLOW
);
151 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
152 host_content_settings_map
->GetContentSetting(
153 host
, host
, CONTENT_SETTINGS_TYPE_PLUGINS
, std::string()));
154 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
155 host_content_settings_map
->GetContentSetting(
156 host
, host
, CONTENT_SETTINGS_TYPE_POPUPS
, std::string()));
157 EXPECT_EQ(CONTENT_SETTING_ASK
,
158 host_content_settings_map
->GetContentSetting(
159 host
, host
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string()));
162 host_content_settings_map
->GetContentSetting(
163 host
, host
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, std::string()));
164 EXPECT_EQ(CONTENT_SETTING_ASK
,
165 host_content_settings_map
->GetContentSetting(
166 host
, host
, CONTENT_SETTINGS_TYPE_FULLSCREEN
, std::string()));
167 EXPECT_EQ(CONTENT_SETTING_ASK
,
168 host_content_settings_map
->GetContentSetting(
169 host
, host
, CONTENT_SETTINGS_TYPE_MOUSELOCK
, std::string()));
171 // Check returning all hosts for a setting.
172 ContentSettingsPattern pattern2
=
173 ContentSettingsPattern::FromString("[*.]example.org");
174 host_content_settings_map
->SetContentSetting(
176 ContentSettingsPattern::Wildcard(),
177 CONTENT_SETTINGS_TYPE_IMAGES
,
179 CONTENT_SETTING_BLOCK
);
180 host_content_settings_map
->SetContentSetting(
182 ContentSettingsPattern::Wildcard(),
183 CONTENT_SETTINGS_TYPE_PLUGINS
,
185 CONTENT_SETTING_BLOCK
);
186 ContentSettingsForOneType host_settings
;
187 host_content_settings_map
->GetSettingsForOneType(
188 CONTENT_SETTINGS_TYPE_IMAGES
, std::string(), &host_settings
);
189 // |host_settings| contains the default setting and an exception.
190 EXPECT_EQ(2U, host_settings
.size());
191 host_content_settings_map
->GetSettingsForOneType(
192 CONTENT_SETTINGS_TYPE_PLUGINS
, std::string(), &host_settings
);
193 // |host_settings| contains the default setting and 2 exceptions.
194 EXPECT_EQ(3U, host_settings
.size());
195 host_content_settings_map
->GetSettingsForOneType(
196 CONTENT_SETTINGS_TYPE_POPUPS
, std::string(), &host_settings
);
197 // |host_settings| contains only the default setting.
198 EXPECT_EQ(1U, host_settings
.size());
201 TEST_F(HostContentSettingsMapTest
, Clear
) {
202 TestingProfile profile
;
203 HostContentSettingsMap
* host_content_settings_map
=
204 profile
.GetHostContentSettingsMap();
206 // Check clearing one type.
207 ContentSettingsPattern pattern
=
208 ContentSettingsPattern::FromString("[*.]example.org");
209 ContentSettingsPattern pattern2
=
210 ContentSettingsPattern::FromString("[*.]example.net");
211 host_content_settings_map
->SetContentSetting(
213 ContentSettingsPattern::Wildcard(),
214 CONTENT_SETTINGS_TYPE_IMAGES
,
216 CONTENT_SETTING_BLOCK
);
217 host_content_settings_map
->SetContentSetting(
219 ContentSettingsPattern::Wildcard(),
220 CONTENT_SETTINGS_TYPE_IMAGES
,
222 CONTENT_SETTING_BLOCK
);
223 host_content_settings_map
->SetContentSetting(
225 ContentSettingsPattern::Wildcard(),
226 CONTENT_SETTINGS_TYPE_PLUGINS
,
228 CONTENT_SETTING_BLOCK
);
229 host_content_settings_map
->SetContentSetting(
231 ContentSettingsPattern::Wildcard(),
232 CONTENT_SETTINGS_TYPE_IMAGES
,
234 CONTENT_SETTING_BLOCK
);
235 host_content_settings_map
->ClearSettingsForOneType(
236 CONTENT_SETTINGS_TYPE_IMAGES
);
237 ContentSettingsForOneType host_settings
;
238 host_content_settings_map
->GetSettingsForOneType(
239 CONTENT_SETTINGS_TYPE_IMAGES
, std::string(), &host_settings
);
240 // |host_settings| contains only the default setting.
241 EXPECT_EQ(1U, host_settings
.size());
242 host_content_settings_map
->GetSettingsForOneType(
243 CONTENT_SETTINGS_TYPE_PLUGINS
, std::string(), &host_settings
);
244 // |host_settings| contains the default setting and an exception.
245 EXPECT_EQ(2U, host_settings
.size());
248 TEST_F(HostContentSettingsMapTest
, Patterns
) {
249 TestingProfile profile
;
250 HostContentSettingsMap
* host_content_settings_map
=
251 profile
.GetHostContentSettingsMap();
253 GURL
host1("http://example.com/");
254 GURL
host2("http://www.example.com/");
255 GURL
host3("http://example.org/");
256 ContentSettingsPattern pattern1
=
257 ContentSettingsPattern::FromString("[*.]example.com");
258 ContentSettingsPattern pattern2
=
259 ContentSettingsPattern::FromString("example.org");
260 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
261 host_content_settings_map
->GetContentSetting(
262 host1
, host1
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
263 host_content_settings_map
->SetContentSetting(
265 ContentSettingsPattern::Wildcard(),
266 CONTENT_SETTINGS_TYPE_IMAGES
,
268 CONTENT_SETTING_BLOCK
);
269 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
270 host_content_settings_map
->GetContentSetting(
271 host1
, host1
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
272 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
273 host_content_settings_map
->GetContentSetting(
274 host2
, host2
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
275 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
276 host_content_settings_map
->GetContentSetting(
277 host3
, host3
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
278 host_content_settings_map
->SetContentSetting(
280 ContentSettingsPattern::Wildcard(),
281 CONTENT_SETTINGS_TYPE_IMAGES
,
283 CONTENT_SETTING_BLOCK
);
284 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
285 host_content_settings_map
->GetContentSetting(
286 host3
, host3
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
289 TEST_F(HostContentSettingsMapTest
, Observer
) {
290 TestingProfile profile
;
291 HostContentSettingsMap
* host_content_settings_map
=
292 profile
.GetHostContentSettingsMap();
293 MockSettingsObserver
observer(host_content_settings_map
);
295 ContentSettingsPattern primary_pattern
=
296 ContentSettingsPattern::FromString("[*.]example.com");
297 ContentSettingsPattern secondary_pattern
=
298 ContentSettingsPattern::Wildcard();
299 EXPECT_CALL(observer
,
300 OnContentSettingsChanged(host_content_settings_map
,
301 CONTENT_SETTINGS_TYPE_IMAGES
,
306 host_content_settings_map
->SetContentSetting(
309 CONTENT_SETTINGS_TYPE_IMAGES
,
311 CONTENT_SETTING_ALLOW
);
312 ::testing::Mock::VerifyAndClearExpectations(&observer
);
314 EXPECT_CALL(observer
,
315 OnContentSettingsChanged(host_content_settings_map
,
316 CONTENT_SETTINGS_TYPE_IMAGES
, false,
318 host_content_settings_map
->ClearSettingsForOneType(
319 CONTENT_SETTINGS_TYPE_IMAGES
);
320 ::testing::Mock::VerifyAndClearExpectations(&observer
);
322 EXPECT_CALL(observer
,
323 OnContentSettingsChanged(host_content_settings_map
,
324 CONTENT_SETTINGS_TYPE_IMAGES
, false,
326 host_content_settings_map
->SetDefaultContentSetting(
327 CONTENT_SETTINGS_TYPE_IMAGES
, CONTENT_SETTING_BLOCK
);
330 TEST_F(HostContentSettingsMapTest
, ObserveDefaultPref
) {
331 TestingProfile profile
;
332 HostContentSettingsMap
* host_content_settings_map
=
333 profile
.GetHostContentSettingsMap();
335 PrefService
* prefs
= profile
.GetPrefs();
336 GURL
host("http://example.com");
338 host_content_settings_map
->SetDefaultContentSetting(
339 CONTENT_SETTINGS_TYPE_IMAGES
, CONTENT_SETTING_BLOCK
);
340 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
341 host_content_settings_map
->GetContentSetting(
342 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
344 const content_settings::WebsiteSettingsInfo
* info
=
345 content_settings::WebsiteSettingsRegistry::GetInstance()->Get(
346 CONTENT_SETTINGS_TYPE_IMAGES
);
347 // Clearing the backing pref should also clear the internal cache.
348 prefs
->ClearPref(info
->default_value_pref_name());
349 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
350 host_content_settings_map
->GetContentSetting(
351 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
353 // Reseting the pref to its previous value should update the cache.
354 prefs
->SetInteger(info
->default_value_pref_name(), CONTENT_SETTING_BLOCK
);
355 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
356 host_content_settings_map
->GetContentSetting(
357 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
360 TEST_F(HostContentSettingsMapTest
, ObserveExceptionPref
) {
361 TestingProfile profile
;
362 HostContentSettingsMap
* host_content_settings_map
=
363 profile
.GetHostContentSettingsMap();
365 PrefService
* prefs
= profile
.GetPrefs();
367 // Make a copy of the default pref value so we can reset it later.
368 scoped_ptr
<base::Value
> default_value(
369 prefs
->FindPreference(GetPrefName(CONTENT_SETTINGS_TYPE_IMAGES
))
373 ContentSettingsPattern pattern
=
374 ContentSettingsPattern::FromString("[*.]example.com");
375 GURL
host("http://example.com");
377 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
378 host_content_settings_map
->GetContentSetting(
379 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
381 host_content_settings_map
->SetContentSetting(
383 ContentSettingsPattern::Wildcard(),
384 CONTENT_SETTINGS_TYPE_IMAGES
,
386 CONTENT_SETTING_BLOCK
);
387 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
388 host_content_settings_map
->GetContentSetting(
389 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
391 // Make a copy of the pref's new value so we can reset it later.
392 scoped_ptr
<base::Value
> new_value(
393 prefs
->FindPreference(GetPrefName(CONTENT_SETTINGS_TYPE_IMAGES
))
397 // Clearing the backing pref should also clear the internal cache.
398 prefs
->Set(GetPrefName(CONTENT_SETTINGS_TYPE_IMAGES
), *default_value
);
399 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
400 host_content_settings_map
->GetContentSetting(
401 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
403 // Reseting the pref to its previous value should update the cache.
404 prefs
->Set(GetPrefName(CONTENT_SETTINGS_TYPE_IMAGES
), *new_value
);
405 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
406 host_content_settings_map
->GetContentSetting(
407 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
410 TEST_F(HostContentSettingsMapTest
, HostTrimEndingDotCheck
) {
411 TestingProfile profile
;
412 HostContentSettingsMap
* host_content_settings_map
=
413 profile
.GetHostContentSettingsMap();
414 content_settings::CookieSettings
* cookie_settings
=
415 CookieSettingsFactory::GetForProfile(&profile
).get();
417 ContentSettingsPattern pattern
=
418 ContentSettingsPattern::FromString("[*.]example.com");
419 GURL
host_ending_with_dot("http://example.com./");
421 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
422 host_content_settings_map
->GetContentSetting(
423 host_ending_with_dot
,
424 host_ending_with_dot
,
425 CONTENT_SETTINGS_TYPE_IMAGES
,
427 host_content_settings_map
->SetContentSetting(
429 ContentSettingsPattern::Wildcard(),
430 CONTENT_SETTINGS_TYPE_IMAGES
,
432 CONTENT_SETTING_DEFAULT
);
434 CONTENT_SETTING_ALLOW
,
435 host_content_settings_map
->GetContentSetting(host_ending_with_dot
,
436 host_ending_with_dot
,
437 CONTENT_SETTINGS_TYPE_IMAGES
,
439 host_content_settings_map
->SetContentSetting(
441 ContentSettingsPattern::Wildcard(),
442 CONTENT_SETTINGS_TYPE_IMAGES
,
444 CONTENT_SETTING_BLOCK
);
446 CONTENT_SETTING_BLOCK
,
447 host_content_settings_map
->GetContentSetting(host_ending_with_dot
,
448 host_ending_with_dot
,
449 CONTENT_SETTINGS_TYPE_IMAGES
,
452 EXPECT_TRUE(cookie_settings
->IsSettingCookieAllowed(
453 host_ending_with_dot
, host_ending_with_dot
));
454 host_content_settings_map
->SetContentSetting(
456 ContentSettingsPattern::Wildcard(),
457 CONTENT_SETTINGS_TYPE_COOKIES
,
459 CONTENT_SETTING_DEFAULT
);
460 EXPECT_TRUE(cookie_settings
->IsSettingCookieAllowed(
461 host_ending_with_dot
, host_ending_with_dot
));
462 host_content_settings_map
->SetContentSetting(
464 ContentSettingsPattern::Wildcard(),
465 CONTENT_SETTINGS_TYPE_COOKIES
,
467 CONTENT_SETTING_BLOCK
);
468 EXPECT_FALSE(cookie_settings
->IsSettingCookieAllowed(
469 host_ending_with_dot
, host_ending_with_dot
));
471 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
472 host_content_settings_map
->GetContentSetting(
473 host_ending_with_dot
,
474 host_ending_with_dot
,
475 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
477 host_content_settings_map
->SetContentSetting(
479 ContentSettingsPattern::Wildcard(),
480 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
482 CONTENT_SETTING_DEFAULT
);
483 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
484 host_content_settings_map
->GetContentSetting(
485 host_ending_with_dot
,
486 host_ending_with_dot
,
487 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
489 host_content_settings_map
->SetContentSetting(
491 ContentSettingsPattern::Wildcard(),
492 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
494 CONTENT_SETTING_BLOCK
);
495 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
496 host_content_settings_map
->GetContentSetting(
497 host_ending_with_dot
,
498 host_ending_with_dot
,
499 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
502 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
503 host_content_settings_map
->GetContentSetting(
504 host_ending_with_dot
,
505 host_ending_with_dot
,
506 CONTENT_SETTINGS_TYPE_PLUGINS
,
508 host_content_settings_map
->SetContentSetting(
510 ContentSettingsPattern::Wildcard(),
511 CONTENT_SETTINGS_TYPE_PLUGINS
,
513 CONTENT_SETTING_DEFAULT
);
514 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
515 host_content_settings_map
->GetContentSetting(
516 host_ending_with_dot
,
517 host_ending_with_dot
,
518 CONTENT_SETTINGS_TYPE_PLUGINS
,
520 host_content_settings_map
->SetContentSetting(
522 ContentSettingsPattern::Wildcard(),
523 CONTENT_SETTINGS_TYPE_PLUGINS
,
525 CONTENT_SETTING_BLOCK
);
526 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
527 host_content_settings_map
->GetContentSetting(
528 host_ending_with_dot
,
529 host_ending_with_dot
,
530 CONTENT_SETTINGS_TYPE_PLUGINS
,
534 CONTENT_SETTING_BLOCK
,
535 host_content_settings_map
->GetContentSetting(host_ending_with_dot
,
536 host_ending_with_dot
,
537 CONTENT_SETTINGS_TYPE_POPUPS
,
539 host_content_settings_map
->SetContentSetting(
541 ContentSettingsPattern::Wildcard(),
542 CONTENT_SETTINGS_TYPE_POPUPS
,
544 CONTENT_SETTING_DEFAULT
);
546 CONTENT_SETTING_BLOCK
,
547 host_content_settings_map
->GetContentSetting(host_ending_with_dot
,
548 host_ending_with_dot
,
549 CONTENT_SETTINGS_TYPE_POPUPS
,
551 host_content_settings_map
->SetContentSetting(
553 ContentSettingsPattern::Wildcard(),
554 CONTENT_SETTINGS_TYPE_POPUPS
,
556 CONTENT_SETTING_ALLOW
);
558 CONTENT_SETTING_ALLOW
,
559 host_content_settings_map
->GetContentSetting(host_ending_with_dot
,
560 host_ending_with_dot
,
561 CONTENT_SETTINGS_TYPE_POPUPS
,
565 TEST_F(HostContentSettingsMapTest
, NestedSettings
) {
566 TestingProfile profile
;
567 HostContentSettingsMap
* host_content_settings_map
=
568 profile
.GetHostContentSettingsMap();
570 GURL
host("http://a.b.example.com/");
571 ContentSettingsPattern pattern1
=
572 ContentSettingsPattern::FromString("[*.]example.com");
573 ContentSettingsPattern pattern2
=
574 ContentSettingsPattern::FromString("[*.]b.example.com");
575 ContentSettingsPattern pattern3
=
576 ContentSettingsPattern::FromString("a.b.example.com");
578 host_content_settings_map
->SetContentSetting(
580 ContentSettingsPattern::Wildcard(),
581 CONTENT_SETTINGS_TYPE_IMAGES
,
583 CONTENT_SETTING_BLOCK
);
585 host_content_settings_map
->SetContentSetting(
587 ContentSettingsPattern::Wildcard(),
588 CONTENT_SETTINGS_TYPE_COOKIES
,
590 CONTENT_SETTING_BLOCK
);
592 host_content_settings_map
->SetContentSetting(
594 ContentSettingsPattern::Wildcard(),
595 CONTENT_SETTINGS_TYPE_PLUGINS
,
597 CONTENT_SETTING_BLOCK
);
598 host_content_settings_map
->SetDefaultContentSetting(
599 CONTENT_SETTINGS_TYPE_JAVASCRIPT
, CONTENT_SETTING_BLOCK
);
601 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
602 host_content_settings_map
->GetContentSetting(
603 host
, host
, CONTENT_SETTINGS_TYPE_COOKIES
, std::string()));
604 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
605 host_content_settings_map
->GetContentSetting(
606 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
607 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
608 host_content_settings_map
->GetContentSetting(
609 host
, host
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
610 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
611 host_content_settings_map
->GetContentSetting(
612 host
, host
, CONTENT_SETTINGS_TYPE_PLUGINS
, std::string()));
613 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
614 host_content_settings_map
->GetContentSetting(
615 host
, host
, CONTENT_SETTINGS_TYPE_POPUPS
, std::string()));
616 EXPECT_EQ(CONTENT_SETTING_ASK
,
617 host_content_settings_map
->GetContentSetting(
618 host
, host
, CONTENT_SETTINGS_TYPE_GEOLOCATION
, std::string()));
621 host_content_settings_map
->GetContentSetting(
622 host
, host
, CONTENT_SETTINGS_TYPE_NOTIFICATIONS
, std::string()));
623 EXPECT_EQ(CONTENT_SETTING_ASK
,
624 host_content_settings_map
->GetContentSetting(
625 host
, host
, CONTENT_SETTINGS_TYPE_FULLSCREEN
, std::string()));
626 EXPECT_EQ(CONTENT_SETTING_ASK
,
627 host_content_settings_map
->GetContentSetting(
628 host
, host
, CONTENT_SETTINGS_TYPE_MOUSELOCK
, std::string()));
631 TEST_F(HostContentSettingsMapTest
, OffTheRecord
) {
632 TestingProfile profile
;
633 HostContentSettingsMap
* host_content_settings_map
=
634 profile
.GetHostContentSettingsMap();
635 scoped_refptr
<HostContentSettingsMap
> otr_map(
636 new HostContentSettingsMap(profile
.GetPrefs(),
639 GURL
host("http://example.com/");
640 ContentSettingsPattern pattern
=
641 ContentSettingsPattern::FromString("[*.]example.com");
643 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
644 host_content_settings_map
->GetContentSetting(
645 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
646 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
647 otr_map
->GetContentSetting(
648 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
650 // Changing content settings on the main map should also affect the
652 host_content_settings_map
->SetContentSetting(
654 ContentSettingsPattern::Wildcard(),
655 CONTENT_SETTINGS_TYPE_IMAGES
,
657 CONTENT_SETTING_BLOCK
);
658 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
659 host_content_settings_map
->GetContentSetting(
660 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
661 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
662 otr_map
->GetContentSetting(
663 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
665 // Changing content settings on the incognito map should NOT affect the
667 otr_map
->SetContentSetting(pattern
,
668 ContentSettingsPattern::Wildcard(),
669 CONTENT_SETTINGS_TYPE_IMAGES
,
671 CONTENT_SETTING_ALLOW
);
672 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
673 host_content_settings_map
->GetContentSetting(
674 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
675 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
676 otr_map
->GetContentSetting(
677 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
679 otr_map
->ShutdownOnUIThread();
682 // For a single Unicode encoded pattern, check if it gets converted to punycode
683 // and old pattern gets deleted.
684 TEST_F(HostContentSettingsMapTest
, CanonicalizeExceptionsUnicodeOnly
) {
685 TestingProfile profile
;
686 PrefService
* prefs
= profile
.GetPrefs();
690 DictionaryPrefUpdate
update(prefs
,
691 GetPrefName(CONTENT_SETTINGS_TYPE_PLUGINS
));
692 base::DictionaryValue
* all_settings_dictionary
= update
.Get();
693 ASSERT_TRUE(NULL
!= all_settings_dictionary
);
695 base::DictionaryValue
* dummy_payload
= new base::DictionaryValue
;
696 dummy_payload
->SetInteger("setting", CONTENT_SETTING_ALLOW
);
697 all_settings_dictionary
->SetWithoutPathExpansion("[*.]\xC4\x87ira.com,*",
701 profile
.GetHostContentSettingsMap();
703 const base::DictionaryValue
* all_settings_dictionary
=
704 prefs
->GetDictionary(GetPrefName(CONTENT_SETTINGS_TYPE_PLUGINS
));
705 const base::DictionaryValue
* result
= NULL
;
706 EXPECT_FALSE(all_settings_dictionary
->GetDictionaryWithoutPathExpansion(
707 "[*.]\xC4\x87ira.com,*", &result
));
708 EXPECT_TRUE(all_settings_dictionary
->GetDictionaryWithoutPathExpansion(
709 "[*.]xn--ira-ppa.com,*", &result
));
712 // If both Unicode and its punycode pattern exist, make sure we don't touch the
713 // settings for the punycode, and that Unicode pattern gets deleted.
714 TEST_F(HostContentSettingsMapTest
, CanonicalizeExceptionsUnicodeAndPunycode
) {
715 TestingProfile profile
;
717 scoped_ptr
<base::Value
> value(base::JSONReader::DeprecatedRead(
718 "{\"[*.]\\xC4\\x87ira.com,*\":{\"setting\":1}}"));
719 profile
.GetPrefs()->Set(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES
), *value
);
721 // Set punycode equivalent, with different setting.
722 scoped_ptr
<base::Value
> puny_value(base::JSONReader::DeprecatedRead(
723 "{\"[*.]xn--ira-ppa.com,*\":{\"setting\":2}}"));
724 profile
.GetPrefs()->Set(GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES
),
727 // Initialize the content map.
728 profile
.GetHostContentSettingsMap();
730 const base::DictionaryValue
& content_setting_prefs
=
731 *profile
.GetPrefs()->GetDictionary(
732 GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES
));
733 std::string prefs_as_json
;
734 base::JSONWriter::Write(content_setting_prefs
, &prefs_as_json
);
735 EXPECT_STREQ("{\"[*.]xn--ira-ppa.com,*\":{\"setting\":2}}",
736 prefs_as_json
.c_str());
739 // If a default-content-setting is managed, the managed value should be used
740 // instead of the default value.
741 TEST_F(HostContentSettingsMapTest
, ManagedDefaultContentSetting
) {
742 TestingProfile profile
;
743 HostContentSettingsMap
* host_content_settings_map
=
744 profile
.GetHostContentSettingsMap();
745 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
747 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
748 host_content_settings_map
->GetDefaultContentSetting(
749 CONTENT_SETTINGS_TYPE_JAVASCRIPT
, NULL
));
751 // Set managed-default-content-setting through the coresponding preferences.
752 prefs
->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting
,
753 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
754 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
755 host_content_settings_map
->GetDefaultContentSetting(
756 CONTENT_SETTINGS_TYPE_JAVASCRIPT
, NULL
));
758 // Remove managed-default-content-settings-preferences.
759 prefs
->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting
);
760 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
761 host_content_settings_map
->GetDefaultContentSetting(
762 CONTENT_SETTINGS_TYPE_JAVASCRIPT
, NULL
));
764 // Set preference to manage the default-content-setting for Plugins.
765 prefs
->SetManagedPref(prefs::kManagedDefaultPluginsSetting
,
766 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
767 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
768 host_content_settings_map
->GetDefaultContentSetting(
769 CONTENT_SETTINGS_TYPE_PLUGINS
, NULL
));
771 // Remove the preference to manage the default-content-setting for Plugins.
772 prefs
->RemoveManagedPref(prefs::kManagedDefaultPluginsSetting
);
773 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
774 host_content_settings_map
->GetDefaultContentSetting(
775 CONTENT_SETTINGS_TYPE_PLUGINS
, NULL
));
778 TEST_F(HostContentSettingsMapTest
,
779 GetNonDefaultContentSettingsIfTypeManaged
) {
780 TestingProfile profile
;
781 HostContentSettingsMap
* host_content_settings_map
=
782 profile
.GetHostContentSettingsMap();
783 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
785 // Set pattern for JavaScript setting.
786 ContentSettingsPattern pattern
=
787 ContentSettingsPattern::FromString("[*.]example.com");
788 host_content_settings_map
->SetContentSetting(
790 ContentSettingsPattern::Wildcard(),
791 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
793 CONTENT_SETTING_BLOCK
);
795 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
796 host_content_settings_map
->GetDefaultContentSetting(
797 CONTENT_SETTINGS_TYPE_JAVASCRIPT
, NULL
));
799 GURL
host("http://example.com/");
800 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
801 host_content_settings_map
->GetContentSetting(
802 host
, host
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
804 // Set managed-default-content-setting for content-settings-type JavaScript.
805 prefs
->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting
,
806 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
807 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
808 host_content_settings_map
->GetContentSetting(
809 host
, host
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
812 // Managed default content setting should have higher priority
813 // than user defined patterns.
814 TEST_F(HostContentSettingsMapTest
,
815 ManagedDefaultContentSettingIgnoreUserPattern
) {
816 TestingProfile profile
;
817 HostContentSettingsMap
* host_content_settings_map
=
818 profile
.GetHostContentSettingsMap();
819 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
821 // Block all JavaScript.
822 host_content_settings_map
->SetDefaultContentSetting(
823 CONTENT_SETTINGS_TYPE_JAVASCRIPT
, CONTENT_SETTING_BLOCK
);
825 // Set an exception to allow "[*.]example.com"
826 ContentSettingsPattern pattern
=
827 ContentSettingsPattern::FromString("[*.]example.com");
829 host_content_settings_map
->SetContentSetting(
831 ContentSettingsPattern::Wildcard(),
832 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
834 CONTENT_SETTING_ALLOW
);
836 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
837 host_content_settings_map
->GetDefaultContentSetting(
838 CONTENT_SETTINGS_TYPE_JAVASCRIPT
, NULL
));
839 GURL
host("http://example.com/");
840 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
841 host_content_settings_map
->GetContentSetting(
842 host
, host
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
844 // Set managed-default-content-settings-preferences.
845 prefs
->SetManagedPref(prefs::kManagedDefaultJavaScriptSetting
,
846 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
847 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
848 host_content_settings_map
->GetContentSetting(
849 host
, host
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
851 // Remove managed-default-content-settings-preferences.
852 prefs
->RemoveManagedPref(prefs::kManagedDefaultJavaScriptSetting
);
853 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
854 host_content_settings_map
->GetContentSetting(
855 host
, host
, CONTENT_SETTINGS_TYPE_JAVASCRIPT
, std::string()));
858 // If a default-content-setting is set to managed setting, the user defined
859 // setting should be preserved.
860 TEST_F(HostContentSettingsMapTest
, OverwrittenDefaultContentSetting
) {
861 TestingProfile profile
;
862 HostContentSettingsMap
* host_content_settings_map
=
863 profile
.GetHostContentSettingsMap();
864 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
866 // Set user defined default-content-setting for Cookies.
867 host_content_settings_map
->SetDefaultContentSetting(
868 CONTENT_SETTINGS_TYPE_COOKIES
, CONTENT_SETTING_BLOCK
);
869 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
870 host_content_settings_map
->GetDefaultContentSetting(
871 CONTENT_SETTINGS_TYPE_COOKIES
, NULL
));
873 // Set preference to manage the default-content-setting for Cookies.
874 prefs
->SetManagedPref(prefs::kManagedDefaultCookiesSetting
,
875 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
876 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
877 host_content_settings_map
->GetDefaultContentSetting(
878 CONTENT_SETTINGS_TYPE_COOKIES
, NULL
));
880 // Remove the preference to manage the default-content-setting for Cookies.
881 prefs
->RemoveManagedPref(prefs::kManagedDefaultCookiesSetting
);
882 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
883 host_content_settings_map
->GetDefaultContentSetting(
884 CONTENT_SETTINGS_TYPE_COOKIES
, NULL
));
887 // If a setting for a default-content-setting-type is set while the type is
888 // managed, then the new setting should be preserved and used after the
889 // default-content-setting-type is not managed anymore.
890 TEST_F(HostContentSettingsMapTest
, SettingDefaultContentSettingsWhenManaged
) {
891 TestingProfile profile
;
892 HostContentSettingsMap
* host_content_settings_map
=
893 profile
.GetHostContentSettingsMap();
894 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
896 prefs
->SetManagedPref(prefs::kManagedDefaultPluginsSetting
,
897 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
898 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
899 host_content_settings_map
->GetDefaultContentSetting(
900 CONTENT_SETTINGS_TYPE_PLUGINS
, NULL
));
902 host_content_settings_map
->SetDefaultContentSetting(
903 CONTENT_SETTINGS_TYPE_PLUGINS
, CONTENT_SETTING_BLOCK
);
904 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
905 host_content_settings_map
->GetDefaultContentSetting(
906 CONTENT_SETTINGS_TYPE_PLUGINS
, NULL
));
908 prefs
->RemoveManagedPref(prefs::kManagedDefaultPluginsSetting
);
909 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
910 host_content_settings_map
->GetDefaultContentSetting(
911 CONTENT_SETTINGS_TYPE_PLUGINS
, NULL
));
914 TEST_F(HostContentSettingsMapTest
, GetContentSetting
) {
915 TestingProfile profile
;
916 HostContentSettingsMap
* host_content_settings_map
=
917 profile
.GetHostContentSettingsMap();
919 GURL
host("http://example.com/");
920 GURL
embedder("chrome://foo");
921 ContentSettingsPattern pattern
=
922 ContentSettingsPattern::FromString("[*.]example.com");
923 host_content_settings_map
->SetContentSetting(
925 ContentSettingsPattern::Wildcard(),
926 CONTENT_SETTINGS_TYPE_IMAGES
,
928 CONTENT_SETTING_BLOCK
);
929 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
930 host_content_settings_map
->GetContentSetting(
931 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
932 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
933 host_content_settings_map
->GetContentSetting(
934 embedder
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
937 TEST_F(HostContentSettingsMapTest
, IsSettingAllowedForType
) {
938 TestingProfile profile
;
939 PrefService
* prefs
= profile
.GetPrefs();
941 EXPECT_TRUE(HostContentSettingsMap::IsSettingAllowedForType(
942 prefs
, CONTENT_SETTING_ASK
,
943 CONTENT_SETTINGS_TYPE_FULLSCREEN
));
945 // The mediastream setting is deprecated.
946 EXPECT_FALSE(HostContentSettingsMap::IsSettingAllowedForType(
947 prefs
, CONTENT_SETTING_ALLOW
,
948 CONTENT_SETTINGS_TYPE_MEDIASTREAM
));
949 EXPECT_FALSE(HostContentSettingsMap::IsSettingAllowedForType(
950 prefs
, CONTENT_SETTING_ASK
,
951 CONTENT_SETTINGS_TYPE_MEDIASTREAM
));
952 EXPECT_FALSE(HostContentSettingsMap::IsSettingAllowedForType(
953 prefs
, CONTENT_SETTING_BLOCK
,
954 CONTENT_SETTINGS_TYPE_MEDIASTREAM
));
956 // We support the ALLOW value for media permission exceptions,
957 // but not as the default setting.
958 EXPECT_TRUE(HostContentSettingsMap::IsSettingAllowedForType(
959 prefs
, CONTENT_SETTING_ALLOW
,
960 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
961 EXPECT_TRUE(HostContentSettingsMap::IsSettingAllowedForType(
962 prefs
, CONTENT_SETTING_ALLOW
,
963 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
964 EXPECT_FALSE(HostContentSettingsMap::IsDefaultSettingAllowedForType(
965 prefs
, CONTENT_SETTING_ALLOW
,
966 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
));
967 EXPECT_FALSE(HostContentSettingsMap::IsDefaultSettingAllowedForType(
968 prefs
, CONTENT_SETTING_ALLOW
,
969 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
));
971 // TODO(msramek): Add more checks for setting type - setting pairs where
972 // it is not obvious whether or not they are allowed.
975 TEST_F(HostContentSettingsMapTest
, AddContentSettingsObserver
) {
976 TestingProfile profile
;
977 HostContentSettingsMap
* host_content_settings_map
=
978 profile
.GetHostContentSettingsMap();
979 content_settings::MockObserver mock_observer
;
981 GURL
host("http://example.com/");
982 ContentSettingsPattern pattern
=
983 ContentSettingsPattern::FromString("[*.]example.com");
984 EXPECT_CALL(mock_observer
,
985 OnContentSettingChanged(pattern
,
986 ContentSettingsPattern::Wildcard(),
987 CONTENT_SETTINGS_TYPE_IMAGES
,
990 host_content_settings_map
->AddObserver(&mock_observer
);
992 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
993 host_content_settings_map
->GetContentSetting(
994 host
, host
, CONTENT_SETTINGS_TYPE_IMAGES
, std::string()));
995 host_content_settings_map
->SetContentSetting(
997 ContentSettingsPattern::Wildcard(),
998 CONTENT_SETTINGS_TYPE_IMAGES
,
1000 CONTENT_SETTING_DEFAULT
);