Commit afd97ff8 authored by lshang's avatar lshang Committed by Commit bot

Only Register() platform specific content settings types on different platforms

Content settings are not used on all platforms. Some content settings are only
used on desktop platforms, or desktop and android. We shouldn't register these
types on other platforms that aren't using them, so that we don't create unused
prefs for them and more importantly so that we don't sync settings to these
platforms which would go unused but be a potential privacy concern.

In this CL, an enum WebsiteSettingsInfo::Platform is added and used when
registering content settings to indicate which platform set it is applied to.

BUG=604632

Review-Url: https://codereview.chromium.org/1991623005
Cr-Commit-Position: refs/heads/master@{#398174}
parent 3a7c8270
......@@ -121,6 +121,12 @@ DefaultProvider::DefaultProvider(PrefService* prefs, bool incognito)
IntToContentSetting(prefs_->GetInteger(
GetPrefName(CONTENT_SETTINGS_TYPE_COOKIES))),
CONTENT_SETTING_NUM_SETTINGS);
UMA_HISTOGRAM_ENUMERATION(
"ContentSettings.DefaultPopupsSetting",
IntToContentSetting(prefs_->GetInteger(
GetPrefName(CONTENT_SETTINGS_TYPE_POPUPS))),
CONTENT_SETTING_NUM_SETTINGS);
#if !defined(OS_IOS)
UMA_HISTOGRAM_ENUMERATION(
"ContentSettings.DefaultImagesSetting",
IntToContentSetting(prefs_->GetInteger(
......@@ -136,11 +142,6 @@ DefaultProvider::DefaultProvider(PrefService* prefs, bool incognito)
IntToContentSetting(prefs_->GetInteger(
GetPrefName(CONTENT_SETTINGS_TYPE_PLUGINS))),
CONTENT_SETTING_NUM_SETTINGS);
UMA_HISTOGRAM_ENUMERATION(
"ContentSettings.DefaultPopupsSetting",
IntToContentSetting(prefs_->GetInteger(
GetPrefName(CONTENT_SETTINGS_TYPE_POPUPS))),
CONTENT_SETTING_NUM_SETTINGS);
UMA_HISTOGRAM_ENUMERATION(
"ContentSettings.DefaultLocationSetting",
IntToContentSetting(prefs_->GetInteger(
......@@ -186,7 +187,7 @@ DefaultProvider::DefaultProvider(PrefService* prefs, bool incognito)
IntToContentSetting(prefs_->GetInteger(
GetPrefName(CONTENT_SETTINGS_TYPE_BLUETOOTH_GUARD))),
CONTENT_SETTING_NUM_SETTINGS);
#endif
pref_change_registrar_.Init(prefs_);
PrefChangeRegistrar::NamedChangeCallback callback = base::Bind(
&DefaultProvider::OnPreferenceChanged, base::Unretained(this));
......
......@@ -54,6 +54,8 @@ class ContentSettingsRegistry {
void Init();
typedef uint32_t Platforms;
// Register a new content setting. This maps an origin to an ALLOW/ASK/BLOCK
// value (see the ContentSetting enum).
void Register(ContentSettingsType type,
......@@ -63,6 +65,7 @@ class ContentSettingsRegistry {
const std::vector<std::string>& whitelisted_schemes,
const std::set<ContentSetting>& valid_settings,
WebsiteSettingsInfo::ScopingType scoping_type,
Platforms platforms,
ContentSettingsInfo::IncognitoBehavior incognito_behavior);
Map content_settings_info_;
......
......@@ -32,6 +32,25 @@ class ContentSettingsRegistryTest : public testing::Test {
ContentSettingsRegistry registry_;
};
TEST_F(ContentSettingsRegistryTest, GetPlatformDependent) {
#if defined(OS_IOS)
// Javascript shouldn't be registered on iOS.
EXPECT_FALSE(registry()->Get(CONTENT_SETTINGS_TYPE_JAVASCRIPT));
#endif
// Protected media identifier only get registered on android and chromeos.
#if defined(ANDROID) | defined(OS_CHROMEOS)
EXPECT_TRUE(
registry()->Get(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER));
#else
EXPECT_FALSE(
registry()->Get(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER));
#endif
// Cookies is registered on all platforms.
EXPECT_TRUE(registry()->Get(CONTENT_SETTINGS_TYPE_COOKIES));
}
TEST_F(ContentSettingsRegistryTest, Properties) {
// The cookies type should be registered.
const ContentSettingsInfo* info =
......@@ -62,8 +81,13 @@ TEST_F(ContentSettingsRegistryTest, Properties) {
ASSERT_TRUE(
website_settings_info->initial_default_value()->GetAsInteger(&setting));
EXPECT_EQ(CONTENT_SETTING_ALLOW, setting);
#if defined(OS_IOS)
EXPECT_EQ(PrefRegistry::NO_REGISTRATION_FLAGS,
website_settings_info->GetPrefRegistrationFlags());
#else
EXPECT_EQ(user_prefs::PrefRegistrySyncable::SYNCABLE_PREF,
website_settings_info->GetPrefRegistrationFlags());
#endif
// Check the WebsiteSettingsInfo is registered correctly.
EXPECT_EQ(website_settings_registry()->Get(CONTENT_SETTINGS_TYPE_COOKIES),
......@@ -86,7 +110,12 @@ TEST_F(ContentSettingsRegistryTest, Iteration) {
}
}
#if defined(OS_IOS)
EXPECT_FALSE(plugins_found);
#else
EXPECT_TRUE(plugins_found);
#endif
EXPECT_TRUE(cookies_found);
}
......
......@@ -459,32 +459,40 @@ void HostContentSettingsMap::SetContentSettingDefaultScope(
}
void HostContentSettingsMap::MigrateKeygenSettings() {
ContentSettingsForOneType settings;
GetSettingsForOneType(CONTENT_SETTINGS_TYPE_KEYGEN, std::string(), &settings);
const content_settings::ContentSettingsInfo* info =
content_settings::ContentSettingsRegistry::GetInstance()->Get(
CONTENT_SETTINGS_TYPE_KEYGEN);
if (info) {
ContentSettingsForOneType settings;
GetSettingsForOneType(CONTENT_SETTINGS_TYPE_KEYGEN, std::string(),
&settings);
for (const ContentSettingPatternSource& setting_entry : settings) {
// Migrate user preference settings only.
if (setting_entry.source != "preference")
continue;
// 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, CONTENT_SETTINGS_TYPE_KEYGEN, std::string());
for (const ContentSettingPatternSource& setting_entry : settings) {
// Migrate user preference settings only.
if (setting_entry.source != "preference")
continue;
// 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, CONTENT_SETTINGS_TYPE_KEYGEN, std::string());
}
// Remove the old pattern.
SetContentSettingCustomScope(setting_entry.primary_pattern,
setting_entry.secondary_pattern,
CONTENT_SETTINGS_TYPE_KEYGEN,
std::string(), CONTENT_SETTING_DEFAULT);
// Set the new pattern.
SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN,
std::string(), content_setting);
}
// Remove the old pattern.
SetContentSettingCustomScope(
setting_entry.primary_pattern, setting_entry.secondary_pattern,
CONTENT_SETTINGS_TYPE_KEYGEN, std::string(), CONTENT_SETTING_DEFAULT);
// Set the new pattern.
SetContentSettingDefaultScope(url, GURL(), CONTENT_SETTINGS_TYPE_KEYGEN,
std::string(), content_setting);
}
}
}
......
......@@ -59,7 +59,36 @@ const WebsiteSettingsInfo* WebsiteSettingsRegistry::Register(
WebsiteSettingsInfo::SyncStatus sync_status,
WebsiteSettingsInfo::LossyStatus lossy_status,
WebsiteSettingsInfo::ScopingType scoping_type,
Platforms platform,
WebsiteSettingsInfo::IncognitoBehavior incognito_behavior) {
#if defined(OS_WIN)
if (!(platform & PLATFORM_WINDOWS))
return nullptr;
#elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
if (!(platform & PLATFORM_LINUX))
return nullptr;
#elif defined(OS_MACOSX) && !defined(OS_IOS)
if (!(platform & PLATFORM_MAC))
return nullptr;
#elif defined(OS_CHROMEOS)
if (!(platform & PLATFORM_CHROMEOS))
return nullptr;
#elif defined(OS_ANDROID)
if (!(platform & PLATFORM_ANDROID))
return nullptr;
#elif defined(OS_IOS)
if (!(platform & PLATFORM_IOS))
return nullptr;
// Only default settings for Cookies and Popups are used in iOS. Exceptions
// and all the other content setting types are not used in iOS currently. So
// make content settings unsyncable on iOS for now.
// TODO(lshang): address this once we have proper content settings on iOS.
sync_status = WebsiteSettingsInfo::UNSYNCABLE;
#else
#error "Unsupported platform"
#endif
WebsiteSettingsInfo* info = new WebsiteSettingsInfo(
type, name, std::move(initial_default_value), sync_status, lossy_status,
scoping_type, incognito_behavior);
......@@ -87,24 +116,28 @@ void WebsiteSettingsRegistry::Init() {
Register(CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE,
"auto-select-certificate", nullptr, WebsiteSettingsInfo::UNSYNCABLE,
WebsiteSettingsInfo::NOT_LOSSY,
WebsiteSettingsInfo::REQUESTING_DOMAIN_ONLY_SCOPE,
WebsiteSettingsInfo::REQUESTING_DOMAIN_ONLY_SCOPE, ALL_PLATFORMS,
WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
Register(CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS, "ssl-cert-decisions",
nullptr, WebsiteSettingsInfo::UNSYNCABLE,
Register(CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS,
"ssl-cert-decisions", nullptr, WebsiteSettingsInfo::UNSYNCABLE,
WebsiteSettingsInfo::NOT_LOSSY,
WebsiteSettingsInfo::REQUESTING_ORIGIN_ONLY_SCOPE,
DESKTOP | PLATFORM_ANDROID,
WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
Register(CONTENT_SETTINGS_TYPE_APP_BANNER, "app-banner", nullptr,
WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::LOSSY,
WebsiteSettingsInfo::REQUESTING_DOMAIN_ONLY_SCOPE,
DESKTOP | PLATFORM_ANDROID,
WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
Register(CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, "site-engagement", nullptr,
WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::LOSSY,
WebsiteSettingsInfo::REQUESTING_ORIGIN_ONLY_SCOPE,
DESKTOP | PLATFORM_ANDROID,
WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
Register(CONTENT_SETTINGS_TYPE_USB_CHOOSER_DATA, "usb-chooser-data", nullptr,
WebsiteSettingsInfo::UNSYNCABLE, WebsiteSettingsInfo::NOT_LOSSY,
WebsiteSettingsInfo::REQUESTING_ORIGIN_AND_TOP_LEVEL_ORIGIN_SCOPE,
DESKTOP | PLATFORM_ANDROID,
WebsiteSettingsInfo::DONT_INHERIT_IN_INCOGNITO);
}
......
......@@ -25,6 +25,27 @@ namespace content_settings {
// const.
class WebsiteSettingsRegistry {
public:
typedef uint32_t Platforms;
// TODO(lshang): Remove this enum when content settings can be registered from
// within the component in which they are used. When this is possible then
// ifdefs can be contained within each component.
enum Platform : Platforms {
PLATFORM_WINDOWS = 1 << 0,
PLATFORM_LINUX = 1 << 1,
PLATFORM_CHROMEOS = 1 << 2,
PLATFORM_MAC = 1 << 3,
PLATFORM_ANDROID = 1 << 4,
PLATFORM_IOS = 1 << 5,
// Settings only applied to win, mac, linux and chromeos.
DESKTOP =
PLATFORM_WINDOWS | PLATFORM_LINUX | PLATFORM_CHROMEOS | PLATFORM_MAC,
// Settings applied to all platforms, including win, mac, linux, chromeos,
// android, ios.
ALL_PLATFORMS = DESKTOP | PLATFORM_ANDROID | PLATFORM_IOS,
};
using Map =
std::map<ContentSettingsType, std::unique_ptr<WebsiteSettingsInfo>>;
using const_iterator = MapValueIterator<typename Map::const_iterator,
......@@ -41,6 +62,8 @@ class WebsiteSettingsRegistry {
// Register a new website setting. This maps an origin to an arbitrary
// base::Value. Returns a pointer to the registered WebsiteSettingsInfo which
// is owned by the registry.
// A nullptr will be returned if registration fails (for example if
// |platforms| doesn't match the current platform).
const WebsiteSettingsInfo* Register(
ContentSettingsType type,
const std::string& name,
......@@ -48,6 +71,7 @@ class WebsiteSettingsRegistry {
WebsiteSettingsInfo::SyncStatus sync_status,
WebsiteSettingsInfo::LossyStatus lossy_status,
WebsiteSettingsInfo::ScopingType scoping_type,
Platforms platforms,
WebsiteSettingsInfo::IncognitoBehavior incognito_behavior);
const_iterator begin() const;
......
......@@ -25,30 +25,33 @@ class WebsiteSettingsRegistryTest : public testing::Test {
};
TEST_F(WebsiteSettingsRegistryTest, Get) {
// CONTENT_SETTINGS_TYPE_APP_BANNER should be registered.
// CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE should be registered.
const WebsiteSettingsInfo* info =
registry()->Get(CONTENT_SETTINGS_TYPE_APP_BANNER);
registry()->Get(CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE);
ASSERT_TRUE(info);
EXPECT_EQ(CONTENT_SETTINGS_TYPE_APP_BANNER, info->type());
EXPECT_EQ("app-banner", info->name());
EXPECT_EQ(CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, info->type());
EXPECT_EQ("auto-select-certificate", info->name());
}
TEST_F(WebsiteSettingsRegistryTest, GetByName) {
// Random string shouldn't be registered.
EXPECT_FALSE(registry()->GetByName("abc"));
// "app-banner" should be registered.
const WebsiteSettingsInfo* info = registry()->GetByName("app-banner");
// "auto-select-certificate" should be registered.
const WebsiteSettingsInfo* info =
registry()->GetByName("auto-select-certificate");
ASSERT_TRUE(info);
EXPECT_EQ(CONTENT_SETTINGS_TYPE_APP_BANNER, info->type());
EXPECT_EQ("app-banner", info->name());
EXPECT_EQ(registry()->Get(CONTENT_SETTINGS_TYPE_APP_BANNER), info);
EXPECT_EQ(CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE, info->type());
EXPECT_EQ("auto-select-certificate", info->name());
EXPECT_EQ(registry()->Get(CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE),
info);
// Register a new setting.
registry()->Register(static_cast<ContentSettingsType>(10), "test", nullptr,
WebsiteSettingsInfo::UNSYNCABLE,
WebsiteSettingsInfo::LOSSY,
WebsiteSettingsInfo::TOP_LEVEL_DOMAIN_ONLY_SCOPE,
WebsiteSettingsRegistry::ALL_PLATFORMS,
WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
info = registry()->GetByName("test");
ASSERT_TRUE(info);
......@@ -57,17 +60,31 @@ TEST_F(WebsiteSettingsRegistryTest, GetByName) {
EXPECT_EQ(registry()->Get(static_cast<ContentSettingsType>(10)), info);
}
TEST_F(WebsiteSettingsRegistryTest, GetPlatformDependent) {
#if defined(OS_IOS)
// App banner shouldn't be registered on iOS.
EXPECT_FALSE(registry()->Get(CONTENT_SETTINGS_TYPE_APP_BANNER));
#else
// App banner should be registered on other platforms.
EXPECT_TRUE(registry()->Get(CONTENT_SETTINGS_TYPE_APP_BANNER));
#endif
// Auto select certificate is registered on all platforms.
EXPECT_TRUE(registry()->Get(CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE));
}
TEST_F(WebsiteSettingsRegistryTest, Properties) {
// "app-banner" should be registered.
// "auto-select-certificate" should be registered.
const WebsiteSettingsInfo* info =
registry()->Get(CONTENT_SETTINGS_TYPE_APP_BANNER);
registry()->Get(CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE);
ASSERT_TRUE(info);
EXPECT_EQ("profile.content_settings.exceptions.app_banner",
EXPECT_EQ("profile.content_settings.exceptions.auto_select_certificate",
info->pref_name());
EXPECT_EQ("profile.default_content_setting_values.app_banner",
EXPECT_EQ("profile.default_content_setting_values.auto_select_certificate",
info->default_value_pref_name());
ASSERT_FALSE(info->initial_default_value());
EXPECT_EQ(PrefRegistry::LOSSY_PREF, info->GetPrefRegistrationFlags());
EXPECT_EQ(PrefRegistry::NO_REGISTRATION_FLAGS,
info->GetPrefRegistrationFlags());
// Register a new setting.
registry()->Register(static_cast<ContentSettingsType>(10), "test",
......@@ -75,6 +92,7 @@ TEST_F(WebsiteSettingsRegistryTest, Properties) {
WebsiteSettingsInfo::SYNCABLE,
WebsiteSettingsInfo::LOSSY,
WebsiteSettingsInfo::TOP_LEVEL_DOMAIN_ONLY_SCOPE,
WebsiteSettingsRegistry::ALL_PLATFORMS,
WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
info = registry()->Get(static_cast<ContentSettingsType>(10));
ASSERT_TRUE(info);
......@@ -84,9 +102,13 @@ TEST_F(WebsiteSettingsRegistryTest, Properties) {
int setting;
ASSERT_TRUE(info->initial_default_value()->GetAsInteger(&setting));
EXPECT_EQ(999, setting);
#if defined(OS_IOS)
EXPECT_EQ(PrefRegistry::LOSSY_PREF, info->GetPrefRegistrationFlags());
#else
EXPECT_EQ(PrefRegistry::LOSSY_PREF |
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF,
info->GetPrefRegistrationFlags());
#endif
EXPECT_EQ(WebsiteSettingsInfo::TOP_LEVEL_DOMAIN_ONLY_SCOPE,
info->scoping_type());
EXPECT_EQ(WebsiteSettingsInfo::INHERIT_IN_INCOGNITO,
......@@ -99,6 +121,7 @@ TEST_F(WebsiteSettingsRegistryTest, Iteration) {
WebsiteSettingsInfo::SYNCABLE,
WebsiteSettingsInfo::LOSSY,
WebsiteSettingsInfo::TOP_LEVEL_DOMAIN_ONLY_SCOPE,
WebsiteSettingsRegistry::ALL_PLATFORMS,
WebsiteSettingsInfo::INHERIT_IN_INCOGNITO);
bool found = false;
......
......@@ -37,9 +37,7 @@ enum ContentSettingsType {
CONTENT_SETTINGS_TYPE_MIDI_SYSEX,
CONTENT_SETTINGS_TYPE_PUSH_MESSAGING,
CONTENT_SETTINGS_TYPE_SSL_CERT_DECISIONS,
#if defined(OS_ANDROID) || defined(OS_CHROMEOS)
CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER,
#endif
CONTENT_SETTINGS_TYPE_APP_BANNER,
CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT,
CONTENT_SETTINGS_TYPE_DURABLE_STORAGE,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment