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 // Provides utility structures for inserting a CanonicalCookie into a hash set.
6 // Two cookies are considered equal if their names, domains, and paths are
9 #ifndef CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_
10 #define CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_
12 #if defined(COMPILER_MSVC)
14 #endif // COMPILER_MSVC
16 #include "base/containers/hash_tables.h"
17 #include "net/cookies/canonical_cookie.h"
19 namespace canonical_cookie
{
21 // Returns a fast hash of a cookie, based on its name, domain, and path.
22 size_t FastHash(const net::CanonicalCookie
& cookie
);
24 #if defined(COMPILER_MSVC)
25 struct CanonicalCookieTraits
{
26 static const size_t bucket_size
= 4;
28 // Returns a hash of |cookie|.
29 size_t operator()(const net::CanonicalCookie
& cookie
) const {
30 return FastHash(cookie
);
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()))
42 if (less_than(cookie2
.Name(), cookie1
.Name()))
44 if (less_than(cookie1
.Path(), cookie2
.Path()))
46 if (less_than(cookie2
.Path(), cookie1
.Path()))
48 if (less_than(cookie1
.Domain(), cookie2
.Domain()))
50 if (less_than(cookie2
.Domain(), cookie1
.Domain()))
53 // The cookies are equivalent.
58 typedef base::hash_set
<net::CanonicalCookie
, CanonicalCookieTraits
>
61 #else // COMPILER_MSVC
63 struct CanonicalCookieHasher
{
64 std::size_t operator()(const net::CanonicalCookie
& cookie
) const {
65 return FastHash(cookie
);
69 struct CanonicalCookieComparer
{
70 bool operator()(const net::CanonicalCookie
& cookie1
,
71 const net::CanonicalCookie
& cookie2
) const {
72 return cookie1
.Name() == cookie2
.Name() &&
73 cookie1
.Domain() == cookie2
.Domain() &&
74 cookie1
.Path() == cookie2
.Path();
78 typedef base::hash_set
<net::CanonicalCookie
,
79 CanonicalCookieHasher
,
80 CanonicalCookieComparer
> CookieHashSet
;
82 #endif // COMPILER_MSVC
84 }; // namespace canonical_cookie
86 #endif // CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_