| 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 "components/omnibox/shortcuts_provider.h" | 5 #include "components/omnibox/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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 129 |
| 130 int max_relevance; | 130 int max_relevance; |
| 131 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( | 131 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( |
| 132 input.current_page_classification(), &max_relevance)) | 132 input.current_page_classification(), &max_relevance)) |
| 133 max_relevance = kShortcutsProviderDefaultMaxRelevance; | 133 max_relevance = kShortcutsProviderDefaultMaxRelevance; |
| 134 TemplateURLService* template_url_service = client_->GetTemplateURLService(); | 134 TemplateURLService* template_url_service = client_->GetTemplateURLService(); |
| 135 const base::string16 fixed_up_input(FixupUserInput(input).second); | 135 const base::string16 fixed_up_input(FixupUserInput(input).second); |
| 136 for (ShortcutsBackend::ShortcutMap::const_iterator it = | 136 for (ShortcutsBackend::ShortcutMap::const_iterator it = |
| 137 FindFirstMatch(term_string, backend.get()); | 137 FindFirstMatch(term_string, backend.get()); |
| 138 it != backend->shortcuts_map().end() && | 138 it != backend->shortcuts_map().end() && |
| 139 base::StartsWith(it->first, term_string, true); | 139 base::StartsWith(it->first, term_string, |
| 140 base::CompareCase::SENSITIVE); |
| 140 ++it) { | 141 ++it) { |
| 141 // Don't return shortcuts with zero relevance. | 142 // Don't return shortcuts with zero relevance. |
| 142 int relevance = CalculateScore(term_string, it->second, max_relevance); | 143 int relevance = CalculateScore(term_string, it->second, max_relevance); |
| 143 if (relevance) { | 144 if (relevance) { |
| 144 matches_.push_back( | 145 matches_.push_back( |
| 145 ShortcutToACMatch(it->second, relevance, input, fixed_up_input)); | 146 ShortcutToACMatch(it->second, relevance, input, fixed_up_input)); |
| 146 matches_.back().ComputeStrippedDestinationURL( | 147 matches_.back().ComputeStrippedDestinationURL( |
| 147 input, client_->GetAcceptLanguages(), template_url_service); | 148 input, client_->GetAcceptLanguages(), template_url_service); |
| 148 } | 149 } |
| 149 } | 150 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 // Set |inline_autocompletion| and |allowed_to_be_default_match| if possible. | 211 // Set |inline_autocompletion| and |allowed_to_be_default_match| if possible. |
| 211 // If the match is a search query this is easy: simply check whether the | 212 // If the match is a search query this is easy: simply check whether the |
| 212 // user text is a prefix of the query. If the match is a navigation, we | 213 // user text is a prefix of the query. If the match is a navigation, we |
| 213 // assume the fill_into_edit looks something like a URL, so we use | 214 // assume the fill_into_edit looks something like a URL, so we use |
| 214 // URLPrefix::GetInlineAutocompleteOffset() to try and strip off any prefixes | 215 // URLPrefix::GetInlineAutocompleteOffset() to try and strip off any prefixes |
| 215 // that the user might not think would change the meaning, but would | 216 // that the user might not think would change the meaning, but would |
| 216 // otherwise prevent inline autocompletion. This allows, for example, the | 217 // otherwise prevent inline autocompletion. This allows, for example, the |
| 217 // input of "foo.c" to autocomplete to "foo.com" for a fill_into_edit of | 218 // input of "foo.c" to autocomplete to "foo.com" for a fill_into_edit of |
| 218 // "http://foo.com". | 219 // "http://foo.com". |
| 219 if (AutocompleteMatch::IsSearchType(match.type)) { | 220 if (AutocompleteMatch::IsSearchType(match.type)) { |
| 220 if (base::StartsWith(match.fill_into_edit, input.text(), false)) { | 221 if (base::StartsWith(base::i18n::ToLower(match.fill_into_edit), |
| 222 base::i18n::ToLower(input.text()), |
| 223 base::CompareCase::SENSITIVE)) { |
| 221 match.inline_autocompletion = | 224 match.inline_autocompletion = |
| 222 match.fill_into_edit.substr(input.text().length()); | 225 match.fill_into_edit.substr(input.text().length()); |
| 223 match.allowed_to_be_default_match = | 226 match.allowed_to_be_default_match = |
| 224 !input.prevent_inline_autocomplete() || | 227 !input.prevent_inline_autocomplete() || |
| 225 match.inline_autocompletion.empty(); | 228 match.inline_autocompletion.empty(); |
| 226 } | 229 } |
| 227 } else { | 230 } else { |
| 228 const size_t inline_autocomplete_offset = | 231 const size_t inline_autocomplete_offset = |
| 229 URLPrefix::GetInlineAutocompleteOffset( | 232 URLPrefix::GetInlineAutocompleteOffset( |
| 230 input.text(), fixed_up_input_text, true, match.fill_into_edit); | 233 input.text(), fixed_up_input_text, true, match.fill_into_edit); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 // if |text| is empty assures we'll return the (correct) empty vector rather | 301 // if |text| is empty assures we'll return the (correct) empty vector rather |
| 299 // than a vector with a single (0, NONE) match. | 302 // than a vector with a single (0, NONE) match. |
| 300 if (text.empty()) | 303 if (text.empty()) |
| 301 return original_class; | 304 return original_class; |
| 302 | 305 |
| 303 // First check whether |text| begins with |find_text| and mark that whole | 306 // First check whether |text| begins with |find_text| and mark that whole |
| 304 // section as a match if so. | 307 // section as a match if so. |
| 305 base::string16 text_lowercase(base::i18n::ToLower(text)); | 308 base::string16 text_lowercase(base::i18n::ToLower(text)); |
| 306 ACMatchClassifications match_class; | 309 ACMatchClassifications match_class; |
| 307 size_t last_position = 0; | 310 size_t last_position = 0; |
| 308 if (base::StartsWith(text_lowercase, find_text, true)) { | 311 if (base::StartsWith(text_lowercase, find_text, |
| 312 base::CompareCase::SENSITIVE)) { |
| 309 match_class.push_back( | 313 match_class.push_back( |
| 310 ACMatchClassification(0, ACMatchClassification::MATCH)); | 314 ACMatchClassification(0, ACMatchClassification::MATCH)); |
| 311 last_position = find_text.length(); | 315 last_position = find_text.length(); |
| 312 // If |text_lowercase| is actually equal to |find_text|, we don't need to | 316 // If |text_lowercase| is actually equal to |find_text|, we don't need to |
| 313 // (and in fact shouldn't) put a trailing NONE classification after the end | 317 // (and in fact shouldn't) put a trailing NONE classification after the end |
| 314 // of the string. | 318 // of the string. |
| 315 if (last_position < text_lowercase.length()) { | 319 if (last_position < text_lowercase.length()) { |
| 316 match_class.push_back( | 320 match_class.push_back( |
| 317 ACMatchClassification(last_position, ACMatchClassification::NONE)); | 321 ACMatchClassification(last_position, ACMatchClassification::NONE)); |
| 318 } | 322 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 | 366 |
| 363 ShortcutsBackend::ShortcutMap::const_iterator ShortcutsProvider::FindFirstMatch( | 367 ShortcutsBackend::ShortcutMap::const_iterator ShortcutsProvider::FindFirstMatch( |
| 364 const base::string16& keyword, | 368 const base::string16& keyword, |
| 365 ShortcutsBackend* backend) { | 369 ShortcutsBackend* backend) { |
| 366 DCHECK(backend); | 370 DCHECK(backend); |
| 367 ShortcutsBackend::ShortcutMap::const_iterator it = | 371 ShortcutsBackend::ShortcutMap::const_iterator it = |
| 368 backend->shortcuts_map().lower_bound(keyword); | 372 backend->shortcuts_map().lower_bound(keyword); |
| 369 // Lower bound not necessarily matches the keyword, check for item pointed by | 373 // Lower bound not necessarily matches the keyword, check for item pointed by |
| 370 // the lower bound iterator to at least start with keyword. | 374 // the lower bound iterator to at least start with keyword. |
| 371 return ((it == backend->shortcuts_map().end()) || | 375 return ((it == backend->shortcuts_map().end()) || |
| 372 base::StartsWith(it->first, keyword, true)) | 376 base::StartsWith(it->first, keyword, base::CompareCase::SENSITIVE)) |
| 373 ? it | 377 ? it |
| 374 : backend->shortcuts_map().end(); | 378 : backend->shortcuts_map().end(); |
| 375 } | 379 } |
| 376 | 380 |
| 377 int ShortcutsProvider::CalculateScore( | 381 int ShortcutsProvider::CalculateScore( |
| 378 const base::string16& terms, | 382 const base::string16& terms, |
| 379 const ShortcutsDatabase::Shortcut& shortcut, | 383 const ShortcutsDatabase::Shortcut& shortcut, |
| 380 int max_relevance) { | 384 int max_relevance) { |
| 381 DCHECK(!terms.empty()); | 385 DCHECK(!terms.empty()); |
| 382 DCHECK_LE(terms.length(), shortcut.text.length()); | 386 DCHECK_LE(terms.length(), shortcut.text.length()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 405 const double kMaxDecaySpeedDivisor = 5.0; | 409 const double kMaxDecaySpeedDivisor = 5.0; |
| 406 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; | 410 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; |
| 407 double decay_divisor = std::min( | 411 double decay_divisor = std::min( |
| 408 kMaxDecaySpeedDivisor, | 412 kMaxDecaySpeedDivisor, |
| 409 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / | 413 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / |
| 410 kNumUsesPerDecaySpeedDivisorIncrement); | 414 kNumUsesPerDecaySpeedDivisorIncrement); |
| 411 | 415 |
| 412 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + | 416 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + |
| 413 0.5); | 417 0.5); |
| 414 } | 418 } |
| OLD | NEW |