token.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // 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. #ifndef BASE_TOKEN_H_
  5. #define BASE_TOKEN_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <tuple>
  9. #include "base/base_export.h"
  10. #include "base/containers/span.h"
  11. #include "base/hash/hash.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace base {
  14. // A Token is a randomly chosen 128-bit integer. This class supports generation
  15. // from a cryptographically strong random source, or constexpr construction over
  16. // fixed values (e.g. to store a pre-generated constant value). Tokens are
  17. // similar in spirit and purpose to UUIDs, without many of the constraints and
  18. // expectations (such as byte layout and string representation) clasically
  19. // associated with UUIDs.
  20. class BASE_EXPORT Token {
  21. public:
  22. // Constructs a zero Token.
  23. constexpr Token() = default;
  24. // Constructs a Token with |high| and |low| as its contents.
  25. constexpr Token(uint64_t high, uint64_t low) : words_{high, low} {}
  26. constexpr Token(const Token&) = default;
  27. constexpr Token& operator=(const Token&) = default;
  28. constexpr Token(Token&&) noexcept = default;
  29. constexpr Token& operator=(Token&&) = default;
  30. // Constructs a new Token with random |high| and |low| values taken from a
  31. // cryptographically strong random source.
  32. static Token CreateRandom();
  33. // The high and low 64 bits of this Token.
  34. constexpr uint64_t high() const { return words_[0]; }
  35. constexpr uint64_t low() const { return words_[1]; }
  36. constexpr bool is_zero() const { return words_[0] == 0 && words_[1] == 0; }
  37. span<const uint8_t, 16> AsBytes() const {
  38. return as_bytes(make_span(words_));
  39. }
  40. constexpr bool operator==(const Token& other) const {
  41. return words_[0] == other.words_[0] && words_[1] == other.words_[1];
  42. }
  43. constexpr bool operator!=(const Token& other) const {
  44. return !(*this == other);
  45. }
  46. constexpr bool operator<(const Token& other) const {
  47. return std::tie(words_[0], words_[1]) <
  48. std::tie(other.words_[0], other.words_[1]);
  49. }
  50. // Generates a string representation of this Token useful for e.g. logging.
  51. std::string ToString() const;
  52. // FromString is the opposite of ToString. It returns absl::nullopt if the
  53. // |string_representation| is invalid.
  54. static absl::optional<Token> FromString(StringPiece string_representation);
  55. private:
  56. // Note: Two uint64_t are used instead of uint8_t[16] in order to have a
  57. // simpler implementation, paricularly for |ToString()|, |is_zero()|, and
  58. // constexpr value construction.
  59. uint64_t words_[2] = {0, 0};
  60. };
  61. // For use in std::unordered_map.
  62. struct TokenHash {
  63. size_t operator()(const base::Token& token) const {
  64. return base::HashInts64(token.high(), token.low());
  65. }
  66. };
  67. class Pickle;
  68. class PickleIterator;
  69. // For serializing and deserializing Token values.
  70. BASE_EXPORT void WriteTokenToPickle(Pickle* pickle, const Token& token);
  71. BASE_EXPORT absl::optional<Token> ReadTokenFromPickle(
  72. PickleIterator* pickle_iterator);
  73. } // namespace base
  74. #endif // BASE_TOKEN_H_