diff options
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/cookie_policy.h | 47 | ||||
-rw-r--r-- | net/base/static_cookie_policy.cc | 46 | ||||
-rw-r--r-- | net/base/static_cookie_policy.h | 57 | ||||
-rw-r--r-- | net/base/static_cookie_policy_unittest.cc | 87 |
4 files changed, 199 insertions, 38 deletions
diff --git a/net/base/cookie_policy.h b/net/base/cookie_policy.h index 8efe998..91bbfb9 100644 --- a/net/base/cookie_policy.h +++ b/net/base/cookie_policy.h @@ -1,55 +1,26 @@ -// 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. #ifndef NET_BASE_COOKIE_POLICY_H_ #define NET_BASE_COOKIE_POLICY_H_ -#include "base/basictypes.h" - class GURL; namespace net { -// The CookiePolicy class implements third-party cookie blocking. class CookiePolicy { public: - // 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); - - // 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); - - enum Type { - ALLOW_ALL_COOKIES = 0, // Do not perform any cookie blocking. - BLOCK_THIRD_PARTY_COOKIES, // Prevent third-party cookies from being set. - BLOCK_ALL_COOKIES // Disable cookies. - }; - - static bool ValidType(int32 type) { - return type >= ALLOW_ALL_COOKIES && type <= BLOCK_ALL_COOKIES; - } - - static Type FromInt(int32 type) { - return static_cast<Type>(type); - } - - // Sets the current policy to enforce. This should be called when the user's - // preferences change. - void set_type(Type type) { type_ = type; } - - Type type() const { - return type_; - } - - CookiePolicy(); + // Determine if the URL's cookies may be read. + virtual bool CanGetCookies(const GURL& url, + const GURL& first_party_for_cookies) = 0; - private: - Type type_; + // Determine if the URL's cookies may be written. + virtual bool CanSetCookie(const GURL& url, + const GURL& first_party_for_cookies) = 0; - DISALLOW_COPY_AND_ASSIGN(CookiePolicy); + protected: + virtual ~CookiePolicy() {} }; } // namespace net diff --git a/net/base/static_cookie_policy.cc b/net/base/static_cookie_policy.cc new file mode 100644 index 0000000..25d58bc --- /dev/null +++ b/net/base/static_cookie_policy.cc @@ -0,0 +1,46 @@ +// 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 "net/base/static_cookie_policy.h" + +#include "base/logging.h" +#include "googleurl/src/gurl.h" +#include "net/base/registry_controlled_domain.h" + +namespace net { + +bool StaticCookiePolicy::CanGetCookies(const GURL& url, + const GURL& first_party_for_cookies) { + switch (type_) { + case StaticCookiePolicy::ALLOW_ALL_COOKIES: + return true; + case StaticCookiePolicy::BLOCK_THIRD_PARTY_COOKIES: + return true; + case StaticCookiePolicy::BLOCK_ALL_COOKIES: + return false; + default: + NOTREACHED(); + return false; + } +} + +bool StaticCookiePolicy::CanSetCookie(const GURL& url, + const GURL& first_party_for_cookies) { + switch (type_) { + case StaticCookiePolicy::ALLOW_ALL_COOKIES: + return true; + case StaticCookiePolicy::BLOCK_THIRD_PARTY_COOKIES: + if (first_party_for_cookies.is_empty()) + return true; // Empty first-party URL indicates a first-party request. + return net::RegistryControlledDomainService::SameDomainOrHost( + url, first_party_for_cookies); + case StaticCookiePolicy::BLOCK_ALL_COOKIES: + return false; + default: + NOTREACHED(); + return false; + } +} + +} // namespace net diff --git a/net/base/static_cookie_policy.h b/net/base/static_cookie_policy.h new file mode 100644 index 0000000..a16fd0c --- /dev/null +++ b/net/base/static_cookie_policy.h @@ -0,0 +1,57 @@ +// 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 NET_BASE_STATIC_COOKIE_POLICY_H_ +#define NET_BASE_STATIC_COOKIE_POLICY_H_ + +#include "base/basictypes.h" +#include "net/base/cookie_policy.h" + +class GURL; + +namespace net { + +// The StaticCookiePolicy class implements a static cookie policy that supports +// three modes: allow all, deny all, or block third-party cookies. +class StaticCookiePolicy : public CookiePolicy { + public: + // 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); + + // 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); + + enum Type { + ALLOW_ALL_COOKIES = 0, // Do not perform any cookie blocking. + BLOCK_THIRD_PARTY_COOKIES, // Prevent third-party cookies from being set. + BLOCK_ALL_COOKIES // Disable cookies. + }; + + // Sets the current policy to enforce. This should be called when the user's + // preferences change. + void set_type(Type type) { type_ = type; } + + Type type() const { + return type_; + } + + StaticCookiePolicy() + : type_(StaticCookiePolicy::ALLOW_ALL_COOKIES) { + } + + explicit StaticCookiePolicy(Type type) + : type_(type) { + } + + private: + Type type_; + + DISALLOW_COPY_AND_ASSIGN(StaticCookiePolicy); +}; + +} // namespace net + +#endif // NET_BASE_STATIC_COOKIE_POLICY_H_ diff --git a/net/base/static_cookie_policy_unittest.cc b/net/base/static_cookie_policy_unittest.cc new file mode 100644 index 0000000..35af0fc --- /dev/null +++ b/net/base/static_cookie_policy_unittest.cc @@ -0,0 +1,87 @@ +// 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 "net/base/static_cookie_policy.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "googleurl/src/gurl.h" + +class StaticCookiePolicyTest : public testing::Test { + public: + StaticCookiePolicyTest() + : url_google_("http://www.google.izzle"), + url_google_secure_("https://www.google.izzle"), + url_google_mail_("http://mail.google.izzle"), + url_google_analytics_("http://www.googleanalytics.izzle") { } + protected: + GURL url_google_; + GURL url_google_secure_; + GURL url_google_mail_; + GURL url_google_analytics_; +}; + +TEST_F(StaticCookiePolicyTest, DefaultPolicyTest) { + net::StaticCookiePolicy cp; + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_secure_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_mail_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_analytics_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, GURL())); + + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_secure_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_mail_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_analytics_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, GURL())); +} + +TEST_F(StaticCookiePolicyTest, AllowAllCookiesTest) { + net::StaticCookiePolicy cp; + cp.set_type(net::StaticCookiePolicy::ALLOW_ALL_COOKIES); + + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_secure_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_mail_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_analytics_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, GURL())); + + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_secure_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_mail_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_analytics_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, GURL())); +} + +TEST_F(StaticCookiePolicyTest, BlockThirdPartyCookiesTest) { + net::StaticCookiePolicy cp; + cp.set_type(net::StaticCookiePolicy::BLOCK_THIRD_PARTY_COOKIES); + + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_secure_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_mail_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, url_google_analytics_)); + EXPECT_TRUE(cp.CanGetCookies(url_google_, GURL())); + + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_secure_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, url_google_mail_)); + EXPECT_FALSE(cp.CanSetCookie(url_google_, url_google_analytics_)); + EXPECT_TRUE(cp.CanSetCookie(url_google_, GURL())); +} + +TEST_F(StaticCookiePolicyTest, BlockAllCookiesTest) { + net::StaticCookiePolicy cp; + cp.set_type(net::StaticCookiePolicy::BLOCK_ALL_COOKIES); + + EXPECT_FALSE(cp.CanGetCookies(url_google_, url_google_)); + EXPECT_FALSE(cp.CanGetCookies(url_google_, url_google_secure_)); + EXPECT_FALSE(cp.CanGetCookies(url_google_, url_google_mail_)); + EXPECT_FALSE(cp.CanGetCookies(url_google_, url_google_analytics_)); + EXPECT_FALSE(cp.CanGetCookies(url_google_, GURL())); + + EXPECT_FALSE(cp.CanSetCookie(url_google_, url_google_)); + EXPECT_FALSE(cp.CanSetCookie(url_google_, url_google_secure_)); + EXPECT_FALSE(cp.CanSetCookie(url_google_, url_google_mail_)); + EXPECT_FALSE(cp.CanSetCookie(url_google_, url_google_analytics_)); + EXPECT_FALSE(cp.CanSetCookie(url_google_, GURL())); +} |