| 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) | 12 #if defined(COMPILER_MSVC) |
| 13 #include <functional> | 13 #include <functional> |
| 14 #endif // COMPILER_MSVC | 14 #endif // COMPILER_MSVC |
| 15 | 15 |
| 16 #include "base/containers/hash_tables.h" | 16 #include "base/containers/hash_tables.h" |
| 17 #include "net/cookies/canonical_cookie.h" | 17 #include "net/cookies/canonical_cookie.h" |
| 18 | 18 |
| 19 namespace canonical_cookie { | 19 namespace canonical_cookie { |
| 20 | 20 |
| 21 // Returns a fast hash of a cookie, based on its name, domain, and path. | 21 // Returns a fast hash of a cookie, based on its name, domain, and path. |
| 22 size_t FastHash(const net::CanonicalCookie& cookie); | 22 size_t FastHash(const net::CanonicalCookie& cookie); |
| 23 | 23 |
| 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 { | 24 struct CanonicalCookieHasher { |
| 64 std::size_t operator()(const net::CanonicalCookie& cookie) const { | 25 std::size_t operator()(const net::CanonicalCookie& cookie) const { |
| 65 return FastHash(cookie); | 26 return FastHash(cookie); |
| 66 } | 27 } |
| 67 }; | 28 }; |
| 68 | 29 |
| 69 struct CanonicalCookieComparer { | 30 struct CanonicalCookieComparer { |
| 70 bool operator()(const net::CanonicalCookie& cookie1, | 31 bool operator()(const net::CanonicalCookie& cookie1, |
| 71 const net::CanonicalCookie& cookie2) const { | 32 const net::CanonicalCookie& cookie2) const { |
| 72 return cookie1.Name() == cookie2.Name() && | 33 return cookie1.Name() == cookie2.Name() && |
| 73 cookie1.Domain() == cookie2.Domain() && | 34 cookie1.Domain() == cookie2.Domain() && |
| 74 cookie1.Path() == cookie2.Path(); | 35 cookie1.Path() == cookie2.Path(); |
| 75 } | 36 } |
| 76 }; | 37 }; |
| 77 | 38 |
| 78 typedef base::hash_set<net::CanonicalCookie, | 39 typedef base::hash_set<net::CanonicalCookie, |
| 79 CanonicalCookieHasher, | 40 CanonicalCookieHasher, |
| 80 CanonicalCookieComparer> CookieHashSet; | 41 CanonicalCookieComparer> CookieHashSet; |
| 81 | 42 |
| 82 #endif // COMPILER_MSVC | |
| 83 | |
| 84 }; // namespace canonical_cookie | 43 }; // namespace canonical_cookie |
| 85 | 44 |
| 86 #endif // CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_ | 45 #endif // CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_ |
| OLD | NEW |