Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 1 | // Copyright 2012 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_UUID_H_ |
| 6 | #define BASE_UUID_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
kylechar | d917b98 | 2023-11-30 22:52:44 | [diff] [blame] | 10 | #include <compare> |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 11 | #include <iosfwd> |
| 12 | #include <string> |
Helmut Januschka | 3968891 | 2024-04-05 01:58:37 | [diff] [blame] | 13 | #include <string_view> |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 14 | |
| 15 | #include "base/base_export.h" |
Tom Sepez | 1705e3f0 | 2024-10-25 01:39:18 | [diff] [blame] | 16 | #include "base/compiler_specific.h" |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 17 | #include "base/containers/span.h" |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 18 | |
| 19 | namespace base { |
| 20 | |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 21 | class BASE_EXPORT Uuid { |
| 22 | public: |
| 23 | // Length in bytes of the input required to format the input as a Uuid in the |
| 24 | // form of version 4. |
| 25 | static constexpr size_t kGuidV4InputLength = 16; |
| 26 | |
| 27 | // Generate a 128-bit random Uuid in the form of version 4. see RFC 4122, |
| 28 | // section 4.4. The format of Uuid version 4 must be |
| 29 | // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where y is one of [8, 9, a, b]. The |
| 30 | // hexadecimal values "a" through "f" are output as lower case characters. |
| 31 | // A cryptographically secure random source will be used, but consider using |
| 32 | // UnguessableToken for greater type-safety if Uuid format is unnecessary. |
| 33 | static Uuid GenerateRandomV4(); |
| 34 | |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 35 | // Returns a valid Uuid if the input string conforms to the Uuid format, and |
| 36 | // an invalid Uuid otherwise. Note that this does NOT check if the hexadecimal |
| 37 | // values "a" through "f" are in lower case characters. |
Helmut Januschka | 3968891 | 2024-04-05 01:58:37 | [diff] [blame] | 38 | static Uuid ParseCaseInsensitive(std::string_view input); |
| 39 | static Uuid ParseCaseInsensitive(std::u16string_view input); |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 40 | |
| 41 | // Similar to ParseCaseInsensitive(), but all hexadecimal values "a" through |
| 42 | // "f" must be lower case characters. |
Helmut Januschka | 3968891 | 2024-04-05 01:58:37 | [diff] [blame] | 43 | static Uuid ParseLowercase(std::string_view input); |
| 44 | static Uuid ParseLowercase(std::u16string_view input); |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 45 | |
| 46 | // Constructs an invalid Uuid. |
| 47 | Uuid(); |
| 48 | |
| 49 | Uuid(const Uuid& other); |
| 50 | Uuid& operator=(const Uuid& other); |
| 51 | Uuid(Uuid&& other); |
| 52 | Uuid& operator=(Uuid&& other); |
| 53 | |
| 54 | bool is_valid() const { return !lowercase_.empty(); } |
| 55 | |
| 56 | // Returns the Uuid in a lowercase string format if it is valid, and an empty |
| 57 | // string otherwise. The returned value is guaranteed to be parsed by |
| 58 | // ParseLowercase(). |
| 59 | // |
| 60 | // NOTE: While AsLowercaseString() is currently a trivial getter, callers |
| 61 | // should not treat it as such. When the internal type of base::Uuid changes, |
| 62 | // this will be a non-trivial converter. See the TODO above `lowercase_` for |
| 63 | // more context. |
Tom Sepez | 1705e3f0 | 2024-10-25 01:39:18 | [diff] [blame] | 64 | const std::string& AsLowercaseString() const LIFETIME_BOUND; |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 65 | |
| 66 | // Invalid Uuids are equal. |
kylechar | d917b98 | 2023-11-30 22:52:44 | [diff] [blame] | 67 | friend bool operator==(const Uuid&, const Uuid&) = default; |
| 68 | // Uuids are 128bit chunks of data so must be indistinguishable if equivalent. |
| 69 | friend std::strong_ordering operator<=>(const Uuid&, const Uuid&) = default; |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 70 | |
| 71 | private: |
| 72 | static Uuid FormatRandomDataAsV4Impl( |
| 73 | base::span<const uint8_t, kGuidV4InputLength> input); |
| 74 | |
Alison Gale | d965ba0 | 2024-04-26 21:50:54 | [diff] [blame] | 75 | // TODO(crbug.com/40108138): Consider using a different internal type. |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 76 | // Most existing representations of Uuids in the codebase use std::string, |
| 77 | // so matching the internal type will avoid inefficient string conversions |
| 78 | // during the migration to base::Uuid. |
| 79 | // |
| 80 | // The lowercase form of the Uuid. Empty for invalid Uuids. |
| 81 | std::string lowercase_; |
| 82 | }; |
| 83 | |
| 84 | // For runtime usage only. Do not store the result of this hash, as it may |
| 85 | // change in future Chromium revisions. |
| 86 | struct BASE_EXPORT UuidHash { |
Lei Zhang | f3c81cb | 2023-09-29 18:07:17 | [diff] [blame] | 87 | size_t operator()(const Uuid& uuid) const; |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | // Stream operator so Uuid objects can be used in logging statements. |
| 91 | BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Uuid& uuid); |
| 92 | |
Claudio DeSouza | d438f05 | 2023-03-30 22:30:45 | [diff] [blame] | 93 | } // namespace base |
| 94 | |
| 95 | #endif // BASE_UUID_H_ |