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 <memory> | 5 #include <memory> |
6 #include <string> | 6 #include <string> |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/test/scoped_feature_list.h" |
15 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 16 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
16 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 17 #include "chrome/browser/content_settings/cookie_settings_factory.h" |
17 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 18 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
18 #include "chrome/browser/content_settings/mock_settings_observer.h" | 19 #include "chrome/browser/content_settings/mock_settings_observer.h" |
| 20 #include "chrome/common/chrome_features.h" |
19 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
20 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
21 #include "components/content_settings/core/browser/content_settings_details.h" | 23 #include "components/content_settings/core/browser/content_settings_details.h" |
22 #include "components/content_settings/core/browser/cookie_settings.h" | 24 #include "components/content_settings/core/browser/cookie_settings.h" |
23 #include "components/content_settings/core/browser/host_content_settings_map.h" | 25 #include "components/content_settings/core/browser/host_content_settings_map.h" |
24 #include "components/content_settings/core/browser/website_settings_info.h" | 26 #include "components/content_settings/core/browser/website_settings_info.h" |
25 #include "components/content_settings/core/browser/website_settings_registry.h" | 27 #include "components/content_settings/core/browser/website_settings_registry.h" |
26 #include "components/content_settings/core/common/pref_names.h" | 28 #include "components/content_settings/core/common/pref_names.h" |
27 #include "components/prefs/pref_service.h" | 29 #include "components/prefs/pref_service.h" |
28 #include "components/prefs/scoped_user_pref_update.h" | 30 #include "components/prefs/scoped_user_pref_update.h" |
29 #include "components/sync_preferences/testing_pref_service_syncable.h" | 31 #include "components/sync_preferences/testing_pref_service_syncable.h" |
30 #include "content/public/test/test_browser_thread.h" | 32 #include "content/public/test/test_browser_thread.h" |
31 #include "net/base/static_cookie_policy.h" | 33 #include "net/base/static_cookie_policy.h" |
32 #include "ppapi/features/features.h" | 34 #include "ppapi/features/features.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
34 #include "url/gurl.h" | 36 #include "url/gurl.h" |
35 | 37 |
36 using content::BrowserThread; | 38 using content::BrowserThread; |
37 | 39 |
38 using ::testing::_; | 40 using ::testing::_; |
39 | 41 |
40 namespace { | 42 namespace { |
41 | 43 |
42 bool MatchPrimaryPattern(const ContentSettingsPattern& expected_primary, | 44 bool MatchPrimaryPattern(const ContentSettingsPattern& expected_primary, |
43 const ContentSettingsPattern& primary_pattern, | 45 const ContentSettingsPattern& primary_pattern, |
44 const ContentSettingsPattern& secondary_pattern) { | 46 const ContentSettingsPattern& secondary_pattern, |
| 47 base::Time last_modified) { |
45 return expected_primary == primary_pattern; | 48 return expected_primary == primary_pattern; |
46 } | 49 } |
47 | 50 |
| 51 bool MatchLastModified(base::Time begin_time, |
| 52 const ContentSettingsPattern& primary_pattern, |
| 53 const ContentSettingsPattern& secondary_pattern, |
| 54 base::Time last_modified) { |
| 55 return last_modified >= begin_time; |
| 56 } |
| 57 |
48 } // namespace | 58 } // namespace |
49 | 59 |
50 class HostContentSettingsMapTest : public testing::Test { | 60 class HostContentSettingsMapTest : public testing::Test { |
51 public: | 61 public: |
52 HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) { | 62 HostContentSettingsMapTest() : ui_thread_(BrowserThread::UI, &message_loop_) { |
53 } | 63 } |
54 | 64 |
55 protected: | 65 protected: |
56 const std::string& GetPrefName(ContentSettingsType type) { | 66 const std::string& GetPrefName(ContentSettingsType type) { |
57 return content_settings::WebsiteSettingsRegistry::GetInstance() | 67 return content_settings::WebsiteSettingsRegistry::GetInstance() |
(...skipping 1623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1681 host_content_settings_map->SetContentSettingCustomScope( | 1691 host_content_settings_map->SetContentSettingCustomScope( |
1682 pattern, ContentSettingsPattern::Wildcard(), | 1692 pattern, ContentSettingsPattern::Wildcard(), |
1683 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); | 1693 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); |
1684 host_content_settings_map->SetWebsiteSettingCustomScope( | 1694 host_content_settings_map->SetWebsiteSettingCustomScope( |
1685 pattern2, ContentSettingsPattern::Wildcard(), | 1695 pattern2, ContentSettingsPattern::Wildcard(), |
1686 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), | 1696 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), |
1687 base::WrapUnique(new base::DictionaryValue())); | 1697 base::WrapUnique(new base::DictionaryValue())); |
1688 | 1698 |
1689 // First, test that we clear only COOKIES (not APP_BANNER), and pattern2. | 1699 // First, test that we clear only COOKIES (not APP_BANNER), and pattern2. |
1690 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( | 1700 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( |
1691 CONTENT_SETTINGS_TYPE_COOKIES, | 1701 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), |
1692 base::Bind(&MatchPrimaryPattern, pattern2)); | 1702 base::Bind(&MatchPrimaryPattern, pattern2)); |
1693 host_content_settings_map->GetSettingsForOneType( | 1703 host_content_settings_map->GetSettingsForOneType( |
1694 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), &host_settings); | 1704 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), &host_settings); |
1695 // |host_settings| contains default & block. | 1705 // |host_settings| contains default & block. |
1696 EXPECT_EQ(2U, host_settings.size()); | 1706 EXPECT_EQ(2U, host_settings.size()); |
1697 EXPECT_EQ(pattern, host_settings[0].primary_pattern); | 1707 EXPECT_EQ(pattern, host_settings[0].primary_pattern); |
1698 EXPECT_EQ("*", host_settings[0].secondary_pattern.ToString()); | 1708 EXPECT_EQ("*", host_settings[0].secondary_pattern.ToString()); |
1699 EXPECT_EQ("*", host_settings[1].primary_pattern.ToString()); | 1709 EXPECT_EQ("*", host_settings[1].primary_pattern.ToString()); |
1700 EXPECT_EQ("*", host_settings[1].secondary_pattern.ToString()); | 1710 EXPECT_EQ("*", host_settings[1].secondary_pattern.ToString()); |
1701 | 1711 |
(...skipping 24 matching lines...) Expand all Loading... |
1726 host_content_settings_map->GetSettingsForOneType( | 1736 host_content_settings_map->GetSettingsForOneType( |
1727 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); | 1737 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); |
1728 EXPECT_EQ(2u, host_settings.size()); | 1738 EXPECT_EQ(2u, host_settings.size()); |
1729 | 1739 |
1730 // Clear the http one, which we should be able to do w/ the origin only, as | 1740 // Clear the http one, which we should be able to do w/ the origin only, as |
1731 // the scope of CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT is | 1741 // the scope of CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT is |
1732 // REQUESTING_ORIGIN_ONLY_SCOPE. | 1742 // REQUESTING_ORIGIN_ONLY_SCOPE. |
1733 ContentSettingsPattern http_pattern = | 1743 ContentSettingsPattern http_pattern = |
1734 ContentSettingsPattern::FromURLNoWildcard(url3_origin_only); | 1744 ContentSettingsPattern::FromURLNoWildcard(url3_origin_only); |
1735 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( | 1745 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( |
1736 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 1746 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), |
1737 base::Bind(&MatchPrimaryPattern, http_pattern)); | 1747 base::Bind(&MatchPrimaryPattern, http_pattern)); |
1738 // Verify we only have one, and it's url1. | 1748 // Verify we only have one, and it's url1. |
1739 host_content_settings_map->GetSettingsForOneType( | 1749 host_content_settings_map->GetSettingsForOneType( |
1740 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); | 1750 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); |
1741 EXPECT_EQ(1u, host_settings.size()); | 1751 EXPECT_EQ(1u, host_settings.size()); |
1742 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), | 1752 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), |
1743 host_settings[0].primary_pattern); | 1753 host_settings[0].primary_pattern); |
1744 } | 1754 } |
1745 | 1755 |
| 1756 TEST_F(HostContentSettingsMapTest, ClearSettingsWithTimePredicate) { |
| 1757 base::test::ScopedFeatureList feature_list; |
| 1758 feature_list.InitAndEnableFeature(features::kTabsInCbd); |
| 1759 |
| 1760 TestingProfile profile; |
| 1761 HostContentSettingsMap* host_content_settings_map = |
| 1762 HostContentSettingsMapFactory::GetForProfile(&profile); |
| 1763 ContentSettingsForOneType host_settings; |
| 1764 |
| 1765 GURL url1("https://www.google.com/"); |
| 1766 GURL url2("https://maps.google.com/"); |
| 1767 |
| 1768 // Add setting for url1. |
| 1769 host_content_settings_map->SetContentSettingDefaultScope( |
| 1770 url1, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, std::string(), |
| 1771 CONTENT_SETTING_BLOCK); |
| 1772 |
| 1773 base::Time t2 = base::Time::Now(); |
| 1774 // Add setting for url2. |
| 1775 host_content_settings_map->SetContentSettingDefaultScope( |
| 1776 url2, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, std::string(), |
| 1777 CONTENT_SETTING_BLOCK); |
| 1778 |
| 1779 // Verify we have two pattern and the default. |
| 1780 host_content_settings_map->GetSettingsForOneType( |
| 1781 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings); |
| 1782 EXPECT_EQ(3u, host_settings.size()); |
| 1783 |
| 1784 // Clear all settings since t2. |
| 1785 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( |
| 1786 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), |
| 1787 base::Bind(&MatchLastModified, t2)); |
| 1788 |
| 1789 // Verify we only have one pattern (url1) and the default. |
| 1790 host_content_settings_map->GetSettingsForOneType( |
| 1791 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings); |
| 1792 EXPECT_EQ(2u, host_settings.size()); |
| 1793 EXPECT_EQ("https://www.google.com:443", |
| 1794 host_settings[0].primary_pattern.ToString()); |
| 1795 EXPECT_EQ("*", host_settings[1].primary_pattern.ToString()); |
| 1796 } |
| 1797 |
| 1798 TEST_F(HostContentSettingsMapTest, ClearSettingsWithTimePredicateAndResource) { |
| 1799 base::test::ScopedFeatureList feature_list; |
| 1800 feature_list.InitAndEnableFeature(features::kTabsInCbd); |
| 1801 |
| 1802 TestingProfile profile; |
| 1803 HostContentSettingsMap* host_content_settings_map = |
| 1804 HostContentSettingsMapFactory::GetForProfile(&profile); |
| 1805 ContentSettingsForOneType host_settings; |
| 1806 |
| 1807 GURL url1("https://www.google.com/"); |
| 1808 GURL url2("https://maps.google.com/"); |
| 1809 |
| 1810 // Add setting for a resource. |
| 1811 host_content_settings_map->SetContentSettingDefaultScope( |
| 1812 url1, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, "r1", CONTENT_SETTING_BLOCK); |
| 1813 host_content_settings_map->SetContentSettingDefaultScope( |
| 1814 url2, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, "r1", CONTENT_SETTING_BLOCK); |
| 1815 |
| 1816 base::Time t2 = base::Time::Now(); |
| 1817 // Add setting for a different resource. |
| 1818 host_content_settings_map->SetContentSettingDefaultScope( |
| 1819 url1, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, "r2", CONTENT_SETTING_BLOCK); |
| 1820 |
| 1821 // Verify we have two pattern. |
| 1822 host_content_settings_map->GetSettingsForOneType( |
| 1823 CONTENT_SETTINGS_TYPE_PLUGINS, "r1", &host_settings); |
| 1824 EXPECT_EQ(2u, host_settings.size()); |
| 1825 host_content_settings_map->GetSettingsForOneType( |
| 1826 CONTENT_SETTINGS_TYPE_PLUGINS, "r2", &host_settings); |
| 1827 EXPECT_EQ(1u, host_settings.size()); |
| 1828 |
| 1829 // Clear all settings since t2. |
| 1830 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( |
| 1831 CONTENT_SETTINGS_TYPE_PLUGINS, "r1", base::Bind(&MatchLastModified, t2)); |
| 1832 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( |
| 1833 CONTENT_SETTINGS_TYPE_PLUGINS, "r2", base::Bind(&MatchLastModified, t2)); |
| 1834 |
| 1835 // Verify we only have one pattern (url2). Both settings |
| 1836 // for url1 were deleted because a change for a single resource affects |
| 1837 // the timestamp for all resources of a url. |
| 1838 host_content_settings_map->GetSettingsForOneType( |
| 1839 CONTENT_SETTINGS_TYPE_PLUGINS, "r1", &host_settings); |
| 1840 EXPECT_EQ(2u, host_settings.size()); |
| 1841 host_content_settings_map->GetSettingsForOneType( |
| 1842 CONTENT_SETTINGS_TYPE_PLUGINS, "r2", &host_settings); |
| 1843 EXPECT_EQ(0u, host_settings.size()); |
| 1844 } |
| 1845 |
1746 TEST_F(HostContentSettingsMapTest, CanSetNarrowestSetting) { | 1846 TEST_F(HostContentSettingsMapTest, CanSetNarrowestSetting) { |
1747 TestingProfile profile; | 1847 TestingProfile profile; |
1748 const auto* map = HostContentSettingsMapFactory::GetForProfile(&profile); | 1848 const auto* map = HostContentSettingsMapFactory::GetForProfile(&profile); |
1749 | 1849 |
1750 GURL valid_url("http://google.com"); | 1850 GURL valid_url("http://google.com"); |
1751 EXPECT_TRUE(map->CanSetNarrowestContentSetting( | 1851 EXPECT_TRUE(map->CanSetNarrowestContentSetting( |
1752 valid_url, valid_url, | 1852 valid_url, valid_url, |
1753 CONTENT_SETTINGS_TYPE_POPUPS)); | 1853 CONTENT_SETTINGS_TYPE_POPUPS)); |
1754 | 1854 |
1755 GURL invalid_url("about:blank"); | 1855 GURL invalid_url("about:blank"); |
1756 EXPECT_FALSE(map->CanSetNarrowestContentSetting( | 1856 EXPECT_FALSE(map->CanSetNarrowestContentSetting( |
1757 invalid_url, invalid_url, | 1857 invalid_url, invalid_url, |
1758 CONTENT_SETTINGS_TYPE_POPUPS)); | 1858 CONTENT_SETTINGS_TYPE_POPUPS)); |
1759 } | 1859 } |
1760 | 1860 |
OLD | NEW |