summaryrefslogtreecommitdiffstats
path: root/chrome/browser/browsing_data/canonical_cookie_hash.h
blob: a6905df2fdbd9324e4e7efc2af8316bb7f9a8a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Provides utility structures for inserting a CanonicalCookie into a hash set.
// Two cookies are considered equal if their names, domains, and paths are
// equivalent.

#ifndef CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_
#define CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_

#if defined(COMPILER_MSVC)
#include <functional>
#endif  // COMPILER_MSVC

#include "base/containers/hash_tables.h"
#include "net/cookies/canonical_cookie.h"

namespace canonical_cookie {

// Returns a fast hash of a cookie, based on its name, domain, and path.
size_t FastHash(const net::CanonicalCookie& cookie);

#if defined(COMPILER_MSVC)
struct CanonicalCookieTraits {
  static const size_t bucket_size = 4;

  // Returns a hash of |cookie|.
  size_t operator()(const net::CanonicalCookie& cookie) const {
    return FastHash(cookie);
  }

  // The 'less' operator on cookies. We need to create a total ordering. We
  // order lexigraphically, first by name, then path, then domain. Name is most
  // likely to be distinct, so it is compared first, and domain is least likely
  // to be distinct, so it is compared last.
  bool operator()(const net::CanonicalCookie& cookie1,
                  const net::CanonicalCookie& cookie2) const {
    std::less<std::string> less_than;
    if (less_than(cookie1.Name(), cookie2.Name()))
      return true;
    if (less_than(cookie2.Name(), cookie1.Name()))
      return false;
    if (less_than(cookie1.Path(), cookie2.Path()))
      return true;
    if (less_than(cookie2.Path(), cookie1.Path()))
      return false;
    if (less_than(cookie1.Domain(), cookie2.Domain()))
      return true;
    if (less_than(cookie2.Domain(), cookie1.Domain()))
      return false;

    // The cookies are equivalent.
    return false;
  }
};

typedef base::hash_set<net::CanonicalCookie, CanonicalCookieTraits>
    CookieHashSet;

#else  // COMPILER_MSVC

struct CanonicalCookieHasher {
  std::size_t operator()(const net::CanonicalCookie& cookie) const {
    return FastHash(cookie);
  }
};

struct CanonicalCookieComparer {
  bool operator()(const net::CanonicalCookie& cookie1,
                  const net::CanonicalCookie& cookie2) const {
    return cookie1.Name() == cookie2.Name() &&
           cookie1.Domain() == cookie2.Domain() &&
           cookie1.Path() == cookie2.Path();
  }
};

typedef base::hash_set<net::CanonicalCookie,
                       CanonicalCookieHasher,
                       CanonicalCookieComparer> CookieHashSet;

#endif  // COMPILER_MSVC

};  // namespace canonical_cookie

#endif  // CHROME_BROWSER_BROWSING_DATA_CANONICAL_COOKIE_HASH_H_