OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 // first iterator with access date >= |access_date|, or cookie_its_end if this | 249 // first iterator with access date >= |access_date|, or cookie_its_end if this |
250 // holds for all. | 250 // holds for all. |
251 CookieMonster::CookieItVector::iterator LowerBoundAccessDate( | 251 CookieMonster::CookieItVector::iterator LowerBoundAccessDate( |
252 const CookieMonster::CookieItVector::iterator its_begin, | 252 const CookieMonster::CookieItVector::iterator its_begin, |
253 const CookieMonster::CookieItVector::iterator its_end, | 253 const CookieMonster::CookieItVector::iterator its_end, |
254 const Time& access_date) { | 254 const Time& access_date) { |
255 return std::lower_bound(its_begin, its_end, access_date, | 255 return std::lower_bound(its_begin, its_end, access_date, |
256 LowerBoundAccessDateComparator); | 256 LowerBoundAccessDateComparator); |
257 } | 257 } |
258 | 258 |
259 // Mapping between DeletionCause and Delegate::ChangeCause; the mapping also | 259 // Mapping between DeletionCause and CookieMonsterDelegate::ChangeCause; the |
260 // provides a boolean that specifies whether or not an OnCookieChanged | 260 // mapping also provides a boolean that specifies whether or not an |
261 // notification ought to be generated. | 261 // OnCookieChanged notification ought to be generated. |
262 typedef struct ChangeCausePair_struct { | 262 typedef struct ChangeCausePair_struct { |
263 CookieMonster::Delegate::ChangeCause cause; | 263 CookieMonsterDelegate::ChangeCause cause; |
264 bool notify; | 264 bool notify; |
265 } ChangeCausePair; | 265 } ChangeCausePair; |
266 ChangeCausePair ChangeCauseMapping[] = { | 266 ChangeCausePair ChangeCauseMapping[] = { |
267 // DELETE_COOKIE_EXPLICIT | 267 // DELETE_COOKIE_EXPLICIT |
268 { CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, true }, | 268 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, true }, |
269 // DELETE_COOKIE_OVERWRITE | 269 // DELETE_COOKIE_OVERWRITE |
270 { CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE, true }, | 270 { CookieMonsterDelegate::CHANGE_COOKIE_OVERWRITE, true }, |
271 // DELETE_COOKIE_EXPIRED | 271 // DELETE_COOKIE_EXPIRED |
272 { CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED, true }, | 272 { CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED, true }, |
273 // DELETE_COOKIE_EVICTED | 273 // DELETE_COOKIE_EVICTED |
274 { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true }, | 274 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true }, |
275 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE | 275 // DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE |
276 { CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, false }, | 276 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false }, |
277 // DELETE_COOKIE_DONT_RECORD | 277 // DELETE_COOKIE_DONT_RECORD |
278 { CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, false }, | 278 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false }, |
279 // DELETE_COOKIE_EVICTED_DOMAIN | 279 // DELETE_COOKIE_EVICTED_DOMAIN |
280 { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true }, | 280 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true }, |
281 // DELETE_COOKIE_EVICTED_GLOBAL | 281 // DELETE_COOKIE_EVICTED_GLOBAL |
282 { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true }, | 282 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true }, |
283 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE | 283 // DELETE_COOKIE_EVICTED_DOMAIN_PRE_SAFE |
284 { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true }, | 284 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true }, |
285 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE | 285 // DELETE_COOKIE_EVICTED_DOMAIN_POST_SAFE |
286 { CookieMonster::Delegate::CHANGE_COOKIE_EVICTED, true }, | 286 { CookieMonsterDelegate::CHANGE_COOKIE_EVICTED, true }, |
287 // DELETE_COOKIE_EXPIRED_OVERWRITE | 287 // DELETE_COOKIE_EXPIRED_OVERWRITE |
288 { CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED_OVERWRITE, true }, | 288 { CookieMonsterDelegate::CHANGE_COOKIE_EXPIRED_OVERWRITE, true }, |
289 // DELETE_COOKIE_LAST_ENTRY | 289 // DELETE_COOKIE_LAST_ENTRY |
290 { CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT, false } | 290 { CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT, false } |
291 }; | 291 }; |
292 | 292 |
293 std::string BuildCookieLine(const CanonicalCookieVector& cookies) { | 293 std::string BuildCookieLine(const CanonicalCookieVector& cookies) { |
294 std::string cookie_line; | 294 std::string cookie_line; |
295 for (CanonicalCookieVector::const_iterator it = cookies.begin(); | 295 for (CanonicalCookieVector::const_iterator it = cookies.begin(); |
296 it != cookies.end(); ++it) { | 296 it != cookies.end(); ++it) { |
297 if (it != cookies.begin()) | 297 if (it != cookies.begin()) |
298 cookie_line += "; "; | 298 cookie_line += "; "; |
299 // In Mozilla if you set a cookie like AAAA, it will have an empty token | 299 // In Mozilla if you set a cookie like AAAA, it will have an empty token |
300 // and a value of AAAA. When it sends the cookie back, it will send AAAA, | 300 // and a value of AAAA. When it sends the cookie back, it will send AAAA, |
301 // so we need to avoid sending =AAAA for a blank token value. | 301 // so we need to avoid sending =AAAA for a blank token value. |
302 if (!(*it)->Name().empty()) | 302 if (!(*it)->Name().empty()) |
303 cookie_line += (*it)->Name() + "="; | 303 cookie_line += (*it)->Name() + "="; |
304 cookie_line += (*it)->Value(); | 304 cookie_line += (*it)->Value(); |
305 } | 305 } |
306 return cookie_line; | 306 return cookie_line; |
307 } | 307 } |
308 | 308 |
309 } // namespace | 309 } // namespace |
310 | 310 |
311 // static | 311 CookieMonster::CookieMonster(PersistentCookieStore* store, |
312 bool CookieMonster::default_enable_file_scheme_ = false; | 312 CookieMonsterDelegate* delegate) |
313 | |
314 CookieMonster::CookieMonster(PersistentCookieStore* store, Delegate* delegate) | |
315 : initialized_(false), | 313 : initialized_(false), |
316 loaded_(false), | 314 loaded_(false), |
317 store_(store), | 315 store_(store), |
318 last_access_threshold_( | 316 last_access_threshold_( |
319 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)), | 317 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)), |
320 delegate_(delegate), | 318 delegate_(delegate), |
321 last_statistic_record_time_(Time::Now()), | 319 last_statistic_record_time_(Time::Now()), |
322 keep_expired_cookies_(false), | 320 keep_expired_cookies_(false), |
323 persist_session_cookies_(false), | 321 persist_session_cookies_(false), |
324 priority_aware_garbage_collection_(false) { | 322 priority_aware_garbage_collection_(false) { |
325 InitializeHistograms(); | 323 InitializeHistograms(); |
326 SetDefaultCookieableSchemes(); | 324 SetDefaultCookieableSchemes(); |
327 } | 325 } |
328 | 326 |
329 CookieMonster::CookieMonster(PersistentCookieStore* store, | 327 CookieMonster::CookieMonster(PersistentCookieStore* store, |
330 Delegate* delegate, | 328 CookieMonsterDelegate* delegate, |
331 int last_access_threshold_milliseconds) | 329 int last_access_threshold_milliseconds) |
332 : initialized_(false), | 330 : initialized_(false), |
333 loaded_(false), | 331 loaded_(false), |
334 store_(store), | 332 store_(store), |
335 last_access_threshold_(base::TimeDelta::FromMilliseconds( | 333 last_access_threshold_(base::TimeDelta::FromMilliseconds( |
336 last_access_threshold_milliseconds)), | 334 last_access_threshold_milliseconds)), |
337 delegate_(delegate), | 335 delegate_(delegate), |
338 last_statistic_record_time_(base::Time::Now()), | 336 last_statistic_record_time_(base::Time::Now()), |
339 keep_expired_cookies_(false), | 337 keep_expired_cookies_(false), |
340 persist_session_cookies_(false), | 338 persist_session_cookies_(false), |
(...skipping 931 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1272 // above kDefaultCookieableSchemes. | 1270 // above kDefaultCookieableSchemes. |
1273 int num_schemes = accept ? kDefaultCookieableSchemesCount : | 1271 int num_schemes = accept ? kDefaultCookieableSchemesCount : |
1274 kDefaultCookieableSchemesCount - 1; | 1272 kDefaultCookieableSchemesCount - 1; |
1275 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes); | 1273 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes); |
1276 } | 1274 } |
1277 | 1275 |
1278 void CookieMonster::SetKeepExpiredCookies() { | 1276 void CookieMonster::SetKeepExpiredCookies() { |
1279 keep_expired_cookies_ = true; | 1277 keep_expired_cookies_ = true; |
1280 } | 1278 } |
1281 | 1279 |
1282 // static | |
1283 void CookieMonster::EnableFileScheme() { | |
1284 default_enable_file_scheme_ = true; | |
1285 } | |
1286 | |
1287 void CookieMonster::FlushStore(const base::Closure& callback) { | 1280 void CookieMonster::FlushStore(const base::Closure& callback) { |
1288 base::AutoLock autolock(lock_); | 1281 base::AutoLock autolock(lock_); |
1289 if (initialized_ && store_.get()) | 1282 if (initialized_ && store_.get()) |
1290 store_->Flush(callback); | 1283 store_->Flush(callback); |
1291 else if (!callback.is_null()) | 1284 else if (!callback.is_null()) |
1292 base::MessageLoop::current()->PostTask(FROM_HERE, callback); | 1285 base::MessageLoop::current()->PostTask(FROM_HERE, callback); |
1293 } | 1286 } |
1294 | 1287 |
1295 bool CookieMonster::SetCookieWithOptions(const GURL& url, | 1288 bool CookieMonster::SetCookieWithOptions(const GURL& url, |
1296 const std::string& cookie_line, | 1289 const std::string& cookie_line, |
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1635 return num_duplicates; | 1628 return num_duplicates; |
1636 } | 1629 } |
1637 | 1630 |
1638 // Note: file must be the last scheme. | 1631 // Note: file must be the last scheme. |
1639 const char* CookieMonster::kDefaultCookieableSchemes[] = | 1632 const char* CookieMonster::kDefaultCookieableSchemes[] = |
1640 { "http", "https", "file" }; | 1633 { "http", "https", "file" }; |
1641 const int CookieMonster::kDefaultCookieableSchemesCount = | 1634 const int CookieMonster::kDefaultCookieableSchemesCount = |
1642 arraysize(CookieMonster::kDefaultCookieableSchemes); | 1635 arraysize(CookieMonster::kDefaultCookieableSchemes); |
1643 | 1636 |
1644 void CookieMonster::SetDefaultCookieableSchemes() { | 1637 void CookieMonster::SetDefaultCookieableSchemes() { |
1645 int num_schemes = default_enable_file_scheme_ ? | 1638 // Always disable file scheme unless SetEnableFileScheme(true) is called. |
1646 kDefaultCookieableSchemesCount : kDefaultCookieableSchemesCount - 1; | 1639 SetCookieableSchemes(kDefaultCookieableSchemes, |
1647 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes); | 1640 kDefaultCookieableSchemesCount - 1); |
1648 } | 1641 } |
1649 | 1642 |
1650 void CookieMonster::FindCookiesForHostAndDomain( | 1643 void CookieMonster::FindCookiesForHostAndDomain( |
1651 const GURL& url, | 1644 const GURL& url, |
1652 const CookieOptions& options, | 1645 const CookieOptions& options, |
1653 bool update_access_time, | 1646 bool update_access_time, |
1654 std::vector<CanonicalCookie*>* cookies) { | 1647 std::vector<CanonicalCookie*>* cookies) { |
1655 lock_.AssertAcquired(); | 1648 lock_.AssertAcquired(); |
1656 | 1649 |
1657 const Time current_time(CurrentTime()); | 1650 const Time current_time(CurrentTime()); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1737 CanonicalCookie* cc, | 1730 CanonicalCookie* cc, |
1738 bool sync_to_store) { | 1731 bool sync_to_store) { |
1739 lock_.AssertAcquired(); | 1732 lock_.AssertAcquired(); |
1740 | 1733 |
1741 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() && | 1734 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() && |
1742 sync_to_store) | 1735 sync_to_store) |
1743 store_->AddCookie(*cc); | 1736 store_->AddCookie(*cc); |
1744 cookies_.insert(CookieMap::value_type(key, cc)); | 1737 cookies_.insert(CookieMap::value_type(key, cc)); |
1745 if (delegate_.get()) { | 1738 if (delegate_.get()) { |
1746 delegate_->OnCookieChanged( | 1739 delegate_->OnCookieChanged( |
1747 *cc, false, CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT); | 1740 *cc, false, CookieMonsterDelegate::CHANGE_COOKIE_EXPLICIT); |
1748 } | 1741 } |
1749 } | 1742 } |
1750 | 1743 |
1751 bool CookieMonster::SetCookieWithCreationTimeAndOptions( | 1744 bool CookieMonster::SetCookieWithCreationTimeAndOptions( |
1752 const GURL& url, | 1745 const GURL& url, |
1753 const std::string& cookie_line, | 1746 const std::string& cookie_line, |
1754 const Time& creation_time_or_null, | 1747 const Time& creation_time_or_null, |
1755 const CookieOptions& options) { | 1748 const CookieOptions& options) { |
1756 lock_.AssertAcquired(); | 1749 lock_.AssertAcquired(); |
1757 | 1750 |
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2214 | 2207 |
2215 // The system resolution is not high enough, so we can have multiple | 2208 // The system resolution is not high enough, so we can have multiple |
2216 // set cookies that result in the same system time. When this happens, we | 2209 // set cookies that result in the same system time. When this happens, we |
2217 // increment by one Time unit. Let's hope computers don't get too fast. | 2210 // increment by one Time unit. Let's hope computers don't get too fast. |
2218 Time CookieMonster::CurrentTime() { | 2211 Time CookieMonster::CurrentTime() { |
2219 return std::max(Time::Now(), | 2212 return std::max(Time::Now(), |
2220 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1)); | 2213 Time::FromInternalValue(last_time_seen_.ToInternalValue() + 1)); |
2221 } | 2214 } |
2222 | 2215 |
2223 } // namespace net | 2216 } // namespace net |
OLD | NEW |