|
OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/autocomplete/bookmark_provider.h" | |
6 | |
7 #include <functional> | |
8 #include <vector> | |
9 | |
10 #include "base/metrics/histogram.h" | |
11 #include "base/time.h" | |
12 #include "chrome/browser/autocomplete/autocomplete_result.h" | |
13 #include "chrome/browser/bookmarks/bookmark_model.h" | |
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | |
15 #include "chrome/browser/prefs/pref_service.h" | |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "chrome/common/pref_names.h" | |
18 #include "net/base/net_util.h" | |
19 | |
20 typedef std::vector<bookmark_utils::TitleMatch> TitleMatches; | |
21 | |
22 // BookmarkProvider ------------------------------------------------------------ | |
23 | |
24 BookmarkProvider::BookmarkProvider( | |
25 AutocompleteProviderListener* listener, | |
26 Profile* profile) | |
27 : AutocompleteProvider(listener, profile, | |
28 AutocompleteProvider::TYPE_BOOKMARK), | |
29 bookmark_model_(NULL) { | |
30 if (profile) | |
31 languages_ = profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); | |
32 } | |
33 | |
34 void BookmarkProvider::Start(const AutocompleteInput& input, | |
35 bool minimal_changes) { | |
36 matches_.clear(); | |
Peter Kasting
2012/10/04 20:40:31
Seems like if |minimal_changes| is true we can jus
mrossetti
2012/10/05 22:10:04
Ah! Excellent point.
| |
37 | |
38 // Only bother with UNKNOWN and QUERY. | |
Peter Kasting
2012/10/04 20:40:31
What about REQUESTED_URL? That's similar to UNKNO
mrossetti
2012/10/05 22:10:04
Done.
| |
39 if ((input.type() != AutocompleteInput::UNKNOWN) && | |
40 (input.type() != AutocompleteInput::QUERY)) | |
41 return; | |
42 | |
43 base::TimeTicks start_time = base::TimeTicks::Now(); | |
44 DoAutocomplete(input, | |
45 input.matches_requested() == AutocompleteInput::BEST_MATCH); | |
Peter Kasting
2012/10/04 20:40:31
We should probably bail if BEST_MATCH and input.pr
mrossetti
2012/10/05 22:10:04
Done.
| |
46 UMA_HISTOGRAM_TIMES("Autocomplete.BookmarkProviderMatchTime", | |
47 base::TimeTicks::Now() - start_time); | |
48 } | |
49 | |
50 BookmarkProvider::~BookmarkProvider() {} | |
51 | |
52 | |
53 void BookmarkProvider::DoAutocomplete(const AutocompleteInput& input, | |
54 bool best_match) { | |
55 if (!bookmark_model_) { | |
Peter Kasting
2012/10/04 20:40:31
Does setting this lazily here speed up startup? I
mrossetti
2012/10/05 22:10:04
Done. (Premature optimization!)
| |
56 // We may not have a profile or bookmark model for some unit tests. | |
57 if (!profile_) | |
58 return; | |
59 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile_); | |
60 if (!bookmark_model_) | |
61 return; | |
62 } | |
63 | |
64 TitleMatches matches; | |
65 // Retrieve enough bookmarks so that we can reasonably score the best. | |
66 const size_t kMaxBookmarkMatches = 50; | |
67 bookmark_model_->GetBookmarksWithTitlesMatching(input.text(), | |
Peter Kasting
2012/10/04 20:40:31
Does this match only exact prefixes, or is it smar
mrossetti
2012/10/05 22:10:04
Yes, you are correct. The BookmarkIndex::GetBookma
| |
68 kMaxBookmarkMatches, | |
69 &matches); | |
70 if (!matches.size()) | |
71 return; // There were no matches. | |
72 | |
73 for (TitleMatches::const_iterator i = matches.begin(); i != matches.end(); | |
74 ++i) | |
75 matches_.push_back(TitleMatchToACMatch(*i)); | |
76 | |
77 // Sort and clip the resulting matches. | |
78 size_t max_matches = best_match ? 1 : AutocompleteProvider::kMaxMatches; | |
79 if (matches_.size() > max_matches) { | |
80 std::partial_sort(matches_.begin(), matches_.end(), | |
81 matches_.begin() + max_matches, | |
82 AutocompleteMatch::MoreRelevant); | |
83 matches_.resize(max_matches); | |
84 } else { | |
85 std::sort(matches_.begin(), matches_.end(), | |
86 AutocompleteMatch::MoreRelevant); | |
87 } | |
88 } | |
89 | |
90 namespace { | |
91 | |
92 // for_each helper function that accumulates match term lengths and calculates | |
93 // position disorders. | |
94 class PositionFunctor { | |
95 public: | |
96 PositionFunctor() : length_(0), disorders_(0), last_pos_(0) {} | |
97 void operator() (const Snippet::MatchPosition& match) { | |
98 length_ += match.second - match.first; | |
99 if (last_pos_ >= match.second) | |
100 ++disorders_; | |
101 } | |
102 size_t TotalLength() { return length_; } | |
103 int TotalDisorders() { return disorders_; } | |
104 | |
105 private: | |
106 long length_; | |
107 int disorders_; | |
108 size_t last_pos_; | |
109 }; | |
110 | |
111 } // namespace | |
112 | |
113 AutocompleteMatch BookmarkProvider::TitleMatchToACMatch( | |
Peter Kasting
2012/10/04 20:40:31
Nit: This function could really use a description
mrossetti
2012/10/05 22:10:04
Forthcoming as I rework the scoring.
| |
114 const bookmark_utils::TitleMatch& title_match) { | |
115 // Calculate the relevance. | |
116 // TODO(mpearson): Fix this very, very simplistic scoring method. | |
117 // Base score = 900. Add 100 for first match, 75 for next, 50, 25, 0. | |
118 const int kMatchCountBoost[4] = { 100, 175, 225, 250 }; | |
119 size_t position_count = | |
120 std::min<size_t>(4, title_match.match_positions.size()); | |
121 int relevance = 900 + kMatchCountBoost[position_count]; | |
122 // Add 1199 - score * total match length / title length | |
123 PositionFunctor position_functor = | |
124 for_each(title_match.match_positions.begin(), | |
125 title_match.match_positions.end(), PositionFunctor()); | |
126 int adjuster = ((AutocompleteResult::kLowestDefaultScore - relevance - 1) * | |
127 static_cast<int>(position_functor.TotalLength())) / | |
128 static_cast<int>(title_match.node->GetTitle().size()); | |
129 relevance += adjuster; | |
130 // Subtract 50 for each out-of-order match. | |
131 relevance -= 50 * position_functor.TotalDisorders(); | |
132 | |
133 AutocompleteMatch match(this, relevance, false, | |
134 AutocompleteMatch::BOOKMARK_TITLE); | |
135 const BookmarkNode& node(*(title_match.node)); | |
136 match.destination_url = node.url(); | |
137 match.contents = net::FormatUrl(node.url(), languages_, | |
138 net::kFormatUrlOmitAll & net::kFormatUrlOmitHTTP, | |
139 net::UnescapeRule::SPACES, NULL, NULL, NULL); | |
140 match.contents_class.push_back( | |
141 ACMatchClassification(0, ACMatchClassification::NONE)); | |
142 match.fill_into_edit = | |
143 AutocompleteInput::FormattedStringWithEquivalentMeaning(node.url(), | |
144 match.contents); | |
145 match.description = node.GetTitle(); | |
146 match.description_class = | |
147 SpansFromMatch(title_match.match_positions, match.description.size()); | |
148 | |
149 match.starred = true; | |
150 return match; | |
151 } | |
152 | |
153 namespace { | |
154 | |
155 bool MatchPositionSort(const Snippet::MatchPosition& p1, | |
156 const Snippet::MatchPosition& p2) { | |
157 return p1.first < p2.first; | |
158 } | |
159 | |
160 } // namespace | |
161 | |
162 // static | |
163 ACMatchClassifications BookmarkProvider::SpansFromMatch( | |
164 const Snippet::MatchPositions& positions, | |
165 size_t text_length) { | |
Peter Kasting
2012/10/04 20:40:31
We do similar things in contact_provider_chromeos.
mrossetti
2012/10/05 22:10:04
Done.
| |
166 ACMatchClassifications spans; | |
167 if (positions.size() == 0) { | |
Peter Kasting
2012/10/04 20:40:31
Nit: empty()
mrossetti
2012/10/05 22:10:04
Gah!
| |
168 spans.push_back(ACMatchClassification(0, ACMatchClassification::NONE)); | |
169 return spans; | |
170 } | |
171 | |
172 Snippet::MatchPositions distinct_positions; | |
173 if (positions.size() == 1) { | |
174 distinct_positions = positions; // Can't use swap: positions is const. | |
175 } else { | |
176 // Sort the positions. | |
177 Snippet::MatchPositions sorted_positions(positions); | |
178 std::sort(sorted_positions.begin(), sorted_positions.end(), | |
179 MatchPositionSort); | |
180 | |
181 // Collapse any positions that overlap. | |
182 Snippet::MatchPositions::const_iterator a_iter = sorted_positions.begin(); | |
183 Snippet::MatchPosition a_pos(*a_iter); | |
184 for (Snippet::MatchPositions::const_iterator b_iter = ++a_iter; | |
185 b_iter != sorted_positions.end(); ++b_iter) { | |
186 if (a_pos.second >= b_iter->first) { | |
187 // Need collapsing. | |
188 a_pos.second = b_iter->second; | |
189 } else { | |
190 // |a| stands on its own. | |
191 distinct_positions.push_back(a_pos); | |
192 a_iter = b_iter; | |
193 a_pos = *a_iter; | |
194 } | |
195 } | |
196 distinct_positions.push_back(a_pos); | |
197 } | |
198 | |
199 // Convert the positions into ACMatchClassifications. | |
200 Snippet::MatchPositions::const_iterator p = distinct_positions.begin(); | |
201 if (p->first > 0) | |
202 spans.push_back(ACMatchClassification(0, ACMatchClassification::NONE)); | |
203 for (; p != distinct_positions.end(); ++p) { | |
204 spans.push_back(ACMatchClassification(p->first, | |
205 ACMatchClassification::MATCH)); | |
206 if (p->second < text_length) { | |
207 spans.push_back(ACMatchClassification(p->second, | |
208 ACMatchClassification::NONE)); | |
209 } | |
210 } | |
211 | |
212 return spans; | |
213 } | |
OLD | NEW |