| 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 "base/memory/scoped_ptr.h" | |
| 10 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 9 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
| 11 #include "chrome/browser/prefs/browser_prefs.h" | 10 #include "chrome/browser/prefs/browser_prefs.h" |
| 12 #include "chrome/browser/prefs/default_pref_store.h" | 11 #include "chrome/browser/prefs/default_pref_store.h" |
| 13 #include "chrome/browser/prefs/incognito_user_pref_store.h" | 12 #include "chrome/browser/prefs/incognito_user_pref_store.h" |
| 14 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
| 15 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 14 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
| 16 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 15 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 17 #include "chrome/browser/prefs/testing_pref_store.h" | 16 #include "chrome/browser/prefs/testing_pref_store.h" |
| 18 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
| (...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 DictionaryPrefUpdate update(prefs, | 670 DictionaryPrefUpdate update(prefs, |
| 672 prefs::kContentSettingsPatternPairs); | 671 prefs::kContentSettingsPatternPairs); |
| 673 DictionaryValue* all_settings_dictionary = update.Get(); | 672 DictionaryValue* all_settings_dictionary = update.Get(); |
| 674 std::string key( | 673 std::string key( |
| 675 primary_pattern.ToString()+ "," + | 674 primary_pattern.ToString()+ "," + |
| 676 secondary_pattern.ToString()); | 675 secondary_pattern.ToString()); |
| 677 all_settings_dictionary->SetWithoutPathExpansion( | 676 all_settings_dictionary->SetWithoutPathExpansion( |
| 678 key, settings_dictionary->DeepCopy()); | 677 key, settings_dictionary->DeepCopy()); |
| 679 | 678 |
| 680 key = std::string( | 679 key = std::string( |
| 681 primary_pattern_2.ToString() + "," + | 680 primary_pattern_2.ToString()+ "," + |
| 682 secondary_pattern.ToString()); | 681 secondary_pattern.ToString()); |
| 683 all_settings_dictionary->SetWithoutPathExpansion( | 682 all_settings_dictionary->SetWithoutPathExpansion( |
| 684 key, settings_dictionary->DeepCopy()); | 683 key, settings_dictionary->DeepCopy()); |
| 685 } | 684 } |
| 686 | 685 |
| 687 // Test if the obsolete geolocation preference is kept in sync if the new | 686 // Test if the obsolete geolocation preference is kept in sync if the new |
| 688 // preference is changed by a sync. | 687 // preference is changed by a sync. |
| 689 GURL primary_url("http://www.bar.com"); | 688 GURL primary_url("http://www.bar.com"); |
| 690 GURL primary_url_2("http://www.example.com"); | 689 GURL primary_url_2("http://www.example.com"); |
| 691 GURL secondary_url("http://www.foo.com"); | 690 GURL secondary_url("http://www.foo.com"); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 728 CONTENT_SETTING_ALLOW); | 727 CONTENT_SETTING_ALLOW); |
| 729 EXPECT_EQ(CONTENT_SETTING_ALLOW, | 728 EXPECT_EQ(CONTENT_SETTING_ALLOW, |
| 730 provider.GetContentSetting( | 729 provider.GetContentSetting( |
| 731 primary_url, | 730 primary_url, |
| 732 secondary_url, | 731 secondary_url, |
| 733 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, | 732 CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, |
| 734 std::string())); | 733 std::string())); |
| 735 provider.ShutdownOnUIThread(); | 734 provider.ShutdownOnUIThread(); |
| 736 } | 735 } |
| 737 | 736 |
| 738 TEST_F(PrefProviderTest, MigrateObsoleteNotificationsPref) { | |
| 739 TestingProfile profile; | |
| 740 PrefService* prefs = profile.GetPrefs(); | |
| 741 GURL allowed_url("http://www.foo.com"); | |
| 742 GURL allowed_url2("http://www.example.com"); | |
| 743 GURL denied_url("http://www.bar.com"); | |
| 744 | |
| 745 // Set obsolete preference. | |
| 746 scoped_ptr<ListValue> allowed_origin_list(new ListValue()); | |
| 747 allowed_origin_list->AppendIfNotPresent( | |
| 748 Value::CreateStringValue(allowed_url.spec())); | |
| 749 prefs->Set(prefs::kDesktopNotificationAllowedOrigins, | |
| 750 *allowed_origin_list); | |
| 751 | |
| 752 scoped_ptr<ListValue> denied_origin_list(new ListValue()); | |
| 753 denied_origin_list->AppendIfNotPresent( | |
| 754 Value::CreateStringValue(denied_url.spec())); | |
| 755 prefs->Set(prefs::kDesktopNotificationDeniedOrigins, | |
| 756 *denied_origin_list); | |
| 757 | |
| 758 content_settings::PrefProvider provider(prefs, false); | |
| 759 | |
| 760 // Test if the migrated settings are loaded and available. | |
| 761 EXPECT_EQ(CONTENT_SETTING_ALLOW, provider.GetContentSetting( | |
| 762 allowed_url, | |
| 763 allowed_url, | |
| 764 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
| 765 "")); | |
| 766 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( | |
| 767 denied_url, | |
| 768 denied_url, | |
| 769 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
| 770 "")); | |
| 771 EXPECT_EQ(CONTENT_SETTING_DEFAULT, provider.GetContentSetting( | |
| 772 allowed_url2, | |
| 773 allowed_url2, | |
| 774 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
| 775 "")); | |
| 776 // Check if the settings where migrated correctly. | |
| 777 const DictionaryValue* const_all_settings_dictionary = | |
| 778 prefs->GetDictionary(prefs::kContentSettingsPatternPairs); | |
| 779 EXPECT_EQ(2U, const_all_settings_dictionary->size()); | |
| 780 EXPECT_TRUE(const_all_settings_dictionary->HasKey( | |
| 781 ContentSettingsPattern::FromURLNoWildcard(allowed_url).ToString() + "," + | |
| 782 ContentSettingsPattern::Wildcard().ToString())); | |
| 783 EXPECT_TRUE(const_all_settings_dictionary->HasKey( | |
| 784 ContentSettingsPattern::FromURLNoWildcard(denied_url).ToString() + "," + | |
| 785 ContentSettingsPattern::Wildcard().ToString())); | |
| 786 | |
| 787 // Check that notifications settings were not synced to the obsolete content | |
| 788 // settings pattern preference. | |
| 789 const DictionaryValue* const_obsolete_patterns_dictionary = | |
| 790 prefs->GetDictionary(prefs::kContentSettingsPatterns); | |
| 791 EXPECT_TRUE(const_obsolete_patterns_dictionary->empty()); | |
| 792 | |
| 793 // Change obsolete preference. This could be triggered by sync if sync is used | |
| 794 // with an old version of chrome. | |
| 795 allowed_origin_list.reset(new ListValue()); | |
| 796 allowed_origin_list->AppendIfNotPresent( | |
| 797 Value::CreateStringValue(allowed_url2.spec())); | |
| 798 prefs->Set(prefs::kDesktopNotificationAllowedOrigins, | |
| 799 *allowed_origin_list); | |
| 800 | |
| 801 // Test if the changed obsolete preference was migrated correctly. | |
| 802 EXPECT_EQ(CONTENT_SETTING_ALLOW, provider.GetContentSetting( | |
| 803 allowed_url2, | |
| 804 allowed_url2, | |
| 805 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
| 806 "")); | |
| 807 EXPECT_EQ(CONTENT_SETTING_DEFAULT, provider.GetContentSetting( | |
| 808 allowed_url, | |
| 809 allowed_url, | |
| 810 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
| 811 "")); | |
| 812 EXPECT_EQ(CONTENT_SETTING_BLOCK, provider.GetContentSetting( | |
| 813 denied_url, | |
| 814 denied_url, | |
| 815 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | |
| 816 "")); | |
| 817 // Check that geolocation settings were not synced to the obsolete content | |
| 818 // settings pattern preference. | |
| 819 const_obsolete_patterns_dictionary = | |
| 820 prefs->GetDictionary(prefs::kContentSettingsPatterns); | |
| 821 EXPECT_TRUE(const_obsolete_patterns_dictionary->empty()); | |
| 822 | |
| 823 provider.ShutdownOnUIThread(); | |
| 824 } | |
| 825 | |
| 826 TEST_F(PrefProviderTest, SyncObsoleteNotificationsPref) { | |
| 827 TestingProfile profile; | |
| 828 PrefService* prefs = profile.GetPrefs(); | |
| 829 | |
| 830 content_settings::PrefProvider provider(prefs, false); | |
| 831 | |
| 832 // Changing the preferences prefs::kContentSettingsPatternPairs. | |
| 833 ContentSettingsPattern primary_pattern= | |
| 834 ContentSettingsPattern::FromString("http://www.bar.com"); | |
| 835 ContentSettingsPattern primary_pattern_2 = | |
| 836 ContentSettingsPattern::FromString("http://www.example.com"); | |
| 837 ContentSettingsPattern secondary_pattern = | |
| 838 ContentSettingsPattern::Wildcard(); | |
| 839 GURL primary_url("http://www.bar.com"); | |
| 840 GURL primary_url_2("http://www.example.com"); | |
| 841 | |
| 842 { | |
| 843 DictionaryPrefUpdate update(prefs, | |
| 844 prefs::kContentSettingsPatternPairs); | |
| 845 DictionaryValue* all_settings_dictionary = update.Get(); | |
| 846 | |
| 847 scoped_ptr<DictionaryValue> settings_dictionary(new DictionaryValue()); | |
| 848 settings_dictionary->SetInteger("notifications", CONTENT_SETTING_BLOCK); | |
| 849 std::string key( | |
| 850 primary_pattern.ToString() + "," + | |
| 851 secondary_pattern.ToString()); | |
| 852 all_settings_dictionary->SetWithoutPathExpansion( | |
| 853 key, settings_dictionary->DeepCopy()); | |
| 854 | |
| 855 settings_dictionary.reset(new DictionaryValue()); | |
| 856 settings_dictionary->SetInteger("notifications", CONTENT_SETTING_ALLOW); | |
| 857 key = primary_pattern_2.ToString() + "," + secondary_pattern.ToString(); | |
| 858 all_settings_dictionary->SetWithoutPathExpansion( | |
| 859 key, settings_dictionary->DeepCopy()); | |
| 860 } | |
| 861 | |
| 862 // Test if the obsolete notifications preference is kept in sync if the new | |
| 863 // preference is changed by a sync. | |
| 864 const ListValue* denied_origin_list = | |
| 865 prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); | |
| 866 EXPECT_EQ(1U, denied_origin_list->GetSize()); | |
| 867 const ListValue* allowed_origin_list = | |
| 868 prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); | |
| 869 EXPECT_EQ(1U, allowed_origin_list->GetSize()); | |
| 870 | |
| 871 provider.ShutdownOnUIThread(); | |
| 872 } | |
| 873 | |
| 874 } // namespace content_settings | 737 } // namespace content_settings |
| OLD | NEW |