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

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, 2 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 // 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();
37
38 // Only bother with UNKNOWN and QUERY.
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);
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_) {
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_);
Mark P 2012/10/03 20:40:14 This sounds fine to me. Where is the 3 set in cod
mrossetti 2012/10/05 22:10:04 Yes, because if the bookmark model has not been lo
60 if (!bookmark_model_)
61 return;
62 }
63
64 TitleMatches matches;
65 // Retrieve enough bookmarks so that we can reasonably score the best.
Mark P 2012/10/03 20:40:14 we can reasonably score the best -> we have a reas
mrossetti 2012/10/05 22:10:04 Done.
66 const size_t kMaxBookmarkMatches = 50;
67 bookmark_model_->GetBookmarksWithTitlesMatching(input.text(),
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 {
Mark P 2012/10/03 20:40:14 When / how does last_pos_ get set?
mrossetti 2012/10/05 22:10:04 Good catch! It _was_ in there, I swear. I'll add
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 long TotalLength() { return length_; }
103 int TotalDisorders() { return disorders_; }
104
105 private:
106 long length_;
107 int disorders_;
108 size_t last_pos_;
109 };
Mark P 2012/10/03 20:40:14 What will happen with an omnibox input foobar foob
mrossetti 2012/10/05 22:10:04 The problem with relying on comments is that it's
110
111 } // namespace
112
113 AutocompleteMatch BookmarkProvider::TitleMatchToACMatch(
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.
Mark P 2012/10/03 20:40:14 I like boosting for matching the URL in multiple *
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
Mark P 2012/10/03 20:40:14 I don't like this idea of using the magic number 1
123 PositionFunctor position_functor =
124 for_each(title_match.match_positions.begin(),
125 title_match.match_positions.end(), PositionFunctor());
126 int adjuster =
127 static_cast<int>((
128 static_cast<long>(AutocompleteResult::kLowestDefaultScore -
129 relevance - 1) * position_functor.TotalLength()) /
130 static_cast<long>(title_match.node->GetTitle().size()));
131 relevance += adjuster;
132 // Subtract 50 for each out-of-order match.
133 relevance -= 50 * position_functor.TotalDisorders();
Mark P 2012/10/03 20:40:14 Lines 132-133 are okay for now. There's one thing
134
135 AutocompleteMatch match(this, relevance, false,
Mark P 2012/10/03 20:40:14 comment on the meaning of false
mrossetti 2012/10/05 22:10:04 That's documented in the header comments for the A
136 AutocompleteMatch::BOOKMARK_TITLE);
137 const BookmarkNode& node(*(title_match.node));
138 match.destination_url = node.url();
139 match.contents = net::FormatUrl(node.url(), languages_,
140 net::kFormatUrlOmitAll & net::kFormatUrlOmitHTTP,
141 net::UnescapeRule::SPACES, NULL, NULL, NULL);
142 match.contents_class.push_back(
143 ACMatchClassification(0, ACMatchClassification::NONE));
144 match.fill_into_edit =
145 AutocompleteInput::FormattedStringWithEquivalentMeaning(node.url(),
146 match.contents);
147 match.description = node.GetTitle();
148 match.description_class =
149 SpansFromMatch(title_match.match_positions, match.description.size());
150
151 match.starred = true;
152 return match;
153 }
154
155 namespace {
156
157 bool MatchPositionSort(const Snippet::MatchPosition& p1,
158 const Snippet::MatchPosition& p2) {
159 return p1.first < p2.first;
160 }
161
162 } // namespace
163
164 // static
165 ACMatchClassifications BookmarkProvider::SpansFromMatch(
166 const Snippet::MatchPositions& positions,
167 size_t text_length) {
168 ACMatchClassifications spans;
169 if (positions.size() == 0) {
170 spans.push_back(ACMatchClassification(0, ACMatchClassification::NONE));
171 return spans;
172 }
173
174 Snippet::MatchPositions distinct_positions;
175 if (positions.size() == 1) {
176 distinct_positions = positions; // Can't use swap: positions is const.
177 } else {
178 // Sort the positions.
179 Snippet::MatchPositions sorted_positions(positions);
180 std::sort(sorted_positions.begin(), sorted_positions.end(),
181 MatchPositionSort);
182
183 // Collapse any positions that overlap.
184 Snippet::MatchPositions::const_iterator a_iter = sorted_positions.begin();
185 Snippet::MatchPosition a_pos(*a_iter);
186 for (Snippet::MatchPositions::const_iterator b_iter = ++a_iter;
Mark P 2012/10/03 20:40:14 I think you can right this loop with only one iter
mrossetti 2012/10/05 22:10:04 OK, left as-is.
187 b_iter != sorted_positions.end(); ++b_iter) {
188 if (a_pos.second >= b_iter->first) {
189 // Need collapsing.
190 a_pos.second = b_iter->second;
191 } else {
192 // |a| stands on its own.
193 distinct_positions.push_back(a_pos);
194 a_iter = b_iter;
195 a_pos = *a_iter;
196 }
197 }
198 distinct_positions.push_back(a_pos);
199 }
200
201 // Convert the positions into ACMatchClassifications.
202 Snippet::MatchPositions::const_iterator p = distinct_positions.begin();
203 if (p->first > 0)
204 spans.push_back(ACMatchClassification(0, ACMatchClassification::NONE));
205 for (; p != distinct_positions.end(); ++p) {
206 spans.push_back(ACMatchClassification(p->first,
207 ACMatchClassification::MATCH));
208 if (p->second < text_length) {
209 spans.push_back(ACMatchClassification(p->second,
210 ACMatchClassification::NONE));
211 }
212 }
213
214 return spans;
215 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698