| Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
| Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 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_JSON_VALUES_UTIL_H_ |
| 6 | #define BASE_JSON_VALUES_UTIL_H_ |
| 7 | |
| Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 8 | #include <optional> |
| 9 | |
| Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 10 | #include "base/base_export.h" |
| 11 | #include "base/values.h" |
| Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 12 | |
| 13 | namespace base { |
| 14 | class FilePath; |
| 15 | class Time; |
| 16 | class TimeDelta; |
| 17 | class UnguessableToken; |
| 18 | |
| 19 | // Simple helper functions for converting between Value and other types. |
| 20 | // The Value representation is stable, suitable for persistent storage |
| 21 | // e.g. as JSON on disk. |
| 22 | // |
| 23 | // It is valid to pass nullptr to the ValueToEtc functions. They will just |
| Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 24 | // return std::nullopt. |
| Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 25 | |
| 26 | // Converts between an int64_t and a string-flavored Value (a human |
| 27 | // readable string of that number). |
| 28 | BASE_EXPORT Value Int64ToValue(int64_t integer); |
| Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 29 | BASE_EXPORT std::optional<int64_t> ValueToInt64(const Value* value); |
| 30 | BASE_EXPORT std::optional<int64_t> ValueToInt64(const Value& value); |
| Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 31 | |
| 32 | // Converts between a TimeDelta (an int64_t number of microseconds) and a |
| 33 | // string-flavored Value (a human readable string of that number). |
| 34 | BASE_EXPORT Value TimeDeltaToValue(TimeDelta time_delta); |
| Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 35 | BASE_EXPORT std::optional<TimeDelta> ValueToTimeDelta(const Value* value); |
| 36 | BASE_EXPORT std::optional<TimeDelta> ValueToTimeDelta(const Value& value); |
| Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 37 | |
| 38 | // Converts between a Time (an int64_t number of microseconds since the |
| 39 | // Windows epoch) and a string-flavored Value (a human readable string of |
| 40 | // that number). |
| 41 | BASE_EXPORT Value TimeToValue(Time time); |
| Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 42 | BASE_EXPORT std::optional<Time> ValueToTime(const Value* value); |
| 43 | BASE_EXPORT std::optional<Time> ValueToTime(const Value& value); |
| Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 44 | |
| 45 | // Converts between a FilePath (a std::string or std::u16string) and a |
| 46 | // string-flavored Value (the UTF-8 representation). |
| Tom Sepez | 335ca78 | 2024-10-31 21:29:52 | [diff] [blame] | 47 | BASE_EXPORT Value FilePathToValue(const FilePath& file_path); |
| Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 48 | BASE_EXPORT std::optional<FilePath> ValueToFilePath(const Value* value); |
| 49 | BASE_EXPORT std::optional<FilePath> ValueToFilePath(const Value& value); |
| Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 50 | |
| 51 | // Converts between a UnguessableToken (128 bits) and a string-flavored |
| 52 | // Value (32 hexadecimal digits). |
| 53 | BASE_EXPORT Value UnguessableTokenToValue(UnguessableToken token); |
| Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 54 | BASE_EXPORT std::optional<UnguessableToken> ValueToUnguessableToken( |
| Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 55 | const Value* value); |
| Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 56 | BASE_EXPORT std::optional<UnguessableToken> ValueToUnguessableToken( |
| Albert J. Wong | f60c8d2 | 2021-07-27 07:25:26 | [diff] [blame] | 57 | const Value& value); |
| 58 | |
| 59 | } // namespace base |
| 60 | |
| 61 | #endif // BASE_JSON_VALUES_UTIL_H_ |