summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autocomplete
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-18 22:44:49 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-18 22:44:49 +0000
commit76e7da2f3993ed156cc633d84f0c0d8ce3475e47 (patch)
tree0bf83bcc78d1c22ce608289415d1bcd39761694c /chrome/browser/autocomplete
parent2f1412eee183dfb9e04777827aae4a8766fc9f45 (diff)
downloadchromium_src-76e7da2f3993ed156cc633d84f0c0d8ce3475e47.zip
chromium_src-76e7da2f3993ed156cc633d84f0c0d8ce3475e47.tar.gz
chromium_src-76e7da2f3993ed156cc633d84f0c0d8ce3475e47.tar.bz2
Make FixupURL() return a GURL instead of a string (i.e. do fixup + canonicalization). Nearly every caller was already doing this.
This in turn allows us to do better fixup/canonicalization of view-source: URLs. We now convert "view-source:google.com" into "view-source:http://google.com/". With a few changes scattered through the omnibox code, this also means we can do HTTP-stripping on view-source: URLs, and support the user typing in things like the case above. This also fixes some weirdness where if you tried to type something starting with "view-source:", the What You Typed match in the dropdown would show only a scheme, or a scheme plus "http:", in some cases. BUG=46612 TEST="view-source:google.com" should work. Review URL: http://codereview.chromium.org/2817011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@50290 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/autocomplete')
-rw-r--r--chrome/browser/autocomplete/autocomplete.cc22
-rw-r--r--chrome/browser/autocomplete/autocomplete.h5
-rw-r--r--chrome/browser/autocomplete/autocomplete_edit.cc2
-rw-r--r--chrome/browser/autocomplete/autocomplete_unittest.cc3
-rw-r--r--chrome/browser/autocomplete/history_contents_provider.cc3
-rw-r--r--chrome/browser/autocomplete/history_url_provider.cc22
-rw-r--r--chrome/browser/autocomplete/history_url_provider.h6
-rw-r--r--chrome/browser/autocomplete/search_provider.cc10
8 files changed, 41 insertions, 32 deletions
diff --git a/chrome/browser/autocomplete/autocomplete.cc b/chrome/browser/autocomplete/autocomplete.cc
index d92677e..cdaa56f 100644
--- a/chrome/browser/autocomplete/autocomplete.cc
+++ b/chrome/browser/autocomplete/autocomplete.cc
@@ -572,23 +572,13 @@ void AutocompleteProvider::SetProfile(Profile* profile) {
}
// static
-size_t AutocompleteProvider::TrimHttpPrefix(std::wstring* url) {
+bool AutocompleteProvider::HasHTTPScheme(const std::wstring& input) {
+ std::string utf8_input(WideToUTF8(input));
url_parse::Component scheme;
- if (!url_util::FindAndCompareScheme(WideToUTF8(*url), chrome::kHttpScheme,
- &scheme))
- return 0; // Not "http".
-
- // Erase scheme plus up to two slashes.
- size_t prefix_len = scheme.end() + 1; // "http:"
- const size_t after_slashes = std::min(url->length(),
- static_cast<size_t>(scheme.end() + 3));
- while ((prefix_len < after_slashes) && ((*url)[prefix_len] == L'/'))
- ++prefix_len;
- if (prefix_len == url->length())
- url->clear();
- else
- url->erase(url->begin(), url->begin() + prefix_len);
- return prefix_len;
+ if (url_util::FindAndCompareScheme(utf8_input, chrome::kViewSourceScheme,
+ &scheme))
+ utf8_input.erase(0, scheme.end() + 1);
+ return url_util::FindAndCompareScheme(utf8_input, chrome::kHttpScheme, NULL);
}
void AutocompleteProvider::UpdateStarredStateOfMatches() {
diff --git a/chrome/browser/autocomplete/autocomplete.h b/chrome/browser/autocomplete/autocomplete.h
index 4f85cf4..2338846 100644
--- a/chrome/browser/autocomplete/autocomplete.h
+++ b/chrome/browser/autocomplete/autocomplete.h
@@ -560,9 +560,8 @@ class AutocompleteProvider
virtual ~AutocompleteProvider();
- // Trims "http:" and up to two subsequent slashes from |url|. Returns the
- // number of characters that were trimmed.
- static size_t TrimHttpPrefix(std::wstring* url);
+ // Returns whether |input| begins "http:" or "view-source:http:".
+ static bool HasHTTPScheme(const std::wstring& input);
// Updates the starred state of each of the matches in matches_ from the
// profile's bookmark bar model.
diff --git a/chrome/browser/autocomplete/autocomplete_edit.cc b/chrome/browser/autocomplete/autocomplete_edit.cc
index 07f584c..6b14c17 100644
--- a/chrome/browser/autocomplete/autocomplete_edit.cc
+++ b/chrome/browser/autocomplete/autocomplete_edit.cc
@@ -163,7 +163,7 @@ bool AutocompleteEditModel::GetURLForText(const std::wstring& text,
if (type != AutocompleteInput::URL)
return false;
- *url = GURL(URLFixerUpper::FixupURL(WideToUTF8(text), std::string()));
+ *url = URLFixerUpper::FixupURL(WideToUTF8(text), std::string());
return true;
}
diff --git a/chrome/browser/autocomplete/autocomplete_unittest.cc b/chrome/browser/autocomplete/autocomplete_unittest.cc
index 8042724..6a66e98 100644
--- a/chrome/browser/autocomplete/autocomplete_unittest.cc
+++ b/chrome/browser/autocomplete/autocomplete_unittest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Copyright (c) 2010 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.
@@ -348,6 +348,7 @@ TEST(AutocompleteInput, ParseForEmphasizeComponent) {
{ L"view-source:http://www.foo.com/", Component(12, 4), Component(19, 11) },
{ L"view-source:https://example.com/",
Component(12, 5), Component(20, 11) },
+ { L"view-source:www.foo.com", kInvalidComponent, Component(12, 11) },
{ L"view-source:", Component(0, 11), kInvalidComponent },
{ L"view-source:garbage", kInvalidComponent, Component(12, 7) },
{ L"view-source:http://http://foo", Component(12, 4), Component(19, 4) },
diff --git a/chrome/browser/autocomplete/history_contents_provider.cc b/chrome/browser/autocomplete/history_contents_provider.cc
index deb35d2..c05fbdc 100644
--- a/chrome/browser/autocomplete/history_contents_provider.cc
+++ b/chrome/browser/autocomplete/history_contents_provider.cc
@@ -75,8 +75,7 @@ void HistoryContentsProvider::Start(const AutocompleteInput& input,
// Change input type so matches will be marked up properly.
input_type_ = input.type();
- trim_http_ = !url_util::FindAndCompareScheme(WideToUTF8(input.text()),
- chrome::kHttpScheme, NULL);
+ trim_http_ = !HasHTTPScheme(input.text());
// Decide what to do about any previous query/results.
if (!minimal_changes) {
diff --git a/chrome/browser/autocomplete/history_url_provider.cc b/chrome/browser/autocomplete/history_url_provider.cc
index 703b909..1952a65 100644
--- a/chrome/browser/autocomplete/history_url_provider.cc
+++ b/chrome/browser/autocomplete/history_url_provider.cc
@@ -365,7 +365,7 @@ std::wstring HistoryURLProvider::FixupUserInput(
const std::wstring& input_text = input.text();
// Fixup and canonicalize user input.
const GURL canonical_gurl(URLFixerUpper::FixupURL(WideToUTF8(input_text),
- std::string()));
+ std::string()));
std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec());
if (canonical_gurl_str.empty()) {
// This probably won't happen, but there are no guarantees.
@@ -428,6 +428,23 @@ std::wstring HistoryURLProvider::FixupUserInput(
}
// static
+size_t HistoryURLProvider::TrimHttpPrefix(std::wstring* url) {
+ // Find any "http:".
+ if (!HasHTTPScheme(*url))
+ return 0;
+ size_t scheme_pos = url->find(ASCIIToWide(chrome::kHttpScheme) + L":");
+ DCHECK(scheme_pos != std::wstring::npos);
+
+ // Erase scheme plus up to two slashes.
+ size_t prefix_end = scheme_pos + strlen(chrome::kHttpScheme) + 1;
+ const size_t after_slashes = std::min(url->length(), prefix_end + 2);
+ while ((prefix_end < after_slashes) && ((*url)[prefix_end] == L'/'))
+ ++prefix_end;
+ url->erase(scheme_pos, prefix_end - scheme_pos);
+ return (scheme_pos == 0) ? prefix_end : 0;
+}
+
+// static
bool HistoryURLProvider::IsHostOnly(const GURL& url) {
DCHECK(url.is_valid());
return (!url.has_path() || (url.path() == "/")) && !url.has_query() &&
@@ -627,8 +644,7 @@ void HistoryURLProvider::RunAutocompletePasses(
// Create a match for exactly what the user typed. This will only be used as
// a fallback in case we can't get the history service or URL DB; otherwise,
// we'll run this again in DoAutocomplete() and use that result instead.
- const bool trim_http = !url_util::FindAndCompareScheme(
- WideToUTF8(input.text()), chrome::kHttpScheme, NULL);
+ const bool trim_http = !HasHTTPScheme(input.text());
// Don't do this for queries -- while we can sometimes mark up a match for
// this, it's not what the user wants, and just adds noise.
if ((input.type() != AutocompleteInput::QUERY) &&
diff --git a/chrome/browser/autocomplete/history_url_provider.h b/chrome/browser/autocomplete/history_url_provider.h
index da4fc61..e98d777 100644
--- a/chrome/browser/autocomplete/history_url_provider.h
+++ b/chrome/browser/autocomplete/history_url_provider.h
@@ -261,6 +261,12 @@ class HistoryURLProvider : public AutocompleteProvider {
// output that surprises the user ("Search Google for xn--6ca.com").
static std::wstring FixupUserInput(const AutocompleteInput& input);
+ // Trims "http:" and up to two subsequent slashes from |url|. Returns the
+ // number of characters that were trimmed.
+ // NOTE: For a view-source: URL, this will trim from after "view-source:" and
+ // return 0.
+ static size_t TrimHttpPrefix(std::wstring* url);
+
// Returns true if |url| is just a host (e.g. "http://www.google.com/") and
// not some other subpage (e.g. "http://www.google.com/foo.html").
static bool IsHostOnly(const GURL& url);
diff --git a/chrome/browser/autocomplete/search_provider.cc b/chrome/browser/autocomplete/search_provider.cc
index ba550a6..3da49df 100644
--- a/chrome/browser/autocomplete/search_provider.cc
+++ b/chrome/browser/autocomplete/search_provider.cc
@@ -445,9 +445,8 @@ bool SearchProvider::ParseSuggestResults(Value* root_val,
site_val->IsType(Value::TYPE_STRING) &&
site_val->GetAsString(&site_name)) {
// We can't blindly trust the URL coming from the server to be valid.
- GURL result_url =
- GURL(URLFixerUpper::FixupURL(WideToUTF8(suggestion_str),
- std::string()));
+ GURL result_url(URLFixerUpper::FixupURL(WideToUTF8(suggestion_str),
+ std::string()));
if (result_url.is_valid())
navigation_results.push_back(NavigationResult(result_url, site_name));
}
@@ -737,9 +736,8 @@ AutocompleteMatch SearchProvider::NavigationToMatch(
AutocompleteMatch match(this, relevance, false,
AutocompleteMatch::NAVSUGGEST);
match.destination_url = navigation.url;
- const bool trim_http = !url_util::FindAndCompareScheme(
- WideToUTF8(input_text), chrome::kHttpScheme, NULL);
- match.contents = StringForURLDisplay(navigation.url, true, trim_http);
+ match.contents =
+ StringForURLDisplay(navigation.url, true, !HasHTTPScheme(input_text));
AutocompleteMatch::ClassifyMatchInString(input_text, match.contents,
ACMatchClassification::URL,
&match.contents_class);