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> |
11 | 11 |
12 #include "base/i18n/break_iterator.h" | 12 #include "base/i18n/break_iterator.h" |
13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
16 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_split.h" |
18 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
20 #include "base/time/time.h" | 21 #include "base/time/time.h" |
21 #include "chrome/browser/autocomplete/history_provider.h" | 22 #include "chrome/browser/autocomplete/history_provider.h" |
22 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h" | 23 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h" |
23 #include "chrome/browser/history/history_service_factory.h" | 24 #include "chrome/browser/history/history_service_factory.h" |
24 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/browser/search_engines/template_url_service_factory.h" | 26 #include "chrome/browser/search_engines/template_url_service_factory.h" |
26 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
27 #include "chrome/common/url_constants.h" | 28 #include "chrome/common/url_constants.h" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 base::string16 term_string(base::i18n::ToLower(input.text())); | 137 base::string16 term_string(base::i18n::ToLower(input.text())); |
137 DCHECK(!term_string.empty()); | 138 DCHECK(!term_string.empty()); |
138 | 139 |
139 int max_relevance; | 140 int max_relevance; |
140 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( | 141 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( |
141 input.current_page_classification(), &max_relevance)) | 142 input.current_page_classification(), &max_relevance)) |
142 max_relevance = kShortcutsProviderDefaultMaxRelevance; | 143 max_relevance = kShortcutsProviderDefaultMaxRelevance; |
143 TemplateURLService* template_url_service = | 144 TemplateURLService* template_url_service = |
144 TemplateURLServiceFactory::GetForProfile(profile_); | 145 TemplateURLServiceFactory::GetForProfile(profile_); |
145 const base::string16 fixed_up_input(FixupUserInput(input).second); | 146 const base::string16 fixed_up_input(FixupUserInput(input).second); |
| 147 std::vector<base::string16> words; |
| 148 base::SplitString(input.text(), ' ', &words); |
146 for (ShortcutsBackend::ShortcutMap::const_iterator it = | 149 for (ShortcutsBackend::ShortcutMap::const_iterator it = |
147 FindFirstMatch(term_string, backend.get()); | 150 FindFirstMatch(term_string, backend.get()); |
148 it != backend->shortcuts_map().end() && | 151 it != backend->shortcuts_map().end() && |
149 StartsWith(it->first, term_string, true); ++it) { | 152 StartsWith(it->first, term_string, true); ++it) { |
150 // Don't return shortcuts with zero relevance. | 153 // Don't return shortcuts with zero relevance. |
151 int relevance = CalculateScore(term_string, it->second, max_relevance); | 154 int relevance = CalculateScore(term_string, it->second, max_relevance); |
152 if (relevance) { | 155 if (relevance) { |
153 matches_.push_back(ShortcutToACMatch(it->second, relevance, input, | 156 matches_.push_back(ShortcutToACMatch(it->second, relevance, input, |
154 fixed_up_input)); | 157 fixed_up_input, words)); |
155 matches_.back().ComputeStrippedDestinationURL(template_url_service); | 158 matches_.back().ComputeStrippedDestinationURL(words, |
| 159 template_url_service); |
156 } | 160 } |
157 } | 161 } |
158 // Remove duplicates. Duplicates don't need to be preserved in the matches | 162 // Remove duplicates. Duplicates don't need to be preserved in the matches |
159 // because they are only used for deletions, and shortcuts deletes matches | 163 // because they are only used for deletions, and shortcuts deletes matches |
160 // based on the URL. | 164 // based on the URL. |
161 AutocompleteResult::DedupMatchesByDestination( | 165 AutocompleteResult::DedupMatchesByDestination( |
162 input.current_page_classification(), false, &matches_); | 166 input.current_page_classification(), false, &matches_); |
163 // Find best matches. | 167 // Find best matches. |
164 std::partial_sort(matches_.begin(), | 168 std::partial_sort(matches_.begin(), |
165 matches_.begin() + | 169 matches_.begin() + |
(...skipping 10 matching lines...) Expand all Loading... |
176 it->relevance = max_relevance; | 180 it->relevance = max_relevance; |
177 if (max_relevance > 1) | 181 if (max_relevance > 1) |
178 --max_relevance; | 182 --max_relevance; |
179 } | 183 } |
180 } | 184 } |
181 | 185 |
182 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( | 186 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( |
183 const ShortcutsDatabase::Shortcut& shortcut, | 187 const ShortcutsDatabase::Shortcut& shortcut, |
184 int relevance, | 188 int relevance, |
185 const AutocompleteInput& input, | 189 const AutocompleteInput& input, |
186 const base::string16& fixed_up_input_text) { | 190 const base::string16& fixed_up_input_text, |
| 191 const std::vector<base::string16>& words) { |
187 DCHECK(!input.text().empty()); | 192 DCHECK(!input.text().empty()); |
188 AutocompleteMatch match; | 193 AutocompleteMatch match; |
189 match.provider = this; | 194 match.provider = this; |
190 match.relevance = relevance; | 195 match.relevance = relevance; |
191 match.deletable = true; | 196 match.deletable = true; |
192 match.fill_into_edit = shortcut.match_core.fill_into_edit; | 197 match.fill_into_edit = shortcut.match_core.fill_into_edit; |
193 match.destination_url = shortcut.match_core.destination_url; | 198 match.destination_url = shortcut.match_core.destination_url; |
194 DCHECK(match.destination_url.is_valid()); | 199 DCHECK(match.destination_url.is_valid()); |
195 match.contents = shortcut.match_core.contents; | 200 match.contents = shortcut.match_core.contents; |
196 match.contents_class = AutocompleteMatch::ClassificationsFromString( | 201 match.contents_class = AutocompleteMatch::ClassificationsFromString( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 input.text(), fixed_up_input_text, true, match.fill_into_edit); | 234 input.text(), fixed_up_input_text, true, match.fill_into_edit); |
230 if (inline_autocomplete_offset != base::string16::npos) { | 235 if (inline_autocomplete_offset != base::string16::npos) { |
231 match.inline_autocompletion = | 236 match.inline_autocompletion = |
232 match.fill_into_edit.substr(inline_autocomplete_offset); | 237 match.fill_into_edit.substr(inline_autocomplete_offset); |
233 match.allowed_to_be_default_match = | 238 match.allowed_to_be_default_match = |
234 !HistoryProvider::PreventInlineAutocomplete(input) || | 239 !HistoryProvider::PreventInlineAutocomplete(input) || |
235 match.inline_autocompletion.empty(); | 240 match.inline_autocompletion.empty(); |
236 } | 241 } |
237 } | 242 } |
238 match.EnsureUWYTIsAllowedToBeDefault( | 243 match.EnsureUWYTIsAllowedToBeDefault( |
239 input.canonicalized_url(), | 244 input.canonicalized_url(), words, |
240 TemplateURLServiceFactory::GetForProfile(profile_)); | 245 TemplateURLServiceFactory::GetForProfile(profile_)); |
241 | 246 |
242 // Try to mark pieces of the contents and description as matches if they | 247 // Try to mark pieces of the contents and description as matches if they |
243 // appear in |input.text()|. | 248 // appear in |input.text()|. |
244 const base::string16 term_string = base::i18n::ToLower(input.text()); | 249 const base::string16 term_string = base::i18n::ToLower(input.text()); |
245 WordMap terms_map(CreateWordMapForString(term_string)); | 250 WordMap terms_map(CreateWordMapForString(term_string)); |
246 if (!terms_map.empty()) { | 251 if (!terms_map.empty()) { |
247 match.contents_class = ClassifyAllMatchesInString(term_string, terms_map, | 252 match.contents_class = ClassifyAllMatchesInString(term_string, terms_map, |
248 match.contents, match.contents_class); | 253 match.contents, match.contents_class); |
249 match.description_class = ClassifyAllMatchesInString(term_string, terms_map, | 254 match.description_class = ClassifyAllMatchesInString(term_string, terms_map, |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. | 406 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. |
402 const double kMaxDecaySpeedDivisor = 5.0; | 407 const double kMaxDecaySpeedDivisor = 5.0; |
403 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; | 408 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; |
404 double decay_divisor = std::min(kMaxDecaySpeedDivisor, | 409 double decay_divisor = std::min(kMaxDecaySpeedDivisor, |
405 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / | 410 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / |
406 kNumUsesPerDecaySpeedDivisorIncrement); | 411 kNumUsesPerDecaySpeedDivisorIncrement); |
407 | 412 |
408 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + | 413 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + |
409 0.5); | 414 0.5); |
410 } | 415 } |
OLD | NEW |