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