| 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" |
| 11 #include "base/prefs/default_pref_store.h" | 11 #include "base/prefs/default_pref_store.h" |
| 12 #include "base/prefs/overlay_user_pref_store.h" | 12 #include "base/prefs/overlay_user_pref_store.h" |
| 13 #include "base/prefs/pref_change_registrar.h" | 13 #include "base/prefs/pref_change_registrar.h" |
| 14 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
| 15 #include "base/prefs/scoped_user_pref_update.h" | 15 #include "base/prefs/scoped_user_pref_update.h" |
| 16 #include "base/prefs/testing_pref_store.h" | 16 #include "base/prefs/testing_pref_store.h" |
| 17 #include "base/test/simple_test_clock.h" | 17 #include "base/test/simple_test_clock.h" |
| 18 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 20 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
| 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_content_settings_client.h" |
| 25 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
| 27 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
| 28 #include "chrome/test/base/testing_pref_service_syncable.h" | 29 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 29 #include "chrome/test/base/testing_profile.h" | 30 #include "chrome/test/base/testing_profile.h" |
| 30 #include "components/pref_registry/pref_registry_syncable.h" | 31 #include "components/pref_registry/pref_registry_syncable.h" |
| 31 #include "content/public/test/test_browser_thread.h" | 32 #include "content/public/test/test_browser_thread.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 34 | 35 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 bool notification_received_; | 92 bool notification_received_; |
| 92 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerObserver); | 93 DISALLOW_COPY_AND_ASSIGN(DeadlockCheckerObserver); |
| 93 }; | 94 }; |
| 94 | 95 |
| 95 class PrefProviderTest : public testing::Test { | 96 class PrefProviderTest : public testing::Test { |
| 96 public: | 97 public: |
| 97 PrefProviderTest() : ui_thread_( | 98 PrefProviderTest() : ui_thread_( |
| 98 BrowserThread::UI, &message_loop_) { | 99 BrowserThread::UI, &message_loop_) { |
| 99 } | 100 } |
| 100 | 101 |
| 102 content_settings::ContentSettingsClient* client() { |
| 103 return &client_; |
| 104 } |
| 105 |
| 101 protected: | 106 protected: |
| 102 base::MessageLoop message_loop_; | 107 base::MessageLoop message_loop_; |
| 103 content::TestBrowserThread ui_thread_; | 108 content::TestBrowserThread ui_thread_; |
| 109 content_settings::ChromeContentSettingsClient client_; |
| 104 }; | 110 }; |
| 105 | 111 |
| 106 TEST_F(PrefProviderTest, Observer) { | 112 TEST_F(PrefProviderTest, Observer) { |
| 107 TestingProfile profile; | 113 TestingProfile profile; |
| 108 PrefProvider pref_content_settings_provider(profile.GetPrefs(), false); | 114 PrefProvider pref_content_settings_provider(profile.GetPrefs(), false); |
| 109 | 115 |
| 110 ContentSettingsPattern pattern = | 116 ContentSettingsPattern pattern = |
| 111 ContentSettingsPattern::FromString("[*.]example.com"); | 117 ContentSettingsPattern::FromString(client(), "[*.]example.com"); |
| 112 content_settings::MockObserver mock_observer; | 118 content_settings::MockObserver mock_observer; |
| 113 EXPECT_CALL(mock_observer, | 119 EXPECT_CALL(mock_observer, |
| 114 OnContentSettingChanged(pattern, | 120 OnContentSettingChanged(pattern, |
| 115 ContentSettingsPattern::Wildcard(), | 121 ContentSettingsPattern::Wildcard(), |
| 116 CONTENT_SETTINGS_TYPE_IMAGES, | 122 CONTENT_SETTINGS_TYPE_IMAGES, |
| 117 "")); | 123 "")); |
| 118 | 124 |
| 119 pref_content_settings_provider.AddObserver(&mock_observer); | 125 pref_content_settings_provider.AddObserver(&mock_observer); |
| 120 | 126 |
| 121 pref_content_settings_provider.SetWebsiteSetting( | 127 pref_content_settings_provider.SetWebsiteSetting( |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 165 |
| 160 TestingProfile::Builder otr_profile_builder; | 166 TestingProfile::Builder otr_profile_builder; |
| 161 otr_profile_builder.SetIncognito(); | 167 otr_profile_builder.SetIncognito(); |
| 162 otr_profile_builder.SetPrefService(make_scoped_ptr(otr_prefs)); | 168 otr_profile_builder.SetPrefService(make_scoped_ptr(otr_prefs)); |
| 163 scoped_ptr<TestingProfile> otr_profile(otr_profile_builder.Build()); | 169 scoped_ptr<TestingProfile> otr_profile(otr_profile_builder.Build()); |
| 164 profile->SetOffTheRecordProfile(otr_profile.PassAs<Profile>()); | 170 profile->SetOffTheRecordProfile(otr_profile.PassAs<Profile>()); |
| 165 | 171 |
| 166 PrefProvider pref_content_settings_provider(regular_prefs, false); | 172 PrefProvider pref_content_settings_provider(regular_prefs, false); |
| 167 PrefProvider pref_content_settings_provider_incognito(otr_prefs, true); | 173 PrefProvider pref_content_settings_provider_incognito(otr_prefs, true); |
| 168 ContentSettingsPattern pattern = | 174 ContentSettingsPattern pattern = |
| 169 ContentSettingsPattern::FromString("[*.]example.com"); | 175 ContentSettingsPattern::FromString(client(), "[*.]example.com"); |
| 170 pref_content_settings_provider.SetWebsiteSetting( | 176 pref_content_settings_provider.SetWebsiteSetting( |
| 171 pattern, | 177 pattern, |
| 172 pattern, | 178 pattern, |
| 173 CONTENT_SETTINGS_TYPE_IMAGES, | 179 CONTENT_SETTINGS_TYPE_IMAGES, |
| 174 std::string(), | 180 std::string(), |
| 175 new base::FundamentalValue(CONTENT_SETTING_ALLOW)); | 181 new base::FundamentalValue(CONTENT_SETTING_ALLOW)); |
| 176 | 182 |
| 177 GURL host("http://example.com/"); | 183 GURL host("http://example.com/"); |
| 178 // The value should of course be visible in the regular PrefProvider. | 184 // The value should of course be visible in the regular PrefProvider. |
| 179 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 185 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 198 pref_content_settings_provider.ShutdownOnUIThread(); | 204 pref_content_settings_provider.ShutdownOnUIThread(); |
| 199 pref_content_settings_provider_incognito.ShutdownOnUIThread(); | 205 pref_content_settings_provider_incognito.ShutdownOnUIThread(); |
| 200 } | 206 } |
| 201 | 207 |
| 202 TEST_F(PrefProviderTest, GetContentSettingsValue) { | 208 TEST_F(PrefProviderTest, GetContentSettingsValue) { |
| 203 TestingProfile testing_profile; | 209 TestingProfile testing_profile; |
| 204 PrefProvider provider(testing_profile.GetPrefs(), false); | 210 PrefProvider provider(testing_profile.GetPrefs(), false); |
| 205 | 211 |
| 206 GURL primary_url("http://example.com/"); | 212 GURL primary_url("http://example.com/"); |
| 207 ContentSettingsPattern primary_pattern = | 213 ContentSettingsPattern primary_pattern = |
| 208 ContentSettingsPattern::FromString("[*.]example.com"); | 214 ContentSettingsPattern::FromString(client(), "[*.]example.com"); |
| 209 | 215 |
| 210 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 216 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 211 GetContentSetting(&provider, | 217 GetContentSetting(&provider, |
| 212 primary_url, | 218 primary_url, |
| 213 primary_url, | 219 primary_url, |
| 214 CONTENT_SETTINGS_TYPE_IMAGES, | 220 CONTENT_SETTINGS_TYPE_IMAGES, |
| 215 std::string(), | 221 std::string(), |
| 216 false)); | 222 false)); |
| 217 | 223 |
| 218 EXPECT_EQ(NULL, | 224 EXPECT_EQ(NULL, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 TEST_F(PrefProviderTest, Patterns) { | 270 TEST_F(PrefProviderTest, Patterns) { |
| 265 TestingProfile testing_profile; | 271 TestingProfile testing_profile; |
| 266 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), | 272 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), |
| 267 false); | 273 false); |
| 268 | 274 |
| 269 GURL host1("http://example.com/"); | 275 GURL host1("http://example.com/"); |
| 270 GURL host2("http://www.example.com/"); | 276 GURL host2("http://www.example.com/"); |
| 271 GURL host3("http://example.org/"); | 277 GURL host3("http://example.org/"); |
| 272 GURL host4("file:///tmp/test.html"); | 278 GURL host4("file:///tmp/test.html"); |
| 273 ContentSettingsPattern pattern1 = | 279 ContentSettingsPattern pattern1 = |
| 274 ContentSettingsPattern::FromString("[*.]example.com"); | 280 ContentSettingsPattern::FromString(client(), "[*.]example.com"); |
| 275 ContentSettingsPattern pattern2 = | 281 ContentSettingsPattern pattern2 = |
| 276 ContentSettingsPattern::FromString("example.org"); | 282 ContentSettingsPattern::FromString(client(), "example.org"); |
| 277 ContentSettingsPattern pattern3 = | 283 ContentSettingsPattern pattern3 = |
| 278 ContentSettingsPattern::FromString("file:///tmp/test.html"); | 284 ContentSettingsPattern::FromString(client(), "file:///tmp/test.html"); |
| 279 | 285 |
| 280 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 286 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 281 GetContentSetting(&pref_content_settings_provider, | 287 GetContentSetting(&pref_content_settings_provider, |
| 282 host1, | 288 host1, |
| 283 host1, | 289 host1, |
| 284 CONTENT_SETTINGS_TYPE_IMAGES, | 290 CONTENT_SETTINGS_TYPE_IMAGES, |
| 285 std::string(), | 291 std::string(), |
| 286 false)); | 292 false)); |
| 287 pref_content_settings_provider.SetWebsiteSetting( | 293 pref_content_settings_provider.SetWebsiteSetting( |
| 288 pattern1, | 294 pattern1, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 pref_content_settings_provider.ShutdownOnUIThread(); | 356 pref_content_settings_provider.ShutdownOnUIThread(); |
| 351 } | 357 } |
| 352 | 358 |
| 353 TEST_F(PrefProviderTest, ResourceIdentifier) { | 359 TEST_F(PrefProviderTest, ResourceIdentifier) { |
| 354 TestingProfile testing_profile; | 360 TestingProfile testing_profile; |
| 355 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), | 361 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), |
| 356 false); | 362 false); |
| 357 | 363 |
| 358 GURL host("http://example.com/"); | 364 GURL host("http://example.com/"); |
| 359 ContentSettingsPattern pattern = | 365 ContentSettingsPattern pattern = |
| 360 ContentSettingsPattern::FromString("[*.]example.com"); | 366 ContentSettingsPattern::FromString(client(), "[*.]example.com"); |
| 361 std::string resource1("someplugin"); | 367 std::string resource1("someplugin"); |
| 362 std::string resource2("otherplugin"); | 368 std::string resource2("otherplugin"); |
| 363 | 369 |
| 364 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | 370 EXPECT_EQ(CONTENT_SETTING_DEFAULT, |
| 365 GetContentSetting( | 371 GetContentSetting( |
| 366 &pref_content_settings_provider, | 372 &pref_content_settings_provider, |
| 367 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, | 373 host, host, CONTENT_SETTINGS_TYPE_PLUGINS, |
| 368 resource1, false)); | 374 resource1, false)); |
| 369 pref_content_settings_provider.SetWebsiteSetting( | 375 pref_content_settings_provider.SetWebsiteSetting( |
| 370 pattern, | 376 pattern, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 TestingProfile testing_profile; | 453 TestingProfile testing_profile; |
| 448 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), | 454 PrefProvider pref_content_settings_provider(testing_profile.GetPrefs(), |
| 449 false); | 455 false); |
| 450 base::SimpleTestClock* test_clock = new base::SimpleTestClock; | 456 base::SimpleTestClock* test_clock = new base::SimpleTestClock; |
| 451 test_clock->SetNow(base::Time::Now()); | 457 test_clock->SetNow(base::Time::Now()); |
| 452 | 458 |
| 453 pref_content_settings_provider.SetClockForTesting( | 459 pref_content_settings_provider.SetClockForTesting( |
| 454 scoped_ptr<base::Clock>(test_clock)); | 460 scoped_ptr<base::Clock>(test_clock)); |
| 455 GURL host("http://example.com/"); | 461 GURL host("http://example.com/"); |
| 456 ContentSettingsPattern pattern = | 462 ContentSettingsPattern pattern = |
| 457 ContentSettingsPattern::FromString("[*.]example.com"); | 463 ContentSettingsPattern::FromString(client(), "[*.]example.com"); |
| 458 | 464 |
| 459 base::Time no_usage = pref_content_settings_provider.GetLastUsage( | 465 base::Time no_usage = pref_content_settings_provider.GetLastUsage( |
| 460 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | 466 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 461 EXPECT_EQ(no_usage.ToDoubleT(), 0); | 467 EXPECT_EQ(no_usage.ToDoubleT(), 0); |
| 462 | 468 |
| 463 pref_content_settings_provider.UpdateLastUsage( | 469 pref_content_settings_provider.UpdateLastUsage( |
| 464 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | 470 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 465 base::Time first = pref_content_settings_provider.GetLastUsage( | 471 base::Time first = pref_content_settings_provider.GetLastUsage( |
| 466 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | 472 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 467 | 473 |
| 468 test_clock->Advance(base::TimeDelta::FromSeconds(10)); | 474 test_clock->Advance(base::TimeDelta::FromSeconds(10)); |
| 469 | 475 |
| 470 pref_content_settings_provider.UpdateLastUsage( | 476 pref_content_settings_provider.UpdateLastUsage( |
| 471 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | 477 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 472 base::Time second = pref_content_settings_provider.GetLastUsage( | 478 base::Time second = pref_content_settings_provider.GetLastUsage( |
| 473 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); | 479 pattern, pattern, CONTENT_SETTINGS_TYPE_GEOLOCATION); |
| 474 | 480 |
| 475 base::TimeDelta delta = second - first; | 481 base::TimeDelta delta = second - first; |
| 476 EXPECT_EQ(delta.InSeconds(), 10); | 482 EXPECT_EQ(delta.InSeconds(), 10); |
| 477 | 483 |
| 478 pref_content_settings_provider.ShutdownOnUIThread(); | 484 pref_content_settings_provider.ShutdownOnUIThread(); |
| 479 } | 485 } |
| 480 | 486 |
| 481 } // namespace content_settings | 487 } // namespace content_settings |
| OLD | NEW |