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 "components/content_settings/core/browser/content_settings_pref_provider.h"
7 #include "base/auto_reset.h"
8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/prefs/default_pref_store.h"
12 #include "base/prefs/overlay_user_pref_store.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/prefs/scoped_user_pref_update.h"
16 #include "base/prefs/testing_pref_store.h"
17 #include "base/test/simple_test_clock.h"
18 #include "base/threading/platform_thread.h"
19 #include "base/values.h"
20 #include "chrome/browser/content_settings/content_settings_mock_observer.h"
21 #include "chrome/browser/prefs/browser_prefs.h"
22 #include "chrome/browser/prefs/pref_service_mock_factory.h"
23 #include "chrome/browser/prefs/pref_service_syncable.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/common/url_constants.h"
27 #include "chrome/test/base/testing_pref_service_syncable.h"
28 #include "chrome/test/base/testing_profile.h"
29 #include "components/content_settings/core/browser/content_settings_pref.h"
30 #include "components/content_settings/core/browser/content_settings_utils.h"
31 #include "components/content_settings/core/test/content_settings_test_utils.h"
32 #include "components/pref_registry/pref_registry_syncable.h"
33 #include "testing/gtest/include/gtest/gtest.h"
38 namespace content_settings
{
40 class DeadlockCheckerThread
: public base::PlatformThread::Delegate
{
42 explicit DeadlockCheckerThread(PrefProvider
* provider
)
43 : provider_(provider
) {}
45 void ThreadMain() override
{
46 bool got_lock
= provider_
->content_settings_pref()->lock_
.Try();
47 EXPECT_TRUE(got_lock
);
49 provider_
->content_settings_pref()->lock_
.Release();
52 PrefProvider
* provider_
;
53 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerThread
);
56 // A helper for observing an preference changes and testing whether
57 // |PrefProvider| holds a lock when the preferences change.
58 class DeadlockCheckerObserver
{
60 // |DeadlockCheckerObserver| doesn't take the ownership of |prefs| or
62 DeadlockCheckerObserver(PrefService
* prefs
, PrefProvider
* provider
)
63 : provider_(provider
),
64 notification_received_(false) {
65 pref_change_registrar_
.Init(prefs
);
66 pref_change_registrar_
.Add(
67 prefs::kContentSettingsPatternPairs
,
69 &DeadlockCheckerObserver::OnContentSettingsPatternPairsChanged
,
70 base::Unretained(this)));
72 virtual ~DeadlockCheckerObserver() {}
74 bool notification_received() const {
75 return notification_received_
;
79 void OnContentSettingsPatternPairsChanged() {
80 // Check whether |provider_| holds its lock. For this, we need a
82 DeadlockCheckerThread
thread(provider_
);
83 base::PlatformThreadHandle handle
;
84 ASSERT_TRUE(base::PlatformThread::Create(0, &thread
, &handle
));
85 base::PlatformThread::Join(handle
);
86 notification_received_
= true;
89 PrefProvider
* provider_
;
90 PrefChangeRegistrar pref_change_registrar_
;
91 bool notification_received_
;
92 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerObserver
);
95 TEST(PrefProviderTest
, Observer
) {
96 TestingProfile profile
;
97 PrefProvider
pref_content_settings_provider(profile
.GetPrefs(), false);
99 ContentSettingsPattern pattern
=
100 ContentSettingsPattern::FromString("[*.]example.com");
101 content_settings::MockObserver mock_observer
;
102 EXPECT_CALL(mock_observer
,
103 OnContentSettingChanged(pattern
,
104 ContentSettingsPattern::Wildcard(),
105 CONTENT_SETTINGS_TYPE_IMAGES
,
108 pref_content_settings_provider
.AddObserver(&mock_observer
);
110 pref_content_settings_provider
.SetWebsiteSetting(
112 ContentSettingsPattern::Wildcard(),
113 CONTENT_SETTINGS_TYPE_IMAGES
,
115 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
117 pref_content_settings_provider
.ShutdownOnUIThread();
120 // Test for regression in which the PrefProvider modified the user pref store
121 // of the OTR unintentionally: http://crbug.com/74466.
122 TEST(PrefProviderTest
, Incognito
) {
123 PersistentPrefStore
* user_prefs
= new TestingPrefStore();
124 OverlayUserPrefStore
* otr_user_prefs
=
125 new OverlayUserPrefStore(user_prefs
);
127 PrefServiceMockFactory factory
;
128 factory
.set_user_prefs(make_scoped_refptr(user_prefs
));
129 scoped_refptr
<user_prefs::PrefRegistrySyncable
> registry(
130 new user_prefs::PrefRegistrySyncable
);
131 PrefServiceSyncable
* regular_prefs
=
132 factory
.CreateSyncable(registry
.get()).release();
134 chrome::RegisterUserProfilePrefs(registry
.get());
136 PrefServiceMockFactory otr_factory
;
137 otr_factory
.set_user_prefs(make_scoped_refptr(otr_user_prefs
));
138 scoped_refptr
<user_prefs::PrefRegistrySyncable
> otr_registry(
139 new user_prefs::PrefRegistrySyncable
);
140 PrefServiceSyncable
* otr_prefs
=
141 otr_factory
.CreateSyncable(otr_registry
.get()).release();
143 chrome::RegisterUserProfilePrefs(otr_registry
.get());
145 TestingProfile::Builder profile_builder
;
146 profile_builder
.SetPrefService(make_scoped_ptr(regular_prefs
));
147 scoped_ptr
<TestingProfile
> profile
= profile_builder
.Build();
149 TestingProfile::Builder otr_profile_builder
;
150 otr_profile_builder
.SetPrefService(make_scoped_ptr(otr_prefs
));
151 otr_profile_builder
.BuildIncognito(profile
.get());
153 PrefProvider
pref_content_settings_provider(regular_prefs
, false);
154 PrefProvider
pref_content_settings_provider_incognito(otr_prefs
, true);
155 ContentSettingsPattern pattern
=
156 ContentSettingsPattern::FromString("[*.]example.com");
157 pref_content_settings_provider
.SetWebsiteSetting(
160 CONTENT_SETTINGS_TYPE_IMAGES
,
162 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
164 GURL
host("http://example.com/");
165 // The value should of course be visible in the regular PrefProvider.
166 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
167 GetContentSetting(&pref_content_settings_provider
,
170 CONTENT_SETTINGS_TYPE_IMAGES
,
173 // And also in the OTR version.
174 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
175 GetContentSetting(&pref_content_settings_provider_incognito
,
178 CONTENT_SETTINGS_TYPE_IMAGES
,
181 // But the value should not be overridden in the OTR user prefs accidentally.
182 EXPECT_FALSE(otr_user_prefs
->IsSetInOverlay(
183 prefs::kContentSettingsPatternPairs
));
185 pref_content_settings_provider
.ShutdownOnUIThread();
186 pref_content_settings_provider_incognito
.ShutdownOnUIThread();
189 TEST(PrefProviderTest
, GetContentSettingsValue
) {
190 TestingProfile testing_profile
;
191 PrefProvider
provider(testing_profile
.GetPrefs(), false);
193 GURL
primary_url("http://example.com/");
194 ContentSettingsPattern primary_pattern
=
195 ContentSettingsPattern::FromString("[*.]example.com");
197 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
198 GetContentSetting(&provider
,
201 CONTENT_SETTINGS_TYPE_IMAGES
,
206 GetContentSettingValue(&provider
,
209 CONTENT_SETTINGS_TYPE_IMAGES
,
213 provider
.SetWebsiteSetting(primary_pattern
,
215 CONTENT_SETTINGS_TYPE_IMAGES
,
217 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
218 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
219 GetContentSetting(&provider
,
222 CONTENT_SETTINGS_TYPE_IMAGES
,
225 scoped_ptr
<base::Value
> value_ptr(
226 GetContentSettingValue(&provider
,
229 CONTENT_SETTINGS_TYPE_IMAGES
,
233 value_ptr
->GetAsInteger(&int_value
);
234 EXPECT_EQ(CONTENT_SETTING_BLOCK
, IntToContentSetting(int_value
));
236 provider
.SetWebsiteSetting(primary_pattern
,
238 CONTENT_SETTINGS_TYPE_IMAGES
,
242 GetContentSettingValue(&provider
,
245 CONTENT_SETTINGS_TYPE_IMAGES
,
248 provider
.ShutdownOnUIThread();
251 TEST(PrefProviderTest
, Patterns
) {
252 TestingProfile testing_profile
;
253 PrefProvider
pref_content_settings_provider(testing_profile
.GetPrefs(),
256 GURL
host1("http://example.com/");
257 GURL
host2("http://www.example.com/");
258 GURL
host3("http://example.org/");
259 GURL
host4("file:///tmp/test.html");
260 ContentSettingsPattern pattern1
=
261 ContentSettingsPattern::FromString("[*.]example.com");
262 ContentSettingsPattern pattern2
=
263 ContentSettingsPattern::FromString("example.org");
264 ContentSettingsPattern pattern3
=
265 ContentSettingsPattern::FromString("file:///tmp/test.html");
267 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
268 GetContentSetting(&pref_content_settings_provider
,
271 CONTENT_SETTINGS_TYPE_IMAGES
,
274 pref_content_settings_provider
.SetWebsiteSetting(
277 CONTENT_SETTINGS_TYPE_IMAGES
,
279 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
280 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
281 GetContentSetting(&pref_content_settings_provider
,
284 CONTENT_SETTINGS_TYPE_IMAGES
,
287 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
288 GetContentSetting(&pref_content_settings_provider
,
291 CONTENT_SETTINGS_TYPE_IMAGES
,
295 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
296 GetContentSetting(&pref_content_settings_provider
,
299 CONTENT_SETTINGS_TYPE_IMAGES
,
302 pref_content_settings_provider
.SetWebsiteSetting(
305 CONTENT_SETTINGS_TYPE_IMAGES
,
307 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
308 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
309 GetContentSetting(&pref_content_settings_provider
,
312 CONTENT_SETTINGS_TYPE_IMAGES
,
316 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
317 GetContentSetting(&pref_content_settings_provider
,
320 CONTENT_SETTINGS_TYPE_IMAGES
,
323 pref_content_settings_provider
.SetWebsiteSetting(
326 CONTENT_SETTINGS_TYPE_IMAGES
,
328 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
329 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
330 GetContentSetting(&pref_content_settings_provider
,
333 CONTENT_SETTINGS_TYPE_IMAGES
,
337 pref_content_settings_provider
.ShutdownOnUIThread();
340 TEST(PrefProviderTest
, ResourceIdentifier
) {
341 TestingProfile testing_profile
;
342 PrefProvider
pref_content_settings_provider(testing_profile
.GetPrefs(),
345 GURL
host("http://example.com/");
346 ContentSettingsPattern pattern
=
347 ContentSettingsPattern::FromString("[*.]example.com");
348 std::string
resource1("someplugin");
349 std::string
resource2("otherplugin");
351 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
353 &pref_content_settings_provider
,
354 host
, host
, CONTENT_SETTINGS_TYPE_PLUGINS
,
356 pref_content_settings_provider
.SetWebsiteSetting(
359 CONTENT_SETTINGS_TYPE_PLUGINS
,
361 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
362 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
364 &pref_content_settings_provider
,
365 host
, host
, CONTENT_SETTINGS_TYPE_PLUGINS
,
367 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
369 &pref_content_settings_provider
,
370 host
, host
, CONTENT_SETTINGS_TYPE_PLUGINS
,
373 pref_content_settings_provider
.ShutdownOnUIThread();
376 TEST(PrefProviderTest
, AutoSubmitCertificateContentSetting
) {
377 TestingProfile profile
;
378 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
379 GURL
primary_url("https://www.example.com");
380 GURL
secondary_url("https://www.sample.com");
382 PrefProvider
provider(prefs
, false);
384 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
389 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
393 provider
.SetWebsiteSetting(ContentSettingsPattern::FromURL(primary_url
),
394 ContentSettingsPattern::Wildcard(),
395 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
397 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
398 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
403 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
406 provider
.ShutdownOnUIThread();
409 // http://crosbug.com/17760
410 TEST(PrefProviderTest
, Deadlock
) {
411 TestingPrefServiceSyncable prefs
;
412 PrefProvider::RegisterProfilePrefs(prefs
.registry());
414 // Chain of events: a preference changes, |PrefProvider| notices it, and reads
415 // and writes the preference. When the preference is written, a notification
416 // is sent, and this used to happen when |PrefProvider| was still holding its
419 PrefProvider
provider(&prefs
, false);
420 DeadlockCheckerObserver
observer(&prefs
, &provider
);
422 DictionaryPrefUpdate
update(&prefs
,
423 prefs::kContentSettingsPatternPairs
);
424 base::DictionaryValue
* mutable_settings
= update
.Get();
425 mutable_settings
->SetWithoutPathExpansion("www.example.com,*",
426 new base::DictionaryValue());
428 EXPECT_TRUE(observer
.notification_received());
430 provider
.ShutdownOnUIThread();
433 TEST(PrefProviderTest
, LastUsage
) {
434 TestingProfile testing_profile
;
435 PrefProvider
pref_content_settings_provider(testing_profile
.GetPrefs(),
437 base::SimpleTestClock
* test_clock
= new base::SimpleTestClock
;
438 test_clock
->SetNow(base::Time::Now());
440 pref_content_settings_provider
.SetClockForTesting(
441 scoped_ptr
<base::Clock
>(test_clock
));
442 GURL
host("http://example.com/");
443 ContentSettingsPattern pattern
=
444 ContentSettingsPattern::FromString("[*.]example.com");
446 base::Time no_usage
= pref_content_settings_provider
.GetLastUsage(
447 pattern
, pattern
, CONTENT_SETTINGS_TYPE_GEOLOCATION
);
448 EXPECT_EQ(no_usage
.ToDoubleT(), 0);
450 pref_content_settings_provider
.UpdateLastUsage(
451 pattern
, pattern
, CONTENT_SETTINGS_TYPE_GEOLOCATION
);
452 base::Time first
= pref_content_settings_provider
.GetLastUsage(
453 pattern
, pattern
, CONTENT_SETTINGS_TYPE_GEOLOCATION
);
455 test_clock
->Advance(base::TimeDelta::FromSeconds(10));
457 pref_content_settings_provider
.UpdateLastUsage(
458 pattern
, pattern
, CONTENT_SETTINGS_TYPE_GEOLOCATION
);
459 base::Time second
= pref_content_settings_provider
.GetLastUsage(
460 pattern
, pattern
, CONTENT_SETTINGS_TYPE_GEOLOCATION
);
462 base::TimeDelta delta
= second
- first
;
463 EXPECT_EQ(delta
.InSeconds(), 10);
465 pref_content_settings_provider
.ShutdownOnUIThread();
468 } // namespace content_settings