Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6638)

Unified Diff: chrome/browser/content_settings/host_content_settings_map.cc

Issue 7713034: HostContentSettingsMap refactoring. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/content_settings/host_content_settings_map.cc
diff --git a/chrome/browser/content_settings/host_content_settings_map.cc b/chrome/browser/content_settings/host_content_settings_map.cc
index 3d15073c40ece187724b2e385ef29fceae11819d..871b967f821f00a5eca15fe9b262c5e627ecfb2b 100644
--- a/chrome/browser/content_settings/host_content_settings_map.cc
+++ b/chrome/browser/content_settings/host_content_settings_map.cc
@@ -73,10 +73,7 @@ HostContentSettingsMap::HostContentSettingsMap(
ExtensionService* extension_service,
bool incognito)
: prefs_(prefs),
- is_off_the_record_(incognito),
- updating_preferences_(false),
- block_third_party_cookies_(false),
- is_block_third_party_cookies_managed_(false) {
+ is_off_the_record_(incognito) {
// The order in which the default content settings providers are created is
// critical, as providers that are further down in the list (i.e. added later)
// override providers further up.
@@ -92,22 +89,7 @@ HostContentSettingsMap::HostContentSettingsMap(
default_content_settings_providers_.push_back(
make_linked_ptr(policy_default_provider));
- // TODO(markusheintz): Discuss whether it is sensible to move migration code
- // to PrefContentSettingsProvider.
- MigrateObsoleteCookiePref();
-
// Read misc. global settings.
- block_third_party_cookies_ =
- prefs_->GetBoolean(prefs::kBlockThirdPartyCookies);
- if (block_third_party_cookies_) {
- UserMetrics::RecordAction(
- UserMetricsAction("ThirdPartyCookieBlockingEnabled"));
- } else {
- UserMetrics::RecordAction(
- UserMetricsAction("ThirdPartyCookieBlockingDisabled"));
- }
- is_block_third_party_cookies_managed_ =
- prefs_->IsManagedPreference(prefs::kBlockThirdPartyCookies);
// User defined non default content settings are provided by the PrefProvider.
// The order in which the content settings providers are created is critical,
@@ -130,8 +112,6 @@ HostContentSettingsMap::HostContentSettingsMap(
provider->AddObserver(this);
content_settings_providers_.push_back(make_linked_ptr(provider));
- pref_change_registrar_.Init(prefs_);
- pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this);
}
// static
@@ -180,42 +160,6 @@ ContentSettings HostContentSettingsMap::GetDefaultContentSettings() const {
return output;
}
-ContentSetting HostContentSettingsMap::GetCookieContentSetting(
- const GURL& url,
- const GURL& first_party_url,
- bool setting_cookie) const {
- if (ShouldAllowAllContent(first_party_url))
- return CONTENT_SETTING_ALLOW;
-
- // First get any host-specific settings.
- ContentSetting setting = GetNonDefaultContentSetting(url,
- first_party_url, CONTENT_SETTINGS_TYPE_COOKIES, "");
-
- // If no explicit exception has been made and third-party cookies are blocked
- // by default, apply that rule.
- if (setting == CONTENT_SETTING_DEFAULT && BlockThirdPartyCookies()) {
- bool strict = CommandLine::ForCurrentProcess()->HasSwitch(
- switches::kBlockReadingThirdPartyCookies);
- net::StaticCookiePolicy policy(strict ?
- net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
- net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
- int rv;
- if (setting_cookie)
- rv = policy.CanSetCookie(url, first_party_url);
- else
- rv = policy.CanGetCookies(url, first_party_url);
- DCHECK_NE(net::ERR_IO_PENDING, rv);
- if (rv != net::OK)
- setting = CONTENT_SETTING_BLOCK;
- }
-
- // If no other policy has changed the setting, use the default.
- if (setting == CONTENT_SETTING_DEFAULT)
- setting = GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES);
-
- return setting;
-}
-
ContentSetting HostContentSettingsMap::GetContentSetting(
const GURL& primary_url,
const GURL& secondary_url,
@@ -435,7 +379,142 @@ bool HostContentSettingsMap::IsSettingAllowedForType(
}
}
-void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) {
+void HostContentSettingsMap::OnContentSettingChanged(
+ ContentSettingsPattern primary_pattern,
+ ContentSettingsPattern secondary_pattern,
+ ContentSettingsType content_type,
+ std::string resource_identifier) {
+ const ContentSettingsDetails details(primary_pattern,
+ secondary_pattern,
+ content_type,
+ resource_identifier);
+ NotificationService::current()->Notify(
+ chrome::NOTIFICATION_CONTENT_SETTINGS_CHANGED,
+ Source<HostContentSettingsMap>(this),
+ Details<const ContentSettingsDetails>(&details));
+}
+
+HostContentSettingsMap::~HostContentSettingsMap() {
+ DCHECK(!prefs_);
+}
+
+bool HostContentSettingsMap::IsDefaultContentSettingManaged(
+ ContentSettingsType content_type) const {
+ for (ConstDefaultProviderIterator provider =
+ default_content_settings_providers_.begin();
+ provider != default_content_settings_providers_.end(); ++provider) {
+ if ((*provider)->DefaultSettingIsManaged(content_type))
+ return true;
+ }
+ return false;
+}
+
+void HostContentSettingsMap::ShutdownOnUIThread() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ DCHECK(prefs_);
+ prefs_ = NULL;
+ for (ProviderIterator it = content_settings_providers_.begin();
+ it != content_settings_providers_.end();
+ ++it) {
+ (*it)->ShutdownOnUIThread();
+ }
+ for (DefaultProviderIterator it = default_content_settings_providers_.begin();
+ it != default_content_settings_providers_.end();
+ ++it) {
+ (*it)->ShutdownOnUIThread();
+ }
+}
+
+CookieContentSettings::CookieContentSettings(
markusheintz_ 2011/08/24 12:29:40 Please move the CookieContentSettings to a separat
marja 2011/08/25 13:56:11 Done.
+ HostContentSettingsMap* host_content_settings_map,
+ PrefService* prefs,
+ bool incognito)
+ : host_content_settings_map_(host_content_settings_map),
+ prefs_(prefs),
+ is_off_the_record_(incognito),
+ block_third_party_cookies_(false),
+ is_block_third_party_cookies_managed_(false) {
+ block_third_party_cookies_ =
+ prefs_->GetBoolean(prefs::kBlockThirdPartyCookies);
+ if (block_third_party_cookies_) {
+ UserMetrics::RecordAction(
+ UserMetricsAction("ThirdPartyCookieBlockingEnabled"));
+ } else {
+ UserMetrics::RecordAction(
+ UserMetricsAction("ThirdPartyCookieBlockingDisabled"));
+ }
+ is_block_third_party_cookies_managed_ =
+ prefs_->IsManagedPreference(prefs::kBlockThirdPartyCookies);
+
+ // TODO(markusheintz): Discuss whether it is sensible to move migration code
+ // to PrefContentSettingsProvider.
+ MigrateObsoleteCookiePref();
+
+ pref_change_registrar_.Init(prefs_);
+ pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this);
+}
+
+
+ContentSetting CookieContentSettings::GetCookieContentSetting(
+ const GURL& url,
+ const GURL& first_party_url,
+ bool setting_cookie) const {
+ if (ShouldAllowAllContent(first_party_url))
+ return CONTENT_SETTING_ALLOW;
+
+ // First get any host-specific settings.
+ ContentSetting setting =
+ host_content_settings_map_->GetNonDefaultContentSetting(
+ url, first_party_url, CONTENT_SETTINGS_TYPE_COOKIES, "");
+
+ // If no explicit exception has been made and third-party cookies are blocked
+ // by default, apply that rule.
+ if (setting == CONTENT_SETTING_DEFAULT && BlockThirdPartyCookies()) {
+ bool strict = CommandLine::ForCurrentProcess()->HasSwitch(
+ switches::kBlockReadingThirdPartyCookies);
+ net::StaticCookiePolicy policy(strict ?
+ net::StaticCookiePolicy::BLOCK_ALL_THIRD_PARTY_COOKIES :
+ net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES);
+ int rv;
+ if (setting_cookie)
+ rv = policy.CanSetCookie(url, first_party_url);
+ else
+ rv = policy.CanGetCookies(url, first_party_url);
+ DCHECK_NE(net::ERR_IO_PENDING, rv);
+ if (rv != net::OK)
+ setting = CONTENT_SETTING_BLOCK;
+ }
+
+ // If no other policy has changed the setting, use the default.
+ if (setting == CONTENT_SETTING_DEFAULT) {
+ setting = host_content_settings_map_->GetDefaultContentSetting(
+ CONTENT_SETTINGS_TYPE_COOKIES);
+ }
+ return setting;
+}
+
+bool CookieContentSettings::Allow(const GURL& url,
+ const GURL& first_party_url,
+ bool setting_cookie) const {
+ ContentSetting setting = GetCookieContentSetting(
+ url, first_party_url, setting_cookie);
+ DCHECK((setting == CONTENT_SETTING_ALLOW) ||
+ (setting == CONTENT_SETTING_BLOCK) ||
+ (setting == CONTENT_SETTING_SESSION_ONLY));
+ return (setting == CONTENT_SETTING_ALLOW ||
+ setting == CONTENT_SETTING_SESSION_ONLY);
+}
+
+bool CookieContentSettings::EnforceSessionOnly(const GURL& origin) const {
+ // FIXME(marja): Add another content type here
+ ContentSetting setting = GetCookieContentSetting(origin, origin, false);
+ DCHECK((setting == CONTENT_SETTING_ALLOW) ||
+ (setting == CONTENT_SETTING_BLOCK) ||
+ (setting == CONTENT_SETTING_SESSION_ONLY));
+ return (setting == CONTENT_SETTING_SESSION_ONLY);
+}
+
+void CookieContentSettings::SetBlockThirdPartyCookies(bool block) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(prefs_);
@@ -461,30 +540,13 @@ void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) {
prefs_->SetBoolean(prefs::kBlockThirdPartyCookies, block);
}
-void HostContentSettingsMap::OnContentSettingChanged(
- ContentSettingsPattern primary_pattern,
- ContentSettingsPattern secondary_pattern,
- ContentSettingsType content_type,
- std::string resource_identifier) {
- const ContentSettingsDetails details(primary_pattern,
- secondary_pattern,
- content_type,
- resource_identifier);
- NotificationService::current()->Notify(
- chrome::NOTIFICATION_CONTENT_SETTINGS_CHANGED,
- Source<HostContentSettingsMap>(this),
- Details<const ContentSettingsDetails>(&details));
-}
-
-void HostContentSettingsMap::Observe(int type,
- const NotificationSource& source,
- const NotificationDetails& details) {
+void CookieContentSettings::Observe(int type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (type == chrome::NOTIFICATION_PREF_CHANGED) {
DCHECK_EQ(prefs_, Source<PrefService>(source).ptr());
- if (updating_preferences_)
- return;
std::string* name = Details<std::string>(details).ptr();
if (*name == prefs::kBlockThirdPartyCookies) {
@@ -503,44 +565,17 @@ void HostContentSettingsMap::Observe(int type,
}
}
-HostContentSettingsMap::~HostContentSettingsMap() {
- DCHECK(!prefs_);
-}
-
-bool HostContentSettingsMap::IsDefaultContentSettingManaged(
- ContentSettingsType content_type) const {
- for (ConstDefaultProviderIterator provider =
- default_content_settings_providers_.begin();
- provider != default_content_settings_providers_.end(); ++provider) {
- if ((*provider)->DefaultSettingIsManaged(content_type))
- return true;
- }
- return false;
-}
-
-void HostContentSettingsMap::ShutdownOnUIThread() {
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- DCHECK(prefs_);
+void CookieContentSettings::ShutdownOnUIThread() {
pref_change_registrar_.RemoveAll();
- prefs_ = NULL;
- for (ProviderIterator it = content_settings_providers_.begin();
- it != content_settings_providers_.end();
- ++it) {
- (*it)->ShutdownOnUIThread();
- }
- for (DefaultProviderIterator it = default_content_settings_providers_.begin();
- it != default_content_settings_providers_.end();
- ++it) {
- (*it)->ShutdownOnUIThread();
- }
}
-void HostContentSettingsMap::MigrateObsoleteCookiePref() {
+void CookieContentSettings::MigrateObsoleteCookiePref() {
if (prefs_->HasPrefPath(prefs::kCookieBehavior)) {
int cookie_behavior = prefs_->GetInteger(prefs::kCookieBehavior);
prefs_->ClearPref(prefs::kCookieBehavior);
if (!prefs_->HasPrefPath(prefs::kDefaultContentSettings)) {
- SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES,
+ host_content_settings_map_->SetDefaultContentSetting(
+ CONTENT_SETTINGS_TYPE_COOKIES,
(cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ?
CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW);
}

Powered by Google App Engine
This is Rietveld 408576698