| 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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 void ShortcutsProvider::GetMatches(const AutocompleteInput& input) { | 135 void ShortcutsProvider::GetMatches(const AutocompleteInput& input) { |
| 136 scoped_refptr<ShortcutsBackend> backend = | 136 scoped_refptr<ShortcutsBackend> backend = |
| 137 ShortcutsBackendFactory::GetForProfileIfExists(profile_); | 137 ShortcutsBackendFactory::GetForProfileIfExists(profile_); |
| 138 if (!backend.get()) | 138 if (!backend.get()) |
| 139 return; | 139 return; |
| 140 // Get the URLs from the shortcuts database with keys that partially or | 140 // Get the URLs from the shortcuts database with keys that partially or |
| 141 // completely match the search term. | 141 // completely match the search term. |
| 142 base::string16 term_string(base::i18n::ToLower(input.text())); | 142 base::string16 term_string(base::i18n::ToLower(input.text())); |
| 143 DCHECK(!term_string.empty()); | 143 DCHECK(!term_string.empty()); |
| 144 | 144 |
| 145 const GURL& input_as_gurl = | |
| 146 url_fixer::FixupURL(base::UTF16ToUTF8(input.text()), std::string()); | |
| 147 const base::string16 fixed_up_input(FixupUserInput(input).second); | |
| 148 | |
| 149 int max_relevance; | 145 int max_relevance; |
| 150 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( | 146 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( |
| 151 input.current_page_classification(), &max_relevance)) | 147 input.current_page_classification(), &max_relevance)) |
| 152 max_relevance = kShortcutsProviderDefaultMaxRelevance; | 148 max_relevance = kShortcutsProviderDefaultMaxRelevance; |
| 153 | 149 TemplateURLService* template_url_service = |
| 150 TemplateURLServiceFactory::GetForProfile(profile_); |
| 151 const base::string16 fixed_up_input(FixupUserInput(input).second); |
| 154 for (ShortcutsBackend::ShortcutMap::const_iterator it = | 152 for (ShortcutsBackend::ShortcutMap::const_iterator it = |
| 155 FindFirstMatch(term_string, backend.get()); | 153 FindFirstMatch(term_string, backend.get()); |
| 156 it != backend->shortcuts_map().end() && | 154 it != backend->shortcuts_map().end() && |
| 157 StartsWith(it->first, term_string, true); ++it) { | 155 StartsWith(it->first, term_string, true); ++it) { |
| 158 // Don't return shortcuts with zero relevance. | 156 // Don't return shortcuts with zero relevance. |
| 159 int relevance = CalculateScore(term_string, it->second, max_relevance); | 157 int relevance = CalculateScore(term_string, it->second, max_relevance); |
| 160 if (relevance) { | 158 if (relevance) { |
| 161 matches_.push_back(ShortcutToACMatch(it->second, relevance, input, | 159 matches_.push_back(ShortcutToACMatch(it->second, relevance, input, |
| 162 fixed_up_input, input_as_gurl)); | 160 fixed_up_input)); |
| 163 matches_.back().ComputeStrippedDestinationURL( | 161 matches_.back().ComputeStrippedDestinationURL(template_url_service); |
| 164 TemplateURLServiceFactory::GetForProfile(profile_)); | |
| 165 } | 162 } |
| 166 } | 163 } |
| 167 // Remove duplicates. Duplicates don't need to be preserved in the matches | 164 // Remove duplicates. Duplicates don't need to be preserved in the matches |
| 168 // because they are only used for deletions, and shortcuts deletes matches | 165 // because they are only used for deletions, and shortcuts deletes matches |
| 169 // based on the URL. | 166 // based on the URL. |
| 170 AutocompleteResult::DedupMatchesByDestination( | 167 AutocompleteResult::DedupMatchesByDestination( |
| 171 input.current_page_classification(), false, &matches_); | 168 input.current_page_classification(), false, &matches_); |
| 172 // Find best matches. | 169 // Find best matches. |
| 173 std::partial_sort(matches_.begin(), | 170 std::partial_sort(matches_.begin(), |
| 174 matches_.begin() + | 171 matches_.begin() + |
| (...skipping 10 matching lines...) Expand all Loading... |
| 185 it->relevance = max_relevance; | 182 it->relevance = max_relevance; |
| 186 if (max_relevance > 1) | 183 if (max_relevance > 1) |
| 187 --max_relevance; | 184 --max_relevance; |
| 188 } | 185 } |
| 189 } | 186 } |
| 190 | 187 |
| 191 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( | 188 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( |
| 192 const history::ShortcutsDatabase::Shortcut& shortcut, | 189 const history::ShortcutsDatabase::Shortcut& shortcut, |
| 193 int relevance, | 190 int relevance, |
| 194 const AutocompleteInput& input, | 191 const AutocompleteInput& input, |
| 195 const base::string16& fixed_up_input_text, | 192 const base::string16& fixed_up_input_text) { |
| 196 const GURL& input_as_gurl) { | |
| 197 DCHECK(!input.text().empty()); | 193 DCHECK(!input.text().empty()); |
| 198 AutocompleteMatch match; | 194 AutocompleteMatch match; |
| 199 match.provider = this; | 195 match.provider = this; |
| 200 match.relevance = relevance; | 196 match.relevance = relevance; |
| 201 match.deletable = true; | 197 match.deletable = true; |
| 202 match.fill_into_edit = shortcut.match_core.fill_into_edit; | 198 match.fill_into_edit = shortcut.match_core.fill_into_edit; |
| 203 match.destination_url = shortcut.match_core.destination_url; | 199 match.destination_url = shortcut.match_core.destination_url; |
| 204 DCHECK(match.destination_url.is_valid()); | 200 DCHECK(match.destination_url.is_valid()); |
| 205 match.contents = shortcut.match_core.contents; | 201 match.contents = shortcut.match_core.contents; |
| 206 match.contents_class = AutocompleteMatch::ClassificationsFromString( | 202 match.contents_class = AutocompleteMatch::ClassificationsFromString( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 237 } else { | 233 } else { |
| 238 const size_t inline_autocomplete_offset = | 234 const size_t inline_autocomplete_offset = |
| 239 URLPrefix::GetInlineAutocompleteOffset( | 235 URLPrefix::GetInlineAutocompleteOffset( |
| 240 input.text(), fixed_up_input_text, true, match.fill_into_edit); | 236 input.text(), fixed_up_input_text, true, match.fill_into_edit); |
| 241 if (inline_autocomplete_offset != base::string16::npos) { | 237 if (inline_autocomplete_offset != base::string16::npos) { |
| 242 match.inline_autocompletion = | 238 match.inline_autocompletion = |
| 243 match.fill_into_edit.substr(inline_autocomplete_offset); | 239 match.fill_into_edit.substr(inline_autocomplete_offset); |
| 244 match.allowed_to_be_default_match = | 240 match.allowed_to_be_default_match = |
| 245 !HistoryProvider::PreventInlineAutocomplete(input) || | 241 !HistoryProvider::PreventInlineAutocomplete(input) || |
| 246 match.inline_autocompletion.empty(); | 242 match.inline_autocompletion.empty(); |
| 247 } else { | |
| 248 // Also allow a user's input to be marked as default if it would be fixed | |
| 249 // up to the same thing as the fill_into_edit. This handles cases like | |
| 250 // the user input containing a trailing slash absent in fill_into_edit. | |
| 251 match.allowed_to_be_default_match = | |
| 252 (input_as_gurl == | |
| 253 url_fixer::FixupURL(base::UTF16ToUTF8(match.fill_into_edit), | |
| 254 std::string())); | |
| 255 } | 243 } |
| 256 } | 244 } |
| 245 match.EnsureUWYTIsAllowedToBeDefault( |
| 246 input.canonicalized_url(), |
| 247 TemplateURLServiceFactory::GetForProfile(profile_)); |
| 257 | 248 |
| 258 // Try to mark pieces of the contents and description as matches if they | 249 // Try to mark pieces of the contents and description as matches if they |
| 259 // appear in |input.text()|. | 250 // appear in |input.text()|. |
| 260 const base::string16 term_string = base::i18n::ToLower(input.text()); | 251 const base::string16 term_string = base::i18n::ToLower(input.text()); |
| 261 WordMap terms_map(CreateWordMapForString(term_string)); | 252 WordMap terms_map(CreateWordMapForString(term_string)); |
| 262 if (!terms_map.empty()) { | 253 if (!terms_map.empty()) { |
| 263 match.contents_class = ClassifyAllMatchesInString(term_string, terms_map, | 254 match.contents_class = ClassifyAllMatchesInString(term_string, terms_map, |
| 264 match.contents, match.contents_class); | 255 match.contents, match.contents_class); |
| 265 match.description_class = ClassifyAllMatchesInString(term_string, terms_map, | 256 match.description_class = ClassifyAllMatchesInString(term_string, terms_map, |
| 266 match.description, match.description_class); | 257 match.description, match.description_class); |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. | 408 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. |
| 418 const double kMaxDecaySpeedDivisor = 5.0; | 409 const double kMaxDecaySpeedDivisor = 5.0; |
| 419 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; | 410 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; |
| 420 double decay_divisor = std::min(kMaxDecaySpeedDivisor, | 411 double decay_divisor = std::min(kMaxDecaySpeedDivisor, |
| 421 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / | 412 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / |
| 422 kNumUsesPerDecaySpeedDivisorIncrement); | 413 kNumUsesPerDecaySpeedDivisorIncrement); |
| 423 | 414 |
| 424 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + | 415 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + |
| 425 0.5); | 416 0.5); |
| 426 } | 417 } |
| OLD | NEW |