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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 int max_relevance; | 139 int max_relevance; |
140 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( | 140 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( |
141 input.current_page_classification(), &max_relevance)) | 141 input.current_page_classification(), &max_relevance)) |
142 max_relevance = kShortcutsProviderDefaultMaxRelevance; | 142 max_relevance = kShortcutsProviderDefaultMaxRelevance; |
143 TemplateURLService* template_url_service = | 143 TemplateURLService* template_url_service = |
144 TemplateURLServiceFactory::GetForProfile(profile_); | 144 TemplateURLServiceFactory::GetForProfile(profile_); |
145 const base::string16 fixed_up_input(FixupUserInput(input).second); | 145 const base::string16 fixed_up_input(FixupUserInput(input).second); |
146 for (ShortcutsBackend::ShortcutMap::const_iterator it = | 146 for (ShortcutsBackend::ShortcutMap::const_iterator it = |
147 FindFirstMatch(term_string, backend.get()); | 147 FindFirstMatch(term_string, backend.get()); |
148 it != backend->shortcuts_map().end() && | 148 it != backend->shortcuts_map().end() && |
149 StartsWith(it->first, term_string, true); ++it) { | 149 base::StartsWith(it->first, term_string, true); |
| 150 ++it) { |
150 // Don't return shortcuts with zero relevance. | 151 // Don't return shortcuts with zero relevance. |
151 int relevance = CalculateScore(term_string, it->second, max_relevance); | 152 int relevance = CalculateScore(term_string, it->second, max_relevance); |
152 if (relevance) { | 153 if (relevance) { |
153 matches_.push_back(ShortcutToACMatch(it->second, relevance, input, | 154 matches_.push_back(ShortcutToACMatch(it->second, relevance, input, |
154 fixed_up_input)); | 155 fixed_up_input)); |
155 matches_.back().ComputeStrippedDestinationURL(template_url_service); | 156 matches_.back().ComputeStrippedDestinationURL(template_url_service); |
156 } | 157 } |
157 } | 158 } |
158 // Remove duplicates. Duplicates don't need to be preserved in the matches | 159 // Remove duplicates. Duplicates don't need to be preserved in the matches |
159 // because they are only used for deletions, and shortcuts deletes matches | 160 // because they are only used for deletions, and shortcuts deletes matches |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 // Set |inline_autocompletion| and |allowed_to_be_default_match| if possible. | 210 // Set |inline_autocompletion| and |allowed_to_be_default_match| if possible. |
210 // If the match is a search query this is easy: simply check whether the | 211 // If the match is a search query this is easy: simply check whether the |
211 // user text is a prefix of the query. If the match is a navigation, we | 212 // user text is a prefix of the query. If the match is a navigation, we |
212 // assume the fill_into_edit looks something like a URL, so we use | 213 // assume the fill_into_edit looks something like a URL, so we use |
213 // URLPrefix::GetInlineAutocompleteOffset() to try and strip off any prefixes | 214 // URLPrefix::GetInlineAutocompleteOffset() to try and strip off any prefixes |
214 // that the user might not think would change the meaning, but would | 215 // that the user might not think would change the meaning, but would |
215 // otherwise prevent inline autocompletion. This allows, for example, the | 216 // otherwise prevent inline autocompletion. This allows, for example, the |
216 // input of "foo.c" to autocomplete to "foo.com" for a fill_into_edit of | 217 // input of "foo.c" to autocomplete to "foo.com" for a fill_into_edit of |
217 // "http://foo.com". | 218 // "http://foo.com". |
218 if (AutocompleteMatch::IsSearchType(match.type)) { | 219 if (AutocompleteMatch::IsSearchType(match.type)) { |
219 if (StartsWith(match.fill_into_edit, input.text(), false)) { | 220 if (base::StartsWith(match.fill_into_edit, input.text(), false)) { |
220 match.inline_autocompletion = | 221 match.inline_autocompletion = |
221 match.fill_into_edit.substr(input.text().length()); | 222 match.fill_into_edit.substr(input.text().length()); |
222 match.allowed_to_be_default_match = | 223 match.allowed_to_be_default_match = |
223 !input.prevent_inline_autocomplete() || | 224 !input.prevent_inline_autocomplete() || |
224 match.inline_autocompletion.empty(); | 225 match.inline_autocompletion.empty(); |
225 } | 226 } |
226 } else { | 227 } else { |
227 const size_t inline_autocomplete_offset = | 228 const size_t inline_autocomplete_offset = |
228 URLPrefix::GetInlineAutocompleteOffset( | 229 URLPrefix::GetInlineAutocompleteOffset( |
229 input.text(), fixed_up_input_text, true, match.fill_into_edit); | 230 input.text(), fixed_up_input_text, true, match.fill_into_edit); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 // if |text| is empty assures we'll return the (correct) empty vector rather | 298 // if |text| is empty assures we'll return the (correct) empty vector rather |
298 // than a vector with a single (0, NONE) match. | 299 // than a vector with a single (0, NONE) match. |
299 if (text.empty()) | 300 if (text.empty()) |
300 return original_class; | 301 return original_class; |
301 | 302 |
302 // First check whether |text| begins with |find_text| and mark that whole | 303 // First check whether |text| begins with |find_text| and mark that whole |
303 // section as a match if so. | 304 // section as a match if so. |
304 base::string16 text_lowercase(base::i18n::ToLower(text)); | 305 base::string16 text_lowercase(base::i18n::ToLower(text)); |
305 ACMatchClassifications match_class; | 306 ACMatchClassifications match_class; |
306 size_t last_position = 0; | 307 size_t last_position = 0; |
307 if (StartsWith(text_lowercase, find_text, true)) { | 308 if (base::StartsWith(text_lowercase, find_text, true)) { |
308 match_class.push_back( | 309 match_class.push_back( |
309 ACMatchClassification(0, ACMatchClassification::MATCH)); | 310 ACMatchClassification(0, ACMatchClassification::MATCH)); |
310 last_position = find_text.length(); | 311 last_position = find_text.length(); |
311 // If |text_lowercase| is actually equal to |find_text|, we don't need to | 312 // If |text_lowercase| is actually equal to |find_text|, we don't need to |
312 // (and in fact shouldn't) put a trailing NONE classification after the end | 313 // (and in fact shouldn't) put a trailing NONE classification after the end |
313 // of the string. | 314 // of the string. |
314 if (last_position < text_lowercase.length()) { | 315 if (last_position < text_lowercase.length()) { |
315 match_class.push_back( | 316 match_class.push_back( |
316 ACMatchClassification(last_position, ACMatchClassification::NONE)); | 317 ACMatchClassification(last_position, ACMatchClassification::NONE)); |
317 } | 318 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 | 362 |
362 ShortcutsBackend::ShortcutMap::const_iterator | 363 ShortcutsBackend::ShortcutMap::const_iterator |
363 ShortcutsProvider::FindFirstMatch(const base::string16& keyword, | 364 ShortcutsProvider::FindFirstMatch(const base::string16& keyword, |
364 ShortcutsBackend* backend) { | 365 ShortcutsBackend* backend) { |
365 DCHECK(backend); | 366 DCHECK(backend); |
366 ShortcutsBackend::ShortcutMap::const_iterator it = | 367 ShortcutsBackend::ShortcutMap::const_iterator it = |
367 backend->shortcuts_map().lower_bound(keyword); | 368 backend->shortcuts_map().lower_bound(keyword); |
368 // Lower bound not necessarily matches the keyword, check for item pointed by | 369 // Lower bound not necessarily matches the keyword, check for item pointed by |
369 // the lower bound iterator to at least start with keyword. | 370 // the lower bound iterator to at least start with keyword. |
370 return ((it == backend->shortcuts_map().end()) || | 371 return ((it == backend->shortcuts_map().end()) || |
371 StartsWith(it->first, keyword, true)) ? it : | 372 base::StartsWith(it->first, keyword, true)) |
372 backend->shortcuts_map().end(); | 373 ? it |
| 374 : backend->shortcuts_map().end(); |
373 } | 375 } |
374 | 376 |
375 int ShortcutsProvider::CalculateScore( | 377 int ShortcutsProvider::CalculateScore( |
376 const base::string16& terms, | 378 const base::string16& terms, |
377 const ShortcutsDatabase::Shortcut& shortcut, | 379 const ShortcutsDatabase::Shortcut& shortcut, |
378 int max_relevance) { | 380 int max_relevance) { |
379 DCHECK(!terms.empty()); | 381 DCHECK(!terms.empty()); |
380 DCHECK_LE(terms.length(), shortcut.text.length()); | 382 DCHECK_LE(terms.length(), shortcut.text.length()); |
381 | 383 |
382 // The initial score is based on how much of the shortcut the user has typed. | 384 // The initial score is based on how much of the shortcut the user has typed. |
(...skipping 18 matching lines...) Expand all Loading... |
401 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. | 403 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. |
402 const double kMaxDecaySpeedDivisor = 5.0; | 404 const double kMaxDecaySpeedDivisor = 5.0; |
403 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; | 405 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; |
404 double decay_divisor = std::min(kMaxDecaySpeedDivisor, | 406 double decay_divisor = std::min(kMaxDecaySpeedDivisor, |
405 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / | 407 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / |
406 kNumUsesPerDecaySpeedDivisorIncrement); | 408 kNumUsesPerDecaySpeedDivisorIncrement); |
407 | 409 |
408 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + | 410 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + |
409 0.5); | 411 0.5); |
410 } | 412 } |
OLD | NEW |