blob: b236ff438e4a8b446ba342cc8a4ea97d77854b99 [file] [log] [blame]
Claudio DeSouzad438f052023-03-30 22:30:451// 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
kylechard917b982023-11-30 22:52:4410#include <compare>
Claudio DeSouzad438f052023-03-30 22:30:4511#include <iosfwd>
12#include <string>
Helmut Januschka39688912024-04-05 01:58:3713#include <string_view>
Claudio DeSouzad438f052023-03-30 22:30:4514
15#include "base/base_export.h"
Tom Sepez1705e3f02024-10-25 01:39:1816#include "base/compiler_specific.h"
Claudio DeSouzad438f052023-03-30 22:30:4517#include "base/containers/span.h"
Claudio DeSouzad438f052023-03-30 22:30:4518
19namespace base {
20
Claudio DeSouzad438f052023-03-30 22:30:4521class 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 DeSouzad438f052023-03-30 22:30:4535 // 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 Januschka39688912024-04-05 01:58:3738 static Uuid ParseCaseInsensitive(std::string_view input);
39 static Uuid ParseCaseInsensitive(std::u16string_view input);
Claudio DeSouzad438f052023-03-30 22:30:4540
41 // Similar to ParseCaseInsensitive(), but all hexadecimal values "a" through
42 // "f" must be lower case characters.
Helmut Januschka39688912024-04-05 01:58:3743 static Uuid ParseLowercase(std::string_view input);
44 static Uuid ParseLowercase(std::u16string_view input);
Claudio DeSouzad438f052023-03-30 22:30:4545
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 Sepez1705e3f02024-10-25 01:39:1864 const std::string& AsLowercaseString() const LIFETIME_BOUND;
Claudio DeSouzad438f052023-03-30 22:30:4565
66 // Invalid Uuids are equal.
kylechard917b982023-11-30 22:52:4467 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 DeSouzad438f052023-03-30 22:30:4570
71 private:
72 static Uuid FormatRandomDataAsV4Impl(
73 base::span<const uint8_t, kGuidV4InputLength> input);
74
Alison Galed965ba02024-04-26 21:50:5475 // TODO(crbug.com/40108138): Consider using a different internal type.
Claudio DeSouzad438f052023-03-30 22:30:4576 // 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.
86struct BASE_EXPORT UuidHash {
Lei Zhangf3c81cb2023-09-29 18:07:1787 size_t operator()(const Uuid& uuid) const;
Claudio DeSouzad438f052023-03-30 22:30:4588};
89
90// Stream operator so Uuid objects can be used in logging statements.
91BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Uuid& uuid);
92
Claudio DeSouzad438f052023-03-30 22:30:4593} // namespace base
94
95#endif // BASE_UUID_H_