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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 void ShortcutsProvider::GetMatches(const AutocompleteInput& input) { | 132 void ShortcutsProvider::GetMatches(const AutocompleteInput& input) { |
133 scoped_refptr<ShortcutsBackend> backend = | 133 scoped_refptr<ShortcutsBackend> backend = |
134 ShortcutsBackendFactory::GetForProfileIfExists(profile_); | 134 ShortcutsBackendFactory::GetForProfileIfExists(profile_); |
135 if (!backend.get()) | 135 if (!backend.get()) |
136 return; | 136 return; |
137 // Get the URLs from the shortcuts database with keys that partially or | 137 // Get the URLs from the shortcuts database with keys that partially or |
138 // completely match the search term. | 138 // completely match the search term. |
139 base::string16 term_string(base::i18n::ToLower(input.text())); | 139 base::string16 term_string(base::i18n::ToLower(input.text())); |
140 DCHECK(!term_string.empty()); | 140 DCHECK(!term_string.empty()); |
141 | 141 |
142 AutocompleteInput fixed_up_input(input); | |
143 FixupUserInput(&fixed_up_input); | |
144 const GURL& input_as_gurl = URLFixerUpper::FixupURL( | 142 const GURL& input_as_gurl = URLFixerUpper::FixupURL( |
145 base::UTF16ToUTF8(input.text()), std::string()); | 143 base::UTF16ToUTF8(input.text()), std::string()); |
| 144 const base::string16 fixed_up_input(FixupUserInput(input).second); |
146 | 145 |
147 int max_relevance; | 146 int max_relevance; |
148 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( | 147 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( |
149 input.current_page_classification(), &max_relevance)) | 148 input.current_page_classification(), &max_relevance)) |
150 max_relevance = kShortcutsProviderDefaultMaxRelevance; | 149 max_relevance = kShortcutsProviderDefaultMaxRelevance; |
151 | 150 |
152 for (ShortcutsBackend::ShortcutMap::const_iterator it = | 151 for (ShortcutsBackend::ShortcutMap::const_iterator it = |
153 FindFirstMatch(term_string, backend.get()); | 152 FindFirstMatch(term_string, backend.get()); |
154 it != backend->shortcuts_map().end() && | 153 it != backend->shortcuts_map().end() && |
155 StartsWith(it->first, term_string, true); ++it) { | 154 StartsWith(it->first, term_string, true); ++it) { |
156 // Don't return shortcuts with zero relevance. | 155 // Don't return shortcuts with zero relevance. |
157 int relevance = CalculateScore(term_string, it->second, max_relevance); | 156 int relevance = CalculateScore(term_string, it->second, max_relevance); |
158 if (relevance) { | 157 if (relevance) { |
159 matches_.push_back(ShortcutToACMatch( | 158 matches_.push_back(ShortcutToACMatch(it->second, relevance, input, |
160 it->second, relevance, input, fixed_up_input, input_as_gurl)); | 159 fixed_up_input, input_as_gurl)); |
161 matches_.back().ComputeStrippedDestinationURL(profile_); | 160 matches_.back().ComputeStrippedDestinationURL(profile_); |
162 } | 161 } |
163 } | 162 } |
164 // 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 |
165 // because they are only used for deletions, and shortcuts deletes matches | 164 // because they are only used for deletions, and shortcuts deletes matches |
166 // based on the URL. | 165 // based on the URL. |
167 AutocompleteResult::DedupMatchesByDestination( | 166 AutocompleteResult::DedupMatchesByDestination( |
168 input.current_page_classification(), false, &matches_); | 167 input.current_page_classification(), false, &matches_); |
169 // Find best matches. | 168 // Find best matches. |
170 std::partial_sort(matches_.begin(), | 169 std::partial_sort(matches_.begin(), |
(...skipping 11 matching lines...) Expand all Loading... |
182 it->relevance = max_relevance; | 181 it->relevance = max_relevance; |
183 if (max_relevance > 1) | 182 if (max_relevance > 1) |
184 --max_relevance; | 183 --max_relevance; |
185 } | 184 } |
186 } | 185 } |
187 | 186 |
188 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( | 187 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( |
189 const history::ShortcutsDatabase::Shortcut& shortcut, | 188 const history::ShortcutsDatabase::Shortcut& shortcut, |
190 int relevance, | 189 int relevance, |
191 const AutocompleteInput& input, | 190 const AutocompleteInput& input, |
192 const AutocompleteInput& fixed_up_input, | 191 const base::string16& fixed_up_input_text, |
193 const GURL& input_as_gurl) { | 192 const GURL& input_as_gurl) { |
194 DCHECK(!input.text().empty()); | 193 DCHECK(!input.text().empty()); |
195 AutocompleteMatch match; | 194 AutocompleteMatch match; |
196 match.provider = this; | 195 match.provider = this; |
197 match.relevance = relevance; | 196 match.relevance = relevance; |
198 match.deletable = true; | 197 match.deletable = true; |
199 match.fill_into_edit = shortcut.match_core.fill_into_edit; | 198 match.fill_into_edit = shortcut.match_core.fill_into_edit; |
200 match.destination_url = shortcut.match_core.destination_url; | 199 match.destination_url = shortcut.match_core.destination_url; |
201 DCHECK(match.destination_url.is_valid()); | 200 DCHECK(match.destination_url.is_valid()); |
202 match.contents = shortcut.match_core.contents; | 201 match.contents = shortcut.match_core.contents; |
(...skipping 24 matching lines...) Expand all Loading... |
227 if (StartsWith(match.fill_into_edit, input.text(), false)) { | 226 if (StartsWith(match.fill_into_edit, input.text(), false)) { |
228 match.inline_autocompletion = | 227 match.inline_autocompletion = |
229 match.fill_into_edit.substr(input.text().length()); | 228 match.fill_into_edit.substr(input.text().length()); |
230 match.allowed_to_be_default_match = | 229 match.allowed_to_be_default_match = |
231 !input.prevent_inline_autocomplete() || | 230 !input.prevent_inline_autocomplete() || |
232 match.inline_autocompletion.empty(); | 231 match.inline_autocompletion.empty(); |
233 } | 232 } |
234 } else { | 233 } else { |
235 const size_t inline_autocomplete_offset = | 234 const size_t inline_autocomplete_offset = |
236 URLPrefix::GetInlineAutocompleteOffset( | 235 URLPrefix::GetInlineAutocompleteOffset( |
237 input, fixed_up_input, true, match.fill_into_edit); | 236 input.text(), fixed_up_input_text, true, match.fill_into_edit); |
238 if (inline_autocomplete_offset != base::string16::npos) { | 237 if (inline_autocomplete_offset != base::string16::npos) { |
239 match.inline_autocompletion = | 238 match.inline_autocompletion = |
240 match.fill_into_edit.substr(inline_autocomplete_offset); | 239 match.fill_into_edit.substr(inline_autocomplete_offset); |
241 match.allowed_to_be_default_match = | 240 match.allowed_to_be_default_match = |
242 !HistoryProvider::PreventInlineAutocomplete(input) || | 241 !HistoryProvider::PreventInlineAutocomplete(input) || |
243 match.inline_autocompletion.empty(); | 242 match.inline_autocompletion.empty(); |
244 } else { | 243 } else { |
245 // Also allow a user's input to be marked as default if it would be fixed | 244 // Also allow a user's input to be marked as default if it would be fixed |
246 // up to the same thing as the fill_into_edit. This handles cases like | 245 // up to the same thing as the fill_into_edit. This handles cases like |
247 // the user input containing a trailing slash absent in fill_into_edit. | 246 // the user input containing a trailing slash absent in fill_into_edit. |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. | 412 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. |
414 const double kMaxDecaySpeedDivisor = 5.0; | 413 const double kMaxDecaySpeedDivisor = 5.0; |
415 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; | 414 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; |
416 double decay_divisor = std::min(kMaxDecaySpeedDivisor, | 415 double decay_divisor = std::min(kMaxDecaySpeedDivisor, |
417 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / | 416 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / |
418 kNumUsesPerDecaySpeedDivisorIncrement); | 417 kNumUsesPerDecaySpeedDivisorIncrement); |
419 | 418 |
420 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + | 419 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + |
421 0.5); | 420 0.5); |
422 } | 421 } |
OLD | NEW |