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.
5 #include "ios/net/cookies/cookie_cache.h"
9 #include "base/logging.h"
10 #include "net/cookies/cookie_options.h"
14 CookieCache::CookieCache() {
17 CookieCache::~CookieCache() {
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());
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(),
34 std::inserter(added_cookies
, added_cookies
.begin()),
35 CookieCache::CookieAndValueComparator());
36 std::set_difference(old_set
.begin(), old_set
.end(), new_set
.begin(),
38 std::inserter(removed_cookies
, removed_cookies
.begin()),
39 CookieCache::CookieAndValueComparator());
41 if (added_cookies
.empty() && removed_cookies
.empty())
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());
49 if (out_added_cookies
) {
50 out_added_cookies
->insert(out_added_cookies
->end(), added_cookies
.begin(),
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();
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();