| 6 |
| 7 | #include <stdint.h> |
| 8 |
| 9 | #include <algorithm> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 |
| 13 | #include "base/files/file_path.h" |
| 14 | #include "base/memory/ptr_util.h" |
| 15 | #include "base/strings/string_number_conversions.h" |
| 16 | #include "base/time/time.h" |
| 17 | #include "base/unguessable_token.h" |
| 18 | #include "base/values.h" |
| 19 |
| 20 | namespace base { |
| 21 | namespace { |
| 22 | // Helper for serialize/deserialize UnguessableToken. |
| 23 | union UnguessableTokenRepresentation { |
| 24 | struct Field { |
| 25 | uint64_t high; |
| 26 | uint64_t low; |
| 27 | } field; |
| 28 |
| 29 | uint8_t buffer[sizeof(Field)]; |
| 30 | }; |
| 31 | } // namespace |
| 32 |
| 33 | // |Value| internally stores strings in UTF-8, so we have to convert from the |
| 34 | // system native code to UTF-8 and back. |
| 35 | Value CreateFilePathValue(const FilePath& in_value) { |
| 36 | return Value(in_value.AsUTF8Unsafe()); |
| 37 | } |
| 38 |
| 39 | bool GetValueAsFilePath(const Value& value, FilePath* file_path) { |
| 40 | std::string str; |
| 41 | if (!value.GetAsString(&str)) |
| 42 | return false; |
| 43 | if (file_path) |
| 44 | *file_path = FilePath::FromUTF8Unsafe(str); |
| 45 | return true; |
| 46 | } |
| 47 |
| 48 | // It is recommended in time.h to use ToDeltaSinceWindowsEpoch() and |
| 49 | // FromDeltaSinceWindowsEpoch() for opaque serialization and |
| 50 | // deserialization of time values. |
| 51 | Value CreateTimeValue(const Time& time) { |
| 52 | return CreateTimeDeltaValue(time.ToDeltaSinceWindowsEpoch()); |
| 53 | } |
| 54 |
| 55 | bool GetValueAsTime(const Value& value, Time* time) { |
| 56 | TimeDelta time_delta; |
| 57 | if (!GetValueAsTimeDelta(value, &time_delta)) |
| 58 | return false; |
| 59 |
| 60 | if (time) |
| 61 | *time = Time::FromDeltaSinceWindowsEpoch(time_delta); |
| 62 | return true; |
| 63 | } |
| 64 |
| 65 | // |Value| does not support 64-bit integers, and doubles do not have enough |
| 66 | // precision, so we store the 64-bit time value as a string instead. |
| 67 | Value CreateTimeDeltaValue(const TimeDelta& time_delta) { |
| 68 | std::string string_value = base::NumberToString(time_delta.InMicroseconds()); |
| 69 | return Value(string_value); |
| 70 | } |
| 71 |
| 72 | bool GetValueAsTimeDelta(const Value& value, TimeDelta* time_delta) { |
| 73 | std::string str; |
| 74 | int64_t int_value; |
| 75 | if (!value.GetAsString(&str) || !base::StringToInt64(str, &int_value)) |
| 76 | return false; |
| 77 | if (time_delta) |
| 78 | *time_delta = TimeDelta::FromMicroseconds(int_value); |
| 79 | return true; |
| 80 | } |
| 81 |
| 82 | Value CreateUnguessableTokenValue(const UnguessableToken& token) { |
| 83 | UnguessableTokenRepresentation representation; |
| 84 | representation.field.high = token.GetHighForSerialization(); |
| 85 | representation.field.low = token.GetLowForSerialization(); |
| 86 |
| 87 | return Value(HexEncode(representation.buffer, sizeof(representation.buffer))); |
| 88 | } |
| 89 |
| 90 | bool GetValueAsUnguessableToken(const Value& value, UnguessableToken* token) { |
| 91 | if (!value.is_string()) { |
| 92 | return false; |
| 93 | } |
| 94 |
| 95 | UnguessableTokenRepresentation representation; |
| 96 | if (!HexStringToSpan(value.GetString(), representation.buffer)) { |
| 97 | return false; |
| 98 | } |
| 99 |
| 100 | *token = UnguessableToken::Deserialize(representation.field.high, |
| 101 | representation.field.low); |
| 102 | return true; |
| 103 | } |
| 104 |
| 105 | } // namespace base |
| 106 |