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.
13 #include "base/base_export.h"
14 #include "base/containers/span.h"
15 #include "base/strings/string_piece.h"
16 #include "base/types/pass_key.h"
17 #include "build/build_config.h"
20 class FileSystemAccessManagerImpl;
25 class BASE_EXPORT Uuid {
27 // Length in bytes of the input required to format the input as a Uuid in the
29 static constexpr size_t kGuidV4InputLength = 16;
31 // Generate a 128-bit random Uuid in the form of version 4. see RFC 4122,
32 // section 4.4. The format of Uuid version 4 must be
33 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where y is one of [8, 9, a, b]. The
34 // hexadecimal values "a" through "f" are output as lower case characters.
35 // A cryptographically secure random source will be used, but consider using
36 // UnguessableToken for greater type-safety if Uuid format is unnecessary.
37 static Uuid GenerateRandomV4();
39 // Formats a sequence of 16 random bytes as a Uuid in the form of version 4.
41 // - have been randomly generated (e.g. created from an UnguessableToken), and
42 // - be of length 16 (this is checked at compile-time).
43 // Despite taking 128 bits of randomness, certain bits will always be
44 // masked over to adhere to the V4 Uuid format.
45 // Useful in cases where an opaque identifier that is generated from stable
46 // inputs needs to be formatted as a V4 Uuid. Currently only exposed to the
47 // File System Access API to return a V4 Uuid for the getUniqueId() method.
48 static Uuid FormatRandomDataAsV4(
49 base::span<const uint8_t, kGuidV4InputLength> input,
50 base::PassKey<content::FileSystemAccessManagerImpl> pass_key);
51 static Uuid FormatRandomDataAsV4ForTesting(
52 base::span<const uint8_t, kGuidV4InputLength> input);
54 // Returns a valid Uuid if the input string conforms to the Uuid format, and
55 // an invalid Uuid otherwise. Note that this does NOT check if the hexadecimal
56 // values "a" through "f" are in lower case characters.
57 static Uuid ParseCaseInsensitive(StringPiece input);
58 static Uuid ParseCaseInsensitive(StringPiece16 input);
60 // Similar to ParseCaseInsensitive(), but all hexadecimal values "a" through
61 // "f" must be lower case characters.
62 static Uuid ParseLowercase(StringPiece input);
63 static Uuid ParseLowercase(StringPiece16 input);
65 // Constructs an invalid Uuid.
68 Uuid(const Uuid& other);
69 Uuid& operator=(const Uuid& other);
71 Uuid& operator=(Uuid&& other);
73 bool is_valid() const { return !lowercase_.empty(); }
75 // Returns the Uuid in a lowercase string format if it is valid, and an empty
76 // string otherwise. The returned value is guaranteed to be parsed by
79 // NOTE: While AsLowercaseString() is currently a trivial getter, callers
80 // should not treat it as such. When the internal type of base::Uuid changes,
81 // this will be a non-trivial converter. See the TODO above `lowercase_` for
83 const std::string& AsLowercaseString() const;
85 // Invalid Uuids are equal.
86 bool operator==(const Uuid& other) const;
87 bool operator!=(const Uuid& other) const;
88 bool operator<(const Uuid& other) const;
89 bool operator<=(const Uuid& other) const;
90 bool operator>(const Uuid& other) const;
91 bool operator>=(const Uuid& other) const;
94 static Uuid FormatRandomDataAsV4Impl(
95 base::span<const uint8_t, kGuidV4InputLength> input);
97 // TODO(crbug.com/1026195): Consider using a different internal type.
98 // Most existing representations of Uuids in the codebase use std::string,
99 // so matching the internal type will avoid inefficient string conversions
100 // during the migration to base::Uuid.
102 // The lowercase form of the Uuid. Empty for invalid Uuids.
103 std::string lowercase_;
106 // For runtime usage only. Do not store the result of this hash, as it may
107 // change in future Chromium revisions.
108 struct BASE_EXPORT UuidHash {
109 size_t operator()(const Uuid& uuid) const;
112 // Stream operator so Uuid objects can be used in logging statements.
113 BASE_EXPORT std::ostream& operator<<(std::ostream& out, const Uuid& uuid);
117 #endif // BASE_UUID_H_