blob: 13620cf4bf52005b9c0f0050e88ea425217eb731 [file] [log] [blame]
[email protected]7e49ad32012-06-14 14:22:071// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]3469e7e2010-10-14 20:34:592// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]7e49ad32012-06-14 14:22:075#ifndef BASE_GUID_H_
6#define BASE_GUID_H_
[email protected]3469e7e2010-10-14 20:34:597
avi9b6f42932015-12-26 22:15:148#include <stdint.h>
9
Daniel Hosseinian16ab8cf2020-11-12 23:18:3810#include <iosfwd>
[email protected]3469e7e2010-10-14 20:34:5911#include <string>
12
[email protected]7e49ad32012-06-14 14:22:0713#include "base/base_export.h"
Daniel Hosseinianaa27a002020-11-13 00:39:3414#include "base/hash/hash.h"
kinuko4ad1f552016-04-27 11:00:2015#include "base/strings/string_piece.h"
[email protected]3469e7e2010-10-14 20:34:5916#include "build/build_config.h"
17
[email protected]7e49ad32012-06-14 14:22:0718namespace base {
[email protected]3469e7e2010-10-14 20:34:5919
Daniel Hosseinian16ab8cf2020-11-12 23:18:3820// DEPRECATED, use GUID::GenerateRandomV4() instead.
[email protected]7e49ad32012-06-14 14:22:0721BASE_EXPORT std::string GenerateGUID();
[email protected]3469e7e2010-10-14 20:34:5922
Daniel Hosseinian16ab8cf2020-11-12 23:18:3823// DEPRECATED, use GUID::ParseCaseInsensitive() and GUID::is_valid() instead.
24BASE_EXPORT bool IsValidGUID(StringPiece input);
25BASE_EXPORT bool IsValidGUID(StringPiece16 input);
[email protected]d3d728e92010-10-20 03:24:5526
Daniel Hosseinian16ab8cf2020-11-12 23:18:3827// DEPRECATED, use GUID::ParseLowercase() and GUID::is_valid() instead.
28BASE_EXPORT bool IsValidGUIDOutputString(StringPiece input);
kinuko4ad1f552016-04-27 11:00:2029
[email protected]3469e7e2010-10-14 20:34:5930// For unit testing purposes only. Do not use outside of tests.
avi9b6f42932015-12-26 22:15:1431BASE_EXPORT std::string RandomDataToGUIDString(const uint64_t bytes[2]);
[email protected]3469e7e2010-10-14 20:34:5932
Daniel Hosseinian16ab8cf2020-11-12 23:18:3833class BASE_EXPORT GUID {
34 public:
35 // Generate a 128-bit random GUID in the form of version 4. see RFC 4122,
36 // section 4.4. The format of GUID version 4 must be
37 // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx, where y is one of [8, 9, a, b]. The
38 // hexadecimal values "a" through "f" are output as lower case characters.
39 // A cryptographically secure random source will be used, but consider using
40 // UnguessableToken for greater type-safety if GUID format is unnecessary.
41 static GUID GenerateRandomV4();
42
43 // Returns a valid GUID if the input string conforms to the GUID format, and
44 // an invalid GUID otherwise. Note that this does NOT check if the hexadecimal
45 // values "a" through "f" are in lower case characters.
46 static GUID ParseCaseInsensitive(StringPiece input);
47 static GUID ParseCaseInsensitive(StringPiece16 input);
48
49 // Similar to ParseCaseInsensitive(), but all hexadecimal values "a" through
50 // "f" must be lower case characters.
51 static GUID ParseLowercase(StringPiece input);
52 static GUID ParseLowercase(StringPiece16 input);
53
54 // Constructs an invalid GUID.
55 GUID();
56
57 GUID(const GUID& other);
58 GUID& operator=(const GUID& other);
59
60 bool is_valid() const { return !lowercase_.empty(); }
61
62 // Returns the GUID in a lowercase string format if it is valid, and an empty
63 // string otherwise. The returned value is guaranteed to be parsed by
64 // ParseLowercase().
65 //
66 // NOTE: While AsLowercaseString() is currently a trivial getter, callers
67 // should not treat it as such. When the internal type of base::GUID changes,
68 // this will be a non-trivial converter. See the TODO above `lowercase_` for
69 // more context.
70 const std::string& AsLowercaseString() const;
71
72 // Invalid GUIDs are equal.
73 bool operator==(const GUID& other) const;
74 bool operator!=(const GUID& other) const;
Daniel Hosseinian2ea7563b2020-11-13 21:26:5475 bool operator<(const GUID& other) const;
76 bool operator<=(const GUID& other) const;
77 bool operator>(const GUID& other) const;
78 bool operator>=(const GUID& other) const;
Daniel Hosseinian16ab8cf2020-11-12 23:18:3879
80 private:
81 // TODO(crbug.com/1026195): Consider using a different internal type.
82 // Most existing representations of GUIDs in the codebase use std::string,
83 // so matching the internal type will avoid inefficient string conversions
84 // during the migration to base::GUID.
85 //
86 // The lowercase form of the GUID. Empty for invalid GUIDs.
87 std::string lowercase_;
88};
89
Daniel Hosseinianaa27a002020-11-13 00:39:3490// For runtime usage only. Do not store the result of this hash, as it may
91// change in future Chromium revisions.
92struct BASE_EXPORT GUIDHash {
93 size_t operator()(const GUID& guid) const {
94 // TODO(crbug.com/1026195): Avoid converting to string to take the hash when
95 // the internal type is migrated to a non-string type.
96 return FastHash(guid.AsLowercaseString());
97 }
98};
99
Daniel Hosseinian16ab8cf2020-11-12 23:18:38100// Stream operator so GUID objects can be used in logging statements.
101BASE_EXPORT std::ostream& operator<<(std::ostream& out, const GUID& guid);
102
[email protected]4461e9ad2013-09-27 08:52:29103} // namespace base
[email protected]3469e7e2010-10-14 20:34:59104
[email protected]7e49ad32012-06-14 14:22:07105#endif // BASE_GUID_H_