diff options
-rw-r--r-- | base/base.gypi | 6 | ||||
-rw-r--r-- | base/value_conversions.cc | 52 | ||||
-rw-r--r-- | base/value_conversions.h | 23 | ||||
-rw-r--r-- | base/values.cc | 33 | ||||
-rw-r--r-- | base/values.h | 4 | ||||
-rw-r--r-- | chrome/browser/download/download_util.cc | 3 | ||||
-rw-r--r-- | chrome/browser/prefs/pref_member.cc | 5 | ||||
-rw-r--r-- | chrome/browser/prefs/pref_service.cc | 7 |
8 files changed, 86 insertions, 47 deletions
diff --git a/base/base.gypi b/base/base.gypi index c265f36..ac8d9acd 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -111,8 +111,8 @@ 'logging.h', 'logging_win.cc', 'mac/cocoa_protocols.h', - 'mac/foundation_util.h', - 'mac/foundation_util.mm', + 'mac/foundation_util.h', + 'mac/foundation_util.mm', 'mac/mac_util.h', 'mac/mac_util.mm', 'mac/os_crash_dumps.cc', @@ -299,6 +299,8 @@ 'utf_string_conversions.h', 'values.cc', 'values.h', + 'value_conversions.cc', + 'value_conversions.h', 'version.cc', 'version.h', 'vlog.cc', diff --git a/base/value_conversions.cc b/base/value_conversions.cc new file mode 100644 index 0000000..09522be --- /dev/null +++ b/base/value_conversions.cc @@ -0,0 +1,52 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/value_conversions.h" + +#include "base/file_path.h" +#include "base/sys_string_conversions.h" +#include "base/utf_string_conversions.h" +#include "base/values.h" + +namespace base { + +namespace { + +// |Value| internally stores strings in UTF-8, so we have to convert from the +// system native code to UTF-8 and back. + +std::string FilePathToUTF8(const FilePath& file_path) { +#if defined(OS_POSIX) + return WideToUTF8(SysNativeMBToWide(file_path.value())); +#else + return UTF16ToUTF8(file_path.value()); +#endif +} + +FilePath UTF8ToFilePath(const std::string& str) { + FilePath::StringType result; +#if defined(OS_POSIX) + result = SysWideToNativeMB(UTF8ToWide(str)); +#elif defined(OS_WIN) + result = UTF8ToUTF16(str); +#endif + return FilePath(result); +} + +} // namespace + +StringValue* CreateFilePathValue(const FilePath& in_value) { + return new StringValue(internal::FilePathToUTF8(in_value)); +} + +bool GetValueAsFilePath(const Value& value, FilePath* file_path) { + std::string str; + if (!value.GetAsString(&str)) + return false; + if (file_path) + *file_path = internal::UTF8ToFilePath(str); + return true; +} + +} // namespace base diff --git a/base/value_conversions.h b/base/value_conversions.h new file mode 100644 index 0000000..9678fc1 --- /dev/null +++ b/base/value_conversions.h @@ -0,0 +1,23 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef BASE_VALUE_CONVERSIONS_H_ +#define BASE_VALUE_CONVERSIONS_H_ +#pragma once + +// This file contains methods to convert a |FilePath| to a |Value| and back. + +class FilePath; +class StringValue; +class Value; + +namespace base { + +// The caller takes ownership of the returned value. +StringValue* CreateFilePathValue(const FilePath& in_value); +bool GetValueAsFilePath(const Value& value, FilePath* file_path); + +} // namespace + +#endif // BASE_VALUE_CONVERSIONS_H_ diff --git a/base/values.cc b/base/values.cc index 3340f9b..9c96f26 100644 --- a/base/values.cc +++ b/base/values.cc @@ -4,10 +4,8 @@ #include "base/values.h" -#include "base/file_path.h" #include "base/logging.h" #include "base/string_util.h" -#include "base/sys_string_conversions.h" #include "base/utf_string_conversions.h" namespace { @@ -97,19 +95,6 @@ StringValue* Value::CreateStringValue(const string16& in_value) { } // static -StringValue* Value::CreateFilePathValue(const FilePath& in_value) { -#if defined(OS_POSIX) - // Value::SetString only knows about UTF8 strings, so convert the path from - // the system native value to UTF8. - std::string path_utf8 = WideToUTF8(base::SysNativeMBToWide(in_value.value())); - return new StringValue(path_utf8); -#else - return new StringValue(in_value.value()); -#endif - -} - -// static BinaryValue* Value::CreateBinaryValue(char* buffer, size_t size) { return BinaryValue::Create(buffer, size); } @@ -134,10 +119,6 @@ bool Value::GetAsString(string16* out_value) const { return false; } -bool Value::GetAsFilePath(FilePath* out_value) const { - return false; -} - bool Value::GetAsList(ListValue** out_value) { return false; } @@ -269,20 +250,6 @@ bool StringValue::GetAsString(string16* out_value) const { return true; } -bool StringValue::GetAsFilePath(FilePath* out_value) const { - if (out_value) { - FilePath::StringType result; -#if defined(OS_POSIX) - // We store filepaths as UTF8, so convert it back to the system type. - result = base::SysWideToNativeMB(UTF8ToWide(value_)); -#elif defined(OS_WIN) - result = UTF8ToUTF16(value_); -#endif - *out_value = FilePath(result); - } - return true; -} - StringValue* StringValue::DeepCopy() const { return CreateStringValue(value_); } diff --git a/base/values.h b/base/values.h index 5814a9f..8dc9f25 100644 --- a/base/values.h +++ b/base/values.h @@ -33,7 +33,6 @@ class BinaryValue; class DictionaryValue; -class FilePath; class FundamentalValue; class ListValue; class StringValue; @@ -69,7 +68,6 @@ class Value { static FundamentalValue* CreateDoubleValue(double in_value); static StringValue* CreateStringValue(const std::string& in_value); static StringValue* CreateStringValue(const string16& in_value); - static StringValue* CreateFilePathValue(const FilePath& in_value); // This one can return NULL if the input isn't valid. If the return value // is non-null, the new object has taken ownership of the buffer pointer. @@ -94,7 +92,6 @@ class Value { virtual bool GetAsDouble(double* out_value) const; virtual bool GetAsString(std::string* out_value) const; virtual bool GetAsString(string16* out_value) const; - virtual bool GetAsFilePath(FilePath* out_value) const; virtual bool GetAsList(ListValue** out_value); // This creates a deep copy of the entire Value tree, and returns a pointer @@ -162,7 +159,6 @@ class StringValue : public Value { // Subclassed methods virtual bool GetAsString(std::string* out_value) const; virtual bool GetAsString(string16* out_value) const; - virtual bool GetAsFilePath(FilePath* out_value) const; virtual StringValue* DeepCopy() const; virtual bool Equals(const Value* other) const; diff --git a/chrome/browser/download/download_util.cc b/chrome/browser/download/download_util.cc index 8144dab..f00b40b 100644 --- a/chrome/browser/download/download_util.cc +++ b/chrome/browser/download/download_util.cc @@ -23,6 +23,7 @@ #include "base/threading/thread_restrictions.h" #include "base/utf_string_conversions.h" #include "base/values.h" +#include "base/value_conversions.h" #include "base/win/windows_version.h" #include "chrome/browser/download/download_extensions.h" #include "chrome/browser/download/download_item.h" @@ -571,7 +572,7 @@ DictionaryValue* CreateDownloadItemValue(DownloadItem* download, int id) { base::TimeFormatShortDate(download->start_time())); file_value->SetInteger("id", id); file_value->Set("file_path", - Value::CreateFilePathValue(download->GetTargetFilePath())); + base::CreateFilePathValue(download->GetTargetFilePath())); // Keep file names as LTR. string16 file_name = download->GetFileNameToReportUser().LossyDisplayName(); file_name = base::i18n::GetDisplayStringInLTRDirectionality(file_name); diff --git a/chrome/browser/prefs/pref_member.cc b/chrome/browser/prefs/pref_member.cc index 66b6609..25ccc55 100644 --- a/chrome/browser/prefs/pref_member.cc +++ b/chrome/browser/prefs/pref_member.cc @@ -5,8 +5,7 @@ #include "chrome/browser/prefs/pref_member.h" #include "base/logging.h" -#include "base/sys_string_conversions.h" -#include "base/utf_string_conversions.h" +#include "base/value_conversions.h" #include "chrome/browser/prefs/pref_service.h" #include "content/common/notification_type.h" @@ -166,6 +165,6 @@ void PrefMember<FilePath>::UpdatePref(const FilePath& value) { template <> bool PrefMember<FilePath>::Internal::UpdateValueInternal(const Value& value) const { - return value.GetAsFilePath(&value_); + return base::GetValueAsFilePath(value, &value_); } diff --git a/chrome/browser/prefs/pref_service.cc b/chrome/browser/prefs/pref_service.cc index 5afb08f..373a86f 100644 --- a/chrome/browser/prefs/pref_service.cc +++ b/chrome/browser/prefs/pref_service.cc @@ -16,8 +16,7 @@ #include "base/stl_util-inl.h" #include "base/string_number_conversions.h" #include "base/string_util.h" -#include "base/sys_string_conversions.h" -#include "base/utf_string_conversions.h" +#include "base/value_conversions.h" #include "build/build_config.h" #include "chrome/browser/extensions/extension_pref_store.h" #include "chrome/browser/policy/configuration_policy_pref_store.h" @@ -362,7 +361,7 @@ FilePath PrefService::GetFilePath(const char* path) const { NOTREACHED() << "Trying to read an unregistered pref: " << path; return FilePath(result); } - bool rv = pref->GetValue()->GetAsFilePath(&result); + bool rv = base::GetValueAsFilePath(*pref->GetValue(), &result); DCHECK(rv); return result; } @@ -521,7 +520,7 @@ void PrefService::SetString(const char* path, const std::string& value) { } void PrefService::SetFilePath(const char* path, const FilePath& value) { - SetUserPrefValue(path, Value::CreateFilePathValue(value)); + SetUserPrefValue(path, base::CreateFilePathValue(value)); } void PrefService::SetInt64(const char* path, int64 value) { |