Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: net/base/cookie_monster.cc

Issue 7864008: Split initial load of cookies by domains (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 * 42 *
43 * ***** END LICENSE BLOCK ***** */ 43 * ***** END LICENSE BLOCK ***** */
44 44
45 #include "net/base/cookie_monster.h" 45 #include "net/base/cookie_monster.h"
46 46
47 #include <algorithm> 47 #include <algorithm>
48 #include <set> 48 #include <set>
49 49
50 #include "base/basictypes.h" 50 #include "base/basictypes.h"
51 #include "base/bind.h" 51 #include "base/bind.h"
52 #include "base/callback.h"
52 #include "base/format_macros.h" 53 #include "base/format_macros.h"
53 #include "base/logging.h" 54 #include "base/logging.h"
54 #include "base/memory/scoped_ptr.h" 55 #include "base/memory/scoped_ptr.h"
55 #include "base/message_loop.h" 56 #include "base/message_loop.h"
56 #include "base/message_loop_proxy.h" 57 #include "base/message_loop_proxy.h"
57 #include "base/metrics/histogram.h" 58 #include "base/metrics/histogram.h"
58 #include "base/string_tokenizer.h" 59 #include "base/string_tokenizer.h"
59 #include "base/string_util.h" 60 #include "base/string_util.h"
60 #include "base/stringprintf.h" 61 #include "base/stringprintf.h"
61 #include "googleurl/src/gurl.h" 62 #include "googleurl/src/gurl.h"
62 #include "googleurl/src/url_canon.h" 63 #include "googleurl/src/url_canon.h"
63 #include "net/base/net_util.h" 64 #include "net/base/net_util.h"
64 #include "net/base/registry_controlled_domain.h" 65 #include "net/base/registry_controlled_domain.h"
65 66
66 using base::Time; 67 using base::Time;
67 using base::TimeDelta; 68 using base::TimeDelta;
68 using base::TimeTicks; 69 using base::TimeTicks;
69 70
71 // In steady state, most cookie requests can be satisfied by the in memory
72 // cookie monster store. However, if a request comes in during the initial
73 // cookie load, it must be delayed until that load completes. That is done by
74 // queueing it on CookieMonster::queue_ and running it when notification of
75 // cookie load completion is received via CookieMonster::OnLoaded. This callback
76 // is passed to the persistent store from CookieMonster::InitStore(), which is
77 // called on the first operation invoked on the CookieMonster.)
78 //
79 // On the browser critical paths (e.g. for loading initial web pages in a
80 // session restore) it may take too long to wait for the full load. In this case
81 // a priority load is triggered by calling PersistentCookieStore::
82 // LoadCookiesForKey. The request is queued in CookieMonster::tasks_queued
83 // and executed upon receiving notification of key load completion via
84 // CookieMonster::OnKeyLoaded(). If multiple requests for the same eTLD+1 are
85 // received before key load completion, only the first request calls
86 // PersistentCookieStore::LoadCookiesForKey, all subsequent requests are queued
87 // in CookieMonster::tasks_queued and executed upon receiving notification of
88 // key load completion triggered by the first request for the same eTLD+1.
89
70 static const int kMinutesInTenYears = 10 * 365 * 24 * 60; 90 static const int kMinutesInTenYears = 10 * 365 * 24 * 60;
71 91
72 namespace net { 92 namespace net {
73 93
74 // See comments at declaration of these variables in cookie_monster.h 94 // See comments at declaration of these variables in cookie_monster.h
75 // for details. 95 // for details.
76 const size_t CookieMonster::kDomainMaxCookies = 180; 96 const size_t CookieMonster::kDomainMaxCookies = 180;
77 const size_t CookieMonster::kDomainPurgeCookies = 30; 97 const size_t CookieMonster::kDomainPurgeCookies = 30;
78 const size_t CookieMonster::kMaxCookies = 3300; 98 const size_t CookieMonster::kMaxCookies = 3300;
79 const size_t CookieMonster::kPurgeCookies = 300; 99 const size_t CookieMonster::kPurgeCookies = 300;
(...skipping 906 matching lines...) Expand 10 before | Expand all | Expand 10 after
986 void CookieMonster::SetCookieWithDetailsAsync( 1006 void CookieMonster::SetCookieWithDetailsAsync(
987 const GURL& url, const std::string& name, const std::string& value, 1007 const GURL& url, const std::string& name, const std::string& value,
988 const std::string& domain, const std::string& path, 1008 const std::string& domain, const std::string& path,
989 const base::Time& expiration_time, bool secure, bool http_only, 1009 const base::Time& expiration_time, bool secure, bool http_only,
990 const SetCookiesCallback& callback) { 1010 const SetCookiesCallback& callback) {
991 scoped_refptr<SetCookieWithDetailsTask> task = 1011 scoped_refptr<SetCookieWithDetailsTask> task =
992 new SetCookieWithDetailsTask(this, url, name, value, domain, path, 1012 new SetCookieWithDetailsTask(this, url, name, value, domain, path,
993 expiration_time, secure, http_only, 1013 expiration_time, secure, http_only,
994 callback); 1014 callback);
995 1015
996 DoCookieTask(task); 1016 DoCookieTaskForURL(task, url);
997 } 1017 }
998 1018
999 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { 1019 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) {
1000 scoped_refptr<GetAllCookiesTask> task = 1020 scoped_refptr<GetAllCookiesTask> task =
1001 new GetAllCookiesTask(this, callback); 1021 new GetAllCookiesTask(this, callback);
1002 1022
1003 DoCookieTask(task); 1023 DoCookieTask(task);
1004 } 1024 }
1005 1025
1006 1026
1007 void CookieMonster::GetAllCookiesForURLWithOptionsAsync( 1027 void CookieMonster::GetAllCookiesForURLWithOptionsAsync(
1008 const GURL& url, 1028 const GURL& url,
1009 const CookieOptions& options, 1029 const CookieOptions& options,
1010 const GetCookieListCallback& callback) { 1030 const GetCookieListCallback& callback) {
1011 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = 1031 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
1012 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); 1032 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
1013 1033
1014 DoCookieTask(task); 1034 DoCookieTaskForURL(task, url);
1015 } 1035 }
1016 1036
1017 void CookieMonster::GetAllCookiesForURLAsync( 1037 void CookieMonster::GetAllCookiesForURLAsync(
1018 const GURL& url, const GetCookieListCallback& callback) { 1038 const GURL& url, const GetCookieListCallback& callback) {
1019 CookieOptions options; 1039 CookieOptions options;
1020 options.set_include_httponly(); 1040 options.set_include_httponly();
1021 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = 1041 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task =
1022 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); 1042 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback);
1023 1043
1024 DoCookieTask(task); 1044 DoCookieTaskForURL(task, url);
1025 } 1045 }
1026 1046
1027 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) { 1047 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) {
1028 scoped_refptr<DeleteAllTask> task = 1048 scoped_refptr<DeleteAllTask> task =
1029 new DeleteAllTask(this, callback); 1049 new DeleteAllTask(this, callback);
1030 1050
1031 DoCookieTask(task); 1051 DoCookieTask(task);
1032 } 1052 }
1033 1053
1034 void CookieMonster::DeleteAllCreatedBetweenAsync( 1054 void CookieMonster::DeleteAllCreatedBetweenAsync(
1035 const Time& delete_begin, const Time& delete_end, 1055 const Time& delete_begin, const Time& delete_end,
1036 const DeleteCallback& callback) { 1056 const DeleteCallback& callback) {
1037 scoped_refptr<DeleteAllCreatedBetweenTask> task = 1057 scoped_refptr<DeleteAllCreatedBetweenTask> task =
1038 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, 1058 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end,
1039 callback); 1059 callback);
1040 1060
1041 DoCookieTask(task); 1061 DoCookieTask(task);
1042 } 1062 }
1043 1063
1044 void CookieMonster::DeleteAllForHostAsync( 1064 void CookieMonster::DeleteAllForHostAsync(
1045 const GURL& url, const DeleteCallback& callback) { 1065 const GURL& url, const DeleteCallback& callback) {
1046 scoped_refptr<DeleteAllForHostTask> task = 1066 scoped_refptr<DeleteAllForHostTask> task =
1047 new DeleteAllForHostTask(this, url, callback); 1067 new DeleteAllForHostTask(this, url, callback);
1048 1068
1049 DoCookieTask(task); 1069 DoCookieTaskForURL(task, url);
1050 } 1070 }
1051 1071
1052 void CookieMonster::DeleteCanonicalCookieAsync( 1072 void CookieMonster::DeleteCanonicalCookieAsync(
1053 const CanonicalCookie& cookie, 1073 const CanonicalCookie& cookie,
1054 const DeleteCookieCallback& callback) { 1074 const DeleteCookieCallback& callback) {
1055 scoped_refptr<DeleteCanonicalCookieTask> task = 1075 scoped_refptr<DeleteCanonicalCookieTask> task =
1056 new DeleteCanonicalCookieTask(this, cookie, callback); 1076 new DeleteCanonicalCookieTask(this, cookie, callback);
1057 1077
1058 DoCookieTask(task); 1078 DoCookieTask(task);
1059 } 1079 }
1060 1080
1061 void CookieMonster::SetCookieWithOptionsAsync( 1081 void CookieMonster::SetCookieWithOptionsAsync(
1062 const GURL& url, 1082 const GURL& url,
1063 const std::string& cookie_line, 1083 const std::string& cookie_line,
1064 const CookieOptions& options, 1084 const CookieOptions& options,
1065 const SetCookiesCallback& callback) { 1085 const SetCookiesCallback& callback) {
1066 scoped_refptr<SetCookieWithOptionsTask> task = 1086 scoped_refptr<SetCookieWithOptionsTask> task =
1067 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback); 1087 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback);
1068 1088
1069 DoCookieTask(task); 1089 DoCookieTaskForURL(task, url);
1070 } 1090 }
1071 1091
1072 void CookieMonster::GetCookiesWithOptionsAsync( 1092 void CookieMonster::GetCookiesWithOptionsAsync(
1073 const GURL& url, 1093 const GURL& url,
1074 const CookieOptions& options, 1094 const CookieOptions& options,
1075 const GetCookiesCallback& callback) { 1095 const GetCookiesCallback& callback) {
1076 scoped_refptr<GetCookiesWithOptionsTask> task = 1096 scoped_refptr<GetCookiesWithOptionsTask> task =
1077 new GetCookiesWithOptionsTask(this, url, options, callback); 1097 new GetCookiesWithOptionsTask(this, url, options, callback);
1078 1098
1079 DoCookieTask(task); 1099 DoCookieTaskForURL(task, url);
1080 } 1100 }
1081 1101
1082 void CookieMonster::GetCookiesWithInfoAsync( 1102 void CookieMonster::GetCookiesWithInfoAsync(
1083 const GURL& url, 1103 const GURL& url,
1084 const CookieOptions& options, 1104 const CookieOptions& options,
1085 const GetCookieInfoCallback& callback) { 1105 const GetCookieInfoCallback& callback) {
1086 scoped_refptr<GetCookiesWithInfoTask> task = 1106 scoped_refptr<GetCookiesWithInfoTask> task =
1087 new GetCookiesWithInfoTask(this, url, options, callback); 1107 new GetCookiesWithInfoTask(this, url, options, callback);
1088 1108
1089 DoCookieTask(task); 1109 DoCookieTaskForURL(task, url);
1090 } 1110 }
1091 1111
1092 void CookieMonster::DeleteCookieAsync(const GURL& url, 1112 void CookieMonster::DeleteCookieAsync(const GURL& url,
1093 const std::string& cookie_name, 1113 const std::string& cookie_name,
1094 const base::Closure& callback) { 1114 const base::Closure& callback) {
1095 scoped_refptr<DeleteCookieTask> task = 1115 scoped_refptr<DeleteCookieTask> task =
1096 new DeleteCookieTask(this, url, cookie_name, callback); 1116 new DeleteCookieTask(this, url, cookie_name, callback);
1097 1117
1098 DoCookieTask(task); 1118 DoCookieTaskForURL(task, url);
1099 } 1119 }
1100 1120
1101 void CookieMonster::DoCookieTask( 1121 void CookieMonster::DoCookieTask(
1102 const scoped_refptr<CookieMonsterTask>& task_item) { 1122 const scoped_refptr<CookieMonsterTask>& task_item) {
1103 InitIfNecessary();
1104
1105 { 1123 {
1124 InitIfNecessary();
erikwright (departed) 2011/10/06 17:16:47 Put InitIfNecessary after the autolock declaration
guohui 2011/10/06 18:34:54 Done.
1106 base::AutoLock autolock(lock_); 1125 base::AutoLock autolock(lock_);
1107 if (!loaded_) { 1126 if (!loaded_) {
1108 queue_.push(task_item); 1127 queue_.push(task_item);
1109 return; 1128 return;
1110 } 1129 }
1111 } 1130 }
1112 1131
1113 task_item->Run(); 1132 task_item->Run();
1114 } 1133 }
1115 1134
1135 void CookieMonster::DoCookieTaskForURL(
1136 const scoped_refptr<CookieMonsterTask>& task_item,
1137 const GURL& url) {
1138 {
1139 base::AutoLock autolock(lock_);
1140 InitIfNecessary();
1141 // If cookies for the requested domain key (eTLD+1) have been loaded from DB
1142 // then run the task, otherwise load from DB.
1143 if (!loaded_) {
1144 // Checks if the domain key has been loaded.
1145 std::string key(GetEffectiveDomain(url.scheme(), url.host()));
1146 if (keys_loaded_.find(key) == keys_loaded_.end()) {
1147 std::map<std::string, std::queue<scoped_refptr<CookieMonsterTask> > >
1148 ::iterator it = tasks_queued_.find(key);
1149 if (it == tasks_queued_.end()) {
1150 store_->LoadCookiesForKey(key,
1151 base::Bind(&CookieMonster::OnKeyLoaded, this, key));
1152 it = tasks_queued_.insert(std::make_pair(key,
1153 std::queue<scoped_refptr<CookieMonsterTask> >())).first;
1154 }
1155 it->second.push(task_item);
1156 return;
1157 }
1158 }
1159 }
1160 task_item->Run();
1161 }
1162
1116 bool CookieMonster::SetCookieWithDetails( 1163 bool CookieMonster::SetCookieWithDetails(
1117 const GURL& url, const std::string& name, const std::string& value, 1164 const GURL& url, const std::string& name, const std::string& value,
1118 const std::string& domain, const std::string& path, 1165 const std::string& domain, const std::string& path,
1119 const base::Time& expiration_time, bool secure, bool http_only) { 1166 const base::Time& expiration_time, bool secure, bool http_only) {
1120 base::AutoLock autolock(lock_); 1167 base::AutoLock autolock(lock_);
1121 1168
1122 if (!HasCookieableScheme(url)) 1169 if (!HasCookieableScheme(url))
1123 return false; 1170 return false;
1124 1171
1125 Time creation_time = CurrentTime(); 1172 Time creation_time = CurrentTime();
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
1463 1510
1464 void CookieMonster::OnLoaded(TimeTicks beginning_time, 1511 void CookieMonster::OnLoaded(TimeTicks beginning_time,
1465 const std::vector<CanonicalCookie*>& cookies) { 1512 const std::vector<CanonicalCookie*>& cookies) {
1466 StoreLoadedCookies(cookies); 1513 StoreLoadedCookies(cookies);
1467 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); 1514 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time);
1468 1515
1469 // Invoke the task queue of cookie request. 1516 // Invoke the task queue of cookie request.
1470 InvokeQueue(); 1517 InvokeQueue();
1471 } 1518 }
1472 1519
1520 void CookieMonster::OnKeyLoaded(const std::string& key,
1521 const std::vector<CanonicalCookie*>& cookies) {
1522 // This function does its own separate locking.
1523 StoreLoadedCookies(cookies);
1524 {
1525 base::AutoLock autolock(lock_);
1526 keys_loaded_.insert(key);
1527 }
1528 std::map<std::string, std::queue<scoped_refptr<CookieMonsterTask> > >
1529 ::iterator it = tasks_queued_.find(key);
1530 if (it != tasks_queued_.end()) {
1531 while (!it->second.empty()) {
1532 scoped_refptr<CookieMonsterTask> task = it->second.front();
1533 task->Run();
1534 it->second.pop();
1535 }
1536 tasks_queued_.erase(it);
1537 }
1538 }
1539
1473 void CookieMonster::StoreLoadedCookies( 1540 void CookieMonster::StoreLoadedCookies(
1474 const std::vector<CanonicalCookie*>& cookies) { 1541 const std::vector<CanonicalCookie*>& cookies) {
1475 // Initialize the store and sync in any saved persistent cookies. We don't 1542 // Initialize the store and sync in any saved persistent cookies. We don't
1476 // care if it's expired, insert it so it can be garbage collected, removed, 1543 // care if it's expired, insert it so it can be garbage collected, removed,
1477 // and sync'd. 1544 // and sync'd.
1478 base::AutoLock autolock(lock_); 1545 base::AutoLock autolock(lock_);
1479 1546
1480 // Avoid ever letting cookies with duplicate creation times into the store;
1481 // that way we don't have to worry about what sections of code are safe
1482 // to call while it's in that state.
1483 std::set<int64> creation_times;
1484
1485 // Presumably later than any access time in the store.
1486 Time earliest_access_time;
1487
1488 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); 1547 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin();
1489 it != cookies.end(); ++it) { 1548 it != cookies.end(); ++it) {
1490 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue(); 1549 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue();
1491 1550
1492 if (creation_times.insert(cookie_creation_time).second) { 1551 if (creation_times_.insert(cookie_creation_time).second) {
1493 InternalInsertCookie(GetKey((*it)->Domain()), *it, false); 1552 InternalInsertCookie(GetKey((*it)->Domain()), *it, false);
1494 const Time cookie_access_time((*it)->LastAccessDate()); 1553 const Time cookie_access_time((*it)->LastAccessDate());
1495 if (earliest_access_time.is_null() || 1554 if (earliest_access_time_.is_null() ||
1496 cookie_access_time < earliest_access_time) 1555 cookie_access_time < earliest_access_time_)
1497 earliest_access_time = cookie_access_time; 1556 earliest_access_time_ = cookie_access_time;
1498 } else { 1557 } else {
1499 LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation " 1558 LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation "
1500 "times in backing store: " 1559 "times in backing store: "
1501 "{name='%s', domain='%s', path='%s'}", 1560 "{name='%s', domain='%s', path='%s'}",
1502 (*it)->Name().c_str(), 1561 (*it)->Name().c_str(),
1503 (*it)->Domain().c_str(), 1562 (*it)->Domain().c_str(),
1504 (*it)->Path().c_str()); 1563 (*it)->Path().c_str());
1505 // We've been given ownership of the cookie and are throwing it 1564 // We've been given ownership of the cookie and are throwing it
1506 // away; reclaim the space. 1565 // away; reclaim the space.
1507 delete (*it); 1566 delete (*it);
1508 } 1567 }
1509 } 1568 }
1510 earliest_access_time_= earliest_access_time;
1511 1569
1512 // After importing cookies from the PersistentCookieStore, verify that 1570 // After importing cookies from the PersistentCookieStore, verify that
1513 // none of our other constraints are violated. 1571 // none of our other constraints are violated.
1514 //
1515 // In particular, the backing store might have given us duplicate cookies. 1572 // In particular, the backing store might have given us duplicate cookies.
1573 // "Priority loaded" cookies will be validated more than once, but this is OK
1574 // since they are expected to be much fewer than total DB.
1516 EnsureCookiesMapIsValid(); 1575 EnsureCookiesMapIsValid();
1517 } 1576 }
1518 1577
1519 void CookieMonster::InvokeQueue() { 1578 void CookieMonster::InvokeQueue() {
1520 while (true) { 1579 while (true) {
1521 scoped_refptr<CookieMonsterTask> request_task; 1580 scoped_refptr<CookieMonsterTask> request_task;
1522 { 1581 {
1523 base::AutoLock autolock(lock_); 1582 base::AutoLock autolock(lock_);
1524 if (queue_.empty()) { 1583 if (queue_.empty()) {
1525 loaded_ = true; 1584 loaded_ = true;
1585 creation_times_.clear();
1586 keys_loaded_.clear();
1526 break; 1587 break;
1527 } 1588 }
1528 request_task = queue_.front(); 1589 request_task = queue_.front();
1529 queue_.pop(); 1590 queue_.pop();
1530 } 1591 }
1531 request_task->Run(); 1592 request_task->Run();
1532 } 1593 }
1533 } 1594 }
1534 1595
1535 void CookieMonster::EnsureCookiesMapIsValid() { 1596 void CookieMonster::EnsureCookiesMapIsValid() {
(...skipping 1228 matching lines...) Expand 10 before | Expand all | Expand 10 after
2764 std::string CookieMonster::CanonicalCookie::DebugString() const { 2825 std::string CookieMonster::CanonicalCookie::DebugString() const {
2765 return base::StringPrintf( 2826 return base::StringPrintf(
2766 "name: %s value: %s domain: %s path: %s creation: %" 2827 "name: %s value: %s domain: %s path: %s creation: %"
2767 PRId64, 2828 PRId64,
2768 name_.c_str(), value_.c_str(), 2829 name_.c_str(), value_.c_str(),
2769 domain_.c_str(), path_.c_str(), 2830 domain_.c_str(), path_.c_str(),
2770 static_cast<int64>(creation_date_.ToTimeT())); 2831 static_cast<int64>(creation_date_.ToTimeT()));
2771 } 2832 }
2772 2833
2773 } // namespace 2834 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698