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