1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/json/values_util.h"
#include <optional>
#include "base/files/file_path.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
// Warning: The Values involved could be stored on persistent storage like files
// on disks. Therefore, changes in implementation could lead to data corruption
// and must be done with caution.
namespace base {
namespace {
// Helper to serialize/deserialize an UnguessableToken.
//
// It assumes a little-endian CPU, which is arguably a bug.
union UnguessableTokenRepresentation {
struct Field {
uint64_t high;
uint64_t low;
} field;
uint8_t buffer[sizeof(Field)];
};
} // namespace
Value Int64ToValue(int64_t integer) {
return Value(NumberToString(integer));
}
std::optional<int64_t> ValueToInt64(const Value* value) {
return value ? ValueToInt64(*value) : std::nullopt;
}
std::optional<int64_t> ValueToInt64(const Value& value) {
if (!value.is_string()) {
return std::nullopt;
}
int64_t integer;
if (!StringToInt64(value.GetString(), &integer)) {
return std::nullopt;
}
return integer;
}
Value TimeDeltaToValue(TimeDelta time_delta) {
return Int64ToValue(time_delta.InMicroseconds());
}
std::optional<TimeDelta> ValueToTimeDelta(const Value* value) {
return value ? ValueToTimeDelta(*value) : std::nullopt;
}
std::optional<TimeDelta> ValueToTimeDelta(const Value& value) {
std::optional<int64_t> integer = ValueToInt64(value);
if (!integer) {
return std::nullopt;
}
return Microseconds(*integer);
}
Value TimeToValue(Time time) {
return TimeDeltaToValue(time.ToDeltaSinceWindowsEpoch());
}
std::optional<Time> ValueToTime(const Value* value) {
return value ? ValueToTime(*value) : std::nullopt;
}
std::optional<Time> ValueToTime(const Value& value) {
std::optional<TimeDelta> time_delta = ValueToTimeDelta(value);
if (!time_delta) {
return std::nullopt;
}
return Time::FromDeltaSinceWindowsEpoch(*time_delta);
}
Value FilePathToValue(const FilePath& file_path) {
return Value(file_path.AsUTF8Unsafe());
}
std::optional<FilePath> ValueToFilePath(const Value* value) {
return value ? ValueToFilePath(*value) : std::nullopt;
}
std::optional<FilePath> ValueToFilePath(const Value& value) {
if (!value.is_string()) {
return std::nullopt;
}
return FilePath::FromUTF8Unsafe(value.GetString());
}
Value UnguessableTokenToValue(UnguessableToken token) {
UnguessableTokenRepresentation repr;
repr.field.high = token.GetHighForSerialization();
repr.field.low = token.GetLowForSerialization();
return Value(HexEncode(repr.buffer, sizeof(repr.buffer)));
}
std::optional<UnguessableToken> ValueToUnguessableToken(const Value* value) {
return value ? ValueToUnguessableToken(*value) : std::nullopt;
}
std::optional<UnguessableToken> ValueToUnguessableToken(const Value& value) {
if (!value.is_string()) {
return std::nullopt;
}
UnguessableTokenRepresentation repr;
if (!HexStringToSpan(value.GetString(), repr.buffer)) {
return std::nullopt;
}
std::optional<base::UnguessableToken> token =
UnguessableToken::Deserialize(repr.field.high, repr.field.low);
if (!token.has_value()) {
return std::nullopt;
}
return token;
}
} // namespace base
|