[M73 Dev][Tizen] Fix compilation errors for TV profile
[platform/framework/web/chromium-efl.git] / base / value_conversions.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // 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
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 // |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.
50 Value CreateTimeDeltaValue(const TimeDelta& time) {
51   std::string string_value = base::Int64ToString(time.ToInternalValue());
52   return Value(string_value);
53 }
54
55 bool GetValueAsTimeDelta(const Value& value, TimeDelta* time) {
56   std::string str;
57   int64_t int_value;
58   if (!value.GetAsString(&str) || !base::StringToInt64(str, &int_value))
59     return false;
60   if (time)
61     *time = TimeDelta::FromInternalValue(int_value);
62   return true;
63 }
64
65 Value CreateUnguessableTokenValue(const UnguessableToken& token) {
66   UnguessableTokenRepresentation representation;
67   representation.field.high = token.GetHighForSerialization();
68   representation.field.low = token.GetLowForSerialization();
69
70   return Value(HexEncode(representation.buffer, sizeof(representation.buffer)));
71 }
72
73 bool GetValueAsUnguessableToken(const Value& value, UnguessableToken* token) {
74   if (!value.is_string()) {
75     return false;
76   }
77
78   // TODO(dcheng|yucliu): Make a function that accepts non vector variant and
79   // reads a fixed number of bytes.
80   std::vector<uint8_t> high_low_bytes;
81   if (!HexStringToBytes(value.GetString(), &high_low_bytes)) {
82     return false;
83   }
84
85   UnguessableTokenRepresentation representation;
86   if (high_low_bytes.size() != sizeof(representation.buffer)) {
87     return false;
88   }
89
90   std::copy(high_low_bytes.begin(), high_low_bytes.end(),
91             std::begin(representation.buffer));
92   *token = UnguessableToken::Deserialize(representation.field.high,
93                                          representation.field.low);
94   return true;
95 }
96
97 }  // namespace base