diff options
author | pfeldman@chromium.org <pfeldman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-03 17:08:19 +0000 |
---|---|---|
committer | pfeldman@chromium.org <pfeldman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-02-03 17:08:19 +0000 |
commit | 79a087a3332e71e674d954a868244f671ab22a22 (patch) | |
tree | fc785ea040d1d60e76b86f8df55da58d5116459b | |
parent | 84352455e708fc771809f34476d4db9ca79e4bc9 (diff) | |
download | chromium_src-79a087a3332e71e674d954a868244f671ab22a22.zip chromium_src-79a087a3332e71e674d954a868244f671ab22a22.tar.gz chromium_src-79a087a3332e71e674d954a868244f671ab22a22.tar.bz2 |
DevTools: CookieMonster::GetRawCookies should return keys as well as cookies.
Review URL: http://codereview.chromium.org/565035
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37978 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/automation/url_request_automation_job.cc | 8 | ||||
-rw-r--r-- | chrome/browser/renderer_host/resource_message_filter.cc | 26 | ||||
-rw-r--r-- | net/base/cookie_monster.cc | 57 | ||||
-rw-r--r-- | net/base/cookie_monster.h | 13 | ||||
-rw-r--r-- | net/base/cookie_monster_unittest.cc | 57 |
5 files changed, 115 insertions, 46 deletions
diff --git a/chrome/browser/automation/url_request_automation_job.cc b/chrome/browser/automation/url_request_automation_job.cc index df32cc2..319b976 100644 --- a/chrome/browser/automation/url_request_automation_job.cc +++ b/chrome/browser/automation/url_request_automation_job.cc @@ -330,11 +330,11 @@ void URLRequestAutomationJob::OnRequestStarted(int tab, int id, url_for_cookies, request_->first_party_for_cookies()))) { StringTokenizer cookie_parser(response.persistent_cookies, ";"); - std::vector<net::CookieMonster::CanonicalCookie> existing_cookies; + net::CookieMonster::CookieList existing_cookies; net::CookieMonster* monster = ctx->cookie_store()->GetCookieMonster(); DCHECK(monster); if (monster) { - monster->GetRawCookies(url_for_cookies, &existing_cookies); + existing_cookies = monster->GetRawCookies(url_for_cookies); } while (cookie_parser.GetNext()) { @@ -350,9 +350,9 @@ void URLRequestAutomationJob::OnRequestStarted(int tab, int id, // Ignore duplicate cookies, i.e. cookies passed in from the host // browser which also exist in the response header. net::CookieMonster::ParsedCookie parsed_cookie(cookie_string); - std::vector<net::CookieMonster::CanonicalCookie>::const_iterator i; + net::CookieMonster::CookieList::const_iterator i; for (i = existing_cookies.begin(); i != existing_cookies.end(); ++i) { - if ((*i).Name() == parsed_cookie.Name()) + if (i->second.Name() == parsed_cookie.Name()) break; } diff --git a/chrome/browser/renderer_host/resource_message_filter.cc b/chrome/browser/renderer_host/resource_message_filter.cc index 7979b3f..1bcb7a7 100644 --- a/chrome/browser/renderer_host/resource_message_filter.cc +++ b/chrome/browser/renderer_host/resource_message_filter.cc @@ -519,21 +519,21 @@ void ResourceMessageFilter::OnGetRawCookies( if (!context->cookie_policy()->CanGetCookies(url, first_party_for_cookies)) return; - typedef std::vector<net::CookieMonster::CanonicalCookie> CanonicalCookieList; - CanonicalCookieList cookies; - cookie_monster->GetRawCookies(url, &cookies); - for (CanonicalCookieList::iterator it = cookies.begin(); - it != cookies.end(); ++it) { + typedef net::CookieMonster::CookieList CookieList; + CookieList cookieList = cookie_monster->GetRawCookies(url); + for (CookieList::iterator it = cookieList.begin(); + it != cookieList.end(); ++it) { + net::CookieMonster::CanonicalCookie& cookie = it->second; raw_cookies->push_back( webkit_glue::WebCookie( - it->Name(), - it->Value(), - url.host(), - it->Path(), - it->ExpiryDate().ToDoubleT() * 1000, - it->IsHttpOnly(), - it->IsSecure(), - !it->IsPersistent())); + cookie.Name(), + cookie.Value(), + it->first, + cookie.Path(), + cookie.ExpiryDate().ToDoubleT() * 1000, + cookie.IsHttpOnly(), + cookie.IsSecure(), + !cookie.IsPersistent())); } } diff --git a/net/base/cookie_monster.cc b/net/base/cookie_monster.cc index 73e03ff..64b0a7e 100644 --- a/net/base/cookie_monster.cc +++ b/net/base/cookie_monster.cc @@ -782,24 +782,6 @@ std::string CookieMonster::GetCookiesWithOptions(const GURL& url, return cookie_line; } -void CookieMonster::GetRawCookies(const GURL& url, - std::vector<CanonicalCookie>* raw_cookies) { - raw_cookies->clear(); - if (!HasCookieableScheme(url)) - return; - - CookieOptions options; - options.set_include_httponly(); - // Get the cookies for this host and its domain(s). - std::vector<CanonicalCookie*> cookies; - FindCookiesForHostAndDomain(url, options, &cookies); - std::sort(cookies.begin(), cookies.end(), CookieSorter); - - for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); - it != cookies.end(); ++it) - raw_cookies->push_back(*(*it)); -} - void CookieMonster::DeleteCookie(const GURL& url, const std::string& cookie_name) { if (!HasCookieableScheme(url)) @@ -852,6 +834,34 @@ CookieMonster::CookieList CookieMonster::GetAllCookies() { return cookie_list; } +CookieMonster::CookieList CookieMonster::GetRawCookies(const GURL& url) { + CookieList cookie_list; + if (!HasCookieableScheme(url)) + return cookie_list; + + bool secure = url.SchemeIsSecure(); + + // Query for the full host, For example: 'a.c.blah.com'. + std::string key(url.host()); + FindRawCookies(key, secure, &cookie_list); + + // See if we can search for domain cookies, i.e. if the host has a TLD + 1. + const std::string domain(GetEffectiveDomain(url.scheme(), key)); + if (domain.empty()) + return cookie_list; + + // Use same logic as in FindCookiesForHostAndDomain. + DCHECK_LE(domain.length(), key.length()); + DCHECK_EQ(0, key.compare(key.length() - domain.length(), domain.length(), + domain)); + for (key = "." + key; key.length() > domain.length(); ) { + FindRawCookies(key, secure, &cookie_list); + const size_t next_dot = key.find('.', 1); // Skip over leading dot. + key.erase(0, next_dot); + } + return cookie_list; +} + void CookieMonster::FindCookiesForHostAndDomain( const GURL& url, const CookieOptions& options, @@ -923,6 +933,17 @@ void CookieMonster::FindCookiesForKey( } } +void CookieMonster::FindRawCookies(const std::string& key, + bool include_secure, + CookieList* list) { + for (CookieMapItPair its = cookies_.equal_range(key); + its.first != its.second; ++its.first) { + CanonicalCookie* cc = its.first->second; + if (include_secure || !cc->IsSecure()) + list->push_back(CookieListPair(key, *cc)); + } +} + CookieMonster::ParsedCookie::ParsedCookie(const std::string& cookie_line) : is_valid_(false), diff --git a/net/base/cookie_monster.h b/net/base/cookie_monster.h index 60dcea1..6976f7f 100644 --- a/net/base/cookie_monster.h +++ b/net/base/cookie_monster.h @@ -93,18 +93,21 @@ class CookieMonster : public CookieStore { virtual std::string GetCookies(const GURL& url); virtual std::string GetCookiesWithOptions(const GURL& url, const CookieOptions& options); - virtual void GetRawCookies(const GURL& url, - std::vector<CanonicalCookie>* raw_cookies); virtual void DeleteCookie(const GURL& url, const std::string& cookie_name); virtual CookieMonster* GetCookieMonster() { return this; } - // Returns all the cookies, for use in management UI, etc. This does not mark + // Returns all the cookies, for use in management UI, etc. This does not mark // the cookies as having been accessed. CookieList GetAllCookies(); + // Returns all the cookies, for use in management UI, etc. Filters results + // using given url scheme and host / domain. This does not mark the cookies + // as having been accessed. + CookieList GetRawCookies(const GURL& url); + // Delete all of the cookies. int DeleteAll(bool sync_to_store); // Delete all of the cookies that have a creation_date greater than or equal @@ -164,6 +167,10 @@ class CookieMonster : public CookieStore { const base::Time& current, std::vector<CanonicalCookie*>* cookies); + void FindRawCookies(const std::string& key, + bool include_secure, + CookieList* list); + // Delete any cookies that are equivalent to |ecc| (same path, key, etc). // If |skip_httponly| is true, httponly cookies will not be deleted. The // return value with be true if |skip_httponly| skipped an httponly cookie. diff --git a/net/base/cookie_monster_unittest.cc b/net/base/cookie_monster_unittest.cc index c6e3f24..ba645ee 100644 --- a/net/base/cookie_monster_unittest.cc +++ b/net/base/cookie_monster_unittest.cc @@ -962,21 +962,62 @@ TEST(CookieMonsterTest, SetCookieableSchemes) { } TEST(CookieMonsterTest, GetRawCookies) { - scoped_refptr<net::CookieMonster> cm(new net::CookieMonster); + GURL url_google(kUrlGoogle); + GURL url_google_secure(kUrlGoogleSecure); + + scoped_refptr<net::CookieMonster> cm( + new net::CookieMonster(kLastAccessThresholdMilliseconds)); + // Create an httponly cookie. net::CookieOptions options; options.set_include_httponly(); - // Create a httponly cookie. EXPECT_TRUE(cm->SetCookieWithOptions(url_google, "A=B; httponly", options)); + EXPECT_TRUE(cm->SetCookieWithOptions(url_google, + "C=D; domain=.google.izzle", + options)); + EXPECT_TRUE(cm->SetCookieWithOptions(url_google_secure, + "E=F; domain=.google.izzle; secure", + options)); + const Time last_access_date(GetFirstCookieAccessDate(cm)); + + PlatformThread::Sleep(kLastAccessThresholdMilliseconds + 20); + + // Check raw cookies. + net::CookieMonster::CookieList raw_cookies = cm->GetRawCookies(url_google); + net::CookieMonster::CookieList::iterator it = raw_cookies.begin(); + + ASSERT_TRUE(it != raw_cookies.end()); + EXPECT_EQ("www.google.izzle", it->first); + EXPECT_EQ("A", it->second.Name()); + + ASSERT_TRUE(++it != raw_cookies.end()); + EXPECT_EQ(".google.izzle", it->first); + EXPECT_EQ("C", it->second.Name()); + + ASSERT_TRUE(++it == raw_cookies.end()); + + // Test secure cookies. + raw_cookies = cm->GetRawCookies(url_google_secure); + it = raw_cookies.begin(); + + ASSERT_TRUE(it != raw_cookies.end()); + EXPECT_EQ("www.google.izzle", it->first); + EXPECT_EQ("A", it->second.Name()); + + ASSERT_TRUE(++it != raw_cookies.end()); + EXPECT_EQ(".google.izzle", it->first); + EXPECT_EQ("C", it->second.Name()); + + ASSERT_TRUE(++it != raw_cookies.end()); + EXPECT_EQ(".google.izzle", it->first); + EXPECT_EQ("E", it->second.Name()); + + ASSERT_TRUE(++it == raw_cookies.end()); - // Get raw cookies. - std::vector<net::CookieMonster::CanonicalCookie> raw_cookies; - cm->GetRawCookies(url_google, &raw_cookies); - EXPECT_TRUE(raw_cookies.begin() != raw_cookies.end()); - net::CookieMonster::CanonicalCookie cookie = *raw_cookies.begin(); - EXPECT_EQ("A", cookie.Name()); + // Reading after a short wait should not update the access date. + EXPECT_TRUE (last_access_date == GetFirstCookieAccessDate(cm)); } TEST(CookieMonsterTest, DeleteCookieByName) { |