OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/content_settings/content_settings_pref_provider.h" | 5 #include "chrome/browser/content_settings/content_settings_pref_provider.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/prefs/default_pref_store.h" | 10 #include "base/prefs/default_pref_store.h" |
12 #include "base/prefs/overlay_user_pref_store.h" | 11 #include "base/prefs/overlay_user_pref_store.h" |
13 #include "base/prefs/pref_change_registrar.h" | 12 #include "base/prefs/pref_change_registrar.h" |
14 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
15 #include "base/prefs/scoped_user_pref_update.h" | 14 #include "base/prefs/scoped_user_pref_update.h" |
16 #include "base/prefs/testing_pref_store.h" | 15 #include "base/prefs/testing_pref_store.h" |
17 #include "base/test/simple_test_clock.h" | 16 #include "base/test/simple_test_clock.h" |
18 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
19 #include "base/values.h" | 18 #include "base/values.h" |
20 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 19 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
21 #include "chrome/browser/content_settings/content_settings_utils.h" | 20 #include "chrome/browser/content_settings/content_settings_utils.h" |
22 #include "chrome/browser/prefs/browser_prefs.h" | 21 #include "chrome/browser/prefs/browser_prefs.h" |
23 #include "chrome/browser/prefs/pref_service_mock_factory.h" | 22 #include "chrome/browser/prefs/pref_service_mock_factory.h" |
24 #include "chrome/browser/prefs/pref_service_syncable.h" | 23 #include "chrome/browser/prefs/pref_service_syncable.h" |
25 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
26 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
27 #include "chrome/common/url_constants.h" | 26 #include "chrome/common/url_constants.h" |
28 #include "chrome/test/base/testing_pref_service_syncable.h" | 27 #include "chrome/test/base/testing_pref_service_syncable.h" |
29 #include "chrome/test/base/testing_profile.h" | 28 #include "chrome/test/base/testing_profile.h" |
30 #include "components/pref_registry/pref_registry_syncable.h" | 29 #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" | 30 #include "testing/gtest/include/gtest/gtest.h" |
33 #include "url/gurl.h" | 31 #include "url/gurl.h" |
34 | 32 |
35 using ::testing::_; | 33 using ::testing::_; |
36 using content::BrowserThread; | |
37 | 34 |
38 namespace content_settings { | 35 namespace content_settings { |
39 | 36 |
40 class DeadlockCheckerThread : public base::PlatformThread::Delegate { | 37 class DeadlockCheckerThread : public base::PlatformThread::Delegate { |
41 public: | 38 public: |
42 explicit DeadlockCheckerThread(PrefProvider* provider) | 39 explicit DeadlockCheckerThread(PrefProvider* provider) |
43 : provider_(provider) {} | 40 : provider_(provider) {} |
44 | 41 |
45 virtual void ThreadMain() OVERRIDE { | 42 virtual void ThreadMain() OVERRIDE { |
46 bool got_lock = provider_->lock_.Try(); | 43 bool got_lock = provider_->lock_.Try(); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 base::PlatformThread::Join(handle); | 82 base::PlatformThread::Join(handle); |
86 notification_received_ = true; | 83 notification_received_ = true; |
87 } | 84 } |
88 | 85 |
89 PrefProvider* provider_; | 86 PrefProvider* provider_; |
90 PrefChangeRegistrar pref_change_registrar_; | 87 PrefChangeRegistrar pref_change_registrar_; |
91 bool notification_received_; | 88 bool notification_received_; |
92 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerObserver); | 89 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerObserver); |
93 }; | 90 }; |
94 | 91 |
95 class PrefProviderTest : public testing::Test { | 92 TEST(PrefProviderTest, Observer) { |
96 public: | |
97 PrefProviderTest() : ui_thread_( | |
98 BrowserThread::UI, &message_loop_) { | |
99 } | |
100 | |
101 protected: | |
102 base::MessageLoop message_loop_; | |
103 content::TestBrowserThread ui_thread_; | |
104 }; | |
105 | |
106 TEST_F(PrefProviderTest, Observer) { | |
107 TestingProfile profile; | 93 TestingProfile profile; |
108 PrefProvider pref_content_settings_provider(profile.GetPrefs(), false); | 94 PrefProvider pref_content_settings_provider(profile.GetPrefs(), false); |
109 | 95 |
110 ContentSettingsPattern pattern = | 96 ContentSettingsPattern pattern = |
111 ContentSettingsPattern::FromString("[*.]example.com"); | 97 ContentSettingsPattern::FromString("[*.]example.com"); |
112 content_settings::MockObserver mock_observer; | 98 content_settings::MockObserver mock_observer; |
113 EXPECT_CALL(mock_observer, | 99 EXPECT_CALL(mock_observer, |
114 OnContentSettingChanged(pattern, | 100 OnContentSettingChanged(pattern, |
115 ContentSettingsPattern::Wildcard(), | 101 ContentSettingsPattern::Wildcard(), |
116 CONTENT_SETTINGS_TYPE_IMAGES, | 102 CONTENT_SETTINGS_TYPE_IMAGES, |
117 "")); | 103 "")); |
118 | 104 |
119 pref_content_settings_provider.AddObserver(&mock_observer); | 105 pref_content_settings_provider.AddObserver(&mock_observer); |
120 | 106 |
121 pref_content_settings_provider.SetWebsiteSetting( | 107 pref_content_settings_provider.SetWebsiteSetting( |
122 pattern, | 108 pattern, |
123 ContentSettingsPattern::Wildcard(), | 109 ContentSettingsPattern::Wildcard(), |
124 CONTENT_SETTINGS_TYPE_IMAGES, | 110 CONTENT_SETTINGS_TYPE_IMAGES, |
125 std::string(), | 111 std::string(), |
126 new base::FundamentalValue(CONTENT_SETTING_ALLOW)); | 112 new base::FundamentalValue(CONTENT_SETTING_ALLOW)); |
127 | 113 |
128 pref_content_settings_provider.ShutdownOnUIThread(); | 114 pref_content_settings_provider.ShutdownOnUIThread(); |
129 } | 115 } |
130 | 116 |
131 // Test for regression in which the PrefProvider modified the user pref store | 117 // Test for regression in which the PrefProvider modified the user pref store |
132 // of the OTR unintentionally: http://crbug.com/74466. | 118 // of the OTR unintentionally: http://crbug.com/74466. |
133 TEST_F(PrefProviderTest, Incognito) { | 119 TEST(PrefProviderTest, Incognito) { |
134 PersistentPrefStore* user_prefs = new TestingPrefStore(); | 120 PersistentPrefStore* user_prefs = new TestingPrefStore(); |
135 OverlayUserPrefStore* otr_user_prefs = | 121 OverlayUserPrefStore* otr_user_prefs = |
136 new OverlayUserPrefStore(user_prefs); | 122 new OverlayUserPrefStore(user_prefs); |
137 | 123 |
138 PrefServiceMockFactory factory; | 124 PrefServiceMockFactory factory; |
139 factory.set_user_prefs(make_scoped_refptr(user_prefs)); | 125 factory.set_user_prefs(make_scoped_refptr(user_prefs)); |
140 scoped_refptr<user_prefs::PrefRegistrySyncable> registry( | 126 scoped_refptr<user_prefs::PrefRegistrySyncable> registry( |
141 new user_prefs::PrefRegistrySyncable); | 127 new user_prefs::PrefRegistrySyncable); |
142 PrefServiceSyncable* regular_prefs = | 128 PrefServiceSyncable* regular_prefs = |
143 factory.CreateSyncable(registry.get()).release(); | 129 factory.CreateSyncable(registry.get()).release(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 std::string(), | 176 std::string(), |
191 false)); | 177 false)); |
192 // But the value should not be overridden in the OTR user prefs accidentally. | 178 // But the value should not be overridden in the OTR user prefs accidentally. |
193 EXPECT_FALSE(otr_user_prefs->IsSetInOverlay( | 179 EXPECT_FALSE(otr_user_prefs->IsSetInOverlay( |
194 prefs::kContentSettingsPatternPairs)); | 180 prefs::kContentSettingsPatternPairs)); |
195 | 181 |
196 pref_content_settings_provider.ShutdownOnUIThread(); | 182 pref_content_settings_provider.ShutdownOnUIThread(); |
197 pref_content_settings_provider_incognito.ShutdownOnUIThread(); | 183 pref_content_settings_provider_incognito.ShutdownOnUIThread(); |
198 } | 184 } |
199 | 185 |
200 TEST_F(PrefProviderTest, GetContentSettingsValue) { | 186 TEST(PrefProviderTest, GetContentSettingsValue) { |
201 TestingProfile testing_profile; | 187 TestingProfile testing_profile; |
202 PrefProvider provider(testing_profile.GetPrefs(), false); | 188 PrefProvider provider(testing_profile.GetPrefs(), false); |
203 | 189 |
204 GURL primary_url("http://example.com/"); | 190 GURL primary_url("http://example.com/"); |
205 ContentSettingsPattern primary_pattern = | 191 ContentSettingsPattern primary_pattern = |
206 ContentSettingsPattern::FromString("[*.]example.com"); | 192 ContentSettingsPattern::FromString("[*.]example.com"); |
207 | 193 |
208 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 194 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
209 GetContentSetting(&provider, | 195 GetContentSetting(&provider, |
210 primary_url, | 196 primary_url, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 EXPECT_EQ(NULL, | 238 EXPECT_EQ(NULL, |
253 GetContentSettingValue(&provider, | 239 GetContentSettingValue(&provider, |
254 primary_url, | 240 primary_url, |
255 primary_url, | 241 primary_url, |
256 CONTENT_SETTINGS_TYPE_IMAGES, | 242 CONTENT_SETTINGS_TYPE_IMAGES, |
257 std::string(), | 243 std::string(), |
258 false)); | 244 false)); |
259 provider.ShutdownOnUIThread(); | 245 provider.ShutdownOnUIThread(); |
260 } | 246 } |
261 | 247 |
262 TEST_F(PrefProviderTest, Patterns) { | 248 TEST(PrefProviderTest, Patterns) { |
263 TestingProfile testing_profile; | 249 TestingProfile testing_profile; |
264 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), | 250 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), |
265 false); | 251 false); |
266 | 252 |
267 GURL host1("http://example.com/"); | 253 GURL host1("http://example.com/"); |
268 GURL host2("http://www.example.com/"); | 254 GURL host2("http://www.example.com/"); |
269 GURL host3("http://example.org/"); | 255 GURL host3("http://example.org/"); |
270 GURL host4("file:///tmp/test.html"); | 256 GURL host4("file:///tmp/test.html"); |
271 ContentSettingsPattern pattern1 = | 257 ContentSettingsPattern pattern1 = |
272 ContentSettingsPattern::FromString("[*.]example.com"); | 258 ContentSettingsPattern::FromString("[*.]example.com"); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 GetContentSetting(&pref_content_settings_provider, | 327 GetContentSetting(&pref_content_settings_provider, |
342 host4, | 328 host4, |
343 host4, | 329 host4, |
344 CONTENT_SETTINGS_TYPE_IMAGES, | 330 CONTENT_SETTINGS_TYPE_IMAGES, |
345 std::string(), | 331 std::string(), |
346 false)); | 332 false)); |
347 | 333 |
348 pref_content_settings_provider.ShutdownOnUIThread(); | 334 pref_content_settings_provider.ShutdownOnUIThread(); |
349 } | 335 } |
350 | 336 |
351 TEST_F(PrefProviderTest, ResourceIdentifier) { | 337 TEST(PrefProviderTest, ResourceIdentifier) { |
352 TestingProfile testing_profile; | 338 TestingProfile testing_profile; |
353 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), | 339 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), |
354 false); | 340 false); |
355 | 341 |
356 GURL host("http://example.com/"); | 342 GURL host("http://example.com/"); |
357 ContentSettingsPattern pattern = | 343 ContentSettingsPattern pattern = |
358 ContentSettingsPattern::FromString("[*.]example.com"); | 344 ContentSettingsPattern::FromString("[*.]example.com"); |
359 std::string resource1("someplugin"); | 345 std::string resource1("someplugin"); |
360 std::string resource2("otherplugin"); | 346 std::string resource2("otherplugin"); |
361 | 347 |
(...skipping 15 matching lines...) Expand all Loading... |
377 resource1, false)); | 363 resource1, false)); |
378 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 364 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
379 GetContentSetting( | 365 GetContentSetting( |
380 &pref_content_settings_provider, | 366 &pref_content_settings_provider, |
381 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, | 367 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, |
382 resource2, false)); | 368 resource2, false)); |
383 | 369 |
384 pref_content_settings_provider.ShutdownOnUIThread(); | 370 pref_content_settings_provider.ShutdownOnUIThread(); |
385 } | 371 } |
386 | 372 |
387 TEST_F(PrefProviderTest, AutoSubmitCertificateContentSetting) { | 373 TEST(PrefProviderTest, AutoSubmitCertificateContentSetting) { |
388 TestingProfile profile; | 374 TestingProfile profile; |
389 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); | 375 TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); |
390 GURL primary_url("https://www.example.com"); | 376 GURL primary_url("https://www.example.com"); |
391 GURL secondary_url("https://www.sample.com"); | 377 GURL secondary_url("https://www.sample.com"); |
392 | 378 |
393 PrefProvider provider(prefs, false); | 379 PrefProvider provider(prefs, false); |
394 | 380 |
395 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 381 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
396 GetContentSetting( | 382 GetContentSetting( |
397 &provider, | 383 &provider, |
(...skipping 13 matching lines...) Expand all Loading... |
411 &provider, | 397 &provider, |
412 primary_url, | 398 primary_url, |
413 secondary_url, | 399 secondary_url, |
414 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, | 400 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, |
415 std::string(), | 401 std::string(), |
416 false)); | 402 false)); |
417 provider.ShutdownOnUIThread(); | 403 provider.ShutdownOnUIThread(); |
418 } | 404 } |
419 | 405 |
420 // http://crosbug.com/17760 | 406 // http://crosbug.com/17760 |
421 TEST_F(PrefProviderTest, Deadlock) { | 407 TEST(PrefProviderTest, Deadlock) { |
422 TestingPrefServiceSyncable prefs; | 408 TestingPrefServiceSyncable prefs; |
423 PrefProvider::RegisterProfilePrefs(prefs.registry()); | 409 PrefProvider::RegisterProfilePrefs(prefs.registry()); |
424 | 410 |
425 // Chain of events: a preference changes, |PrefProvider| notices it, and reads | 411 // Chain of events: a preference changes, |PrefProvider| notices it, and reads |
426 // and writes the preference. When the preference is written, a notification | 412 // and writes the preference. When the preference is written, a notification |
427 // is sent, and this used to happen when |PrefProvider| was still holding its | 413 // is sent, and this used to happen when |PrefProvider| was still holding its |
428 // lock. | 414 // lock. |
429 | 415 |
430 PrefProvider provider(&prefs, false); | 416 PrefProvider provider(&prefs, false); |
431 DeadlockCheckerObserver observer(&prefs, &provider); | 417 DeadlockCheckerObserver observer(&prefs, &provider); |
432 { | 418 { |
433 DictionaryPrefUpdate update(&prefs, | 419 DictionaryPrefUpdate update(&prefs, |
434 prefs::kContentSettingsPatternPairs); | 420 prefs::kContentSettingsPatternPairs); |
435 base::DictionaryValue* mutable_settings = update.Get(); | 421 base::DictionaryValue* mutable_settings = update.Get(); |
436 mutable_settings->SetWithoutPathExpansion("www.example.com,*", | 422 mutable_settings->SetWithoutPathExpansion("www.example.com,*", |
437 new base::DictionaryValue()); | 423 new base::DictionaryValue()); |
438 } | 424 } |
439 EXPECT_TRUE(observer.notification_received()); | 425 EXPECT_TRUE(observer.notification_received()); |
440 | 426 |
441 provider.ShutdownOnUIThread(); | 427 provider.ShutdownOnUIThread(); |
442 } | 428 } |
443 | 429 |
444 TEST_F(PrefProviderTest, LastUsage) { | 430 TEST(PrefProviderTest, LastUsage) { |
445 TestingProfile testing_profile; | 431 TestingProfile testing_profile; |
446 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), | 432 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), |
447 false); | 433 false); |
448 base::SimpleTestClock* test_clock = new base::SimpleTestClock; | 434 base::SimpleTestClock* test_clock = new base::SimpleTestClock; |
449 test_clock->SetNow(base::Time::Now()); | 435 test_clock->SetNow(base::Time::Now()); |
450 | 436 |
451 pref_content_settings_provider.SetClockForTesting( | 437 pref_content_settings_provider.SetClockForTesting( |
452 scoped_ptr<base::Clock>(test_clock)); | 438 scoped_ptr<base::Clock>(test_clock)); |
453 GURL host("http://example.com/"); | 439 GURL host("http://example.com/"); |
454 ContentSettingsPattern pattern = | 440 ContentSettingsPattern pattern = |
(...skipping 15 matching lines...) Expand all Loading... |
470 base::Time second = pref_content_settings_provider.GetLastUsage( | 456 base::Time second = pref_content_settings_provider.GetLastUsage( |
471 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | 457 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
472 | 458 |
473 base::TimeDelta delta = second - first; | 459 base::TimeDelta delta = second - first; |
474 EXPECT_EQ(delta.InSeconds(), 10); | 460 EXPECT_EQ(delta.InSeconds(), 10); |
475 | 461 |
476 pref_content_settings_provider.ShutdownOnUIThread(); | 462 pref_content_settings_provider.ShutdownOnUIThread(); |
477 } | 463 } |
478 | 464 |
479 } // namespace content_settings | 465 } // namespace content_settings |
OLD | NEW |