1 // Copyright (c) 2011 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 "base/file_path.h" 8 #include "base/sys_string_conversions.h" 9 #include "base/utf_string_conversions.h" 10 #include "base/values.h" 11 12 namespace base { 13 14 namespace { 15 16 // |Value| internally stores strings in UTF-8, so we have to convert from the 17 // system native code to UTF-8 and back. 18 FilePathToUTF8(const FilePath & file_path)19std::string FilePathToUTF8(const FilePath& file_path) { 20 #if defined(OS_POSIX) 21 return WideToUTF8(SysNativeMBToWide(file_path.value())); 22 #else 23 return UTF16ToUTF8(file_path.value()); 24 #endif 25 } 26 UTF8ToFilePath(const std::string & str)27FilePath UTF8ToFilePath(const std::string& str) { 28 FilePath::StringType result; 29 #if defined(OS_POSIX) 30 result = SysWideToNativeMB(UTF8ToWide(str)); 31 #elif defined(OS_WIN) 32 result = UTF8ToUTF16(str); 33 #endif 34 return FilePath(result); 35 } 36 37 } // namespace 38 CreateFilePathValue(const FilePath & in_value)39StringValue* CreateFilePathValue(const FilePath& in_value) { 40 return new StringValue(FilePathToUTF8(in_value)); 41 } 42 GetValueAsFilePath(const Value & value,FilePath * file_path)43bool GetValueAsFilePath(const Value& value, FilePath* file_path) { 44 std::string str; 45 if (!value.GetAsString(&str)) 46 return false; 47 if (file_path) 48 *file_path = UTF8ToFilePath(str); 49 return true; 50 } 51 52 } // namespace base 53