OLD | NEW |
---|---|
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 975 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
986 void CookieMonster::SetCookieWithDetailsAsync( | 986 void CookieMonster::SetCookieWithDetailsAsync( |
987 const GURL& url, const std::string& name, const std::string& value, | 987 const GURL& url, const std::string& name, const std::string& value, |
988 const std::string& domain, const std::string& path, | 988 const std::string& domain, const std::string& path, |
989 const base::Time& expiration_time, bool secure, bool http_only, | 989 const base::Time& expiration_time, bool secure, bool http_only, |
990 const SetCookiesCallback& callback) { | 990 const SetCookiesCallback& callback) { |
991 scoped_refptr<SetCookieWithDetailsTask> task = | 991 scoped_refptr<SetCookieWithDetailsTask> task = |
992 new SetCookieWithDetailsTask(this, url, name, value, domain, path, | 992 new SetCookieWithDetailsTask(this, url, name, value, domain, path, |
993 expiration_time, secure, http_only, | 993 expiration_time, secure, http_only, |
994 callback); | 994 callback); |
995 | 995 |
996 DoCookieTask(task); | 996 DoCookieTask(task, url); |
997 } | 997 } |
998 | 998 |
999 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { | 999 void CookieMonster::GetAllCookiesAsync(const GetCookieListCallback& callback) { |
1000 scoped_refptr<GetAllCookiesTask> task = | 1000 scoped_refptr<GetAllCookiesTask> task = |
1001 new GetAllCookiesTask(this, callback); | 1001 new GetAllCookiesTask(this, callback); |
1002 | 1002 |
1003 DoCookieTask(task); | 1003 DoCookieTask(task); |
1004 } | 1004 } |
1005 | 1005 |
1006 | 1006 |
1007 void CookieMonster::GetAllCookiesForURLWithOptionsAsync( | 1007 void CookieMonster::GetAllCookiesForURLWithOptionsAsync( |
1008 const GURL& url, | 1008 const GURL& url, |
1009 const CookieOptions& options, | 1009 const CookieOptions& options, |
1010 const GetCookieListCallback& callback) { | 1010 const GetCookieListCallback& callback) { |
1011 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = | 1011 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = |
1012 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); | 1012 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); |
1013 | 1013 |
1014 DoCookieTask(task); | 1014 DoCookieTask(task, url); |
1015 } | 1015 } |
1016 | 1016 |
1017 void CookieMonster::GetAllCookiesForURLAsync( | 1017 void CookieMonster::GetAllCookiesForURLAsync( |
1018 const GURL& url, const GetCookieListCallback& callback) { | 1018 const GURL& url, const GetCookieListCallback& callback) { |
1019 CookieOptions options; | 1019 CookieOptions options; |
1020 options.set_include_httponly(); | 1020 options.set_include_httponly(); |
1021 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = | 1021 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = |
1022 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); | 1022 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); |
1023 | 1023 |
1024 DoCookieTask(task); | 1024 DoCookieTask(task, url); |
1025 } | 1025 } |
1026 | 1026 |
1027 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) { | 1027 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) { |
1028 scoped_refptr<DeleteAllTask> task = | 1028 scoped_refptr<DeleteAllTask> task = |
1029 new DeleteAllTask(this, callback); | 1029 new DeleteAllTask(this, callback); |
1030 | 1030 |
1031 DoCookieTask(task); | 1031 DoCookieTask(task); |
1032 } | 1032 } |
1033 | 1033 |
1034 void CookieMonster::DeleteAllCreatedBetweenAsync( | 1034 void CookieMonster::DeleteAllCreatedBetweenAsync( |
1035 const Time& delete_begin, const Time& delete_end, | 1035 const Time& delete_begin, const Time& delete_end, |
1036 const DeleteCallback& callback) { | 1036 const DeleteCallback& callback) { |
1037 scoped_refptr<DeleteAllCreatedBetweenTask> task = | 1037 scoped_refptr<DeleteAllCreatedBetweenTask> task = |
1038 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, | 1038 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, |
1039 callback); | 1039 callback); |
1040 | 1040 |
1041 DoCookieTask(task); | 1041 DoCookieTask(task); |
1042 } | 1042 } |
1043 | 1043 |
1044 void CookieMonster::DeleteAllForHostAsync( | 1044 void CookieMonster::DeleteAllForHostAsync( |
1045 const GURL& url, const DeleteCallback& callback) { | 1045 const GURL& url, const DeleteCallback& callback) { |
1046 scoped_refptr<DeleteAllForHostTask> task = | 1046 scoped_refptr<DeleteAllForHostTask> task = |
1047 new DeleteAllForHostTask(this, url, callback); | 1047 new DeleteAllForHostTask(this, url, callback); |
1048 | 1048 |
1049 DoCookieTask(task); | 1049 DoCookieTask(task, url); |
1050 } | 1050 } |
1051 | 1051 |
1052 void CookieMonster::DeleteCanonicalCookieAsync( | 1052 void CookieMonster::DeleteCanonicalCookieAsync( |
1053 const CanonicalCookie& cookie, | 1053 const CanonicalCookie& cookie, |
1054 const DeleteCookieCallback& callback) { | 1054 const DeleteCookieCallback& callback) { |
1055 scoped_refptr<DeleteCanonicalCookieTask> task = | 1055 scoped_refptr<DeleteCanonicalCookieTask> task = |
1056 new DeleteCanonicalCookieTask(this, cookie, callback); | 1056 new DeleteCanonicalCookieTask(this, cookie, callback); |
1057 | 1057 |
1058 DoCookieTask(task); | 1058 DoCookieTask(task); |
1059 } | 1059 } |
1060 | 1060 |
1061 void CookieMonster::SetCookieWithOptionsAsync( | 1061 void CookieMonster::SetCookieWithOptionsAsync( |
1062 const GURL& url, | 1062 const GURL& url, |
1063 const std::string& cookie_line, | 1063 const std::string& cookie_line, |
1064 const CookieOptions& options, | 1064 const CookieOptions& options, |
1065 const SetCookiesCallback& callback) { | 1065 const SetCookiesCallback& callback) { |
1066 scoped_refptr<SetCookieWithOptionsTask> task = | 1066 scoped_refptr<SetCookieWithOptionsTask> task = |
1067 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback); | 1067 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback); |
1068 | 1068 |
1069 DoCookieTask(task); | 1069 DoCookieTask(task, url); |
1070 } | 1070 } |
1071 | 1071 |
1072 void CookieMonster::GetCookiesWithOptionsAsync( | 1072 void CookieMonster::GetCookiesWithOptionsAsync( |
1073 const GURL& url, | 1073 const GURL& url, |
1074 const CookieOptions& options, | 1074 const CookieOptions& options, |
1075 const GetCookiesCallback& callback) { | 1075 const GetCookiesCallback& callback) { |
1076 scoped_refptr<GetCookiesWithOptionsTask> task = | 1076 scoped_refptr<GetCookiesWithOptionsTask> task = |
1077 new GetCookiesWithOptionsTask(this, url, options, callback); | 1077 new GetCookiesWithOptionsTask(this, url, options, callback); |
1078 | 1078 |
1079 DoCookieTask(task); | 1079 DoCookieTask(task, url); |
1080 } | 1080 } |
1081 | 1081 |
1082 void CookieMonster::GetCookiesWithInfoAsync( | 1082 void CookieMonster::GetCookiesWithInfoAsync( |
1083 const GURL& url, | 1083 const GURL& url, |
1084 const CookieOptions& options, | 1084 const CookieOptions& options, |
1085 const GetCookieInfoCallback& callback) { | 1085 const GetCookieInfoCallback& callback) { |
1086 scoped_refptr<GetCookiesWithInfoTask> task = | 1086 scoped_refptr<GetCookiesWithInfoTask> task = |
1087 new GetCookiesWithInfoTask(this, url, options, callback); | 1087 new GetCookiesWithInfoTask(this, url, options, callback); |
1088 | 1088 |
1089 DoCookieTask(task); | 1089 DoCookieTask(task, url); |
1090 } | 1090 } |
1091 | 1091 |
1092 void CookieMonster::DeleteCookieAsync(const GURL& url, | 1092 void CookieMonster::DeleteCookieAsync(const GURL& url, |
1093 const std::string& cookie_name, | 1093 const std::string& cookie_name, |
1094 const base::Closure& callback) { | 1094 const base::Closure& callback) { |
1095 scoped_refptr<DeleteCookieTask> task = | 1095 scoped_refptr<DeleteCookieTask> task = |
1096 new DeleteCookieTask(this, url, cookie_name, callback); | 1096 new DeleteCookieTask(this, url, cookie_name, callback); |
1097 | 1097 |
1098 DoCookieTask(task); | 1098 DoCookieTask(task, url); |
1099 } | 1099 } |
1100 | 1100 |
1101 void CookieMonster::DoCookieTask( | 1101 void CookieMonster::DoCookieTask( |
1102 const scoped_refptr<CookieMonsterTask>& task_item) { | 1102 const scoped_refptr<CookieMonsterTask>& task_item) { |
1103 InitIfNecessary(); | 1103 InitIfNecessary(); |
1104 | 1104 |
1105 { | 1105 { |
1106 base::AutoLock autolock(lock_); | 1106 base::AutoLock autolock(lock_); |
1107 if (!loaded_) { | 1107 if (!loaded_) { |
1108 queue_.push(task_item); | 1108 queue_.push(task_item); |
1109 return; | 1109 return; |
1110 } | 1110 } |
1111 } | 1111 } |
1112 | 1112 |
1113 task_item->Run(); | 1113 task_item->Run(); |
1114 } | 1114 } |
1115 | 1115 |
1116 void CookieMonster::DoCookieTask( | |
1117 const scoped_refptr<CookieMonsterTask>& task_item, | |
1118 const GURL& url) { | |
1119 InitIfNecessary(); | |
1120 | |
1121 { | |
1122 base::AutoLock autolock(lock_); | |
1123 // If cookies for the requested domain key (eTLD+1) have been loaded from DB | |
1124 // then run the task, otherwise load from DB. | |
1125 if (!loaded_) { | |
1126 // Check if the domain key has been loaded. | |
1127 std::string key(GetEffectiveDomain(url.scheme(), url.host())); | |
1128 if (keys_loaded_.find(key) == keys_loaded_.end()) { | |
1129 store_->LoadCookiesForKey(key, | |
1130 base::Bind(&CookieMonster::OnKeyLoaded, this, | |
1131 base::Bind(&CookieMonsterTask::Run, task_item.get()), | |
1132 key)); | |
1133 return; | |
1134 } | |
1135 } | |
1136 } | |
1137 | |
1138 | |
erikwright (departed)
2011/09/15 20:56:27
Remove extra blank.
guohui
2011/09/16 18:21:09
Done.
| |
1139 task_item->Run(); | |
1140 } | |
1141 | |
1116 bool CookieMonster::SetCookieWithDetails( | 1142 bool CookieMonster::SetCookieWithDetails( |
1117 const GURL& url, const std::string& name, const std::string& value, | 1143 const GURL& url, const std::string& name, const std::string& value, |
1118 const std::string& domain, const std::string& path, | 1144 const std::string& domain, const std::string& path, |
1119 const base::Time& expiration_time, bool secure, bool http_only) { | 1145 const base::Time& expiration_time, bool secure, bool http_only) { |
1120 base::AutoLock autolock(lock_); | 1146 base::AutoLock autolock(lock_); |
1121 | 1147 |
1122 if (!HasCookieableScheme(url)) | 1148 if (!HasCookieableScheme(url)) |
1123 return false; | 1149 return false; |
1124 | 1150 |
1125 Time creation_time = CurrentTime(); | 1151 Time creation_time = CurrentTime(); |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1463 | 1489 |
1464 void CookieMonster::OnLoaded(TimeTicks beginning_time, | 1490 void CookieMonster::OnLoaded(TimeTicks beginning_time, |
1465 const std::vector<CanonicalCookie*>& cookies) { | 1491 const std::vector<CanonicalCookie*>& cookies) { |
1466 StoreLoadedCookies(cookies); | 1492 StoreLoadedCookies(cookies); |
1467 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); | 1493 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); |
1468 | 1494 |
1469 // Invoke the task queue of cookie request. | 1495 // Invoke the task queue of cookie request. |
1470 InvokeQueue(); | 1496 InvokeQueue(); |
1471 } | 1497 } |
1472 | 1498 |
1499 void CookieMonster::OnKeyLoaded( | |
1500 const base::Closure& request_task, const std::string& key, | |
1501 const std::vector<CanonicalCookie*>& cookies) { | |
erikwright (departed)
2011/09/15 20:56:27
It seems you require lock_ in order to access keys
guohui
2011/09/16 18:21:09
Done.
| |
1502 StoreLoadedCookies(cookies); | |
1503 keys_loaded_.insert(key); | |
1504 request_task.Run(); | |
1505 } | |
1506 | |
1473 void CookieMonster::StoreLoadedCookies( | 1507 void CookieMonster::StoreLoadedCookies( |
1474 const std::vector<CanonicalCookie*>& cookies) { | 1508 const std::vector<CanonicalCookie*>& cookies) { |
1475 // Initialize the store and sync in any saved persistent cookies. We don't | 1509 // 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, | 1510 // care if it's expired, insert it so it can be garbage collected, removed, |
1477 // and sync'd. | 1511 // and sync'd. |
1478 base::AutoLock autolock(lock_); | 1512 base::AutoLock autolock(lock_); |
1479 | 1513 |
1480 // Avoid ever letting cookies with duplicate creation times into the store; | 1514 // 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 | 1515 // that way we don't have to worry about what sections of code are safe |
1482 // to call while it's in that state. | 1516 // to call while it's in that state. |
1483 std::set<int64> creation_times; | 1517 std::set<int64> creation_times; |
erikwright (departed)
2011/09/15 20:56:27
It seems this will need to become a member variabl
guohui
2011/09/16 18:21:09
Done.
| |
1484 | 1518 |
1485 // Presumably later than any access time in the store. | 1519 // Presumably later than any access time in the store. |
erikwright (departed)
2011/09/15 20:56:27
This comment seems insensible. The default value f
guohui
2011/09/16 18:21:09
Done.
| |
1486 Time earliest_access_time; | 1520 Time earliest_access_time; |
1487 | 1521 |
1488 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); | 1522 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
1489 it != cookies.end(); ++it) { | 1523 it != cookies.end(); ++it) { |
1490 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue(); | 1524 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue(); |
1491 | 1525 |
1492 if (creation_times.insert(cookie_creation_time).second) { | 1526 if (creation_times.insert(cookie_creation_time).second) { |
1493 InternalInsertCookie(GetKey((*it)->Domain()), *it, false); | 1527 InternalInsertCookie(GetKey((*it)->Domain()), *it, false); |
1494 const Time cookie_access_time((*it)->LastAccessDate()); | 1528 const Time cookie_access_time((*it)->LastAccessDate()); |
1495 if (earliest_access_time.is_null() || | 1529 if (earliest_access_time.is_null() || |
1496 cookie_access_time < earliest_access_time) | 1530 cookie_access_time < earliest_access_time) |
1497 earliest_access_time = cookie_access_time; | 1531 earliest_access_time = cookie_access_time; |
1498 } else { | 1532 } else { |
1499 LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation " | 1533 LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation " |
1500 "times in backing store: " | 1534 "times in backing store: " |
1501 "{name='%s', domain='%s', path='%s'}", | 1535 "{name='%s', domain='%s', path='%s'}", |
1502 (*it)->Name().c_str(), | 1536 (*it)->Name().c_str(), |
1503 (*it)->Domain().c_str(), | 1537 (*it)->Domain().c_str(), |
1504 (*it)->Path().c_str()); | 1538 (*it)->Path().c_str()); |
1505 // We've been given ownership of the cookie and are throwing it | 1539 // We've been given ownership of the cookie and are throwing it |
1506 // away; reclaim the space. | 1540 // away; reclaim the space. |
1507 delete (*it); | 1541 delete (*it); |
1508 } | 1542 } |
1509 } | 1543 } |
1510 earliest_access_time_= earliest_access_time; | 1544 earliest_access_time_= earliest_access_time; |
erikwright (departed)
2011/09/15 20:56:27
This could have previously been set, possibly to a
guohui
2011/09/16 18:21:09
Done.
| |
1511 | 1545 |
1512 // After importing cookies from the PersistentCookieStore, verify that | 1546 // After importing cookies from the PersistentCookieStore, verify that |
1513 // none of our other constraints are violated. | 1547 // none of our other constraints are violated. |
1514 // | 1548 // |
1515 // In particular, the backing store might have given us duplicate cookies. | 1549 // In particular, the backing store might have given us duplicate cookies. |
1516 EnsureCookiesMapIsValid(); | 1550 EnsureCookiesMapIsValid(); |
erikwright (departed)
2011/09/15 20:56:27
Add a comment that we will validate "priority load
guohui
2011/09/16 18:21:09
Done.
| |
1517 } | 1551 } |
1518 | 1552 |
1519 void CookieMonster::InvokeQueue() { | 1553 void CookieMonster::InvokeQueue() { |
1520 while (true) { | 1554 while (true) { |
1521 scoped_refptr<CookieMonsterTask> request_task; | 1555 scoped_refptr<CookieMonsterTask> request_task; |
1522 { | 1556 { |
1523 base::AutoLock autolock(lock_); | 1557 base::AutoLock autolock(lock_); |
1524 if (queue_.empty()) { | 1558 if (queue_.empty()) { |
1525 loaded_ = true; | 1559 loaded_ = true; |
1526 break; | 1560 break; |
(...skipping 1237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2764 std::string CookieMonster::CanonicalCookie::DebugString() const { | 2798 std::string CookieMonster::CanonicalCookie::DebugString() const { |
2765 return base::StringPrintf( | 2799 return base::StringPrintf( |
2766 "name: %s value: %s domain: %s path: %s creation: %" | 2800 "name: %s value: %s domain: %s path: %s creation: %" |
2767 PRId64, | 2801 PRId64, |
2768 name_.c_str(), value_.c_str(), | 2802 name_.c_str(), value_.c_str(), |
2769 domain_.c_str(), path_.c_str(), | 2803 domain_.c_str(), path_.c_str(), |
2770 static_cast<int64>(creation_date_.ToTimeT())); | 2804 static_cast<int64>(creation_date_.ToTimeT())); |
2771 } | 2805 } |
2772 | 2806 |
2773 } // namespace | 2807 } // namespace |
OLD | NEW |