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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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/format_macros.h" | 52 #include "base/format_macros.h" |
erikwright (departed)
2011/09/17 02:22:10
#include "base/callback.h" for Closure
guohui
2011/09/20 18:34:46
Done.
| |
53 #include "base/logging.h" | 53 #include "base/logging.h" |
54 #include "base/memory/scoped_ptr.h" | 54 #include "base/memory/scoped_ptr.h" |
55 #include "base/message_loop.h" | 55 #include "base/message_loop.h" |
56 #include "base/message_loop_proxy.h" | 56 #include "base/message_loop_proxy.h" |
57 #include "base/metrics/histogram.h" | 57 #include "base/metrics/histogram.h" |
58 #include "base/string_tokenizer.h" | 58 #include "base/string_tokenizer.h" |
59 #include "base/string_util.h" | 59 #include "base/string_util.h" |
60 #include "base/stringprintf.h" | 60 #include "base/stringprintf.h" |
61 #include "googleurl/src/gurl.h" | 61 #include "googleurl/src/gurl.h" |
62 #include "googleurl/src/url_canon.h" | 62 #include "googleurl/src/url_canon.h" |
(...skipping 923 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(); |
Randy Smith (Not in Mondays)
2011/09/19 00:58:57
(Not fron this CL, but worrisome to me.) Historic
| |
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(); | |
Randy Smith (Not in Mondays)
2011/09/19 00:58:57
See above.
| |
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 task_item->Run(); | |
1138 } | |
1139 | |
1116 bool CookieMonster::SetCookieWithDetails( | 1140 bool CookieMonster::SetCookieWithDetails( |
1117 const GURL& url, const std::string& name, const std::string& value, | 1141 const GURL& url, const std::string& name, const std::string& value, |
1118 const std::string& domain, const std::string& path, | 1142 const std::string& domain, const std::string& path, |
1119 const base::Time& expiration_time, bool secure, bool http_only) { | 1143 const base::Time& expiration_time, bool secure, bool http_only) { |
1120 base::AutoLock autolock(lock_); | 1144 base::AutoLock autolock(lock_); |
1121 | 1145 |
1122 if (!HasCookieableScheme(url)) | 1146 if (!HasCookieableScheme(url)) |
1123 return false; | 1147 return false; |
1124 | 1148 |
1125 Time creation_time = CurrentTime(); | 1149 Time creation_time = CurrentTime(); |
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1463 | 1487 |
1464 void CookieMonster::OnLoaded(TimeTicks beginning_time, | 1488 void CookieMonster::OnLoaded(TimeTicks beginning_time, |
1465 const std::vector<CanonicalCookie*>& cookies) { | 1489 const std::vector<CanonicalCookie*>& cookies) { |
1466 StoreLoadedCookies(cookies); | 1490 StoreLoadedCookies(cookies); |
1467 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); | 1491 histogram_time_load_->AddTime(TimeTicks::Now() - beginning_time); |
1468 | 1492 |
1469 // Invoke the task queue of cookie request. | 1493 // Invoke the task queue of cookie request. |
1470 InvokeQueue(); | 1494 InvokeQueue(); |
1471 } | 1495 } |
1472 | 1496 |
1497 void CookieMonster::OnKeyLoaded( | |
1498 const base::Closure& request_task, const std::string& key, | |
1499 const std::vector<CanonicalCookie*>& cookies) { | |
1500 StoreLoadedCookies(cookies); | |
Randy Smith (Not in Mondays)
2011/09/19 00:58:57
Would you put in a comment that this function does
guohui
2011/09/20 18:34:46
Done.
| |
1501 { | |
1502 base::AutoLock autolock(lock_); | |
1503 keys_loaded_.insert(key); | |
1504 } | |
1505 request_task.Run(); | |
1506 } | |
1507 | |
1473 void CookieMonster::StoreLoadedCookies( | 1508 void CookieMonster::StoreLoadedCookies( |
1474 const std::vector<CanonicalCookie*>& cookies) { | 1509 const std::vector<CanonicalCookie*>& cookies) { |
1475 // Initialize the store and sync in any saved persistent cookies. We don't | 1510 // 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, | 1511 // care if it's expired, insert it so it can be garbage collected, removed, |
1477 // and sync'd. | 1512 // and sync'd. |
1478 base::AutoLock autolock(lock_); | 1513 base::AutoLock autolock(lock_); |
1479 | 1514 |
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(); | 1515 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); |
1489 it != cookies.end(); ++it) { | 1516 it != cookies.end(); ++it) { |
1490 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue(); | 1517 int64 cookie_creation_time = (*it)->CreationDate().ToInternalValue(); |
1491 | 1518 |
1492 if (creation_times.insert(cookie_creation_time).second) { | 1519 if (creation_times_.insert(cookie_creation_time).second) { |
1493 InternalInsertCookie(GetKey((*it)->Domain()), *it, false); | 1520 InternalInsertCookie(GetKey((*it)->Domain()), *it, false); |
1494 const Time cookie_access_time((*it)->LastAccessDate()); | 1521 const Time cookie_access_time((*it)->LastAccessDate()); |
1495 if (earliest_access_time.is_null() || | 1522 if (earliest_access_time_.is_null() || |
1496 cookie_access_time < earliest_access_time) | 1523 cookie_access_time < earliest_access_time_) |
1497 earliest_access_time = cookie_access_time; | 1524 earliest_access_time_ = cookie_access_time; |
1498 } else { | 1525 } else { |
1499 LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation " | 1526 LOG(ERROR) << base::StringPrintf("Found cookies with duplicate creation " |
1500 "times in backing store: " | 1527 "times in backing store: " |
1501 "{name='%s', domain='%s', path='%s'}", | 1528 "{name='%s', domain='%s', path='%s'}", |
1502 (*it)->Name().c_str(), | 1529 (*it)->Name().c_str(), |
1503 (*it)->Domain().c_str(), | 1530 (*it)->Domain().c_str(), |
1504 (*it)->Path().c_str()); | 1531 (*it)->Path().c_str()); |
1505 // We've been given ownership of the cookie and are throwing it | 1532 // We've been given ownership of the cookie and are throwing it |
1506 // away; reclaim the space. | 1533 // away; reclaim the space. |
1507 delete (*it); | 1534 delete (*it); |
1508 } | 1535 } |
1509 } | 1536 } |
1510 earliest_access_time_= earliest_access_time; | |
1511 | 1537 |
1512 // After importing cookies from the PersistentCookieStore, verify that | 1538 // After importing cookies from the PersistentCookieStore, verify that |
1513 // none of our other constraints are violated. | 1539 // none of our other constraints are violated. |
1514 // | |
1515 // In particular, the backing store might have given us duplicate cookies. | 1540 // In particular, the backing store might have given us duplicate cookies. |
1541 // "Priority loaded" cookies will be validated more than once, but this is OK | |
1542 // since they are expected to be much fewer than total DB. | |
1516 EnsureCookiesMapIsValid(); | 1543 EnsureCookiesMapIsValid(); |
1517 } | 1544 } |
1518 | 1545 |
1519 void CookieMonster::InvokeQueue() { | 1546 void CookieMonster::InvokeQueue() { |
1520 while (true) { | 1547 while (true) { |
1521 scoped_refptr<CookieMonsterTask> request_task; | 1548 scoped_refptr<CookieMonsterTask> request_task; |
1522 { | 1549 { |
1523 base::AutoLock autolock(lock_); | 1550 base::AutoLock autolock(lock_); |
1524 if (queue_.empty()) { | 1551 if (queue_.empty()) { |
Randy Smith (Not in Mondays)
2011/09/19 00:58:57
Are we using this queue anymore? Or are we bounci
guohui
2011/09/20 18:34:46
Yes, this queue is still used for cookie tasks tha
| |
1525 loaded_ = true; | 1552 loaded_ = true; |
1553 creation_times_.clear(); | |
1554 keys_loaded_.clear(); | |
1526 break; | 1555 break; |
1527 } | 1556 } |
1528 request_task = queue_.front(); | 1557 request_task = queue_.front(); |
1529 queue_.pop(); | 1558 queue_.pop(); |
1530 } | 1559 } |
1531 request_task->Run(); | 1560 request_task->Run(); |
1532 } | 1561 } |
1533 } | 1562 } |
1534 | 1563 |
1535 void CookieMonster::EnsureCookiesMapIsValid() { | 1564 void CookieMonster::EnsureCookiesMapIsValid() { |
(...skipping 1228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2764 std::string CookieMonster::CanonicalCookie::DebugString() const { | 2793 std::string CookieMonster::CanonicalCookie::DebugString() const { |
2765 return base::StringPrintf( | 2794 return base::StringPrintf( |
2766 "name: %s value: %s domain: %s path: %s creation: %" | 2795 "name: %s value: %s domain: %s path: %s creation: %" |
2767 PRId64, | 2796 PRId64, |
2768 name_.c_str(), value_.c_str(), | 2797 name_.c_str(), value_.c_str(), |
2769 domain_.c_str(), path_.c_str(), | 2798 domain_.c_str(), path_.c_str(), |
2770 static_cast<int64>(creation_date_.ToTimeT())); | 2799 static_cast<int64>(creation_date_.ToTimeT())); |
2771 } | 2800 } |
2772 | 2801 |
2773 } // namespace | 2802 } // namespace |
OLD | NEW |