Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / browsing_data / canonical_cookie_hash.h
bloba6905df2fdbd9324e4e7efc2af8316bb7f9a8a98
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
7 // equivalent.
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)
13 #include <functional>
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()))
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;
53 // The cookies are equivalent.
54 return false;
58 typedef base::hash_set<net::CanonicalCookie, CanonicalCookieTraits>
59 CookieHashSet;
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_