diff options
author | mrossetti@chromium.org <mrossetti@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-11 22:55:04 +0000 |
---|---|---|
committer | mrossetti@chromium.org <mrossetti@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-11 22:55:04 +0000 |
commit | ea798f750571aed66bbcc912e83cd0661e8b2e2e (patch) | |
tree | 443d8f8501f06653552413351b3994cacbb63ff5 /chrome/browser/autocomplete/history_url_provider.cc | |
parent | d90617b6b1278a6bdcc239c61bf4d58c1b822704 (diff) | |
download | chromium_src-ea798f750571aed66bbcc912e83cd0661e8b2e2e.zip chromium_src-ea798f750571aed66bbcc912e83cd0661e8b2e2e.tar.gz chromium_src-ea798f750571aed66bbcc912e83cd0661e8b2e2e.tar.bz2 |
Revert 62193 - Final phase of the URL History Quick autocomplete provider integration. This phase hooks up the InMemoryURLIndex to the HistoryQuickProvider which is itself made active during the autocomplete process. A small amount of refactoring was done to liberate some common functionality from the history_url_provider.
BUG=None
TEST=None
Review URL: http://codereview.chromium.org/3375002
TBR=mrossetti@chromium.org
Review URL: http://codereview.chromium.org/3660007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@62201 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/autocomplete/history_url_provider.cc')
-rw-r--r-- | chrome/browser/autocomplete/history_url_provider.cc | 95 |
1 files changed, 90 insertions, 5 deletions
diff --git a/chrome/browser/autocomplete/history_url_provider.cc b/chrome/browser/autocomplete/history_url_provider.cc index 234bed4..9dab45d 100644 --- a/chrome/browser/autocomplete/history_url_provider.cc +++ b/chrome/browser/autocomplete/history_url_provider.cc @@ -104,7 +104,7 @@ GURL ConvertToHostOnly(const HistoryMatch& match, const std::wstring& input) { HistoryURLProviderParams::HistoryURLProviderParams( const AutocompleteInput& input, bool trim_http, - const std::string& languages) + const std::wstring& languages) : message_loop(MessageLoop::current()), input(input), trim_http(trim_http), @@ -115,7 +115,7 @@ HistoryURLProviderParams::HistoryURLProviderParams( HistoryURLProvider::HistoryURLProvider(ACProviderListener* listener, Profile* profile) - : HistoryProvider(listener, profile, "HistoryURL"), + : AutocompleteProvider(listener, profile, "HistoryURL"), prefixes_(GetPrefixes()), params_(NULL) { } @@ -457,6 +457,91 @@ bool HistoryURLProvider::PromoteMatchForInlineAutocomplete( } // static +std::wstring HistoryURLProvider::FixupUserInput( + const AutocompleteInput& input) { + 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 canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); + if (canonical_gurl_str.empty()) { + // This probably won't happen, but there are no guarantees. + return input_text; + } + + // If the user types a number, GURL will convert it to a dotted quad. + // However, if the parser did not mark this as a URL, then the user probably + // didn't intend this interpretation. Since this can break history matching + // for hostname beginning with numbers (e.g. input of "17173" will be matched + // against "0.0.67.21" instead of the original "17173", failing to find + // "17173.com"), swap the original hostname in for the fixed-up one. + if ((input.type() != AutocompleteInput::URL) && + canonical_gurl.HostIsIPAddress()) { + std::string original_hostname = + WideToUTF8(input_text.substr(input.parts().host.begin, + input.parts().host.len)); + const url_parse::Parsed& parts = + canonical_gurl.parsed_for_possibly_invalid_spec(); + // parts.host must not be empty when HostIsIPAddress() is true. + DCHECK(parts.host.is_nonempty()); + canonical_gurl_str.replace(parts.host.begin, parts.host.len, + original_hostname); + } + std::wstring output(UTF8ToWide(canonical_gurl_str)); + // Don't prepend a scheme when the user didn't have one. Since the fixer + // upper only prepends the "http" scheme, that's all we need to check for. + if (canonical_gurl.SchemeIs(chrome::kHttpScheme) && + !url_util::FindAndCompareScheme(WideToUTF8(input_text), + chrome::kHttpScheme, NULL)) + TrimHttpPrefix(&output); + + // Make the number of trailing slashes on the output exactly match the input. + // Examples of why not doing this would matter: + // * The user types "a" and has this fixed up to "a/". Now no other sites + // beginning with "a" will match. + // * The user types "file:" and has this fixed up to "file://". Now inline + // autocomplete will append too few slashes, resulting in e.g. "file:/b..." + // instead of "file:///b..." + // * The user types "http:/" and has this fixed up to "http:". Now inline + // autocomplete will append too many slashes, resulting in e.g. + // "http:///c..." instead of "http://c...". + // NOTE: We do this after calling TrimHttpPrefix() since that can strip + // trailing slashes (if the scheme is the only thing in the input). It's not + // clear that the result of fixup really matters in this case, but there's no + // harm in making sure. + const size_t last_input_nonslash = input_text.find_last_not_of(L"/\\"); + const size_t num_input_slashes = (last_input_nonslash == std::wstring::npos) ? + input_text.length() : (input_text.length() - 1 - last_input_nonslash); + const size_t last_output_nonslash = output.find_last_not_of(L"/\\"); + const size_t num_output_slashes = + (last_output_nonslash == std::wstring::npos) ? + output.length() : (output.length() - 1 - last_output_nonslash); + if (num_output_slashes < num_input_slashes) + output.append(num_input_slashes - num_output_slashes, '/'); + else if (num_output_slashes > num_input_slashes) + output.erase(output.length() - num_output_slashes + num_input_slashes); + + return output; +} + +// 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 history::Prefixes HistoryURLProvider::GetPrefixes() { // We'll complete text following these prefixes. // NOTE: There's no requirement that these be in any particular order. @@ -611,10 +696,10 @@ void HistoryURLProvider::RunAutocompletePasses( // Create the data structure for the autocomplete passes. We'll save this off // onto the |params_| member for later deletion below if we need to run pass // 2. - std::string languages(languages_); + std::wstring languages(languages_); if (languages.empty() && profile_) { languages = - profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); + UTF8ToWide(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); } scoped_ptr<HistoryURLProviderParams> params( new HistoryURLProviderParams(input, trim_http, languages)); @@ -803,7 +888,7 @@ AutocompleteMatch HistoryURLProvider::HistoryMatchToACMatch( size_t inline_autocomplete_offset = history_match.input_location + params->input.text().length(); std::string languages = (match_type == WHAT_YOU_TYPED) ? - std::string() : params->languages; + std::string() : WideToUTF8(params->languages); const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll & ~((params->trim_http && !history_match.match_in_scheme) ? 0 : net::kFormatUrlOmitHTTP); |