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 "chrome/browser/content_settings/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/content_settings/content_settings_utils.h"
22 #include "chrome/browser/prefs/browser_prefs.h"
23 #include "chrome/browser/prefs/pref_service_mock_factory.h"
24 #include "chrome/browser/prefs/pref_service_syncable.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/pref_names.h"
27 #include "chrome/common/url_constants.h"
28 #include "chrome/test/base/testing_pref_service_syncable.h"
29 #include "chrome/test/base/testing_profile.h"
30 #include "components/pref_registry/pref_registry_syncable.h"
31 #include "content/public/test/test_browser_thread.h"
32 #include "testing/gtest/include/gtest/gtest.h"
36 using content::BrowserThread
;
38 namespace content_settings
{
40 class DeadlockCheckerThread
: public base::PlatformThread::Delegate
{
42 explicit DeadlockCheckerThread(PrefProvider
* provider
)
43 : provider_(provider
) {}
45 virtual void ThreadMain() OVERRIDE
{
46 bool got_lock
= provider_
->lock_
.Try();
47 EXPECT_TRUE(got_lock
);
49 provider_
->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 class PrefProviderTest
: public testing::Test
{
97 PrefProviderTest() : ui_thread_(
98 BrowserThread::UI
, &message_loop_
) {
102 base::MessageLoop message_loop_
;
103 content::TestBrowserThread ui_thread_
;
106 TEST_F(PrefProviderTest
, Observer
) {
107 TestingProfile profile
;
108 PrefProvider
pref_content_settings_provider(profile
.GetPrefs(), false);
110 ContentSettingsPattern pattern
=
111 ContentSettingsPattern::FromString("[*.]example.com");
112 content_settings::MockObserver mock_observer
;
113 EXPECT_CALL(mock_observer
,
114 OnContentSettingChanged(pattern
,
115 ContentSettingsPattern::Wildcard(),
116 CONTENT_SETTINGS_TYPE_IMAGES
,
119 pref_content_settings_provider
.AddObserver(&mock_observer
);
121 pref_content_settings_provider
.SetWebsiteSetting(
123 ContentSettingsPattern::Wildcard(),
124 CONTENT_SETTINGS_TYPE_IMAGES
,
126 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
128 pref_content_settings_provider
.ShutdownOnUIThread();
131 // Test for regression in which the PrefProvider modified the user pref store
132 // of the OTR unintentionally: http://crbug.com/74466.
133 TEST_F(PrefProviderTest
, Incognito
) {
134 PersistentPrefStore
* user_prefs
= new TestingPrefStore();
135 OverlayUserPrefStore
* otr_user_prefs
=
136 new OverlayUserPrefStore(user_prefs
);
138 PrefServiceMockFactory factory
;
139 factory
.set_user_prefs(make_scoped_refptr(user_prefs
));
140 scoped_refptr
<user_prefs::PrefRegistrySyncable
> registry(
141 new user_prefs::PrefRegistrySyncable
);
142 PrefServiceSyncable
* regular_prefs
=
143 factory
.CreateSyncable(registry
.get()).release();
145 chrome::RegisterUserProfilePrefs(registry
.get());
147 PrefServiceMockFactory otr_factory
;
148 otr_factory
.set_user_prefs(make_scoped_refptr(otr_user_prefs
));
149 scoped_refptr
<user_prefs::PrefRegistrySyncable
> otr_registry(
150 new user_prefs::PrefRegistrySyncable
);
151 PrefServiceSyncable
* otr_prefs
=
152 otr_factory
.CreateSyncable(otr_registry
.get()).release();
154 chrome::RegisterUserProfilePrefs(otr_registry
.get());
156 TestingProfile::Builder profile_builder
;
157 profile_builder
.SetPrefService(make_scoped_ptr(regular_prefs
));
158 scoped_ptr
<TestingProfile
> profile
= profile_builder
.Build();
160 TestingProfile::Builder otr_profile_builder
;
161 otr_profile_builder
.SetIncognito();
162 otr_profile_builder
.SetPrefService(make_scoped_ptr(otr_prefs
));
163 scoped_ptr
<TestingProfile
> otr_profile(otr_profile_builder
.Build());
164 profile
->SetOffTheRecordProfile(otr_profile
.PassAs
<Profile
>());
166 PrefProvider
pref_content_settings_provider(regular_prefs
, false);
167 PrefProvider
pref_content_settings_provider_incognito(otr_prefs
, true);
168 ContentSettingsPattern pattern
=
169 ContentSettingsPattern::FromString("[*.]example.com");
170 pref_content_settings_provider
.SetWebsiteSetting(
173 CONTENT_SETTINGS_TYPE_IMAGES
,
175 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
177 GURL
host("http://example.com/");
178 // The value should of course be visible in the regular PrefProvider.
179 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
180 GetContentSetting(&pref_content_settings_provider
,
183 CONTENT_SETTINGS_TYPE_IMAGES
,
186 // And also in the OTR version.
187 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
188 GetContentSetting(&pref_content_settings_provider_incognito
,
191 CONTENT_SETTINGS_TYPE_IMAGES
,
194 // But the value should not be overridden in the OTR user prefs accidentally.
195 EXPECT_FALSE(otr_user_prefs
->IsSetInOverlay(
196 prefs::kContentSettingsPatternPairs
));
198 pref_content_settings_provider
.ShutdownOnUIThread();
199 pref_content_settings_provider_incognito
.ShutdownOnUIThread();
202 TEST_F(PrefProviderTest
, GetContentSettingsValue
) {
203 TestingProfile testing_profile
;
204 PrefProvider
provider(testing_profile
.GetPrefs(), false);
206 GURL
primary_url("http://example.com/");
207 ContentSettingsPattern primary_pattern
=
208 ContentSettingsPattern::FromString("[*.]example.com");
210 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
211 GetContentSetting(&provider
,
214 CONTENT_SETTINGS_TYPE_IMAGES
,
219 GetContentSettingValue(&provider
,
222 CONTENT_SETTINGS_TYPE_IMAGES
,
226 provider
.SetWebsiteSetting(primary_pattern
,
228 CONTENT_SETTINGS_TYPE_IMAGES
,
230 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
231 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
232 GetContentSetting(&provider
,
235 CONTENT_SETTINGS_TYPE_IMAGES
,
238 scoped_ptr
<base::Value
> value_ptr(
239 GetContentSettingValue(&provider
,
242 CONTENT_SETTINGS_TYPE_IMAGES
,
246 value_ptr
->GetAsInteger(&int_value
);
247 EXPECT_EQ(CONTENT_SETTING_BLOCK
, IntToContentSetting(int_value
));
249 provider
.SetWebsiteSetting(primary_pattern
,
251 CONTENT_SETTINGS_TYPE_IMAGES
,
255 GetContentSettingValue(&provider
,
258 CONTENT_SETTINGS_TYPE_IMAGES
,
261 provider
.ShutdownOnUIThread();
264 TEST_F(PrefProviderTest
, Patterns
) {
265 TestingProfile testing_profile
;
266 PrefProvider
pref_content_settings_provider(testing_profile
.GetPrefs(),
269 GURL
host1("http://example.com/");
270 GURL
host2("http://www.example.com/");
271 GURL
host3("http://example.org/");
272 GURL
host4("file:///tmp/test.html");
273 ContentSettingsPattern pattern1
=
274 ContentSettingsPattern::FromString("[*.]example.com");
275 ContentSettingsPattern pattern2
=
276 ContentSettingsPattern::FromString("example.org");
277 ContentSettingsPattern pattern3
=
278 ContentSettingsPattern::FromString("file:///tmp/test.html");
280 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
281 GetContentSetting(&pref_content_settings_provider
,
284 CONTENT_SETTINGS_TYPE_IMAGES
,
287 pref_content_settings_provider
.SetWebsiteSetting(
290 CONTENT_SETTINGS_TYPE_IMAGES
,
292 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
293 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
294 GetContentSetting(&pref_content_settings_provider
,
297 CONTENT_SETTINGS_TYPE_IMAGES
,
300 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
301 GetContentSetting(&pref_content_settings_provider
,
304 CONTENT_SETTINGS_TYPE_IMAGES
,
308 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
309 GetContentSetting(&pref_content_settings_provider
,
312 CONTENT_SETTINGS_TYPE_IMAGES
,
315 pref_content_settings_provider
.SetWebsiteSetting(
318 CONTENT_SETTINGS_TYPE_IMAGES
,
320 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
321 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
322 GetContentSetting(&pref_content_settings_provider
,
325 CONTENT_SETTINGS_TYPE_IMAGES
,
329 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
330 GetContentSetting(&pref_content_settings_provider
,
333 CONTENT_SETTINGS_TYPE_IMAGES
,
336 pref_content_settings_provider
.SetWebsiteSetting(
339 CONTENT_SETTINGS_TYPE_IMAGES
,
341 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
342 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
343 GetContentSetting(&pref_content_settings_provider
,
346 CONTENT_SETTINGS_TYPE_IMAGES
,
350 pref_content_settings_provider
.ShutdownOnUIThread();
353 TEST_F(PrefProviderTest
, ResourceIdentifier
) {
354 TestingProfile testing_profile
;
355 PrefProvider
pref_content_settings_provider(testing_profile
.GetPrefs(),
358 GURL
host("http://example.com/");
359 ContentSettingsPattern pattern
=
360 ContentSettingsPattern::FromString("[*.]example.com");
361 std::string
resource1("someplugin");
362 std::string
resource2("otherplugin");
364 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
366 &pref_content_settings_provider
,
367 host
, host
, CONTENT_SETTINGS_TYPE_PLUGINS
,
369 pref_content_settings_provider
.SetWebsiteSetting(
372 CONTENT_SETTINGS_TYPE_PLUGINS
,
374 new base::FundamentalValue(CONTENT_SETTING_BLOCK
));
375 EXPECT_EQ(CONTENT_SETTING_BLOCK
,
377 &pref_content_settings_provider
,
378 host
, host
, CONTENT_SETTINGS_TYPE_PLUGINS
,
380 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
382 &pref_content_settings_provider
,
383 host
, host
, CONTENT_SETTINGS_TYPE_PLUGINS
,
386 pref_content_settings_provider
.ShutdownOnUIThread();
389 TEST_F(PrefProviderTest
, AutoSubmitCertificateContentSetting
) {
390 TestingProfile profile
;
391 TestingPrefServiceSyncable
* prefs
= profile
.GetTestingPrefService();
392 GURL
primary_url("https://www.example.com");
393 GURL
secondary_url("https://www.sample.com");
395 PrefProvider
provider(prefs
, false);
397 EXPECT_EQ(CONTENT_SETTING_DEFAULT
,
402 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
406 provider
.SetWebsiteSetting(ContentSettingsPattern::FromURL(primary_url
),
407 ContentSettingsPattern::Wildcard(),
408 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
410 new base::FundamentalValue(CONTENT_SETTING_ALLOW
));
411 EXPECT_EQ(CONTENT_SETTING_ALLOW
,
416 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE
,
419 provider
.ShutdownOnUIThread();
422 // http://crosbug.com/17760
423 TEST_F(PrefProviderTest
, Deadlock
) {
424 TestingPrefServiceSyncable prefs
;
425 PrefProvider::RegisterProfilePrefs(prefs
.registry());
427 // Chain of events: a preference changes, |PrefProvider| notices it, and reads
428 // and writes the preference. When the preference is written, a notification
429 // is sent, and this used to happen when |PrefProvider| was still holding its
432 PrefProvider
provider(&prefs
, false);
433 DeadlockCheckerObserver
observer(&prefs
, &provider
);
435 DictionaryPrefUpdate
update(&prefs
,
436 prefs::kContentSettingsPatternPairs
);
437 base::DictionaryValue
* mutable_settings
= update
.Get();
438 mutable_settings
->SetWithoutPathExpansion("www.example.com,*",
439 new base::DictionaryValue());
441 EXPECT_TRUE(observer
.notification_received());
443 provider
.ShutdownOnUIThread();
446 TEST_F(PrefProviderTest
, LastUsage
) {
447 TestingProfile testing_profile
;
448 PrefProvider
pref_content_settings_provider(testing_profile
.GetPrefs(),
450 base::SimpleTestClock
* test_clock
= new base::SimpleTestClock
;
451 test_clock
->SetNow(base::Time::Now());
453 pref_content_settings_provider
.SetClockForTesting(
454 scoped_ptr
<base::Clock
>(test_clock
));
455 GURL
host("http://example.com/");
456 ContentSettingsPattern pattern
=
457 ContentSettingsPattern::FromString("[*.]example.com");
459 base::Time no_usage
= pref_content_settings_provider
.GetLastUsage(
460 pattern
, pattern
, CONTENT_SETTINGS_TYPE_GEOLOCATION
);
461 EXPECT_EQ(no_usage
.ToDoubleT(), 0);
463 pref_content_settings_provider
.UpdateLastUsage(
464 pattern
, pattern
, CONTENT_SETTINGS_TYPE_GEOLOCATION
);
465 base::Time first
= pref_content_settings_provider
.GetLastUsage(
466 pattern
, pattern
, CONTENT_SETTINGS_TYPE_GEOLOCATION
);
468 test_clock
->Advance(base::TimeDelta::FromSeconds(10));
470 pref_content_settings_provider
.UpdateLastUsage(
471 pattern
, pattern
, CONTENT_SETTINGS_TYPE_GEOLOCATION
);
472 base::Time second
= pref_content_settings_provider
.GetLastUsage(
473 pattern
, pattern
, CONTENT_SETTINGS_TYPE_GEOLOCATION
);
475 base::TimeDelta delta
= second
- first
;
476 EXPECT_EQ(delta
.InSeconds(), 10);
478 pref_content_settings_provider
.ShutdownOnUIThread();
481 } // namespace content_settings