Chromium Code Reviews| 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 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 const CookieStore::CookieChangedCallback& callback, | 348 const CookieStore::CookieChangedCallback& callback, |
| 349 const CanonicalCookie& cookie, | 349 const CanonicalCookie& cookie, |
| 350 bool removed) { | 350 bool removed) { |
| 351 proxy->PostTask(FROM_HERE, base::Bind(callback, cookie, removed)); | 351 proxy->PostTask(FROM_HERE, base::Bind(callback, cookie, removed)); |
| 352 } | 352 } |
| 353 | 353 |
| 354 } // namespace | 354 } // namespace |
| 355 | 355 |
| 356 CookieMonster::CookieMonster(PersistentCookieStore* store, | 356 CookieMonster::CookieMonster(PersistentCookieStore* store, |
| 357 CookieMonsterDelegate* delegate) | 357 CookieMonsterDelegate* delegate) |
| 358 : initialized_(false), | 358 : CookieMonster(store, delegate, kDefaultAccessUpdateThresholdSeconds) { |
|
mmenke
2016/01/20 22:14:03
Other than this and the change in the header, all
| |
| 359 started_fetching_all_cookies_(false), | |
| 360 finished_fetching_all_cookies_(false), | |
| 361 fetch_strategy_(kUnknownFetch), | |
| 362 store_(store), | |
| 363 last_access_threshold_( | |
| 364 TimeDelta::FromSeconds(kDefaultAccessUpdateThresholdSeconds)), | |
| 365 delegate_(delegate), | |
| 366 last_statistic_record_time_(Time::Now()), | |
| 367 keep_expired_cookies_(false), | |
| 368 persist_session_cookies_(false) { | |
| 369 InitializeHistograms(); | 359 InitializeHistograms(); |
| 370 SetDefaultCookieableSchemes(); | 360 SetDefaultCookieableSchemes(); |
| 371 } | 361 } |
| 372 | 362 |
| 373 CookieMonster::CookieMonster(PersistentCookieStore* store, | 363 CookieMonster::CookieMonster(PersistentCookieStore* store, |
| 374 CookieMonsterDelegate* delegate, | 364 CookieMonsterDelegate* delegate, |
| 375 int last_access_threshold_milliseconds) | 365 int last_access_threshold_milliseconds) |
| 376 : initialized_(false), | 366 : initialized_(false), |
| 377 started_fetching_all_cookies_(false), | 367 started_fetching_all_cookies_(false), |
| 378 finished_fetching_all_cookies_(false), | 368 finished_fetching_all_cookies_(false), |
| 379 fetch_strategy_(kUnknownFetch), | 369 fetch_strategy_(kUnknownFetch), |
| 380 store_(store), | 370 store_(store), |
| 381 last_access_threshold_(base::TimeDelta::FromMilliseconds( | 371 last_access_threshold_(base::TimeDelta::FromMilliseconds( |
| 382 last_access_threshold_milliseconds)), | 372 last_access_threshold_milliseconds)), |
| 383 delegate_(delegate), | 373 delegate_(delegate), |
| 384 last_statistic_record_time_(base::Time::Now()), | 374 last_statistic_record_time_(base::Time::Now()), |
| 385 keep_expired_cookies_(false), | 375 keep_expired_cookies_(false), |
| 386 persist_session_cookies_(false) { | 376 persist_session_cookies_(false) { |
| 387 InitializeHistograms(); | 377 InitializeHistograms(); |
| 388 SetDefaultCookieableSchemes(); | 378 SetDefaultCookieableSchemes(); |
| 389 } | 379 } |
| 390 | 380 |
| 381 bool CookieMonster::ImportCookies(const CookieList& list) { | |
| 382 base::AutoLock autolock(lock_); | |
| 383 MarkCookieStoreAsInitialized(); | |
| 384 if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) | |
| 385 FetchAllCookiesIfNecessary(); | |
| 386 for (CookieList::const_iterator iter = list.begin(); iter != list.end(); | |
| 387 ++iter) { | |
| 388 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(*iter)); | |
| 389 CookieOptions options; | |
| 390 options.set_include_httponly(); | |
| 391 options.set_include_first_party_only_cookies(); | |
| 392 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), options)) | |
| 393 return false; | |
| 394 } | |
| 395 return true; | |
| 396 } | |
| 397 | |
| 391 // Task classes for queueing the coming request. | 398 // Task classes for queueing the coming request. |
| 392 | 399 |
| 393 class CookieMonster::CookieMonsterTask | 400 class CookieMonster::CookieMonsterTask |
| 394 : public base::RefCountedThreadSafe<CookieMonsterTask> { | 401 : public base::RefCountedThreadSafe<CookieMonsterTask> { |
| 395 public: | 402 public: |
| 396 // Runs the task and invokes the client callback on the thread that | 403 // Runs the task and invokes the client callback on the thread that |
| 397 // originally constructed the task. | 404 // originally constructed the task. |
| 398 virtual void Run() = 0; | 405 virtual void Run() = 0; |
| 399 | 406 |
| 400 protected: | 407 protected: |
| (...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 975 void CookieMonster::GetAllCookiesForURLWithOptionsAsync( | 982 void CookieMonster::GetAllCookiesForURLWithOptionsAsync( |
| 976 const GURL& url, | 983 const GURL& url, |
| 977 const CookieOptions& options, | 984 const CookieOptions& options, |
| 978 const GetCookieListCallback& callback) { | 985 const GetCookieListCallback& callback) { |
| 979 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = | 986 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = |
| 980 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); | 987 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); |
| 981 | 988 |
| 982 DoCookieTaskForURL(task, url); | 989 DoCookieTaskForURL(task, url); |
| 983 } | 990 } |
| 984 | 991 |
| 985 void CookieMonster::GetAllCookiesForURLAsync( | |
| 986 const GURL& url, | |
| 987 const GetCookieListCallback& callback) { | |
| 988 CookieOptions options; | |
| 989 options.set_include_httponly(); | |
| 990 options.set_include_first_party_only_cookies(); | |
| 991 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = | |
| 992 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); | |
| 993 | |
| 994 DoCookieTaskForURL(task, url); | |
| 995 } | |
| 996 | |
| 997 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) { | 992 void CookieMonster::DeleteAllAsync(const DeleteCallback& callback) { |
| 998 scoped_refptr<DeleteAllTask> task = new DeleteAllTask(this, callback); | 993 scoped_refptr<DeleteAllTask> task = new DeleteAllTask(this, callback); |
| 999 | 994 |
| 1000 DoCookieTask(task); | 995 DoCookieTask(task); |
| 1001 } | 996 } |
| 1002 | 997 |
| 1003 void CookieMonster::DeleteAllCreatedBetweenAsync( | |
| 1004 const Time& delete_begin, | |
| 1005 const Time& delete_end, | |
| 1006 const DeleteCallback& callback) { | |
| 1007 scoped_refptr<DeleteAllCreatedBetweenTask> task = | |
| 1008 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, callback); | |
| 1009 | |
| 1010 DoCookieTask(task); | |
| 1011 } | |
| 1012 | |
| 1013 void CookieMonster::DeleteAllCreatedBetweenForHostAsync( | |
| 1014 const Time delete_begin, | |
| 1015 const Time delete_end, | |
| 1016 const GURL& url, | |
| 1017 const DeleteCallback& callback) { | |
| 1018 scoped_refptr<DeleteAllCreatedBetweenForHostTask> task = | |
| 1019 new DeleteAllCreatedBetweenForHostTask(this, delete_begin, delete_end, | |
| 1020 url, callback); | |
| 1021 | |
| 1022 DoCookieTaskForURL(task, url); | |
| 1023 } | |
| 1024 | |
| 1025 void CookieMonster::DeleteAllForHostAsync(const GURL& url, | 998 void CookieMonster::DeleteAllForHostAsync(const GURL& url, |
| 1026 const DeleteCallback& callback) { | 999 const DeleteCallback& callback) { |
| 1027 scoped_refptr<DeleteAllForHostTask> task = | 1000 scoped_refptr<DeleteAllForHostTask> task = |
| 1028 new DeleteAllForHostTask(this, url, callback); | 1001 new DeleteAllForHostTask(this, url, callback); |
| 1029 | 1002 |
| 1030 DoCookieTaskForURL(task, url); | 1003 DoCookieTaskForURL(task, url); |
| 1031 } | 1004 } |
| 1032 | 1005 |
| 1033 void CookieMonster::DeleteCanonicalCookieAsync( | 1006 void CookieMonster::DeleteCanonicalCookieAsync( |
| 1034 const CanonicalCookie& cookie, | 1007 const CanonicalCookie& cookie, |
| 1035 const DeleteCookieCallback& callback) { | 1008 const DeleteCookieCallback& callback) { |
| 1036 scoped_refptr<DeleteCanonicalCookieTask> task = | 1009 scoped_refptr<DeleteCanonicalCookieTask> task = |
| 1037 new DeleteCanonicalCookieTask(this, cookie, callback); | 1010 new DeleteCanonicalCookieTask(this, cookie, callback); |
| 1038 | 1011 |
| 1039 DoCookieTask(task); | 1012 DoCookieTask(task); |
| 1040 } | 1013 } |
| 1041 | 1014 |
| 1015 void CookieMonster::FlushStore(const base::Closure& callback) { | |
| 1016 base::AutoLock autolock(lock_); | |
| 1017 if (initialized_ && store_.get()) | |
| 1018 store_->Flush(callback); | |
| 1019 else if (!callback.is_null()) | |
| 1020 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | |
| 1021 } | |
| 1022 | |
| 1042 void CookieMonster::SetAllCookiesAsync(const CookieList& list, | 1023 void CookieMonster::SetAllCookiesAsync(const CookieList& list, |
| 1043 const SetCookiesCallback& callback) { | 1024 const SetCookiesCallback& callback) { |
| 1044 scoped_refptr<SetAllCookiesTask> task = | 1025 scoped_refptr<SetAllCookiesTask> task = |
| 1045 new SetAllCookiesTask(this, list, callback); | 1026 new SetAllCookiesTask(this, list, callback); |
| 1046 DoCookieTask(task); | 1027 DoCookieTask(task); |
| 1047 } | 1028 } |
| 1048 | 1029 |
| 1049 void CookieMonster::SetCookieWithOptionsAsync( | 1030 void CookieMonster::SetCookieWithOptionsAsync( |
| 1050 const GURL& url, | 1031 const GURL& url, |
| 1051 const std::string& cookie_line, | 1032 const std::string& cookie_line, |
| 1052 const CookieOptions& options, | 1033 const CookieOptions& options, |
| 1053 const SetCookiesCallback& callback) { | 1034 const SetCookiesCallback& callback) { |
| 1054 scoped_refptr<SetCookieWithOptionsTask> task = | 1035 scoped_refptr<SetCookieWithOptionsTask> task = |
| 1055 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback); | 1036 new SetCookieWithOptionsTask(this, url, cookie_line, options, callback); |
| 1056 | 1037 |
| 1057 DoCookieTaskForURL(task, url); | 1038 DoCookieTaskForURL(task, url); |
| 1058 } | 1039 } |
| 1059 | 1040 |
| 1060 void CookieMonster::GetCookiesWithOptionsAsync( | 1041 void CookieMonster::GetCookiesWithOptionsAsync( |
| 1061 const GURL& url, | 1042 const GURL& url, |
| 1062 const CookieOptions& options, | 1043 const CookieOptions& options, |
| 1063 const GetCookiesCallback& callback) { | 1044 const GetCookiesCallback& callback) { |
| 1064 scoped_refptr<GetCookiesWithOptionsTask> task = | 1045 scoped_refptr<GetCookiesWithOptionsTask> task = |
| 1065 new GetCookiesWithOptionsTask(this, url, options, callback); | 1046 new GetCookiesWithOptionsTask(this, url, options, callback); |
| 1066 | 1047 |
| 1067 DoCookieTaskForURL(task, url); | 1048 DoCookieTaskForURL(task, url); |
| 1068 } | 1049 } |
| 1069 | 1050 |
| 1051 void CookieMonster::GetAllCookiesForURLAsync( | |
| 1052 const GURL& url, | |
| 1053 const GetCookieListCallback& callback) { | |
| 1054 CookieOptions options; | |
| 1055 options.set_include_httponly(); | |
| 1056 options.set_include_first_party_only_cookies(); | |
| 1057 scoped_refptr<GetAllCookiesForURLWithOptionsTask> task = | |
| 1058 new GetAllCookiesForURLWithOptionsTask(this, url, options, callback); | |
| 1059 | |
| 1060 DoCookieTaskForURL(task, url); | |
| 1061 } | |
| 1062 | |
| 1070 void CookieMonster::DeleteCookieAsync(const GURL& url, | 1063 void CookieMonster::DeleteCookieAsync(const GURL& url, |
| 1071 const std::string& cookie_name, | 1064 const std::string& cookie_name, |
| 1072 const base::Closure& callback) { | 1065 const base::Closure& callback) { |
| 1073 scoped_refptr<DeleteCookieTask> task = | 1066 scoped_refptr<DeleteCookieTask> task = |
| 1074 new DeleteCookieTask(this, url, cookie_name, callback); | 1067 new DeleteCookieTask(this, url, cookie_name, callback); |
| 1075 | 1068 |
| 1076 DoCookieTaskForURL(task, url); | 1069 DoCookieTaskForURL(task, url); |
| 1077 } | 1070 } |
| 1078 | 1071 |
| 1072 void CookieMonster::DeleteAllCreatedBetweenAsync( | |
| 1073 const Time& delete_begin, | |
| 1074 const Time& delete_end, | |
| 1075 const DeleteCallback& callback) { | |
| 1076 scoped_refptr<DeleteAllCreatedBetweenTask> task = | |
| 1077 new DeleteAllCreatedBetweenTask(this, delete_begin, delete_end, callback); | |
| 1078 | |
| 1079 DoCookieTask(task); | |
| 1080 } | |
| 1081 | |
| 1082 void CookieMonster::DeleteAllCreatedBetweenForHostAsync( | |
| 1083 const Time delete_begin, | |
| 1084 const Time delete_end, | |
| 1085 const GURL& url, | |
| 1086 const DeleteCallback& callback) { | |
| 1087 scoped_refptr<DeleteAllCreatedBetweenForHostTask> task = | |
| 1088 new DeleteAllCreatedBetweenForHostTask(this, delete_begin, delete_end, | |
| 1089 url, callback); | |
| 1090 | |
| 1091 DoCookieTaskForURL(task, url); | |
| 1092 } | |
| 1093 | |
| 1079 void CookieMonster::DeleteSessionCookiesAsync( | 1094 void CookieMonster::DeleteSessionCookiesAsync( |
| 1080 const CookieStore::DeleteCallback& callback) { | 1095 const CookieStore::DeleteCallback& callback) { |
| 1081 scoped_refptr<DeleteSessionCookiesTask> task = | 1096 scoped_refptr<DeleteSessionCookiesTask> task = |
| 1082 new DeleteSessionCookiesTask(this, callback); | 1097 new DeleteSessionCookiesTask(this, callback); |
| 1083 | 1098 |
| 1084 DoCookieTask(task); | 1099 DoCookieTask(task); |
| 1085 } | 1100 } |
| 1086 | 1101 |
| 1087 void CookieMonster::DoCookieTask( | 1102 CookieMonster* CookieMonster::GetCookieMonster() { |
| 1088 const scoped_refptr<CookieMonsterTask>& task_item) { | 1103 return this; |
| 1089 { | 1104 } |
| 1090 base::AutoLock autolock(lock_); | 1105 |
| 1091 MarkCookieStoreAsInitialized(); | 1106 void CookieMonster::SetCookieableSchemes(const char* const schemes[], |
| 1092 FetchAllCookiesIfNecessary(); | 1107 size_t num_schemes) { |
| 1093 if (!finished_fetching_all_cookies_ && store_.get()) { | 1108 base::AutoLock autolock(lock_); |
| 1094 tasks_pending_.push(task_item); | 1109 |
| 1095 return; | 1110 // Calls to this method will have no effect if made after a WebView or |
| 1096 } | 1111 // CookieManager instance has been created. |
| 1112 if (initialized_) { | |
| 1113 return; | |
| 1097 } | 1114 } |
| 1098 | 1115 |
| 1099 task_item->Run(); | 1116 cookieable_schemes_.clear(); |
| 1117 cookieable_schemes_.insert(cookieable_schemes_.end(), schemes, | |
| 1118 schemes + num_schemes); | |
| 1100 } | 1119 } |
| 1101 | 1120 |
| 1102 void CookieMonster::DoCookieTaskForURL( | 1121 void CookieMonster::SetKeepExpiredCookies() { |
| 1103 const scoped_refptr<CookieMonsterTask>& task_item, | 1122 keep_expired_cookies_ = true; |
| 1104 const GURL& url) { | 1123 } |
| 1105 { | 1124 |
| 1106 base::AutoLock autolock(lock_); | 1125 void CookieMonster::SetForceKeepSessionState() { |
| 1107 MarkCookieStoreAsInitialized(); | 1126 if (store_.get()) { |
| 1108 if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) | 1127 store_->SetForceKeepSessionState(); |
| 1109 FetchAllCookiesIfNecessary(); | |
| 1110 // If cookies for the requested domain key (eTLD+1) have been loaded from DB | |
| 1111 // then run the task, otherwise load from DB. | |
| 1112 if (!finished_fetching_all_cookies_ && store_.get()) { | |
| 1113 // Checks if the domain key has been loaded. | |
| 1114 std::string key( | |
| 1115 cookie_util::GetEffectiveDomain(url.scheme(), url.host())); | |
| 1116 if (keys_loaded_.find(key) == keys_loaded_.end()) { | |
| 1117 std::map<std::string, | |
| 1118 std::deque<scoped_refptr<CookieMonsterTask>>>::iterator it = | |
| 1119 tasks_pending_for_key_.find(key); | |
| 1120 if (it == tasks_pending_for_key_.end()) { | |
| 1121 store_->LoadCookiesForKey( | |
| 1122 key, base::Bind(&CookieMonster::OnKeyLoaded, this, key)); | |
| 1123 it = tasks_pending_for_key_ | |
| 1124 .insert(std::make_pair( | |
| 1125 key, std::deque<scoped_refptr<CookieMonsterTask>>())) | |
| 1126 .first; | |
| 1127 } | |
| 1128 it->second.push_back(task_item); | |
| 1129 return; | |
| 1130 } | |
| 1131 } | |
| 1132 } | 1128 } |
| 1133 task_item->Run(); | 1129 } |
| 1130 | |
| 1131 // This function must be called before the CookieMonster is used. | |
| 1132 void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies) { | |
| 1133 DCHECK(!initialized_); | |
| 1134 persist_session_cookies_ = persist_session_cookies; | |
| 1135 } | |
| 1136 | |
| 1137 bool CookieMonster::IsCookieableScheme(const std::string& scheme) { | |
| 1138 base::AutoLock autolock(lock_); | |
| 1139 | |
| 1140 return std::find(cookieable_schemes_.begin(), cookieable_schemes_.end(), | |
| 1141 scheme) != cookieable_schemes_.end(); | |
| 1142 } | |
| 1143 | |
| 1144 // Note: file must be the last scheme. | |
| 1145 const char* const CookieMonster::kDefaultCookieableSchemes[] = {"http", | |
| 1146 "https", | |
| 1147 "ws", | |
| 1148 "wss", | |
| 1149 "file"}; | |
| 1150 const int CookieMonster::kDefaultCookieableSchemesCount = | |
| 1151 arraysize(kDefaultCookieableSchemes); | |
| 1152 | |
| 1153 scoped_ptr<CookieStore::CookieChangedSubscription> | |
| 1154 CookieMonster::AddCallbackForCookie(const GURL& gurl, | |
| 1155 const std::string& name, | |
| 1156 const CookieChangedCallback& callback) { | |
| 1157 base::AutoLock autolock(lock_); | |
| 1158 std::pair<GURL, std::string> key(gurl, name); | |
| 1159 if (hook_map_.count(key) == 0) | |
| 1160 hook_map_[key] = make_linked_ptr(new CookieChangedCallbackList()); | |
| 1161 return hook_map_[key]->Add( | |
| 1162 base::Bind(&RunAsync, base::ThreadTaskRunnerHandle::Get(), callback)); | |
| 1163 } | |
| 1164 | |
| 1165 #if defined(OS_ANDROID) | |
| 1166 void CookieMonster::SetEnableFileScheme(bool accept) { | |
| 1167 // This assumes "file" is always at the end of the array. See the comment | |
| 1168 // above kDefaultCookieableSchemes. | |
| 1169 // | |
| 1170 // TODO(mkwst): We're keeping this method around to support the | |
| 1171 // 'CookieManager::setAcceptFileSchemeCookies' method on Android's WebView; | |
| 1172 // if/when we can deprecate and remove that method, we can remove this one | |
| 1173 // as well. Until then, we'll just ensure that the method has no effect on | |
| 1174 // non-android systems. | |
| 1175 int num_schemes = accept ? kDefaultCookieableSchemesCount | |
| 1176 : kDefaultCookieableSchemesCount - 1; | |
| 1177 | |
| 1178 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes); | |
| 1179 } | |
| 1180 #endif | |
| 1181 | |
| 1182 CookieMonster::~CookieMonster() { | |
| 1183 DeleteAll(false); | |
| 1134 } | 1184 } |
| 1135 | 1185 |
| 1136 bool CookieMonster::SetCookieWithDetails(const GURL& url, | 1186 bool CookieMonster::SetCookieWithDetails(const GURL& url, |
| 1137 const std::string& name, | 1187 const std::string& name, |
| 1138 const std::string& value, | 1188 const std::string& value, |
| 1139 const std::string& domain, | 1189 const std::string& domain, |
| 1140 const std::string& path, | 1190 const std::string& path, |
| 1141 const base::Time& expiration_time, | 1191 const base::Time& expiration_time, |
| 1142 bool secure, | 1192 bool secure, |
| 1143 bool http_only, | 1193 bool http_only, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 1162 return false; | 1212 return false; |
| 1163 | 1213 |
| 1164 CookieOptions options; | 1214 CookieOptions options; |
| 1165 options.set_include_httponly(); | 1215 options.set_include_httponly(); |
| 1166 options.set_include_first_party_only_cookies(); | 1216 options.set_include_first_party_only_cookies(); |
| 1167 if (enforce_strict_secure) | 1217 if (enforce_strict_secure) |
| 1168 options.set_enforce_strict_secure(); | 1218 options.set_enforce_strict_secure(); |
| 1169 return SetCanonicalCookie(&cc, creation_time, options); | 1219 return SetCanonicalCookie(&cc, creation_time, options); |
| 1170 } | 1220 } |
| 1171 | 1221 |
| 1172 bool CookieMonster::ImportCookies(const CookieList& list) { | |
| 1173 base::AutoLock autolock(lock_); | |
| 1174 MarkCookieStoreAsInitialized(); | |
| 1175 if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) | |
| 1176 FetchAllCookiesIfNecessary(); | |
| 1177 for (CookieList::const_iterator iter = list.begin(); iter != list.end(); | |
| 1178 ++iter) { | |
| 1179 scoped_ptr<CanonicalCookie> cookie(new CanonicalCookie(*iter)); | |
| 1180 CookieOptions options; | |
| 1181 options.set_include_httponly(); | |
| 1182 options.set_include_first_party_only_cookies(); | |
| 1183 if (!SetCanonicalCookie(&cookie, cookie->CreationDate(), options)) | |
| 1184 return false; | |
| 1185 } | |
| 1186 return true; | |
| 1187 } | |
| 1188 | |
| 1189 CookieList CookieMonster::GetAllCookies() { | 1222 CookieList CookieMonster::GetAllCookies() { |
| 1190 base::AutoLock autolock(lock_); | 1223 base::AutoLock autolock(lock_); |
| 1191 | 1224 |
| 1192 // This function is being called to scrape the cookie list for management UI | 1225 // This function is being called to scrape the cookie list for management UI |
| 1193 // or similar. We shouldn't show expired cookies in this list since it will | 1226 // or similar. We shouldn't show expired cookies in this list since it will |
| 1194 // just be confusing to users, and this function is called rarely enough (and | 1227 // just be confusing to users, and this function is called rarely enough (and |
| 1195 // is already slow enough) that it's OK to take the time to garbage collect | 1228 // is already slow enough) that it's OK to take the time to garbage collect |
| 1196 // the expired cookies now. | 1229 // the expired cookies now. |
| 1197 // | 1230 // |
| 1198 // Note that this does not prune cookies to be below our limits (if we've | 1231 // Note that this does not prune cookies to be below our limits (if we've |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1266 (delete_end.is_null() || cc->CreationDate() < delete_end)) { | 1299 (delete_end.is_null() || cc->CreationDate() < delete_end)) { |
| 1267 InternalDeleteCookie(curit, true, /*sync_to_store*/ | 1300 InternalDeleteCookie(curit, true, /*sync_to_store*/ |
| 1268 DELETE_COOKIE_EXPLICIT); | 1301 DELETE_COOKIE_EXPLICIT); |
| 1269 ++num_deleted; | 1302 ++num_deleted; |
| 1270 } | 1303 } |
| 1271 } | 1304 } |
| 1272 | 1305 |
| 1273 return num_deleted; | 1306 return num_deleted; |
| 1274 } | 1307 } |
| 1275 | 1308 |
| 1309 int CookieMonster::DeleteAllForHost(const GURL& url) { | |
| 1310 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url); | |
| 1311 } | |
| 1312 | |
| 1276 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin, | 1313 int CookieMonster::DeleteAllCreatedBetweenForHost(const Time delete_begin, |
| 1277 const Time delete_end, | 1314 const Time delete_end, |
| 1278 const GURL& url) { | 1315 const GURL& url) { |
| 1279 base::AutoLock autolock(lock_); | 1316 base::AutoLock autolock(lock_); |
| 1280 | 1317 |
| 1281 if (!HasCookieableScheme(url)) | 1318 if (!HasCookieableScheme(url)) |
| 1282 return 0; | 1319 return 0; |
| 1283 | 1320 |
| 1284 const std::string host(url.host()); | 1321 const std::string host(url.host()); |
| 1285 | 1322 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 1301 // Time::Max() is confusing. | 1338 // Time::Max() is confusing. |
| 1302 (delete_end.is_null() || cc->CreationDate() < delete_end)) { | 1339 (delete_end.is_null() || cc->CreationDate() < delete_end)) { |
| 1303 num_deleted++; | 1340 num_deleted++; |
| 1304 | 1341 |
| 1305 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); | 1342 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); |
| 1306 } | 1343 } |
| 1307 } | 1344 } |
| 1308 return num_deleted; | 1345 return num_deleted; |
| 1309 } | 1346 } |
| 1310 | 1347 |
| 1311 int CookieMonster::DeleteAllForHost(const GURL& url) { | |
| 1312 return DeleteAllCreatedBetweenForHost(Time(), Time::Max(), url); | |
| 1313 } | |
| 1314 | 1348 |
| 1315 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { | 1349 bool CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { |
| 1316 base::AutoLock autolock(lock_); | 1350 base::AutoLock autolock(lock_); |
| 1317 | 1351 |
| 1318 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); | 1352 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); |
| 1319 its.first != its.second; ++its.first) { | 1353 its.first != its.second; ++its.first) { |
| 1320 // The creation date acts as our unique index... | 1354 // The creation date acts as our unique index... |
| 1321 if (its.first->second->CreationDate() == cookie.CreationDate()) { | 1355 if (its.first->second->CreationDate() == cookie.CreationDate()) { |
| 1322 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); | 1356 InternalDeleteCookie(its.first, true, DELETE_COOKIE_EXPLICIT); |
| 1323 return true; | 1357 return true; |
| 1324 } | 1358 } |
| 1325 } | 1359 } |
| 1326 return false; | 1360 return false; |
| 1327 } | 1361 } |
| 1328 | 1362 |
| 1329 void CookieMonster::SetCookieableSchemes(const char* const schemes[], | |
| 1330 size_t num_schemes) { | |
| 1331 base::AutoLock autolock(lock_); | |
| 1332 | |
| 1333 // Calls to this method will have no effect if made after a WebView or | |
| 1334 // CookieManager instance has been created. | |
| 1335 if (initialized_) { | |
| 1336 return; | |
| 1337 } | |
| 1338 | |
| 1339 cookieable_schemes_.clear(); | |
| 1340 cookieable_schemes_.insert(cookieable_schemes_.end(), schemes, | |
| 1341 schemes + num_schemes); | |
| 1342 } | |
| 1343 | |
| 1344 void CookieMonster::SetKeepExpiredCookies() { | |
| 1345 keep_expired_cookies_ = true; | |
| 1346 } | |
| 1347 | |
| 1348 void CookieMonster::FlushStore(const base::Closure& callback) { | |
| 1349 base::AutoLock autolock(lock_); | |
| 1350 if (initialized_ && store_.get()) | |
| 1351 store_->Flush(callback); | |
| 1352 else if (!callback.is_null()) | |
| 1353 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback); | |
| 1354 } | |
| 1355 | |
| 1356 bool CookieMonster::SetCookieWithOptions(const GURL& url, | 1363 bool CookieMonster::SetCookieWithOptions(const GURL& url, |
| 1357 const std::string& cookie_line, | 1364 const std::string& cookie_line, |
| 1358 const CookieOptions& options) { | 1365 const CookieOptions& options) { |
| 1359 base::AutoLock autolock(lock_); | 1366 base::AutoLock autolock(lock_); |
| 1360 | 1367 |
| 1361 if (!HasCookieableScheme(url)) { | 1368 if (!HasCookieableScheme(url)) { |
| 1362 return false; | 1369 return false; |
| 1363 } | 1370 } |
| 1364 | 1371 |
| 1365 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); | 1372 return SetCookieWithCreationTimeAndOptions(url, cookie_line, Time(), options); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1409 | 1416 |
| 1410 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1417 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1411 CookieMap::iterator curit = it; | 1418 CookieMap::iterator curit = it; |
| 1412 ++it; | 1419 ++it; |
| 1413 if (matching_cookies.find(curit->second) != matching_cookies.end()) { | 1420 if (matching_cookies.find(curit->second) != matching_cookies.end()) { |
| 1414 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); | 1421 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); |
| 1415 } | 1422 } |
| 1416 } | 1423 } |
| 1417 } | 1424 } |
| 1418 | 1425 |
| 1426 bool CookieMonster::SetCookieWithCreationTime(const GURL& url, | |
| 1427 const std::string& cookie_line, | |
| 1428 const base::Time& creation_time) { | |
| 1429 DCHECK(!store_.get()) << "This method is only to be used by unit-tests."; | |
| 1430 base::AutoLock autolock(lock_); | |
| 1431 | |
| 1432 if (!HasCookieableScheme(url)) { | |
| 1433 return false; | |
| 1434 } | |
| 1435 | |
| 1436 MarkCookieStoreAsInitialized(); | |
| 1437 if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) | |
| 1438 FetchAllCookiesIfNecessary(); | |
| 1439 | |
| 1440 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, | |
| 1441 CookieOptions()); | |
| 1442 } | |
| 1443 | |
| 1419 int CookieMonster::DeleteSessionCookies() { | 1444 int CookieMonster::DeleteSessionCookies() { |
| 1420 base::AutoLock autolock(lock_); | 1445 base::AutoLock autolock(lock_); |
| 1421 | 1446 |
| 1422 int num_deleted = 0; | 1447 int num_deleted = 0; |
| 1423 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1448 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1424 CookieMap::iterator curit = it; | 1449 CookieMap::iterator curit = it; |
| 1425 CanonicalCookie* cc = curit->second; | 1450 CanonicalCookie* cc = curit->second; |
| 1426 ++it; | 1451 ++it; |
| 1427 | 1452 |
| 1428 if (!cc->IsPersistent()) { | 1453 if (!cc->IsPersistent()) { |
| 1429 InternalDeleteCookie(curit, true, /*sync_to_store*/ | 1454 InternalDeleteCookie(curit, true, /*sync_to_store*/ |
| 1430 DELETE_COOKIE_EXPIRED); | 1455 DELETE_COOKIE_EXPIRED); |
| 1431 ++num_deleted; | 1456 ++num_deleted; |
| 1432 } | 1457 } |
| 1433 } | 1458 } |
| 1434 | 1459 |
| 1435 return num_deleted; | 1460 return num_deleted; |
| 1436 } | 1461 } |
| 1437 | 1462 |
| 1438 CookieMonster* CookieMonster::GetCookieMonster() { | |
| 1439 return this; | |
| 1440 } | |
| 1441 | |
| 1442 // This function must be called before the CookieMonster is used. | |
| 1443 void CookieMonster::SetPersistSessionCookies(bool persist_session_cookies) { | |
| 1444 DCHECK(!initialized_); | |
| 1445 persist_session_cookies_ = persist_session_cookies; | |
| 1446 } | |
| 1447 | |
| 1448 void CookieMonster::SetForceKeepSessionState() { | |
| 1449 if (store_.get()) { | |
| 1450 store_->SetForceKeepSessionState(); | |
| 1451 } | |
| 1452 } | |
| 1453 | |
| 1454 CookieMonster::~CookieMonster() { | |
| 1455 DeleteAll(false); | |
| 1456 } | |
| 1457 | |
| 1458 bool CookieMonster::SetCookieWithCreationTime(const GURL& url, | |
| 1459 const std::string& cookie_line, | |
| 1460 const base::Time& creation_time) { | |
| 1461 DCHECK(!store_.get()) << "This method is only to be used by unit-tests."; | |
| 1462 base::AutoLock autolock(lock_); | |
| 1463 | |
| 1464 if (!HasCookieableScheme(url)) { | |
| 1465 return false; | |
| 1466 } | |
| 1467 | |
| 1468 MarkCookieStoreAsInitialized(); | |
| 1469 if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) | |
| 1470 FetchAllCookiesIfNecessary(); | |
| 1471 | |
| 1472 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, | |
| 1473 CookieOptions()); | |
| 1474 } | |
| 1475 | |
| 1476 void CookieMonster::MarkCookieStoreAsInitialized() { | 1463 void CookieMonster::MarkCookieStoreAsInitialized() { |
| 1477 initialized_ = true; | 1464 initialized_ = true; |
| 1478 } | 1465 } |
| 1479 | 1466 |
| 1480 void CookieMonster::FetchAllCookiesIfNecessary() { | 1467 void CookieMonster::FetchAllCookiesIfNecessary() { |
| 1481 if (store_.get() && !started_fetching_all_cookies_) { | 1468 if (store_.get() && !started_fetching_all_cookies_) { |
| 1482 started_fetching_all_cookies_ = true; | 1469 started_fetching_all_cookies_ = true; |
| 1483 FetchAllCookies(); | 1470 FetchAllCookies(); |
| 1484 } | 1471 } |
| 1485 } | 1472 } |
| 1486 | 1473 |
| 1474 void CookieMonster::FetchAllCookies() { | |
| 1475 DCHECK(store_.get()) << "Store must exist to initialize"; | |
| 1476 DCHECK(!finished_fetching_all_cookies_) | |
| 1477 << "All cookies have already been fetched."; | |
| 1478 | |
| 1479 // We bind in the current time so that we can report the wall-clock time for | |
| 1480 // loading cookies. | |
| 1481 store_->Load(base::Bind(&CookieMonster::OnLoaded, this, TimeTicks::Now())); | |
| 1482 } | |
| 1483 | |
| 1487 bool CookieMonster::ShouldFetchAllCookiesWhenFetchingAnyCookie() { | 1484 bool CookieMonster::ShouldFetchAllCookiesWhenFetchingAnyCookie() { |
| 1488 if (fetch_strategy_ == kUnknownFetch) { | 1485 if (fetch_strategy_ == kUnknownFetch) { |
| 1489 const std::string group_name = | 1486 const std::string group_name = |
| 1490 base::FieldTrialList::FindFullName(kCookieMonsterFetchStrategyName); | 1487 base::FieldTrialList::FindFullName(kCookieMonsterFetchStrategyName); |
| 1491 if (group_name == kFetchWhenNecessaryName) { | 1488 if (group_name == kFetchWhenNecessaryName) { |
| 1492 fetch_strategy_ = kFetchWhenNecessary; | 1489 fetch_strategy_ = kFetchWhenNecessary; |
| 1493 } else if (group_name == kAlwaysFetchName) { | 1490 } else if (group_name == kAlwaysFetchName) { |
| 1494 fetch_strategy_ = kAlwaysFetch; | 1491 fetch_strategy_ = kAlwaysFetch; |
| 1495 } else { | 1492 } else { |
| 1496 // The logic in the conditional is redundant, but it makes trials of | 1493 // The logic in the conditional is redundant, but it makes trials of |
| 1497 // the Finch experiment more explicit. | 1494 // the Finch experiment more explicit. |
| 1498 fetch_strategy_ = kAlwaysFetch; | 1495 fetch_strategy_ = kAlwaysFetch; |
| 1499 } | 1496 } |
| 1500 } | 1497 } |
| 1501 | 1498 |
| 1502 return fetch_strategy_ == kAlwaysFetch; | 1499 return fetch_strategy_ == kAlwaysFetch; |
| 1503 } | 1500 } |
| 1504 | 1501 |
| 1505 void CookieMonster::FetchAllCookies() { | |
| 1506 DCHECK(store_.get()) << "Store must exist to initialize"; | |
| 1507 DCHECK(!finished_fetching_all_cookies_) | |
| 1508 << "All cookies have already been fetched."; | |
| 1509 | |
| 1510 // We bind in the current time so that we can report the wall-clock time for | |
| 1511 // loading cookies. | |
| 1512 store_->Load(base::Bind(&CookieMonster::OnLoaded, this, TimeTicks::Now())); | |
| 1513 } | |
| 1514 | |
| 1515 void CookieMonster::OnLoaded(TimeTicks beginning_time, | 1502 void CookieMonster::OnLoaded(TimeTicks beginning_time, |
| 1516 const std::vector<CanonicalCookie*>& cookies) { | 1503 const std::vector<CanonicalCookie*>& cookies) { |
| 1517 StoreLoadedCookies(cookies); | 1504 StoreLoadedCookies(cookies); |
| 1518 histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time); | 1505 histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time); |
| 1519 | 1506 |
| 1520 // Invoke the task queue of cookie request. | 1507 // Invoke the task queue of cookie request. |
| 1521 InvokeQueue(); | 1508 InvokeQueue(); |
| 1522 } | 1509 } |
| 1523 | 1510 |
| 1524 void CookieMonster::OnKeyLoaded(const std::string& key, | 1511 void CookieMonster::OnKeyLoaded(const std::string& key, |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1722 // don't invalidate existing iterators following deletion). | 1709 // don't invalidate existing iterators following deletion). |
| 1723 for (CookieSet::iterator dupes_it = dupes.begin(); dupes_it != dupes.end(); | 1710 for (CookieSet::iterator dupes_it = dupes.begin(); dupes_it != dupes.end(); |
| 1724 ++dupes_it) { | 1711 ++dupes_it) { |
| 1725 InternalDeleteCookie(*dupes_it, true, | 1712 InternalDeleteCookie(*dupes_it, true, |
| 1726 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE); | 1713 DELETE_COOKIE_DUPLICATE_IN_BACKING_STORE); |
| 1727 } | 1714 } |
| 1728 } | 1715 } |
| 1729 DCHECK_EQ(num_duplicates, num_duplicates_found); | 1716 DCHECK_EQ(num_duplicates, num_duplicates_found); |
| 1730 } | 1717 } |
| 1731 | 1718 |
| 1732 // Note: file must be the last scheme. | |
| 1733 const char* const CookieMonster::kDefaultCookieableSchemes[] = {"http", | |
| 1734 "https", | |
| 1735 "ws", | |
| 1736 "wss", | |
| 1737 "file"}; | |
| 1738 const int CookieMonster::kDefaultCookieableSchemesCount = | |
| 1739 arraysize(kDefaultCookieableSchemes); | |
| 1740 | |
| 1741 void CookieMonster::SetDefaultCookieableSchemes() { | 1719 void CookieMonster::SetDefaultCookieableSchemes() { |
| 1742 // Always disable file scheme unless SetEnableFileScheme(true) is called. | 1720 // Always disable file scheme unless SetEnableFileScheme(true) is called. |
| 1743 SetCookieableSchemes(kDefaultCookieableSchemes, | 1721 SetCookieableSchemes(kDefaultCookieableSchemes, |
| 1744 kDefaultCookieableSchemesCount - 1); | 1722 kDefaultCookieableSchemesCount - 1); |
| 1745 } | 1723 } |
| 1746 | 1724 |
| 1747 void CookieMonster::FindCookiesForHostAndDomain( | 1725 void CookieMonster::FindCookiesForHostAndDomain( |
| 1748 const GURL& url, | 1726 const GURL& url, |
| 1749 const CookieOptions& options, | 1727 const CookieOptions& options, |
| 1750 bool update_access_time, | 1728 bool update_access_time, |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2052 ChangeCausePair mapping = ChangeCauseMapping[deletion_cause]; | 2030 ChangeCausePair mapping = ChangeCauseMapping[deletion_cause]; |
| 2053 | 2031 |
| 2054 if (mapping.notify) | 2032 if (mapping.notify) |
| 2055 delegate_->OnCookieChanged(*cc, true, mapping.cause); | 2033 delegate_->OnCookieChanged(*cc, true, mapping.cause); |
| 2056 } | 2034 } |
| 2057 RunCallbacks(*cc, true); | 2035 RunCallbacks(*cc, true); |
| 2058 cookies_.erase(it); | 2036 cookies_.erase(it); |
| 2059 delete cc; | 2037 delete cc; |
| 2060 } | 2038 } |
| 2061 | 2039 |
| 2062 size_t CookieMonster::GarbageCollectLeastRecentlyAccessed( | |
| 2063 const base::Time& current, | |
| 2064 const base::Time& safe_date, | |
| 2065 size_t purge_goal, | |
| 2066 CookieItVector cookie_its) { | |
| 2067 // Sorts up to *and including* |cookie_its[purge_goal]|, so | |
| 2068 // |earliest_access_time| will be properly assigned even if | |
| 2069 // |global_purge_it| == |cookie_its.begin() + purge_goal|. | |
| 2070 SortLeastRecentlyAccessed(cookie_its.begin(), cookie_its.end(), purge_goal); | |
| 2071 // Find boundary to cookies older than safe_date. | |
| 2072 CookieItVector::iterator global_purge_it = LowerBoundAccessDate( | |
| 2073 cookie_its.begin(), cookie_its.begin() + purge_goal, safe_date); | |
| 2074 // Only delete the old cookies, and if strict secure is enabled, delete | |
| 2075 // non-secure ones first. | |
| 2076 size_t num_deleted = | |
| 2077 GarbageCollectDeleteRange(current, DELETE_COOKIE_EVICTED_GLOBAL, | |
| 2078 cookie_its.begin(), global_purge_it); | |
| 2079 // Set access day to the oldest cookie that wasn't deleted. | |
| 2080 earliest_access_time_ = (*global_purge_it)->second->LastAccessDate(); | |
| 2081 return num_deleted; | |
| 2082 } | |
| 2083 | |
| 2084 // Domain expiry behavior is unchanged by key/expiry scheme (the | 2040 // Domain expiry behavior is unchanged by key/expiry scheme (the |
| 2085 // meaning of the key is different, but that's not visible to this routine). | 2041 // meaning of the key is different, but that's not visible to this routine). |
| 2086 size_t CookieMonster::GarbageCollect(const Time& current, | 2042 size_t CookieMonster::GarbageCollect(const Time& current, |
| 2087 const std::string& key, | 2043 const std::string& key, |
| 2088 bool enforce_strict_secure) { | 2044 bool enforce_strict_secure) { |
| 2089 lock_.AssertAcquired(); | 2045 lock_.AssertAcquired(); |
| 2090 | 2046 |
| 2091 size_t num_deleted = 0; | 2047 size_t num_deleted = 0; |
| 2092 Time safe_date(Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays)); | 2048 Time safe_date(Time::Now() - TimeDelta::FromDays(kSafeFromGlobalPurgeDays)); |
| 2093 | 2049 |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2261 CookieItVector::iterator it_begin, | 2217 CookieItVector::iterator it_begin, |
| 2262 CookieItVector::iterator it_end) { | 2218 CookieItVector::iterator it_end) { |
| 2263 for (CookieItVector::iterator it = it_begin; it != it_end; it++) { | 2219 for (CookieItVector::iterator it = it_begin; it != it_end; it++) { |
| 2264 histogram_evicted_last_access_minutes_->Add( | 2220 histogram_evicted_last_access_minutes_->Add( |
| 2265 (current - (*it)->second->LastAccessDate()).InMinutes()); | 2221 (current - (*it)->second->LastAccessDate()).InMinutes()); |
| 2266 InternalDeleteCookie((*it), true, cause); | 2222 InternalDeleteCookie((*it), true, cause); |
| 2267 } | 2223 } |
| 2268 return it_end - it_begin; | 2224 return it_end - it_begin; |
| 2269 } | 2225 } |
| 2270 | 2226 |
| 2227 size_t CookieMonster::GarbageCollectLeastRecentlyAccessed( | |
| 2228 const base::Time& current, | |
| 2229 const base::Time& safe_date, | |
| 2230 size_t purge_goal, | |
| 2231 CookieItVector cookie_its) { | |
| 2232 // Sorts up to *and including* |cookie_its[purge_goal]|, so | |
| 2233 // |earliest_access_time| will be properly assigned even if | |
| 2234 // |global_purge_it| == |cookie_its.begin() + purge_goal|. | |
| 2235 SortLeastRecentlyAccessed(cookie_its.begin(), cookie_its.end(), purge_goal); | |
| 2236 // Find boundary to cookies older than safe_date. | |
| 2237 CookieItVector::iterator global_purge_it = LowerBoundAccessDate( | |
| 2238 cookie_its.begin(), cookie_its.begin() + purge_goal, safe_date); | |
| 2239 // Only delete the old cookies, and if strict secure is enabled, delete | |
| 2240 // non-secure ones first. | |
| 2241 size_t num_deleted = | |
| 2242 GarbageCollectDeleteRange(current, DELETE_COOKIE_EVICTED_GLOBAL, | |
| 2243 cookie_its.begin(), global_purge_it); | |
| 2244 // Set access day to the oldest cookie that wasn't deleted. | |
| 2245 earliest_access_time_ = (*global_purge_it)->second->LastAccessDate(); | |
| 2246 return num_deleted; | |
| 2247 } | |
| 2248 | |
| 2271 // A wrapper around registry_controlled_domains::GetDomainAndRegistry | 2249 // A wrapper around registry_controlled_domains::GetDomainAndRegistry |
| 2272 // to make clear we're creating a key for our local map. Here and | 2250 // to make clear we're creating a key for our local map. Here and |
| 2273 // in FindCookiesForHostAndDomain() are the only two places where | 2251 // in FindCookiesForHostAndDomain() are the only two places where |
| 2274 // we need to conditionalize based on key type. | 2252 // we need to conditionalize based on key type. |
| 2275 // | 2253 // |
| 2276 // Note that this key algorithm explicitly ignores the scheme. This is | 2254 // Note that this key algorithm explicitly ignores the scheme. This is |
| 2277 // because when we're entering cookies into the map from the backing store, | 2255 // because when we're entering cookies into the map from the backing store, |
| 2278 // we in general won't have the scheme at that point. | 2256 // we in general won't have the scheme at that point. |
| 2279 // In practical terms, this means that file cookies will be stored | 2257 // In practical terms, this means that file cookies will be stored |
| 2280 // in the map either by an empty string or by UNC name (and will be | 2258 // in the map either by an empty string or by UNC name (and will be |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 2296 registry_controlled_domains::GetDomainAndRegistry( | 2274 registry_controlled_domains::GetDomainAndRegistry( |
| 2297 domain, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)); | 2275 domain, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)); |
| 2298 if (effective_domain.empty()) | 2276 if (effective_domain.empty()) |
| 2299 effective_domain = domain; | 2277 effective_domain = domain; |
| 2300 | 2278 |
| 2301 if (!effective_domain.empty() && effective_domain[0] == '.') | 2279 if (!effective_domain.empty() && effective_domain[0] == '.') |
| 2302 return effective_domain.substr(1); | 2280 return effective_domain.substr(1); |
| 2303 return effective_domain; | 2281 return effective_domain; |
| 2304 } | 2282 } |
| 2305 | 2283 |
| 2306 bool CookieMonster::IsCookieableScheme(const std::string& scheme) { | |
| 2307 base::AutoLock autolock(lock_); | |
| 2308 | |
| 2309 return std::find(cookieable_schemes_.begin(), cookieable_schemes_.end(), | |
| 2310 scheme) != cookieable_schemes_.end(); | |
| 2311 } | |
| 2312 | |
| 2313 bool CookieMonster::HasCookieableScheme(const GURL& url) { | 2284 bool CookieMonster::HasCookieableScheme(const GURL& url) { |
| 2314 lock_.AssertAcquired(); | 2285 lock_.AssertAcquired(); |
| 2315 | 2286 |
| 2316 // Make sure the request is on a cookie-able url scheme. | 2287 // Make sure the request is on a cookie-able url scheme. |
| 2317 for (size_t i = 0; i < cookieable_schemes_.size(); ++i) { | 2288 for (size_t i = 0; i < cookieable_schemes_.size(); ++i) { |
| 2318 // We matched a scheme. | 2289 // We matched a scheme. |
| 2319 if (url.SchemeIs(cookieable_schemes_[i].c_str())) { | 2290 if (url.SchemeIs(cookieable_schemes_[i].c_str())) { |
| 2320 // We've matched a supported scheme. | 2291 // We've matched a supported scheme. |
| 2321 return true; | 2292 return true; |
| 2322 } | 2293 } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2411 } | 2382 } |
| 2412 | 2383 |
| 2413 // The system resolution is not high enough, so we can have multiple | 2384 // The system resolution is not high enough, so we can have multiple |
| 2414 // set cookies that result in the same system time. When this happens, we | 2385 // set cookies that result in the same system time. When this happens, we |
| 2415 // increment by one Time unit. Let's hope computers don't get too fast. | 2386 // increment by one Time unit. Let's hope computers don't get too fast. |
| 2416 Time CookieMonster::CurrentTime() { | 2387 Time CookieMonster::CurrentTime() { |
| 2417 return std::max(Time::Now(), Time::FromInternalValue( | 2388 return std::max(Time::Now(), Time::FromInternalValue( |
| 2418 last_time_seen_.ToInternalValue() + 1)); | 2389 last_time_seen_.ToInternalValue() + 1)); |
| 2419 } | 2390 } |
| 2420 | 2391 |
| 2392 void CookieMonster::DoCookieTask( | |
| 2393 const scoped_refptr<CookieMonsterTask>& task_item) { | |
| 2394 { | |
| 2395 base::AutoLock autolock(lock_); | |
| 2396 MarkCookieStoreAsInitialized(); | |
| 2397 FetchAllCookiesIfNecessary(); | |
| 2398 if (!finished_fetching_all_cookies_ && store_.get()) { | |
| 2399 tasks_pending_.push(task_item); | |
| 2400 return; | |
| 2401 } | |
| 2402 } | |
| 2403 | |
| 2404 task_item->Run(); | |
| 2405 } | |
| 2406 | |
| 2407 void CookieMonster::DoCookieTaskForURL( | |
| 2408 const scoped_refptr<CookieMonsterTask>& task_item, | |
| 2409 const GURL& url) { | |
| 2410 { | |
| 2411 base::AutoLock autolock(lock_); | |
| 2412 MarkCookieStoreAsInitialized(); | |
| 2413 if (ShouldFetchAllCookiesWhenFetchingAnyCookie()) | |
| 2414 FetchAllCookiesIfNecessary(); | |
| 2415 // If cookies for the requested domain key (eTLD+1) have been loaded from DB | |
| 2416 // then run the task, otherwise load from DB. | |
| 2417 if (!finished_fetching_all_cookies_ && store_.get()) { | |
| 2418 // Checks if the domain key has been loaded. | |
| 2419 std::string key( | |
| 2420 cookie_util::GetEffectiveDomain(url.scheme(), url.host())); | |
| 2421 if (keys_loaded_.find(key) == keys_loaded_.end()) { | |
| 2422 std::map<std::string, | |
| 2423 std::deque<scoped_refptr<CookieMonsterTask>>>::iterator it = | |
| 2424 tasks_pending_for_key_.find(key); | |
| 2425 if (it == tasks_pending_for_key_.end()) { | |
| 2426 store_->LoadCookiesForKey( | |
| 2427 key, base::Bind(&CookieMonster::OnKeyLoaded, this, key)); | |
| 2428 it = tasks_pending_for_key_ | |
| 2429 .insert(std::make_pair( | |
| 2430 key, std::deque<scoped_refptr<CookieMonsterTask>>())) | |
| 2431 .first; | |
| 2432 } | |
| 2433 it->second.push_back(task_item); | |
| 2434 return; | |
| 2435 } | |
| 2436 } | |
| 2437 } | |
| 2438 task_item->Run(); | |
| 2439 } | |
| 2440 | |
| 2421 void CookieMonster::ComputeCookieDiff(CookieList* old_cookies, | 2441 void CookieMonster::ComputeCookieDiff(CookieList* old_cookies, |
| 2422 CookieList* new_cookies, | 2442 CookieList* new_cookies, |
| 2423 CookieList* cookies_to_add, | 2443 CookieList* cookies_to_add, |
| 2424 CookieList* cookies_to_delete) { | 2444 CookieList* cookies_to_delete) { |
| 2425 DCHECK(old_cookies); | 2445 DCHECK(old_cookies); |
| 2426 DCHECK(new_cookies); | 2446 DCHECK(new_cookies); |
| 2427 DCHECK(cookies_to_add); | 2447 DCHECK(cookies_to_add); |
| 2428 DCHECK(cookies_to_delete); | 2448 DCHECK(cookies_to_delete); |
| 2429 DCHECK(cookies_to_add->empty()); | 2449 DCHECK(cookies_to_add->empty()); |
| 2430 DCHECK(cookies_to_delete->empty()); | 2450 DCHECK(cookies_to_delete->empty()); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 2444 PartialDiffCookieSorter); | 2464 PartialDiffCookieSorter); |
| 2445 | 2465 |
| 2446 // Select any new cookie for addition (or update) if no old cookie is exactly | 2466 // Select any new cookie for addition (or update) if no old cookie is exactly |
| 2447 // equivalent. | 2467 // equivalent. |
| 2448 std::set_difference(new_cookies->begin(), new_cookies->end(), | 2468 std::set_difference(new_cookies->begin(), new_cookies->end(), |
| 2449 old_cookies->begin(), old_cookies->end(), | 2469 old_cookies->begin(), old_cookies->end(), |
| 2450 std::inserter(*cookies_to_add, cookies_to_add->begin()), | 2470 std::inserter(*cookies_to_add, cookies_to_add->begin()), |
| 2451 FullDiffCookieSorter); | 2471 FullDiffCookieSorter); |
| 2452 } | 2472 } |
| 2453 | 2473 |
| 2454 scoped_ptr<CookieStore::CookieChangedSubscription> | |
| 2455 CookieMonster::AddCallbackForCookie(const GURL& gurl, | |
| 2456 const std::string& name, | |
| 2457 const CookieChangedCallback& callback) { | |
| 2458 base::AutoLock autolock(lock_); | |
| 2459 std::pair<GURL, std::string> key(gurl, name); | |
| 2460 if (hook_map_.count(key) == 0) | |
| 2461 hook_map_[key] = make_linked_ptr(new CookieChangedCallbackList()); | |
| 2462 return hook_map_[key]->Add( | |
| 2463 base::Bind(&RunAsync, base::ThreadTaskRunnerHandle::Get(), callback)); | |
| 2464 } | |
| 2465 | |
| 2466 #if defined(OS_ANDROID) | |
| 2467 void CookieMonster::SetEnableFileScheme(bool accept) { | |
| 2468 // This assumes "file" is always at the end of the array. See the comment | |
| 2469 // above kDefaultCookieableSchemes. | |
| 2470 // | |
| 2471 // TODO(mkwst): We're keeping this method around to support the | |
| 2472 // 'CookieManager::setAcceptFileSchemeCookies' method on Android's WebView; | |
| 2473 // if/when we can deprecate and remove that method, we can remove this one | |
| 2474 // as well. Until then, we'll just ensure that the method has no effect on | |
| 2475 // non-android systems. | |
| 2476 int num_schemes = accept ? kDefaultCookieableSchemesCount | |
| 2477 : kDefaultCookieableSchemesCount - 1; | |
| 2478 | |
| 2479 SetCookieableSchemes(kDefaultCookieableSchemes, num_schemes); | |
| 2480 } | |
| 2481 #endif | |
| 2482 | |
| 2483 void CookieMonster::RunCallbacks(const CanonicalCookie& cookie, bool removed) { | 2474 void CookieMonster::RunCallbacks(const CanonicalCookie& cookie, bool removed) { |
| 2484 lock_.AssertAcquired(); | 2475 lock_.AssertAcquired(); |
| 2485 CookieOptions opts; | 2476 CookieOptions opts; |
| 2486 opts.set_include_httponly(); | 2477 opts.set_include_httponly(); |
| 2487 opts.set_include_first_party_only_cookies(); | 2478 opts.set_include_first_party_only_cookies(); |
| 2488 // Note that the callbacks in hook_map_ are wrapped with MakeAsync(), so they | 2479 // Note that the callbacks in hook_map_ are wrapped with MakeAsync(), so they |
| 2489 // are guaranteed to not take long - they just post a RunAsync task back to | 2480 // are guaranteed to not take long - they just post a RunAsync task back to |
| 2490 // the appropriate thread's message loop and return. It is important that this | 2481 // the appropriate thread's message loop and return. It is important that this |
| 2491 // method not run user-supplied callbacks directly, since the CookieMonster | 2482 // method not run user-supplied callbacks directly, since the CookieMonster |
| 2492 // lock is held and it is easy to accidentally introduce deadlocks. | 2483 // lock is held and it is easy to accidentally introduce deadlocks. |
| 2493 for (CookieChangedHookMap::iterator it = hook_map_.begin(); | 2484 for (CookieChangedHookMap::iterator it = hook_map_.begin(); |
| 2494 it != hook_map_.end(); ++it) { | 2485 it != hook_map_.end(); ++it) { |
| 2495 std::pair<GURL, std::string> key = it->first; | 2486 std::pair<GURL, std::string> key = it->first; |
| 2496 if (cookie.IncludeForRequestURL(key.first, opts) && | 2487 if (cookie.IncludeForRequestURL(key.first, opts) && |
| 2497 cookie.Name() == key.second) { | 2488 cookie.Name() == key.second) { |
| 2498 it->second->Notify(cookie, removed); | 2489 it->second->Notify(cookie, removed); |
| 2499 } | 2490 } |
| 2500 } | 2491 } |
| 2501 } | 2492 } |
| 2502 | 2493 |
| 2503 } // namespace net | 2494 } // namespace net |
| OLD | NEW |