diff options
author | mpcomplete@google.com <mpcomplete@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-14 18:46:21 +0000 |
---|---|---|
committer | mpcomplete@google.com <mpcomplete@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-14 18:46:21 +0000 |
commit | 47accfd64443e175562abc65769457ffef5d3322 (patch) | |
tree | 27d646d32565934449f56cb77982f7283851aca8 /net/base | |
parent | 351e70b561dfbf40350cf7607e6af0403e43e64c (diff) | |
download | chromium_src-47accfd64443e175562abc65769457ffef5d3322.zip chromium_src-47accfd64443e175562abc65769457ffef5d3322.tar.gz chromium_src-47accfd64443e175562abc65769457ffef5d3322.tar.bz2 |
Add a separate cookie store that's used for extensions.
Modify CookieMonster to support overriding the "cookieable schemes".
Review URL: http://codereview.chromium.org/115204
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@16083 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/cookie_monster.cc | 31 | ||||
-rw-r--r-- | net/base/cookie_monster.h | 13 | ||||
-rw-r--r-- | net/base/cookie_monster_unittest.cc | 17 |
3 files changed, 49 insertions, 12 deletions
diff --git a/net/base/cookie_monster.cc b/net/base/cookie_monster.cc index 4b5d8e0..470bfc2 100644 --- a/net/base/cookie_monster.cc +++ b/net/base/cookie_monster.cc @@ -94,6 +94,7 @@ CookieMonster::CookieMonster() store_(NULL), last_access_threshold_( TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) { + SetDefaultCookieableSchemes(); } CookieMonster::CookieMonster(PersistentCookieStore* store) @@ -101,6 +102,7 @@ CookieMonster::CookieMonster(PersistentCookieStore* store) store_(store), last_access_threshold_( TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) { + SetDefaultCookieableSchemes(); } CookieMonster::~CookieMonster() { @@ -124,6 +126,13 @@ void CookieMonster::InitStore() { } } +void CookieMonster::SetDefaultCookieableSchemes() { + // Note: file must be the last scheme. + static const char* kDefaultCookieableSchemes[] = { "http", "https", "file" }; + int num_schemes = enable_file_scheme_ ? 3 : 2; + SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes); +} + // The system resolution is not high enough, so we can have multiple // set cookies that result in the same system time. When this happens, we // increment by one Time unit. Let's hope computers don't get too fast. @@ -358,18 +367,11 @@ static Time CanonExpiration(const CookieMonster::ParsedCookie& pc, return Time(); } -static bool HasCookieableScheme(const GURL& url) { - static const char* kCookieableSchemes[] = { "http", "https", "file" }; - static const int kCookieableSchemesLen = arraysize(kCookieableSchemes); - static const int kCookieableSchemesFileIndex = 2; - +bool CookieMonster::HasCookieableScheme(const GURL& url) { // Make sure the request is on a cookie-able url scheme. - for (int i = 0; i < kCookieableSchemesLen; ++i) { + for (size_t i = 0; i < cookieable_schemes_.size(); ++i) { // We matched a scheme. - if (url.SchemeIs(kCookieableSchemes[i])) { - // This is file:// scheme - if (i == kCookieableSchemesFileIndex) - return CookieMonster::enable_file_scheme_; + if (url.SchemeIs(cookieable_schemes_[i].c_str())) { // We've matched a supported scheme. return true; } @@ -380,6 +382,13 @@ static bool HasCookieableScheme(const GURL& url) { return false; } +void CookieMonster::SetCookieableSchemes( + const char* schemes[], size_t num_schemes) { + cookieable_schemes_.clear(); + cookieable_schemes_.insert(cookieable_schemes_.end(), + schemes, schemes + num_schemes); +} + bool CookieMonster::SetCookie(const GURL& url, const std::string& cookie_line) { CookieOptions options; @@ -419,7 +428,6 @@ bool CookieMonster::SetCookieWithCreationTimeWithOptions( DCHECK(!creation_time.is_null()); if (!HasCookieableScheme(url)) { - DLOG(WARNING) << "Unsupported cookie scheme: " << url.scheme(); return false; } @@ -733,7 +741,6 @@ std::string CookieMonster::GetCookies(const GURL& url) { std::string CookieMonster::GetCookiesWithOptions(const GURL& url, const CookieOptions& options) { if (!HasCookieableScheme(url)) { - DLOG(WARNING) << "Unsupported cookie scheme: " << url.scheme(); return std::string(); } diff --git a/net/base/cookie_monster.h b/net/base/cookie_monster.h index 0bece15..24db893 100644 --- a/net/base/cookie_monster.h +++ b/net/base/cookie_monster.h @@ -76,6 +76,7 @@ class CookieMonster { store_(NULL), last_access_threshold_( base::TimeDelta::FromSeconds(last_access_threshold_seconds)) { + SetDefaultCookieableSchemes(); } #endif @@ -133,9 +134,15 @@ class CookieMonster { const CanonicalCookie& cookie, bool sync_to_store); + // Override the default list of schemes that are allowed to be set in + // this cookie store. Calling his overrides the value of + // "enable_file_scheme_". + void SetCookieableSchemes(const char* schemes[], size_t num_schemes); + // There are some unknowns about how to correctly handle file:// cookies, // and our implementation for this is not robust enough. This allows you // to enable support, but it should only be used for testing. Bug 1157243. + // Must be called before creating a CookieMonster instance. static void EnableFileScheme(); static bool enable_file_scheme_; @@ -156,6 +163,8 @@ class CookieMonster { // Should only be called by InitIfNecessary(). void InitStore(); + void SetDefaultCookieableSchemes(); + void FindCookiesForHostAndDomain(const GURL& url, const CookieOptions& options, std::vector<CanonicalCookie*>* cookies); @@ -210,6 +219,8 @@ class CookieMonster { const CookieMapItPair& itpair, std::vector<CookieMap::iterator>* cookie_its); + bool HasCookieableScheme(const GURL& url); + CookieMap cookies_; // Indicates whether the cookie store has been initialized. This happens @@ -227,6 +238,8 @@ class CookieMonster { // update it again. const base::TimeDelta last_access_threshold_; + std::vector<std::string> cookieable_schemes_; + // Lock for thread-safety Lock lock_; diff --git a/net/base/cookie_monster_unittest.cc b/net/base/cookie_monster_unittest.cc index db76cdb..5ca3e76 100644 --- a/net/base/cookie_monster_unittest.cc +++ b/net/base/cookie_monster_unittest.cc @@ -928,4 +928,21 @@ TEST(CookieMonsterTest, TestDeleteSingleCookie) { EXPECT_EQ("A=B; E=F", cm.GetCookies(url_google)); } +TEST(CookieMonsterTest, SetCookieableSchemes) { + net::CookieMonster cm; + net::CookieMonster cm_foo; + + // Only cm_foo should allow foo:// cookies. + const char* kSchemes[] = {"foo"}; + cm_foo.SetCookieableSchemes(kSchemes, 1); + + GURL foo_url("foo://host/path"); + GURL http_url("http://host/path"); + + EXPECT_TRUE(cm.SetCookie(http_url, "x=1")); + EXPECT_FALSE(cm.SetCookie(foo_url, "x=1")); + EXPECT_TRUE(cm_foo.SetCookie(foo_url, "x=1")); + EXPECT_FALSE(cm_foo.SetCookie(http_url, "x=1")); +} + // TODO test overwrite cookie |