| 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/test/scoped_feature_list.h" | 14 #include "base/test/scoped_feature_list.h" |
| 15 #include "base/test/test_timeouts.h" | 15 #include "base/test/simple_test_clock.h" |
| 16 #include "base/threading/platform_thread.h" | |
| 17 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 16 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
| 18 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 17 #include "chrome/browser/content_settings/cookie_settings_factory.h" |
| 19 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 18 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| 20 #include "chrome/browser/content_settings/mock_settings_observer.h" | 19 #include "chrome/browser/content_settings/mock_settings_observer.h" |
| 21 #include "chrome/common/chrome_features.h" | 20 #include "chrome/common/chrome_features.h" |
| 22 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
| 23 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
| 24 #include "components/content_settings/core/browser/content_settings_details.h" | 23 #include "components/content_settings/core/browser/content_settings_details.h" |
| 25 #include "components/content_settings/core/browser/cookie_settings.h" | 24 #include "components/content_settings/core/browser/cookie_settings.h" |
| 26 #include "components/content_settings/core/browser/host_content_settings_map.h" | 25 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| (...skipping 1713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1740 EXPECT_EQ(1u, host_settings.size()); | 1739 EXPECT_EQ(1u, host_settings.size()); |
| 1741 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), | 1740 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), |
| 1742 host_settings[0].primary_pattern); | 1741 host_settings[0].primary_pattern); |
| 1743 } | 1742 } |
| 1744 | 1743 |
| 1745 TEST_F(HostContentSettingsMapTest, ClearSettingsWithTimePredicate) { | 1744 TEST_F(HostContentSettingsMapTest, ClearSettingsWithTimePredicate) { |
| 1746 base::test::ScopedFeatureList feature_list; | 1745 base::test::ScopedFeatureList feature_list; |
| 1747 feature_list.InitAndEnableFeature(features::kTabsInCbd); | 1746 feature_list.InitAndEnableFeature(features::kTabsInCbd); |
| 1748 | 1747 |
| 1749 TestingProfile profile; | 1748 TestingProfile profile; |
| 1750 HostContentSettingsMap* host_content_settings_map = | 1749 auto* map = HostContentSettingsMapFactory::GetForProfile(&profile); |
| 1751 HostContentSettingsMapFactory::GetForProfile(&profile); | 1750 auto* clock = new base::SimpleTestClock(); |
| 1751 clock->SetNow(base::Time::Now()); |
| 1752 map->SetClockForTesting(base::WrapUnique(clock)); |
| 1753 |
| 1752 ContentSettingsForOneType host_settings; | 1754 ContentSettingsForOneType host_settings; |
| 1753 | 1755 |
| 1754 GURL url1("https://www.google.com/"); | 1756 GURL url1("https://www.google.com/"); |
| 1755 GURL url2("https://maps.google.com/"); | 1757 GURL url2("https://maps.google.com/"); |
| 1756 | 1758 |
| 1757 // Add setting for url1. | 1759 // Add setting for url1. |
| 1758 host_content_settings_map->SetContentSettingDefaultScope( | 1760 map->SetContentSettingDefaultScope(url1, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, |
| 1759 url1, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, std::string(), | 1761 std::string(), CONTENT_SETTING_BLOCK); |
| 1760 CONTENT_SETTING_BLOCK); | |
| 1761 | 1762 |
| 1762 // Make sure that the timestamp for url1 is different from |t|. | 1763 // Make sure that the timestamp for url1 is different from |t|. |
| 1763 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); | 1764 clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 1764 base::Time t = base::Time::Now(); | 1765 base::Time t = clock->Now(); |
| 1765 | 1766 |
| 1766 // Add setting for url2. | 1767 // Add setting for url2. |
| 1767 host_content_settings_map->SetContentSettingDefaultScope( | 1768 map->SetContentSettingDefaultScope(url2, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, |
| 1768 url2, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, std::string(), | 1769 std::string(), CONTENT_SETTING_BLOCK); |
| 1769 CONTENT_SETTING_BLOCK); | |
| 1770 | 1770 |
| 1771 // Verify we have two pattern and the default. | 1771 // Verify we have two pattern and the default. |
| 1772 host_content_settings_map->GetSettingsForOneType( | 1772 map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS, std::string(), |
| 1773 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings); | 1773 &host_settings); |
| 1774 EXPECT_EQ(3u, host_settings.size()); | 1774 EXPECT_EQ(3u, host_settings.size()); |
| 1775 | 1775 |
| 1776 // Clear all settings since |t|. | 1776 // Clear all settings since |t|. |
| 1777 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( | 1777 map->ClearSettingsForOneTypeWithPredicate( |
| 1778 CONTENT_SETTINGS_TYPE_POPUPS, t, | 1778 CONTENT_SETTINGS_TYPE_POPUPS, t, |
| 1779 HostContentSettingsMap::PatternSourcePredicate()); | 1779 HostContentSettingsMap::PatternSourcePredicate()); |
| 1780 | 1780 |
| 1781 // Verify we only have one pattern (url1) and the default. | 1781 // Verify we only have one pattern (url1) and the default. |
| 1782 host_content_settings_map->GetSettingsForOneType( | 1782 map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS, std::string(), |
| 1783 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings); | 1783 &host_settings); |
| 1784 EXPECT_EQ(2u, host_settings.size()); | 1784 EXPECT_EQ(2u, host_settings.size()); |
| 1785 EXPECT_EQ("https://www.google.com:443", | 1785 EXPECT_EQ("https://www.google.com:443", |
| 1786 host_settings[0].primary_pattern.ToString()); | 1786 host_settings[0].primary_pattern.ToString()); |
| 1787 EXPECT_EQ("*", host_settings[1].primary_pattern.ToString()); | 1787 EXPECT_EQ("*", host_settings[1].primary_pattern.ToString()); |
| 1788 | 1788 |
| 1789 // Clear all settings since the beginning of time. | 1789 // Clear all settings since the beginning of time. |
| 1790 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( | 1790 map->ClearSettingsForOneTypeWithPredicate( |
| 1791 CONTENT_SETTINGS_TYPE_POPUPS, base::Time(), | 1791 CONTENT_SETTINGS_TYPE_POPUPS, base::Time(), |
| 1792 HostContentSettingsMap::PatternSourcePredicate()); | 1792 HostContentSettingsMap::PatternSourcePredicate()); |
| 1793 | 1793 |
| 1794 // Verify we only have the default setting. | 1794 // Verify we only have the default setting. |
| 1795 host_content_settings_map->GetSettingsForOneType( | 1795 map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_POPUPS, std::string(), |
| 1796 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings); | 1796 &host_settings); |
| 1797 EXPECT_EQ(1u, host_settings.size()); | 1797 EXPECT_EQ(1u, host_settings.size()); |
| 1798 EXPECT_EQ("*", host_settings[0].primary_pattern.ToString()); | 1798 EXPECT_EQ("*", host_settings[0].primary_pattern.ToString()); |
| 1799 } | 1799 } |
| 1800 | 1800 |
| 1801 TEST_F(HostContentSettingsMapTest, GetSettingLastModified) { |
| 1802 base::test::ScopedFeatureList feature_list; |
| 1803 feature_list.InitAndEnableFeature(features::kTabsInCbd); |
| 1804 |
| 1805 TestingProfile profile; |
| 1806 auto* map = HostContentSettingsMapFactory::GetForProfile(&profile); |
| 1807 auto* clock = new base::SimpleTestClock(); |
| 1808 clock->SetNow(base::Time::Now()); |
| 1809 map->SetClockForTesting(base::WrapUnique(clock)); |
| 1810 |
| 1811 ContentSettingsForOneType host_settings; |
| 1812 |
| 1813 GURL url("https://www.google.com/"); |
| 1814 ContentSettingsPattern pattern = |
| 1815 ContentSettingsPattern::FromURLNoWildcard(url); |
| 1816 |
| 1817 // Last modified date for non existant settings should be base::Time(). |
| 1818 base::Time t = map->GetSettingLastModifiedDate( |
| 1819 pattern, ContentSettingsPattern::Wildcard(), |
| 1820 CONTENT_SETTINGS_TYPE_POPUPS); |
| 1821 EXPECT_EQ(base::Time(), t); |
| 1822 |
| 1823 // Add setting for url. |
| 1824 map->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, |
| 1825 std::string(), CONTENT_SETTING_BLOCK); |
| 1826 t = map->GetSettingLastModifiedDate(pattern, |
| 1827 ContentSettingsPattern::Wildcard(), |
| 1828 CONTENT_SETTINGS_TYPE_POPUPS); |
| 1829 EXPECT_EQ(t, clock->Now()); |
| 1830 |
| 1831 clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 1832 // Modify setting. |
| 1833 map->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, |
| 1834 std::string(), CONTENT_SETTING_ALLOW); |
| 1835 |
| 1836 t = map->GetSettingLastModifiedDate(pattern, |
| 1837 ContentSettingsPattern::Wildcard(), |
| 1838 CONTENT_SETTINGS_TYPE_POPUPS); |
| 1839 EXPECT_EQ(t, clock->Now()); |
| 1840 } |
| 1841 |
| 1842 TEST_F(HostContentSettingsMapTest, LastModifiedIsNotRecordedWhenDisabled) { |
| 1843 base::test::ScopedFeatureList feature_list; |
| 1844 feature_list.InitAndDisableFeature(features::kTabsInCbd); |
| 1845 |
| 1846 TestingProfile profile; |
| 1847 auto* map = HostContentSettingsMapFactory::GetForProfile(&profile); |
| 1848 ContentSettingsForOneType host_settings; |
| 1849 |
| 1850 GURL url("https://www.google.com/"); |
| 1851 ContentSettingsPattern pattern = |
| 1852 ContentSettingsPattern::FromURLNoWildcard(url); |
| 1853 |
| 1854 // Add setting for url. |
| 1855 map->SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, |
| 1856 std::string(), CONTENT_SETTING_BLOCK); |
| 1857 |
| 1858 base::Time t = map->GetSettingLastModifiedDate( |
| 1859 pattern, ContentSettingsPattern::Wildcard(), |
| 1860 CONTENT_SETTINGS_TYPE_POPUPS); |
| 1861 EXPECT_EQ(base::Time(), t); |
| 1862 } |
| 1863 |
| 1801 TEST_F(HostContentSettingsMapTest, CanSetNarrowestSetting) { | 1864 TEST_F(HostContentSettingsMapTest, CanSetNarrowestSetting) { |
| 1802 TestingProfile profile; | 1865 TestingProfile profile; |
| 1803 const auto* map = HostContentSettingsMapFactory::GetForProfile(&profile); | 1866 const auto* map = HostContentSettingsMapFactory::GetForProfile(&profile); |
| 1804 | 1867 |
| 1805 GURL valid_url("http://google.com"); | 1868 GURL valid_url("http://google.com"); |
| 1806 EXPECT_TRUE(map->CanSetNarrowestContentSetting( | 1869 EXPECT_TRUE(map->CanSetNarrowestContentSetting( |
| 1807 valid_url, valid_url, | 1870 valid_url, valid_url, |
| 1808 CONTENT_SETTINGS_TYPE_POPUPS)); | 1871 CONTENT_SETTINGS_TYPE_POPUPS)); |
| 1809 | 1872 |
| 1810 GURL invalid_url("about:blank"); | 1873 GURL invalid_url("about:blank"); |
| 1811 EXPECT_FALSE(map->CanSetNarrowestContentSetting( | 1874 EXPECT_FALSE(map->CanSetNarrowestContentSetting( |
| 1812 invalid_url, invalid_url, | 1875 invalid_url, invalid_url, |
| 1813 CONTENT_SETTINGS_TYPE_POPUPS)); | 1876 CONTENT_SETTINGS_TYPE_POPUPS)); |
| 1814 } | 1877 } |
| OLD | NEW |