diff options
author | lshang <lshang@chromium.org> | 2016-03-02 21:08:06 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-03-03 05:09:30 +0000 |
commit | 48657c27ec55dc003cdad3e2472db8a3553ab3c4 (patch) | |
tree | 104200267e2ac1c422997e83b53a0e4507a674f9 | |
parent | f1f20728a18fff332af52c0e60726a8b34510ba6 (diff) | |
download | chromium_src-48657c27ec55dc003cdad3e2472db8a3553ab3c4.zip chromium_src-48657c27ec55dc003cdad3e2472db8a3553ab3c4.tar.gz chromium_src-48657c27ec55dc003cdad3e2472db8a3553ab3c4.tar.bz2 |
Change HostContentSettingsMap::SetContentSetting to use GURLs instead of patterns
A new version of SetContentSetting which takes GURLs as parameters. The patterns
are determined by WebsiteSettingsInfo::ScopingType from the ContentSettingsType.
Developers can directly pass in GURLs to decrease errors and inconsistency.
Will incrementally change all the callsites of SetContentSetting to use the new
version in the following CLs.
BUG=551747
Review URL: https://codereview.chromium.org/1686343002
Cr-Commit-Position: refs/heads/master@{#378960}
4 files changed, 198 insertions, 295 deletions
diff --git a/chrome/browser/content_settings/content_settings_usages_state_unittest.cc b/chrome/browser/content_settings/content_settings_usages_state_unittest.cc index 8fcbb67..6c91889 100644 --- a/chrome/browser/content_settings/content_settings_usages_state_unittest.cc +++ b/chrome/browser/content_settings/content_settings_usages_state_unittest.cc @@ -46,21 +46,15 @@ class ContentSettingsUsagesStateTests : public testing::Test { CreateDetailsWithURL(url_0); state.DidNavigate(details); - HostContentSettingsMapFactory::GetForProfile(&profile)->SetContentSetting( - ContentSettingsPattern::FromURLNoWildcard(url_0), - ContentSettingsPattern::FromURLNoWildcard(url_0), - type, - std::string(), - CONTENT_SETTING_ALLOW); + HostContentSettingsMapFactory::GetForProfile(&profile) + ->SetContentSettingDefaultScope(url_0, url_0, type, std::string(), + CONTENT_SETTING_ALLOW); state.OnPermissionSet(url_0, true); GURL url_1("http://www.example1.com"); - HostContentSettingsMapFactory::GetForProfile(&profile)->SetContentSetting( - ContentSettingsPattern::FromURLNoWildcard(url_1), - ContentSettingsPattern::FromURLNoWildcard(url_0), - type, - std::string(), - CONTENT_SETTING_BLOCK); + HostContentSettingsMapFactory::GetForProfile(&profile) + ->SetContentSettingDefaultScope(url_1, url_0, type, std::string(), + CONTENT_SETTING_BLOCK); state.OnPermissionSet(url_1, false); ContentSettingsUsagesState::StateMap state_map = @@ -152,30 +146,21 @@ class ContentSettingsUsagesStateTests : public testing::Test { CreateDetailsWithURL(url_0); state.DidNavigate(details); - HostContentSettingsMapFactory::GetForProfile(&profile)->SetContentSetting( - ContentSettingsPattern::FromURLNoWildcard(url_0), - ContentSettingsPattern::FromURLNoWildcard(url_0), - type, - std::string(), - CONTENT_SETTING_ALLOW); + HostContentSettingsMapFactory::GetForProfile(&profile) + ->SetContentSettingDefaultScope(url_0, url_0, type, std::string(), + CONTENT_SETTING_ALLOW); state.OnPermissionSet(url_0, true); GURL url_1("https://www.example.com"); - HostContentSettingsMapFactory::GetForProfile(&profile)->SetContentSetting( - ContentSettingsPattern::FromURLNoWildcard(url_1), - ContentSettingsPattern::FromURLNoWildcard(url_0), - type, - std::string(), - CONTENT_SETTING_ALLOW); + HostContentSettingsMapFactory::GetForProfile(&profile) + ->SetContentSettingDefaultScope(url_1, url_0, type, std::string(), + CONTENT_SETTING_ALLOW); state.OnPermissionSet(url_1, true); GURL url_2("http://www.example1.com"); - HostContentSettingsMapFactory::GetForProfile(&profile)->SetContentSetting( - ContentSettingsPattern::FromURLNoWildcard(url_2), - ContentSettingsPattern::FromURLNoWildcard(url_0), - type, - std::string(), - CONTENT_SETTING_ALLOW); + HostContentSettingsMapFactory::GetForProfile(&profile) + ->SetContentSettingDefaultScope(url_2, url_0, type, std::string(), + CONTENT_SETTING_ALLOW); state.OnPermissionSet(url_2, true); ContentSettingsUsagesState::StateMap state_map = diff --git a/chrome/browser/content_settings/host_content_settings_map_unittest.cc b/chrome/browser/content_settings/host_content_settings_map_unittest.cc index 217ef0c..a530604 100644 --- a/chrome/browser/content_settings/host_content_settings_map_unittest.cc +++ b/chrome/browser/content_settings/host_content_settings_map_unittest.cc @@ -102,25 +102,17 @@ TEST_F(HostContentSettingsMapTest, IndividualSettings) { // Check returning individual settings. GURL host("http://example.com/"); - ContentSettingsPattern pattern = - ContentSettingsPattern::FromString("[*.]example.com"); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_DEFAULT); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( @@ -132,30 +124,21 @@ TEST_F(HostContentSettingsMapTest, IndividualSettings) { #endif // Check returning all settings for a host. - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_DEFAULT); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_JAVASCRIPT, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string())); #if defined(ENABLE_PLUGINS) - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_PLUGINS, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), CONTENT_SETTING_ALLOW); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( @@ -177,28 +160,22 @@ TEST_F(HostContentSettingsMapTest, IndividualSettings) { EXPECT_EQ(CONTENT_SETTING_ASK, host_content_settings_map->GetContentSetting( host, host, CONTENT_SETTINGS_TYPE_MOUSELOCK, std::string())); - host_content_settings_map->SetContentSetting( - pattern, ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_KEYGEN, - std::string(), CONTENT_SETTING_ALLOW); + + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN, std::string(), + CONTENT_SETTING_ALLOW); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host, host, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); // Check returning all hosts for a setting. - ContentSettingsPattern pattern2 = - ContentSettingsPattern::FromString("[*.]example.org"); - host_content_settings_map->SetContentSetting( - pattern2, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + GURL host2("http://example.org/"); + host_content_settings_map->SetContentSettingDefaultScope( + host2, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); #if defined(ENABLE_PLUGINS) - host_content_settings_map->SetContentSetting( - pattern2, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_PLUGINS, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host2, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), CONTENT_SETTING_BLOCK); #endif ContentSettingsForOneType host_settings; @@ -224,35 +201,21 @@ TEST_F(HostContentSettingsMapTest, Clear) { HostContentSettingsMapFactory::GetForProfile(&profile); // Check clearing one type. - ContentSettingsPattern pattern = - ContentSettingsPattern::FromString("[*.]example.org"); - ContentSettingsPattern pattern2 = - ContentSettingsPattern::FromString("[*.]example.net"); - host_content_settings_map->SetContentSetting( - pattern2, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + GURL host("http://example.org/"); + GURL host2("http://example.net/"); + host_content_settings_map->SetContentSettingDefaultScope( + host2, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); #if defined(ENABLE_PLUGINS) - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_PLUGINS, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), CONTENT_SETTING_BLOCK); #endif - host_content_settings_map->SetContentSetting( - pattern2, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host2, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); host_content_settings_map->ClearSettingsForOneType( CONTENT_SETTINGS_TYPE_IMAGES); @@ -277,18 +240,11 @@ TEST_F(HostContentSettingsMapTest, Patterns) { GURL host1("http://example.com/"); GURL host2("http://www.example.com/"); GURL host3("http://example.org/"); - ContentSettingsPattern pattern1 = - ContentSettingsPattern::FromString("[*.]example.com"); - ContentSettingsPattern pattern2 = - ContentSettingsPattern::FromString("example.org"); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host1, host1, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); - host_content_settings_map->SetContentSetting( - pattern1, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host1, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( @@ -299,11 +255,8 @@ TEST_F(HostContentSettingsMapTest, Patterns) { EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host3, host3, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); - host_content_settings_map->SetContentSetting( - pattern2, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host3, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( @@ -316,6 +269,7 @@ TEST_F(HostContentSettingsMapTest, Observer) { HostContentSettingsMapFactory::GetForProfile(&profile); MockSettingsObserver observer(host_content_settings_map); + GURL host("http://example.com/"); ContentSettingsPattern primary_pattern = ContentSettingsPattern::FromString("[*.]example.com"); ContentSettingsPattern secondary_pattern = @@ -327,11 +281,8 @@ TEST_F(HostContentSettingsMapTest, Observer) { primary_pattern, secondary_pattern, false)); - host_content_settings_map->SetContentSetting( - primary_pattern, - secondary_pattern, - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_ALLOW); ::testing::Mock::VerifyAndClearExpectations(&observer); @@ -394,19 +345,14 @@ TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) { ->GetValue() ->DeepCopy()); - ContentSettingsPattern pattern = - ContentSettingsPattern::FromString("[*.]example.com"); GURL host("http://example.com"); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( @@ -438,8 +384,6 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { content_settings::CookieSettings* cookie_settings = CookieSettingsFactory::GetForProfile(&profile).get(); - ContentSettingsPattern pattern = - ContentSettingsPattern::FromString("[*.]example.com"); GURL host_ending_with_dot("http://example.com./"); EXPECT_EQ(CONTENT_SETTING_ALLOW, @@ -448,11 +392,8 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { host_ending_with_dot, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host_ending_with_dot, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_DEFAULT); EXPECT_EQ( CONTENT_SETTING_ALLOW, @@ -460,11 +401,8 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { host_ending_with_dot, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host_ending_with_dot, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ( CONTENT_SETTING_BLOCK, @@ -475,20 +413,14 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( host_ending_with_dot, host_ending_with_dot)); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_COOKIES, - std::string(), - CONTENT_SETTING_DEFAULT); + host_content_settings_map->SetContentSettingDefaultScope( + host_ending_with_dot, GURL(), CONTENT_SETTINGS_TYPE_COOKIES, + std::string(), CONTENT_SETTING_DEFAULT); EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( host_ending_with_dot, host_ending_with_dot)); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_COOKIES, - std::string(), - CONTENT_SETTING_BLOCK); + host_content_settings_map->SetContentSettingDefaultScope( + host_ending_with_dot, GURL(), CONTENT_SETTINGS_TYPE_COOKIES, + std::string(), CONTENT_SETTING_BLOCK); EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( host_ending_with_dot, host_ending_with_dot)); @@ -498,24 +430,18 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { host_ending_with_dot, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_JAVASCRIPT, - std::string(), - CONTENT_SETTING_DEFAULT); + host_content_settings_map->SetContentSettingDefaultScope( + host_ending_with_dot, GURL(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, + std::string(), CONTENT_SETTING_DEFAULT); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host_ending_with_dot, host_ending_with_dot, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_JAVASCRIPT, - std::string(), - CONTENT_SETTING_BLOCK); + host_content_settings_map->SetContentSettingDefaultScope( + host_ending_with_dot, GURL(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, + std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( host_ending_with_dot, @@ -530,24 +456,18 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { host_ending_with_dot, CONTENT_SETTINGS_TYPE_PLUGINS, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_PLUGINS, - std::string(), - CONTENT_SETTING_DEFAULT); + host_content_settings_map->SetContentSettingDefaultScope( + host_ending_with_dot, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, + std::string(), CONTENT_SETTING_DEFAULT); EXPECT_EQ(CONTENT_SETTING_DETECT_IMPORTANT_CONTENT, host_content_settings_map->GetContentSetting( host_ending_with_dot, host_ending_with_dot, CONTENT_SETTINGS_TYPE_PLUGINS, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_PLUGINS, - std::string(), - CONTENT_SETTING_BLOCK); + host_content_settings_map->SetContentSettingDefaultScope( + host_ending_with_dot, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, + std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( host_ending_with_dot, @@ -562,11 +482,8 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { host_ending_with_dot, CONTENT_SETTINGS_TYPE_POPUPS, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_POPUPS, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host_ending_with_dot, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, std::string(), CONTENT_SETTING_DEFAULT); EXPECT_EQ( CONTENT_SETTING_BLOCK, @@ -574,11 +491,8 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { host_ending_with_dot, CONTENT_SETTINGS_TYPE_POPUPS, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_POPUPS, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host_ending_with_dot, GURL(), CONTENT_SETTINGS_TYPE_POPUPS, std::string(), CONTENT_SETTING_ALLOW); EXPECT_EQ( CONTENT_SETTING_ALLOW, @@ -591,16 +505,16 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { host_content_settings_map->GetContentSetting( host_ending_with_dot, host_ending_with_dot, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); - host_content_settings_map->SetContentSetting( - pattern, ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_KEYGEN, - std::string(), CONTENT_SETTING_ALLOW); + host_content_settings_map->SetContentSettingDefaultScope( + host_ending_with_dot, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN, std::string(), + CONTENT_SETTING_ALLOW); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host_ending_with_dot, host_ending_with_dot, CONTENT_SETTINGS_TYPE_KEYGEN, std::string())); - host_content_settings_map->SetContentSetting( - pattern, ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_KEYGEN, - std::string(), CONTENT_SETTING_DEFAULT); + host_content_settings_map->SetContentSettingDefaultScope( + host_ending_with_dot, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN, std::string(), + CONTENT_SETTING_DEFAULT); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( host_ending_with_dot, host_ending_with_dot, @@ -613,32 +527,19 @@ TEST_F(HostContentSettingsMapTest, NestedSettings) { HostContentSettingsMapFactory::GetForProfile(&profile); GURL host("http://a.b.example.com/"); - ContentSettingsPattern pattern1 = - ContentSettingsPattern::FromString("[*.]example.com"); - ContentSettingsPattern pattern2 = - ContentSettingsPattern::FromString("[*.]b.example.com"); - ContentSettingsPattern pattern3 = - ContentSettingsPattern::FromString("a.b.example.com"); - - host_content_settings_map->SetContentSetting( - pattern1, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + GURL host1("http://example.com/"); + GURL host2("http://b.example.com/"); + + host_content_settings_map->SetContentSettingDefaultScope( + host1, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); - host_content_settings_map->SetContentSetting( - pattern2, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_COOKIES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host2, GURL(), CONTENT_SETTINGS_TYPE_COOKIES, std::string(), CONTENT_SETTING_BLOCK); - host_content_settings_map->SetContentSetting( - pattern3, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_PLUGINS, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_PLUGINS, std::string(), CONTENT_SETTING_BLOCK); host_content_settings_map->SetDefaultContentSetting( CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK); @@ -685,8 +586,6 @@ TEST_F(HostContentSettingsMapTest, OffTheRecord) { HostContentSettingsMapFactory::GetForProfile(otr_profile); GURL host("http://example.com/"); - ContentSettingsPattern pattern = - ContentSettingsPattern::FromString("[*.]example.com"); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( @@ -697,11 +596,8 @@ TEST_F(HostContentSettingsMapTest, OffTheRecord) { // Changing content settings on the main map should also affect the // incognito map. - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( @@ -712,11 +608,9 @@ TEST_F(HostContentSettingsMapTest, OffTheRecord) { // Changing content settings on the incognito map should NOT affect the // main map. - otr_map->SetContentSetting(pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), - CONTENT_SETTING_ALLOW); + otr_map->SetContentSettingDefaultScope(host, GURL(), + CONTENT_SETTINGS_TYPE_IMAGES, + std::string(), CONTENT_SETTING_ALLOW); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); @@ -737,8 +631,6 @@ TEST_F(HostContentSettingsMapTest, OffTheRecordPartialInheritPref) { HostContentSettingsMapFactory::GetForProfile(otr_profile); GURL host("http://example.com/"); - ContentSettingsPattern pattern = - ContentSettingsPattern::FromURLNoWildcard(host); EXPECT_EQ( CONTENT_SETTING_ASK, @@ -750,11 +642,8 @@ TEST_F(HostContentSettingsMapTest, OffTheRecordPartialInheritPref) { host, host, CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string())); // BLOCK should be inherited from the main map to the incognito map. - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_NOTIFICATIONS, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ( CONTENT_SETTING_BLOCK, @@ -767,11 +656,8 @@ TEST_F(HostContentSettingsMapTest, OffTheRecordPartialInheritPref) { // ALLOW should not be inherited from the main map to the incognito map (but // it still overwrites the BLOCK, hence incognito reverts to ASK). - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_NOTIFICATIONS, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_NOTIFICATIONS, std::string(), CONTENT_SETTING_ALLOW); EXPECT_EQ( CONTENT_SETTING_ALLOW, @@ -1011,21 +897,16 @@ TEST_F(HostContentSettingsMapTest, syncable_prefs::TestingPrefServiceSyncable* prefs = profile.GetTestingPrefService(); - // Set pattern for JavaScript setting. - ContentSettingsPattern pattern = - ContentSettingsPattern::FromString("[*.]example.com"); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_JAVASCRIPT, - std::string(), + // Set url for JavaScript setting. + GURL host("http://example.com/"); + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetDefaultContentSetting( CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL)); - GURL host("http://example.com/"); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string())); @@ -1053,20 +934,16 @@ TEST_F(HostContentSettingsMapTest, CONTENT_SETTINGS_TYPE_JAVASCRIPT, CONTENT_SETTING_BLOCK); // Set an exception to allow "[*.]example.com" - ContentSettingsPattern pattern = - ContentSettingsPattern::FromString("[*.]example.com"); + GURL host("http://example.com/"); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_JAVASCRIPT, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string(), CONTENT_SETTING_ALLOW); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetDefaultContentSetting( CONTENT_SETTINGS_TYPE_JAVASCRIPT, NULL)); - GURL host("http://example.com/"); + EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host, host, CONTENT_SETTINGS_TYPE_JAVASCRIPT, std::string())); @@ -1150,13 +1027,8 @@ TEST_F(HostContentSettingsMapTest, GetContentSetting) { GURL host("http://example.com/"); GURL embedder("chrome://foo"); - ContentSettingsPattern pattern = - ContentSettingsPattern::FromString("[*.]example.com"); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( @@ -1193,11 +1065,8 @@ TEST_F(HostContentSettingsMapTest, AddContentSettingsObserver) { EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); - host_content_settings_map->SetContentSetting( - pattern, - ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_DEFAULT); } @@ -1217,9 +1086,9 @@ TEST_F(HostContentSettingsMapTest, GuestProfile) { // Changing content settings should not result in any prefs being stored // however the value should be set in memory. - host_content_settings_map->SetContentSetting( - pattern, ContentSettingsPattern::Wildcard(), CONTENT_SETTINGS_TYPE_IMAGES, - std::string(), CONTENT_SETTING_BLOCK); + host_content_settings_map->SetContentSettingDefaultScope( + host, GURL(), CONTENT_SETTINGS_TYPE_IMAGES, std::string(), + CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, host_content_settings_map->GetContentSetting( host, host, CONTENT_SETTINGS_TYPE_IMAGES, std::string())); diff --git a/components/content_settings/core/browser/host_content_settings_map.cc b/components/content_settings/core/browser/host_content_settings_map.cc index 303ec98..d1151ea 100644 --- a/components/content_settings/core/browser/host_content_settings_map.cc +++ b/components/content_settings/core/browser/host_content_settings_map.cc @@ -34,6 +34,8 @@ #include "net/base/static_cookie_policy.h" #include "url/gurl.h" +using content_settings::WebsiteSettingsInfo; + namespace { typedef std::vector<content_settings::Rule> Rules; @@ -106,6 +108,31 @@ scoped_ptr<base::Value> ProcessIncognitoInheritanceBehavior( return value; } +content_settings::PatternPair GetPatternsFromScopingType( + WebsiteSettingsInfo::ScopingType scoping_type, + const GURL& primary_url, + const GURL& secondary_url) { + content_settings::PatternPair patterns; + + switch (scoping_type) { + case WebsiteSettingsInfo::TOP_LEVEL_DOMAIN_ONLY_SCOPE: + case WebsiteSettingsInfo::REQUESTING_DOMAIN_ONLY_SCOPE: + patterns.first = ContentSettingsPattern::FromURL(primary_url); + patterns.second = ContentSettingsPattern::Wildcard(); + break; + case WebsiteSettingsInfo::REQUESTING_ORIGIN_ONLY_SCOPE: + patterns.first = ContentSettingsPattern::FromURLNoWildcard(primary_url); + patterns.second = ContentSettingsPattern::Wildcard(); + break; + case WebsiteSettingsInfo::REQUESTING_ORIGIN_AND_TOP_LEVEL_ORIGIN_SCOPE: + patterns.first = ContentSettingsPattern::FromURLNoWildcard(primary_url); + patterns.second = + ContentSettingsPattern::FromURLNoWildcard(secondary_url); + break; + } + return patterns; +} + } // namespace HostContentSettingsMap::HostContentSettingsMap(PrefService* prefs, @@ -283,39 +310,16 @@ void HostContentSettingsMap::SetWebsiteSettingDefaultScope( ContentSettingsType content_type, const std::string& resource_identifier, base::Value* value) { - using content_settings::WebsiteSettingsInfo; - const WebsiteSettingsInfo* info = content_settings::WebsiteSettingsRegistry::GetInstance()->Get( content_type); - ContentSettingsPattern primary_pattern; - ContentSettingsPattern secondary_pattern; - switch (info->scoping_type()) { - case WebsiteSettingsInfo::TOP_LEVEL_DOMAIN_ONLY_SCOPE: - primary_pattern = ContentSettingsPattern::FromURL(top_level_url); - secondary_pattern = ContentSettingsPattern::Wildcard(); - DCHECK(requesting_url.is_empty()); - break; - case WebsiteSettingsInfo::REQUESTING_DOMAIN_ONLY_SCOPE: - primary_pattern = ContentSettingsPattern::FromURL(requesting_url); - secondary_pattern = ContentSettingsPattern::Wildcard(); - DCHECK(top_level_url.is_empty()); - break; - case WebsiteSettingsInfo::REQUESTING_ORIGIN_ONLY_SCOPE: - primary_pattern = - ContentSettingsPattern::FromURLNoWildcard(requesting_url); - secondary_pattern = ContentSettingsPattern::Wildcard(); - DCHECK(top_level_url.is_empty()); - break; - case WebsiteSettingsInfo::REQUESTING_ORIGIN_AND_TOP_LEVEL_ORIGIN_SCOPE: - primary_pattern = - ContentSettingsPattern::FromURLNoWildcard(requesting_url); - secondary_pattern = - ContentSettingsPattern::FromURLNoWildcard(top_level_url); - break; - } + content_settings::PatternPair patterns = GetPatternsFromScopingType( + info->scoping_type(), requesting_url, top_level_url); + ContentSettingsPattern primary_pattern = patterns.first; + ContentSettingsPattern secondary_pattern = patterns.second; if (!primary_pattern.IsValid() || !secondary_pattern.IsValid()) return; + SetWebsiteSettingCustomScope(primary_pattern, secondary_pattern, content_type, resource_identifier, make_scoped_ptr(value)); } @@ -437,6 +441,30 @@ void HostContentSettingsMap::SetContentSetting( resource_identifier, std::move(value)); } +void HostContentSettingsMap::SetContentSettingDefaultScope( + const GURL& primary_url, + const GURL& secondary_url, + ContentSettingsType content_type, + const std::string& resource_identifier, + ContentSetting setting) { + using content_settings::ContentSettingsInfo; + const ContentSettingsInfo* info = + content_settings::ContentSettingsRegistry::GetInstance()->Get( + content_type); + DCHECK(info); + + content_settings::PatternPair patterns = + GetPatternsFromScopingType(info->website_settings_info()->scoping_type(), + primary_url, secondary_url); + ContentSettingsPattern primary_pattern = patterns.first; + ContentSettingsPattern secondary_pattern = patterns.second; + if (!primary_pattern.IsValid() || !secondary_pattern.IsValid()) + return; + + SetContentSetting(primary_pattern, secondary_pattern, content_type, + resource_identifier, setting); +} + ContentSetting HostContentSettingsMap::GetContentSettingAndMaybeUpdateLastUsage( const GURL& primary_url, const GURL& secondary_url, diff --git a/components/content_settings/core/browser/host_content_settings_map.h b/components/content_settings/core/browser/host_content_settings_map.h index 98fe724..9273057 100644 --- a/components/content_settings/core/browser/host_content_settings_map.h +++ b/components/content_settings/core/browser/host_content_settings_map.h @@ -134,20 +134,41 @@ class HostContentSettingsMap : public content_settings::Observer, // Sets the content |setting| for the given patterns, |content_type| and // |resource_identifier|. Setting the value to CONTENT_SETTING_DEFAULT causes // the default setting for that type to be used when loading pages matching - // this pattern. + // this pattern. Unless adding a custom-scoped setting, most developers will + // want to use SetContentSettingDefaultScope() instead. + // // NOTICE: This is just a convenience method for content types that use // |CONTENT_SETTING| as their data type. For content types that use other // data types please use the method SetWebsiteSettingDefaultScope(). // // This should only be called on the UI thread. - // TODO(raymes): Create a version of this function which uses the default - // scope. void SetContentSetting(const ContentSettingsPattern& primary_pattern, const ContentSettingsPattern& secondary_pattern, ContentSettingsType content_type, const std::string& resource_identifier, ContentSetting setting); + // Sets the content |setting| for the default scope of the url that is + // appropriate for the given |content_type| and |resource_identifier|. + // Setting the value to CONTENT_SETTING_DEFAULT causes the default setting + // for that type to be used. + // + // NOTICE: This is just a convenience method for content types that use + // |CONTENT_SETTING| as their data type. For content types that use other + // data types please use the method SetWebsiteSettingDefaultScope(). + // + // This should only be called on the UI thread. + // + // Internally this will call SetContentSetting() with the default scope + // patterns for the given |content_type|. Developers will generally want + // to use this function instead of SetContentSetting() unless they need + // to specify custom scoping. + void SetContentSettingDefaultScope(const GURL& primary_url, + const GURL& secondary_url, + ContentSettingsType content_type, + const std::string& resource_identifier, + ContentSetting setting); + // Sets the |value| for the default scope of the url that is appropriate for // the given |content_type| and |resource_identifier|. Setting the value to // null removes the default pattern pair for this content type. |