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

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

Issue 229733004: Omnibox: Make Bookmarks Set Inline_Autocompletion (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: expand shortcuts test Created 6 years, 8 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 "chrome/browser/autocomplete/autocomplete_result.h" 13 #include "chrome/browser/autocomplete/autocomplete_result.h"
14 #include "chrome/browser/autocomplete/history_provider.h"
15 #include "chrome/browser/autocomplete/url_prefix.h"
13 #include "chrome/browser/bookmarks/bookmark_model.h" 16 #include "chrome/browser/bookmarks/bookmark_model.h"
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 17 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
15 #include "chrome/browser/bookmarks/bookmark_title_match.h" 18 #include "chrome/browser/bookmarks/bookmark_title_match.h"
16 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
18 #include "net/base/net_util.h" 21 #include "net/base/net_util.h"
19 22
20 typedef std::vector<BookmarkTitleMatch> TitleMatches; 23 typedef std::vector<BookmarkTitleMatch> TitleMatches;
21 24
22 // BookmarkProvider ------------------------------------------------------------ 25 // BookmarkProvider ------------------------------------------------------------
23 26
24 BookmarkProvider::BookmarkProvider( 27 BookmarkProvider::BookmarkProvider(
25 AutocompleteProviderListener* listener, 28 AutocompleteProviderListener* listener,
26 Profile* profile) 29 Profile* profile)
27 : AutocompleteProvider(listener, profile, 30 : AutocompleteProvider(listener, profile,
28 AutocompleteProvider::TYPE_BOOKMARK), 31 AutocompleteProvider::TYPE_BOOKMARK),
29 bookmark_model_(NULL) { 32 bookmark_model_(NULL) {
30 if (profile) { 33 if (profile) {
31 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile); 34 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile);
32 languages_ = profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); 35 languages_ = profile_->GetPrefs()->GetString(prefs::kAcceptLanguages);
33 } 36 }
34 } 37 }
35 38
36 void BookmarkProvider::Start(const AutocompleteInput& input, 39 void BookmarkProvider::Start(const AutocompleteInput& input,
37 bool minimal_changes) { 40 bool minimal_changes) {
38 if (minimal_changes) 41 if (minimal_changes)
39 return; 42 return;
40 matches_.clear(); 43 matches_.clear();
41 44
42 // Short-circuit any matching when inline autocompletion is disabled and
43 // we're looking for BEST_MATCH because none of the BookmarkProvider's
44 // matches can score high enough to qualify.
45 if (input.text().empty() || 45 if (input.text().empty() ||
46 ((input.type() != AutocompleteInput::UNKNOWN) && 46 ((input.type() != AutocompleteInput::UNKNOWN) &&
47 (input.type() != AutocompleteInput::QUERY)) || 47 (input.type() != AutocompleteInput::QUERY)))
48 ((input.matches_requested() == AutocompleteInput::BEST_MATCH) &&
Peter Kasting 2014/04/10 00:22:35 Man. Are there legitimate uses of BEST_MATCH left
Mark P 2014/04/10 17:10:02 Interesting question. The only legitimate uses I
Peter Kasting 2014/04/14 22:56:13 I believe if we tried to remove this distinction e
Mark P 2014/04/14 23:28:15 I gave you the two possible ways to simplify it.
49 input.prevent_inline_autocomplete()))
50 return; 48 return;
51 49
52 DoAutocomplete(input, 50 DoAutocomplete(input,
53 input.matches_requested() == AutocompleteInput::BEST_MATCH); 51 input.matches_requested() == AutocompleteInput::BEST_MATCH);
54 } 52 }
55 53
56 BookmarkProvider::~BookmarkProvider() {} 54 BookmarkProvider::~BookmarkProvider() {}
57 55
58 void BookmarkProvider::DoAutocomplete(const AutocompleteInput& input, 56 void BookmarkProvider::DoAutocomplete(const AutocompleteInput& input,
59 bool best_match) { 57 bool best_match) {
(...skipping 27 matching lines...) Expand all
87 // for all matched terms. 85 // for all matched terms.
88 // 86 //
89 // Please refer to the code for BookmarkIndex::GetBookmarksWithTitlesMatching 87 // Please refer to the code for BookmarkIndex::GetBookmarksWithTitlesMatching
90 // for complete details of how title searches are performed against the user's 88 // for complete details of how title searches are performed against the user's
91 // bookmarks. 89 // bookmarks.
92 bookmark_model_->GetBookmarksWithTitlesMatching(input.text(), 90 bookmark_model_->GetBookmarksWithTitlesMatching(input.text(),
93 kMaxBookmarkMatches, 91 kMaxBookmarkMatches,
94 &matches); 92 &matches);
95 if (matches.empty()) 93 if (matches.empty())
96 return; // There were no matches. 94 return; // There were no matches.
95 AutocompleteInput fixed_up_input(input);
96 FixupUserInput(&fixed_up_input);
97 for (TitleMatches::const_iterator i = matches.begin(); i != matches.end(); 97 for (TitleMatches::const_iterator i = matches.begin(); i != matches.end();
98 ++i) { 98 ++i) {
99 // Create and score the AutocompleteMatch. If its score is 0 then the 99 // Create and score the AutocompleteMatch. If its score is 0 then the
100 // match is discarded. 100 // match is discarded.
101 AutocompleteMatch match(TitleMatchToACMatch(*i)); 101 AutocompleteMatch match(TitleMatchToACMatch(input, fixed_up_input, *i));
102 if (match.relevance > 0) 102 if (match.relevance > 0)
103 matches_.push_back(match); 103 matches_.push_back(match);
104 } 104 }
105 105
106 // Sort and clip the resulting matches. 106 // Sort and clip the resulting matches.
107 size_t max_matches = best_match ? 1 : AutocompleteProvider::kMaxMatches; 107 size_t max_matches = best_match ? 1 : AutocompleteProvider::kMaxMatches;
108 if (matches_.size() > max_matches) { 108 if (matches_.size() > max_matches) {
109 std::partial_sort(matches_.begin(), 109 std::partial_sort(matches_.begin(),
110 matches_.begin() + max_matches, 110 matches_.begin() + max_matches,
111 matches_.end(), 111 matches_.end(),
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 double ScoringFactor() { return scoring_factor_; } 146 double ScoringFactor() { return scoring_factor_; }
147 147
148 private: 148 private:
149 double title_length_; 149 double title_length_;
150 double scoring_factor_; 150 double scoring_factor_;
151 }; 151 };
152 152
153 } // namespace 153 } // namespace
154 154
155 AutocompleteMatch BookmarkProvider::TitleMatchToACMatch( 155 AutocompleteMatch BookmarkProvider::TitleMatchToACMatch(
156 const AutocompleteInput& input,
157 const AutocompleteInput& fixed_up_input,
156 const BookmarkTitleMatch& title_match) { 158 const BookmarkTitleMatch& title_match) {
157 // The AutocompleteMatch we construct is non-deletable because the only 159 // The AutocompleteMatch we construct is non-deletable because the only
158 // way to support this would be to delete the underlying bookmark, which is 160 // way to support this would be to delete the underlying bookmark, which is
159 // unlikely to be what the user intends. 161 // unlikely to be what the user intends.
160 AutocompleteMatch match(this, 0, false, 162 AutocompleteMatch match(this, 0, false,
161 AutocompleteMatchType::BOOKMARK_TITLE); 163 AutocompleteMatchType::BOOKMARK_TITLE);
162 const base::string16& title(title_match.node->GetTitle()); 164 const base::string16& title(title_match.node->GetTitle());
163 DCHECK(!title.empty()); 165 DCHECK(!title.empty());
166
164 const GURL& url(title_match.node->url()); 167 const GURL& url(title_match.node->url());
168 const base::string16& url_utf16 = base::UTF8ToUTF16(url.spec());
169 size_t match_start, inline_autocomplete_offset;
170 URLPrefix::ComputeMatchStartAndInlineAutocompleteOffset(input, fixed_up_input,
171 false, url_utf16, &match_start, &inline_autocomplete_offset);
Peter Kasting 2014/04/10 00:22:35 Nit: All lines of args should begin at the same po
Mark P 2014/04/10 17:10:02 Done.
165 match.destination_url = url; 172 match.destination_url = url;
173 const bool trim_http = !AutocompleteInput::HasHTTPScheme(input.text()) &&
174 ((match_start == base::string16::npos) || (match_start != 0));
166 match.contents = net::FormatUrl(url, languages_, 175 match.contents = net::FormatUrl(url, languages_,
167 net::kFormatUrlOmitAll & net::kFormatUrlOmitHTTP, 176 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP),
168 net::UnescapeRule::SPACES, NULL, NULL, NULL); 177 net::UnescapeRule::SPACES, NULL, NULL, &inline_autocomplete_offset);
169 match.contents_class.push_back( 178 match.contents_class.push_back(
170 ACMatchClassification(0, ACMatchClassification::URL)); 179 ACMatchClassification(0, ACMatchClassification::URL));
171 match.fill_into_edit = 180 match.fill_into_edit =
172 AutocompleteInput::FormattedStringWithEquivalentMeaning(url, 181 AutocompleteInput::FormattedStringWithEquivalentMeaning(url,
173 match.contents); 182 match.contents);
183 if (inline_autocomplete_offset != base::string16::npos) {
184 // |inline_autocomplete_offset| may be beyond the end of the
185 // |fill_into_edit| if the user has typed an URL with a scheme and the
186 // last character typed is a slash. That slash is removed by the
187 // FormatURLWithOffsets call above.
188 if (inline_autocomplete_offset < match.fill_into_edit.length()) {
189 match.inline_autocompletion =
190 match.fill_into_edit.substr(inline_autocomplete_offset);
191 }
192 match.allowed_to_be_default_match = match.inline_autocompletion.empty() ||
193 !HistoryProvider::PreventInlineAutocomplete(input);
194 }
174 match.description = title; 195 match.description = title;
175 match.description_class = 196 match.description_class =
176 ClassificationsFromMatch(title_match.match_positions, 197 ClassificationsFromMatch(title_match.match_positions,
177 match.description.size()); 198 match.description.size());
178 match.starred = true; 199 match.starred = true;
179 200
180 // Summary on how a relevance score is determined for the match: 201 // Summary on how a relevance score is determined for the match:
181 // 202 //
182 // For each term matching within the bookmark's title (as given by the set of 203 // For each term matching within the bookmark's title (as given by the set of
183 // Snippet::MatchPositions) calculate a 'factor', sum up those factors, then 204 // Snippet::MatchPositions) calculate a 'factor', sum up those factors, then
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 for (Snippet::MatchPositions::const_iterator i = positions.begin(); 286 for (Snippet::MatchPositions::const_iterator i = positions.begin();
266 i != positions.end(); ++i) { 287 i != positions.end(); ++i) {
267 AutocompleteMatch::ACMatchClassifications new_class; 288 AutocompleteMatch::ACMatchClassifications new_class;
268 AutocompleteMatch::ClassifyLocationInString(i->first, i->second - i->first, 289 AutocompleteMatch::ClassifyLocationInString(i->first, i->second - i->first,
269 text_length, 0, &new_class); 290 text_length, 0, &new_class);
270 classifications = AutocompleteMatch::MergeClassifications( 291 classifications = AutocompleteMatch::MergeClassifications(
271 classifications, new_class); 292 classifications, new_class);
272 } 293 }
273 return classifications; 294 return classifications;
274 } 295 }
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/bookmark_provider.h ('k') | chrome/browser/autocomplete/bookmark_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698