Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: trunk/src/chrome/browser/autocomplete/shortcuts_provider.cc

Issue 106573003: Revert 242029 "Omnibox: Bug Fixes for Shortcuts Inlining" (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
25 #include "chrome/browser/autocomplete/url_prefix.h" 24 #include "chrome/browser/autocomplete/url_prefix.h"
26 #include "chrome/browser/history/history_notifications.h" 25 #include "chrome/browser/history/history_notifications.h"
27 #include "chrome/browser/history/history_service.h" 26 #include "chrome/browser/history/history_service.h"
28 #include "chrome/browser/history/history_service_factory.h" 27 #include "chrome/browser/history/history_service_factory.h"
29 #include "chrome/browser/history/shortcuts_backend_factory.h" 28 #include "chrome/browser/history/shortcuts_backend_factory.h"
30 #include "chrome/browser/omnibox/omnibox_field_trial.h" 29 #include "chrome/browser/omnibox/omnibox_field_trial.h"
31 #include "chrome/browser/profiles/profile.h" 30 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/common/net/url_fixer_upper.h"
33 #include "chrome/common/pref_names.h" 31 #include "chrome/common/pref_names.h"
34 #include "chrome/common/url_constants.h" 32 #include "chrome/common/url_constants.h"
35 #include "url/url_parse.h" 33 #include "url/url_parse.h"
36 34
37 namespace { 35 namespace {
38 36
39 class DestinationURLEqualsURL { 37 class DestinationURLEqualsURL {
40 public: 38 public:
41 explicit DestinationURLEqualsURL(const GURL& url) : url_(url) {} 39 explicit DestinationURLEqualsURL(const GURL& url) : url_(url) {}
42 bool operator()(const AutocompleteMatch& match) const { 40 bool operator()(const AutocompleteMatch& match) const {
43 return match.destination_url == url_; 41 return match.destination_url == url_;
44 } 42 }
45 private: 43 private:
46 const GURL url_; 44 const GURL url_;
47 }; 45 };
48 46
49 } // namespace 47 } // namespace
50 48
51 ShortcutsProvider::ShortcutsProvider(AutocompleteProviderListener* listener, 49 ShortcutsProvider::ShortcutsProvider(AutocompleteProviderListener* listener,
52 Profile* profile) 50 Profile* profile)
53 : AutocompleteProvider(listener, profile, 51 : AutocompleteProvider(listener, profile,
54 AutocompleteProvider::TYPE_SHORTCUTS), 52 AutocompleteProvider::TYPE_SHORTCUTS),
55 languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)), 53 languages_(profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)),
56 initialized_(false), 54 initialized_(false) {
57 www_prefix_(ASCIIToUTF16("www."), 1) {
58 scoped_refptr<history::ShortcutsBackend> backend = 55 scoped_refptr<history::ShortcutsBackend> backend =
59 ShortcutsBackendFactory::GetForProfile(profile_); 56 ShortcutsBackendFactory::GetForProfile(profile_);
60 if (backend.get()) { 57 if (backend.get()) {
61 backend->AddObserver(this); 58 backend->AddObserver(this);
62 if (backend->initialized()) 59 if (backend->initialized())
63 initialized_ = true; 60 initialized_ = true;
64 } 61 }
65 } 62 }
66 63
67 void ShortcutsProvider::Start(const AutocompleteInput& input, 64 void ShortcutsProvider::Start(const AutocompleteInput& input,
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 void ShortcutsProvider::GetMatches(const AutocompleteInput& input) { 127 void ShortcutsProvider::GetMatches(const AutocompleteInput& input) {
131 scoped_refptr<history::ShortcutsBackend> backend = 128 scoped_refptr<history::ShortcutsBackend> backend =
132 ShortcutsBackendFactory::GetForProfileIfExists(profile_); 129 ShortcutsBackendFactory::GetForProfileIfExists(profile_);
133 if (!backend.get()) 130 if (!backend.get())
134 return; 131 return;
135 // Get the URLs from the shortcuts database with keys that partially or 132 // Get the URLs from the shortcuts database with keys that partially or
136 // completely match the search term. 133 // completely match the search term.
137 base::string16 term_string(base::i18n::ToLower(input.text())); 134 base::string16 term_string(base::i18n::ToLower(input.text()));
138 DCHECK(!term_string.empty()); 135 DCHECK(!term_string.empty());
139 136
140 base::string16 fixed_up_term_string(term_string);
141 AutocompleteInput fixed_up_input(input);
142 if (HistoryProvider::FixupUserInput(&fixed_up_input))
143 fixed_up_term_string = fixed_up_input.text();
144
145 int max_relevance; 137 int max_relevance;
146 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( 138 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance(
147 input.current_page_classification(), &max_relevance)) 139 input.current_page_classification(), &max_relevance))
148 max_relevance = AutocompleteResult::kLowestDefaultScore - 1; 140 max_relevance = AutocompleteResult::kLowestDefaultScore - 1;
149 141
150 for (history::ShortcutsBackend::ShortcutMap::const_iterator it = 142 for (history::ShortcutsBackend::ShortcutMap::const_iterator it =
151 FindFirstMatch(term_string, backend.get()); 143 FindFirstMatch(term_string, backend.get());
152 it != backend->shortcuts_map().end() && 144 it != backend->shortcuts_map().end() &&
153 StartsWith(it->first, term_string, true); ++it) { 145 StartsWith(it->first, term_string, true); ++it) {
154 // Don't return shortcuts with zero relevance. 146 // Don't return shortcuts with zero relevance.
155 int relevance = CalculateScore(term_string, it->second, max_relevance); 147 int relevance = CalculateScore(term_string, it->second, max_relevance);
156 if (relevance) { 148 if (relevance) {
157 matches_.push_back(ShortcutToACMatch( 149 matches_.push_back(ShortcutToACMatch(
158 it->second, relevance, term_string, fixed_up_term_string, 150 it->second, relevance, term_string,
159 input.prevent_inline_autocomplete())); 151 input.prevent_inline_autocomplete()));
160 } 152 }
161 } 153 }
162 std::partial_sort(matches_.begin(), 154 std::partial_sort(matches_.begin(),
163 matches_.begin() + 155 matches_.begin() +
164 std::min(AutocompleteProvider::kMaxMatches, matches_.size()), 156 std::min(AutocompleteProvider::kMaxMatches, matches_.size()),
165 matches_.end(), &AutocompleteMatch::MoreRelevant); 157 matches_.end(), &AutocompleteMatch::MoreRelevant);
166 if (matches_.size() > AutocompleteProvider::kMaxMatches) { 158 if (matches_.size() > AutocompleteProvider::kMaxMatches) {
167 matches_.erase(matches_.begin() + AutocompleteProvider::kMaxMatches, 159 matches_.erase(matches_.begin() + AutocompleteProvider::kMaxMatches,
168 matches_.end()); 160 matches_.end());
(...skipping 15 matching lines...) Expand all
184 it->relevance = max_relevance; 176 it->relevance = max_relevance;
185 if (max_relevance > 1) 177 if (max_relevance > 1)
186 --max_relevance; 178 --max_relevance;
187 } 179 }
188 } 180 }
189 181
190 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( 182 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch(
191 const history::ShortcutsBackend::Shortcut& shortcut, 183 const history::ShortcutsBackend::Shortcut& shortcut,
192 int relevance, 184 int relevance,
193 const base::string16& term_string, 185 const base::string16& term_string,
194 const base::string16& fixed_up_term_string, 186 bool prevent_inline_autocomplete) {
195 const bool prevent_inline_autocomplete) {
196 DCHECK(!term_string.empty()); 187 DCHECK(!term_string.empty());
197 AutocompleteMatch match(shortcut.match_core.ToMatch()); 188 AutocompleteMatch match(shortcut.match_core.ToMatch());
198 match.provider = this; 189 match.provider = this;
199 match.relevance = relevance; 190 match.relevance = relevance;
200 match.deletable = true; 191 match.deletable = true;
201 DCHECK(match.destination_url.is_valid()); 192 DCHECK(match.destination_url.is_valid());
202 match.RecordAdditionalInfo("number of hits", shortcut.number_of_hits); 193 match.RecordAdditionalInfo("number of hits", shortcut.number_of_hits);
203 match.RecordAdditionalInfo("last access time", shortcut.last_access_time); 194 match.RecordAdditionalInfo("last access time", shortcut.last_access_time);
204 match.RecordAdditionalInfo("original input text", UTF16ToUTF8(shortcut.text)); 195 match.RecordAdditionalInfo("original input text", UTF16ToUTF8(shortcut.text));
205 196
206 // Set |inline_autocompletion| and |allowed_to_be_default_match| if possible. 197 // Set |inline_autocompletion| and |allowed_to_be_default_match| if possible.
207 // If the match is a search query this is easy: simply check whether the 198 // If the match is a search query this is easy: simply check whether the
208 // user text is a prefix of the query. If the match is a navigation, we 199 // user text is a prefix of the query. If the match is a navigation, we
209 // assume the fill_into_edit looks something like a URL, so we use 200 // assume the fill_into_edit looks something like a URL, so we use
210 // BestURLPrefix() to try and strip off any prefixes that the user might 201 // BestURLPrefix() to try and strip off any prefixes that the user might
211 // not think would change the meaning, but would otherwise prevent inline 202 // not think would change the meaning, but would otherwise prevent inline
212 // autocompletion. This allows, for example, the input of "foo.c" to 203 // autocompletion. This allows, for example, the input of "foo.c" to
213 // autocomplete to "foo.com" for a fill_into_edit of "http://foo.com". 204 // autocomplete to "foo.com" for a fill_into_edit of "http://foo.com".
214 if (AutocompleteMatch::IsSearchType(match.type)) { 205 if (AutocompleteMatch::IsSearchType(match.type)) {
215 if (StartsWith(match.fill_into_edit, term_string, false)) { 206 if (StartsWith(match.fill_into_edit, term_string, false)) {
216 match.inline_autocompletion = 207 match.inline_autocompletion =
217 match.fill_into_edit.substr(term_string.length()); 208 match.fill_into_edit.substr(term_string.length());
218 match.allowed_to_be_default_match = 209 match.allowed_to_be_default_match =
219 !prevent_inline_autocomplete || match.inline_autocompletion.empty(); 210 !prevent_inline_autocomplete || match.inline_autocompletion.empty();
220 } 211 }
221 } else { 212 } else {
222 const URLPrefix* best_prefix = 213 const URLPrefix* best_prefix =
223 BestURLPrefixWithWWWCase(match.fill_into_edit, term_string); 214 URLPrefix::BestURLPrefix(match.fill_into_edit, term_string);
224 const base::string16* matching_string = &term_string; 215 URLPrefix www_prefix(ASCIIToUTF16("www."), 1);
225 // If we failed to find a best_prefix initially, try again using a 216 if ((best_prefix == NULL) ||
226 // fixed-up version of the user input. This is especially useful to 217 (best_prefix->num_components < www_prefix.num_components)) {
227 // get about: URLs to inline against chrome:// shortcuts. (about: 218 // Sometimes |fill_into_edit| can start with "www." without having a
228 // URLs are fixed up to the chrome:// scheme.) 219 // protocol at the beginning. Because "www." is not on the default
229 if ((best_prefix == NULL) && !fixed_up_term_string.empty() && 220 // prefix list, we test for it explicitly here and use that match if
230 (fixed_up_term_string != term_string)) { 221 // the default list didn't have a match or the default list's match
231 best_prefix = BestURLPrefixWithWWWCase( 222 // was shorter than it could've been.
232 match.fill_into_edit, fixed_up_term_string); 223 if (URLPrefix::PrefixMatch(www_prefix, match.fill_into_edit, term_string))
233 matching_string = &fixed_up_term_string; 224 best_prefix = &www_prefix;
234 } 225 }
235 if (best_prefix != NULL) { 226 if (best_prefix != NULL) {
236 match.inline_autocompletion = match.fill_into_edit.substr( 227 match.inline_autocompletion = match.fill_into_edit.substr(
237 best_prefix->prefix.length() + matching_string->length()); 228 best_prefix->prefix.length() + term_string.length());
238 match.allowed_to_be_default_match = 229 match.allowed_to_be_default_match =
239 !prevent_inline_autocomplete || match.inline_autocompletion.empty(); 230 !prevent_inline_autocomplete || match.inline_autocompletion.empty();
240 } 231 }
241 } 232 }
242 233
243 // Try to mark pieces of the contents and description as matches if they 234 // Try to mark pieces of the contents and description as matches if they
244 // appear in |term_string|. 235 // appear in |term_string|.
245 WordMap terms_map(CreateWordMapForString(term_string)); 236 WordMap terms_map(CreateWordMapForString(term_string));
246 if (!terms_map.empty()) { 237 if (!terms_map.empty()) {
247 match.contents_class = ClassifyAllMatchesInString(term_string, terms_map, 238 match.contents_class = ClassifyAllMatchesInString(term_string, terms_map,
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 // (1.0 / each 5 additional hits), up to a maximum of 5x as long. 392 // (1.0 / each 5 additional hits), up to a maximum of 5x as long.
402 const double kMaxDecaySpeedDivisor = 5.0; 393 const double kMaxDecaySpeedDivisor = 5.0;
403 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; 394 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0;
404 double decay_divisor = std::min(kMaxDecaySpeedDivisor, 395 double decay_divisor = std::min(kMaxDecaySpeedDivisor,
405 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / 396 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) /
406 kNumUsesPerDecaySpeedDivisorIncrement); 397 kNumUsesPerDecaySpeedDivisorIncrement);
407 398
408 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + 399 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) +
409 0.5); 400 0.5);
410 } 401 }
411
412 const URLPrefix* ShortcutsProvider::BestURLPrefixWithWWWCase(
413 const base::string16& text,
414 const base::string16& prefix_suffix) const {
415 const URLPrefix* best_prefix = URLPrefix::BestURLPrefix(text, prefix_suffix);
416 if ((best_prefix == NULL) ||
417 (best_prefix->num_components < www_prefix_.num_components)) {
418 if (URLPrefix::PrefixMatch(www_prefix_, text, prefix_suffix))
419 best_prefix = &www_prefix_;
420 }
421 return best_prefix;
422 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698