values_util.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_UTIL_VALUES_VALUES_UTIL_H_
  5. #define BASE_UTIL_VALUES_VALUES_UTIL_H_
  6. #include "base/optional.h"
  7. #include "base/time/time.h"
  8. #include "base/values.h"
  9. namespace base {
  10. class FilePath;
  11. class UnguessableToken;
  12. } // namespace base
  13. namespace util {
  14. // Simple helper functions for converting between base::Value and other types.
  15. // The base::Value representation is stable, suitable for persistent storage
  16. // e.g. as JSON on disk.
  17. //
  18. // It is valid to pass nullptr to the ValueToEtc functions. They will just
  19. // return base::nullopt.
  20. // Converts between an int64_t and a string-flavored base::Value (a human
  21. // readable string of that number).
  22. base::Value Int64ToValue(int64_t integer);
  23. base::Optional<int64_t> ValueToInt64(const base::Value* value);
  24. base::Optional<int64_t> ValueToInt64(const base::Value& value);
  25. // Converts between a base::TimeDelta (an int64_t number of microseconds) and a
  26. // string-flavored base::Value (a human readable string of that number).
  27. base::Value TimeDeltaToValue(base::TimeDelta time_delta);
  28. base::Optional<base::TimeDelta> ValueToTimeDelta(const base::Value* value);
  29. base::Optional<base::TimeDelta> ValueToTimeDelta(const base::Value& value);
  30. // Converts between a base::Time (an int64_t number of microseconds since the
  31. // Windows epoch) and a string-flavored base::Value (a human readable string of
  32. // that number).
  33. base::Value TimeToValue(base::Time time);
  34. base::Optional<base::Time> ValueToTime(const base::Value* value);
  35. base::Optional<base::Time> ValueToTime(const base::Value& value);
  36. // Converts between a base::FilePath (a std::string or base::string16) and a
  37. // string-flavored base::Value (the UTF-8 representation).
  38. base::Value FilePathToValue(base::FilePath file_path);
  39. base::Optional<base::FilePath> ValueToFilePath(const base::Value* value);
  40. base::Optional<base::FilePath> ValueToFilePath(const base::Value& value);
  41. // Converts between a base::UnguessableToken (128 bits) and a string-flavored
  42. // base::Value (32 hexadecimal digits).
  43. base::Value UnguessableTokenToValue(base::UnguessableToken token);
  44. base::Optional<base::UnguessableToken> ValueToUnguessableToken(
  45. const base::Value* value);
  46. base::Optional<base::UnguessableToken> ValueToUnguessableToken(
  47. const base::Value& value);
  48. } // namespace util
  49. #endif // BASE_UTIL_VALUES_VALUES_UTIL_H_