// 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 #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 ValueToInt64(const Value* value) { return value ? ValueToInt64(*value) : std::nullopt; } std::optional 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 ValueToTimeDelta(const Value* value) { return value ? ValueToTimeDelta(*value) : std::nullopt; } std::optional ValueToTimeDelta(const Value& value) { std::optional integer = ValueToInt64(value); if (!integer) return std::nullopt; return Microseconds(*integer); } Value TimeToValue(Time time) { return TimeDeltaToValue(time.ToDeltaSinceWindowsEpoch()); } std::optional