summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/url_fixer_upper.cc
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/browser/net/url_fixer_upper.cc
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/browser/net/url_fixer_upper.cc')
-rw-r--r--chrome/browser/net/url_fixer_upper.cc36
1 files changed, 35 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