blob: df981afec6745ead7e6d0d31752b5d364fed38f7 [file] [log] [blame]
Ken Rockot8c6991c72018-11-07 21:23:191// Copyright 2018 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.
4
5#ifndef BASE_TOKEN_H_
6#define BASE_TOKEN_H_
7
8#include <stdint.h>
9
Victor Costan57f635e2020-07-08 04:08:2210#include <string>
Ken Rockot8c6991c72018-11-07 21:23:1911#include <tuple>
12
13#include "base/base_export.h"
Daniel Chengc0581992019-03-29 04:52:5614#include "base/hash/hash.h"
Anton Bikineev7dd58ad2021-05-18 01:01:3915#include "third_party/abseil-cpp/absl/types/optional.h"
Ken Rockot8c6991c72018-11-07 21:23:1916
17namespace base {
18
19// A Token is a randomly chosen 128-bit integer. This class supports generation
20// from a cryptographically strong random source, or constexpr construction over
21// fixed values (e.g. to store a pre-generated constant value). Tokens are
22// similar in spirit and purpose to UUIDs, without many of the constraints and
23// expectations (such as byte layout and string representation) clasically
24// associated with UUIDs.
25class BASE_EXPORT Token {
26 public:
27 // Constructs a zero Token.
Victor Costan57f635e2020-07-08 04:08:2228 constexpr Token() = default;
Ken Rockot8c6991c72018-11-07 21:23:1929
30 // Constructs a Token with |high| and |low| as its contents.
31 constexpr Token(uint64_t high, uint64_t low) : high_(high), low_(low) {}
32
Victor Costan57f635e2020-07-08 04:08:2233 constexpr Token(const Token&) = default;
34 constexpr Token& operator=(const Token&) = default;
35 constexpr Token(Token&&) noexcept = default;
36 constexpr Token& operator=(Token&&) = default;
37
Ken Rockot8c6991c72018-11-07 21:23:1938 // Constructs a new Token with random |high| and |low| values taken from a
39 // cryptographically strong random source.
40 static Token CreateRandom();
41
42 // The high and low 64 bits of this Token.
Victor Costan57f635e2020-07-08 04:08:2243 constexpr uint64_t high() const { return high_; }
44 constexpr uint64_t low() const { return low_; }
Ken Rockot8c6991c72018-11-07 21:23:1945
Victor Costan57f635e2020-07-08 04:08:2246 constexpr bool is_zero() const { return high_ == 0 && low_ == 0; }
Ken Rockot8c6991c72018-11-07 21:23:1947
Victor Costan57f635e2020-07-08 04:08:2248 constexpr bool operator==(const Token& other) const {
Ken Rockot8c6991c72018-11-07 21:23:1949 return high_ == other.high_ && low_ == other.low_;
50 }
51
Victor Costan57f635e2020-07-08 04:08:2252 constexpr bool operator!=(const Token& other) const {
53 return !(*this == other);
54 }
Ken Rockot8c6991c72018-11-07 21:23:1955
Victor Costan57f635e2020-07-08 04:08:2256 constexpr bool operator<(const Token& other) const {
Ken Rockot8c6991c72018-11-07 21:23:1957 return std::tie(high_, low_) < std::tie(other.high_, other.low_);
58 }
59
60 // Generates a string representation of this Token useful for e.g. logging.
61 std::string ToString() const;
62
63 private:
64 // Note: Two uint64_t are used instead of uint8_t[16] in order to have a
65 // simpler implementation, paricularly for |ToString()|, |is_zero()|, and
66 // constexpr value construction.
Victor Costan57f635e2020-07-08 04:08:2267 uint64_t high_ = 0;
68 uint64_t low_ = 0;
Ken Rockot8c6991c72018-11-07 21:23:1969};
70
71// For use in std::unordered_map.
72struct TokenHash {
73 size_t operator()(const base::Token& token) const {
74 return base::HashInts64(token.high(), token.low());
75 }
76};
77
Collin Bakerffe1d0eb2019-08-05 23:00:4578class Pickle;
79class PickleIterator;
80
81// For serializing and deserializing Token values.
82BASE_EXPORT void WriteTokenToPickle(Pickle* pickle, const Token& token);
Anton Bikineev7dd58ad2021-05-18 01:01:3983BASE_EXPORT absl::optional<Token> ReadTokenFromPickle(
Collin Bakerffe1d0eb2019-08-05 23:00:4584 PickleIterator* pickle_iterator);
85
Ken Rockot8c6991c72018-11-07 21:23:1986} // namespace base
87
88#endif // BASE_TOKEN_H_