Index: components/content_settings/core/browser/host_content_settings_map.cc |
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 303ec9855df7551c4c7d78eabbf5190e8278c968..1d1acc1e42a0554ead38b0d07fe049b808fac5c1 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; |
@@ -54,6 +56,26 @@ const ProviderNamesSourceMapEntry kProviderNamesSourceMap[] = { |
{"default", content_settings::SETTING_SOURCE_USER}, |
}; |
+const ContentSettingsType kMigrateContentSettingTypes[] = { |
+ // Only content types of scoping type: REQUESTING_DOMAIN_ONLY_SCOPE, |
+ // REQUESTING_ORIGIN_ONLY_SCOPE and TOP_LEVEL_DOMAIN_ONLY_SCOPE need to be |
+ // migrated. |
+ CONTENT_SETTINGS_TYPE_COOKIES, |
+ CONTENT_SETTINGS_TYPE_IMAGES, |
+ CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
+ CONTENT_SETTINGS_TYPE_PLUGINS, |
+ CONTENT_SETTINGS_TYPE_POPUPS, |
+ CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
+ CONTENT_SETTINGS_TYPE_MOUSELOCK, |
+ CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, |
+ CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA, |
+ CONTENT_SETTINGS_TYPE_PPAPI_BROKER, |
+ CONTENT_SETTINGS_TYPE_AUTOMATIC_DOWNLOADS, |
+ CONTENT_SETTINGS_TYPE_DURABLE_STORAGE, |
+ CONTENT_SETTINGS_TYPE_KEYGEN, |
+ CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS, |
+ CONTENT_SETTINGS_TYPE_MIXEDSCRIPT}; |
+ |
static_assert( |
arraysize(kProviderNamesSourceMap) == |
HostContentSettingsMap::NUM_PROVIDER_TYPES, |
@@ -106,6 +128,38 @@ scoped_ptr<base::Value> ProcessIncognitoInheritanceBehavior( |
return value; |
} |
+struct PatternPair { |
+ ContentSettingsPattern primary_pattern; |
+ ContentSettingsPattern secondary_pattern; |
+}; |
+ |
+PatternPair GetPatternsFromScopingType( |
+ WebsiteSettingsInfo::ScopingType scoping_type, |
+ const GURL& primary_url, |
+ const GURL& secondary_url) { |
+ PatternPair patterns; |
+ |
+ switch (scoping_type) { |
+ case WebsiteSettingsInfo::TOP_LEVEL_DOMAIN_ONLY_SCOPE: |
+ case WebsiteSettingsInfo::REQUESTING_DOMAIN_ONLY_SCOPE: |
+ patterns.primary_pattern = ContentSettingsPattern::FromURL(primary_url); |
+ patterns.secondary_pattern = ContentSettingsPattern::Wildcard(); |
+ break; |
+ case WebsiteSettingsInfo::REQUESTING_ORIGIN_ONLY_SCOPE: |
+ patterns.primary_pattern = |
+ ContentSettingsPattern::FromURLNoWildcard(primary_url); |
+ patterns.secondary_pattern = ContentSettingsPattern::Wildcard(); |
+ break; |
+ case WebsiteSettingsInfo::REQUESTING_ORIGIN_AND_TOP_LEVEL_ORIGIN_SCOPE: |
+ patterns.primary_pattern = |
+ ContentSettingsPattern::FromURLNoWildcard(primary_url); |
+ patterns.secondary_pattern = |
+ ContentSettingsPattern::FromURLNoWildcard(secondary_url); |
+ break; |
+ } |
+ return patterns; |
+} |
+ |
} // namespace |
HostContentSettingsMap::HostContentSettingsMap(PrefService* prefs, |
@@ -137,6 +191,8 @@ HostContentSettingsMap::HostContentSettingsMap(PrefService* prefs, |
new content_settings::DefaultProvider(prefs_, is_off_the_record_); |
default_provider->AddObserver(this); |
content_settings_providers_[DEFAULT_PROVIDER] = default_provider; |
+ |
+ MigrateOldSettings(); |
} |
// static |
@@ -283,39 +339,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; |
- } |
+ PatternPair patterns = GetPatternsFromScopingType( |
+ info->scoping_type(), requesting_url, top_level_url); |
+ ContentSettingsPattern primary_pattern = patterns.primary_pattern; |
+ ContentSettingsPattern secondary_pattern = patterns.secondary_pattern; |
if (!primary_pattern.IsValid() || !secondary_pattern.IsValid()) |
return; |
+ |
SetWebsiteSettingCustomScope(primary_pattern, secondary_pattern, content_type, |
resource_identifier, make_scoped_ptr(value)); |
} |
@@ -437,6 +470,87 @@ 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); |
+ |
+ PatternPair patterns = |
+ GetPatternsFromScopingType(info->website_settings_info()->scoping_type(), |
+ primary_url, secondary_url); |
+ ContentSettingsPattern primary_pattern = patterns.primary_pattern; |
+ ContentSettingsPattern secondary_pattern = patterns.secondary_pattern; |
+ if (!primary_pattern.IsValid() || !secondary_pattern.IsValid()) |
+ return; |
+ |
+ SetContentSettingCustomScope(primary_pattern, secondary_pattern, content_type, |
+ resource_identifier, setting); |
+} |
+ |
+void HostContentSettingsMap::SetContentSettingCustomScope( |
raymes
2016/03/01 06:17:32
Maybe we shouldn't add this yet - instead we could
lshang
2016/03/02 04:02:28
Done.
|
+ const ContentSettingsPattern& primary_pattern, |
+ const ContentSettingsPattern& secondary_pattern, |
+ ContentSettingsType content_type, |
+ const std::string& resource_identifier, |
+ ContentSetting setting) { |
+ DCHECK(content_settings::ContentSettingsRegistry::GetInstance()->Get( |
+ content_type)); |
+ if (setting == CONTENT_SETTING_ALLOW && |
+ (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION || |
+ content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS)) { |
+ UpdateLastUsageByPattern(primary_pattern, secondary_pattern, content_type); |
+ } |
+ |
+ scoped_ptr<base::Value> value; |
+ // A value of CONTENT_SETTING_DEFAULT implies deleting the content setting. |
+ if (setting != CONTENT_SETTING_DEFAULT) { |
+ DCHECK(content_settings::ContentSettingsRegistry::GetInstance() |
+ ->Get(content_type) |
+ ->IsSettingValid(setting)); |
+ value.reset(new base::FundamentalValue(setting)); |
+ } |
+ SetWebsiteSettingCustomScope(primary_pattern, secondary_pattern, content_type, |
+ resource_identifier, std::move(value)); |
+} |
+ |
+void HostContentSettingsMap::MigrateOldSettings() { |
+ for (const ContentSettingsType& type : kMigrateContentSettingTypes) { |
+ ContentSettingsForOneType settings; |
+ GetSettingsForOneType(type, std::string(), &settings); |
+ for (const ContentSettingPatternSource& setting_entry : settings) { |
+ // Migrate old-format settings only. |
+ if (setting_entry.secondary_pattern != |
+ ContentSettingsPattern::Wildcard()) { |
+ GURL url(setting_entry.primary_pattern.ToString()); |
+ // Pull out the value of the old-format setting. Only do this if the |
+ // patterns are as we expect them to be, otherwise the setting will just |
+ // be removed for safety. |
+ ContentSetting content_setting = CONTENT_SETTING_DEFAULT; |
+ if (setting_entry.primary_pattern == setting_entry.secondary_pattern && |
+ url.is_valid()) { |
+ content_setting = GetContentSetting(url, url, type, std::string()); |
+ } |
+ // Remove the old pattern. |
+ SetContentSettingCustomScope(setting_entry.primary_pattern, |
+ setting_entry.secondary_pattern, type, |
+ std::string(), CONTENT_SETTING_DEFAULT); |
+ // Set the new pattern. |
+ if (content_setting) { |
+ SetContentSettingDefaultScope(url, GURL(), type, std::string(), |
+ content_setting); |
+ } |
+ } |
+ } |
+ } |
+} |
+ |
ContentSetting HostContentSettingsMap::GetContentSettingAndMaybeUpdateLastUsage( |
const GURL& primary_url, |
const GURL& secondary_url, |