| 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 988 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 999 arraysize(kDefaultCookieableSchemes); | 999 arraysize(kDefaultCookieableSchemes); |
| 1000 | 1000 |
| 1001 std::unique_ptr<CookieStore::CookieChangedSubscription> | 1001 std::unique_ptr<CookieStore::CookieChangedSubscription> |
| 1002 CookieMonster::AddCallbackForCookie(const GURL& gurl, | 1002 CookieMonster::AddCallbackForCookie(const GURL& gurl, |
| 1003 const std::string& name, | 1003 const std::string& name, |
| 1004 const CookieChangedCallback& callback) { | 1004 const CookieChangedCallback& callback) { |
| 1005 DCHECK(thread_checker_.CalledOnValidThread()); | 1005 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1006 | 1006 |
| 1007 std::pair<GURL, std::string> key(gurl, name); | 1007 std::pair<GURL, std::string> key(gurl, name); |
| 1008 if (hook_map_.count(key) == 0) | 1008 if (hook_map_.count(key) == 0) |
| 1009 hook_map_[key] = make_linked_ptr(new CookieChangedCallbackList()); | 1009 hook_map_[key] = base::MakeUnique<CookieChangedCallbackList>(); |
| 1010 return hook_map_[key]->Add( | 1010 return hook_map_[key]->Add( |
| 1011 base::Bind(&RunAsync, base::ThreadTaskRunnerHandle::Get(), callback)); | 1011 base::Bind(&RunAsync, base::ThreadTaskRunnerHandle::Get(), callback)); |
| 1012 } | 1012 } |
| 1013 | 1013 |
| 1014 bool CookieMonster::IsEphemeral() { | 1014 bool CookieMonster::IsEphemeral() { |
| 1015 return store_.get() == nullptr; | 1015 return store_.get() == nullptr; |
| 1016 } | 1016 } |
| 1017 | 1017 |
| 1018 CookieMonster::~CookieMonster() { | 1018 CookieMonster::~CookieMonster() { |
| 1019 DCHECK(thread_checker_.CalledOnValidThread()); | 1019 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1087 // | 1087 // |
| 1088 // Note that this does not prune cookies to be below our limits (if we've | 1088 // Note that this does not prune cookies to be below our limits (if we've |
| 1089 // exceeded them) the way that calling GarbageCollect() would. | 1089 // exceeded them) the way that calling GarbageCollect() would. |
| 1090 GarbageCollectExpired( | 1090 GarbageCollectExpired( |
| 1091 Time::Now(), CookieMapItPair(cookies_.begin(), cookies_.end()), NULL); | 1091 Time::Now(), CookieMapItPair(cookies_.begin(), cookies_.end()), NULL); |
| 1092 | 1092 |
| 1093 // Copy the CanonicalCookie pointers from the map so that we can use the same | 1093 // Copy the CanonicalCookie pointers from the map so that we can use the same |
| 1094 // sorter as elsewhere, then copy the result out. | 1094 // sorter as elsewhere, then copy the result out. |
| 1095 std::vector<CanonicalCookie*> cookie_ptrs; | 1095 std::vector<CanonicalCookie*> cookie_ptrs; |
| 1096 cookie_ptrs.reserve(cookies_.size()); | 1096 cookie_ptrs.reserve(cookies_.size()); |
| 1097 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end(); ++it) | 1097 for (const auto& cookie : cookies_) |
| 1098 cookie_ptrs.push_back(it->second); | 1098 cookie_ptrs.push_back(cookie.second.get()); |
| 1099 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); | 1099 std::sort(cookie_ptrs.begin(), cookie_ptrs.end(), CookieSorter); |
| 1100 | 1100 |
| 1101 CookieList cookie_list; | 1101 CookieList cookie_list; |
| 1102 cookie_list.reserve(cookie_ptrs.size()); | 1102 cookie_list.reserve(cookie_ptrs.size()); |
| 1103 for (std::vector<CanonicalCookie*>::const_iterator it = cookie_ptrs.begin(); | 1103 for (const auto& cookie_ptr : cookie_ptrs) |
| 1104 it != cookie_ptrs.end(); ++it) | 1104 cookie_list.push_back(*cookie_ptr); |
| 1105 cookie_list.push_back(**it); | |
| 1106 | 1105 |
| 1107 return cookie_list; | 1106 return cookie_list; |
| 1108 } | 1107 } |
| 1109 | 1108 |
| 1110 CookieList CookieMonster::GetCookieListWithOptions( | 1109 CookieList CookieMonster::GetCookieListWithOptions( |
| 1111 const GURL& url, | 1110 const GURL& url, |
| 1112 const CookieOptions& options) { | 1111 const CookieOptions& options) { |
| 1113 DCHECK(thread_checker_.CalledOnValidThread()); | 1112 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1114 | 1113 |
| 1115 CookieList cookies; | 1114 CookieList cookies; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1128 return cookies; | 1127 return cookies; |
| 1129 } | 1128 } |
| 1130 | 1129 |
| 1131 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, | 1130 int CookieMonster::DeleteAllCreatedBetween(const Time& delete_begin, |
| 1132 const Time& delete_end) { | 1131 const Time& delete_end) { |
| 1133 DCHECK(thread_checker_.CalledOnValidThread()); | 1132 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1134 | 1133 |
| 1135 int num_deleted = 0; | 1134 int num_deleted = 0; |
| 1136 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1135 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1137 CookieMap::iterator curit = it; | 1136 CookieMap::iterator curit = it; |
| 1138 CanonicalCookie* cc = curit->second; | 1137 CanonicalCookie* cc = curit->second.get(); |
| 1139 ++it; | 1138 ++it; |
| 1140 | 1139 |
| 1141 if (cc->CreationDate() >= delete_begin && | 1140 if (cc->CreationDate() >= delete_begin && |
| 1142 (delete_end.is_null() || cc->CreationDate() < delete_end)) { | 1141 (delete_end.is_null() || cc->CreationDate() < delete_end)) { |
| 1143 InternalDeleteCookie(curit, true, /*sync_to_store*/ | 1142 InternalDeleteCookie(curit, true, /*sync_to_store*/ |
| 1144 DELETE_COOKIE_EXPLICIT); | 1143 DELETE_COOKIE_EXPLICIT); |
| 1145 ++num_deleted; | 1144 ++num_deleted; |
| 1146 } | 1145 } |
| 1147 } | 1146 } |
| 1148 | 1147 |
| 1149 return num_deleted; | 1148 return num_deleted; |
| 1150 } | 1149 } |
| 1151 | 1150 |
| 1152 int CookieMonster::DeleteAllCreatedBetweenWithPredicate( | 1151 int CookieMonster::DeleteAllCreatedBetweenWithPredicate( |
| 1153 const base::Time& delete_begin, | 1152 const base::Time& delete_begin, |
| 1154 const base::Time& delete_end, | 1153 const base::Time& delete_end, |
| 1155 const base::Callback<bool(const CanonicalCookie&)>& predicate) { | 1154 const base::Callback<bool(const CanonicalCookie&)>& predicate) { |
| 1156 int num_deleted = 0; | 1155 int num_deleted = 0; |
| 1157 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1156 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1158 CookieMap::iterator curit = it; | 1157 CookieMap::iterator curit = it; |
| 1159 CanonicalCookie* cc = curit->second; | 1158 CanonicalCookie* cc = curit->second.get(); |
| 1160 ++it; | 1159 ++it; |
| 1161 | 1160 |
| 1162 if (cc->CreationDate() >= delete_begin && | 1161 if (cc->CreationDate() >= delete_begin && |
| 1163 // The assumption that null |delete_end| is equivalent to | 1162 // The assumption that null |delete_end| is equivalent to |
| 1164 // Time::Max() is confusing. | 1163 // Time::Max() is confusing. |
| 1165 (delete_end.is_null() || cc->CreationDate() < delete_end) && | 1164 (delete_end.is_null() || cc->CreationDate() < delete_end) && |
| 1166 predicate.Run(*cc)) { | 1165 predicate.Run(*cc)) { |
| 1167 InternalDeleteCookie(curit, true, /*sync_to_store*/ | 1166 InternalDeleteCookie(curit, true, /*sync_to_store*/ |
| 1168 DELETE_COOKIE_EXPLICIT); | 1167 DELETE_COOKIE_EXPLICIT); |
| 1169 ++num_deleted; | 1168 ++num_deleted; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1223 if (cookie->Name() != cookie_name) | 1222 if (cookie->Name() != cookie_name) |
| 1224 continue; | 1223 continue; |
| 1225 if (!cookie->IsOnPath(url.path())) | 1224 if (!cookie->IsOnPath(url.path())) |
| 1226 continue; | 1225 continue; |
| 1227 matching_cookies.insert(cookie); | 1226 matching_cookies.insert(cookie); |
| 1228 } | 1227 } |
| 1229 | 1228 |
| 1230 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1229 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1231 CookieMap::iterator curit = it; | 1230 CookieMap::iterator curit = it; |
| 1232 ++it; | 1231 ++it; |
| 1233 if (matching_cookies.find(curit->second) != matching_cookies.end()) { | 1232 if (matching_cookies.find(curit->second.get()) != matching_cookies.end()) { |
| 1234 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); | 1233 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPLICIT); |
| 1235 } | 1234 } |
| 1236 } | 1235 } |
| 1237 } | 1236 } |
| 1238 | 1237 |
| 1239 int CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { | 1238 int CookieMonster::DeleteCanonicalCookie(const CanonicalCookie& cookie) { |
| 1240 DCHECK(thread_checker_.CalledOnValidThread()); | 1239 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1241 | 1240 |
| 1242 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); | 1241 for (CookieMapItPair its = cookies_.equal_range(GetKey(cookie.Domain())); |
| 1243 its.first != its.second; ++its.first) { | 1242 its.first != its.second; ++its.first) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1267 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, | 1266 return SetCookieWithCreationTimeAndOptions(url, cookie_line, creation_time, |
| 1268 CookieOptions()); | 1267 CookieOptions()); |
| 1269 } | 1268 } |
| 1270 | 1269 |
| 1271 int CookieMonster::DeleteSessionCookies() { | 1270 int CookieMonster::DeleteSessionCookies() { |
| 1272 DCHECK(thread_checker_.CalledOnValidThread()); | 1271 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1273 | 1272 |
| 1274 int num_deleted = 0; | 1273 int num_deleted = 0; |
| 1275 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { | 1274 for (CookieMap::iterator it = cookies_.begin(); it != cookies_.end();) { |
| 1276 CookieMap::iterator curit = it; | 1275 CookieMap::iterator curit = it; |
| 1277 CanonicalCookie* cc = curit->second; | 1276 CanonicalCookie* cc = curit->second.get(); |
| 1278 ++it; | 1277 ++it; |
| 1279 | 1278 |
| 1280 if (!cc->IsPersistent()) { | 1279 if (!cc->IsPersistent()) { |
| 1281 InternalDeleteCookie(curit, true, /*sync_to_store*/ | 1280 InternalDeleteCookie(curit, true, /*sync_to_store*/ |
| 1282 DELETE_COOKIE_EXPIRED); | 1281 DELETE_COOKIE_EXPIRED); |
| 1283 ++num_deleted; | 1282 ++num_deleted; |
| 1284 } | 1283 } |
| 1285 } | 1284 } |
| 1286 | 1285 |
| 1287 return num_deleted; | 1286 return num_deleted; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1325 } else { | 1324 } else { |
| 1326 // The logic in the conditional is redundant, but it makes trials of | 1325 // The logic in the conditional is redundant, but it makes trials of |
| 1327 // the Finch experiment more explicit. | 1326 // the Finch experiment more explicit. |
| 1328 fetch_strategy_ = kAlwaysFetch; | 1327 fetch_strategy_ = kAlwaysFetch; |
| 1329 } | 1328 } |
| 1330 } | 1329 } |
| 1331 | 1330 |
| 1332 return fetch_strategy_ == kAlwaysFetch; | 1331 return fetch_strategy_ == kAlwaysFetch; |
| 1333 } | 1332 } |
| 1334 | 1333 |
| 1335 void CookieMonster::OnLoaded(TimeTicks beginning_time, | 1334 void CookieMonster::OnLoaded( |
| 1336 const std::vector<CanonicalCookie*>& cookies) { | 1335 TimeTicks beginning_time, |
| 1336 std::vector<std::unique_ptr<CanonicalCookie>> cookies) { |
| 1337 DCHECK(thread_checker_.CalledOnValidThread()); | 1337 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1338 StoreLoadedCookies(cookies); | 1338 StoreLoadedCookies(std::move(cookies)); |
| 1339 histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time); | 1339 histogram_time_blocked_on_load_->AddTime(TimeTicks::Now() - beginning_time); |
| 1340 | 1340 |
| 1341 // Invoke the task queue of cookie request. | 1341 // Invoke the task queue of cookie request. |
| 1342 InvokeQueue(); | 1342 InvokeQueue(); |
| 1343 } | 1343 } |
| 1344 | 1344 |
| 1345 void CookieMonster::OnKeyLoaded(const std::string& key, | 1345 void CookieMonster::OnKeyLoaded( |
| 1346 const std::vector<CanonicalCookie*>& cookies) { | 1346 const std::string& key, |
| 1347 std::vector<std::unique_ptr<CanonicalCookie>> cookies) { |
| 1347 DCHECK(thread_checker_.CalledOnValidThread()); | 1348 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1348 | 1349 |
| 1349 StoreLoadedCookies(cookies); | 1350 StoreLoadedCookies(std::move(cookies)); |
| 1350 | 1351 |
| 1351 auto tasks_pending_for_key = tasks_pending_for_key_.find(key); | 1352 auto tasks_pending_for_key = tasks_pending_for_key_.find(key); |
| 1352 | 1353 |
| 1353 // TODO(mmenke): Can this be turned into a DCHECK? | 1354 // TODO(mmenke): Can this be turned into a DCHECK? |
| 1354 if (tasks_pending_for_key == tasks_pending_for_key_.end()) | 1355 if (tasks_pending_for_key == tasks_pending_for_key_.end()) |
| 1355 return; | 1356 return; |
| 1356 | 1357 |
| 1357 // Run all tasks for the key. Note that running a task can result in multiple | 1358 // Run all tasks for the key. Note that running a task can result in multiple |
| 1358 // tasks being added to the back of the deque. | 1359 // tasks being added to the back of the deque. |
| 1359 while (!tasks_pending_for_key->second.empty()) { | 1360 while (!tasks_pending_for_key->second.empty()) { |
| 1360 scoped_refptr<CookieMonsterTask> task = | 1361 scoped_refptr<CookieMonsterTask> task = |
| 1361 tasks_pending_for_key->second.front(); | 1362 tasks_pending_for_key->second.front(); |
| 1362 tasks_pending_for_key->second.pop_front(); | 1363 tasks_pending_for_key->second.pop_front(); |
| 1363 | 1364 |
| 1364 task->Run(); | 1365 task->Run(); |
| 1365 } | 1366 } |
| 1366 | 1367 |
| 1367 tasks_pending_for_key_.erase(tasks_pending_for_key); | 1368 tasks_pending_for_key_.erase(tasks_pending_for_key); |
| 1368 | 1369 |
| 1369 // This has to be done last, in case running a task queues a new task for the | 1370 // This has to be done last, in case running a task queues a new task for the |
| 1370 // key, to ensure tasks are run in the correct order. | 1371 // key, to ensure tasks are run in the correct order. |
| 1371 keys_loaded_.insert(key); | 1372 keys_loaded_.insert(key); |
| 1372 } | 1373 } |
| 1373 | 1374 |
| 1374 void CookieMonster::StoreLoadedCookies( | 1375 void CookieMonster::StoreLoadedCookies( |
| 1375 const std::vector<CanonicalCookie*>& cookies) { | 1376 std::vector<std::unique_ptr<CanonicalCookie>> cookies) { |
| 1376 DCHECK(thread_checker_.CalledOnValidThread()); | 1377 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1377 | 1378 |
| 1378 // TODO(erikwright): Remove ScopedTracker below once crbug.com/457528 is | 1379 // TODO(erikwright): Remove ScopedTracker below once crbug.com/457528 is |
| 1379 // fixed. | 1380 // fixed. |
| 1380 tracked_objects::ScopedTracker tracking_profile( | 1381 tracked_objects::ScopedTracker tracking_profile( |
| 1381 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 1382 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 1382 "457528 CookieMonster::StoreLoadedCookies")); | 1383 "457528 CookieMonster::StoreLoadedCookies")); |
| 1383 | 1384 |
| 1384 // Even if a key is expired, insert it so it can be garbage collected, | 1385 // Even if a key is expired, insert it so it can be garbage collected, |
| 1385 // removed, and sync'd. | 1386 // removed, and sync'd. |
| 1386 CookieItVector cookies_with_control_chars; | 1387 CookieItVector cookies_with_control_chars; |
| 1387 | 1388 |
| 1388 for (std::vector<CanonicalCookie*>::const_iterator it = cookies.begin(); | 1389 for (auto& cookie : cookies) { |
| 1389 it != cookies.end(); ++it) { | 1390 int64_t cookie_creation_time = cookie->CreationDate().ToInternalValue(); |
| 1390 int64_t cookie_creation_time = (*it)->CreationDate().ToInternalValue(); | |
| 1391 | 1391 |
| 1392 if (creation_times_.insert(cookie_creation_time).second) { | 1392 if (creation_times_.insert(cookie_creation_time).second) { |
| 1393 CookieMap::iterator inserted = | 1393 CanonicalCookie* cookie_ptr = cookie.get(); |
| 1394 InternalInsertCookie(GetKey((*it)->Domain()), *it, GURL(), false); | 1394 CookieMap::iterator inserted = InternalInsertCookie( |
| 1395 const Time cookie_access_time((*it)->LastAccessDate()); | 1395 GetKey(cookie_ptr->Domain()), std::move(cookie), GURL(), false); |
| 1396 const Time cookie_access_time(cookie_ptr->LastAccessDate()); |
| 1396 if (earliest_access_time_.is_null() || | 1397 if (earliest_access_time_.is_null() || |
| 1397 cookie_access_time < earliest_access_time_) | 1398 cookie_access_time < earliest_access_time_) |
| 1398 earliest_access_time_ = cookie_access_time; | 1399 earliest_access_time_ = cookie_access_time; |
| 1399 | 1400 |
| 1400 if (ContainsControlCharacter((*it)->Name()) || | 1401 if (ContainsControlCharacter(cookie_ptr->Name()) || |
| 1401 ContainsControlCharacter((*it)->Value())) { | 1402 ContainsControlCharacter(cookie_ptr->Value())) { |
| 1402 cookies_with_control_chars.push_back(inserted); | 1403 cookies_with_control_chars.push_back(inserted); |
| 1403 } | 1404 } |
| 1404 } else { | 1405 } else { |
| 1405 LOG(ERROR) << base::StringPrintf( | 1406 LOG(ERROR) << base::StringPrintf( |
| 1406 "Found cookies with duplicate creation " | 1407 "Found cookies with duplicate creation " |
| 1407 "times in backing store: " | 1408 "times in backing store: " |
| 1408 "{name='%s', domain='%s', path='%s'}", | 1409 "{name='%s', domain='%s', path='%s'}", |
| 1409 (*it)->Name().c_str(), (*it)->Domain().c_str(), | 1410 cookie->Name().c_str(), cookie->Domain().c_str(), |
| 1410 (*it)->Path().c_str()); | 1411 cookie->Path().c_str()); |
| 1411 // We've been given ownership of the cookie and are throwing it | |
| 1412 // away; reclaim the space. | |
| 1413 delete (*it); | |
| 1414 } | 1412 } |
| 1415 } | 1413 } |
| 1416 | 1414 |
| 1417 // Any cookies that contain control characters that we have loaded from the | 1415 // Any cookies that contain control characters that we have loaded from the |
| 1418 // persistent store should be deleted. See http://crbug.com/238041. | 1416 // persistent store should be deleted. See http://crbug.com/238041. |
| 1419 for (CookieItVector::iterator it = cookies_with_control_chars.begin(); | 1417 for (CookieItVector::iterator it = cookies_with_control_chars.begin(); |
| 1420 it != cookies_with_control_chars.end();) { | 1418 it != cookies_with_control_chars.end();) { |
| 1421 CookieItVector::iterator curit = it; | 1419 CookieItVector::iterator curit = it; |
| 1422 ++it; | 1420 ++it; |
| 1423 | 1421 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1491 typedef std::map<CookieSignature, CookieSet> EquivalenceMap; | 1489 typedef std::map<CookieSignature, CookieSet> EquivalenceMap; |
| 1492 EquivalenceMap equivalent_cookies; | 1490 EquivalenceMap equivalent_cookies; |
| 1493 | 1491 |
| 1494 // The number of duplicate cookies that have been found. | 1492 // The number of duplicate cookies that have been found. |
| 1495 int num_duplicates = 0; | 1493 int num_duplicates = 0; |
| 1496 | 1494 |
| 1497 // Iterate through all of the cookies in our range, and insert them into | 1495 // Iterate through all of the cookies in our range, and insert them into |
| 1498 // the equivalence map. | 1496 // the equivalence map. |
| 1499 for (CookieMap::iterator it = begin; it != end; ++it) { | 1497 for (CookieMap::iterator it = begin; it != end; ++it) { |
| 1500 DCHECK_EQ(key, it->first); | 1498 DCHECK_EQ(key, it->first); |
| 1501 CanonicalCookie* cookie = it->second; | 1499 CanonicalCookie* cookie = it->second.get(); |
| 1502 | 1500 |
| 1503 CookieSignature signature(cookie->Name(), cookie->Domain(), cookie->Path()); | 1501 CookieSignature signature(cookie->Name(), cookie->Domain(), cookie->Path()); |
| 1504 CookieSet& set = equivalent_cookies[signature]; | 1502 CookieSet& set = equivalent_cookies[signature]; |
| 1505 | 1503 |
| 1506 // We found a duplicate! | 1504 // We found a duplicate! |
| 1507 if (!set.empty()) | 1505 if (!set.empty()) |
| 1508 num_duplicates++; | 1506 num_duplicates++; |
| 1509 | 1507 |
| 1510 // We save the iterator into |cookies_| rather than the actual cookie | 1508 // We save the iterator into |cookies_| rather than the actual cookie |
| 1511 // pointer, since we may need to delete it later. | 1509 // pointer, since we may need to delete it later. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1575 void CookieMonster::FindCookiesForKey(const std::string& key, | 1573 void CookieMonster::FindCookiesForKey(const std::string& key, |
| 1576 const GURL& url, | 1574 const GURL& url, |
| 1577 const CookieOptions& options, | 1575 const CookieOptions& options, |
| 1578 const Time& current, | 1576 const Time& current, |
| 1579 std::vector<CanonicalCookie*>* cookies) { | 1577 std::vector<CanonicalCookie*>* cookies) { |
| 1580 DCHECK(thread_checker_.CalledOnValidThread()); | 1578 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1581 | 1579 |
| 1582 for (CookieMapItPair its = cookies_.equal_range(key); | 1580 for (CookieMapItPair its = cookies_.equal_range(key); |
| 1583 its.first != its.second;) { | 1581 its.first != its.second;) { |
| 1584 CookieMap::iterator curit = its.first; | 1582 CookieMap::iterator curit = its.first; |
| 1585 CanonicalCookie* cc = curit->second; | 1583 CanonicalCookie* cc = curit->second.get(); |
| 1586 ++its.first; | 1584 ++its.first; |
| 1587 | 1585 |
| 1588 // If the cookie is expired, delete it. | 1586 // If the cookie is expired, delete it. |
| 1589 if (cc->IsExpired(current)) { | 1587 if (cc->IsExpired(current)) { |
| 1590 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED); | 1588 InternalDeleteCookie(curit, true, DELETE_COOKIE_EXPIRED); |
| 1591 continue; | 1589 continue; |
| 1592 } | 1590 } |
| 1593 | 1591 |
| 1594 // Filter out cookies that should not be included for a request to the | 1592 // Filter out cookies that should not be included for a request to the |
| 1595 // given |url|. HTTP only cookies are filtered depending on the passed | 1593 // given |url|. HTTP only cookies are filtered depending on the passed |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1616 | 1614 |
| 1617 bool found_equivalent_cookie = false; | 1615 bool found_equivalent_cookie = false; |
| 1618 bool skipped_httponly = false; | 1616 bool skipped_httponly = false; |
| 1619 bool skipped_secure_cookie = false; | 1617 bool skipped_secure_cookie = false; |
| 1620 | 1618 |
| 1621 histogram_cookie_delete_equivalent_->Add(COOKIE_DELETE_EQUIVALENT_ATTEMPT); | 1619 histogram_cookie_delete_equivalent_->Add(COOKIE_DELETE_EQUIVALENT_ATTEMPT); |
| 1622 | 1620 |
| 1623 for (CookieMapItPair its = cookies_.equal_range(key); | 1621 for (CookieMapItPair its = cookies_.equal_range(key); |
| 1624 its.first != its.second;) { | 1622 its.first != its.second;) { |
| 1625 CookieMap::iterator curit = its.first; | 1623 CookieMap::iterator curit = its.first; |
| 1626 CanonicalCookie* cc = curit->second; | 1624 CanonicalCookie* cc = curit->second.get(); |
| 1627 ++its.first; | 1625 ++its.first; |
| 1628 | 1626 |
| 1629 // If strict secure cookies is being enforced, then the equivalency | 1627 // If strict secure cookies is being enforced, then the equivalency |
| 1630 // requirements are looser. If the cookie is being set from an insecure | 1628 // requirements are looser. If the cookie is being set from an insecure |
| 1631 // scheme, then if a cookie already exists with the same name and it is | 1629 // scheme, then if a cookie already exists with the same name and it is |
| 1632 // Secure, then the cookie should *not* be updated if they domain-match and | 1630 // Secure, then the cookie should *not* be updated if they domain-match and |
| 1633 // ignoring the path attribute. | 1631 // ignoring the path attribute. |
| 1634 // | 1632 // |
| 1635 // See: https://tools.ietf.org/html/draft-west-leave-secure-cookies-alone | 1633 // See: https://tools.ietf.org/html/draft-west-leave-secure-cookies-alone |
| 1636 if (enforce_strict_secure && cc->IsSecure() && | 1634 if (enforce_strict_secure && cc->IsSecure() && |
| (...skipping 30 matching lines...) Expand all Loading... |
| 1667 : DELETE_COOKIE_OVERWRITE); | 1665 : DELETE_COOKIE_OVERWRITE); |
| 1668 } | 1666 } |
| 1669 found_equivalent_cookie = true; | 1667 found_equivalent_cookie = true; |
| 1670 } | 1668 } |
| 1671 } | 1669 } |
| 1672 return skipped_httponly || skipped_secure_cookie; | 1670 return skipped_httponly || skipped_secure_cookie; |
| 1673 } | 1671 } |
| 1674 | 1672 |
| 1675 CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie( | 1673 CookieMonster::CookieMap::iterator CookieMonster::InternalInsertCookie( |
| 1676 const std::string& key, | 1674 const std::string& key, |
| 1677 CanonicalCookie* cc, | 1675 std::unique_ptr<CanonicalCookie> cc, |
| 1678 const GURL& source_url, | 1676 const GURL& source_url, |
| 1679 bool sync_to_store) { | 1677 bool sync_to_store) { |
| 1680 DCHECK(thread_checker_.CalledOnValidThread()); | 1678 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1679 CanonicalCookie* cc_ptr = cc.get(); |
| 1681 | 1680 |
| 1682 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() && | 1681 if ((cc_ptr->IsPersistent() || persist_session_cookies_) && store_.get() && |
| 1683 sync_to_store) | 1682 sync_to_store) |
| 1684 store_->AddCookie(*cc); | 1683 store_->AddCookie(*cc_ptr); |
| 1685 CookieMap::iterator inserted = | 1684 CookieMap::iterator inserted = |
| 1686 cookies_.insert(CookieMap::value_type(key, cc)); | 1685 cookies_.insert(CookieMap::value_type(key, std::move(cc))); |
| 1687 if (delegate_.get()) | 1686 if (delegate_.get()) { |
| 1688 delegate_->OnCookieChanged(*cc, false, CookieStore::ChangeCause::INSERTED); | 1687 delegate_->OnCookieChanged(*cc_ptr, false, |
| 1688 CookieStore::ChangeCause::INSERTED); |
| 1689 } |
| 1689 | 1690 |
| 1690 // See InitializeHistograms() for details. | 1691 // See InitializeHistograms() for details. |
| 1691 int32_t type_sample = cc->SameSite() != CookieSameSite::NO_RESTRICTION | 1692 int32_t type_sample = cc_ptr->SameSite() != CookieSameSite::NO_RESTRICTION |
| 1692 ? 1 << COOKIE_TYPE_SAME_SITE | 1693 ? 1 << COOKIE_TYPE_SAME_SITE |
| 1693 : 0; | 1694 : 0; |
| 1694 type_sample |= cc->IsHttpOnly() ? 1 << COOKIE_TYPE_HTTPONLY : 0; | 1695 type_sample |= cc_ptr->IsHttpOnly() ? 1 << COOKIE_TYPE_HTTPONLY : 0; |
| 1695 type_sample |= cc->IsSecure() ? 1 << COOKIE_TYPE_SECURE : 0; | 1696 type_sample |= cc_ptr->IsSecure() ? 1 << COOKIE_TYPE_SECURE : 0; |
| 1696 histogram_cookie_type_->Add(type_sample); | 1697 histogram_cookie_type_->Add(type_sample); |
| 1697 | 1698 |
| 1698 // Histogram the type of scheme used on URLs that set cookies. This | 1699 // Histogram the type of scheme used on URLs that set cookies. This |
| 1699 // intentionally includes cookies that are set or overwritten by | 1700 // intentionally includes cookies that are set or overwritten by |
| 1700 // http:// URLs, but not cookies that are cleared by http:// URLs, to | 1701 // http:// URLs, but not cookies that are cleared by http:// URLs, to |
| 1701 // understand if the former behavior can be deprecated for Secure | 1702 // understand if the former behavior can be deprecated for Secure |
| 1702 // cookies. | 1703 // cookies. |
| 1703 if (!source_url.is_empty()) { | 1704 if (!source_url.is_empty()) { |
| 1704 CookieSource cookie_source_sample; | 1705 CookieSource cookie_source_sample; |
| 1705 if (source_url.SchemeIsCryptographic()) { | 1706 if (source_url.SchemeIsCryptographic()) { |
| 1706 cookie_source_sample = | 1707 cookie_source_sample = |
| 1707 cc->IsSecure() ? COOKIE_SOURCE_SECURE_COOKIE_CRYPTOGRAPHIC_SCHEME | 1708 cc_ptr->IsSecure() |
| 1708 : COOKIE_SOURCE_NONSECURE_COOKIE_CRYPTOGRAPHIC_SCHEME; | 1709 ? COOKIE_SOURCE_SECURE_COOKIE_CRYPTOGRAPHIC_SCHEME |
| 1710 : COOKIE_SOURCE_NONSECURE_COOKIE_CRYPTOGRAPHIC_SCHEME; |
| 1709 } else { | 1711 } else { |
| 1710 cookie_source_sample = | 1712 cookie_source_sample = |
| 1711 cc->IsSecure() | 1713 cc_ptr->IsSecure() |
| 1712 ? COOKIE_SOURCE_SECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME | 1714 ? COOKIE_SOURCE_SECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME |
| 1713 : COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME; | 1715 : COOKIE_SOURCE_NONSECURE_COOKIE_NONCRYPTOGRAPHIC_SCHEME; |
| 1714 } | 1716 } |
| 1715 histogram_cookie_source_scheme_->Add(cookie_source_sample); | 1717 histogram_cookie_source_scheme_->Add(cookie_source_sample); |
| 1716 } | 1718 } |
| 1717 | 1719 |
| 1718 RunCookieChangedCallbacks(*cc, CookieStore::ChangeCause::INSERTED); | 1720 RunCookieChangedCallbacks(*cc_ptr, CookieStore::ChangeCause::INSERTED); |
| 1719 | 1721 |
| 1720 return inserted; | 1722 return inserted; |
| 1721 } | 1723 } |
| 1722 | 1724 |
| 1723 bool CookieMonster::SetCookieWithCreationTimeAndOptions( | 1725 bool CookieMonster::SetCookieWithCreationTimeAndOptions( |
| 1724 const GURL& url, | 1726 const GURL& url, |
| 1725 const std::string& cookie_line, | 1727 const std::string& cookie_line, |
| 1726 const Time& creation_time_or_null, | 1728 const Time& creation_time_or_null, |
| 1727 const CookieOptions& options) { | 1729 const CookieOptions& options) { |
| 1728 DCHECK(thread_checker_.CalledOnValidThread()); | 1730 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1775 | 1777 |
| 1776 // Realize that we might be setting an expired cookie, and the only point | 1778 // Realize that we might be setting an expired cookie, and the only point |
| 1777 // was to delete the cookie which we've already done. | 1779 // was to delete the cookie which we've already done. |
| 1778 if (!already_expired) { | 1780 if (!already_expired) { |
| 1779 // See InitializeHistograms() for details. | 1781 // See InitializeHistograms() for details. |
| 1780 if (cc->IsPersistent()) { | 1782 if (cc->IsPersistent()) { |
| 1781 histogram_expiration_duration_minutes_->Add( | 1783 histogram_expiration_duration_minutes_->Add( |
| 1782 (cc->ExpiryDate() - creation_time).InMinutes()); | 1784 (cc->ExpiryDate() - creation_time).InMinutes()); |
| 1783 } | 1785 } |
| 1784 | 1786 |
| 1785 InternalInsertCookie(key, cc.release(), source_url, true); | 1787 InternalInsertCookie(key, std::move(cc), source_url, true); |
| 1786 } else { | 1788 } else { |
| 1787 VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie."; | 1789 VLOG(kVlogSetCookies) << "SetCookie() not storing already expired cookie."; |
| 1788 } | 1790 } |
| 1789 | 1791 |
| 1790 // We assume that hopefully setting a cookie will be less common than | 1792 // We assume that hopefully setting a cookie will be less common than |
| 1791 // querying a cookie. Since setting a cookie can put us over our limits, | 1793 // querying a cookie. Since setting a cookie can put us over our limits, |
| 1792 // make sure that we garbage collect... We can also make the assumption that | 1794 // make sure that we garbage collect... We can also make the assumption that |
| 1793 // if a cookie was set, in the common case it will be used soon after, | 1795 // if a cookie was set, in the common case it will be used soon after, |
| 1794 // and we will purge the expired cookies in GetCookies(). | 1796 // and we will purge the expired cookies in GetCookies(). |
| 1795 GarbageCollect(creation_time, key, options.enforce_strict_secure()); | 1797 GarbageCollect(creation_time, key, options.enforce_strict_secure()); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1840 // Ideally, this would be asserted up where we define kChangeCauseMapping, | 1842 // Ideally, this would be asserted up where we define kChangeCauseMapping, |
| 1841 // but DeletionCause's visibility (or lack thereof) forces us to make | 1843 // but DeletionCause's visibility (or lack thereof) forces us to make |
| 1842 // this check here. | 1844 // this check here. |
| 1843 static_assert(arraysize(kChangeCauseMapping) == DELETE_COOKIE_LAST_ENTRY + 1, | 1845 static_assert(arraysize(kChangeCauseMapping) == DELETE_COOKIE_LAST_ENTRY + 1, |
| 1844 "kChangeCauseMapping size should match DeletionCause size"); | 1846 "kChangeCauseMapping size should match DeletionCause size"); |
| 1845 | 1847 |
| 1846 // See InitializeHistograms() for details. | 1848 // See InitializeHistograms() for details. |
| 1847 if (deletion_cause != DELETE_COOKIE_DONT_RECORD) | 1849 if (deletion_cause != DELETE_COOKIE_DONT_RECORD) |
| 1848 histogram_cookie_deletion_cause_->Add(deletion_cause); | 1850 histogram_cookie_deletion_cause_->Add(deletion_cause); |
| 1849 | 1851 |
| 1850 CanonicalCookie* cc = it->second; | 1852 CanonicalCookie* cc = it->second.get(); |
| 1851 VLOG(kVlogSetCookies) << "InternalDeleteCookie()" | 1853 VLOG(kVlogSetCookies) << "InternalDeleteCookie()" |
| 1852 << ", cause:" << deletion_cause | 1854 << ", cause:" << deletion_cause |
| 1853 << ", cc: " << cc->DebugString(); | 1855 << ", cc: " << cc->DebugString(); |
| 1854 | 1856 |
| 1855 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() && | 1857 if ((cc->IsPersistent() || persist_session_cookies_) && store_.get() && |
| 1856 sync_to_store) | 1858 sync_to_store) |
| 1857 store_->DeleteCookie(*cc); | 1859 store_->DeleteCookie(*cc); |
| 1858 ChangeCausePair mapping = kChangeCauseMapping[deletion_cause]; | 1860 ChangeCausePair mapping = kChangeCauseMapping[deletion_cause]; |
| 1859 if (delegate_.get() && mapping.notify) | 1861 if (delegate_.get() && mapping.notify) |
| 1860 delegate_->OnCookieChanged(*cc, true, mapping.cause); | 1862 delegate_->OnCookieChanged(*cc, true, mapping.cause); |
| 1861 RunCookieChangedCallbacks(*cc, mapping.cause); | 1863 RunCookieChangedCallbacks(*cc, mapping.cause); |
| 1862 cookies_.erase(it); | 1864 cookies_.erase(it); |
| 1863 delete cc; | |
| 1864 } | 1865 } |
| 1865 | 1866 |
| 1866 // Domain expiry behavior is unchanged by key/expiry scheme (the | 1867 // Domain expiry behavior is unchanged by key/expiry scheme (the |
| 1867 // meaning of the key is different, but that's not visible to this routine). | 1868 // meaning of the key is different, but that's not visible to this routine). |
| 1868 size_t CookieMonster::GarbageCollect(const Time& current, | 1869 size_t CookieMonster::GarbageCollect(const Time& current, |
| 1869 const std::string& key, | 1870 const std::string& key, |
| 1870 bool enforce_strict_secure) { | 1871 bool enforce_strict_secure) { |
| 1871 DCHECK(thread_checker_.CalledOnValidThread()); | 1872 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1872 | 1873 |
| 1873 size_t num_deleted = 0; | 1874 size_t num_deleted = 0; |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2038 cookies_count_possibly_to_be_deleted -= | 2039 cookies_count_possibly_to_be_deleted -= |
| 2039 std::max(secure_cookies, to_protect - secure_cookies); | 2040 std::max(secure_cookies, to_protect - secure_cookies); |
| 2040 } else { | 2041 } else { |
| 2041 cookies_count_possibly_to_be_deleted -= to_protect; | 2042 cookies_count_possibly_to_be_deleted -= to_protect; |
| 2042 } | 2043 } |
| 2043 | 2044 |
| 2044 size_t removed = 0u; | 2045 size_t removed = 0u; |
| 2045 size_t current = 0u; | 2046 size_t current = 0u; |
| 2046 while ((removed < purge_goal && current < cookies->size()) && | 2047 while ((removed < purge_goal && current < cookies->size()) && |
| 2047 cookies_count_possibly_to_be_deleted > 0) { | 2048 cookies_count_possibly_to_be_deleted > 0) { |
| 2048 const CanonicalCookie* current_cookie = cookies->at(current)->second; | 2049 const CanonicalCookie* current_cookie = cookies->at(current)->second.get(); |
| 2049 // Only delete the current cookie if the priority is equal to | 2050 // Only delete the current cookie if the priority is equal to |
| 2050 // the current level. | 2051 // the current level. |
| 2051 if (IsCookieEligibleForEviction(priority, protect_secure_cookies, | 2052 if (IsCookieEligibleForEviction(priority, protect_secure_cookies, |
| 2052 current_cookie)) { | 2053 current_cookie)) { |
| 2053 InternalDeleteCookie(cookies->at(current), true, | 2054 InternalDeleteCookie(cookies->at(current), true, |
| 2054 DELETE_COOKIE_EVICTED_DOMAIN); | 2055 DELETE_COOKIE_EVICTED_DOMAIN); |
| 2055 cookies->erase(cookies->begin() + current); | 2056 cookies->erase(cookies->begin() + current); |
| 2056 removed++; | 2057 removed++; |
| 2057 cookies_count_possibly_to_be_deleted--; | 2058 cookies_count_possibly_to_be_deleted--; |
| 2058 } else { | 2059 } else { |
| (...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2385 it != hook_map_.end(); ++it) { | 2386 it != hook_map_.end(); ++it) { |
| 2386 std::pair<GURL, std::string> key = it->first; | 2387 std::pair<GURL, std::string> key = it->first; |
| 2387 if (cookie.IncludeForRequestURL(key.first, opts) && | 2388 if (cookie.IncludeForRequestURL(key.first, opts) && |
| 2388 cookie.Name() == key.second) { | 2389 cookie.Name() == key.second) { |
| 2389 it->second->Notify(cookie, cause); | 2390 it->second->Notify(cookie, cause); |
| 2390 } | 2391 } |
| 2391 } | 2392 } |
| 2392 } | 2393 } |
| 2393 | 2394 |
| 2394 } // namespace net | 2395 } // namespace net |
| OLD | NEW |