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/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/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 14 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" |
15 #include "chrome/browser/autocomplete/history_provider.h" | 15 #include "chrome/browser/autocomplete/history_provider.h" |
16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
19 #include "components/bookmarks/browser/bookmark_match.h" | 19 #include "components/bookmarks/browser/bookmark_match.h" |
20 #include "components/bookmarks/browser/bookmark_model.h" | 20 #include "components/bookmarks/browser/bookmark_model.h" |
21 #include "components/metrics/proto/omnibox_input_type.pb.h" | 21 #include "components/metrics/proto/omnibox_input_type.pb.h" |
22 #include "components/omnibox/autocomplete_result.h" | 22 #include "components/omnibox/autocomplete_result.h" |
23 #include "components/omnibox/url_prefix.h" | 23 #include "components/omnibox/url_prefix.h" |
24 #include "net/base/net_util.h" | 24 #include "net/base/net_util.h" |
| 25 #include "url/url_constants.h" |
25 | 26 |
26 using bookmarks::BookmarkMatch; | 27 using bookmarks::BookmarkMatch; |
27 | 28 |
28 typedef std::vector<BookmarkMatch> BookmarkMatches; | 29 typedef std::vector<BookmarkMatch> BookmarkMatches; |
29 | 30 |
30 namespace { | 31 namespace { |
31 | 32 |
32 // Removes leading spaces from |title| before displaying, otherwise it looks | 33 // Removes leading spaces from |title| before displaying, otherwise it looks |
33 // funny. In the process, corrects |title_match_positions| so the correct | 34 // funny. In the process, corrects |title_match_positions| so the correct |
34 // characters are highlighted. | 35 // characters are highlighted. |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 // matches, keeping only the longest. Together these mean that each | 255 // matches, keeping only the longest. Together these mean that each |
255 // character is included in at most one match.) If there are matches in the | 256 // character is included in at most one match.) If there are matches in the |
256 // URL, the sum can be greater. | 257 // URL, the sum can be greater. |
257 // | 258 // |
258 // This sum is then normalized by the length of the bookmark title + 10 | 259 // This sum is then normalized by the length of the bookmark title + 10 |
259 // and capped at 1.0. The +10 is to expand the scoring range so fewer | 260 // and capped at 1.0. The +10 is to expand the scoring range so fewer |
260 // bookmarks will hit the 1.0 cap and hence lose all ability to distinguish | 261 // bookmarks will hit the 1.0 cap and hence lose all ability to distinguish |
261 // between these high-quality bookmarks. | 262 // between these high-quality bookmarks. |
262 // | 263 // |
263 // The normalized value is multiplied against the scoring range available, | 264 // The normalized value is multiplied against the scoring range available, |
264 // which is 299. The 299 is calculated by subtracting the minimum possible | 265 // which is the difference between the minimum possible score and the maximum |
265 // score, 900, from the maximum possible score, 1199. This product, ranging | 266 // possible score. This product is added to the minimum possible score to |
266 // from 0 to 299, is added to the minimum possible score, 900, giving the | 267 // give the preliminary score. |
267 // preliminary score. | |
268 // | 268 // |
269 // If the preliminary score is less than the maximum possible score, 1199, | 269 // If the preliminary score is less than the maximum possible score, 1199, |
270 // it can be boosted up to that maximum possible score if the URL referenced | 270 // it can be boosted up to that maximum possible score if the URL referenced |
271 // by the bookmark is also referenced by any of the user's other bookmarks. | 271 // by the bookmark is also referenced by any of the user's other bookmarks. |
272 // A count of how many times the bookmark's URL is referenced is determined | 272 // A count of how many times the bookmark's URL is referenced is determined |
273 // and, for each additional reference beyond the one for the bookmark being | 273 // and, for each additional reference beyond the one for the bookmark being |
274 // scored up to a maximum of three, the score is boosted by a fixed amount | 274 // scored up to a maximum of three, the score is boosted by a fixed amount |
275 // given by |kURLCountBoost|, below. | 275 // given by |kURLCountBoost|, below. |
276 // | 276 // |
277 | 277 |
278 // Pretend empty titles are identical to the URL. | 278 // Pretend empty titles are identical to the URL. |
279 if (title.empty()) | 279 if (title.empty()) |
280 title = base::ASCIIToUTF16(url.spec()); | 280 title = base::ASCIIToUTF16(url.spec()); |
281 ScoringFunctor title_position_functor = | 281 ScoringFunctor title_position_functor = |
282 for_each(bookmark_match.title_match_positions.begin(), | 282 for_each(bookmark_match.title_match_positions.begin(), |
283 bookmark_match.title_match_positions.end(), | 283 bookmark_match.title_match_positions.end(), |
284 ScoringFunctor(title.size())); | 284 ScoringFunctor(title.size())); |
285 ScoringFunctor url_position_functor = | 285 ScoringFunctor url_position_functor = |
286 for_each(bookmark_match.url_match_positions.begin(), | 286 for_each(bookmark_match.url_match_positions.begin(), |
287 bookmark_match.url_match_positions.end(), | 287 bookmark_match.url_match_positions.end(), |
288 ScoringFunctor(bookmark_match.node->url().spec().length())); | 288 ScoringFunctor(bookmark_match.node->url().spec().length())); |
289 const double summed_factors = title_position_functor.ScoringFactor() + | 289 const double title_match_strength = title_position_functor.ScoringFactor(); |
| 290 const double summed_factors = title_match_strength + |
290 url_position_functor.ScoringFactor(); | 291 url_position_functor.ScoringFactor(); |
291 const double normalized_sum = | 292 const double normalized_sum = |
292 std::min(summed_factors / (title.size() + 10), 1.0); | 293 std::min(summed_factors / (title.size() + 10), 1.0); |
293 const int kBaseBookmarkScore = 900; | 294 // Bookmarks with javascript scheme ("bookmarklets") that do not have title |
294 const int kMaxBookmarkScore = 1199; | 295 // matches get a lower base and lower maximum score because returning them |
| 296 // for matches in their (often very long) URL looks stupid and is often not |
| 297 // intended by the user. |
| 298 const bool bookmarklet_without_title_match = |
| 299 url.SchemeIs(url::kJavaScriptScheme) && (title_match_strength == 0.0); |
| 300 const int kBaseBookmarkScore = bookmarklet_without_title_match ? 400 : 900; |
| 301 const int kMaxBookmarkScore = bookmarklet_without_title_match ? 799 : 1199; |
295 const double kBookmarkScoreRange = | 302 const double kBookmarkScoreRange = |
296 static_cast<double>(kMaxBookmarkScore - kBaseBookmarkScore); | 303 static_cast<double>(kMaxBookmarkScore - kBaseBookmarkScore); |
297 match.relevance = static_cast<int>(normalized_sum * kBookmarkScoreRange) + | 304 match.relevance = static_cast<int>(normalized_sum * kBookmarkScoreRange) + |
298 kBaseBookmarkScore; | 305 kBaseBookmarkScore; |
299 // Don't waste any time searching for additional referenced URLs if we | 306 // Don't waste any time searching for additional referenced URLs if we |
300 // already have a perfect title match. | 307 // already have a perfect title match. |
301 if (match.relevance >= kMaxBookmarkScore) | 308 if (match.relevance >= kMaxBookmarkScore) |
302 return match; | 309 return match; |
303 // Boost the score if the bookmark's URL is referenced by other bookmarks. | 310 // Boost the score if the bookmark's URL is referenced by other bookmarks. |
304 const int kURLCountBoost[4] = { 0, 75, 125, 150 }; | 311 const int kURLCountBoost[4] = { 0, 75, 125, 150 }; |
(...skipping 25 matching lines...) Expand all Loading... |
330 i != positions.end(); | 337 i != positions.end(); |
331 ++i) { | 338 ++i) { |
332 AutocompleteMatch::ACMatchClassifications new_class; | 339 AutocompleteMatch::ACMatchClassifications new_class; |
333 AutocompleteMatch::ClassifyLocationInString(i->first, i->second - i->first, | 340 AutocompleteMatch::ClassifyLocationInString(i->first, i->second - i->first, |
334 text_length, url_style, &new_class); | 341 text_length, url_style, &new_class); |
335 classifications = AutocompleteMatch::MergeClassifications( | 342 classifications = AutocompleteMatch::MergeClassifications( |
336 classifications, new_class); | 343 classifications, new_class); |
337 } | 344 } |
338 return classifications; | 345 return classifications; |
339 } | 346 } |
OLD | NEW |