Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Portions of this code based on Mozilla: | 5 // Portions of this code based on Mozilla: |
| 6 // (netwerk/cookie/src/nsCookieService.cpp) | 6 // (netwerk/cookie/src/nsCookieService.cpp) |
| 7 /* ***** BEGIN LICENSE BLOCK ***** | 7 /* ***** BEGIN LICENSE BLOCK ***** |
| 8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | 8 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 9 * | 9 * |
| 10 * The contents of this file are subject to the Mozilla Public License Version | 10 * The contents of this file are subject to the Mozilla Public License Version |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 // static | 87 // static |
| 88 void CookieMonster::EnableFileScheme() { | 88 void CookieMonster::EnableFileScheme() { |
| 89 enable_file_scheme_ = true; | 89 enable_file_scheme_ = true; |
| 90 } | 90 } |
| 91 | 91 |
| 92 CookieMonster::CookieMonster() | 92 CookieMonster::CookieMonster() |
| 93 : initialized_(false), | 93 : initialized_(false), |
| 94 store_(NULL), | 94 store_(NULL), |
| 95 last_access_threshold_( | 95 last_access_threshold_( |
| 96 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) { | 96 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) { |
| 97 SetDefaultCookieableSchemes(); | |
| 97 } | 98 } |
| 98 | 99 |
| 99 CookieMonster::CookieMonster(PersistentCookieStore* store) | 100 CookieMonster::CookieMonster(PersistentCookieStore* store) |
| 100 : initialized_(false), | 101 : initialized_(false), |
| 101 store_(store), | 102 store_(store), |
| 102 last_access_threshold_( | 103 last_access_threshold_( |
| 103 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) { | 104 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)) { |
| 105 SetDefaultCookieableSchemes(); | |
| 104 } | 106 } |
| 105 | 107 |
| 106 CookieMonster::~CookieMonster() { | 108 CookieMonster::~CookieMonster() { |
| 107 DeleteAll(false); | 109 DeleteAll(false); |
| 108 } | 110 } |
| 109 | 111 |
| 110 void CookieMonster::InitStore() { | 112 void CookieMonster::InitStore() { |
| 111 DCHECK(store_) << "Store must exist to initialize"; | 113 DCHECK(store_) << "Store must exist to initialize"; |
| 112 | 114 |
| 113 // Initialize the store and sync in any saved persistent cookies. We don't | 115 // Initialize the store and sync in any saved persistent cookies. We don't |
| 114 // care if it's expired, insert it so it can be garbage collected, removed, | 116 // care if it's expired, insert it so it can be garbage collected, removed, |
| 115 // and sync'd. | 117 // and sync'd. |
| 116 std::vector<KeyedCanonicalCookie> cookies; | 118 std::vector<KeyedCanonicalCookie> cookies; |
| 117 // Reserve space for the maximum amount of cookies a database should have. | 119 // Reserve space for the maximum amount of cookies a database should have. |
| 118 // This prevents multiple vector growth / copies as we append cookies. | 120 // This prevents multiple vector growth / copies as we append cookies. |
| 119 cookies.reserve(kNumCookiesTotal); | 121 cookies.reserve(kNumCookiesTotal); |
| 120 store_->Load(&cookies); | 122 store_->Load(&cookies); |
| 121 for (std::vector<KeyedCanonicalCookie>::const_iterator it = cookies.begin(); | 123 for (std::vector<KeyedCanonicalCookie>::const_iterator it = cookies.begin(); |
| 122 it != cookies.end(); ++it) { | 124 it != cookies.end(); ++it) { |
| 123 InternalInsertCookie(it->first, it->second, false); | 125 InternalInsertCookie(it->first, it->second, false); |
| 124 } | 126 } |
| 125 } | 127 } |
| 126 | 128 |
| 129 void CookieMonster::SetDefaultCookieableSchemes() { | |
| 130 // Note: file must be the last scheme. | |
| 131 static const char* kDefaultCookieableSchemes[] = { "http", "https", "file" }; | |
| 132 int num_schemes = enable_file_scheme_ ? 3 : 2; | |
| 133 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes); | |
|
Dean McNamee
2009/05/14 07:34:38
This is so much simpler, thanks.
| |
| 134 } | |
| 135 | |
| 127 // The system resolution is not high enough, so we can have multiple | 136 // The system resolution is not high enough, so we can have multiple |
| 128 // set cookies that result in the same system time. When this happens, we | 137 // set cookies that result in the same system time. When this happens, we |
| 129 // increment by one Time unit. Let's hope computers don't get too fast. | 138 // increment by one Time unit. Let's hope computers don't get too fast. |
| 130 Time CookieMonster::CurrentTime() { | 139 Time CookieMonster::CurrentTime() { |
| 131 return std::max(Time::Now(), | 140 return std::max(Time::Now(), |
| 132 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1)); | 141 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1)); |
| 133 } | 142 } |
| 134 | 143 |
| 135 // Parse a cookie expiration time. We try to be lenient, but we need to | 144 // Parse a cookie expiration time. We try to be lenient, but we need to |
| 136 // assume some order to distinguish the fields. The basic rules: | 145 // assume some order to distinguish the fields. The basic rules: |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 351 } | 360 } |
| 352 | 361 |
| 353 // Try the Expires attribute. | 362 // Try the Expires attribute. |
| 354 if (pc.HasExpires()) | 363 if (pc.HasExpires()) |
| 355 return CookieMonster::ParseCookieTime(pc.Expires()); | 364 return CookieMonster::ParseCookieTime(pc.Expires()); |
| 356 | 365 |
| 357 // Invalid or no expiration, persistent cookie. | 366 // Invalid or no expiration, persistent cookie. |
| 358 return Time(); | 367 return Time(); |
| 359 } | 368 } |
| 360 | 369 |
| 361 static bool HasCookieableScheme(const GURL& url) { | 370 bool CookieMonster::HasCookieableScheme(const GURL& url) { |
| 362 static const char* kCookieableSchemes[] = { "http", "https", "file" }; | |
| 363 static const int kCookieableSchemesLen = arraysize(kCookieableSchemes); | |
| 364 static const int kCookieableSchemesFileIndex = 2; | |
| 365 | |
| 366 // Make sure the request is on a cookie-able url scheme. | 371 // Make sure the request is on a cookie-able url scheme. |
| 367 for (int i = 0; i < kCookieableSchemesLen; ++i) { | 372 for (size_t i = 0; i < cookieable_schemes_.size(); ++i) { |
| 368 // We matched a scheme. | 373 // We matched a scheme. |
| 369 if (url.SchemeIs(kCookieableSchemes[i])) { | 374 if (url.SchemeIs(cookieable_schemes_[i].c_str())) { |
| 370 // This is file:// scheme | |
| 371 if (i == kCookieableSchemesFileIndex) | |
| 372 return CookieMonster::enable_file_scheme_; | |
| 373 // We've matched a supported scheme. | 375 // We've matched a supported scheme. |
| 374 return true; | 376 return true; |
| 375 } | 377 } |
| 376 } | 378 } |
| 377 | 379 |
| 378 // The scheme didn't match any in our whitelist. | 380 // The scheme didn't match any in our whitelist. |
| 379 COOKIE_DLOG(WARNING) << "Unsupported cookie scheme: " << url.scheme(); | 381 COOKIE_DLOG(WARNING) << "Unsupported cookie scheme: " << url.scheme(); |
| 380 return false; | 382 return false; |
| 381 } | 383 } |
| 382 | 384 |
| 385 void CookieMonster::SetCookieableSchemes( | |
| 386 const char* schemes[], size_t num_schemes) { | |
| 387 cookieable_schemes_.clear(); | |
| 388 cookieable_schemes_.insert(cookieable_schemes_.end(), | |
| 389 schemes, schemes + num_schemes); | |
| 390 } | |
| 391 | |
| 383 bool CookieMonster::SetCookie(const GURL& url, | 392 bool CookieMonster::SetCookie(const GURL& url, |
| 384 const std::string& cookie_line) { | 393 const std::string& cookie_line) { |
| 385 CookieOptions options; | 394 CookieOptions options; |
| 386 return SetCookieWithOptions(url, cookie_line, options); | 395 return SetCookieWithOptions(url, cookie_line, options); |
| 387 } | 396 } |
| 388 | 397 |
| 389 bool CookieMonster::SetCookieWithOptions(const GURL& url, | 398 bool CookieMonster::SetCookieWithOptions(const GURL& url, |
| 390 const std::string& cookie_line, | 399 const std::string& cookie_line, |
| 391 const CookieOptions& options) { | 400 const CookieOptions& options) { |
| 392 Time creation_date; | 401 Time creation_date; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 412 } | 421 } |
| 413 | 422 |
| 414 bool CookieMonster::SetCookieWithCreationTimeWithOptions( | 423 bool CookieMonster::SetCookieWithCreationTimeWithOptions( |
| 415 const GURL& url, | 424 const GURL& url, |
| 416 const std::string& cookie_line, | 425 const std::string& cookie_line, |
| 417 const Time& creation_time, | 426 const Time& creation_time, |
| 418 const CookieOptions& options) { | 427 const CookieOptions& options) { |
| 419 DCHECK(!creation_time.is_null()); | 428 DCHECK(!creation_time.is_null()); |
| 420 | 429 |
| 421 if (!HasCookieableScheme(url)) { | 430 if (!HasCookieableScheme(url)) { |
| 422 DLOG(WARNING) << "Unsupported cookie scheme: " << url.scheme(); | |
| 423 return false; | 431 return false; |
| 424 } | 432 } |
| 425 | 433 |
| 426 AutoLock autolock(lock_); | 434 AutoLock autolock(lock_); |
| 427 InitIfNecessary(); | 435 InitIfNecessary(); |
| 428 | 436 |
| 429 COOKIE_DLOG(INFO) << "SetCookie() line: " << cookie_line; | 437 COOKIE_DLOG(INFO) << "SetCookie() line: " << cookie_line; |
| 430 | 438 |
| 431 // Parse the cookie. | 439 // Parse the cookie. |
| 432 ParsedCookie pc(cookie_line); | 440 ParsedCookie pc(cookie_line); |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 726 // keys that are a prefix of our hostname. I think the hash probing | 734 // keys that are a prefix of our hostname. I think the hash probing |
| 727 // should be fast and simple enough for now. | 735 // should be fast and simple enough for now. |
| 728 std::string CookieMonster::GetCookies(const GURL& url) { | 736 std::string CookieMonster::GetCookies(const GURL& url) { |
| 729 CookieOptions options; | 737 CookieOptions options; |
| 730 return GetCookiesWithOptions(url, options); | 738 return GetCookiesWithOptions(url, options); |
| 731 } | 739 } |
| 732 | 740 |
| 733 std::string CookieMonster::GetCookiesWithOptions(const GURL& url, | 741 std::string CookieMonster::GetCookiesWithOptions(const GURL& url, |
| 734 const CookieOptions& options) { | 742 const CookieOptions& options) { |
| 735 if (!HasCookieableScheme(url)) { | 743 if (!HasCookieableScheme(url)) { |
| 736 DLOG(WARNING) << "Unsupported cookie scheme: " << url.scheme(); | |
| 737 return std::string(); | 744 return std::string(); |
| 738 } | 745 } |
| 739 | 746 |
| 740 // Get the cookies for this host and its domain(s). | 747 // Get the cookies for this host and its domain(s). |
| 741 std::vector<CanonicalCookie*> cookies; | 748 std::vector<CanonicalCookie*> cookies; |
| 742 FindCookiesForHostAndDomain(url, options, &cookies); | 749 FindCookiesForHostAndDomain(url, options, &cookies); |
| 743 std::sort(cookies.begin(), cookies.end(), CookieSorter); | 750 std::sort(cookies.begin(), cookies.end(), CookieSorter); |
| 744 | 751 |
| 745 std::string cookie_line; | 752 std::string cookie_line; |
| 746 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); | 753 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1115 return true; | 1122 return true; |
| 1116 } | 1123 } |
| 1117 | 1124 |
| 1118 std::string CookieMonster::CanonicalCookie::DebugString() const { | 1125 std::string CookieMonster::CanonicalCookie::DebugString() const { |
| 1119 return StringPrintf("name: %s value: %s path: %s creation: %llu", | 1126 return StringPrintf("name: %s value: %s path: %s creation: %llu", |
| 1120 name_.c_str(), value_.c_str(), path_.c_str(), | 1127 name_.c_str(), value_.c_str(), path_.c_str(), |
| 1121 creation_date_.ToTimeT()); | 1128 creation_date_.ToTimeT()); |
| 1122 } | 1129 } |
| 1123 | 1130 |
| 1124 } // namespace | 1131 } // namespace |
| OLD | NEW |