blob: 45cd619e90820ed180259c9d3329aeb0af23715d [file] [log] [blame]
mpcomplete@chromium.orgeb7afa32012-06-13 08:11:00 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
bauerb@chromium.orgfe9f8532011-03-15 18:51:50 +09002// 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
mostynb@opera.comf29dd612013-09-04 08:29:12 +09007#include <string>
8
9#include "base/basictypes.h"
brettw@chromium.org59eef1f2013-02-24 14:40:52 +090010#include "base/files/file_path.h"
brettw@chromium.orgabcde5c2013-02-07 11:57:22 +090011#include "base/strings/string_number_conversions.h"
avi@chromium.orgb45ec932013-06-29 00:14:18 +090012#include "base/time/time.h"
bauerb@chromium.orgfe9f8532011-03-15 18:51:50 +090013#include "base/values.h"
14
15namespace base {
16
bauerb@chromium.orgfe9f8532011-03-15 18:51:50 +090017// |Value| internally stores strings in UTF-8, so we have to convert from the
18// system native code to UTF-8 and back.
bauerb@chromium.orgfe9f8532011-03-15 18:51:50 +090019StringValue* CreateFilePathValue(const FilePath& in_value) {
satorux@chromium.orgefba4172011-11-02 13:55:23 +090020 return new StringValue(in_value.AsUTF8Unsafe());
bauerb@chromium.orgfe9f8532011-03-15 18:51:50 +090021}
22
23bool GetValueAsFilePath(const Value& value, FilePath* file_path) {
24 std::string str;
25 if (!value.GetAsString(&str))
26 return false;
27 if (file_path)
satorux@chromium.orgefba4172011-11-02 13:55:23 +090028 *file_path = FilePath::FromUTF8Unsafe(str);
bauerb@chromium.orgfe9f8532011-03-15 18:51:50 +090029 return true;
30}
31
mpcomplete@chromium.orgeb7afa32012-06-13 08:11:00 +090032// |Value| does not support 64-bit integers, and doubles do not have enough
33// precision, so we store the 64-bit time value as a string instead.
jyasskin@chromium.org8e3c3df2012-06-14 09:30:56 +090034StringValue* CreateTimeDeltaValue(const TimeDelta& time) {
mpcomplete@chromium.orgeb7afa32012-06-13 08:11:00 +090035 std::string string_value = base::Int64ToString(time.ToInternalValue());
36 return new StringValue(string_value);
37}
38
jyasskin@chromium.org8e3c3df2012-06-14 09:30:56 +090039bool GetValueAsTimeDelta(const Value& value, TimeDelta* time) {
mpcomplete@chromium.orgeb7afa32012-06-13 08:11:00 +090040 std::string str;
41 int64 int_value;
42 if (!value.GetAsString(&str) || !base::StringToInt64(str, &int_value))
43 return false;
44 if (time)
jyasskin@chromium.org8e3c3df2012-06-14 09:30:56 +090045 *time = TimeDelta::FromInternalValue(int_value);
mpcomplete@chromium.orgeb7afa32012-06-13 08:11:00 +090046 return true;
47}
48
bauerb@chromium.orgfe9f8532011-03-15 18:51:50 +090049} // namespace base