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

Side by Side Diff: chrome/browser/autocomplete/bookmark_provider.cc

Issue 489373005: Omnibox: Make URLs of Bookmarks Searchable (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months 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/bookmark_provider.h" 5 #include "chrome/browser/autocomplete/bookmark_provider.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" 13 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h"
14 #include "chrome/browser/autocomplete/history_provider.h" 14 #include "chrome/browser/autocomplete/history_provider.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
18 #include "components/bookmarks/browser/bookmark_match.h" 18 #include "components/bookmarks/browser/bookmark_match.h"
19 #include "components/bookmarks/browser/bookmark_model.h" 19 #include "components/bookmarks/browser/bookmark_model.h"
20 #include "components/metrics/proto/omnibox_input_type.pb.h" 20 #include "components/metrics/proto/omnibox_input_type.pb.h"
21 #include "components/omnibox/autocomplete_result.h" 21 #include "components/omnibox/autocomplete_result.h"
22 #include "components/omnibox/omnibox_field_trial.h"
23 #include "components/omnibox/url_prefix.h" 22 #include "components/omnibox/url_prefix.h"
24 #include "net/base/net_util.h" 23 #include "net/base/net_util.h"
25 24
26 using bookmarks::BookmarkMatch; 25 using bookmarks::BookmarkMatch;
27 26
28 typedef std::vector<BookmarkMatch> BookmarkMatches; 27 typedef std::vector<BookmarkMatch> BookmarkMatches;
29 28
30 // BookmarkProvider ------------------------------------------------------------ 29 // BookmarkProvider ------------------------------------------------------------
31 30
32 BookmarkProvider::BookmarkProvider(Profile* profile) 31 BookmarkProvider::BookmarkProvider(Profile* profile)
33 : AutocompleteProvider(AutocompleteProvider::TYPE_BOOKMARK), 32 : AutocompleteProvider(AutocompleteProvider::TYPE_BOOKMARK),
34 profile_(profile), 33 profile_(profile),
35 bookmark_model_(NULL), 34 bookmark_model_(NULL) {
36 score_using_url_matches_(OmniboxFieldTrial::BookmarksIndexURLsValue()) {
37 if (profile) { 35 if (profile) {
38 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile); 36 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile);
39 languages_ = profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); 37 languages_ = profile_->GetPrefs()->GetString(prefs::kAcceptLanguages);
40 } 38 }
41 } 39 }
42 40
43 void BookmarkProvider::Start(const AutocompleteInput& input, 41 void BookmarkProvider::Start(const AutocompleteInput& input,
44 bool minimal_changes) { 42 bool minimal_changes) {
45 if (minimal_changes) 43 if (minimal_changes)
46 return; 44 return;
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 // giving more points for matches that appear earlier in the string: 211 // giving more points for matches that appear earlier in the string:
214 // ((string_length - position of match start) / string_length). 212 // ((string_length - position of match start) / string_length).
215 // 213 //
216 // Example: Given a bookmark title of 'abcde fghijklm', with a title length 214 // Example: Given a bookmark title of 'abcde fghijklm', with a title length
217 // of 14, and two different search terms, 'abcde' and 'fghij', with 215 // of 14, and two different search terms, 'abcde' and 'fghij', with
218 // start positions of 0 and 6, respectively, 'abcde' will score higher 216 // start positions of 0 and 6, respectively, 'abcde' will score higher
219 // (with a a partial factor of (14-0)/14 = 1.000 ) than 'fghij' (with 217 // (with a a partial factor of (14-0)/14 = 1.000 ) than 'fghij' (with
220 // a partial factor of (14-6)/14 = 0.571 ). (In this example neither 218 // a partial factor of (14-6)/14 = 0.571 ). (In this example neither
221 // term matches in the URL.) 219 // term matches in the URL.)
222 // 220 //
223 // Once all match factors have been calculated they are summed. If URL 221 // Once all match factors have been calculated they are summed. If there
224 // matches are not considered, the resulting sum will never be greater than 222 // are no URL matches, the resulting sum will never be greater than the
225 // the length of the bookmark title because of the way the bookmark model 223 // length of the bookmark title because of the way the bookmark model matches
226 // matches and removes overlaps. (In particular, the bookmark model only 224 // and removes overlaps. (In particular, the bookmark model only
227 // matches terms to the beginning of words and it removes all overlapping 225 // matches terms to the beginning of words and it removes all overlapping
228 // matches, keeping only the longest. Together these mean that each 226 // matches, keeping only the longest. Together these mean that each
229 // character is included in at most one match.) If URL matches are 227 // character is included in at most one match.) If there are matches in the
230 // considered, the sum can be greater. 228 // URL, the sum can be greater.
231 // 229 //
232 // This sum is then normalized by the length of the bookmark title (if URL 230 // This sum is then normalized by the length of the bookmark title + 10
233 // matches are not considered) or by the length of the bookmark title + 10 231 // and capped at 1.0. The +10 is to expand the scoring range so fewer
234 // (if URL matches are considered) and capped at 1.0. (If URL matches 232 // bookmarks will hit the 1.0 cap and hence lose all ability to distinguish
235 // are considered, we want to expand the scoring range so fewer bookmarks 233 // between these high-quality bookmarks.)
Peter Kasting 2014/08/22 00:31:22 Nit: Closing paren does not have matching open par
Mark P 2014/08/22 15:56:38 Done.
236 // will hit the 1.0 cap and hence lose all ability to distinguish between
237 // these high-quality bookmarks.)
238 // 234 //
239 // The normalized value is multiplied against the scoring range available, 235 // The normalized value is multiplied against the scoring range available,
240 // which is 299. The 299 is calculated by subtracting the minimum possible 236 // which is 299. The 299 is calculated by subtracting the minimum possible
241 // score, 900, from the maximum possible score, 1199. This product, ranging 237 // score, 900, from the maximum possible score, 1199. This product, ranging
242 // from 0 to 299, is added to the minimum possible score, 900, giving the 238 // from 0 to 299, is added to the minimum possible score, 900, giving the
243 // preliminary score. 239 // preliminary score.
244 // 240 //
245 // If the preliminary score is less than the maximum possible score, 1199, 241 // If the preliminary score is less than the maximum possible score, 1199,
246 // it can be boosted up to that maximum possible score if the URL referenced 242 // it can be boosted up to that maximum possible score if the URL referenced
247 // by the bookmark is also referenced by any of the user's other bookmarks. 243 // by the bookmark is also referenced by any of the user's other bookmarks.
248 // A count of how many times the bookmark's URL is referenced is determined 244 // A count of how many times the bookmark's URL is referenced is determined
249 // and, for each additional reference beyond the one for the bookmark being 245 // and, for each additional reference beyond the one for the bookmark being
250 // scored up to a maximum of three, the score is boosted by a fixed amount 246 // scored up to a maximum of three, the score is boosted by a fixed amount
251 // given by |kURLCountBoost|, below. 247 // given by |kURLCountBoost|, below.
252 // 248 //
253 if (score_using_url_matches_) { 249
254 // Pretend empty titles are identical to the URL. 250 // Pretend empty titles are identical to the URL.
255 if (title.empty()) 251 if (title.empty())
256 title = base::ASCIIToUTF16(url.spec()); 252 title = base::ASCIIToUTF16(url.spec());
257 } else {
258 DCHECK(!title.empty());
259 }
260 ScoringFunctor title_position_functor = 253 ScoringFunctor title_position_functor =
261 for_each(bookmark_match.title_match_positions.begin(), 254 for_each(bookmark_match.title_match_positions.begin(),
262 bookmark_match.title_match_positions.end(), 255 bookmark_match.title_match_positions.end(),
263 ScoringFunctor(title.size())); 256 ScoringFunctor(title.size()));
264 ScoringFunctor url_position_functor = 257 ScoringFunctor url_position_functor =
265 for_each(bookmark_match.url_match_positions.begin(), 258 for_each(bookmark_match.url_match_positions.begin(),
266 bookmark_match.url_match_positions.end(), 259 bookmark_match.url_match_positions.end(),
267 ScoringFunctor(bookmark_match.node->url().spec().length())); 260 ScoringFunctor(bookmark_match.node->url().spec().length()));
268 const double summed_factors = title_position_functor.ScoringFactor() + 261 const double summed_factors = title_position_functor.ScoringFactor() +
269 (score_using_url_matches_ ? url_position_functor.ScoringFactor() : 0); 262 url_position_functor.ScoringFactor();
270 const double normalized_sum = std::min( 263 const double normalized_sum =
271 summed_factors / (title.size() + (score_using_url_matches_ ? 10 : 0)), 264 std::min(summed_factors / (title.size() + 10), 1.0);
272 1.0);
273 const int kBaseBookmarkScore = 900; 265 const int kBaseBookmarkScore = 900;
274 const int kMaxBookmarkScore = 1199; 266 const int kMaxBookmarkScore = 1199;
275 const double kBookmarkScoreRange = 267 const double kBookmarkScoreRange =
276 static_cast<double>(kMaxBookmarkScore - kBaseBookmarkScore); 268 static_cast<double>(kMaxBookmarkScore - kBaseBookmarkScore);
277 match.relevance = static_cast<int>(normalized_sum * kBookmarkScoreRange) + 269 match.relevance = static_cast<int>(normalized_sum * kBookmarkScoreRange) +
278 kBaseBookmarkScore; 270 kBaseBookmarkScore;
279 // Don't waste any time searching for additional referenced URLs if we 271 // Don't waste any time searching for additional referenced URLs if we
280 // already have a perfect title match. 272 // already have a perfect title match.
281 if (match.relevance >= kMaxBookmarkScore) 273 if (match.relevance >= kMaxBookmarkScore)
282 return match; 274 return match;
(...skipping 27 matching lines...) Expand all
310 i != positions.end(); 302 i != positions.end();
311 ++i) { 303 ++i) {
312 AutocompleteMatch::ACMatchClassifications new_class; 304 AutocompleteMatch::ACMatchClassifications new_class;
313 AutocompleteMatch::ClassifyLocationInString(i->first, i->second - i->first, 305 AutocompleteMatch::ClassifyLocationInString(i->first, i->second - i->first,
314 text_length, url_style, &new_class); 306 text_length, url_style, &new_class);
315 classifications = AutocompleteMatch::MergeClassifications( 307 classifications = AutocompleteMatch::MergeClassifications(
316 classifications, new_class); 308 classifications, new_class);
317 } 309 }
318 return classifications; 310 return classifications;
319 } 311 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698