OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ios/net/cookies/cookie_cache.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "net/cookies/cookie_options.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 CookieCache::CookieCache() { |
| 15 } |
| 16 |
| 17 CookieCache::~CookieCache() { |
| 18 } |
| 19 |
| 20 bool CookieCache::Update(const GURL& url, |
| 21 const std::string& name, |
| 22 const std::vector<net::CanonicalCookie>& new_cookies, |
| 23 std::vector<net::CanonicalCookie>* out_removed_cookies, |
| 24 std::vector<net::CanonicalCookie>* out_added_cookies) { |
| 25 CookieKey key(url, name); |
| 26 CookieSet old_set = cache_[key]; |
| 27 CookieSet new_set(new_cookies.begin(), new_cookies.end()); |
| 28 |
| 29 // Compute the changes and the removals. |
| 30 CookieSet added_cookies; |
| 31 CookieSet removed_cookies; |
| 32 std::set_difference(new_set.begin(), new_set.end(), old_set.begin(), |
| 33 old_set.end(), |
| 34 std::inserter(added_cookies, added_cookies.begin()), |
| 35 CookieCache::CookieAndValueComparator()); |
| 36 std::set_difference(old_set.begin(), old_set.end(), new_set.begin(), |
| 37 new_set.end(), |
| 38 std::inserter(removed_cookies, removed_cookies.begin()), |
| 39 CookieCache::CookieAndValueComparator()); |
| 40 |
| 41 if (added_cookies.empty() && removed_cookies.empty()) |
| 42 return false; |
| 43 |
| 44 cache_[key] = new_set; |
| 45 if (out_removed_cookies) { |
| 46 out_removed_cookies->insert(out_removed_cookies->end(), |
| 47 removed_cookies.begin(), removed_cookies.end()); |
| 48 } |
| 49 if (out_added_cookies) { |
| 50 out_added_cookies->insert(out_added_cookies->end(), added_cookies.begin(), |
| 51 added_cookies.end()); |
| 52 } |
| 53 return true; |
| 54 } |
| 55 |
| 56 bool CookieCache::CookieComparator::operator()( |
| 57 const net::CanonicalCookie& lhs, |
| 58 const net::CanonicalCookie& rhs) const { |
| 59 if (lhs.Domain() != rhs.Domain()) |
| 60 return lhs.Domain() < rhs.Domain(); |
| 61 if (lhs.Path() != rhs.Path()) |
| 62 return lhs.Path() < rhs.Path(); |
| 63 if (lhs.Name() != rhs.Name()) |
| 64 return lhs.Name() < rhs.Name(); |
| 65 return false; |
| 66 } |
| 67 |
| 68 bool CookieCache::CookieAndValueComparator::operator()( |
| 69 const net::CanonicalCookie& lhs, |
| 70 const net::CanonicalCookie& rhs) const { |
| 71 if (lhs.Domain() != rhs.Domain()) |
| 72 return lhs.Domain() < rhs.Domain(); |
| 73 if (lhs.Path() != rhs.Path()) |
| 74 return lhs.Path() < rhs.Path(); |
| 75 if (lhs.Name() != rhs.Name()) |
| 76 return lhs.Name() < rhs.Name(); |
| 77 if (lhs.Value() != rhs.Value()) |
| 78 return lhs.Value() < rhs.Value(); |
| 79 return false; |
| 80 } |
| 81 |
| 82 } // namespace net |
OLD | NEW |