summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-21 16:45:38 +0000
committerevan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-21 16:45:38 +0000
commit762c554314d15ac62e997758d7a4f8e0e35d3fce (patch)
tree3ab1f8b3be29c82f00e63b2e777edc8aea036087 /chrome
parent631bad00c1230ed49bda8ccfdc451114dee99cf4 (diff)
downloadchromium_src-762c554314d15ac62e997758d7a4f8e0e35d3fce.zip
chromium_src-762c554314d15ac62e997758d7a4f8e0e35d3fce.tar.gz
chromium_src-762c554314d15ac62e997758d7a4f8e0e35d3fce.tar.bz2
posix: ~ should be considered a path character in the omnibox
We expand it to $HOME or the appropriate /home/foobar. BUG=18200 TEST=Extended the unit test with test cases. Review URL: http://codereview.chromium.org/300026 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@29657 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/browser/net/url_fixer_upper.cc36
-rw-r--r--chrome/browser/net/url_fixer_upper.h4
-rw-r--r--chrome/browser/net/url_fixer_upper_unittest.cc19
3 files changed, 58 insertions, 1 deletions
diff --git a/chrome/browser/net/url_fixer_upper.cc b/chrome/browser/net/url_fixer_upper.cc
index 8aee921..b465268 100644
--- a/chrome/browser/net/url_fixer_upper.cc
+++ b/chrome/browser/net/url_fixer_upper.cc
@@ -21,6 +21,8 @@
using std::string;
using std::wstring;
+const char* URLFixerUpper::home_directory_override = NULL;
+
namespace {
// TODO(estade): Remove these ugly, ugly functions. They are only used in
@@ -113,6 +115,36 @@ static bool ValidPathForFile(const FilePath::StringType& text,
return true;
}
+#if defined(OS_POSIX)
+// Given a path that starts with ~, return a path that starts with an
+// expanded-out /user/foobar directory.
+static string FixupHomedir(const string& text) {
+ DCHECK(text.length() > 0 && text[0] == '~');
+
+ if (text.length() == 1 || text[1] == '/') {
+ const char* home = getenv("HOME");
+ if (URLFixerUpper::home_directory_override)
+ home = URLFixerUpper::home_directory_override;
+ // We'll probably break elsewhere if $HOME is undefined, but check here
+ // just in case.
+ if (!home)
+ return text;
+ return home + text.substr(1);
+ }
+
+ // Otherwise, this is a path like ~foobar/baz, where we must expand to
+ // user foobar's home directory. Officially, we should use getpwent(),
+ // but that is a nasty blocking call.
+
+#if defined(OS_MACOSX)
+ static const char kHome[] = "/Users/";
+#else
+ static const char kHome[] = "/home/";
+#endif
+ return kHome + text.substr(1);
+}
+#endif
+
// Tries to create a file: URL from |text| if it looks like a filename, even if
// it doesn't resolve as a valid path or to an existing file. Returns true
// with a (possibly invalid) file: URL in |fixed_up_url| for input beginning
@@ -133,6 +165,8 @@ static string FixupPath(const string& text) {
#elif defined(OS_POSIX)
FilePath input_path(text);
PrepareStringForFileOps(input_path, &filename);
+ if (filename.length() > 0 && filename[0] == '~')
+ filename = FixupHomedir(filename);
#endif
// Here, we know the input looks like a file.
@@ -385,7 +419,7 @@ string URLFixerUpper::SegmentURL(const string& text,
url_parse::DoesBeginUNCPath(trimmed.data(), 0, trimmed_length, false))
return "file";
#elif defined(OS_POSIX)
- if (FilePath::IsSeparator(trimmed.c_str()[0]))
+ if (FilePath::IsSeparator(trimmed.data()[0]) || trimmed.data()[0] == '~')
return "file";
#endif
diff --git a/chrome/browser/net/url_fixer_upper.h b/chrome/browser/net/url_fixer_upper.h
index 5bd7fef..d3db746 100644
--- a/chrome/browser/net/url_fixer_upper.h
+++ b/chrome/browser/net/url_fixer_upper.h
@@ -61,6 +61,10 @@ namespace URLFixerUpper {
std::wstring FixupRelativeFile(const std::wstring& base_dir,
const std::wstring& text);
+ // For paths like ~, we use $HOME for the current user's home
+ // directory. For tests, we allow our idea of $HOME to be overriden
+ // by this variable.
+ extern const char* home_directory_override;
};
#endif // #ifndef CHROME_BROWSER_NET_URL_FIXER_UPPER_H_
diff --git a/chrome/browser/net/url_fixer_upper_unittest.cc b/chrome/browser/net/url_fixer_upper_unittest.cc
index 62ca1f9..23bb56b 100644
--- a/chrome/browser/net/url_fixer_upper_unittest.cc
+++ b/chrome/browser/net/url_fixer_upper_unittest.cc
@@ -363,6 +363,13 @@ TEST(URLFixerUpperTest, FixupFile) {
// {"file:/\\/server\\folder/file", "", "file://server/folder/file"},
};
#elif defined(OS_POSIX)
+
+#if defined(OS_MACOSX)
+#define HOME "/Users/"
+#else
+#define HOME "/home/"
+#endif
+ URLFixerUpper::home_directory_override = "/foo";
fixup_case file_cases[] = {
// File URLs go through GURL, which tries to escape intelligently.
{"/This%20is a non-existent file.txt", "",
@@ -370,6 +377,18 @@ TEST(URLFixerUpperTest, FixupFile) {
// A plain "/" refers to the root.
{"/", "",
"file:///"},
+
+ // These rely on the above home_directory_override.
+ {"~", "",
+ "file:///foo"},
+ {"~/bar", "",
+ "file:///foo/bar"},
+
+ // References to other users' homedirs.
+ {"~foo", "",
+ "file://" HOME "foo"},
+ {"~x/blah", "",
+ "file://" HOME "x/blah"},
};
#endif
for (size_t i = 0; i < arraysize(file_cases); i++) {