Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/autocomplete/shortcuts_provider.h" | 5 #include "chrome/browser/autocomplete/shortcuts_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 void ShortcutsProvider::GetMatches(const AutocompleteInput& input) { | 133 void ShortcutsProvider::GetMatches(const AutocompleteInput& input) { |
| 134 scoped_refptr<ShortcutsBackend> backend = | 134 scoped_refptr<ShortcutsBackend> backend = |
| 135 ShortcutsBackendFactory::GetForProfileIfExists(profile_); | 135 ShortcutsBackendFactory::GetForProfileIfExists(profile_); |
| 136 if (!backend.get()) | 136 if (!backend.get()) |
| 137 return; | 137 return; |
| 138 // Get the URLs from the shortcuts database with keys that partially or | 138 // Get the URLs from the shortcuts database with keys that partially or |
| 139 // completely match the search term. | 139 // completely match the search term. |
| 140 base::string16 term_string(base::i18n::ToLower(input.text())); | 140 base::string16 term_string(base::i18n::ToLower(input.text())); |
| 141 DCHECK(!term_string.empty()); | 141 DCHECK(!term_string.empty()); |
| 142 | 142 |
| 143 const GURL& input_as_gurl = | 143 const GURL& stripped_canonical_input_url = |
| 144 url_fixer::FixupURL(base::UTF16ToUTF8(input.text()), std::string()); | 144 AutocompleteMatch::GURLToStrippedGURL( |
| 145 input.canonicalized_url(), profile_, base::string16()); | |
| 145 const base::string16 fixed_up_input(FixupUserInput(input).second); | 146 const base::string16 fixed_up_input(FixupUserInput(input).second); |
| 146 | 147 |
| 147 int max_relevance; | 148 int max_relevance; |
| 148 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( | 149 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( |
| 149 input.current_page_classification(), &max_relevance)) | 150 input.current_page_classification(), &max_relevance)) |
| 150 max_relevance = kShortcutsProviderDefaultMaxRelevance; | 151 max_relevance = kShortcutsProviderDefaultMaxRelevance; |
| 151 | 152 |
| 152 for (ShortcutsBackend::ShortcutMap::const_iterator it = | 153 for (ShortcutsBackend::ShortcutMap::const_iterator it = |
| 153 FindFirstMatch(term_string, backend.get()); | 154 FindFirstMatch(term_string, backend.get()); |
| 154 it != backend->shortcuts_map().end() && | 155 it != backend->shortcuts_map().end() && |
| 155 StartsWith(it->first, term_string, true); ++it) { | 156 StartsWith(it->first, term_string, true); ++it) { |
| 156 // Don't return shortcuts with zero relevance. | 157 // Don't return shortcuts with zero relevance. |
| 157 int relevance = CalculateScore(term_string, it->second, max_relevance); | 158 int relevance = CalculateScore(term_string, it->second, max_relevance); |
| 158 if (relevance) { | 159 if (relevance) { |
| 159 matches_.push_back(ShortcutToACMatch(it->second, relevance, input, | 160 matches_.push_back(ShortcutToACMatch(it->second, relevance, input, |
| 160 fixed_up_input, input_as_gurl)); | 161 fixed_up_input, |
| 162 stripped_canonical_input_url)); | |
| 161 matches_.back().ComputeStrippedDestinationURL(profile_); | 163 matches_.back().ComputeStrippedDestinationURL(profile_); |
| 162 } | 164 } |
| 163 } | 165 } |
| 164 // Remove duplicates. Duplicates don't need to be preserved in the matches | 166 // Remove duplicates. Duplicates don't need to be preserved in the matches |
| 165 // because they are only used for deletions, and shortcuts deletes matches | 167 // because they are only used for deletions, and shortcuts deletes matches |
| 166 // based on the URL. | 168 // based on the URL. |
| 167 AutocompleteResult::DedupMatchesByDestination( | 169 AutocompleteResult::DedupMatchesByDestination( |
| 168 input.current_page_classification(), false, &matches_); | 170 input.current_page_classification(), false, &matches_); |
| 169 // Find best matches. | 171 // Find best matches. |
| 170 std::partial_sort(matches_.begin(), | 172 std::partial_sort(matches_.begin(), |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 183 if (max_relevance > 1) | 185 if (max_relevance > 1) |
| 184 --max_relevance; | 186 --max_relevance; |
| 185 } | 187 } |
| 186 } | 188 } |
| 187 | 189 |
| 188 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( | 190 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( |
| 189 const history::ShortcutsDatabase::Shortcut& shortcut, | 191 const history::ShortcutsDatabase::Shortcut& shortcut, |
| 190 int relevance, | 192 int relevance, |
| 191 const AutocompleteInput& input, | 193 const AutocompleteInput& input, |
| 192 const base::string16& fixed_up_input_text, | 194 const base::string16& fixed_up_input_text, |
| 193 const GURL& input_as_gurl) { | 195 const GURL& stripped_canonical_input_url) { |
| 194 DCHECK(!input.text().empty()); | 196 DCHECK(!input.text().empty()); |
| 195 AutocompleteMatch match; | 197 AutocompleteMatch match; |
| 196 match.provider = this; | 198 match.provider = this; |
| 197 match.relevance = relevance; | 199 match.relevance = relevance; |
| 198 match.deletable = true; | 200 match.deletable = true; |
| 199 match.fill_into_edit = shortcut.match_core.fill_into_edit; | 201 match.fill_into_edit = shortcut.match_core.fill_into_edit; |
| 200 match.destination_url = shortcut.match_core.destination_url; | 202 match.destination_url = shortcut.match_core.destination_url; |
| 201 DCHECK(match.destination_url.is_valid()); | 203 DCHECK(match.destination_url.is_valid()); |
| 202 match.contents = shortcut.match_core.contents; | 204 match.contents = shortcut.match_core.contents; |
| 203 match.contents_class = AutocompleteMatch::ClassificationsFromString( | 205 match.contents_class = AutocompleteMatch::ClassificationsFromString( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 URLPrefix::GetInlineAutocompleteOffset( | 238 URLPrefix::GetInlineAutocompleteOffset( |
| 237 input.text(), fixed_up_input_text, true, match.fill_into_edit); | 239 input.text(), fixed_up_input_text, true, match.fill_into_edit); |
| 238 if (inline_autocomplete_offset != base::string16::npos) { | 240 if (inline_autocomplete_offset != base::string16::npos) { |
| 239 match.inline_autocompletion = | 241 match.inline_autocompletion = |
| 240 match.fill_into_edit.substr(inline_autocomplete_offset); | 242 match.fill_into_edit.substr(inline_autocomplete_offset); |
| 241 match.allowed_to_be_default_match = | 243 match.allowed_to_be_default_match = |
| 242 !HistoryProvider::PreventInlineAutocomplete(input) || | 244 !HistoryProvider::PreventInlineAutocomplete(input) || |
| 243 match.inline_autocompletion.empty(); | 245 match.inline_autocompletion.empty(); |
| 244 } else { | 246 } else { |
| 245 // Also allow a user's input to be marked as default if it would be fixed | 247 // Also allow a user's input to be marked as default if it would be fixed |
| 246 // up to the same thing as the fill_into_edit. This handles cases like | 248 // up to the same thing as the fill_into_edit (or something that would be |
| 249 // dupped against the fill_into_edit match). This handles cases like | |
| 247 // the user input containing a trailing slash absent in fill_into_edit. | 250 // the user input containing a trailing slash absent in fill_into_edit. |
| 248 match.allowed_to_be_default_match = | 251 match.allowed_to_be_default_match = |
| 249 (input_as_gurl == | 252 (stripped_canonical_input_url == |
| 250 url_fixer::FixupURL(base::UTF16ToUTF8(match.fill_into_edit), | 253 AutocompleteMatch::GURLToStrippedGURL( |
| 251 std::string())); | 254 url_fixer::FixupURL(base::UTF16ToUTF8(match.fill_into_edit), |
|
Peter Kasting
2014/06/30 23:02:58
Why are we computing the stripped fixed-up fill_in
Mark P
2014/06/30 23:27:36
Because when I originally wrote this code, you ins
Peter Kasting
2014/06/30 23:31:25
I think it doesn't make sense to be inconsistent.
Mark P
2014/07/01 17:45:33
Yes, the code looks very similar. I can refactor
| |
| 255 std::string()), | |
| 256 profile_, base::string16())); | |
| 252 } | 257 } |
| 253 } | 258 } |
| 254 | 259 |
| 255 // Try to mark pieces of the contents and description as matches if they | 260 // Try to mark pieces of the contents and description as matches if they |
| 256 // appear in |input.text()|. | 261 // appear in |input.text()|. |
| 257 const base::string16 term_string = base::i18n::ToLower(input.text()); | 262 const base::string16 term_string = base::i18n::ToLower(input.text()); |
| 258 WordMap terms_map(CreateWordMapForString(term_string)); | 263 WordMap terms_map(CreateWordMapForString(term_string)); |
| 259 if (!terms_map.empty()) { | 264 if (!terms_map.empty()) { |
| 260 match.contents_class = ClassifyAllMatchesInString(term_string, terms_map, | 265 match.contents_class = ClassifyAllMatchesInString(term_string, terms_map, |
| 261 match.contents, match.contents_class); | 266 match.contents, match.contents_class); |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 414 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. | 419 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. |
| 415 const double kMaxDecaySpeedDivisor = 5.0; | 420 const double kMaxDecaySpeedDivisor = 5.0; |
| 416 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; | 421 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; |
| 417 double decay_divisor = std::min(kMaxDecaySpeedDivisor, | 422 double decay_divisor = std::min(kMaxDecaySpeedDivisor, |
| 418 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / | 423 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / |
| 419 kNumUsesPerDecaySpeedDivisorIncrement); | 424 kNumUsesPerDecaySpeedDivisorIncrement); |
| 420 | 425 |
| 421 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + | 426 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + |
| 422 0.5); | 427 0.5); |
| 423 } | 428 } |
| OLD | NEW |