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/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) && | |
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 Loading... |
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 Loading... |
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( |
| 171 input, fixed_up_input, false, url_utf16, &match_start, |
| 172 &inline_autocomplete_offset); |
165 match.destination_url = url; | 173 match.destination_url = url; |
| 174 const bool trim_http = !AutocompleteInput::HasHTTPScheme(input.text()) && |
| 175 ((match_start == base::string16::npos) || (match_start != 0)); |
166 match.contents = net::FormatUrl(url, languages_, | 176 match.contents = net::FormatUrl(url, languages_, |
167 net::kFormatUrlOmitAll & net::kFormatUrlOmitHTTP, | 177 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP), |
168 net::UnescapeRule::SPACES, NULL, NULL, NULL); | 178 net::UnescapeRule::SPACES, NULL, NULL, &inline_autocomplete_offset); |
169 match.contents_class.push_back( | 179 match.contents_class.push_back( |
170 ACMatchClassification(0, ACMatchClassification::URL)); | 180 ACMatchClassification(0, ACMatchClassification::URL)); |
171 match.fill_into_edit = | 181 match.fill_into_edit = |
172 AutocompleteInput::FormattedStringWithEquivalentMeaning(url, | 182 AutocompleteInput::FormattedStringWithEquivalentMeaning(url, |
173 match.contents); | 183 match.contents); |
| 184 if (inline_autocomplete_offset != base::string16::npos) { |
| 185 // |inline_autocomplete_offset| may be beyond the end of the |
| 186 // |fill_into_edit| if the user has typed an URL with a scheme and the |
| 187 // last character typed is a slash. That slash is removed by the |
| 188 // FormatURLWithOffsets call above. |
| 189 if (inline_autocomplete_offset < match.fill_into_edit.length()) { |
| 190 match.inline_autocompletion = |
| 191 match.fill_into_edit.substr(inline_autocomplete_offset); |
| 192 } |
| 193 match.allowed_to_be_default_match = match.inline_autocompletion.empty() || |
| 194 !HistoryProvider::PreventInlineAutocomplete(input); |
| 195 } |
174 match.description = title; | 196 match.description = title; |
175 match.description_class = | 197 match.description_class = |
176 ClassificationsFromMatch(title_match.match_positions, | 198 ClassificationsFromMatch(title_match.match_positions, |
177 match.description.size()); | 199 match.description.size()); |
178 match.starred = true; | 200 match.starred = true; |
179 | 201 |
180 // Summary on how a relevance score is determined for the match: | 202 // Summary on how a relevance score is determined for the match: |
181 // | 203 // |
182 // For each term matching within the bookmark's title (as given by the set of | 204 // 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 | 205 // Snippet::MatchPositions) calculate a 'factor', sum up those factors, then |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 for (Snippet::MatchPositions::const_iterator i = positions.begin(); | 287 for (Snippet::MatchPositions::const_iterator i = positions.begin(); |
266 i != positions.end(); ++i) { | 288 i != positions.end(); ++i) { |
267 AutocompleteMatch::ACMatchClassifications new_class; | 289 AutocompleteMatch::ACMatchClassifications new_class; |
268 AutocompleteMatch::ClassifyLocationInString(i->first, i->second - i->first, | 290 AutocompleteMatch::ClassifyLocationInString(i->first, i->second - i->first, |
269 text_length, 0, &new_class); | 291 text_length, 0, &new_class); |
270 classifications = AutocompleteMatch::MergeClassifications( | 292 classifications = AutocompleteMatch::MergeClassifications( |
271 classifications, new_class); | 293 classifications, new_class); |
272 } | 294 } |
273 return classifications; | 295 return classifications; |
274 } | 296 } |
OLD | NEW |