OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/content_settings/content_settings_mock_observer.h" | 9 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
10 #include "chrome/browser/prefs/browser_prefs.h" | 10 #include "chrome/browser/prefs/browser_prefs.h" |
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
654 DictionaryPrefUpdate update(prefs, | 654 DictionaryPrefUpdate update(prefs, |
655 prefs::kContentSettingsPatternPairs); | 655 prefs::kContentSettingsPatternPairs); |
656 DictionaryValue* all_settings_dictionary = update.Get(); | 656 DictionaryValue* all_settings_dictionary = update.Get(); |
657 std::string key( | 657 std::string key( |
658 primary_pattern.ToString()+ "," + | 658 primary_pattern.ToString()+ "," + |
659 secondary_pattern.ToString()); | 659 secondary_pattern.ToString()); |
660 all_settings_dictionary->SetWithoutPathExpansion( | 660 all_settings_dictionary->SetWithoutPathExpansion( |
661 key, settings_dictionary->DeepCopy()); | 661 key, settings_dictionary->DeepCopy()); |
662 | 662 |
663 key = std::string( | 663 key = std::string( |
664 primary_pattern_2.ToString()+ "," + | 664 primary_pattern_2.ToString() + "," + |
665 secondary_pattern.ToString()); | 665 secondary_pattern.ToString()); |
666 all_settings_dictionary->SetWithoutPathExpansion( | 666 all_settings_dictionary->SetWithoutPathExpansion( |
667 key, settings_dictionary->DeepCopy()); | 667 key, settings_dictionary->DeepCopy()); |
668 } | 668 } |
669 | 669 |
670 // Test if the obsolete geolocation preference is kept in sync if the new | 670 // Test if the obsolete geolocation preference is kept in sync if the new |
671 // preference is changed by a sync. | 671 // preference is changed by a sync. |
672 GURL primary_url("http://www.bar.com"); | 672 GURL primary_url("http://www.bar.com"); |
673 GURL primary_url_2("http://www.example.com"); | 673 GURL primary_url_2("http://www.example.com"); |
674 GURL secondary_url("http://www.foo.com"); | 674 GURL secondary_url("http://www.foo.com"); |
675 | 675 |
676 const DictionaryValue* geo_settings_dictionary = | 676 const DictionaryValue* geo_settings_dictionary = |
677 prefs->GetDictionary(prefs::kGeolocationContentSettings); | 677 prefs->GetDictionary(prefs::kGeolocationContentSettings); |
678 EXPECT_EQ(2U, geo_settings_dictionary->size()); | 678 EXPECT_EQ(2U, geo_settings_dictionary->size()); |
679 ExpectObsoleteGeolocationSetting(*geo_settings_dictionary, | 679 ExpectObsoleteGeolocationSetting(*geo_settings_dictionary, |
680 primary_url, | 680 primary_url, |
681 secondary_url, | 681 secondary_url, |
682 CONTENT_SETTING_BLOCK); | 682 CONTENT_SETTING_BLOCK); |
683 ExpectObsoleteGeolocationSetting(*geo_settings_dictionary, | 683 ExpectObsoleteGeolocationSetting(*geo_settings_dictionary, |
684 primary_url_2, | 684 primary_url_2, |
685 secondary_url, | 685 secondary_url, |
686 CONTENT_SETTING_BLOCK); | 686 CONTENT_SETTING_BLOCK); |
687 | 687 |
688 provider.ShutdownOnUIThread(); | 688 provider.ShutdownOnUIThread(); |
689 } | 689 } |
690 | 690 |
691 TEST_F(PrefProviderTest, MigrateObsoleteNotificationsPref) { | |
692 TestingProfile profile; | |
693 PrefService* prefs = profile.GetPrefs(); | |
694 GURL allowed_url("http://www.foo.com"); | |
695 GURL allowed_url2("http://www.example.com"); | |
696 GURL denied_url("http://www.bar.com"); | |
697 | |
698 // Set obsolete preference. | |
699 ListValue* allowed_origin_list = new ListValue(); | |
battre
2011/08/22 15:00:57
memory leak
markusheintz_
2011/08/24 00:47:12
Done.
| |
700 allowed_origin_list->AppendIfNotPresent( | |
701 Value::CreateStringValue(allowed_url.spec())); | |
702 prefs->Set(prefs::kDesktopNotificationAllowedOrigins, | |
703 *allowed_origin_list); | |
704 | |
705 ListValue* denied_origin_list = new ListValue(); | |
battre
2011/08/22 15:00:57
memory leak
markusheintz_
2011/08/24 00:47:12
Done.
| |
706 denied_origin_list->AppendIfNotPresent( | |
707 Value::CreateStringValue(denied_url.spec())); | |
708 prefs->Set(prefs::kDesktopNotificationDeniedOrigins, | |
709 *denied_origin_list); | |
710 | |
711 content_settings::PrefProvider provider(prefs, false); | |
712 | |
713 // Test if the migrated settings are loaded and available. | |
714 EXPECT_EQ(CONTENT_SETTING_ALLOW, provider.GetContentSetting( | |
715 allowed_url, | |
716 allowed_url, | |
717 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
718 "")); | |
719 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( | |
720 denied_url, | |
721 denied_url, | |
722 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
723 "")); | |
724 EXPECT_EQ(CONTENT_SETTING_DEFAULT, provider.GetContentSetting( | |
725 allowed_url2, | |
726 allowed_url2, | |
727 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
728 "")); | |
729 // Check if the settings where migrated correctly. | |
730 const DictionaryValue* const_all_settings_dictionary = | |
731 prefs->GetDictionary(prefs::kContentSettingsPatternPairs); | |
732 EXPECT_EQ(2U, const_all_settings_dictionary->size()); | |
733 EXPECT_TRUE(const_all_settings_dictionary->HasKey( | |
734 ContentSettingsPattern::FromURLNoWildcard(allowed_url).ToString() + "," + | |
735 ContentSettingsPattern::Wildcard().ToString())); | |
736 EXPECT_TRUE(const_all_settings_dictionary->HasKey( | |
737 ContentSettingsPattern::FromURLNoWildcard(denied_url).ToString() + "," + | |
738 ContentSettingsPattern::Wildcard().ToString())); | |
739 | |
740 // Check that notifications settings were not synced to the obsolete content | |
741 // settings pattern preference. | |
742 const DictionaryValue* const_obsolete_patterns_dictionary = | |
743 prefs->GetDictionary(prefs::kContentSettingsPatterns); | |
744 EXPECT_TRUE(const_obsolete_patterns_dictionary->empty()); | |
745 | |
746 // Change obsolete preference. This could be triggered by sync if sync is used | |
747 // with an old version of chrome. | |
748 allowed_origin_list = new ListValue(); | |
battre
2011/08/22 15:00:57
memory leak
markusheintz_
2011/08/24 00:47:12
Done.
| |
749 allowed_origin_list->AppendIfNotPresent( | |
750 Value::CreateStringValue(allowed_url2.spec())); | |
751 prefs->Set(prefs::kDesktopNotificationAllowedOrigins, | |
752 *allowed_origin_list); | |
753 | |
754 // Test if the changed obsolete preference was migrated correctly. | |
755 EXPECT_EQ(CONTENT_SETTING_ALLOW, provider.GetContentSetting( | |
756 allowed_url2, | |
757 allowed_url2, | |
758 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
759 "")); | |
760 EXPECT_EQ(CONTENT_SETTING_DEFAULT, provider.GetContentSetting( | |
761 allowed_url, | |
762 allowed_url, | |
763 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
764 "")); | |
765 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( | |
766 denied_url, | |
767 denied_url, | |
768 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
769 "")); | |
770 // Check that geolocation settings were not synced to the obsolete content | |
771 // settings pattern preference. | |
772 const_obsolete_patterns_dictionary = | |
773 prefs->GetDictionary(prefs::kContentSettingsPatterns); | |
774 EXPECT_TRUE(const_obsolete_patterns_dictionary->empty()); | |
775 | |
776 provider.ShutdownOnUIThread(); | |
777 } | |
778 | |
779 TEST_F(PrefProviderTest, SyncObsoleteNotificationsPref) { | |
780 TestingProfile profile; | |
781 PrefService* prefs = profile.GetPrefs(); | |
782 | |
783 content_settings::PrefProvider provider(prefs, false); | |
784 | |
785 // Changing the preferences prefs::kContentSettingsPatternPairs. | |
786 ContentSettingsPattern primary_pattern= | |
787 ContentSettingsPattern::FromString("http://www.bar.com"); | |
788 ContentSettingsPattern primary_pattern_2 = | |
789 ContentSettingsPattern::FromString("http://www.example.com"); | |
790 ContentSettingsPattern secondary_pattern = | |
791 ContentSettingsPattern::Wildcard(); | |
battre
2011/08/22 15:00:57
nit: single line?
markusheintz_
2011/08/24 00:47:12
No 81 characters :(
| |
792 GURL primary_url("http://www.bar.com"); | |
793 GURL primary_url_2("http://www.example.com"); | |
794 | |
795 { | |
796 DictionaryPrefUpdate update(prefs, | |
797 prefs::kContentSettingsPatternPairs); | |
798 DictionaryValue* all_settings_dictionary = update.Get(); | |
battre
2011/08/22 15:00:57
nit/opt: You can also do something like:
Dictionar
markusheintz_
2011/08/24 00:47:12
Cool. Thanks for pointing this out to me :).
| |
799 | |
800 scoped_ptr<DictionaryValue> settings_dictionary(new DictionaryValue()); | |
801 settings_dictionary->SetInteger("notifications", CONTENT_SETTING_BLOCK); | |
802 std::string key( | |
803 primary_pattern.ToString() + "," + | |
804 secondary_pattern.ToString()); | |
805 all_settings_dictionary->SetWithoutPathExpansion( | |
806 key, settings_dictionary->DeepCopy()); | |
807 | |
808 settings_dictionary.reset(new DictionaryValue()); | |
809 settings_dictionary->SetInteger("notifications", CONTENT_SETTING_ALLOW); | |
810 key = primary_pattern_2.ToString() + "," + secondary_pattern.ToString(); | |
811 all_settings_dictionary->SetWithoutPathExpansion( | |
812 key, settings_dictionary->DeepCopy()); | |
813 } | |
814 | |
815 // Test if the obsolete notifications preference is kept in sync if the new | |
816 // preference is changed by a sync. | |
817 const ListValue* denied_origin_list = | |
818 prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); | |
819 EXPECT_EQ(1U, denied_origin_list->GetSize()); | |
820 const ListValue* allowed_origin_list = | |
821 prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); | |
822 EXPECT_EQ(1U, allowed_origin_list->GetSize()); | |
823 | |
824 provider.ShutdownOnUIThread(); | |
825 } | |
826 | |
691 } // namespace content_settings | 827 } // namespace content_settings |
OLD | NEW |