diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-29 21:00:34 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-29 21:00:34 +0000 |
commit | 15e84d9dad946d522ca6b5acf7db721a4ea76946 (patch) | |
tree | af079dd93c04f3736f10ac77da8a86ce099cc4d2 | |
parent | 1cc71de3863b49ba125576bdff84d739356070fb (diff) | |
download | chromium_src-15e84d9dad946d522ca6b5acf7db721a4ea76946.zip chromium_src-15e84d9dad946d522ca6b5acf7db721a4ea76946.tar.gz chromium_src-15e84d9dad946d522ca6b5acf7db721a4ea76946.tar.bz2 |
Implement extended cookie controls.
BUG=32782
TEST=none
Review URL: http://codereview.chromium.org/554119
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37535 0039d316-1c4b-4281-b951-d872f2087c98
24 files changed, 362 insertions, 53 deletions
diff --git a/chrome/browser/browser.cc b/chrome/browser/browser.cc index 7ebead2..a4e3a88 100644 --- a/chrome/browser/browser.cc +++ b/chrome/browser/browser.cc @@ -1359,8 +1359,6 @@ void Browser::RegisterUserPrefs(PrefService* prefs) { prefs->RegisterStringPref(prefs::kHomePage, ASCIIToWide(chrome::kChromeUINewTabURL)); prefs->RegisterBooleanPref(prefs::kHomePageIsNewTabPage, true); - prefs->RegisterIntegerPref(prefs::kCookieBehavior, - net::CookiePolicy::ALLOW_ALL_COOKIES); prefs->RegisterBooleanPref(prefs::kShowHomeButton, false); #if defined(OS_MACOSX) // This really belongs in platform code, but there's no good place to diff --git a/chrome/browser/browser_prefs.cc b/chrome/browser/browser_prefs.cc index 85f35a0..23e7549 100644 --- a/chrome/browser/browser_prefs.cc +++ b/chrome/browser/browser_prefs.cc @@ -21,6 +21,7 @@ #include "chrome/browser/host_zoom_map.h" #include "chrome/browser/intranet_redirect_detector.h" #include "chrome/browser/metrics/metrics_service.h" +#include "chrome/browser/net/chrome_cookie_policy.h" #include "chrome/browser/net/dns_global.h" #include "chrome/browser/page_info_model.h" #include "chrome/browser/password_manager/password_manager.h" @@ -98,6 +99,7 @@ void RegisterUserPrefs(PrefService* user_prefs) { HostZoomMap::RegisterUserPrefs(user_prefs); DevToolsManager::RegisterUserPrefs(user_prefs); Blacklist::RegisterUserPrefs(user_prefs); + ChromeCookiePolicy::RegisterUserPrefs(user_prefs); #if defined(TOOLKIT_VIEWS) // TODO(port): whittle this down as we port. BrowserActionsContainer::RegisterUserPrefs(user_prefs); #if defined(OS_WIN) diff --git a/chrome/browser/cocoa/preferences_window_controller.h b/chrome/browser/cocoa/preferences_window_controller.h index e29047e..9c59343 100644 --- a/chrome/browser/cocoa/preferences_window_controller.h +++ b/chrome/browser/cocoa/preferences_window_controller.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -38,6 +38,7 @@ class ProfileSyncService; // weak ref - Also obtained from profile_ for convenience. May be NULL. ProfileSyncService* syncService_; scoped_ptr<PrefObserverBridge> observer_; // Watches for pref changes. + bool pref_changing_; // Flag to ignore pref updates caused by ourselves. IBOutlet NSToolbar* toolbar_; diff --git a/chrome/browser/cocoa/preferences_window_controller.mm b/chrome/browser/cocoa/preferences_window_controller.mm index 19ad0f2..6a378d2 100644 --- a/chrome/browser/cocoa/preferences_window_controller.mm +++ b/chrome/browser/cocoa/preferences_window_controller.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -7,6 +7,7 @@ #include <algorithm> #include "app/l10n_util.h" #include "app/l10n_util_mac.h" +#include "base/auto_reset.h" #include "base/logging.h" #include "base/mac_util.h" #include "base/string16.h" @@ -25,6 +26,7 @@ #include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/metrics/metrics_service.h" #include "chrome/browser/metrics/user_metrics.h" +#include "chrome/browser/net/chrome_cookie_policy.h" #include "chrome/browser/net/dns_global.h" #include "chrome/browser/net/url_fixer_upper.h" #include "chrome/browser/options_window.h" @@ -45,7 +47,6 @@ #include "grit/chromium_strings.h" #include "grit/generated_resources.h" #include "grit/locale_settings.h" -#include "net/base/cookie_policy.h" #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" #import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h" @@ -456,6 +457,7 @@ class PrefObserverBridge : public NotificationObserver, // This needs to be done before awakeFromNib: because the bindings set up // in the nib rely on it. [self registerPrefObservers]; + pref_changing_ = false; // Use one animation so we can stop it if the user clicks quickly, and // start the new animation. @@ -814,6 +816,7 @@ class PrefObserverBridge : public NotificationObserver, // Cocoa Bindings. // Handles prefs for the "Basics" panel. - (void)basicsPrefChanged:(std::wstring*)prefName { + AutoReset auto_reset(&pref_changing_, true); if (*prefName == prefs::kRestoreOnStartup) { const SessionStartupPref startupPref = SessionStartupPref::GetStartupPref(prefs_); @@ -1129,6 +1132,7 @@ const int kDisabledIndex = 1; // initializing, that's handled by Cocoa Bindings. // Handles prefs for the "Personal Stuff" panel. - (void)userDataPrefChanged:(std::wstring*)prefName { + AutoReset auto_reset(&pref_changing_, true); if (*prefName == prefs::kPasswordManagerEnabled) { [self setPasswordManagerEnabledIndex:askSavePasswords_.GetValue() ? kEnabledIndex : kDisabledIndex]; @@ -1281,6 +1285,7 @@ const int kDisabledIndex = 1; // initializing, that's handled by Cocoa Bindings. // Handles prefs for the "Under the hood" panel. - (void)underHoodPrefChanged:(std::wstring*)prefName { + AutoReset auto_reset(&pref_changing_, true); if (*prefName == prefs::kAlternateErrorPagesEnabled) { [self setShowAlternateErrorPages: alternateErrorPages_.GetValue() ? YES : NO]; @@ -1471,6 +1476,8 @@ const int kDisabledIndex = 1; // Sets the backend pref for whether or not to accept cookies based on |index|. - (void)setCookieBehavior:(NSInteger)index { + if (pref_changing_) + return; net::CookiePolicy::Type policy = net::CookiePolicy::ALLOW_ALL_COOKIES; if (net::CookiePolicy::ValidType(index)) policy = net::CookiePolicy::FromInt(index); @@ -1481,7 +1488,7 @@ const int kDisabledIndex = 1; }; DCHECK(policy >= 0 && (unsigned int)policy < arraysize(kUserMetrics)); [self recordUserAction:kUserMetrics[policy]]; - cookieBehavior_.SetValue(policy); + profile_->GetCookiePolicy()->set_type(policy); } - (NSURL*)defaultDownloadLocation { diff --git a/chrome/browser/gtk/options/advanced_contents_gtk.cc b/chrome/browser/gtk/options/advanced_contents_gtk.cc index d493a39..2f7ed5b 100644 --- a/chrome/browser/gtk/options/advanced_contents_gtk.cc +++ b/chrome/browser/gtk/options/advanced_contents_gtk.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -21,6 +21,7 @@ #include "chrome/browser/gtk/gtk_chrome_link_button.h" #include "chrome/browser/gtk/options/cookies_view.h" #include "chrome/browser/gtk/options/options_layout_gtk.h" +#include "chrome/browser/net/chrome_cookie_policy.h" #include "chrome/browser/net/dns_global.h" #include "chrome/browser/options_page_base.h" #include "chrome/browser/options_util.h" @@ -35,7 +36,6 @@ #include "grit/chromium_strings.h" #include "grit/generated_resources.h" #include "grit/locale_settings.h" -#include "net/base/cookie_policy.h" namespace { @@ -754,7 +754,7 @@ void PrivacySection::OnCookieBehaviorChanged(GtkComboBox* combo_box, } privacy_section->UserMetricsRecordAction( kUserMetrics[cookie_policy], privacy_section->profile()->GetPrefs()); - privacy_section->cookie_behavior_.SetValue(cookie_policy); + privacy_section->profile()->GetCookiePolicy()->set_type(cookie_policy); } // static diff --git a/chrome/browser/net/chrome_cookie_policy.cc b/chrome/browser/net/chrome_cookie_policy.cc new file mode 100644 index 0000000..fccb737 --- /dev/null +++ b/chrome/browser/net/chrome_cookie_policy.cc @@ -0,0 +1,142 @@ +// Copyright (c) 2010 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/net/chrome_cookie_policy.h" + +#include "base/utf_string_conversions.h" +#include "chrome/browser/profile.h" +#include "chrome/common/pref_service.h" +#include "chrome/common/pref_names.h" +#include "googleurl/src/gurl.h" +#include "net/base/registry_controlled_domain.h" + +ChromeCookiePolicy::ChromeCookiePolicy(Profile* profile) : profile_(profile) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + + const DictionaryValue* cookie_exceptions_dict = + profile_->GetPrefs()->GetDictionary(prefs::kCookieExceptions); + // Careful: The returned value could be NULL if the pref has never been set. + if (cookie_exceptions_dict != NULL) { + for (DictionaryValue::key_iterator i(cookie_exceptions_dict->begin_keys()); + i != cookie_exceptions_dict->end_keys(); ++i) { + std::wstring wide_domain(*i); + int content_settings = 0; + bool success = cookie_exceptions_dict->GetIntegerWithoutPathExpansion( + wide_domain, &content_settings); + DCHECK(success); + per_domain_policy_[WideToUTF8(wide_domain)] = + static_cast<ContentPermissionType>(content_settings); + } + } + net::CookiePolicy::set_type(net::CookiePolicy::FromInt( + profile_->GetPrefs()->GetInteger(prefs::kCookieBehavior))); +} + +// static +void ChromeCookiePolicy::RegisterUserPrefs(PrefService* prefs) { + prefs->RegisterDictionaryPref(prefs::kCookieExceptions); + prefs->RegisterIntegerPref(prefs::kCookieBehavior, + net::CookiePolicy::ALLOW_ALL_COOKIES); +} + +void ChromeCookiePolicy::ResetToDefaults() { + net::CookiePolicy::set_type(net::CookiePolicy::ALLOW_ALL_COOKIES); + per_domain_policy_.clear(); + profile_->GetPrefs()->ClearPref(prefs::kCookieBehavior); + profile_->GetPrefs()->ClearPref(prefs::kCookieExceptions); +} + +bool ChromeCookiePolicy::CanGetCookies(const GURL& url, + const GURL& first_party_for_cookies) { + AutoLock auto_lock(lock_); + ContentPermissionType permission = CheckPermissionForHost(url.host()); + switch (permission) { + case CONTENT_PERMISSION_TYPE_DEFAULT: + return net::CookiePolicy::CanGetCookies(url, first_party_for_cookies); + + case CONTENT_PERMISSION_TYPE_BLOCK: + return false; + + case CONTENT_PERMISSION_TYPE_ALLOW: + return true; + + case CONTENT_PERMISSION_TYPE_ASK: + // TODO(darin): ask the user. + + default: + NOTREACHED(); + } + return false; // To avoid compiler warnings. +} + +bool ChromeCookiePolicy::CanSetCookie(const GURL& url, + const GURL& first_party_for_cookies) { + AutoLock auto_lock(lock_); + ContentPermissionType permission = CheckPermissionForHost(url.host()); + switch (permission) { + case CONTENT_PERMISSION_TYPE_DEFAULT: + return net::CookiePolicy::CanSetCookie(url, first_party_for_cookies); + + case CONTENT_PERMISSION_TYPE_BLOCK: + return false; + + case CONTENT_PERMISSION_TYPE_ALLOW: + return true; + + case CONTENT_PERMISSION_TYPE_ASK: + // TODO(darin): ask the user. + + default: + NOTREACHED(); + } + return false; // To avoid compiler warnings. +} + +void ChromeCookiePolicy::SetPerDomainPermission(const std::string& domain, + ContentPermissionType type) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + if (domain.empty()) + return; + + { + AutoLock auto_lock(lock_); + if (type == CONTENT_PERMISSION_TYPE_DEFAULT) + per_domain_policy_.erase(domain); + else + per_domain_policy_[domain] = type; + } + + DictionaryValue* cookie_exceptions_dict = + profile_->GetPrefs()->GetMutableDictionary(prefs::kCookieExceptions); + std::wstring wide_domain(UTF8ToWide(domain)); + if (type == CONTENT_PERMISSION_TYPE_DEFAULT) { + cookie_exceptions_dict->RemoveWithoutPathExpansion(wide_domain, NULL); + } else { + cookie_exceptions_dict->SetWithoutPathExpansion(wide_domain, + Value::CreateIntegerValue(type)); + } +} + +void ChromeCookiePolicy::set_type(Type type) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + + AutoLock auto_lock(lock_); + net::CookiePolicy::set_type(type); + profile_->GetPrefs()->SetInteger(prefs::kCookieBehavior, type); +} + +ContentPermissionType ChromeCookiePolicy::CheckPermissionForHost( + const std::string& host) const { + std::string search_key(host); + while (!search_key.empty()) { + CookiePolicies::const_iterator i(per_domain_policy_.find(search_key)); + if (i != per_domain_policy_.end()) + return i->second; + size_t dot_pos = search_key.find_first_of('.'); + if (dot_pos == std::string::npos) + break; + search_key = search_key.substr(dot_pos+1); + } + return CONTENT_PERMISSION_TYPE_DEFAULT; +} diff --git a/chrome/browser/net/chrome_cookie_policy.h b/chrome/browser/net/chrome_cookie_policy.h new file mode 100644 index 0000000..88e698b --- /dev/null +++ b/chrome/browser/net/chrome_cookie_policy.h @@ -0,0 +1,81 @@ +// Copyright (c) 2010 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_NET_CHROME_COOKIE_POLICY_H_ +#define CHROME_BROWSER_NET_CHROME_COOKIE_POLICY_H_ + +#include <map> +#include <string> + +#include "base/basictypes.h" +#include "base/lock.h" +#include "chrome/common/content_permission_types.h" +#include "net/base/cookie_policy.h" + +class GURL; +class PrefService; +class Profile; + +// The ChromeCookiePolicy class implements per-domain cookie policies. +class ChromeCookiePolicy : public net::CookiePolicy { + public: + typedef std::map<std::string, ContentPermissionType> CookiePolicies; + + explicit ChromeCookiePolicy(Profile* profile); + + virtual ~ChromeCookiePolicy() {} + + static void RegisterUserPrefs(PrefService* prefs); + + void ResetToDefaults(); + + // Consult the user's cookie blocking preferences to determine whether the + // URL's cookies can be read. + virtual bool CanGetCookies(const GURL& url, + const GURL& first_party_for_cookies); + + // Consult the user's cookie blocking preferences to determine whether the + // URL's cookies can be set. + virtual bool CanSetCookie(const GURL& url, + const GURL& first_party_for_cookies); + + // Sets per domain policies. A policy of type DEFAULT will erase the entry. + // + // This should be called only on the UI thread. + void SetPerDomainPermission(const std::string& domain, + ContentPermissionType type); + + // Returns all per domain policies. + // + // This can be called on any thread. + CookiePolicies GetAllPerDomainPermissions() const { + return per_domain_policy_; + } + + // Sets the current policy to enforce. + // + // This should be called only on the UI thread. + void set_type(Type type); + + // This can be called on any thread. + Type type() const { + AutoLock auto_lock(lock_); + return net::CookiePolicy::type(); + } + + protected: + ContentPermissionType CheckPermissionForHost(const std::string& host) const; + + Profile* profile_; + + mutable Lock lock_; + + // Local copy of prefs. + CookiePolicies per_domain_policy_; + + private: + DISALLOW_COPY_AND_ASSIGN(ChromeCookiePolicy); +}; + +#endif // CHROME_BROWSER_NET_CHROME_COOKIE_POLICY_H_ diff --git a/chrome/browser/net/chrome_cookie_policy_unittest.cc b/chrome/browser/net/chrome_cookie_policy_unittest.cc new file mode 100644 index 0000000..5bec23a --- /dev/null +++ b/chrome/browser/net/chrome_cookie_policy_unittest.cc @@ -0,0 +1,49 @@ +// Copyright (c) 2010 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/net/chrome_cookie_policy.h" + +#include "chrome/test/testing_profile.h" +#include "testing/gtest/include/gtest/gtest.h" + + +namespace { + +class ChromeCookiePolicyTest : public testing::Test { + public: + ChromeCookiePolicyTest() + : ui_thread_(ChromeThread::UI, &message_loop_) {} + + protected: + MessageLoop message_loop_; + ChromeThread ui_thread_; +}; + +TEST_F(ChromeCookiePolicyTest, DefaultValues) { + TestingProfile profile; + ChromeCookiePolicy* cookie_policy = profile.GetCookiePolicy(); + + // Check setting of default permissions. + cookie_policy->set_type(net::CookiePolicy::ALLOW_ALL_COOKIES); + EXPECT_EQ(net::CookiePolicy::ALLOW_ALL_COOKIES, cookie_policy->type()); + + // Check per host permissions returned. + EXPECT_TRUE(cookie_policy->CanSetCookie(GURL("http://www.example.com"), + GURL("http://www.example.com"))); + cookie_policy->SetPerDomainPermission("example.com", + CONTENT_PERMISSION_TYPE_BLOCK); + EXPECT_FALSE(cookie_policy->CanSetCookie(GURL("http://www.example.com"), + GURL("http://www.example.com"))); + EXPECT_TRUE(cookie_policy->CanSetCookie(GURL("http://other.com"), + GURL("http://other.com"))); + + // Check returning settings for a given resource. + ChromeCookiePolicy::CookiePolicies policies; + policies = cookie_policy->GetAllPerDomainPermissions(); + EXPECT_EQ(1U, policies.size()); + EXPECT_EQ("example.com", policies.begin()->first); + EXPECT_EQ(CONTENT_PERMISSION_TYPE_BLOCK, policies.begin()->second); +} + +} // namespace diff --git a/chrome/browser/net/chrome_url_request_context.cc b/chrome/browser/net/chrome_url_request_context.cc index a50013d..88914dc 100644 --- a/chrome/browser/net/chrome_url_request_context.cc +++ b/chrome/browser/net/chrome_url_request_context.cc @@ -544,15 +544,6 @@ void ChromeURLRequestContextGetter::Observe( this, &ChromeURLRequestContextGetter::OnAcceptLanguageChange, accept_language)); - } else if (*pref_name_in == prefs::kCookieBehavior) { - net::CookiePolicy::Type policy_type = net::CookiePolicy::FromInt( - prefs_->GetInteger(prefs::kCookieBehavior)); - ChromeThread::PostTask( - ChromeThread::IO, FROM_HERE, - NewRunnableMethod( - this, - &ChromeURLRequestContextGetter::OnCookiePolicyChange, - policy_type)); } else if (*pref_name_in == prefs::kDefaultCharset) { std::string default_charset = WideToASCII(prefs->GetString(prefs::kDefaultCharset)); @@ -574,7 +565,6 @@ void ChromeURLRequestContextGetter::RegisterPrefsObserver(Profile* profile) { prefs_ = profile->GetPrefs(); prefs_->AddPrefObserver(prefs::kAcceptLanguages, this); - prefs_->AddPrefObserver(prefs::kCookieBehavior, this); prefs_->AddPrefObserver(prefs::kDefaultCharset, this); } @@ -596,11 +586,6 @@ void ChromeURLRequestContextGetter::OnAcceptLanguageChange( GetIOContext()->OnAcceptLanguageChange(accept_language); } -void ChromeURLRequestContextGetter::OnCookiePolicyChange( - net::CookiePolicy::Type type) { - GetIOContext()->OnCookiePolicyChange(type); -} - void ChromeURLRequestContextGetter::OnDefaultCharsetChange( const std::string& default_charset) { GetIOContext()->OnDefaultCharsetChange(default_charset); @@ -769,7 +754,7 @@ ChromeURLRequestContext::ChromeURLRequestContext( http_transaction_factory_ = other->http_transaction_factory_; ftp_transaction_factory_ = other->ftp_transaction_factory_; cookie_store_ = other->cookie_store_; - cookie_policy_.set_type(other->cookie_policy_.type()); + cookie_policy_ = other->cookie_policy_; transport_security_state_ = other->transport_security_state_; accept_language_ = other->accept_language_; accept_charset_ = other->accept_charset_; @@ -798,12 +783,6 @@ void ChromeURLRequestContext::OnAcceptLanguageChange( net::HttpUtil::GenerateAcceptLanguageHeader(accept_language); } -void ChromeURLRequestContext::OnCookiePolicyChange( - net::CookiePolicy::Type type) { - CheckCurrentlyOnIOThread(); - cookie_policy_.set_type(type); -} - void ChromeURLRequestContext::OnDefaultCharsetChange( const std::string& default_charset) { CheckCurrentlyOnIOThread(); @@ -855,8 +834,7 @@ ChromeURLRequestContextFactory::ChromeURLRequestContextFactory(Profile* profile) // net_util::GetSuggestedFilename is unlikely to be taken. referrer_charset_ = default_charset; - cookie_policy_type_ = net::CookiePolicy::FromInt( - prefs->GetInteger(prefs::kCookieBehavior)); + cookie_policy_ = profile->GetCookiePolicy(); host_content_settings_map_ = profile->GetHostContentSettingsMap(); @@ -903,7 +881,7 @@ void ChromeURLRequestContextFactory::ApplyProfileParametersToContext( context->set_accept_language(accept_language_); context->set_accept_charset(accept_charset_); context->set_referrer_charset(referrer_charset_); - context->set_cookie_policy_type(cookie_policy_type_); + context->set_cookie_policy(cookie_policy_); context->set_extension_info(extension_info_); context->set_user_script_dir_path(user_script_dir_path_); context->set_host_content_settings_map(host_content_settings_map_); diff --git a/chrome/browser/net/chrome_url_request_context.h b/chrome/browser/net/chrome_url_request_context.h index d1953ef..ae412ba 100644 --- a/chrome/browser/net/chrome_url_request_context.h +++ b/chrome/browser/net/chrome_url_request_context.h @@ -9,6 +9,7 @@ #include "base/linked_ptr.h" #include "chrome/browser/host_content_settings_map.h" #include "chrome/browser/host_zoom_map.h" +#include "chrome/browser/net/chrome_cookie_policy.h" #include "chrome/browser/net/url_request_context_getter.h" #include "chrome/common/appcache/chrome_appcache_service.h" #include "chrome/common/notification_registrar.h" @@ -136,8 +137,8 @@ class ChromeURLRequestContext : public URLRequestContext { void set_referrer_charset(const std::string& referrer_charset) { referrer_charset_ = referrer_charset; } - void set_cookie_policy_type(net::CookiePolicy::Type type) { - cookie_policy_.set_type(type); + void set_cookie_policy(ChromeCookiePolicy* policy) { + cookie_policy_ = policy; } void set_extension_info( const ChromeURLRequestContext::ExtensionInfoMap& info) { @@ -189,9 +190,6 @@ class ChromeURLRequestContext : public URLRequestContext { // Callback for when the accept language changes. void OnAcceptLanguageChange(const std::string& accept_language); - // Callback for when the cookie policy changes. - void OnCookiePolicyChange(net::CookiePolicy::Type type); - // Callback for when the default charset changes. void OnDefaultCharsetChange(const std::string& default_charset); @@ -314,7 +312,6 @@ class ChromeURLRequestContextGetter : public URLRequestContextGetter, // These methods simply forward to the corresponding method on // ChromeURLRequestContext. void OnAcceptLanguageChange(const std::string& accept_language); - void OnCookiePolicyChange(net::CookiePolicy::Type type); void OnDefaultCharsetChange(const std::string& default_charset); // Saves the cookie store to |result| and signals |completion|. @@ -368,7 +365,7 @@ class ChromeURLRequestContextFactory { std::string accept_language_; std::string accept_charset_; std::string referrer_charset_; - net::CookiePolicy::Type cookie_policy_type_; + ChromeCookiePolicy* cookie_policy_; ChromeURLRequestContext::ExtensionInfoMap extension_info_; // TODO(aa): I think this can go away now as we no longer support standalone // user scripts. diff --git a/chrome/browser/net/cookie_policy_browsertest.cc b/chrome/browser/net/cookie_policy_browsertest.cc index 7dbecf9..9f4b1f4 100644 --- a/chrome/browser/net/cookie_policy_browsertest.cc +++ b/chrome/browser/net/cookie_policy_browsertest.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "chrome/browser/browser.h" +#include "chrome/browser/net/chrome_cookie_policy.h" #include "chrome/browser/net/url_request_context_getter.h" #include "chrome/browser/profile.h" #include "chrome/common/pref_names.h" @@ -25,8 +26,8 @@ IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, AllowFirstPartyCookies) { ASSERT_TRUE(server != NULL); PrefService* prefs = browser()->profile()->GetPrefs(); - prefs->SetInteger(prefs::kCookieBehavior, - net::CookiePolicy::BLOCK_THIRD_PARTY_COOKIES); + browser()->profile()->GetCookiePolicy()->set_type( + net::CookiePolicy::BLOCK_THIRD_PARTY_COOKIES); net::CookiePolicy::Type policy_type = net::CookiePolicy::FromInt( prefs->GetInteger(prefs::kCookieBehavior)); ASSERT_EQ(net::CookiePolicy::BLOCK_THIRD_PARTY_COOKIES, policy_type); @@ -54,8 +55,8 @@ IN_PROC_BROWSER_TEST_F(CookiePolicyBrowserTest, ASSERT_TRUE(server != NULL); PrefService* prefs = browser()->profile()->GetPrefs(); - prefs->SetInteger(prefs::kCookieBehavior, - net::CookiePolicy::BLOCK_THIRD_PARTY_COOKIES); + browser()->profile()->GetCookiePolicy()->set_type( + net::CookiePolicy::BLOCK_THIRD_PARTY_COOKIES); net::CookiePolicy::Type policy_type = net::CookiePolicy::FromInt( prefs->GetInteger(prefs::kCookieBehavior)); ASSERT_EQ(net::CookiePolicy::BLOCK_THIRD_PARTY_COOKIES, policy_type); diff --git a/chrome/browser/options_util.cc b/chrome/browser/options_util.cc index 6562c3d..abb8957 100644 --- a/chrome/browser/options_util.cc +++ b/chrome/browser/options_util.cc @@ -9,6 +9,7 @@ #include "chrome/browser/download/download_manager.h" #include "chrome/browser/host_content_settings_map.h" #include "chrome/browser/metrics/metrics_service.h" +#include "chrome/browser/net/chrome_cookie_policy.h" #include "chrome/common/pref_names.h" #include "chrome/common/pref_service.h" #include "chrome/installer/util/google_update_settings.h" @@ -22,7 +23,6 @@ void OptionsUtil::ResetToDefaults(Profile* profile) { const wchar_t* kUserPrefs[] = { prefs::kAcceptLanguages, prefs::kAlternateErrorPagesEnabled, - prefs::kCookieBehavior, prefs::kDefaultCharset, prefs::kDnsPrefetchingEnabled, #if defined(OS_LINUX) @@ -64,6 +64,7 @@ void OptionsUtil::ResetToDefaults(Profile* profile) { prefs::kWebKitSansSerifFontFamily, prefs::kWebKitSerifFontFamily, }; + profile->GetCookiePolicy()->ResetToDefaults(); profile->GetDownloadManager()->ResetAutoOpenFiles(); profile->GetHostContentSettingsMap()->ResetToDefaults(); for (size_t i = 0; i < arraysize(kUserPrefs); ++i) diff --git a/chrome/browser/profile.cc b/chrome/browser/profile.cc index c197a78..acfeb61 100644 --- a/chrome/browser/profile.cc +++ b/chrome/browser/profile.cc @@ -32,6 +32,7 @@ #include "chrome/browser/host_content_settings_map.h" #include "chrome/browser/host_zoom_map.h" #include "chrome/browser/in_process_webkit/webkit_context.h" +#include "chrome/browser/net/chrome_cookie_policy.h" #include "chrome/browser/net/chrome_url_request_context.h" #include "chrome/browser/net/ssl_config_service_manager.h" #include "chrome/browser/notifications/desktop_notification_service.h" @@ -431,6 +432,10 @@ class OffTheRecordProfileImpl : public Profile, return NULL; } + virtual ChromeCookiePolicy* GetCookiePolicy() { + return GetOriginalProfile()->GetCookiePolicy(); + } + virtual void ShutdownSessionService() { // We don't allow a session service, nothing to do. } @@ -1168,6 +1173,13 @@ SessionService* ProfileImpl::GetSessionService() { return session_service_.get(); } +ChromeCookiePolicy* ProfileImpl::GetCookiePolicy() { + if (!cookie_policy_.get()) + cookie_policy_.reset(new ChromeCookiePolicy(this)); + return cookie_policy_.get(); +} + + void ProfileImpl::ShutdownSessionService() { if (shutdown_session_service_) return; diff --git a/chrome/browser/profile.h b/chrome/browser/profile.h index 6f97fec..3548a34 100644 --- a/chrome/browser/profile.h +++ b/chrome/browser/profile.h @@ -33,6 +33,7 @@ class DatabaseTracker; class Blacklist; class BookmarkModel; class BrowserThemeProvider; +class ChromeCookiePolicy; class ChromeURLRequestContextGetter; class DesktopNotificationService; class DownloadManager; @@ -308,6 +309,9 @@ class Profile { // should always check the return value for NULL. virtual SessionService* GetSessionService() = 0; + // Returns the cookie policy for this profile. + virtual ChromeCookiePolicy* GetCookiePolicy() = 0; + // If this profile has a session service, it is shut down. To properly record // the current state this forces creation of the session service, then shuts // it down. @@ -445,6 +449,7 @@ class ProfileImpl : public Profile, virtual HostZoomMap* GetHostZoomMap(); virtual Blacklist* GetPrivacyBlacklist(); virtual SessionService* GetSessionService(); + virtual ChromeCookiePolicy* GetCookiePolicy(); virtual void ShutdownSessionService(); virtual bool HasSessionService() const; virtual bool DidLastSessionExitCleanly(); @@ -529,6 +534,7 @@ class ProfileImpl : public Profile, scoped_ptr<HostContentSettingsMap> host_content_settings_map_; scoped_refptr<HostZoomMap> host_zoom_map_; scoped_ptr<Blacklist> privacy_blacklist_; + scoped_ptr<ChromeCookiePolicy> cookie_policy_; scoped_refptr<DownloadManager> download_manager_; scoped_refptr<HistoryService> history_service_; scoped_refptr<FaviconService> favicon_service_; diff --git a/chrome/browser/sync/glue/http_bridge.cc b/chrome/browser/sync/glue/http_bridge.cc index 95d95ac..511bbce2 100644 --- a/chrome/browser/sync/glue/http_bridge.cc +++ b/chrome/browser/sync/glue/http_bridge.cc @@ -67,6 +67,8 @@ HttpBridge::RequestContext::RequestContext(URLRequestContext* baseline_context) // Create empty, in-memory cookie store. cookie_store_ = new net::CookieMonster(); + cookie_policy_ = new net::CookiePolicy(); + // We don't use a cache for bridged loads, but we do want to share proxy info. host_resolver_ = baseline_context->host_resolver(); proxy_service_ = baseline_context->proxy_service(); @@ -97,6 +99,7 @@ HttpBridge::RequestContext::RequestContext(URLRequestContext* baseline_context) } HttpBridge::RequestContext::~RequestContext() { + delete cookie_policy_; delete http_transaction_factory_; } diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index ed01725..951bce7 100755 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1217,6 +1217,8 @@ 'browser/modal_html_dialog_delegate.h', 'browser/net/browser_url_util.cc', 'browser/net/browser_url_util.h', + 'browser/net/chrome_cookie_policy.cc', + 'browser/net/chrome_cookie_policy.h', 'browser/net/chrome_url_request_context.cc', 'browser/net/chrome_url_request_context.h', 'browser/net/url_request_context_getter.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 3611dae3..cf5b784 100755 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -739,6 +739,7 @@ 'browser/metrics/metrics_log_unittest.cc', 'browser/metrics/metrics_response_unittest.cc', 'browser/metrics/metrics_service_unittest.cc', + 'browser/net/chrome_cookie_policy_unittest.cc', 'browser/net/chrome_url_request_context_unittest.cc', 'browser/net/dns_host_info_unittest.cc', 'browser/net/dns_master_unittest.cc', diff --git a/chrome/common/pref_names.cc b/chrome/common/pref_names.cc index ad07566..664f5bd 100644 --- a/chrome/common/pref_names.cc +++ b/chrome/common/pref_names.cc @@ -120,6 +120,9 @@ const wchar_t kSearchSuggestEnabled[] = L"search.suggest_enabled"; // 2 - block all cookies const wchar_t kCookieBehavior[] = L"security.cookie_behavior"; +// Dictionary of exceptions to the default cookie behavior. +const wchar_t kCookieExceptions[] = L"security.cookie_exceptions"; + // Boolean that is true if mixed content should be filtered. // TODO(jcampan): http://b/1084034: at some point this will become an enum // (int): don't filter, filter everything, filter images only. diff --git a/chrome/common/pref_names.h b/chrome/common/pref_names.h index 2afb1e5..30cf464 100644 --- a/chrome/common/pref_names.h +++ b/chrome/common/pref_names.h @@ -49,6 +49,7 @@ extern const wchar_t kFormAutofillEnabled[]; extern const wchar_t kSafeBrowsingEnabled[]; extern const wchar_t kSearchSuggestEnabled[]; extern const wchar_t kCookieBehavior[]; +extern const wchar_t kCookieExceptions[]; extern const wchar_t kMixedContentFiltering[]; extern const wchar_t kDefaultSearchProviderSearchURL[]; extern const wchar_t kDefaultSearchProviderSuggestURL[]; diff --git a/chrome/test/testing_profile.h b/chrome/test/testing_profile.h index 86aa2dd..1875010 100644 --- a/chrome/test/testing_profile.h +++ b/chrome/test/testing_profile.h @@ -15,6 +15,7 @@ #include "chrome/browser/host_content_settings_map.h" #include "chrome/browser/history/history.h" #include "chrome/browser/in_process_webkit/webkit_context.h" +#include "chrome/browser/net/chrome_cookie_policy.h" #include "chrome/browser/net/url_request_context_getter.h" #include "chrome/browser/profile.h" #include "chrome/browser/search_engines/template_url_model.h" @@ -188,6 +189,11 @@ class TestingProfile : public Profile { session_service_ = session_service; } virtual SessionService* GetSessionService() { return session_service_.get(); } + virtual ChromeCookiePolicy* GetCookiePolicy() { + if (!cookie_policy_.get()) + cookie_policy_.reset(new ChromeCookiePolicy(this)); + return cookie_policy_.get(); + } virtual void ShutdownSessionService() {} virtual bool HasSessionService() const { return (session_service_.get() != NULL); @@ -301,6 +307,8 @@ class TestingProfile : public Profile { scoped_refptr<WebKitContext> webkit_context_; scoped_ptr<HostContentSettingsMap> host_content_settings_map_; + + scoped_ptr<ChromeCookiePolicy> cookie_policy_; }; // A profile that derives from another profile. This does not actually diff --git a/net/base/cookie_policy.h b/net/base/cookie_policy.h index 8efe998..bf647fc 100644 --- a/net/base/cookie_policy.h +++ b/net/base/cookie_policy.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -14,13 +14,17 @@ namespace net { // The CookiePolicy class implements third-party cookie blocking. class CookiePolicy { public: + virtual ~CookiePolicy() {} + // Consult the user's third-party cookie blocking preferences to determine // whether the URL's cookies can be read. - bool CanGetCookies(const GURL& url, const GURL& first_party_for_cookies); + virtual bool CanGetCookies(const GURL& url, + const GURL& first_party_for_cookies); // Consult the user's third-party cookie blocking preferences to determine // whether the URL's cookies can be set. - bool CanSetCookie(const GURL& url, const GURL& first_party_for_cookies); + virtual bool CanSetCookie(const GURL& url, + const GURL& first_party_for_cookies); enum Type { ALLOW_ALL_COOKIES = 0, // Do not perform any cookie blocking. @@ -46,9 +50,10 @@ class CookiePolicy { CookiePolicy(); - private: + protected: Type type_; + private: DISALLOW_COPY_AND_ASSIGN(CookiePolicy); }; diff --git a/net/url_request/url_request_context.h b/net/url_request/url_request_context.h index b0204ce..978960e 100644 --- a/net/url_request/url_request_context.h +++ b/net/url_request/url_request_context.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -35,6 +35,7 @@ class URLRequestContext : URLRequestContext() : http_transaction_factory_(NULL), ftp_transaction_factory_(NULL), + cookie_policy_(NULL), cookie_store_(NULL), transport_security_state_(NULL) { } @@ -67,7 +68,12 @@ class URLRequestContext : net::CookieStore* cookie_store() { return cookie_store_.get(); } // Gets the cookie policy for this context. - net::CookiePolicy* cookie_policy() { return &cookie_policy_; } + net::CookiePolicy* cookie_policy() const { + return cookie_policy_; + } + void set_cookie_policy(net::CookiePolicy* cookie_policy) { + cookie_policy_ = cookie_policy; + } net::TransportSecurityState* transport_security_state() { return transport_security_state_; } @@ -131,8 +137,8 @@ class URLRequestContext : scoped_refptr<net::SSLConfigService> ssl_config_service_; net::HttpTransactionFactory* http_transaction_factory_; net::FtpTransactionFactory* ftp_transaction_factory_; + net::CookiePolicy* cookie_policy_; scoped_refptr<net::CookieStore> cookie_store_; - net::CookiePolicy cookie_policy_; scoped_refptr<net::TransportSecurityState> transport_security_state_; net::FtpAuthCache ftp_auth_cache_; std::string accept_language_; diff --git a/net/url_request/url_request_unittest.h b/net/url_request/url_request_unittest.h index abd94aa..3f5a55a 100644 --- a/net/url_request/url_request_unittest.h +++ b/net/url_request/url_request_unittest.h @@ -65,6 +65,7 @@ class TestURLRequestContext : public URLRequestContext { virtual ~TestURLRequestContext() { delete ftp_transaction_factory_; delete http_transaction_factory_; + delete cookie_policy_; } private: @@ -78,6 +79,7 @@ class TestURLRequestContext : public URLRequestContext { disk_cache::CreateInMemoryCacheBackend(0)); // In-memory cookie store. cookie_store_ = new net::CookieMonster(); + cookie_policy_ = new net::CookiePolicy(); accept_language_ = "en-us,fr"; accept_charset_ = "iso-8859-1,*,utf-8"; } diff --git a/webkit/tools/test_shell/test_shell_request_context.cc b/webkit/tools/test_shell/test_shell_request_context.cc index 0357e21..ef23908 100644 --- a/webkit/tools/test_shell/test_shell_request_context.cc +++ b/webkit/tools/test_shell/test_shell_request_context.cc @@ -8,6 +8,7 @@ #include "base/file_path.h" #include "net/base/cookie_monster.h" +#include "net/base/cookie_policy.h" #include "net/base/host_resolver.h" #include "net/base/ssl_config_service.h" #include "net/ftp/ftp_network_layer.h" @@ -32,6 +33,7 @@ void TestShellRequestContext::Init( net::HttpCache::Mode cache_mode, bool no_proxy) { cookie_store_ = new net::CookieMonster(); + cookie_policy_ = new net::CookiePolicy(); // hard-code A-L and A-C for test shells accept_language_ = "en-us,en"; @@ -75,6 +77,7 @@ void TestShellRequestContext::Init( TestShellRequestContext::~TestShellRequestContext() { delete ftp_transaction_factory_; delete http_transaction_factory_; + delete cookie_policy_; } const std::string& TestShellRequestContext::GetUserAgent( |