| 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/browser/shortcuts_provider.h" | 5 #include "components/omnibox/browser/shortcuts_provider.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <cmath> | 10 #include <cmath> |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 const GURL url_; | 47 const GURL url_; |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 } // namespace | 50 } // namespace |
| 51 | 51 |
| 52 const int ShortcutsProvider::kShortcutsProviderDefaultMaxRelevance = 1199; | 52 const int ShortcutsProvider::kShortcutsProviderDefaultMaxRelevance = 1199; |
| 53 | 53 |
| 54 ShortcutsProvider::ShortcutsProvider(AutocompleteProviderClient* client) | 54 ShortcutsProvider::ShortcutsProvider(AutocompleteProviderClient* client) |
| 55 : AutocompleteProvider(AutocompleteProvider::TYPE_SHORTCUTS), | 55 : AutocompleteProvider(AutocompleteProvider::TYPE_SHORTCUTS), |
| 56 client_(client), | 56 client_(client), |
| 57 languages_(client_->GetAcceptLanguages()), | |
| 58 initialized_(false) { | 57 initialized_(false) { |
| 59 scoped_refptr<ShortcutsBackend> backend = client_->GetShortcutsBackend(); | 58 scoped_refptr<ShortcutsBackend> backend = client_->GetShortcutsBackend(); |
| 60 if (backend.get()) { | 59 if (backend.get()) { |
| 61 backend->AddObserver(this); | 60 backend->AddObserver(this); |
| 62 if (backend->initialized()) | 61 if (backend->initialized()) |
| 63 initialized_ = true; | 62 initialized_ = true; |
| 64 } | 63 } |
| 65 } | 64 } |
| 66 | 65 |
| 67 void ShortcutsProvider::Start(const AutocompleteInput& input, | 66 void ShortcutsProvider::Start(const AutocompleteInput& input, |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 FindFirstMatch(term_string, backend.get()); | 141 FindFirstMatch(term_string, backend.get()); |
| 143 it != backend->shortcuts_map().end() && | 142 it != backend->shortcuts_map().end() && |
| 144 base::StartsWith(it->first, term_string, | 143 base::StartsWith(it->first, term_string, |
| 145 base::CompareCase::SENSITIVE); | 144 base::CompareCase::SENSITIVE); |
| 146 ++it) { | 145 ++it) { |
| 147 // Don't return shortcuts with zero relevance. | 146 // Don't return shortcuts with zero relevance. |
| 148 int relevance = CalculateScore(term_string, it->second, max_relevance); | 147 int relevance = CalculateScore(term_string, it->second, max_relevance); |
| 149 if (relevance) { | 148 if (relevance) { |
| 150 matches_.push_back( | 149 matches_.push_back( |
| 151 ShortcutToACMatch(it->second, relevance, input, fixed_up_input)); | 150 ShortcutToACMatch(it->second, relevance, input, fixed_up_input)); |
| 152 matches_.back().ComputeStrippedDestinationURL( | 151 matches_.back().ComputeStrippedDestinationURL(input, |
| 153 input, client_->GetAcceptLanguages(), template_url_service); | 152 template_url_service); |
| 154 } | 153 } |
| 155 } | 154 } |
| 156 // Remove duplicates. This is important because it's common to have multiple | 155 // Remove duplicates. This is important because it's common to have multiple |
| 157 // shortcuts pointing to the same URL, e.g., ma, mai, and mail all pointing | 156 // shortcuts pointing to the same URL, e.g., ma, mai, and mail all pointing |
| 158 // to mail.google.com, so typing "m" will return them all. If we then simply | 157 // to mail.google.com, so typing "m" will return them all. If we then simply |
| 159 // clamp to kMaxMatches and let the AutocompleteResult take care of | 158 // clamp to kMaxMatches and let the AutocompleteResult take care of |
| 160 // collapsing the duplicates, we'll effectively only be returning one match, | 159 // collapsing the duplicates, we'll effectively only be returning one match, |
| 161 // instead of several possibilities. | 160 // instead of several possibilities. |
| 162 // | 161 // |
| 163 // Note that while removing duplicates, we don't populate a match's | 162 // Note that while removing duplicates, we don't populate a match's |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 input.text(), fixed_up_input_text, true, match.fill_into_edit); | 239 input.text(), fixed_up_input_text, true, match.fill_into_edit); |
| 241 if (inline_autocomplete_offset != base::string16::npos) { | 240 if (inline_autocomplete_offset != base::string16::npos) { |
| 242 match.inline_autocompletion = | 241 match.inline_autocompletion = |
| 243 match.fill_into_edit.substr(inline_autocomplete_offset); | 242 match.fill_into_edit.substr(inline_autocomplete_offset); |
| 244 match.allowed_to_be_default_match = | 243 match.allowed_to_be_default_match = |
| 245 !HistoryProvider::PreventInlineAutocomplete(input) || | 244 !HistoryProvider::PreventInlineAutocomplete(input) || |
| 246 match.inline_autocompletion.empty(); | 245 match.inline_autocompletion.empty(); |
| 247 } | 246 } |
| 248 } | 247 } |
| 249 match.EnsureUWYTIsAllowedToBeDefault(input, | 248 match.EnsureUWYTIsAllowedToBeDefault(input, |
| 250 client_->GetAcceptLanguages(), | |
| 251 client_->GetTemplateURLService()); | 249 client_->GetTemplateURLService()); |
| 252 | 250 |
| 253 // Try to mark pieces of the contents and description as matches if they | 251 // Try to mark pieces of the contents and description as matches if they |
| 254 // appear in |input.text()|. | 252 // appear in |input.text()|. |
| 255 const base::string16 term_string = base::i18n::ToLower(input.text()); | 253 const base::string16 term_string = base::i18n::ToLower(input.text()); |
| 256 WordMap terms_map(CreateWordMapForString(term_string)); | 254 WordMap terms_map(CreateWordMapForString(term_string)); |
| 257 if (!terms_map.empty()) { | 255 if (!terms_map.empty()) { |
| 258 match.contents_class = ClassifyAllMatchesInString( | 256 match.contents_class = ClassifyAllMatchesInString( |
| 259 term_string, terms_map, match.contents, match.contents_class); | 257 term_string, terms_map, match.contents, match.contents_class); |
| 260 match.description_class = ClassifyAllMatchesInString( | 258 match.description_class = ClassifyAllMatchesInString( |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 const double kMaxDecaySpeedDivisor = 5.0; | 414 const double kMaxDecaySpeedDivisor = 5.0; |
| 417 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; | 415 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; |
| 418 double decay_divisor = std::min( | 416 double decay_divisor = std::min( |
| 419 kMaxDecaySpeedDivisor, | 417 kMaxDecaySpeedDivisor, |
| 420 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / | 418 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / |
| 421 kNumUsesPerDecaySpeedDivisorIncrement); | 419 kNumUsesPerDecaySpeedDivisorIncrement); |
| 422 | 420 |
| 423 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + | 421 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + |
| 424 0.5); | 422 0.5); |
| 425 } | 423 } |
| OLD | NEW |