OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 // Provides utility structures for inserting a CanonicalCookie into a hash set. | 5 // Provides utility structures for inserting a CanonicalCookie into a hash set. |
6 // Two cookies are considered equal if their names, domains, and paths are | 6 // Two cookies are considered equal if their names, domains, and paths are |
7 // equivalent. | 7 // equivalent. |
8 | 8 |
9 #ifndef CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_ | 9 #ifndef CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_ |
10 #define CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_ | 10 #define CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_ |
11 | 11 |
12 #if defined(COMPILER_MSVC) | |
13 #include <functional> | |
14 #endif // COMPILER_MSVC | |
15 | |
16 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
17 #include "net/cookies/canonical_cookie.h" | 13 #include "net/cookies/canonical_cookie.h" |
18 | 14 |
19 namespace canonical_cookie { | 15 namespace canonical_cookie { |
20 | 16 |
21 // Returns a fast hash of a cookie, based on its name, domain, and path. | 17 // Returns a fast hash of a cookie, based on its name, domain, and path. |
22 size_t FastHash(const net::CanonicalCookie& cookie); | 18 size_t FastHash(const net::CanonicalCookie& cookie); |
23 | 19 |
24 #if defined(COMPILER_MSVC) | |
25 struct CanonicalCookieTraits { | |
26 static const size_t bucket_size = 4; | |
27 | |
28 // Returns a hash of |cookie|. | |
29 size_t operator()(const net::CanonicalCookie& cookie) const { | |
30 return FastHash(cookie); | |
31 } | |
32 | |
33 // The 'less' operator on cookies. We need to create a total ordering. We | |
34 // order lexigraphically, first by name, then path, then domain. Name is most | |
35 // likely to be distinct, so it is compared first, and domain is least likely | |
36 // to be distinct, so it is compared last. | |
37 bool operator()(const net::CanonicalCookie& cookie1, | |
38 const net::CanonicalCookie& cookie2) const { | |
39 std::less<std::string> less_than; | |
40 if (less_than(cookie1.Name(), cookie2.Name())) | |
41 return true; | |
42 if (less_than(cookie2.Name(), cookie1.Name())) | |
43 return false; | |
44 if (less_than(cookie1.Path(), cookie2.Path())) | |
45 return true; | |
46 if (less_than(cookie2.Path(), cookie1.Path())) | |
47 return false; | |
48 if (less_than(cookie1.Domain(), cookie2.Domain())) | |
49 return true; | |
50 if (less_than(cookie2.Domain(), cookie1.Domain())) | |
51 return false; | |
52 | |
53 // The cookies are equivalent. | |
54 return false; | |
55 } | |
56 }; | |
57 | |
58 typedef base::hash_set<net::CanonicalCookie, CanonicalCookieTraits> | |
59 CookieHashSet; | |
60 | |
61 #else // COMPILER_MSVC | |
62 | |
63 struct CanonicalCookieHasher { | 20 struct CanonicalCookieHasher { |
64 std::size_t operator()(const net::CanonicalCookie& cookie) const { | 21 std::size_t operator()(const net::CanonicalCookie& cookie) const { |
65 return FastHash(cookie); | 22 return FastHash(cookie); |
66 } | 23 } |
67 }; | 24 }; |
68 | 25 |
69 struct CanonicalCookieComparer { | 26 struct CanonicalCookieComparer { |
70 bool operator()(const net::CanonicalCookie& cookie1, | 27 bool operator()(const net::CanonicalCookie& cookie1, |
71 const net::CanonicalCookie& cookie2) const { | 28 const net::CanonicalCookie& cookie2) const { |
72 return cookie1.Name() == cookie2.Name() && | 29 return cookie1.Name() == cookie2.Name() && |
73 cookie1.Domain() == cookie2.Domain() && | 30 cookie1.Domain() == cookie2.Domain() && |
74 cookie1.Path() == cookie2.Path(); | 31 cookie1.Path() == cookie2.Path(); |
75 } | 32 } |
76 }; | 33 }; |
77 | 34 |
78 typedef base::hash_set<net::CanonicalCookie, | 35 typedef base::hash_set<net::CanonicalCookie, |
79 CanonicalCookieHasher, | 36 CanonicalCookieHasher, |
80 CanonicalCookieComparer> CookieHashSet; | 37 CanonicalCookieComparer> CookieHashSet; |
81 | 38 |
82 #endif // COMPILER_MSVC | |
83 | |
84 }; // namespace canonical_cookie | 39 }; // namespace canonical_cookie |
85 | 40 |
86 #endif // CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_ | 41 #endif // CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_ |
OLD | NEW |