blob: 45cd619e90820ed180259c9d3329aeb0af23715d [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
[email protected]c014f2b32013-09-03 23:29:127#include <string>
8
9#include "base/basictypes.h"
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
[email protected]dfa049e2013-02-07 02:57:2211#include "base/strings/string_number_conversions.h"
[email protected]8f9a3a52013-06-28 15:14:1812#include "base/time/time.h"
[email protected]8703b2b2011-03-15 09:51:5013#include "base/values.h"
14
15namespace base {
16
[email protected]8703b2b2011-03-15 09:51:5017// |Value| internally stores strings in UTF-8, so we have to convert from the
18// system native code to UTF-8 and back.
[email protected]8703b2b2011-03-15 09:51:5019StringValue* CreateFilePathValue(const FilePath& in_value) {
[email protected]45440512011-11-02 04:55:2320 return new StringValue(in_value.AsUTF8Unsafe());
[email protected]8703b2b2011-03-15 09:51:5021}
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)
[email protected]45440512011-11-02 04:55:2328 *file_path = FilePath::FromUTF8Unsafe(str);
[email protected]8703b2b2011-03-15 09:51:5029 return true;
30}
31
[email protected]90e800c2012-06-12 23:11:0032// |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.
[email protected]64a82a52012-06-14 00:30:5634StringValue* CreateTimeDeltaValue(const TimeDelta& time) {
[email protected]90e800c2012-06-12 23:11:0035 std::string string_value = base::Int64ToString(time.ToInternalValue());
36 return new StringValue(string_value);
37}
38
[email protected]64a82a52012-06-14 00:30:5639bool GetValueAsTimeDelta(const Value& value, TimeDelta* time) {
[email protected]90e800c2012-06-12 23:11:0040 std::string str;
41 int64 int_value;
42 if (!value.GetAsString(&str) || !base::StringToInt64(str, &int_value))
43 return false;
44 if (time)
[email protected]64a82a52012-06-14 00:30:5645 *time = TimeDelta::FromInternalValue(int_value);
[email protected]90e800c2012-06-12 23:11:0046 return true;
47}
48
[email protected]8703b2b2011-03-15 09:51:5049} // namespace base