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

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

Issue 10913262: Implement Bookmark Autocomplete Provider (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 3 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 #include "chrome/browser/autocomplete/bookmark_provider.h"
2
3 #include <functional>
4 #include <vector>
5
6 #include "base/metrics/histogram.h"
7 #include "base/string_number_conversions.h"
Mark P 2012/09/28 23:10:22 Why do you need this?
mrossetti 2012/10/02 00:44:16 I don't any longer. Removed. Good catch.
8 #include "chrome/browser/autocomplete/autocomplete_result.h"
9 #include "chrome/browser/bookmarks/bookmark_model.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
11 #include "chrome/browser/prefs/pref_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/pref_names.h"
14 #include "net/base/net_util.h"
15
16 typedef std::vector<bookmark_utils::TitleMatch> TitleMatches;
17
18 // BookmarkProvider ------------------------------------------------------------
19
20 BookmarkProvider::BookmarkProvider(
21 AutocompleteProviderListener* listener,
22 Profile* profile)
23 : AutocompleteProvider(listener, profile,
24 AutocompleteProvider::TYPE_BOOKMARK),
25 bookmark_model_(NULL) {
26 if (profile)
27 languages_ = profile_->GetPrefs()->GetString(prefs::kAcceptLanguages);
28 }
29
30 void BookmarkProvider::Start(const AutocompleteInput& input,
31 bool minimal_changes) {
32 matches_.clear();
33
34 // Only bother with UNKNOWN and QUERY.
35 if ((input.type() != AutocompleteInput::UNKNOWN) &&
36 (input.type() != AutocompleteInput::QUERY))
37 return;
38
39 base::TimeTicks start_time = base::TimeTicks::Now();
40 DoAutocomplete(input,
41 input.matches_requested() == AutocompleteInput::BEST_MATCH);
42 UMA_HISTOGRAM_TIMES("Autocomplete.BookmarkProviderMatchTime",
Mark P 2012/09/28 23:10:22 Perhaps you want two different histograms, one for
mrossetti 2012/10/02 00:44:16 It doesn't make a significant difference in execut
Mark P 2012/10/03 20:40:14 Okay.
43 base::TimeTicks::Now() - start_time);
44 }
45
46 BookmarkProvider::~BookmarkProvider() {}
47
48
49 void BookmarkProvider::DoAutocomplete(const AutocompleteInput& input,
50 bool best_match) {
51 // Get the matching bookmarks from the bookmark model.
52 string16 search_string = input.text();
Mark P 2012/09/28 23:10:22 It makes more sense (to me) to have these two line
mrossetti 2012/10/02 00:44:16 Good point! And I was able to get rid of the tempo
53
54 if (!bookmark_model_) {
55 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile_);
Mark P 2012/09/28 23:10:22 Is this fast enough that it can be done synchronou
mrossetti 2012/10/02 00:44:16 Right now, the BookmarkProvider will returning sug
56 if (!bookmark_model_)
57 return;
Mark P 2012/09/28 23:10:22 Is this worth a DCHECK NOT REACHED?
mrossetti 2012/10/02 00:44:16 Not really, because there might not be a bookmark
58 }
59
60 TitleMatches matches;
61 bookmark_model_->GetBookmarksWithTitlesMatching(search_string, 99, &matches);
Mark P 2012/09/28 23:10:22 Comment on the meaning of 99.
mrossetti 2012/10/02 00:44:16 Done.
62 if (!matches.size())
63 return; // There were no matches.
64
65 for (TitleMatches::const_iterator i = matches.begin(); i != matches.end();
66 ++i)
67 matches_.push_back(TitleMatchToACMatch(*i));
68
69 // Sort and clip the resulting matches.
70 size_t max_matches = best_match ? 1 : AutocompleteProvider::kMaxMatches;
71 if (matches_.size() > max_matches) {
72 std::partial_sort(matches_.begin(), matches_.end(),
73 matches_.begin() + max_matches,
74 AutocompleteMatch::MoreRelevant);
75 matches_.resize(max_matches);
76 } else {
77 std::sort(matches_.begin(), matches_.end(),
78 AutocompleteMatch::MoreRelevant);
79 }
80 }
81
82 namespace {
83
84 // for_each helper function that accumulates match term lengths and calculates
85 // position disorders.
86 class PositionFunctor {
87 public:
88 PositionFunctor() : length_(0), disorders_(0), last_pos_(0) {}
89 void operator() (const Snippet::MatchPosition& match) {
90 length_ += match.second - match.first;
91 if (last_pos_ >= match.second)
92 ++disorders_;
93 }
94 long TotalLength() { return length_; }
95 int TotalDisorders() { return disorders_; }
96
97 private:
98 long length_;
99 int disorders_;
100 size_t last_pos_;
101 };
102
103 } // namespace
104
105 AutocompleteMatch BookmarkProvider::TitleMatchToACMatch(
106 const bookmark_utils::TitleMatch& title_match) {
107 // Calculate the relevance.
108 // TODO(mpearson): Fix this very, very simplistic scoring method.
109 // Base score = 900. Add 100 for first match, 75 for next, 50, 25, 0.
110 const int kMatchCountBoost[4] = { 100, 175, 225, 250 };
111 size_t position_count =
112 std::min<size_t>(4, title_match.match_positions.size());
113 int relevance = 900 + kMatchCountBoost[position_count];
114 // Add 1199 - score * total match length / title length
Mark P 2012/09/28 00:55:13 I can't follow this code from 114-125. Perhaps in
mrossetti 2012/10/02 00:44:16 I threw in a very simple scoring algorithm here as
Mark P 2012/10/03 20:40:14 I don't think I can. :) Your approach sounds rea
115 PositionFunctor position_functor =
116 for_each(title_match.match_positions.begin(),
117 title_match.match_positions.end(), PositionFunctor());
118 int adjuster =
119 static_cast<int>((
120 static_cast<long>(AutocompleteResult::kLowestDefaultScore -
121 relevance - 1) * position_functor.TotalLength()) /
122 static_cast<long>(title_match.node->GetTitle().size()));
123 relevance += adjuster;
124 // Subtract 50 for each out-of-order match.
125 relevance -= 50 * position_functor.TotalDisorders();
126
127 AutocompleteMatch match(this, relevance, false,
128 AutocompleteMatch::BOOKMARK_TITLE);
129 const BookmarkNode& node(*(title_match.node));
130 match.destination_url = node.url();
131 match.contents = net::FormatUrl(node.url(), languages_,
132 net::kFormatUrlOmitAll & net::kFormatUrlOmitHTTP,
133 net::UnescapeRule::SPACES, NULL, NULL, NULL);
134 match.contents_class.push_back(
135 ACMatchClassification(0, ACMatchClassification::NONE));
136 match.fill_into_edit =
137 AutocompleteInput::FormattedStringWithEquivalentMeaning(node.url(),
138 match.contents);
139 match.description = node.GetTitle();
140 match.description_class =
141 SpansFromMatch(title_match.match_positions, match.description.size());
142
143 match.starred = true;
144 return match;
145 }
146
147 namespace {
148
149 bool MatchPositionSort(const Snippet::MatchPosition& p1,
150 const Snippet::MatchPosition& p2) {
151 return p1.first < p2.first;
152 }
153
154 } // namespace
155
156 // static
157 ACMatchClassifications BookmarkProvider::SpansFromMatch(
158 const Snippet::MatchPositions& positions,
159 size_t text_length) {
160 ACMatchClassifications spans;
161 if (positions.size() == 0) {
162 spans.push_back(ACMatchClassification(0, ACMatchClassification::NONE));
163 return spans;
164 }
165
166 Snippet::MatchPositions distinct_positions;
167 if (positions.size() == 1) {
168 distinct_positions = positions; // Can't use swap: positions is const.
169 } else {
170 // Sort the positions.
171 Snippet::MatchPositions sorted_positions(positions);
172 std::sort(sorted_positions.begin(), sorted_positions.end(),
173 MatchPositionSort);
174
175 // Collapse any positions that overlap.
176 Snippet::MatchPositions::const_iterator a_iter = sorted_positions.begin();
177 Snippet::MatchPosition a_pos(*a_iter);
178 for (Snippet::MatchPositions::const_iterator b_iter = ++a_iter;
179 b_iter != sorted_positions.end(); ++b_iter) {
180 if (a_pos.second >= b_iter->first) {
181 // Need collapsing.
182 a_pos.second = b_iter->second;
183 } else {
184 // |a| stands on its own.
185 distinct_positions.push_back(a_pos);
186 a_iter = b_iter;
187 a_pos = *a_iter;
188 }
189 }
190 distinct_positions.push_back(a_pos);
191 }
192
193 // Convert the positions into ACMatchClassifications.
194 Snippet::MatchPositions::const_iterator p = distinct_positions.begin();
195 if (p->first > 0)
196 spans.push_back(ACMatchClassification(0, ACMatchClassification::NONE));
197 for (; p != distinct_positions.end(); ++p) {
198 spans.push_back(ACMatchClassification(p->first,
199 ACMatchClassification::MATCH));
200 if (p->second < text_length) {
201 spans.push_back(ACMatchClassification(p->second,
202 ACMatchClassification::NONE));
203 }
204 }
205
206 return spans;
207 }
Mark P 2012/09/28 23:10:22 I don't see you ever touching |done_|. Does it ge
mrossetti 2012/10/02 00:44:16 Excellent question. |done_| is always true unless
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698