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_util.h" | 18 #include "base/strings/string_util.h" |
19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
21 #include "chrome/browser/autocomplete/autocomplete_input.h" | 21 #include "chrome/browser/autocomplete/autocomplete_input.h" |
22 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 22 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
23 #include "chrome/browser/autocomplete/autocomplete_result.h" | 23 #include "chrome/browser/autocomplete/autocomplete_result.h" |
| 24 #include "chrome/browser/autocomplete/history_provider.h" |
24 #include "chrome/browser/autocomplete/url_prefix.h" | 25 #include "chrome/browser/autocomplete/url_prefix.h" |
25 #include "chrome/browser/history/history_notifications.h" | 26 #include "chrome/browser/history/history_notifications.h" |
26 #include "chrome/browser/history/history_service.h" | 27 #include "chrome/browser/history/history_service.h" |
27 #include "chrome/browser/history/history_service_factory.h" | 28 #include "chrome/browser/history/history_service_factory.h" |
28 #include "chrome/browser/history/shortcuts_backend_factory.h" | 29 #include "chrome/browser/history/shortcuts_backend_factory.h" |
29 #include "chrome/browser/omnibox/omnibox_field_trial.h" | 30 #include "chrome/browser/omnibox/omnibox_field_trial.h" |
30 #include "chrome/browser/profiles/profile.h" | 31 #include "chrome/browser/profiles/profile.h" |
| 32 #include "chrome/common/net/url_fixer_upper.h" |
31 #include "chrome/common/pref_names.h" | 33 #include "chrome/common/pref_names.h" |
32 #include "chrome/common/url_constants.h" | 34 #include "chrome/common/url_constants.h" |
33 #include "url/url_parse.h" | 35 #include "url/url_parse.h" |
34 | 36 |
35 namespace { | 37 namespace { |
36 | 38 |
37 class DestinationURLEqualsURL { | 39 class DestinationURLEqualsURL { |
38 public: | 40 public: |
39 explicit DestinationURLEqualsURL(const GURL& url) : url_(url) {} | 41 explicit DestinationURLEqualsURL(const GURL& url) : url_(url) {} |
40 bool operator()(const AutocompleteMatch& match) const { | 42 bool operator()(const AutocompleteMatch& match) const { |
41 return match.destination_url == url_; | 43 return match.destination_url == url_; |
42 } | 44 } |
43 private: | 45 private: |
44 const GURL url_; | 46 const GURL url_; |
45 }; | 47 }; |
46 | 48 |
| 49 // Like URLPrefix::BestURLPrefix() except also handles the prefix of |
| 50 // "www.". This is needed because sometimes the string we're matching |
| 51 // against here (which comes from |fill_into_edit|) can start with |
| 52 // "www." without having a protocol at the beginning. Because "www." |
| 53 // is not on the default prefix list, we test for it explicitly here |
| 54 // and use that match if the default list didn't have a match or the |
| 55 // default list's match was shorter than it could've been. |
| 56 const URLPrefix* BestURLPrefixWithWWWCase( |
| 57 const base::string16& text, |
| 58 const base::string16& prefix_suffix) { |
| 59 CR_DEFINE_STATIC_LOCAL(URLPrefix, www_prefix, (ASCIIToUTF16("www."), 1)); |
| 60 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix(text, prefix_suffix); |
| 61 if ((best_prefix == NULL) || |
| 62 (best_prefix->num_components < www_prefix.num_components)) { |
| 63 if (URLPrefix::PrefixMatch(www_prefix, text, prefix_suffix)) |
| 64 best_prefix = &www_prefix; |
| 65 } |
| 66 return best_prefix; |
| 67 } |
| 68 |
47 } // namespace | 69 } // namespace |
48 | 70 |
49 ShortcutsProvider::ShortcutsProvider(AutocompleteProviderListener* listener, | 71 ShortcutsProvider::ShortcutsProvider(AutocompleteProviderListener* listener, |
50 Profile* profile) | 72 Profile* profile) |
51 : AutocompleteProvider(listener, profile, | 73 : AutocompleteProvider(listener, profile, |
52 AutocompleteProvider::TYPE_SHORTCUTS), | 74 AutocompleteProvider::TYPE_SHORTCUTS), |
53 languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)), | 75 languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)), |
54 initialized_(false) { | 76 initialized_(false) { |
55 scoped_refptr<history::ShortcutsBackend> backend = | 77 scoped_refptr<history::ShortcutsBackend> backend = |
56 ShortcutsBackendFactory::GetForProfile(profile_); | 78 ShortcutsBackendFactory::GetForProfile(profile_); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 void ShortcutsProvider::GetMatches(const AutocompleteInput& input) { | 149 void ShortcutsProvider::GetMatches(const AutocompleteInput& input) { |
128 scoped_refptr<history::ShortcutsBackend> backend = | 150 scoped_refptr<history::ShortcutsBackend> backend = |
129 ShortcutsBackendFactory::GetForProfileIfExists(profile_); | 151 ShortcutsBackendFactory::GetForProfileIfExists(profile_); |
130 if (!backend.get()) | 152 if (!backend.get()) |
131 return; | 153 return; |
132 // Get the URLs from the shortcuts database with keys that partially or | 154 // Get the URLs from the shortcuts database with keys that partially or |
133 // completely match the search term. | 155 // completely match the search term. |
134 base::string16 term_string(base::i18n::ToLower(input.text())); | 156 base::string16 term_string(base::i18n::ToLower(input.text())); |
135 DCHECK(!term_string.empty()); | 157 DCHECK(!term_string.empty()); |
136 | 158 |
| 159 base::string16 fixed_up_term_string(term_string); |
| 160 AutocompleteInput fixed_up_input(input); |
| 161 if (FixupUserInput(&fixed_up_input)) |
| 162 fixed_up_term_string = fixed_up_input.text(); |
| 163 |
137 int max_relevance; | 164 int max_relevance; |
138 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( | 165 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( |
139 input.current_page_classification(), &max_relevance)) | 166 input.current_page_classification(), &max_relevance)) |
140 max_relevance = AutocompleteResult::kLowestDefaultScore - 1; | 167 max_relevance = AutocompleteResult::kLowestDefaultScore - 1; |
141 | 168 |
142 for (history::ShortcutsBackend::ShortcutMap::const_iterator it = | 169 for (history::ShortcutsBackend::ShortcutMap::const_iterator it = |
143 FindFirstMatch(term_string, backend.get()); | 170 FindFirstMatch(term_string, backend.get()); |
144 it != backend->shortcuts_map().end() && | 171 it != backend->shortcuts_map().end() && |
145 StartsWith(it->first, term_string, true); ++it) { | 172 StartsWith(it->first, term_string, true); ++it) { |
146 // Don't return shortcuts with zero relevance. | 173 // Don't return shortcuts with zero relevance. |
147 int relevance = CalculateScore(term_string, it->second, max_relevance); | 174 int relevance = CalculateScore(term_string, it->second, max_relevance); |
148 if (relevance) { | 175 if (relevance) { |
149 matches_.push_back(ShortcutToACMatch( | 176 matches_.push_back(ShortcutToACMatch( |
150 it->second, relevance, term_string, | 177 it->second, relevance, term_string, fixed_up_term_string, |
151 input.prevent_inline_autocomplete())); | 178 input.prevent_inline_autocomplete())); |
152 } | 179 } |
153 } | 180 } |
154 std::partial_sort(matches_.begin(), | 181 std::partial_sort(matches_.begin(), |
155 matches_.begin() + | 182 matches_.begin() + |
156 std::min(AutocompleteProvider::kMaxMatches, matches_.size()), | 183 std::min(AutocompleteProvider::kMaxMatches, matches_.size()), |
157 matches_.end(), &AutocompleteMatch::MoreRelevant); | 184 matches_.end(), &AutocompleteMatch::MoreRelevant); |
158 if (matches_.size() > AutocompleteProvider::kMaxMatches) { | 185 if (matches_.size() > AutocompleteProvider::kMaxMatches) { |
159 matches_.erase(matches_.begin() + AutocompleteProvider::kMaxMatches, | 186 matches_.erase(matches_.begin() + AutocompleteProvider::kMaxMatches, |
160 matches_.end()); | 187 matches_.end()); |
(...skipping 15 matching lines...) Expand all Loading... |
176 it->relevance = max_relevance; | 203 it->relevance = max_relevance; |
177 if (max_relevance > 1) | 204 if (max_relevance > 1) |
178 --max_relevance; | 205 --max_relevance; |
179 } | 206 } |
180 } | 207 } |
181 | 208 |
182 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( | 209 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( |
183 const history::ShortcutsBackend::Shortcut& shortcut, | 210 const history::ShortcutsBackend::Shortcut& shortcut, |
184 int relevance, | 211 int relevance, |
185 const base::string16& term_string, | 212 const base::string16& term_string, |
186 bool prevent_inline_autocomplete) { | 213 const base::string16& fixed_up_term_string, |
| 214 const bool prevent_inline_autocomplete) { |
187 DCHECK(!term_string.empty()); | 215 DCHECK(!term_string.empty()); |
188 AutocompleteMatch match(shortcut.match_core.ToMatch()); | 216 AutocompleteMatch match(shortcut.match_core.ToMatch()); |
189 match.provider = this; | 217 match.provider = this; |
190 match.relevance = relevance; | 218 match.relevance = relevance; |
191 match.deletable = true; | 219 match.deletable = true; |
192 DCHECK(match.destination_url.is_valid()); | 220 DCHECK(match.destination_url.is_valid()); |
193 match.RecordAdditionalInfo("number of hits", shortcut.number_of_hits); | 221 match.RecordAdditionalInfo("number of hits", shortcut.number_of_hits); |
194 match.RecordAdditionalInfo("last access time", shortcut.last_access_time); | 222 match.RecordAdditionalInfo("last access time", shortcut.last_access_time); |
195 match.RecordAdditionalInfo("original input text", UTF16ToUTF8(shortcut.text)); | 223 match.RecordAdditionalInfo("original input text", UTF16ToUTF8(shortcut.text)); |
196 | 224 |
197 // Set |inline_autocompletion| and |allowed_to_be_default_match| if possible. | 225 // Set |inline_autocompletion| and |allowed_to_be_default_match| if possible. |
198 // If the match is a search query this is easy: simply check whether the | 226 // If the match is a search query this is easy: simply check whether the |
199 // user text is a prefix of the query. If the match is a navigation, we | 227 // user text is a prefix of the query. If the match is a navigation, we |
200 // assume the fill_into_edit looks something like a URL, so we use | 228 // assume the fill_into_edit looks something like a URL, so we use |
201 // BestURLPrefix() to try and strip off any prefixes that the user might | 229 // BestURLPrefix() to try and strip off any prefixes that the user might |
202 // not think would change the meaning, but would otherwise prevent inline | 230 // not think would change the meaning, but would otherwise prevent inline |
203 // autocompletion. This allows, for example, the input of "foo.c" to | 231 // autocompletion. This allows, for example, the input of "foo.c" to |
204 // autocomplete to "foo.com" for a fill_into_edit of "http://foo.com". | 232 // autocomplete to "foo.com" for a fill_into_edit of "http://foo.com". |
205 if (AutocompleteMatch::IsSearchType(match.type)) { | 233 if (AutocompleteMatch::IsSearchType(match.type)) { |
206 if (StartsWith(match.fill_into_edit, term_string, false)) { | 234 if (StartsWith(match.fill_into_edit, term_string, false)) { |
207 match.inline_autocompletion = | 235 match.inline_autocompletion = |
208 match.fill_into_edit.substr(term_string.length()); | 236 match.fill_into_edit.substr(term_string.length()); |
209 match.allowed_to_be_default_match = | 237 match.allowed_to_be_default_match = |
210 !prevent_inline_autocomplete || match.inline_autocompletion.empty(); | 238 !prevent_inline_autocomplete || match.inline_autocompletion.empty(); |
211 } | 239 } |
212 } else { | 240 } else { |
213 const URLPrefix* best_prefix = | 241 const URLPrefix* best_prefix = |
214 URLPrefix::BestURLPrefix(match.fill_into_edit, term_string); | 242 BestURLPrefixWithWWWCase(match.fill_into_edit, term_string); |
215 URLPrefix www_prefix(ASCIIToUTF16("www."), 1); | 243 const base::string16* matching_string = &term_string; |
216 if ((best_prefix == NULL) || | 244 // If we failed to find a best_prefix initially, try again using a |
217 (best_prefix->num_components < www_prefix.num_components)) { | 245 // fixed-up version of the user input. This is especially useful to |
218 // Sometimes |fill_into_edit| can start with "www." without having a | 246 // get about: URLs to inline against chrome:// shortcuts. (about: |
219 // protocol at the beginning. Because "www." is not on the default | 247 // URLs are fixed up to the chrome:// scheme.) |
220 // prefix list, we test for it explicitly here and use that match if | 248 if ((best_prefix == NULL) && !fixed_up_term_string.empty() && |
221 // the default list didn't have a match or the default list's match | 249 (fixed_up_term_string != term_string)) { |
222 // was shorter than it could've been. | 250 best_prefix = BestURLPrefixWithWWWCase( |
223 if (URLPrefix::PrefixMatch(www_prefix, match.fill_into_edit, term_string)) | 251 match.fill_into_edit, fixed_up_term_string); |
224 best_prefix = &www_prefix; | 252 matching_string = &fixed_up_term_string; |
225 } | 253 } |
226 if (best_prefix != NULL) { | 254 if (best_prefix != NULL) { |
227 match.inline_autocompletion = match.fill_into_edit.substr( | 255 match.inline_autocompletion = match.fill_into_edit.substr( |
228 best_prefix->prefix.length() + term_string.length()); | 256 best_prefix->prefix.length() + matching_string->length()); |
229 match.allowed_to_be_default_match = | 257 match.allowed_to_be_default_match = |
230 !prevent_inline_autocomplete || match.inline_autocompletion.empty(); | 258 !prevent_inline_autocomplete || match.inline_autocompletion.empty(); |
231 } | 259 } |
232 } | 260 } |
233 | 261 |
234 // Try to mark pieces of the contents and description as matches if they | 262 // Try to mark pieces of the contents and description as matches if they |
235 // appear in |term_string|. | 263 // appear in |term_string|. |
236 WordMap terms_map(CreateWordMapForString(term_string)); | 264 WordMap terms_map(CreateWordMapForString(term_string)); |
237 if (!terms_map.empty()) { | 265 if (!terms_map.empty()) { |
238 match.contents_class = ClassifyAllMatchesInString(term_string, terms_map, | 266 match.contents_class = ClassifyAllMatchesInString(term_string, terms_map, |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. | 420 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. |
393 const double kMaxDecaySpeedDivisor = 5.0; | 421 const double kMaxDecaySpeedDivisor = 5.0; |
394 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; | 422 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; |
395 double decay_divisor = std::min(kMaxDecaySpeedDivisor, | 423 double decay_divisor = std::min(kMaxDecaySpeedDivisor, |
396 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / | 424 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / |
397 kNumUsesPerDecaySpeedDivisorIncrement); | 425 kNumUsesPerDecaySpeedDivisorIncrement); |
398 | 426 |
399 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + | 427 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + |
400 0.5); | 428 0.5); |
401 } | 429 } |
OLD | NEW |