diff options
36 files changed, 842 insertions, 574 deletions
diff --git a/chrome/browser/chrome_content_browser_client.cc b/chrome/browser/chrome_content_browser_client.cc index cd84fd2..9be88fc 100644 --- a/chrome/browser/chrome_content_browser_client.cc +++ b/chrome/browser/chrome_content_browser_client.cc @@ -14,7 +14,7 @@ #include "chrome/browser/chrome_plugin_message_filter.h" #include "chrome/browser/chrome_quota_permission_context.h" #include "chrome/browser/chrome_worker_message_filter.h" -#include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/tab_specific_content_settings.h" #include "chrome/browser/download/download_util.h" #include "chrome/browser/extensions/extension_info_map.h" @@ -584,10 +584,8 @@ bool ChromeContentBrowserClient::AllowAppCache( DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); ProfileIOData* io_data = reinterpret_cast<ProfileIOData*>(context.GetUserData(NULL)); - ContentSetting setting = io_data->GetHostContentSettingsMap()-> - GetCookieContentSetting(manifest_url, first_party, true); - DCHECK(setting != CONTENT_SETTING_DEFAULT); - return setting != CONTENT_SETTING_BLOCK; + return io_data->GetCookieSettings()-> + IsSettingCookieAllowed(manifest_url, first_party); } bool ChromeContentBrowserClient::AllowGetCookie( @@ -600,10 +598,8 @@ bool ChromeContentBrowserClient::AllowGetCookie( DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); ProfileIOData* io_data = reinterpret_cast<ProfileIOData*>(context.GetUserData(NULL)); - ContentSetting setting = io_data->GetHostContentSettingsMap()-> - GetCookieContentSetting(url, first_party, false); - bool allow = setting == CONTENT_SETTING_ALLOW || - setting == CONTENT_SETTING_SESSION_ONLY; + bool allow = io_data->GetCookieSettings()-> + IsReadingCookieAllowed(url, first_party); BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, @@ -624,14 +620,12 @@ bool ChromeContentBrowserClient::AllowSetCookie( DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); ProfileIOData* io_data = reinterpret_cast<ProfileIOData*>(context.GetUserData(NULL)); - ContentSetting setting = io_data->GetHostContentSettingsMap()-> - GetCookieContentSetting(url, first_party, true); - if (setting == CONTENT_SETTING_SESSION_ONLY) - options->set_force_session(); + CookieSettings* cookie_settings = io_data->GetCookieSettings(); + bool allow = cookie_settings->IsSettingCookieAllowed(url, first_party); - bool allow = setting == CONTENT_SETTING_ALLOW || - setting == CONTENT_SETTING_SESSION_ONLY; + if (cookie_settings->IsCookieSessionOnly(url)) + options->set_force_session(); BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, diff --git a/chrome/browser/chrome_worker_message_filter.cc b/chrome/browser/chrome_worker_message_filter.cc index 786e032..57535a2 100644 --- a/chrome/browser/chrome_worker_message_filter.cc +++ b/chrome/browser/chrome_worker_message_filter.cc @@ -5,7 +5,7 @@ #include "chrome/browser/chrome_worker_message_filter.h" #include "base/bind.h" -#include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/tab_specific_content_settings.h" #include "chrome/browser/profiles/profile_io_data.h" #include "content/browser/resource_context.h" @@ -16,7 +16,7 @@ ChromeWorkerMessageFilter::ChromeWorkerMessageFilter(WorkerProcessHost* process) : process_(process) { ProfileIOData* io_data = reinterpret_cast<ProfileIOData*>( process->resource_context()->GetUserData(NULL)); - host_content_settings_map_ = io_data->GetHostContentSettingsMap(); + cookie_settings_ = io_data->GetCookieSettings(); } ChromeWorkerMessageFilter::~ChromeWorkerMessageFilter() { @@ -43,10 +43,7 @@ void ChromeWorkerMessageFilter::OnAllowDatabase(int worker_route_id, const string16& display_name, unsigned long estimated_size, bool* result) { - ContentSetting content_setting = - host_content_settings_map_->GetCookieContentSetting(url, url, true); - - *result = content_setting != CONTENT_SETTING_BLOCK; + *result = cookie_settings_->IsSettingCookieAllowed(url, url); // Record access to database for potential display in UI: Find the worker // instance and forward the message to all attached documents. @@ -73,10 +70,7 @@ void ChromeWorkerMessageFilter::OnAllowDatabase(int worker_route_id, void ChromeWorkerMessageFilter::OnAllowFileSystem(int worker_route_id, const GURL& url, bool* result) { - ContentSetting content_setting = - host_content_settings_map_->GetCookieContentSetting(url, url, true); - - *result = content_setting != CONTENT_SETTING_BLOCK; + *result = cookie_settings_->IsSettingCookieAllowed(url, url); // Record access to file system for potential display in UI: Find the worker // instance and forward the message to all attached documents. diff --git a/chrome/browser/chrome_worker_message_filter.h b/chrome/browser/chrome_worker_message_filter.h index 6b3f4aa..e5a0bd62 100644 --- a/chrome/browser/chrome_worker_message_filter.h +++ b/chrome/browser/chrome_worker_message_filter.h @@ -9,8 +9,8 @@ #include "base/string16.h" #include "ipc/ipc_channel_proxy.h" +class CookieSettings; class GURL; -class HostContentSettingsMap; class WorkerProcessHost; // This class filters out incoming Chrome-specific IPC messages for the renderer @@ -40,7 +40,7 @@ class ChromeWorkerMessageFilter : public IPC::ChannelProxy::MessageFilter, bool* result); WorkerProcessHost* process_; - scoped_refptr<HostContentSettingsMap> host_content_settings_map_; + scoped_refptr<CookieSettings> cookie_settings_; DISALLOW_COPY_AND_ASSIGN(ChromeWorkerMessageFilter); }; diff --git a/chrome/browser/content_settings/content_settings_browsertest.cc b/chrome/browser/content_settings/content_settings_browsertest.cc index d992717..cbe2b94 100644 --- a/chrome/browser/content_settings/content_settings_browsertest.cc +++ b/chrome/browser/content_settings/content_settings_browsertest.cc @@ -4,6 +4,7 @@ #include "base/stringprintf.h" #include "base/utf_string_conversions.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/content_settings/tab_specific_content_settings.h" #include "chrome/browser/profiles/profile.h" @@ -19,8 +20,8 @@ IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, RedirectLoopCookies) { GURL test_url = test_server()->GetURL("files/redirect-loop.html"); - browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); + CookieSettings::GetForProfile(browser()->profile())-> + SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); ui_test_utils::NavigateToURL(browser(), test_url); @@ -60,8 +61,8 @@ IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, RedirectCrossOrigin) { host_port.port())); GURL test_url = test_server()->GetURL("server-redirect?" + redirect); - browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); + CookieSettings::GetForProfile(browser()->profile())-> + SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); ui_test_utils::NavigateToURL(browser(), test_url); diff --git a/chrome/browser/content_settings/cookie_settings.cc b/chrome/browser/content_settings/cookie_settings.cc new file mode 100644 index 0000000..141ea67 --- /dev/null +++ b/chrome/browser/content_settings/cookie_settings.cc @@ -0,0 +1,250 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/content_settings/cookie_settings.h" + +#include "base/command_line.h" +#include "chrome/browser/content_settings/content_settings_utils.h" +#include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/prefs/pref_service.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_dependency_manager.h" +#include "chrome/browser/profiles/profile_keyed_service.h" +#include "chrome/common/chrome_notification_types.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/common/content_settings_pattern.h" +#include "chrome/common/pref_names.h" +#include "content/browser/browser_thread.h" +#include "content/browser/user_metrics.h" +#include "content/public/browser/notification_service.h" +#include "content/public/browser/notification_source.h" +#include "googleurl/src/gurl.h" +#include "net/base/net_errors.h" +#include "net/base/static_cookie_policy.h" + +namespace { + +bool IsValidSetting(ContentSetting setting) { + return (setting == CONTENT_SETTING_ALLOW || + setting == CONTENT_SETTING_SESSION_ONLY || + setting == CONTENT_SETTING_BLOCK); +} + +bool IsAllowed(ContentSetting setting) { + DCHECK(IsValidSetting(setting)); + return (setting == CONTENT_SETTING_ALLOW || + setting == CONTENT_SETTING_SESSION_ONLY); +} + +} // namespace + +// |ProfileKeyedFactory| is the owner of the |ProfileKeyedService|s. This +// wrapper class allows others to hold shared pointers to CookieSettings. +class CookieSettingsWrapper : public ProfileKeyedService { + public: + explicit CookieSettingsWrapper(scoped_refptr<CookieSettings> cookie_settings) + : cookie_settings_(cookie_settings) {} + virtual ~CookieSettingsWrapper() {} + + CookieSettings* cookie_settings() { return cookie_settings_.get(); } + + private: + // ProfileKeyedService methods: + virtual void Shutdown() OVERRIDE { + cookie_settings_->ShutdownOnUIThread(); + } + + scoped_refptr<CookieSettings> cookie_settings_; +}; + +// static +CookieSettings::Factory* CookieSettings::Factory::GetInstance() { + return Singleton<CookieSettings::Factory>::get(); +} + +CookieSettingsWrapper* CookieSettings::Factory::GetWrapperForProfile( + Profile* profile) { + return static_cast<CookieSettingsWrapper*>( + GetServiceForProfile(profile,true)); +} + +CookieSettings::Factory::Factory() + : ProfileKeyedServiceFactory(ProfileDependencyManager::GetInstance()) { +} + +ProfileKeyedService* CookieSettings::Factory::BuildServiceInstanceFor( + Profile* profile) const { + scoped_refptr<CookieSettings> cookie_settings = new CookieSettings( + profile->GetHostContentSettingsMap(), + profile->GetPrefs()); + return new CookieSettingsWrapper(cookie_settings); +} + +CookieSettings::CookieSettings( + HostContentSettingsMap* host_content_settings_map, + PrefService* prefs) + : host_content_settings_map_(host_content_settings_map), + block_third_party_cookies_( + prefs->GetBoolean(prefs::kBlockThirdPartyCookies)) { + if (block_third_party_cookies_) { + UserMetrics::RecordAction( + UserMetricsAction("ThirdPartyCookieBlockingEnabled")); + } else { + UserMetrics::RecordAction( + UserMetricsAction("ThirdPartyCookieBlockingDisabled")); + } + + pref_change_registrar_.Init(prefs); + pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); +} + +CookieSettings::~CookieSettings() { +} + +// static +CookieSettings* CookieSettings::GetForProfile(Profile* profile) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + CookieSettings* cookie_settings = + Factory::GetInstance()->GetWrapperForProfile(profile)->cookie_settings(); + DCHECK(cookie_settings); + return cookie_settings; +} + +ContentSetting +CookieSettings::GetDefaultCookieSetting(std::string* provider_id) const { + return host_content_settings_map_->GetDefaultContentSetting( + CONTENT_SETTINGS_TYPE_COOKIES, provider_id); +} + +bool CookieSettings::IsReadingCookieAllowed(const GURL& url, + const GURL& first_party_url) const { + ContentSetting setting = GetCookieSetting(url, first_party_url, false); + return IsAllowed(setting); +} + +bool CookieSettings::IsSettingCookieAllowed(const GURL& url, + const GURL& first_party_url) const { + ContentSetting setting = GetCookieSetting(url, first_party_url, true); + return IsAllowed(setting); +} + +bool CookieSettings::IsCookieSessionOnly(const GURL& origin) const { + ContentSetting setting = GetCookieSetting(origin, origin, true); + DCHECK(IsValidSetting(setting)); + return (setting == CONTENT_SETTING_SESSION_ONLY); +} + +void CookieSettings::GetCookieSettings( + ContentSettingsForOneType* settings) const { + return host_content_settings_map_->GetSettingsForOneType( + CONTENT_SETTINGS_TYPE_COOKIES, "", settings); +} + +void CookieSettings::SetDefaultCookieSetting(ContentSetting setting) { + DCHECK(IsValidSetting(setting)); + host_content_settings_map_->SetDefaultContentSetting( + CONTENT_SETTINGS_TYPE_COOKIES, setting); +} + +void CookieSettings::SetCookieSetting( + const ContentSettingsPattern& primary_pattern, + const ContentSettingsPattern& secondary_pattern, + ContentSetting setting) { + DCHECK(IsValidSetting(setting)); + if (setting == CONTENT_SETTING_SESSION_ONLY) { + DCHECK(secondary_pattern == ContentSettingsPattern::Wildcard()); + } + host_content_settings_map_->SetContentSetting( + primary_pattern, secondary_pattern, CONTENT_SETTINGS_TYPE_COOKIES, "", + setting); +} + +void CookieSettings::ResetCookieSetting( + const ContentSettingsPattern& primary_pattern, + const ContentSettingsPattern& secondary_pattern) { + host_content_settings_map_->SetContentSetting( + primary_pattern, secondary_pattern, CONTENT_SETTINGS_TYPE_COOKIES, "", + CONTENT_SETTING_DEFAULT); +} + +void CookieSettings::Observe(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + + if (type == chrome::NOTIFICATION_PREF_CHANGED) { + PrefService* prefs = content::Source<PrefService>(source).ptr(); + std::string* name = content::Details<std::string>(details).ptr(); + if (*name == prefs::kBlockThirdPartyCookies) { + base::AutoLock auto_lock(lock_); + block_third_party_cookies_ = prefs->GetBoolean( + prefs::kBlockThirdPartyCookies); + } else { + NOTREACHED() << "Unexpected preference observed"; + return; + } + } else { + NOTREACHED() << "Unexpected notification"; + } +} + +void CookieSettings::ShutdownOnUIThread() { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + pref_change_registrar_.RemoveAll(); +} + +ContentSetting CookieSettings::GetCookieSetting( + const GURL& url, + const GURL& first_party_url, + bool setting_cookie) const { + if (HostContentSettingsMap::ShouldAllowAllContent( + first_party_url, + CONTENT_SETTINGS_TYPE_COOKIES)) { + return CONTENT_SETTING_ALLOW; + } + + ContentSettingsPattern primary_pattern; + ContentSettingsPattern secondary_pattern; + // First get any host-specific settings. + scoped_ptr<base::Value> value( + host_content_settings_map_->GetContentSettingValue( + url, first_party_url, CONTENT_SETTINGS_TYPE_COOKIES, "", + &primary_pattern, &secondary_pattern)); + + // If no explicit exception has been made and third-party cookies are blocked + // by default, apply that rule. + if (primary_pattern == ContentSettingsPattern::Wildcard() && + secondary_pattern == ContentSettingsPattern::Wildcard() && + ShouldBlockThirdPartyCookies()) { + 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) + return CONTENT_SETTING_BLOCK; + } + + // We should always have a value, at least from the default provider. + DCHECK(value.get()); + return content_settings::ValueToContentSetting(value.get()); +} + +// static +void CookieSettings::RegisterUserPrefs(PrefService* prefs) { + prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, + false, + PrefService::SYNCABLE_PREF); +} + +bool CookieSettings::ShouldBlockThirdPartyCookies() const { + base::AutoLock auto_lock(lock_); + return block_third_party_cookies_; +} diff --git a/chrome/browser/content_settings/cookie_settings.h b/chrome/browser/content_settings/cookie_settings.h new file mode 100644 index 0000000..1ad491c --- /dev/null +++ b/chrome/browser/content_settings/cookie_settings.h @@ -0,0 +1,151 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_CONTENT_SETTINGS_COOKIE_SETTINGS_H_ +#define CHROME_BROWSER_CONTENT_SETTINGS_COOKIE_SETTINGS_H_ +#pragma once + +#include <string> + +#include "base/compiler_specific.h" +#include "base/memory/ref_counted.h" +#include "base/memory/singleton.h" +#include "base/synchronization/lock.h" +#include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/prefs/pref_change_registrar.h" +#include "chrome/browser/profiles/profile_keyed_service_factory.h" +#include "chrome/common/content_settings.h" +#include "content/public/browser/notification_observer.h" + +class ContentSettingsPattern; +class CookieSettingsWrapper; +class GURL; +class PrefService; +class Profile; + +// A frontend to the cookie settings of |HostContentSettingsMap|. Handles +// cookie-specific logic such as blocking third-party cookies. Written on the UI +// thread and read on any thread. One instance per profile. + +class CookieSettings + : public content::NotificationObserver, + public base::RefCountedThreadSafe<CookieSettings> { + public: + CookieSettings( + HostContentSettingsMap* host_content_settings_map, + PrefService* prefs); + + virtual ~CookieSettings(); + + // Returns the |CookieSettings| associated with the |profile|. + // + // This should only be called on the UI thread. + static CookieSettings* GetForProfile(Profile* profile); + + // Returns the default content setting (CONTENT_SETTING_ALLOW, + // CONTENT_SETTING_BLOCK, or CONTENT_SETTING_SESSION_ONLY) for cookies. If + // |provider_id| is not NULL, the id of the provider which provided the + // default setting is assigned to it. + // + // This may be called on any thread. + ContentSetting GetDefaultCookieSetting(std::string* provider_id) const; + + // Returns true if the page identified by (|url|, |first_party_url|) is + // allowed to read cookies. + // + // This may be called on any thread. + bool IsReadingCookieAllowed(const GURL& url, + const GURL& first_party_url) const; + + // Returns true if the page identified by (|url|, |first_party_url|) is + // allowed to set cookies (permanent or session only). + // + // This may be called on any thread. + bool IsSettingCookieAllowed(const GURL& url, + const GURL& first_party_url) const; + + // Returns true if the cookie set by a page identified by |url| should be + // session only. Querying this only makes sense if |IsSettingCookieAllowed| + // has returned true. + // + // This may be called on any thread. + bool IsCookieSessionOnly(const GURL& url) const; + + // Returns all patterns with a non-default cookie setting, mapped to their + // actual settings, in the precedence order of the setting rules. |settings| + // must be a non-NULL outparam. + // + // This may be called on any thread. + void GetCookieSettings(ContentSettingsForOneType* settings) const; + + // Sets the default content setting (CONTENT_SETTING_ALLOW, + // CONTENT_SETTING_BLOCK, or CONTENT_SETTING_SESSION_ONLY) for cookies. + // + // This should only be called on the UI thread. + void SetDefaultCookieSetting(ContentSetting setting); + + // Sets the cookie setting for the given patterns. + // + // This should only be called on the UI thread. + void SetCookieSetting(const ContentSettingsPattern& primary_pattern, + const ContentSettingsPattern& secondary_pattern, + ContentSetting setting); + + // Resets the cookie setting for the given patterns. + // + // This should only be called on the UI thread. + void ResetCookieSetting(const ContentSettingsPattern& primary_pattern, + const ContentSettingsPattern& secondary_pattern); + + // |NotificationObserver| implementation. + virtual void Observe(int type, + const content::NotificationSource& source, + const content::NotificationDetails& details) OVERRIDE; + + // Detaches the |CookieSettings| from all |Profile|-related objects like + // |PrefService|. This methods needs to be called before destroying the + // |Profile|. Afterwards, only const methods can be called. + void ShutdownOnUIThread(); + + // A helper for applying third party cookie blocking rules. + ContentSetting GetCookieSetting(const GURL& url, + const GURL& first_party_url, + bool setting_cookie) const; + + static void RegisterUserPrefs(PrefService* prefs); + + class Factory : public ProfileKeyedServiceFactory { + public: + static Factory* GetInstance(); + CookieSettingsWrapper* GetWrapperForProfile(Profile* profile); + + private: + friend struct DefaultSingletonTraits<Factory>; + + Factory(); + virtual ~Factory() {} + + // |ProfileKeyedServiceFactory| methods: + virtual ProfileKeyedService* BuildServiceInstanceFor( + Profile* profile) const OVERRIDE; + virtual bool ServiceRedirectedInIncognito() OVERRIDE { return true; } + }; + + private: + // Returns true if the "block third party cookies" preference is set. + // + // This method may be called on any thread. + bool ShouldBlockThirdPartyCookies() const; + + scoped_refptr<HostContentSettingsMap> host_content_settings_map_; + PrefChangeRegistrar pref_change_registrar_; + + // Used around accesses to |block_third_party_cookies_| to guarantee thread + // safety. + mutable base::Lock lock_; + + bool block_third_party_cookies_; +}; + +#endif // CHROME_BROWSER_CONTENT_SETTINGS_COOKIE_SETTINGS_H_ diff --git a/chrome/browser/content_settings/cookie_settings_unittest.cc b/chrome/browser/content_settings/cookie_settings_unittest.cc new file mode 100644 index 0000000..e5ccacf --- /dev/null +++ b/chrome/browser/content_settings/cookie_settings_unittest.cc @@ -0,0 +1,233 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/auto_reset.h" +#include "base/command_line.h" +#include "chrome/browser/content_settings/cookie_settings.h" +#include "chrome/browser/prefs/pref_service.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/common/content_settings_pattern.h" +#include "chrome/common/pref_names.h" +#include "chrome/test/base/testing_profile.h" +#include "googleurl/src/gurl.h" +#include "net/base/static_cookie_policy.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class CookieSettingsTest : public testing::Test { + public: + CookieSettingsTest() : + ui_thread_(BrowserThread::UI, &message_loop_), + kBlockedSite("http://ads.thirdparty.com"), + kAllowedSite("http://good.allays.com"), + kFirstPartySite("http://cool.things.com"), + kBlockedFirstPartySite("http://no.thirdparties.com"), + kExtensionURL("chrome-extension://deadbeef") {} + + protected: + MessageLoop message_loop_; + BrowserThread ui_thread_; + const GURL kBlockedSite; + const GURL kAllowedSite; + const GURL kFirstPartySite; + const GURL kBlockedFirstPartySite; + const GURL kExtensionURL; +}; + +TEST_F(CookieSettingsTest, CookiesBlockSingle) { + TestingProfile profile; + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); + cookie_settings->SetCookieSetting( + ContentSettingsPattern::FromURL(kBlockedSite), + ContentSettingsPattern::Wildcard(), + CONTENT_SETTING_BLOCK); + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( + kBlockedSite, kBlockedSite)); +} + +TEST_F(CookieSettingsTest, CookiesBlockThirdParty) { + TestingProfile profile; + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); + profile.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( + kBlockedSite, kFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( + kBlockedSite, kFirstPartySite)); + + CommandLine* cmd = CommandLine::ForCurrentProcess(); + AutoReset<CommandLine> auto_reset(cmd, *cmd); + cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies); + + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( + kBlockedSite, kFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( + kBlockedSite, kFirstPartySite)); +} + +TEST_F(CookieSettingsTest, CookiesAllowThirdParty) { + TestingProfile profile; + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( + kBlockedSite, kFirstPartySite)); + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( + kBlockedSite, kFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); +} + +TEST_F(CookieSettingsTest, CookiesExplicitBlockSingleThirdParty) { + TestingProfile profile; + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); + cookie_settings->SetCookieSetting( + ContentSettingsPattern::FromURL(kBlockedSite), + ContentSettingsPattern::Wildcard(), + CONTENT_SETTING_BLOCK); + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( + kBlockedSite, kFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( + kBlockedSite, kFirstPartySite)); + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( + kAllowedSite, kFirstPartySite)); +} + +TEST_F(CookieSettingsTest, CookiesExplicitSessionOnly) { + TestingProfile profile; + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); + cookie_settings->SetCookieSetting( + ContentSettingsPattern::FromURL(kBlockedSite), + ContentSettingsPattern::Wildcard(), + CONTENT_SETTING_SESSION_ONLY); + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( + kBlockedSite, kFirstPartySite)); + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( + kBlockedSite, kFirstPartySite)); + EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); + + profile.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); + EXPECT_TRUE(cookie_settings-> + IsReadingCookieAllowed(kBlockedSite, kFirstPartySite)); + EXPECT_TRUE(cookie_settings-> + IsSettingCookieAllowed(kBlockedSite, kFirstPartySite)); + EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(kBlockedSite)); +} + +TEST_F(CookieSettingsTest, CookiesThirdPartyBlockedExplicitAllow) { + TestingProfile profile; + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); + cookie_settings->SetCookieSetting( + ContentSettingsPattern::FromURL(kAllowedSite), + ContentSettingsPattern::Wildcard(), + CONTENT_SETTING_ALLOW); + profile.GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, true); + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( + kAllowedSite, kFirstPartySite)); + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( + kAllowedSite, kFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); + + // Extensions should always be allowed to use cookies. + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( + kAllowedSite, kExtensionURL)); + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( + kAllowedSite, kExtensionURL)); + + CommandLine* cmd = CommandLine::ForCurrentProcess(); + AutoReset<CommandLine> auto_reset(cmd, *cmd); + cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies); + + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( + kAllowedSite, kFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); + + // Extensions should always be allowed to use cookies. + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( + kAllowedSite, kExtensionURL)); + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( + kAllowedSite, kExtensionURL)); +} + +TEST_F(CookieSettingsTest, CookiesBlockEverything) { + TestingProfile profile; + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); + cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); + + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( + kFirstPartySite, kFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( + kFirstPartySite, kFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( + kAllowedSite, kFirstPartySite)); +} + +TEST_F(CookieSettingsTest, CookiesBlockEverythingExceptAllowed) { + TestingProfile profile; + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); + cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); + cookie_settings->SetCookieSetting( + ContentSettingsPattern::FromURL(kAllowedSite), + ContentSettingsPattern::Wildcard(), + CONTENT_SETTING_ALLOW); + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( + kFirstPartySite, kFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( + kFirstPartySite, kFirstPartySite)); + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( + kAllowedSite, kFirstPartySite)); + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( + kAllowedSite, kFirstPartySite)); + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( + kAllowedSite, kAllowedSite)); + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( + kAllowedSite, kAllowedSite)); + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); +} + +TEST_F(CookieSettingsTest, CookiesBlockSingleFirstParty) { + TestingProfile profile; + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); + cookie_settings->SetCookieSetting( + ContentSettingsPattern::FromURL(kAllowedSite), + ContentSettingsPattern::FromURL(kFirstPartySite), + CONTENT_SETTING_ALLOW); + cookie_settings->SetCookieSetting( + ContentSettingsPattern::FromURL(kAllowedSite), + ContentSettingsPattern::FromURL(kBlockedFirstPartySite), + CONTENT_SETTING_BLOCK); + + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( + kAllowedSite, kFirstPartySite)); + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( + kAllowedSite, kFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); + + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( + kAllowedSite, kBlockedFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( + kAllowedSite, kBlockedFirstPartySite)); + + cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_BLOCK); + + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( + kAllowedSite, kFirstPartySite)); + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( + kAllowedSite, kFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsCookieSessionOnly(kAllowedSite)); + + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( + kAllowedSite, kBlockedFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( + kAllowedSite, kBlockedFirstPartySite)); + + cookie_settings->ResetCookieSetting( + ContentSettingsPattern::FromURL(kAllowedSite), + ContentSettingsPattern::FromURL(kFirstPartySite)); + + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed( + kAllowedSite, kFirstPartySite)); + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( + kAllowedSite, kFirstPartySite)); +} + +} // namespace diff --git a/chrome/browser/content_settings/host_content_settings_map.cc b/chrome/browser/content_settings/host_content_settings_map.cc index c345ae2..5d491a5 100644 --- a/chrome/browser/content_settings/host_content_settings_map.cc +++ b/chrome/browser/content_settings/host_content_settings_map.cc @@ -37,18 +37,6 @@ namespace { -// Returns true if we should allow all content types for this URL. This is -// true for various internal objects like chrome:// URLs, so UI and other -// things users think of as "not webpages" don't break. -bool ShouldAllowAllContent(const GURL& url, ContentSettingsType content_type) { - if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) - return false; - return url.SchemeIs(chrome::kChromeDevToolsScheme) || - url.SchemeIs(chrome::kChromeInternalScheme) || - url.SchemeIs(chrome::kChromeUIScheme) || - url.SchemeIs(chrome::kExtensionScheme); -} - typedef std::vector<content_settings::Rule> Rules; typedef std::pair<std::string, std::string> StringPair; @@ -87,10 +75,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) { content_settings::ObservableProvider* policy_provider = new content_settings::PolicyProvider(prefs_); policy_provider->AddObserver(this); @@ -115,40 +100,14 @@ HostContentSettingsMap::HostContentSettingsMap( new content_settings::DefaultProvider(prefs_, is_off_the_record_); default_provider->AddObserver(this); content_settings_providers_[DEFAULT_PROVIDER] = default_provider; - - 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); - - pref_change_registrar_.Init(prefs_); - pref_change_registrar_.Add(prefs::kBlockThirdPartyCookies, this); } // static void HostContentSettingsMap::RegisterUserPrefs(PrefService* prefs) { - prefs->RegisterBooleanPref(prefs::kBlockThirdPartyCookies, - false, - PrefService::SYNCABLE_PREF); prefs->RegisterIntegerPref(prefs::kContentSettingsWindowLastTabIndex, 0, PrefService::UNSYNCABLE_PREF); - // Obsolete prefs, for migration: - prefs->RegisterIntegerPref(prefs::kCookieBehavior, - net::StaticCookiePolicy::ALLOW_ALL_COOKIES, - PrefService::UNSYNCABLE_PREF); - // Register the prefs for the content settings providers. content_settings::DefaultProvider::RegisterUserPrefs(prefs); content_settings::PrefProvider::RegisterUserPrefs(prefs); @@ -201,53 +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, CONTENT_SETTINGS_TYPE_COOKIES)) - return CONTENT_SETTING_ALLOW; - - // First get any host-specific settings. - scoped_ptr<base::Value> value; - for (ConstProviderIterator provider = content_settings_providers_.begin(); - provider != content_settings_providers_.end(); - ++provider) { - if (provider->first == DEFAULT_PROVIDER) - continue; - - value.reset(content_settings::GetContentSettingValueAndPatterns( - provider->second, url, first_party_url, CONTENT_SETTINGS_TYPE_COOKIES, - std::string(), is_off_the_record_, NULL, NULL)); - if (value.get()) - break; - } - - // If no explicit exception has been made and third-party cookies are blocked - // by default, apply that rule. - if (!value.get() && 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) - return CONTENT_SETTING_BLOCK; - } - - // If no other policy has changed the setting, use the default. - if (value.get()) - return content_settings::ValueToContentSetting(value.get()); - - return GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES, NULL); -} - ContentSetting HostContentSettingsMap::GetContentSetting( const GURL& primary_url, const GURL& secondary_url, @@ -266,7 +178,6 @@ base::Value* HostContentSettingsMap::GetContentSettingValue( const std::string& resource_identifier, ContentSettingsPattern* primary_pattern, ContentSettingsPattern* secondary_pattern) const { - DCHECK_NE(CONTENT_SETTINGS_TYPE_COOKIES, content_type); DCHECK(content_settings::SupportsResourceIdentifier(content_type) || resource_identifier.empty()); @@ -290,8 +201,7 @@ base::Value* HostContentSettingsMap::GetContentSettingValue( } ContentSettings HostContentSettingsMap::GetContentSettings( - const GURL& primary_url, - const GURL& secondary_url) const { + const GURL& primary_url) const { ContentSettings output; // If we require a resource identifier, set the content settings to default, // otherwise make the defaults explicit. Values for content type @@ -299,15 +209,9 @@ ContentSettings HostContentSettingsMap::GetContentSettings( // |ContentSetting|. So we ignore them here. for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { ContentSettingsType type = ContentSettingsType(j); - if (type == CONTENT_SETTINGS_TYPE_COOKIES) { - output.settings[j] = GetCookieContentSetting( - primary_url, secondary_url, false); - } else if (!ContentTypeHasCompoundValue(type)) { + if (!ContentTypeHasCompoundValue(type)) { output.settings[j] = GetContentSetting( - primary_url, - secondary_url, - ContentSettingsType(j), - std::string()); + primary_url, primary_url, ContentSettingsType(j), std::string()); } } return output; @@ -445,32 +349,6 @@ bool HostContentSettingsMap::IsSettingAllowedForType( } } -void HostContentSettingsMap::SetBlockThirdPartyCookies(bool block) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - DCHECK(prefs_); - - // This setting may not be directly modified for OTR sessions. Instead, it - // is synced to the main profile's setting. - if (is_off_the_record_) { - NOTREACHED(); - return; - } - - // If the preference block-third-party-cookies is managed then do not allow to - // change it. - if (prefs_->IsManagedPreference(prefs::kBlockThirdPartyCookies)) { - NOTREACHED(); - return; - } - - { - base::AutoLock auto_lock(lock_); - block_third_party_cookies_ = block; - } - - prefs_->SetBoolean(prefs::kBlockThirdPartyCookies, block); -} - void HostContentSettingsMap::OnContentSettingChanged( ContentSettingsPattern primary_pattern, ContentSettingsPattern secondary_pattern, @@ -486,34 +364,6 @@ void HostContentSettingsMap::OnContentSettingChanged( content::Details<const ContentSettingsDetails>(&details)); } -void HostContentSettingsMap::Observe( - int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - - if (type == chrome::NOTIFICATION_PREF_CHANGED) { - DCHECK_EQ(prefs_, content::Source<PrefService>(source).ptr()); - if (updating_preferences_) - return; - - std::string* name = content::Details<std::string>(details).ptr(); - if (*name == prefs::kBlockThirdPartyCookies) { - base::AutoLock auto_lock(lock_); - block_third_party_cookies_ = prefs_->GetBoolean( - prefs::kBlockThirdPartyCookies); - is_block_third_party_cookies_managed_ = - prefs_->IsManagedPreference( - prefs::kBlockThirdPartyCookies); - } else { - NOTREACHED() << "Unexpected preference observed"; - return; - } - } else { - NOTREACHED() << "Unexpected notification"; - } -} - HostContentSettingsMap::~HostContentSettingsMap() { DCHECK(!prefs_); STLDeleteValues(&content_settings_providers_); @@ -522,7 +372,6 @@ HostContentSettingsMap::~HostContentSettingsMap() { void HostContentSettingsMap::ShutdownOnUIThread() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(prefs_); - pref_change_registrar_.RemoveAll(); prefs_ = NULL; for (ProviderIterator it = content_settings_providers_.begin(); it != content_settings_providers_.end(); @@ -531,22 +380,6 @@ void HostContentSettingsMap::ShutdownOnUIThread() { } } -void HostContentSettingsMap::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, - (cookie_behavior == net::StaticCookiePolicy::BLOCK_ALL_COOKIES) ? - CONTENT_SETTING_BLOCK : CONTENT_SETTING_ALLOW); - } - if (!prefs_->HasPrefPath(prefs::kBlockThirdPartyCookies)) { - SetBlockThirdPartyCookies(cookie_behavior == - net::StaticCookiePolicy::BLOCK_SETTING_THIRD_PARTY_COOKIES); - } - } -} - void HostContentSettingsMap::AddSettingsForOneType( const content_settings::ProviderInterface* provider, ProviderType provider_type, @@ -568,3 +401,14 @@ void HostContentSettingsMap::AddSettingsForOneType( incognito)); } } + +bool HostContentSettingsMap::ShouldAllowAllContent( + const GURL& url, + ContentSettingsType content_type) { + if (content_type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) + return false; + return url.SchemeIs(chrome::kChromeDevToolsScheme) || + url.SchemeIs(chrome::kChromeInternalScheme) || + url.SchemeIs(chrome::kChromeUIScheme) || + url.SchemeIs(chrome::kExtensionScheme); +} diff --git a/chrome/browser/content_settings/host_content_settings_map.h b/chrome/browser/content_settings/host_content_settings_map.h index 4980656..272df57 100644 --- a/chrome/browser/content_settings/host_content_settings_map.h +++ b/chrome/browser/content_settings/host_content_settings_map.h @@ -21,8 +21,6 @@ #include "chrome/browser/prefs/pref_change_registrar.h" #include "chrome/common/content_settings.h" #include "chrome/common/content_settings_pattern.h" -#include "content/public/browser/notification_observer.h" -#include "content/public/browser/notification_registrar.h" namespace base { class Value; @@ -38,7 +36,6 @@ class PrefService; class HostContentSettingsMap : public content_settings::Observer, - public content::NotificationObserver, public base::RefCountedThreadSafe<HostContentSettingsMap> { public: enum ProviderType { @@ -68,12 +65,12 @@ class HostContentSettingsMap // This may be called on any thread. ContentSettings GetDefaultContentSettings() const; - // Returns a single |ContentSetting| which applies to the given URLs. - // Note that certain internal schemes are whitelisted. - // For |CONTENT_TYPE_COOKIES|, |GetCookieContentSetting| should be called, - // and for content types that can't be converted to a ContentSetting, - // |GetContentSettingValue| should be called. + // Returns a single |ContentSetting| which applies to the given URLs. Note + // that certain internal schemes are whitelisted. For |CONTENT_TYPE_COOKIES|, + // |CookieSettings| should be used instead. For content types that can't be + // converted to a |ContentSetting|, |GetContentSettingValue| should be called. // If there is no content setting, returns CONTENT_SETTING_DEFAULT. + // // May be called on any thread. ContentSetting GetContentSetting( const GURL& primary_url, @@ -97,24 +94,13 @@ class HostContentSettingsMap ContentSettingsPattern* primary_pattern, ContentSettingsPattern* secondary_pattern) const; - // Gets the content setting for cookies. This takes the third party cookie - // flag into account, and therefore needs to know whether we read or write a - // cookie. - // - // This may be called on any thread. - ContentSetting GetCookieContentSetting( - const GURL& url, - const GURL& first_party_url, - bool setting_cookie) const; - - // Returns all ContentSettings which apply to the given URLs. For content - // setting types that require an additional resource identifier, the default - // content setting is returned. + // Returns all ContentSettings which apply to the given |primary_url|. For + // content setting types that require an additional resource identifier, the + // default content setting is returned. // // This may be called on any thread. ContentSettings GetContentSettings( - const GURL& primary_url, - const GURL& secondary_url) const; + const GURL& primary_url) const; // For a given content type, returns all patterns with a non-default setting, // mapped to their actual settings, in the precedence order of the rules. @@ -165,18 +151,6 @@ class HostContentSettingsMap static bool IsSettingAllowedForType(ContentSetting setting, ContentSettingsType content_type); - // This setting trumps any host-specific settings. - bool BlockThirdPartyCookies() const { return block_third_party_cookies_; } - bool IsBlockThirdPartyCookiesManaged() const { - return is_block_third_party_cookies_managed_; - } - - // Sets whether we block all third-party cookies. This method must not be - // invoked on an incognito map. - // - // This should only be called on the UI thread. - void SetBlockThirdPartyCookies(bool block); - // Detaches the HostContentSettingsMap from all Profile-related objects like // PrefService. This methods needs to be called before destroying the Profile. // Afterwards, none of the methods above that should only be called on the UI @@ -190,10 +164,11 @@ class HostContentSettingsMap ContentSettingsType content_type, std::string resource_identifier); - // content::NotificationObserver implementation. - virtual void Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details); + // Returns true if we should allow all content types for this URL. This is + // true for various internal objects like chrome:// URLs, so UI and other + // things users think of as "not webpages" don't break. + static bool ShouldAllowAllContent(const GURL& url, + ContentSettingsType content_type); private: friend class base::RefCountedThreadSafe<HostContentSettingsMap>; @@ -230,8 +205,6 @@ class HostContentSettingsMap // Weak; owned by the Profile. PrefService* prefs_; - PrefChangeRegistrar pref_change_registrar_; - // Whether this settings map is for an OTR session. bool is_off_the_record_; @@ -245,10 +218,6 @@ class HostContentSettingsMap // Used around accesses to the following objects to guarantee thread safety. mutable base::Lock lock_; - // Misc global settings. - bool block_third_party_cookies_; - bool is_block_third_party_cookies_managed_; - DISALLOW_COPY_AND_ASSIGN(HostContentSettingsMap); }; 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 6cd7e7c9..312f1d9 100644 --- a/chrome/browser/content_settings/host_content_settings_map_unittest.cc +++ b/chrome/browser/content_settings/host_content_settings_map_unittest.cc @@ -7,6 +7,7 @@ #include "base/json/json_reader.h" #include "base/json/json_writer.h" #include "chrome/browser/content_settings/content_settings_details.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/content_settings/mock_settings_observer.h" #include "chrome/browser/prefs/pref_service.h" @@ -162,7 +163,7 @@ TEST_F(HostContentSettingsMapTest, IndividualSettings) { desired_settings.settings[CONTENT_SETTINGS_TYPE_MOUSELOCK] = CONTENT_SETTING_ASK; ContentSettings settings = - host_content_settings_map->GetContentSettings(host, host); + host_content_settings_map->GetContentSettings(host); EXPECT_TRUE(SettingsEqual(desired_settings, settings)); // Check returning all hosts for a setting. @@ -341,10 +342,10 @@ TEST_F(HostContentSettingsMapTest, ObserveDefaultPref) { GURL host("http://example.com"); host_content_settings_map->SetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); + CONTENT_SETTINGS_TYPE_IMAGES, CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - host, host, true)); + host_content_settings_map->GetContentSetting( + host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); // Make a copy of the pref's new value so we can reset it later. scoped_ptr<Value> new_value(prefs->FindPreference( @@ -353,14 +354,14 @@ TEST_F(HostContentSettingsMapTest, ObserveDefaultPref) { // Clearing the backing pref should also clear the internal cache. prefs->Set(prefs::kDefaultContentSettings, *default_value); EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - host, host, true)); + host_content_settings_map->GetContentSetting( + host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); // Reseting the pref to its previous value should update the cache. prefs->Set(prefs::kDefaultContentSettings, *new_value); EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - host, host, true)); + host_content_settings_map->GetContentSetting( + host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); } TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) { @@ -379,18 +380,18 @@ TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) { GURL host("http://example.com"); EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - host, host, true)); + host_content_settings_map->GetContentSetting( + host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); host_content_settings_map->SetContentSetting( pattern, ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_COOKIES, + CONTENT_SETTINGS_TYPE_IMAGES, std::string(), CONTENT_SETTING_BLOCK); EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - host, host, true)); + host_content_settings_map->GetContentSetting( + host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); // Make a copy of the pref's new value so we can reset it later. scoped_ptr<Value> new_value(prefs->FindPreference( @@ -399,20 +400,21 @@ TEST_F(HostContentSettingsMapTest, ObserveExceptionPref) { // Clearing the backing pref should also clear the internal cache. prefs->Set(prefs::kContentSettingsPatternPairs, *default_value); EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - host, host, true)); + host_content_settings_map->GetContentSetting( + host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); // Reseting the pref to its previous value should update the cache. prefs->Set(prefs::kContentSettingsPatternPairs, *new_value); EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - host, host, true)); + host_content_settings_map->GetContentSetting( + host, host, CONTENT_SETTINGS_TYPE_IMAGES, "")); } TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { TestingProfile profile; HostContentSettingsMap* host_content_settings_map = profile.GetHostContentSettingsMap(); + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); ContentSettingsPattern pattern = ContentSettingsPattern::FromString("[*.]example.com"); @@ -449,27 +451,24 @@ TEST_F(HostContentSettingsMapTest, HostTrimEndingDotCheck) { CONTENT_SETTINGS_TYPE_IMAGES, "")); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - host_ending_with_dot, host_ending_with_dot, true)); + 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, "", CONTENT_SETTING_DEFAULT); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - host_ending_with_dot, host_ending_with_dot, true)); + 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, "", CONTENT_SETTING_BLOCK); - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - host_ending_with_dot, host_ending_with_dot, true)); + EXPECT_FALSE(cookie_settings->IsSettingCookieAllowed( + host_ending_with_dot, host_ending_with_dot)); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( @@ -623,7 +622,7 @@ TEST_F(HostContentSettingsMapTest, NestedSettings) { desired_settings.settings[CONTENT_SETTINGS_TYPE_MOUSELOCK] = CONTENT_SETTING_ASK; ContentSettings settings = - host_content_settings_map->GetContentSettings(host, host); + host_content_settings_map->GetContentSettings(host); EXPECT_TRUE(SettingsEqual(desired_settings, settings)); EXPECT_EQ(desired_settings.settings[CONTENT_SETTINGS_TYPE_COOKIES], settings.settings[CONTENT_SETTINGS_TYPE_COOKIES]); @@ -696,14 +695,11 @@ TEST_F(HostContentSettingsMapTest, OffTheRecord) { otr_map->ShutdownOnUIThread(); } -TEST_F(HostContentSettingsMapTest, MigrateObsoletePrefs) { +TEST_F(HostContentSettingsMapTest, MigrateObsoletePopupPrefs) { TestingProfile profile; PrefService* prefs = profile.GetPrefs(); // Set obsolete data. - prefs->SetInteger(prefs::kCookieBehavior, - net::StaticCookiePolicy::BLOCK_ALL_COOKIES); - ListValue popup_hosts; popup_hosts.Append(new StringValue("[*.]example.com")); prefs->Set(prefs::kPopupWhitelistedHosts, popup_hosts); @@ -711,10 +707,6 @@ TEST_F(HostContentSettingsMapTest, MigrateObsoletePrefs) { HostContentSettingsMap* host_content_settings_map = profile.GetHostContentSettingsMap(); - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_COOKIES, NULL)); - GURL host("http://example.com"); EXPECT_EQ(CONTENT_SETTING_ALLOW, host_content_settings_map->GetContentSetting( @@ -820,7 +812,7 @@ TEST_F(HostContentSettingsMapTest, ResourceIdentifier) { host_content_settings_map->GetDefaultContentSetting( CONTENT_SETTINGS_TYPE_PLUGINS, NULL); ContentSettings settings = - host_content_settings_map->GetContentSettings(host, host); + host_content_settings_map->GetContentSettings(host); EXPECT_EQ(default_plugin_setting, settings.settings[CONTENT_SETTINGS_TYPE_PLUGINS]); @@ -1070,189 +1062,3 @@ TEST_F(HostContentSettingsMapTest, SettingDefaultContentSettingsWhenManaged) { host_content_settings_map->GetDefaultContentSetting( CONTENT_SETTINGS_TYPE_PLUGINS, NULL)); } - -// Tests for cookie content settings. -const GURL kBlockedSite = GURL("http://ads.thirdparty.com"); -const GURL kAllowedSite = GURL("http://good.allays.com"); -const GURL kFirstPartySite = GURL("http://cool.things.com"); -const GURL kExtensionURL = GURL("chrome-extension://deadbeef"); - -TEST_F(HostContentSettingsMapTest, CookiesBlockSingle) { - TestingProfile profile; - HostContentSettingsMap* host_content_settings_map = - profile.GetHostContentSettingsMap(); - host_content_settings_map->AddExceptionForURL( - kBlockedSite, - kBlockedSite, - CONTENT_SETTINGS_TYPE_COOKIES, - "", - CONTENT_SETTING_BLOCK); - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kBlockedSite, false)); -} - -TEST_F(HostContentSettingsMapTest, CookiesBlockThirdParty) { - TestingProfile profile; - HostContentSettingsMap* host_content_settings_map = - profile.GetHostContentSettingsMap(); - host_content_settings_map->SetBlockThirdPartyCookies(true); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kFirstPartySite, false)); - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kFirstPartySite, true)); - - CommandLine* cmd = CommandLine::ForCurrentProcess(); - AutoReset<CommandLine> auto_reset(cmd, *cmd); - cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies); - - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kFirstPartySite, false)); - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kFirstPartySite, true)); -} - -TEST_F(HostContentSettingsMapTest, CookiesAllowThirdParty) { - TestingProfile profile; - HostContentSettingsMap* host_content_settings_map = - profile.GetHostContentSettingsMap(); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kFirstPartySite, false)); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kFirstPartySite, true)); -} - -TEST_F(HostContentSettingsMapTest, CookiesExplicitBlockSingleThirdParty) { - TestingProfile profile; - HostContentSettingsMap* host_content_settings_map = - profile.GetHostContentSettingsMap(); - host_content_settings_map->AddExceptionForURL( - kBlockedSite, kBlockedSite, CONTENT_SETTINGS_TYPE_COOKIES, "", - CONTENT_SETTING_BLOCK); - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kFirstPartySite, false)); - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kFirstPartySite, true)); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kFirstPartySite, true)); -} - -TEST_F(HostContentSettingsMapTest, CookiesExplicitSessionOnly) { - TestingProfile profile; - HostContentSettingsMap* host_content_settings_map = - profile.GetHostContentSettingsMap(); - host_content_settings_map->AddExceptionForURL( - kBlockedSite, kBlockedSite, CONTENT_SETTINGS_TYPE_COOKIES, "", - CONTENT_SETTING_SESSION_ONLY); - EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kFirstPartySite, false)); - EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kFirstPartySite, true)); - - host_content_settings_map->SetBlockThirdPartyCookies(true); - EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kFirstPartySite, false)); - EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY, - host_content_settings_map->GetCookieContentSetting( - kBlockedSite, kFirstPartySite, true)); -} - -TEST_F(HostContentSettingsMapTest, CookiesThirdPartyBlockedExplicitAllow) { - TestingProfile profile; - HostContentSettingsMap* host_content_settings_map = - profile.GetHostContentSettingsMap(); - host_content_settings_map->AddExceptionForURL( - kAllowedSite, kAllowedSite, CONTENT_SETTINGS_TYPE_COOKIES, "", - CONTENT_SETTING_ALLOW); - host_content_settings_map->SetBlockThirdPartyCookies(true); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kFirstPartySite, false)); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kFirstPartySite, true)); - - // Extensions should always be allowed to use cookies. - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kExtensionURL, false)); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kExtensionURL, true)); - - CommandLine* cmd = CommandLine::ForCurrentProcess(); - AutoReset<CommandLine> auto_reset(cmd, *cmd); - cmd->AppendSwitch(switches::kBlockReadingThirdPartyCookies); - - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kFirstPartySite, false)); - - // Extensions should always be allowed to use cookies. - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kExtensionURL, false)); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kExtensionURL, true)); -} - -TEST_F(HostContentSettingsMapTest, CookiesBlockEverything) { - TestingProfile profile; - HostContentSettingsMap* host_content_settings_map = - profile.GetHostContentSettingsMap(); - host_content_settings_map->SetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); - - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - kFirstPartySite, kFirstPartySite, false)); - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - kFirstPartySite, kFirstPartySite, true)); - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kFirstPartySite, true)); -} - -TEST_F(HostContentSettingsMapTest, CookiesBlockEverythingExceptAllowed) { - TestingProfile profile; - HostContentSettingsMap* host_content_settings_map = - profile.GetHostContentSettingsMap(); - host_content_settings_map->SetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); - host_content_settings_map->AddExceptionForURL( - kAllowedSite, kAllowedSite, CONTENT_SETTINGS_TYPE_COOKIES, "", - CONTENT_SETTING_ALLOW); - - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - kFirstPartySite, kFirstPartySite, false)); - EXPECT_EQ(CONTENT_SETTING_BLOCK, - host_content_settings_map->GetCookieContentSetting( - kFirstPartySite, kFirstPartySite, true)); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kFirstPartySite, false)); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kFirstPartySite, true)); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kAllowedSite, false)); - EXPECT_EQ(CONTENT_SETTING_ALLOW, - host_content_settings_map->GetCookieContentSetting( - kAllowedSite, kAllowedSite, true)); -} diff --git a/chrome/browser/content_settings/mock_settings_observer.cc b/chrome/browser/content_settings/mock_settings_observer.cc index 94cada60..a0cc5b1 100644 --- a/chrome/browser/content_settings/mock_settings_observer.cc +++ b/chrome/browser/content_settings/mock_settings_observer.cc @@ -34,6 +34,5 @@ void MockSettingsObserver::Observe( settings_details->update_all()); // This checks that calling a Get function from an observer doesn't // deadlock. - map->GetContentSettings(GURL("http://random-hostname.com/"), - GURL("http://random-hostname.com/")); + map->GetContentSettings(GURL("http://random-hostname.com/")); } diff --git a/chrome/browser/content_settings/tab_specific_content_settings.cc b/chrome/browser/content_settings/tab_specific_content_settings.cc index b15e1f7..5a56797 100644 --- a/chrome/browser/content_settings/tab_specific_content_settings.cc +++ b/chrome/browser/content_settings/tab_specific_content_settings.cc @@ -488,7 +488,7 @@ void TabSpecificContentSettings::Observe( Send(new ChromeViewMsg_SetDefaultContentSettings( map->GetDefaultContentSettings())); Send(new ChromeViewMsg_SetContentSettingsForCurrentURL( - entry_url, map->GetContentSettings(entry_url, entry_url))); + entry_url, map->GetContentSettings(entry_url))); ContentSettingsForOneType settings; map->GetSettingsForOneType(CONTENT_SETTINGS_TYPE_IMAGES, "", &settings); Send(new ChromeViewMsg_SetImageSettingRules(settings)); diff --git a/chrome/browser/cookies_tree_model.cc b/chrome/browser/cookies_tree_model.cc index 002b8e7..348ee09 100644 --- a/chrome/browser/cookies_tree_model.cc +++ b/chrome/browser/cookies_tree_model.cc @@ -15,7 +15,7 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/browser/browsing_data_cookie_helper.h" -#include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/extensions/extension_service.h" #include "content/browser/in_process_webkit/webkit_context.h" #include "grit/generated_resources.h" @@ -453,13 +453,17 @@ CookieTreeQuotaNode* CookieTreeOriginNode::UpdateOrCreateQuotaNode( } void CookieTreeOriginNode::CreateContentException( - HostContentSettingsMap* content_settings, ContentSetting setting) const { + CookieSettings* cookie_settings, ContentSetting setting) const { + DCHECK(setting == CONTENT_SETTING_ALLOW || + setting == CONTENT_SETTING_BLOCK || + setting == CONTENT_SETTING_SESSION_ONLY); if (CanCreateContentException()) { - content_settings->AddExceptionForURL(url_, - url_, - CONTENT_SETTINGS_TYPE_COOKIES, - "", - setting); + cookie_settings->ResetCookieSetting( + ContentSettingsPattern::FromURLNoWildcard(url_), + ContentSettingsPattern::Wildcard()); + cookie_settings->SetCookieSetting( + ContentSettingsPattern::FromURL(url_), + ContentSettingsPattern::Wildcard(), setting); } } diff --git a/chrome/browser/cookies_tree_model.h b/chrome/browser/cookies_tree_model.h index f7aa859..694139b 100644 --- a/chrome/browser/cookies_tree_model.h +++ b/chrome/browser/cookies_tree_model.h @@ -28,6 +28,7 @@ #include "ui/base/models/tree_node_model.h" class BrowsingDataCookieHelper; +class CookieSettings; class CookiesTreeModel; class CookieTreeAppCacheNode; class CookieTreeAppCachesNode; @@ -45,7 +46,6 @@ class CookieTreeSessionStoragesNode; class CookieTreeIndexedDBNode; class CookieTreeIndexedDBsNode; class CookieTreeOriginNode; -class HostContentSettingsMap; // CookieTreeNode ------------------------------------------------------------- // The base node type in the Cookies, Databases, and Local Storage options @@ -228,7 +228,7 @@ class CookieTreeOriginNode : public CookieTreeNode { // Creates an content exception for this origin of type // CONTENT_SETTINGS_TYPE_COOKIES. - void CreateContentException(HostContentSettingsMap* content_settings, + void CreateContentException(CookieSettings* cookie_settings, ContentSetting setting) const; // True if a content exception can be created for this origin. diff --git a/chrome/browser/cookies_tree_model_unittest.cc b/chrome/browser/cookies_tree_model_unittest.cc index 275f2d7..ea34a09 100644 --- a/chrome/browser/cookies_tree_model_unittest.cc +++ b/chrome/browser/cookies_tree_model_unittest.cc @@ -6,6 +6,7 @@ #include <string> +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/content_settings/mock_settings_observer.h" #include "chrome/browser/mock_browsing_data_appcache_helper.h" @@ -948,6 +949,7 @@ TEST_F(CookiesTreeModelTest, ContentSettings) { TestingProfile profile; HostContentSettingsMap* content_settings = profile.GetHostContentSettingsMap(); + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); MockSettingsObserver observer; CookieTreeRootNode* root = @@ -972,9 +974,9 @@ TEST_F(CookiesTreeModelTest, ContentSettings) { ContentSettingsPattern::Wildcard(), false)); origin->CreateContentException( - content_settings, CONTENT_SETTING_SESSION_ONLY); - EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY, - content_settings->GetCookieContentSetting(host, host, true)); + cookie_settings, CONTENT_SETTING_SESSION_ONLY); + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed(host, host)); + EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(host)); } TEST_F(CookiesTreeModelTest, FileSystemFilter) { diff --git a/chrome/browser/extensions/extension_content_settings_api.cc b/chrome/browser/extensions/extension_content_settings_api.cc index 82305d3..f6a76bd 100644 --- a/chrome/browser/extensions/extension_content_settings_api.cc +++ b/chrome/browser/extensions/extension_content_settings_api.cc @@ -9,6 +9,7 @@ #include "base/bind.h" #include "base/command_line.h" #include "base/values.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/extensions/extension_content_settings_api_constants.h" #include "chrome/browser/extensions/extension_content_settings_helpers.h" @@ -128,6 +129,7 @@ bool GetContentSettingFunction::RunImpl() { } HostContentSettingsMap* map; + CookieSettings* cookie_settings; if (incognito) { if (!profile()->HasOffTheRecordProfile()) { // TODO(bauerb): Allow reading incognito content settings @@ -136,16 +138,19 @@ bool GetContentSettingFunction::RunImpl() { return false; } map = profile()->GetOffTheRecordProfile()->GetHostContentSettingsMap(); + cookie_settings = CookieSettings::GetForProfile( + profile()->GetOffTheRecordProfile()); } else { map = profile()->GetHostContentSettingsMap(); + cookie_settings = CookieSettings::GetForProfile(profile()); } ContentSetting setting; if (content_type == CONTENT_SETTINGS_TYPE_COOKIES) { // TODO(jochen): Do we return the value for setting or for reading cookies? bool setting_cookie = false; - setting = map->GetCookieContentSetting(primary_url, secondary_url, - setting_cookie); + setting = cookie_settings->GetCookieSetting(primary_url, secondary_url, + setting_cookie); } else { setting = map->GetContentSetting(primary_url, secondary_url, content_type, resource_identifier); diff --git a/chrome/browser/extensions/extension_content_settings_apitest.cc b/chrome/browser/extensions/extension_content_settings_apitest.cc index 9d7e568..e0b7955 100644 --- a/chrome/browser/extensions/extension_content_settings_apitest.cc +++ b/chrome/browser/extensions/extension_content_settings_apitest.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "base/utf_string_conversions.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/extensions/extension_apitest.h" #include "chrome/browser/extensions/extension_content_settings_api.h" @@ -21,12 +22,16 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentSettings) { HostContentSettingsMap* map = browser()->profile()->GetHostContentSettingsMap(); + CookieSettings* cookie_settings = + CookieSettings::GetForProfile(browser()->profile()); // Check default content settings by using an unknown URL. GURL example_url("http://www.example.com"); - EXPECT_EQ(CONTENT_SETTING_SESSION_ONLY, - map->GetCookieContentSetting( - example_url, example_url, false)); + EXPECT_TRUE(cookie_settings->IsReadingCookieAllowed( + example_url, example_url)); + EXPECT_TRUE(cookie_settings->IsSettingCookieAllowed( + example_url, example_url)); + EXPECT_TRUE(cookie_settings->IsCookieSessionOnly(example_url)); EXPECT_EQ(CONTENT_SETTING_ALLOW, map->GetContentSetting(example_url, example_url, @@ -64,8 +69,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ContentSettings) { // Check content settings for www.google.com GURL url("http://www.google.com"); - EXPECT_EQ(CONTENT_SETTING_BLOCK, - map->GetCookieContentSetting(url, url, false)); + EXPECT_FALSE(cookie_settings->IsReadingCookieAllowed(url, url)); EXPECT_EQ(CONTENT_SETTING_ALLOW, map->GetContentSetting( url, url, CONTENT_SETTINGS_TYPE_IMAGES, "")); diff --git a/chrome/browser/extensions/extension_special_storage_policy.cc b/chrome/browser/extensions/extension_special_storage_policy.cc index eddaedf..79ef082 100644 --- a/chrome/browser/extensions/extension_special_storage_policy.cc +++ b/chrome/browser/extensions/extension_special_storage_policy.cc @@ -6,7 +6,7 @@ #include "base/bind.h" #include "base/logging.h" -#include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/common/content_settings.h" #include "chrome/common/content_settings_types.h" #include "chrome/common/extensions/extension.h" @@ -14,8 +14,8 @@ #include "content/browser/browser_thread.h" ExtensionSpecialStoragePolicy::ExtensionSpecialStoragePolicy( - HostContentSettingsMap* host_content_settings_map) - : host_content_settings_map_(host_content_settings_map) {} + CookieSettings* cookie_settings) + : cookie_settings_(cookie_settings) {} ExtensionSpecialStoragePolicy::~ExtensionSpecialStoragePolicy() {} @@ -32,22 +32,19 @@ bool ExtensionSpecialStoragePolicy::IsStorageUnlimited(const GURL& origin) { } bool ExtensionSpecialStoragePolicy::IsStorageSessionOnly(const GURL& origin) { - if (host_content_settings_map_ == NULL) + if (cookie_settings_ == NULL) return false; - ContentSetting content_setting = host_content_settings_map_-> - GetCookieContentSetting(origin, origin, true); - return (content_setting == CONTENT_SETTING_SESSION_ONLY); + return cookie_settings_->IsCookieSessionOnly(origin); } bool ExtensionSpecialStoragePolicy::HasSessionOnlyOrigins() { - if (host_content_settings_map_ == NULL) + if (cookie_settings_ == NULL) return false; - if (host_content_settings_map_->GetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_COOKIES, NULL) == CONTENT_SETTING_SESSION_ONLY) + if (cookie_settings_->GetDefaultCookieSetting(NULL) == + CONTENT_SETTING_SESSION_ONLY) return true; ContentSettingsForOneType entries; - host_content_settings_map_->GetSettingsForOneType( - CONTENT_SETTINGS_TYPE_COOKIES, "", &entries); + cookie_settings_->GetCookieSettings(&entries); for (size_t i = 0; i < entries.size(); ++i) { if (entries[i].setting == CONTENT_SETTING_SESSION_ONLY) return true; diff --git a/chrome/browser/extensions/extension_special_storage_policy.h b/chrome/browser/extensions/extension_special_storage_policy.h index bce392c..df2f2a3 100644 --- a/chrome/browser/extensions/extension_special_storage_policy.h +++ b/chrome/browser/extensions/extension_special_storage_policy.h @@ -13,16 +13,15 @@ #include "googleurl/src/gurl.h" #include "webkit/quota/special_storage_policy.h" +class CookieSettings; class Extension; -class HostContentSettingsMap; // Special rights are granted to 'extensions' and 'applications'. The // storage subsystems and the browsing data remover query this interface // to determine which origins have these rights. class ExtensionSpecialStoragePolicy : public quota::SpecialStoragePolicy { public: - explicit ExtensionSpecialStoragePolicy( - HostContentSettingsMap* host_content_settings_map); + explicit ExtensionSpecialStoragePolicy(CookieSettings* cookie_settings); // SpecialStoragePolicy methods used by storage subsystems and the browsing // data remover. These methods are safe to call on any thread. @@ -65,7 +64,7 @@ class ExtensionSpecialStoragePolicy : public quota::SpecialStoragePolicy { SpecialCollection protected_apps_; SpecialCollection unlimited_extensions_; SpecialCollection file_handler_extensions_; - scoped_refptr<HostContentSettingsMap> host_content_settings_map_; + scoped_refptr<CookieSettings> cookie_settings_; }; #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SPECIAL_STORAGE_POLICY_H_ diff --git a/chrome/browser/extensions/extension_special_storage_policy_unittest.cc b/chrome/browser/extensions/extension_special_storage_policy_unittest.cc index 938b3f9..892d925 100644 --- a/chrome/browser/extensions/extension_special_storage_policy_unittest.cc +++ b/chrome/browser/extensions/extension_special_storage_policy_unittest.cc @@ -3,7 +3,7 @@ // found in the LICENSE file. #include "base/values.h" -#include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/extensions/extension_special_storage_policy.h" #include "chrome/common/content_settings.h" #include "chrome/common/content_settings_types.h" @@ -220,36 +220,32 @@ TEST_F(ExtensionSpecialStoragePolicyTest, HasSessionOnlyOrigins) { BrowserThread ui_thread(BrowserThread::UI, &message_loop); TestingProfile profile; - HostContentSettingsMap* host_content_settings_map = - profile.GetHostContentSettingsMap(); + CookieSettings* cookie_settings = CookieSettings::GetForProfile(&profile); scoped_refptr<ExtensionSpecialStoragePolicy> policy( - new ExtensionSpecialStoragePolicy(host_content_settings_map)); + new ExtensionSpecialStoragePolicy(cookie_settings)); EXPECT_FALSE(policy->HasSessionOnlyOrigins()); // The default setting can be session-only. - host_content_settings_map->SetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_SESSION_ONLY); + cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_SESSION_ONLY); EXPECT_TRUE(policy->HasSessionOnlyOrigins()); - host_content_settings_map->SetDefaultContentSetting( - CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_ALLOW); + cookie_settings->SetDefaultCookieSetting(CONTENT_SETTING_ALLOW); EXPECT_FALSE(policy->HasSessionOnlyOrigins()); // Or the session-onlyness can affect individual origins. ContentSettingsPattern pattern = ContentSettingsPattern::FromString("pattern.com"); - host_content_settings_map->SetContentSetting( - pattern, ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_COOKIES, "", CONTENT_SETTING_SESSION_ONLY); + cookie_settings->SetCookieSetting(pattern, + ContentSettingsPattern::Wildcard(), + CONTENT_SETTING_SESSION_ONLY); EXPECT_TRUE(policy->HasSessionOnlyOrigins()); // Clearing an origin-spesific rule. - host_content_settings_map->SetContentSetting( - pattern, ContentSettingsPattern::Wildcard(), - CONTENT_SETTINGS_TYPE_COOKIES, "", CONTENT_SETTING_DEFAULT); + cookie_settings->ResetCookieSetting(pattern, + ContentSettingsPattern::Wildcard()); EXPECT_FALSE(policy->HasSessionOnlyOrigins()); } diff --git a/chrome/browser/net/cookie_policy_browsertest.cc b/chrome/browser/net/cookie_policy_browsertest.cc index 74e5eaa..2128a15 100644 --- a/chrome/browser/net/cookie_policy_browsertest.cc +++ b/chrome/browser/net/cookie_policy_browsertest.cc @@ -6,8 +6,10 @@ #include "base/task.h" #include "base/synchronization/waitable_event.h" #include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" +#include "chrome/common/pref_names.h" #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/ui_test_utils.h" #include "net/base/cookie_store.h" @@ -80,8 +82,8 @@ class CookiePolicyBrowserTest : public InProcessBrowserTest { IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) { ASSERT_TRUE(test_server()->Start()); - browser()->profile()->GetHostContentSettingsMap()-> - SetBlockThirdPartyCookies(true); + browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, + true); GURL url(test_server()->GetURL("set-cookie?cookie1")); @@ -100,8 +102,8 @@ IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookiesRedirect) { ASSERT_TRUE(test_server()->Start()); - browser()->profile()->GetHostContentSettingsMap()-> - SetBlockThirdPartyCookies(true); + browser()->profile()->GetPrefs()->SetBoolean(prefs::kBlockThirdPartyCookies, + true); GURL url(test_server()->GetURL("server-redirect?")); GURL redirected_url(test_server()->GetURL("set-cookie?cookie2")); diff --git a/chrome/browser/prefs/browser_prefs.cc b/chrome/browser/prefs/browser_prefs.cc index e9a6dfe..7419011 100644 --- a/chrome/browser/prefs/browser_prefs.cc +++ b/chrome/browser/prefs/browser_prefs.cc @@ -10,6 +10,7 @@ #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/bookmarks/bookmark_utils.h" #include "chrome/browser/browser_shutdown.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/custom_handlers/protocol_handler_registry.h" #include "chrome/browser/debugger/devtools_window.h" @@ -162,6 +163,7 @@ void RegisterUserPrefs(PrefService* user_prefs) { ProfileImpl::RegisterUserPrefs(user_prefs); PromoResourceService::RegisterUserPrefs(user_prefs); HostContentSettingsMap::RegisterUserPrefs(user_prefs); + CookieSettings::RegisterUserPrefs(user_prefs); DevToolsWindow::RegisterUserPrefs(user_prefs); PinnedTabCodec::RegisterUserPrefs(user_prefs); ExtensionPrefs::RegisterUserPrefs(user_prefs); diff --git a/chrome/browser/profiles/profile_dependency_manager.cc b/chrome/browser/profiles/profile_dependency_manager.cc index 914006b..3eee33d 100644 --- a/chrome/browser/profiles/profile_dependency_manager.cc +++ b/chrome/browser/profiles/profile_dependency_manager.cc @@ -10,6 +10,7 @@ #include "chrome/browser/autofill/personal_data_manager_factory.h" #include "chrome/browser/background/background_contents_service_factory.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/extensions/speech_input/extension_speech_input_manager.h" #include "chrome/browser/plugin_prefs_factory.h" #include "chrome/browser/prerender/prerender_manager_factory.h" @@ -40,13 +41,14 @@ void AssertFactoriesBuilt() { if (!g_initialized) { BackgroundContentsServiceFactory::GetInstance(); CloudPrintProxyServiceFactory::GetInstance(); + CookieSettings::Factory::GetInstance(); + ExtensionSpeechInputManager::InitializeFactory(); PersonalDataManagerFactory::GetInstance(); PluginPrefsFactory::GetInstance(); prerender::PrerenderManagerFactory::GetInstance(); SessionServiceFactory::GetInstance(); TabRestoreServiceFactory::GetInstance(); TemplateURLServiceFactory::GetInstance(); - ExtensionSpeechInputManager::InitializeFactory(); g_initialized = true; } diff --git a/chrome/browser/profiles/profile_impl.cc b/chrome/browser/profiles/profile_impl.cc index c1a8d50..592aa8c 100644 --- a/chrome/browser/profiles/profile_impl.cc +++ b/chrome/browser/profiles/profile_impl.cc @@ -22,6 +22,7 @@ #include "chrome/browser/browser_process.h" #include "chrome/browser/browsing_data_remover.h" #include "chrome/browser/chrome_plugin_service_filter.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/custom_handlers/protocol_handler_registry.h" #include "chrome/browser/defaults.h" @@ -823,7 +824,7 @@ ExtensionSpecialStoragePolicy* ProfileImpl::GetExtensionSpecialStoragePolicy() { if (!extension_special_storage_policy_.get()) { extension_special_storage_policy_ = - new ExtensionSpecialStoragePolicy(GetHostContentSettingsMap()); + new ExtensionSpecialStoragePolicy(CookieSettings::GetForProfile(this)); } return extension_special_storage_policy_.get(); } diff --git a/chrome/browser/profiles/profile_io_data.cc b/chrome/browser/profiles/profile_io_data.cc index 7f138d0..af74542 100644 --- a/chrome/browser/profiles/profile_io_data.cc +++ b/chrome/browser/profiles/profile_io_data.cc @@ -15,6 +15,7 @@ #include "base/string_number_conversions.h" #include "base/task.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/custom_handlers/protocol_handler_registry.h" #include "chrome/browser/extensions/extension_info_map.h" @@ -220,6 +221,7 @@ void ProfileIOData::InitializeOnUIThread(Profile* profile) { params->io_thread = g_browser_process->io_thread(); params->host_content_settings_map = profile->GetHostContentSettingsMap(); + params->cookie_settings = CookieSettings::GetForProfile(profile); params->host_zoom_map = profile->GetHostZoomMap(); params->ssl_config_service = profile->GetSSLConfigService(); base::Callback<Profile*(void)> profile_getter = @@ -371,6 +373,10 @@ HostContentSettingsMap* ProfileIOData::GetHostContentSettingsMap() const { return host_content_settings_map_; } +CookieSettings* ProfileIOData::GetCookieSettings() const { + return cookie_settings_; +} + DesktopNotificationService* ProfileIOData::GetNotificationService() const { return notification_service_; } @@ -486,6 +492,7 @@ void ProfileIOData::LazyInitialize() const { quota_manager_ = profile_params_->quota_manager; host_zoom_map_ = profile_params_->host_zoom_map; host_content_settings_map_ = profile_params_->host_content_settings_map; + cookie_settings_ = profile_params_->cookie_settings; notification_service_ = profile_params_->notification_service; extension_info_map_ = profile_params_->extension_info_map; diff --git a/chrome/browser/profiles/profile_io_data.h b/chrome/browser/profiles/profile_io_data.h index 9787647..9af7f84 100644 --- a/chrome/browser/profiles/profile_io_data.h +++ b/chrome/browser/profiles/profile_io_data.h @@ -23,6 +23,7 @@ class CommandLine; class ChromeAppCacheService; class ChromeBlobStorageContext; +class CookieSettings; class DesktopNotificationService; class ExtensionInfoMap; class HostContentSettingsMap; @@ -101,6 +102,7 @@ class ProfileIOData { // that profile. ExtensionInfoMap* GetExtensionInfoMap() const; HostContentSettingsMap* GetHostContentSettingsMap() const; + CookieSettings* GetCookieSettings() const; DesktopNotificationService* GetNotificationService() const; BooleanPrefMember* clear_local_state_on_exit() const { @@ -147,6 +149,7 @@ class ProfileIOData { std::string referrer_charset; IOThread* io_thread; scoped_refptr<HostContentSettingsMap> host_content_settings_map; + scoped_refptr<CookieSettings> cookie_settings; scoped_refptr<HostZoomMap> host_zoom_map; scoped_refptr<net::SSLConfigService> ssl_config_service; scoped_refptr<net::CookieMonster::Delegate> cookie_monster_delegate; @@ -300,6 +303,7 @@ class ProfileIOData { // TODO(willchan): Remove from ResourceContext. mutable scoped_refptr<ExtensionInfoMap> extension_info_map_; mutable scoped_refptr<HostContentSettingsMap> host_content_settings_map_; + mutable scoped_refptr<CookieSettings> cookie_settings_; mutable DesktopNotificationService* notification_service_; mutable ResourceContext resource_context_; diff --git a/chrome/browser/renderer_host/chrome_render_message_filter.cc b/chrome/browser/renderer_host/chrome_render_message_filter.cc index 6e8f263..5472214 100644 --- a/chrome/browser/renderer_host/chrome_render_message_filter.cc +++ b/chrome/browser/renderer_host/chrome_render_message_filter.cc @@ -11,6 +11,7 @@ #include "chrome/browser/automation/automation_resource_message_filter.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/content_settings/content_settings_utils.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/content_settings/tab_specific_content_settings.h" #include "chrome/browser/extensions/extension_event_router.h" @@ -100,6 +101,7 @@ ChromeRenderMessageFilter::ChromeRenderMessageFilter( profile_->GetPrefs(), NULL); always_authorize_plugins_.MoveToThread(BrowserThread::IO); host_content_settings_map_ = profile->GetHostContentSettingsMap(); + cookie_settings_ = CookieSettings::GetForProfile(profile); } ChromeRenderMessageFilter::~ChromeRenderMessageFilter() { @@ -434,13 +436,8 @@ void ChromeRenderMessageFilter::OnAllowDatabase(int render_view_id, const string16& name, const string16& display_name, bool* allowed) { - ContentSetting setting = host_content_settings_map_->GetCookieContentSetting( - origin_url, top_origin_url, true); - DCHECK((setting == CONTENT_SETTING_ALLOW) || - (setting == CONTENT_SETTING_BLOCK) || - (setting == CONTENT_SETTING_SESSION_ONLY)); - *allowed = setting != CONTENT_SETTING_BLOCK; - + *allowed = cookie_settings_->IsSettingCookieAllowed(origin_url, + top_origin_url); BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, NewRunnableFunction( @@ -454,9 +451,8 @@ void ChromeRenderMessageFilter::OnAllowDOMStorage(int render_view_id, const GURL& top_origin_url, DOMStorageType type, bool* allowed) { - ContentSetting setting = host_content_settings_map_->GetCookieContentSetting( - origin_url, top_origin_url, true); - *allowed = setting != CONTENT_SETTING_BLOCK; + *allowed = cookie_settings_->IsSettingCookieAllowed(origin_url, + top_origin_url); // Record access to DOM storage for potential display in UI. BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, @@ -469,12 +465,8 @@ void ChromeRenderMessageFilter::OnAllowFileSystem(int render_view_id, const GURL& origin_url, const GURL& top_origin_url, bool* allowed) { - ContentSetting setting = host_content_settings_map_->GetCookieContentSetting( - origin_url, top_origin_url, true); - DCHECK((setting == CONTENT_SETTING_ALLOW) || - (setting == CONTENT_SETTING_BLOCK) || - (setting == CONTENT_SETTING_SESSION_ONLY)); - *allowed = setting != CONTENT_SETTING_BLOCK; + *allowed = cookie_settings_->IsSettingCookieAllowed(origin_url, + top_origin_url); // Record access to file system for potential display in UI. BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, @@ -488,10 +480,8 @@ void ChromeRenderMessageFilter::OnAllowIndexedDB(int render_view_id, const GURL& top_origin_url, const string16& name, bool* allowed) { - ContentSetting setting = host_content_settings_map_->GetCookieContentSetting( - origin_url, top_origin_url, true); - *allowed = setting != CONTENT_SETTING_BLOCK; - + *allowed = cookie_settings_->IsSettingCookieAllowed(origin_url, + top_origin_url); BrowserThread::PostTask( BrowserThread::UI, FROM_HERE, NewRunnableFunction( diff --git a/chrome/browser/renderer_host/chrome_render_message_filter.h b/chrome/browser/renderer_host/chrome_render_message_filter.h index 2bb8949..322b290 100644 --- a/chrome/browser/renderer_host/chrome_render_message_filter.h +++ b/chrome/browser/renderer_host/chrome_render_message_filter.h @@ -16,6 +16,7 @@ struct ChromeViewHostMsg_GetPluginInfo_Status; class ContentSettingsPattern; +class CookieSettings; struct ExtensionHostMsg_Request_Params; class ExtensionInfoMap; class FilePath; @@ -165,6 +166,7 @@ class ChromeRenderMessageFilter : public BrowserMessageFilter { scoped_refptr<ExtensionInfoMap> extension_info_map_; // Used to look up permissions at database creation time. scoped_refptr<HostContentSettingsMap> host_content_settings_map_; + scoped_refptr<CookieSettings> cookie_settings_; const content::ResourceContext& resource_context_; diff --git a/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc b/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc index c84e0ab..8851a10 100644 --- a/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc +++ b/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc @@ -321,7 +321,7 @@ void ChromeResourceDispatcherHostDelegate::OnResponseStarted( resource_dispatcher_host_->InfoForRequest(request); filter->Send(new ChromeViewMsg_SetContentSettingsForLoadingURL( info->route_id(), request->url(), - map->GetContentSettings(request->url(), request->url()))); + map->GetContentSettings(request->url()))); // See if the response contains the X-Auto-Login header. If so, this was // a request for a login page, and the server is allowing the browser to diff --git a/chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.mm b/chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.mm index 358fbbf..a7e2d9d 100644 --- a/chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.mm +++ b/chrome/browser/ui/cocoa/content_settings/collected_cookies_mac.mm @@ -8,9 +8,10 @@ #import "base/mac/mac_util.h" #include "base/sys_string_conversions.h" -#include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/tab_specific_content_settings.h" #include "chrome/browser/infobars/infobar_tab_helper.h" +#include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/cocoa/constrained_window_mac.h" #import "chrome/browser/ui/cocoa/content_settings/cookie_details_view_controller.h" @@ -18,6 +19,7 @@ #include "chrome/browser/ui/collected_cookies_infobar_delegate.h" #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" #include "chrome/common/chrome_notification_types.h" +#include "chrome/common/pref_names.h" #include "content/browser/tab_contents/tab_contents.h" #include "content/public/browser/notification_details.h" #include "content/public/browser/notification_source.h" @@ -184,7 +186,7 @@ void CollectedCookiesMac::OnSheetDidEnd(NSWindow* sheet) { // Change the label of the blocked cookies part if necessary. Profile* profile = wrapper_->profile(); - if (profile->GetHostContentSettingsMap()->BlockThirdPartyCookies()) { + if (profile->GetPrefs()->GetBoolean(prefs::kBlockThirdPartyCookies)) { [blockedCookiesText_ setStringValue:l10n_util::GetNSString( IDS_COLLECTED_COOKIES_BLOCKED_THIRD_PARTY_BLOCKING_ENABLED)]; CGFloat textDeltaY = [GTMUILocalizerAndLayoutTweaker @@ -244,7 +246,7 @@ void CollectedCookiesMac::OnSheetDidEnd(NSWindow* sheet) { Profile* profile = wrapper_->profile(); CookieTreeOriginNode* origin_node = static_cast<CookieTreeOriginNode*>(cookie); - origin_node->CreateContentException(profile->GetHostContentSettingsMap(), + origin_node->CreateContentException(CookieSettings::GetForProfile(profile), setting); if (!lastDomain.empty()) multipleDomainsChanged = YES; diff --git a/chrome/browser/ui/content_settings/content_setting_bubble_model.cc b/chrome/browser/ui/content_settings/content_setting_bubble_model.cc index 65833ac..344e201 100644 --- a/chrome/browser/ui/content_settings/content_setting_bubble_model.cc +++ b/chrome/browser/ui/content_settings/content_setting_bubble_model.cc @@ -5,6 +5,7 @@ #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" #include "base/utf_string_conversions.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/host_content_settings_map.h" #include "chrome/browser/content_settings/tab_specific_content_settings.h" #include "chrome/browser/favicon/favicon_tab_helper.h" @@ -253,14 +254,15 @@ class ContentSettingSingleRadioGroup radio_group.radio_items.push_back(radio_allow_label); radio_group.radio_items.push_back(radio_block_label); HostContentSettingsMap* map = profile()->GetHostContentSettingsMap(); - ContentSetting mostRestrictiveSetting; + CookieSettings* cookie_settings = CookieSettings::GetForProfile(profile()); + ContentSetting most_restrictive_setting; if (resources.empty()) { - mostRestrictiveSetting = + most_restrictive_setting = content_type() == CONTENT_SETTINGS_TYPE_COOKIES ? - map->GetCookieContentSetting(url, url, true) : + cookie_settings->GetCookieSetting(url, url, true) : map->GetContentSetting(url, url, content_type(), std::string()); } else { - mostRestrictiveSetting = CONTENT_SETTING_ALLOW; + most_restrictive_setting = CONTENT_SETTING_ALLOW; for (std::set<std::string>::const_iterator it = resources.begin(); it != resources.end(); ++it) { ContentSetting setting = map->GetContentSetting(url, @@ -268,19 +270,19 @@ class ContentSettingSingleRadioGroup content_type(), *it); if (setting == CONTENT_SETTING_BLOCK) { - mostRestrictiveSetting = CONTENT_SETTING_BLOCK; + most_restrictive_setting = CONTENT_SETTING_BLOCK; break; } if (setting == CONTENT_SETTING_ASK) - mostRestrictiveSetting = CONTENT_SETTING_ASK; + most_restrictive_setting = CONTENT_SETTING_ASK; } } - if (mostRestrictiveSetting == CONTENT_SETTING_ALLOW) { + if (most_restrictive_setting == CONTENT_SETTING_ALLOW) { radio_group.default_item = 0; // |block_setting_| is already set to |CONTENT_SETTING_BLOCK|. } else { radio_group.default_item = 1; - block_setting_ = mostRestrictiveSetting; + block_setting_ = most_restrictive_setting; } selected_item_ = radio_group.default_item; set_radio_group(radio_group); diff --git a/chrome/browser/ui/gtk/collected_cookies_gtk.cc b/chrome/browser/ui/gtk/collected_cookies_gtk.cc index 21425f6..a2fa8c7 100644 --- a/chrome/browser/ui/gtk/collected_cookies_gtk.cc +++ b/chrome/browser/ui/gtk/collected_cookies_gtk.cc @@ -6,10 +6,11 @@ #include <string> -#include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/tab_specific_content_settings.h" #include "chrome/browser/cookies_tree_model.h" #include "chrome/browser/infobars/infobar_tab_helper.h" +#include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/collected_cookies_infobar_delegate.h" #include "chrome/browser/ui/gtk/constrained_window_gtk.h" @@ -17,6 +18,7 @@ #include "chrome/browser/ui/gtk/gtk_util.h" #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" #include "chrome/common/chrome_notification_types.h" +#include "chrome/common/pref_names.h" #include "content/public/browser/notification_source.h" #include "grit/generated_resources.h" #include "ui/base/gtk/gtk_hig_constants.h" @@ -238,14 +240,13 @@ GtkWidget* CollectedCookiesGtk::CreateAllowedPane() { } GtkWidget* CollectedCookiesGtk::CreateBlockedPane() { - HostContentSettingsMap* host_content_settings_map = - wrapper_->profile()->GetHostContentSettingsMap(); + PrefService* prefs = wrapper_->profile()->GetPrefs(); GtkWidget* cookie_list_vbox = gtk_vbox_new(FALSE, ui::kControlSpacing); GtkWidget* label = gtk_label_new( l10n_util::GetStringUTF8( - host_content_settings_map->BlockThirdPartyCookies() ? + prefs->GetBoolean(prefs::kBlockThirdPartyCookies) ? IDS_COLLECTED_COOKIES_BLOCKED_THIRD_PARTY_BLOCKING_ENABLED : IDS_COLLECTED_COOKIES_BLOCKED_COOKIES_LABEL).c_str()); gtk_widget_set_size_request(label, kTreeViewWidth, -1); @@ -460,7 +461,7 @@ void CollectedCookiesGtk::AddExceptions(GtkTreeSelection* selection, last_domain_name = origin_node->GetTitle(); Profile* profile = wrapper_->profile(); origin_node->CreateContentException( - profile->GetHostContentSettingsMap(), setting); + CookieSettings::GetForProfile(profile), setting); } } g_list_foreach(paths, reinterpret_cast<GFunc>(gtk_tree_path_free), NULL); diff --git a/chrome/browser/ui/views/collected_cookies_win.cc b/chrome/browser/ui/views/collected_cookies_win.cc index 602eafc..66ac121 100644 --- a/chrome/browser/ui/views/collected_cookies_win.cc +++ b/chrome/browser/ui/views/collected_cookies_win.cc @@ -4,10 +4,11 @@ #include "chrome/browser/ui/views/collected_cookies_win.h" -#include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/tab_specific_content_settings.h" #include "chrome/browser/cookies_tree_model.h" #include "chrome/browser/infobars/infobar_tab_helper.h" +#include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/collected_cookies_infobar_delegate.h" #include "chrome/browser/ui/constrained_window.h" @@ -15,6 +16,7 @@ #include "chrome/browser/ui/views/constrained_window_views.h" #include "chrome/browser/ui/views/cookie_info_view.h" #include "chrome/common/chrome_notification_types.h" +#include "chrome/common/pref_names.h" #include "content/public/browser/notification_details.h" #include "content/public/browser/notification_source.h" #include "grit/generated_resources.h" @@ -287,13 +289,12 @@ views::View* CollectedCookiesWin::CreateAllowedPane() { views::View* CollectedCookiesWin::CreateBlockedPane() { TabSpecificContentSettings* content_settings = wrapper_->content_settings(); - HostContentSettingsMap* host_content_settings_map = - wrapper_->profile()->GetHostContentSettingsMap(); + PrefService* prefs = wrapper_->profile()->GetPrefs(); // Create the controls that go into the pane. blocked_label_ = new views::Label( l10n_util::GetStringUTF16( - host_content_settings_map->BlockThirdPartyCookies() ? + prefs->GetBoolean(prefs::kBlockThirdPartyCookies) ? IDS_COLLECTED_COOKIES_BLOCKED_THIRD_PARTY_BLOCKING_ENABLED : IDS_COLLECTED_COOKIES_BLOCKED_COOKIES_LABEL)); blocked_label_->SetMultiLine(true); @@ -481,7 +482,7 @@ void CollectedCookiesWin::AddContentException(views::TreeView* tree_view, CookieTreeOriginNode* origin_node = static_cast<CookieTreeOriginNode*>(tree_view->GetSelectedNode()); Profile* profile = wrapper_->profile(); - origin_node->CreateContentException(profile->GetHostContentSettingsMap(), + origin_node->CreateContentException(CookieSettings::GetForProfile(profile), setting); infobar_->UpdateVisibility(true, setting, origin_node->GetTitle()); gfx::Rect bounds = GetWidget()->GetClientAreaScreenBounds(); diff --git a/chrome/browser/ui/webui/collected_cookies_ui_delegate.cc b/chrome/browser/ui/webui/collected_cookies_ui_delegate.cc index 36636fb..b7a21a0 100644 --- a/chrome/browser/ui/webui/collected_cookies_ui_delegate.cc +++ b/chrome/browser/ui/webui/collected_cookies_ui_delegate.cc @@ -9,9 +9,10 @@ #include "base/message_loop.h" #include "base/string_util.h" #include "base/values.h" -#include "chrome/browser/content_settings/host_content_settings_map.h" +#include "chrome/browser/content_settings/cookie_settings.h" #include "chrome/browser/content_settings/tab_specific_content_settings.h" #include "chrome/browser/cookies_tree_model.h" +#include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" #include "chrome/browser/ui/webui/chrome_url_data_manager.h" @@ -19,6 +20,7 @@ #include "chrome/browser/ui/webui/cookies_tree_model_util.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/jstemplate_builder.h" +#include "chrome/common/pref_names.h" #include "chrome/common/url_constants.h" #include "content/browser/tab_contents/tab_contents.h" #include "content/public/browser/notification_source.h" @@ -156,8 +158,7 @@ CollectedCookiesUIDelegate::CollectedCookiesUIDelegate( : wrapper_(wrapper), closed_(false) { TabSpecificContentSettings* content_settings = wrapper->content_settings(); - HostContentSettingsMap* host_content_settings_map = - wrapper->profile()->GetHostContentSettingsMap(); + PrefService* prefs = wrapper->profile()->GetPrefs(); registrar_.Add(this, chrome::NOTIFICATION_COLLECTED_COOKIES_SHOWN, content::Source<TabSpecificContentSettings>(content_settings)); @@ -168,7 +169,7 @@ CollectedCookiesUIDelegate::CollectedCookiesUIDelegate( content_settings->GetBlockedCookiesTreeModel()); CollectedCookiesSource* source = new CollectedCookiesSource( - host_content_settings_map->BlockThirdPartyCookies()); + prefs->GetBoolean(prefs::kBlockThirdPartyCookies)); wrapper->profile()->GetChromeURLDataManager()->AddDataSource(source); } @@ -243,7 +244,7 @@ void CollectedCookiesUIDelegate::AddContentException( CookieTreeOriginNode* origin_node, ContentSetting setting) { if (origin_node->CanCreateContentException()) { Profile* profile = wrapper_->profile(); - origin_node->CreateContentException(profile->GetHostContentSettingsMap(), + origin_node->CreateContentException(CookieSettings::GetForProfile(profile), setting); SetInfobarLabel(GetInfobarLabel(setting, origin_node->GetTitle())); diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index eb71fb4..a3f81a2 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -870,6 +870,8 @@ 'browser/content_settings/content_settings_rule.h', 'browser/content_settings/content_settings_utils.cc', 'browser/content_settings/content_settings_utils.h', + 'browser/content_settings/cookie_settings.cc', + 'browser/content_settings/cookie_settings.h', 'browser/content_settings/host_content_settings_map.cc', 'browser/content_settings/host_content_settings_map.h', 'browser/content_settings/tab_specific_content_settings.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 96e09bc..d7c209c 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1265,6 +1265,7 @@ 'browser/content_settings/content_settings_provider_unittest.cc', 'browser/content_settings/content_settings_rule_unittest.cc', 'browser/content_settings/content_settings_utils_unittest.cc', + 'browser/content_settings/cookie_settings_unittest.cc', 'browser/content_settings/host_content_settings_map_unittest.cc', 'browser/content_settings/mock_settings_observer.cc', 'browser/content_settings/mock_settings_observer.h', |