blob: 7e3fd94551950243d35072ade9318e10e9455212 [file] [log] [blame]
[email protected]90e800c2012-06-12 23:11:001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]8703b2b2011-03-15 09:51:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/value_conversions.h"
6
avi9b6f42932015-12-26 22:15:147#include <stdint.h>
8
yucliud62536a2017-08-01 06:06:309#include <algorithm>
[email protected]c014f2b32013-09-03 23:29:1210#include <string>
yucliud62536a2017-08-01 06:06:3011#include <vector>
[email protected]c014f2b32013-09-03 23:29:1212
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
vabrbce355c2017-03-23 18:52:4314#include "base/memory/ptr_util.h"
[email protected]dfa049e2013-02-07 02:57:2215#include "base/strings/string_number_conversions.h"
[email protected]8f9a3a52013-06-28 15:14:1816#include "base/time/time.h"
yucliud62536a2017-08-01 06:06:3017#include "base/unguessable_token.h"
[email protected]8703b2b2011-03-15 09:51:5018#include "base/values.h"
19
20namespace base {
yucliud62536a2017-08-01 06:06:3021namespace {
22// Helper for serialize/deserialize UnguessableToken.
23union UnguessableTokenRepresentation {
24 struct Field {
25 uint64_t high;
26 uint64_t low;
27 } field;
28
29 uint8_t buffer[sizeof(Field)];
30};
31} // namespace
[email protected]8703b2b2011-03-15 09:51:5032
[email protected]8703b2b2011-03-15 09:51:5033// |Value| internally stores strings in UTF-8, so we have to convert from the
34// system native code to UTF-8 and back.
vabrbce355c2017-03-23 18:52:4335std::unique_ptr<Value> CreateFilePathValue(const FilePath& in_value) {
Jeremy Roman9532f252017-08-16 23:27:2436 return std::make_unique<Value>(in_value.AsUTF8Unsafe());
[email protected]8703b2b2011-03-15 09:51:5037}
38
39bool GetValueAsFilePath(const Value& value, FilePath* file_path) {
40 std::string str;
41 if (!value.GetAsString(&str))
42 return false;
43 if (file_path)
[email protected]45440512011-11-02 04:55:2344 *file_path = FilePath::FromUTF8Unsafe(str);
[email protected]8703b2b2011-03-15 09:51:5045 return true;
46}
47
[email protected]90e800c2012-06-12 23:11:0048// |Value| does not support 64-bit integers, and doubles do not have enough
49// precision, so we store the 64-bit time value as a string instead.
vabrbce355c2017-03-23 18:52:4350std::unique_ptr<Value> CreateTimeDeltaValue(const TimeDelta& time) {
[email protected]90e800c2012-06-12 23:11:0051 std::string string_value = base::Int64ToString(time.ToInternalValue());
Jeremy Roman9532f252017-08-16 23:27:2452 return std::make_unique<Value>(string_value);
[email protected]90e800c2012-06-12 23:11:0053}
54
[email protected]64a82a52012-06-14 00:30:5655bool GetValueAsTimeDelta(const Value& value, TimeDelta* time) {
[email protected]90e800c2012-06-12 23:11:0056 std::string str;
avi9b6f42932015-12-26 22:15:1457 int64_t int_value;
[email protected]90e800c2012-06-12 23:11:0058 if (!value.GetAsString(&str) || !base::StringToInt64(str, &int_value))
59 return false;
60 if (time)
[email protected]64a82a52012-06-14 00:30:5661 *time = TimeDelta::FromInternalValue(int_value);
[email protected]90e800c2012-06-12 23:11:0062 return true;
63}
64
yucliud62536a2017-08-01 06:06:3065std::unique_ptr<Value> CreateUnguessableTokenValue(
66 const UnguessableToken& token) {
67 UnguessableTokenRepresentation representation;
68 representation.field.high = token.GetHighForSerialization();
69 representation.field.low = token.GetLowForSerialization();
70
Jeremy Roman9532f252017-08-16 23:27:2471 return std::make_unique<Value>(
yucliud62536a2017-08-01 06:06:3072 HexEncode(representation.buffer, sizeof(representation.buffer)));
73}
74
75bool GetValueAsUnguessableToken(const Value& value, UnguessableToken* token) {
76 if (!value.is_string()) {
77 return false;
78 }
79
80 // TODO(dcheng|yucliu): Make a function that accepts non vector variant and
81 // reads a fixed number of bytes.
82 std::vector<uint8_t> high_low_bytes;
83 if (!HexStringToBytes(value.GetString(), &high_low_bytes)) {
84 return false;
85 }
86
87 UnguessableTokenRepresentation representation;
88 if (high_low_bytes.size() != sizeof(representation.buffer)) {
89 return false;
90 }
91
92 std::copy(high_low_bytes.begin(), high_low_bytes.end(),
93 std::begin(representation.buffer));
94 *token = UnguessableToken::Deserialize(representation.field.high,
95 representation.field.low);
96 return true;
97}
98
[email protected]8703b2b2011-03-15 09:51:5099} // namespace base