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