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" |
| 15 #include "base/test/test_timeouts.h" |
| 16 #include "base/threading/platform_thread.h" |
14 #include "chrome/browser/content_settings/content_settings_mock_observer.h" | 17 #include "chrome/browser/content_settings/content_settings_mock_observer.h" |
15 #include "chrome/browser/content_settings/cookie_settings_factory.h" | 18 #include "chrome/browser/content_settings/cookie_settings_factory.h" |
16 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 19 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
17 #include "chrome/browser/content_settings/mock_settings_observer.h" | 20 #include "chrome/browser/content_settings/mock_settings_observer.h" |
| 21 #include "chrome/common/chrome_features.h" |
18 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
19 #include "chrome/test/base/testing_profile.h" | 23 #include "chrome/test/base/testing_profile.h" |
20 #include "components/content_settings/core/browser/content_settings_details.h" | 24 #include "components/content_settings/core/browser/content_settings_details.h" |
21 #include "components/content_settings/core/browser/cookie_settings.h" | 25 #include "components/content_settings/core/browser/cookie_settings.h" |
22 #include "components/content_settings/core/browser/host_content_settings_map.h" | 26 #include "components/content_settings/core/browser/host_content_settings_map.h" |
23 #include "components/content_settings/core/browser/website_settings_info.h" | 27 #include "components/content_settings/core/browser/website_settings_info.h" |
24 #include "components/content_settings/core/browser/website_settings_registry.h" | 28 #include "components/content_settings/core/browser/website_settings_registry.h" |
25 #include "components/content_settings/core/common/pref_names.h" | 29 #include "components/content_settings/core/common/pref_names.h" |
26 #include "components/prefs/pref_service.h" | 30 #include "components/prefs/pref_service.h" |
27 #include "components/prefs/scoped_user_pref_update.h" | 31 #include "components/prefs/scoped_user_pref_update.h" |
(...skipping 1648 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1676 host_content_settings_map->SetContentSettingCustomScope( | 1680 host_content_settings_map->SetContentSettingCustomScope( |
1677 pattern, ContentSettingsPattern::Wildcard(), | 1681 pattern, ContentSettingsPattern::Wildcard(), |
1678 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); | 1682 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); |
1679 host_content_settings_map->SetWebsiteSettingCustomScope( | 1683 host_content_settings_map->SetWebsiteSettingCustomScope( |
1680 pattern2, ContentSettingsPattern::Wildcard(), | 1684 pattern2, ContentSettingsPattern::Wildcard(), |
1681 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), | 1685 CONTENT_SETTINGS_TYPE_APP_BANNER, std::string(), |
1682 base::WrapUnique(new base::DictionaryValue())); | 1686 base::WrapUnique(new base::DictionaryValue())); |
1683 | 1687 |
1684 // First, test that we clear only COOKIES (not APP_BANNER), and pattern2. | 1688 // First, test that we clear only COOKIES (not APP_BANNER), and pattern2. |
1685 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( | 1689 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( |
1686 CONTENT_SETTINGS_TYPE_COOKIES, | 1690 CONTENT_SETTINGS_TYPE_COOKIES, base::Time(), |
1687 base::Bind(&MatchPrimaryPattern, pattern2)); | 1691 base::Bind(&MatchPrimaryPattern, pattern2)); |
1688 host_content_settings_map->GetSettingsForOneType( | 1692 host_content_settings_map->GetSettingsForOneType( |
1689 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), &host_settings); | 1693 CONTENT_SETTINGS_TYPE_COOKIES, std::string(), &host_settings); |
1690 // |host_settings| contains default & block. | 1694 // |host_settings| contains default & block. |
1691 EXPECT_EQ(2U, host_settings.size()); | 1695 EXPECT_EQ(2U, host_settings.size()); |
1692 EXPECT_EQ(pattern, host_settings[0].primary_pattern); | 1696 EXPECT_EQ(pattern, host_settings[0].primary_pattern); |
1693 EXPECT_EQ("*", host_settings[0].secondary_pattern.ToString()); | 1697 EXPECT_EQ("*", host_settings[0].secondary_pattern.ToString()); |
1694 EXPECT_EQ("*", host_settings[1].primary_pattern.ToString()); | 1698 EXPECT_EQ("*", host_settings[1].primary_pattern.ToString()); |
1695 EXPECT_EQ("*", host_settings[1].secondary_pattern.ToString()); | 1699 EXPECT_EQ("*", host_settings[1].secondary_pattern.ToString()); |
1696 | 1700 |
(...skipping 24 matching lines...) Expand all Loading... |
1721 host_content_settings_map->GetSettingsForOneType( | 1725 host_content_settings_map->GetSettingsForOneType( |
1722 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); | 1726 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); |
1723 EXPECT_EQ(2u, host_settings.size()); | 1727 EXPECT_EQ(2u, host_settings.size()); |
1724 | 1728 |
1725 // Clear the http one, which we should be able to do w/ the origin only, as | 1729 // Clear the http one, which we should be able to do w/ the origin only, as |
1726 // the scope of CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT is | 1730 // the scope of CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT is |
1727 // REQUESTING_ORIGIN_ONLY_SCOPE. | 1731 // REQUESTING_ORIGIN_ONLY_SCOPE. |
1728 ContentSettingsPattern http_pattern = | 1732 ContentSettingsPattern http_pattern = |
1729 ContentSettingsPattern::FromURLNoWildcard(url3_origin_only); | 1733 ContentSettingsPattern::FromURLNoWildcard(url3_origin_only); |
1730 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( | 1734 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( |
1731 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, | 1735 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, base::Time(), |
1732 base::Bind(&MatchPrimaryPattern, http_pattern)); | 1736 base::Bind(&MatchPrimaryPattern, http_pattern)); |
1733 // Verify we only have one, and it's url1. | 1737 // Verify we only have one, and it's url1. |
1734 host_content_settings_map->GetSettingsForOneType( | 1738 host_content_settings_map->GetSettingsForOneType( |
1735 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); | 1739 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); |
1736 EXPECT_EQ(1u, host_settings.size()); | 1740 EXPECT_EQ(1u, host_settings.size()); |
1737 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), | 1741 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), |
1738 host_settings[0].primary_pattern); | 1742 host_settings[0].primary_pattern); |
1739 } | 1743 } |
1740 | 1744 |
| 1745 TEST_F(HostContentSettingsMapTest, ClearSettingsWithTimePredicate) { |
| 1746 base::test::ScopedFeatureList feature_list; |
| 1747 feature_list.InitAndEnableFeature(features::kTabsInCbd); |
| 1748 |
| 1749 TestingProfile profile; |
| 1750 HostContentSettingsMap* host_content_settings_map = |
| 1751 HostContentSettingsMapFactory::GetForProfile(&profile); |
| 1752 ContentSettingsForOneType host_settings; |
| 1753 |
| 1754 GURL url1("https://www.google.com/"); |
| 1755 GURL url2("https://maps.google.com/"); |
| 1756 |
| 1757 // Add setting for url1. |
| 1758 host_content_settings_map->SetContentSettingDefaultScope( |
| 1759 url1, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, std::string(), |
| 1760 CONTENT_SETTING_BLOCK); |
| 1761 |
| 1762 // Make sure that the timestamp for url1 is different from |t|. |
| 1763 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 1764 base::Time t = base::Time::Now(); |
| 1765 |
| 1766 // Add setting for url2. |
| 1767 host_content_settings_map->SetContentSettingDefaultScope( |
| 1768 url2, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, std::string(), |
| 1769 CONTENT_SETTING_BLOCK); |
| 1770 |
| 1771 // Verify we have two pattern and the default. |
| 1772 host_content_settings_map->GetSettingsForOneType( |
| 1773 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings); |
| 1774 EXPECT_EQ(3u, host_settings.size()); |
| 1775 |
| 1776 // Clear all settings since |t|. |
| 1777 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( |
| 1778 CONTENT_SETTINGS_TYPE_POPUPS, t, |
| 1779 HostContentSettingsMap::PatternSourcePredicate()); |
| 1780 |
| 1781 // Verify we only have one pattern (url1) and the default. |
| 1782 host_content_settings_map->GetSettingsForOneType( |
| 1783 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings); |
| 1784 EXPECT_EQ(2u, host_settings.size()); |
| 1785 EXPECT_EQ("https://www.google.com:443", |
| 1786 host_settings[0].primary_pattern.ToString()); |
| 1787 EXPECT_EQ("*", host_settings[1].primary_pattern.ToString()); |
| 1788 |
| 1789 // Clear all settings since the beginning of time. |
| 1790 host_content_settings_map->ClearSettingsForOneTypeWithPredicate( |
| 1791 CONTENT_SETTINGS_TYPE_POPUPS, base::Time(), |
| 1792 HostContentSettingsMap::PatternSourcePredicate()); |
| 1793 |
| 1794 // Verify we only have the default setting. |
| 1795 host_content_settings_map->GetSettingsForOneType( |
| 1796 CONTENT_SETTINGS_TYPE_POPUPS, std::string(), &host_settings); |
| 1797 EXPECT_EQ(1u, host_settings.size()); |
| 1798 EXPECT_EQ("*", host_settings[0].primary_pattern.ToString()); |
| 1799 } |
| 1800 |
1741 TEST_F(HostContentSettingsMapTest, CanSetNarrowestSetting) { | 1801 TEST_F(HostContentSettingsMapTest, CanSetNarrowestSetting) { |
1742 TestingProfile profile; | 1802 TestingProfile profile; |
1743 const auto* map = HostContentSettingsMapFactory::GetForProfile(&profile); | 1803 const auto* map = HostContentSettingsMapFactory::GetForProfile(&profile); |
1744 | 1804 |
1745 GURL valid_url("http://google.com"); | 1805 GURL valid_url("http://google.com"); |
1746 EXPECT_TRUE(map->CanSetNarrowestContentSetting( | 1806 EXPECT_TRUE(map->CanSetNarrowestContentSetting( |
1747 valid_url, valid_url, | 1807 valid_url, valid_url, |
1748 CONTENT_SETTINGS_TYPE_POPUPS)); | 1808 CONTENT_SETTINGS_TYPE_POPUPS)); |
1749 | 1809 |
1750 GURL invalid_url("about:blank"); | 1810 GURL invalid_url("about:blank"); |
1751 EXPECT_FALSE(map->CanSetNarrowestContentSetting( | 1811 EXPECT_FALSE(map->CanSetNarrowestContentSetting( |
1752 invalid_url, invalid_url, | 1812 invalid_url, invalid_url, |
1753 CONTENT_SETTINGS_TYPE_POPUPS)); | 1813 CONTENT_SETTINGS_TYPE_POPUPS)); |
1754 } | 1814 } |
OLD | NEW |