diff options
author | mrossetti@chromium.org <mrossetti@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-10 17:23:14 +0000 |
---|---|---|
committer | mrossetti@chromium.org <mrossetti@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-10 17:23:14 +0000 |
commit | 91c0cf566ad54739115992c3e63a7b7a3b262c1f (patch) | |
tree | 4bec3213579dcb077a17b18862fb502389baa1f2 /chrome/browser/autocomplete/history_quick_provider.cc | |
parent | 3c4d26cf52210d2367fd79369fc527f4ec2268b3 (diff) | |
download | chromium_src-91c0cf566ad54739115992c3e63a7b7a3b262c1f.zip chromium_src-91c0cf566ad54739115992c3e63a7b7a3b262c1f.tar.gz chromium_src-91c0cf566ad54739115992c3e63a7b7a3b262c1f.tar.bz2 |
Implemented substring matching within page titles. Fixed bug where URL was being lower-cased. Added unit tests for page title matching as well as unit tests for various static functions and other areas not previously unit tested.
BUG=58958,57853,58559,60107,75245
TEST=Many unit tests added.
Review URL: http://codereview.chromium.org/6652008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77646 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/autocomplete/history_quick_provider.cc')
-rw-r--r-- | chrome/browser/autocomplete/history_quick_provider.cc | 108 |
1 files changed, 56 insertions, 52 deletions
diff --git a/chrome/browser/autocomplete/history_quick_provider.cc b/chrome/browser/autocomplete/history_quick_provider.cc index 8caf7e4..af0148c 100644 --- a/chrome/browser/autocomplete/history_quick_provider.cc +++ b/chrome/browser/autocomplete/history_quick_provider.cc @@ -9,7 +9,6 @@ #include "base/logging.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" -#include "chrome/browser/autocomplete/autocomplete_match.h" #include "chrome/browser/history/history.h" #include "chrome/browser/history/in_memory_url_index.h" #include "chrome/browser/net/url_fixer_upper.h" @@ -17,7 +16,7 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/common/pref_names.h" #include "chrome/common/url_constants.h" -#include "content/browser/plugin_service.h" +#include "googleurl/src/url_parse.h" #include "content/common/notification_source.h" #include "content/common/notification_type.h" #include "googleurl/src/url_util.h" @@ -31,7 +30,6 @@ using history::ScoredHistoryMatches; HistoryQuickProvider::HistoryQuickProvider(ACProviderListener* listener, Profile* profile) : HistoryProvider(listener, profile, "HistoryQuickProvider"), - trim_http_(false), languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)) {} HistoryQuickProvider::~HistoryQuickProvider() {} @@ -45,7 +43,6 @@ void HistoryQuickProvider::Start(const AutocompleteInput& input, return; autocomplete_input_ = input; - trim_http_ = !HasHTTPScheme(input.text()); // Do some fixup on the user input before matching against it, so we provide // good results for local file paths, input with spaces, etc. @@ -80,41 +77,42 @@ void HistoryQuickProvider::DoAutocomplete() { term_string = UnescapeURLComponent(term_string, UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); history::InMemoryURLIndex::String16Vector terms( - HistoryQuickProvider::WordVectorFromString16(term_string)); + InMemoryURLIndex::WordVectorFromString16(term_string, false)); ScoredHistoryMatches matches = GetIndex()->HistoryItemsForTerms(terms); + if (matches.empty()) + return; size_t match_num = matches.size() - 1; for (ScoredHistoryMatches::const_iterator match_iter = matches.begin(); match_iter != matches.end(); ++match_iter, --match_num) { const ScoredHistoryMatch& history_match(*match_iter); - AutocompleteMatch ac_match = - QuickMatchToACMatch(history_match, NORMAL, match_num); - matches_.push_back(ac_match); + if (history_match.raw_score > 0) { + AutocompleteMatch ac_match = + QuickMatchToACMatch(history_match, match_num); + matches_.push_back(ac_match); + } } } AutocompleteMatch HistoryQuickProvider::QuickMatchToACMatch( const ScoredHistoryMatch& history_match, - MatchType match_type, size_t match_number) { const history::URLRow& info = history_match.url_info; int score = CalculateRelevance(history_match.raw_score, autocomplete_input_.type(), - match_type, match_number); + NORMAL, match_number); AutocompleteMatch match(this, score, !!info.visit_count(), - AutocompleteMatch::HISTORY_URL); + history_match.url_matches.empty() ? + AutocompleteMatch::HISTORY_URL : + AutocompleteMatch::HISTORY_TITLE); match.destination_url = info.url(); DCHECK(match.destination_url.is_valid()); size_t inline_autocomplete_offset = history_match.input_location + autocomplete_input_.text().length(); - const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll & - ~((trim_http_ && !history_match.match_in_scheme) ? - 0 : net::kFormatUrlOmitHTTP); - std::string languages = - match_type == WHAT_YOU_TYPED ? std::string() : languages_; + const net::FormatUrlTypes format_types = net::kFormatUrlOmitAll; match.fill_into_edit = AutocompleteInput::FormattedStringWithEquivalentMeaning(info.url(), - net::FormatUrl(info.url(), languages, format_types, + net::FormatUrl(info.url(), languages_, format_types, UnescapeRule::SPACES, NULL, NULL, &inline_autocomplete_offset)); if (!autocomplete_input_.prevent_inline_autocomplete()) @@ -122,27 +120,16 @@ AutocompleteMatch HistoryQuickProvider::QuickMatchToACMatch( DCHECK((match.inline_autocomplete_offset == string16::npos) || (match.inline_autocomplete_offset <= match.fill_into_edit.length())); - size_t match_start = history_match.input_location; - match.contents = net::FormatUrl(info.url(), languages, format_types, - UnescapeRule::SPACES, NULL, NULL, - &match_start); - if ((match_start != string16::npos) && - (inline_autocomplete_offset != string16::npos) && - (inline_autocomplete_offset != match_start)) { - DCHECK(inline_autocomplete_offset > match_start); - AutocompleteMatch::ClassifyLocationInString(match_start, - inline_autocomplete_offset - match_start, match.contents.length(), - ACMatchClassification::URL, &match.contents_class); - } else { - AutocompleteMatch::ClassifyLocationInString(string16::npos, 0, - match.contents.length(), ACMatchClassification::URL, - &match.contents_class); - } + // Format the URL autocomplete presentation. + match.contents = net::FormatUrl(info.url(), languages_, format_types, + UnescapeRule::SPACES, NULL, NULL, NULL); + match.contents_class = SpansFromTermMatch(history_match.url_matches, + match.contents.size(), 0); + + // Format the description autocomplete presentation. match.description = info.title(); - AutocompleteMatch::ClassifyMatchInString(autocomplete_input_.text(), - info.title(), - ACMatchClassification::NONE, - &match.description_class); + match.description_class = SpansFromTermMatch(history_match.title_matches, + match.description.size(), 0); return match; } @@ -165,21 +152,6 @@ void HistoryQuickProvider::SetIndexForTesting( index_for_testing_.reset(index); } -// Utility Functions - -history::InMemoryURLIndex::String16Vector - HistoryQuickProvider::WordVectorFromString16(const string16& uni_string) { - history::InMemoryURLIndex::String16Vector words; - base::BreakIterator iter(&uni_string, base::BreakIterator::BREAK_WORD); - if (iter.Init()) { - while (iter.Advance()) { - if (iter.IsWord()) - words.push_back(iter.GetString()); - } - } - return words; -} - // static int HistoryQuickProvider::CalculateRelevance(int raw_score, AutocompleteInput::Type input_type, @@ -196,3 +168,35 @@ int HistoryQuickProvider::CalculateRelevance(int raw_score, return 900 + static_cast<int>(match_number); } } + +// static +ACMatchClassifications HistoryQuickProvider::SpansFromTermMatch( + const history::TermMatches& matches, + size_t text_length, + size_t adjust) { + ACMatchClassifications spans; + if (matches.empty()) { + if (text_length) + spans.push_back(ACMatchClassification(0, ACMatchClassification::DIM)); + return spans; + } + if (matches[0].offset > adjust) + spans.push_back(ACMatchClassification(0, ACMatchClassification::NONE)); + size_t match_count = matches.size(); + for (size_t i = 0; i < match_count;) { + size_t offset = matches[i].offset - adjust; + spans.push_back(ACMatchClassification(offset, + ACMatchClassification::MATCH)); + // Skip all adjacent matches. + do { + offset += matches[i].length; + ++i; + } while ((i < match_count) && (offset == matches[i].offset - adjust)); + if (offset < text_length) { + spans.push_back(ACMatchClassification(offset, + ACMatchClassification::NONE)); + } + } + + return spans; +} |