summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/url_fixer_upper.cc
diff options
context:
space:
mode:
authorestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-23 01:51:23 +0000
committerestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-23 01:51:23 +0000
commitb1c33f8b46f7f2b2a4489609c4b8d6e0d6905f25 (patch)
tree960b966701601a19072ea06648e19bc991060d35 /chrome/browser/net/url_fixer_upper.cc
parent3efc4679d44aa442d4ae35ed95dff3ef0cf5ded6 (diff)
downloadchromium_src-b1c33f8b46f7f2b2a4489609c4b8d6e0d6905f25.zip
chromium_src-b1c33f8b46f7f2b2a4489609c4b8d6e0d6905f25.tar.gz
chromium_src-b1c33f8b46f7f2b2a4489609c4b8d6e0d6905f25.tar.bz2
Fix strings in url_fixer_upper.
* change URLs from wstring to string * change filenames from wstring to FilePath * url_fixer_upper{_unittest}.cc building on posix Review URL: http://codereview.chromium.org/18525 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8536 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/net/url_fixer_upper.cc')
-rw-r--r--chrome/browser/net/url_fixer_upper.cc218
1 files changed, 129 insertions, 89 deletions
diff --git a/chrome/browser/net/url_fixer_upper.cc b/chrome/browser/net/url_fixer_upper.cc
index 1aca150..ec908cd 100644
--- a/chrome/browser/net/url_fixer_upper.cc
+++ b/chrome/browser/net/url_fixer_upper.cc
@@ -3,7 +3,6 @@
// found in the LICENSE file.
#include <algorithm>
-#include <windows.h>
#include "chrome/browser/net/url_fixer_upper.h"
@@ -23,23 +22,26 @@
using namespace std;
// does some basic fixes for input that we want to test for file-ness
-static void PrepareStringForFileOps(const wstring& text, wstring* output) {
- TrimWhitespace(text, TRIM_ALL, output);
+static void PrepareStringForFileOps(const FilePath& text,
+ FilePath::StringType* output) {
+ TrimWhitespace(text.value(), TRIM_ALL, output);
+#if defined(OS_WIN)
replace(output->begin(), output->end(), '/', '\\');
+#endif
}
// Tries to create a full path from |text|. If the result is valid and the
// file exists, returns true and sets |full_path| to the result. Otherwise,
// returns false and leaves |full_path| unchanged.
-static bool ValidPathForFile(const wstring& text, wstring* full_path) {
- wchar_t file_path[MAX_PATH];
- if (!_wfullpath(file_path, text.c_str(), MAX_PATH))
- return false;
+static bool ValidPathForFile(const FilePath::StringType& text,
+ FilePath* full_path) {
+ FilePath file_path(text);
+ file_util::AbsolutePath(&file_path);
if (!file_util::PathExists(file_path))
return false;
- full_path->assign(file_path);
+ *full_path = file_path;
return true;
}
@@ -49,19 +51,26 @@ static bool ValidPathForFile(const wstring& text, wstring* full_path) {
// with a drive specifier or "\\". Returns false in other cases (including
// file: URLs: these don't look like filenames), leaving fixed_up_url
// unchanged.
-static wstring FixupPath(const wstring& text) {
+static string FixupPath(const string& text) {
DCHECK(text.length() >= 2);
- wstring filename;
- PrepareStringForFileOps(text, &filename);
+ FilePath::StringType filename;
+#if defined(OS_WIN)
+ FilePath input_path(UTF8ToWide(text));
+#elif defined(OS_POSIX)
+ FilePath input_path(text);
+#endif
+ PrepareStringForFileOps(input_path, &filename);
if (filename[1] == '|')
filename[1] = ':';
// Here, we know the input looks like a file.
- GURL file_url = net::FilePathToFileURL(filename);
- if (file_url.is_valid())
- return gfx::ElideUrl(file_url, ChromeFont(), 0, std::wstring());
+ GURL file_url = net::FilePathToFileURL(FilePath(filename));
+ if (file_url.is_valid()) {
+ return WideToUTF8(gfx::GetCleanStringFromUrl(file_url, std::wstring(),
+ NULL, NULL));
+ }
// Invalid file URL, just return the input.
return text;
@@ -70,13 +79,13 @@ static wstring FixupPath(const wstring& text) {
// Checks |domain| to see if a valid TLD is already present. If not, appends
// |desired_tld| to the domain, and prepends "www." unless it's already present.
// Then modifies |fixed_up_url| to reflect the changes.
-static void AddDesiredTLD(const wstring& desired_tld,
- wstring* domain) {
+static void AddDesiredTLD(const string& desired_tld,
+ string* domain) {
if (desired_tld.empty() || domain->empty())
return;
// Check the TLD. If the return value is positive, we already have a TLD, so
- // abort; if the return value is wstring::npos, there's no valid host (e.g. if
+ // abort; if the return value is string::npos, there's no valid host (e.g. if
// the user pasted in garbage for which HistoryURLProvider is trying to
// suggest an exact match), so adding a TLD makes no sense. The only useful
// case is where the return value is 0 (there's a valid host with no known
@@ -96,16 +105,16 @@ static void AddDesiredTLD(const wstring& desired_tld,
domain->append(desired_tld);
// Now, if the domain begins with "www.", stop.
- const wstring prefix(L"www.");
+ const string prefix("www.");
if (domain->compare(0, prefix.length(), prefix) != 0) {
// Otherwise, add www. to the beginning of the URL.
domain->insert(0, prefix);
}
}
-static inline void FixupUsername(const wstring& text,
+static inline void FixupUsername(const string& text,
const url_parse::Component& part,
- wstring* url) {
+ string* url) {
if (!part.is_valid())
return;
@@ -115,22 +124,22 @@ static inline void FixupUsername(const wstring& text,
// password. FixupURL itself will append the '@' for us.
}
-static inline void FixupPassword(const wstring& text,
+static inline void FixupPassword(const string& text,
const url_parse::Component& part,
- wstring* url) {
+ string* url) {
if (!part.is_valid())
return;
// We don't fix up the password at the moment.
- url->append(L":");
+ url->append(":");
url->append(text, part.begin, part.len);
}
-static void FixupHost(const wstring& text,
+static void FixupHost(const string& text,
const url_parse::Component& part,
bool has_scheme,
- const wstring& desired_tld,
- wstring* url) {
+ const string& desired_tld,
+ string* url) {
if (!part.is_valid())
return;
@@ -138,12 +147,12 @@ static void FixupHost(const wstring& text,
// Strip all leading dots and all but one trailing dot, unless the user only
// typed dots, in which case their input is totally invalid and we should just
// leave it unchanged.
- wstring domain(text, part.begin, part.len);
+ string domain(text, part.begin, part.len);
const size_t first_nondot(domain.find_first_not_of('.'));
- if (first_nondot != wstring::npos) {
+ if (first_nondot != string::npos) {
domain.erase(0, first_nondot);
size_t last_nondot(domain.find_last_not_of('.'));
- DCHECK(last_nondot != wstring::npos);
+ DCHECK(last_nondot != string::npos);
last_nondot += 2; // Point at second period in ending string
if (last_nondot < domain.length())
domain.erase(last_nondot);
@@ -159,15 +168,15 @@ static void FixupHost(const wstring& text,
// something invalid (which cannot be fixed up) is found, like ":foo" or
// ":7:7", returns false. Otherwise, removes any extra colons
// ("::1337" -> ":1337", ":/" -> "/") and returns true.
-static void FixupPort(const wstring& text,
+static void FixupPort(const string& text,
const url_parse::Component& part,
- wstring* url) {
+ string* url) {
if (!part.is_valid())
return;
// Look for non-digit in port and strip if found.
- wstring port(text, part.begin, part.len);
- for (wstring::iterator i = port.begin(); i != port.end(); ) {
+ string port(text, part.begin, part.len);
+ for (string::iterator i = port.begin(); i != port.end(); ) {
if (IsAsciiDigit(*i))
++i;
else
@@ -175,18 +184,18 @@ static void FixupPort(const wstring& text,
}
if (port.empty())
- return; // Nothing to append.
+ return; // Nothing to append.
- url->append(L":");
+ url->append(":");
url->append(port);
}
-static inline void FixupPath(const wstring& text,
+static inline void FixupPath(const string& text,
const url_parse::Component& part,
- wstring* url) {
+ string* url) {
if (!part.is_valid() || part.len == 0) {
// We should always have a path.
- url->append(L"/");
+ url->append("/");
return;
}
@@ -194,25 +203,25 @@ static inline void FixupPath(const wstring& text,
url->append(text, part.begin, part.len);
}
-static inline void FixupQuery(const wstring& text,
+static inline void FixupQuery(const string& text,
const url_parse::Component& part,
- wstring* url) {
+ string* url) {
if (!part.is_valid())
return;
// We don't fix up the query at the moment.
- url->append(L"?");
+ url->append("?");
url->append(text, part.begin, part.len);
}
-static inline void FixupRef(const wstring& text,
+static inline void FixupRef(const string& text,
const url_parse::Component& part,
- wstring* url) {
+ string* url) {
if (!part.is_valid())
return;
// We don't fix up the ref at the moment.
- url->append(L"#");
+ url->append("#");
url->append(text, part.begin, part.len);
}
@@ -229,9 +238,8 @@ static void OffsetComponent(int offset, url_parse::Component* part) {
}
}
-static bool HasPort(const std::wstring& original_text,
- const url_parse::Component& scheme_component,
- const std::wstring& scheme) {
+static bool HasPort(const std::string& original_text,
+ const url_parse::Component& scheme_component) {
// Find the range between the ":" and the "/".
size_t port_start = scheme_component.end() + 1;
size_t port_end = port_start;
@@ -250,23 +258,28 @@ static bool HasPort(const std::wstring& original_text,
return true;
}
-wstring URLFixerUpper::SegmentURL(const wstring& text,
- url_parse::Parsed* parts) {
+string URLFixerUpper::SegmentURL(const string& text,
+ url_parse::Parsed* parts) {
// Initialize the result.
*parts = url_parse::Parsed();
- wstring trimmed;
+ string trimmed;
TrimWhitespace(text, TRIM_ALL, &trimmed);
if (trimmed.empty())
- return wstring(); // Nothing to segment.
+ return string(); // Nothing to segment.
+#if defined(OS_WIN)
int trimmed_length = static_cast<int>(trimmed.length());
- if (url_parse::DoesBeginWindowsDriveSpec(trimmed.data(), 0, trimmed_length)
- || url_parse::DoesBeginUNCPath(trimmed.data(), 0, trimmed_length, false))
- return L"file";
+ if (url_parse::DoesBeginWindowsDriveSpec(trimmed.data(), 0, trimmed_length) ||
+ url_parse::DoesBeginUNCPath(trimmed.data(), 0, trimmed_length, false))
+ return "file";
+#elif defined(OS_POSIX)
+ if (FilePath::IsSeparator(trimmed.c_str()[0]))
+ return "file";
+#endif
// Otherwise, we need to look at things carefully.
- wstring scheme;
+ string scheme;
if (url_parse::ExtractScheme(text.data(),
static_cast<int>(text.length()),
&parts->scheme)) {
@@ -279,11 +292,11 @@ wstring URLFixerUpper::SegmentURL(const wstring& text,
(!IsStringASCII(scheme) ||
// We need to fix up the segmentation for "www.example.com:/". For this
// case, we guess that schemes with a "." are not actually schemes.
- (scheme.find(L".") != wstring::npos) ||
+ (scheme.find(".") != wstring::npos) ||
// We need to fix up the segmentation for "www:123/". For this case, we
// will add an HTTP scheme later and make the URL parser happy.
// TODO(pkasting): Maybe we should try to use GURL's parser for this?
- HasPort(text, parts->scheme, scheme)))
+ HasPort(text, parts->scheme)))
parts->scheme.reset();
}
@@ -291,15 +304,15 @@ wstring URLFixerUpper::SegmentURL(const wstring& text,
// we choose http, but if the URL starts with "ftp.", we match other browsers
// and choose ftp.
if (!parts->scheme.is_valid())
- scheme.assign(StartsWith(text, L"ftp.", false) ? L"ftp" : L"http");
+ scheme.assign(StartsWithASCII(text, "ftp.", false) ? "ftp" : "http");
// Cannonicalize the scheme.
StringToLowerASCII(&scheme);
// Not segmenting file schemes or nonstandard schemes.
- if ((scheme == L"file") ||
+ if ((scheme == "file") ||
!url_util::IsStandard(scheme.c_str(), static_cast<int>(scheme.length()),
- url_parse::Component(0, static_cast<int>(scheme.length()))))
+ url_parse::Component(0, static_cast<int>(scheme.length()))))
return scheme;
if (parts->scheme.is_valid()) {
@@ -311,14 +324,14 @@ wstring URLFixerUpper::SegmentURL(const wstring& text,
// We need to add a scheme in order for ParseStandardURL to be happy.
// Find the first non-whitespace character.
- wstring::const_iterator first_nonwhite = text.begin();
+ string::const_iterator first_nonwhite = text.begin();
while ((first_nonwhite != text.end()) && IsWhitespace(*first_nonwhite))
++first_nonwhite;
// Construct the text to parse by inserting the scheme.
- wstring inserted_text(scheme);
- inserted_text.append(L"://");
- wstring text_to_parse(text.begin(), first_nonwhite);
+ string inserted_text(scheme);
+ inserted_text.append("://");
+ string text_to_parse(text.begin(), first_nonwhite);
text_to_parse.append(inserted_text);
text_to_parse.append(first_nonwhite, text.end());
@@ -341,26 +354,26 @@ wstring URLFixerUpper::SegmentURL(const wstring& text,
return scheme;
}
-std::wstring URLFixerUpper::FixupURL(const wstring& text,
- const wstring& desired_tld) {
- wstring trimmed;
+string URLFixerUpper::FixupURL(const string& text,
+ const string& desired_tld) {
+ string trimmed;
TrimWhitespace(text, TRIM_ALL, &trimmed);
if (trimmed.empty())
- return wstring(); // Nothing here.
+ return string(); // Nothing here.
// Segment the URL.
url_parse::Parsed parts;
- wstring scheme(SegmentURL(trimmed, &parts));
+ string scheme(SegmentURL(trimmed, &parts));
// We handle the file scheme separately.
- if (scheme == L"file")
+ if (scheme == "file")
return (parts.scheme.is_valid() ? text : FixupPath(text));
// For some schemes whose layouts we understand, we rebuild it.
if (url_util::IsStandard(scheme.c_str(), static_cast<int>(scheme.length()),
url_parse::Component(0, static_cast<int>(scheme.length())))) {
- wstring url(scheme);
- url.append(L"://");
+ string url(scheme);
+ url.append("://");
// We need to check whether the |username| is valid because it is our
// responsibility to append the '@' to delineate the user information from
@@ -368,7 +381,7 @@ std::wstring URLFixerUpper::FixupURL(const wstring& text,
if (parts.username.is_valid()) {
FixupUsername(trimmed, parts.username, &url);
FixupPassword(trimmed, parts.password, &url);
- url.append(L"@");
+ url.append("@");
}
FixupHost(trimmed, parts.host, parts.scheme.is_valid(), desired_tld, &url);
@@ -382,8 +395,8 @@ std::wstring URLFixerUpper::FixupURL(const wstring& text,
// In the worst-case, we insert a scheme if the URL lacks one.
if (!parts.scheme.is_valid()) {
- wstring fixed_scheme(scheme);
- fixed_scheme.append(L"://");
+ string fixed_scheme(scheme);
+ fixed_scheme.append("://");
trimmed.insert(0, fixed_scheme);
}
@@ -395,45 +408,72 @@ std::wstring URLFixerUpper::FixupURL(const wstring& text,
// fixup will look for cues that it is actually a file path before trying to
// figure out what file it is. If our logic doesn't work, we will fall back on
// regular fixup.
-wstring URLFixerUpper::FixupRelativeFile(const wstring& base_dir,
- const wstring& text) {
- wchar_t old_cur_directory[MAX_PATH];
+string URLFixerUpper::FixupRelativeFile(const FilePath& base_dir,
+ const FilePath& text) {
+ FilePath old_cur_directory;
if (!base_dir.empty()) {
- // save the old current directory before we move to the new one
- // TODO: in the future, we may want to handle paths longer than MAX_PATH
- GetCurrentDirectory(MAX_PATH, old_cur_directory);
- SetCurrentDirectory(base_dir.c_str());
+ // Save the old current directory before we move to the new one.
+ file_util::GetCurrentDirectory(&old_cur_directory);
+ file_util::SetCurrentDirectory(base_dir);
}
- // allow funny input with extra whitespace and the wrong kind of slashes
- wstring trimmed;
+ // Allow funny input with extra whitespace and the wrong kind of slashes.
+ FilePath::StringType trimmed;
PrepareStringForFileOps(text, &trimmed);
bool is_file = true;
- wstring full_path;
+ FilePath full_path;
if (!ValidPathForFile(trimmed, &full_path)) {
// Not a path as entered, try unescaping it in case the user has
// escaped things. We need to go through 8-bit since the escaped values
// only represent 8-bit values.
+#if defined(OS_WIN)
std::wstring unescaped = UTF8ToWide(UnescapeURLComponent(
WideToUTF8(trimmed),
UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS));
+#elif defined(OS_POSIX)
+ std::string unescaped = UnescapeURLComponent(
+ trimmed,
+ UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS);
+#endif
+
if (!ValidPathForFile(unescaped, &full_path))
is_file = false;
}
// Put back the current directory if we saved it.
- if (!base_dir.empty())
- SetCurrentDirectory(old_cur_directory);
+ if (!base_dir.empty()) {
+ file_util::SetCurrentDirectory(old_cur_directory);
+ }
if (is_file) {
GURL file_url = net::FilePathToFileURL(full_path);
if (file_url.is_valid())
- return gfx::ElideUrl(file_url, ChromeFont(), 0, std::wstring());
+ return WideToUTF8(gfx::GetCleanStringFromUrl(file_url, std::wstring(),
+ NULL, NULL));
// Invalid files fall through to regular processing.
}
// Fall back on regular fixup for this input.
- return FixupURL(text, L"");
+#if defined(OS_WIN)
+ string text_utf8 = WideToUTF8(text.value());
+#elif defined(OS_POSIX)
+ string text_utf8 = text.value();
+#endif
+ return FixupURL(text_utf8, "");
}
+// Deprecated functions. To be removed when all callers are updated.
+wstring URLFixerUpper::SegmentURL(const wstring& text,
+ url_parse::Parsed* parts) {
+ return UTF8ToWide(SegmentURL(WideToUTF8(text), parts));
+}
+wstring URLFixerUpper::FixupURL(const wstring& text,
+ const wstring& desired_tld) {
+ return UTF8ToWide(FixupURL(WideToUTF8(text), WideToUTF8(desired_tld)));
+}
+wstring URLFixerUpper::FixupRelativeFile(const wstring& base_dir,
+ const wstring& text) {
+ return UTF8ToWide(FixupRelativeFile(FilePath::FromWStringHack(base_dir),
+ FilePath::FromWStringHack(text)));
+}