// Copyright 2019 The Chromium Authors. All rights reserved. // 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 "base/files/file_path.h" #include "base/strings/string_number_conversions.h" #include "base/time/time.h" #include "base/unguessable_token.h" #include "third_party/abseil-cpp/absl/types/optional.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)); } absl::optional ValueToInt64(const Value* value) { return value ? ValueToInt64(*value) : absl::nullopt; } absl::optional ValueToInt64(const Value& value) { if (!value.is_string()) return absl::nullopt; int64_t integer; if (!StringToInt64(value.GetString(), &integer)) return absl::nullopt; return integer; } Value TimeDeltaToValue(TimeDelta time_delta) { return Int64ToValue(time_delta.InMicroseconds()); } absl::optional ValueToTimeDelta(const Value* value) { return value ? ValueToTimeDelta(*value) : absl::nullopt; } absl::optional ValueToTimeDelta(const Value& value) { absl::optional integer = ValueToInt64(value); if (!integer) return absl::nullopt; return Microseconds(*integer); } Value TimeToValue(Time time) { return TimeDeltaToValue(time.ToDeltaSinceWindowsEpoch()); } absl::optional