summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-02-04 20:41:33 +0000
committerevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-02-04 20:41:33 +0000
commit8a205c0f534bf8e5f17a531182dee38aaacd46e6 (patch)
tree1b1bab965f71658393dccdda1590d59711689962 /base
parent58023bea8fa444c4ac08c12c825dcc79b5954abe (diff)
downloadchromium_src-8a205c0f534bf8e5f17a531182dee38aaacd46e6.zip
chromium_src-8a205c0f534bf8e5f17a531182dee38aaacd46e6.tar.gz
chromium_src-8a205c0f534bf8e5f17a531182dee38aaacd46e6.tar.bz2
FilePath: Remove much of ToWStringHack, adding a LossyDisplayName()
The reason we don't want a free conversion between FilePaths and Unicode is that it can be lossy. But when displaying a string to the user, we're ok if it's lossy when we have no other option. This change introduces a LossyDisplayName() method that returns a string16, and converts many of the users of ToWStringHack to use it. BUG=69467 Review URL: http://codereview.chromium.org/6246036 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@73840 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/file_path.cc14
-rw-r--r--base/file_path.h9
-rw-r--r--base/file_util_unittest.cc4
3 files changed, 22 insertions, 5 deletions
diff --git a/base/file_path.cc b/base/file_path.cc
index 9bc46f0..5f1375a 100644
--- a/base/file_path.cc
+++ b/base/file_path.cc
@@ -513,10 +513,14 @@ bool FilePath::ReferencesParent() const {
}
#if defined(OS_POSIX)
-
// See file_path.h for a discussion of the encoding of paths on POSIX
-// platforms. These *Hack() functions are not quite correct, but they're
-// only temporary while we fix the remainder of the code.
+// platforms. These encoding conversion functions are not quite correct.
+
+string16 FilePath::LossyDisplayName() const {
+ return WideToUTF16(base::SysNativeMBToWide(path_));
+}
+
+// The *Hack functions are temporary while we fix the remainder of the code.
// Remember to remove the #includes at the top when you remove these.
// static
@@ -527,6 +531,10 @@ std::wstring FilePath::ToWStringHack() const {
return base::SysNativeMBToWide(path_);
}
#elif defined(OS_WIN)
+string16 FilePath::LossyDisplayName() const {
+ return path_;
+}
+
// static
FilePath FilePath::FromWStringHack(const std::wstring& wstring) {
return FilePath(wstring);
diff --git a/base/file_path.h b/base/file_path.h
index 01cd4a5..84bb350 100644
--- a/base/file_path.h
+++ b/base/file_path.h
@@ -277,6 +277,12 @@ class FilePath {
// directory (i.e. has a path component that is ".."
bool ReferencesParent() const;
+ // Return a Unicode human-readable version of this path.
+ // Warning: you can *not*, in general, go from a display name back to a real
+ // path. Only use this when displaying paths to users, not just when you
+ // want to stuff a string16 into some other API.
+ string16 LossyDisplayName() const;
+
// Older Chromium code assumes that paths are always wstrings.
// These functions convert wstrings to/from FilePaths, and are
// useful to smooth porting that old code to the FilePath API.
@@ -290,6 +296,9 @@ class FilePath {
// OS-native string format.
// - Am I using well-known file names, like "config.ini"? Then use the
// ASCII functions (we require paths to always be supersets of ASCII).
+ // - Am I displaying a string to the user in some UI? Then use the
+ // LossyDisplayName() function, but keep in mind that you can't
+ // ever use the result of that again as a path.
static FilePath FromWStringHack(const std::wstring& wstring);
std::wstring ToWStringHack() const;
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index 6ea94e4..ea29df5 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -163,7 +163,7 @@ class FindResultCollector {
void CreateTextFile(const FilePath& filename,
const std::wstring& contents) {
std::ofstream file;
- file.open(WideToUTF8(filename.ToWStringHack()).c_str());
+ file.open(filename.value().c_str());
ASSERT_TRUE(file.is_open());
file << contents;
file.close();
@@ -173,7 +173,7 @@ void CreateTextFile(const FilePath& filename,
std::wstring ReadTextFile(const FilePath& filename) {
wchar_t contents[64];
std::wifstream file;
- file.open(WideToUTF8(filename.ToWStringHack()).c_str());
+ file.open(filename.value().c_str());
EXPECT_TRUE(file.is_open());
file.getline(contents, 64);
file.close();