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/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
13 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 14 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" |
14 #include "chrome/browser/autocomplete/history_provider.h" | 15 #include "chrome/browser/autocomplete/history_provider.h" |
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
18 #include "components/bookmarks/browser/bookmark_match.h" | 19 #include "components/bookmarks/browser/bookmark_match.h" |
19 #include "components/bookmarks/browser/bookmark_model.h" | 20 #include "components/bookmarks/browser/bookmark_model.h" |
20 #include "components/metrics/proto/omnibox_input_type.pb.h" | 21 #include "components/metrics/proto/omnibox_input_type.pb.h" |
21 #include "components/omnibox/autocomplete_result.h" | 22 #include "components/omnibox/autocomplete_result.h" |
22 #include "components/omnibox/omnibox_field_trial.h" | 23 #include "components/omnibox/omnibox_field_trial.h" |
23 #include "components/omnibox/url_prefix.h" | 24 #include "components/omnibox/url_prefix.h" |
24 #include "net/base/net_util.h" | 25 #include "net/base/net_util.h" |
25 | 26 |
26 using bookmarks::BookmarkMatch; | 27 using bookmarks::BookmarkMatch; |
27 | 28 |
28 typedef std::vector<BookmarkMatch> BookmarkMatches; | 29 typedef std::vector<BookmarkMatch> BookmarkMatches; |
29 | 30 |
| 31 namespace { |
| 32 |
| 33 // Removes leading spaces from |title| before displaying, otherwise it looks |
| 34 // funny. In the process, corrects |title_match_positions| so the correct |
| 35 // characters are highlighted. |
| 36 void CorrectTitleAndMatchPositions( |
| 37 base::string16* title, |
| 38 BookmarkMatch::MatchPositions* title_match_positions) { |
| 39 size_t leading_whitespace_chars = title->length(); |
| 40 base::TrimWhitespace(*title, base::TRIM_LEADING, title); |
| 41 leading_whitespace_chars-= title->length(); |
| 42 if (leading_whitespace_chars == 0) |
| 43 return; |
| 44 for (query_parser::Snippet::MatchPositions::iterator it = |
| 45 title_match_positions->begin(); |
| 46 it != title_match_positions->end(); ++it) { |
| 47 (*it) = query_parser::Snippet::MatchPosition( |
| 48 it->first - leading_whitespace_chars, |
| 49 it->second - leading_whitespace_chars); |
| 50 } |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
30 // BookmarkProvider ------------------------------------------------------------ | 55 // BookmarkProvider ------------------------------------------------------------ |
31 | 56 |
32 BookmarkProvider::BookmarkProvider(Profile* profile) | 57 BookmarkProvider::BookmarkProvider(Profile* profile) |
33 : AutocompleteProvider(AutocompleteProvider::TYPE_BOOKMARK), | 58 : AutocompleteProvider(AutocompleteProvider::TYPE_BOOKMARK), |
34 profile_(profile), | 59 profile_(profile), |
35 bookmark_model_(NULL), | 60 bookmark_model_(NULL), |
36 score_using_url_matches_(OmniboxFieldTrial::BookmarksIndexURLsValue()) { | 61 score_using_url_matches_(OmniboxFieldTrial::BookmarksIndexURLsValue()) { |
37 if (profile) { | 62 if (profile) { |
38 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile); | 63 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile); |
39 languages_ = profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); | 64 languages_ = profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 AutocompleteMatch BookmarkProvider::BookmarkMatchToACMatch( | 165 AutocompleteMatch BookmarkProvider::BookmarkMatchToACMatch( |
141 const AutocompleteInput& input, | 166 const AutocompleteInput& input, |
142 const base::string16& fixed_up_input_text, | 167 const base::string16& fixed_up_input_text, |
143 const BookmarkMatch& bookmark_match) { | 168 const BookmarkMatch& bookmark_match) { |
144 // The AutocompleteMatch we construct is non-deletable because the only | 169 // The AutocompleteMatch we construct is non-deletable because the only |
145 // way to support this would be to delete the underlying bookmark, which is | 170 // way to support this would be to delete the underlying bookmark, which is |
146 // unlikely to be what the user intends. | 171 // unlikely to be what the user intends. |
147 AutocompleteMatch match(this, 0, false, | 172 AutocompleteMatch match(this, 0, false, |
148 AutocompleteMatchType::BOOKMARK_TITLE); | 173 AutocompleteMatchType::BOOKMARK_TITLE); |
149 base::string16 title(bookmark_match.node->GetTitle()); | 174 base::string16 title(bookmark_match.node->GetTitle()); |
| 175 BookmarkMatch::MatchPositions new_title_match_positions = |
| 176 bookmark_match.title_match_positions; |
| 177 CorrectTitleAndMatchPositions(&title, &new_title_match_positions); |
150 const GURL& url(bookmark_match.node->url()); | 178 const GURL& url(bookmark_match.node->url()); |
151 const base::string16& url_utf16 = base::UTF8ToUTF16(url.spec()); | 179 const base::string16& url_utf16 = base::UTF8ToUTF16(url.spec()); |
152 size_t inline_autocomplete_offset = URLPrefix::GetInlineAutocompleteOffset( | 180 size_t inline_autocomplete_offset = URLPrefix::GetInlineAutocompleteOffset( |
153 input.text(), fixed_up_input_text, false, url_utf16); | 181 input.text(), fixed_up_input_text, false, url_utf16); |
154 match.destination_url = url; | 182 match.destination_url = url; |
155 const size_t match_start = bookmark_match.url_match_positions.empty() ? | 183 const size_t match_start = bookmark_match.url_match_positions.empty() ? |
156 0 : bookmark_match.url_match_positions[0].first; | 184 0 : bookmark_match.url_match_positions[0].first; |
157 const bool trim_http = !AutocompleteInput::HasHTTPScheme(input.text()) && | 185 const bool trim_http = !AutocompleteInput::HasHTTPScheme(input.text()) && |
158 ((match_start == base::string16::npos) || (match_start != 0)); | 186 ((match_start == base::string16::npos) || (match_start != 0)); |
159 std::vector<size_t> offsets = BookmarkMatch::OffsetsFromMatchPositions( | 187 std::vector<size_t> offsets = BookmarkMatch::OffsetsFromMatchPositions( |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 i != positions.end(); | 338 i != positions.end(); |
311 ++i) { | 339 ++i) { |
312 AutocompleteMatch::ACMatchClassifications new_class; | 340 AutocompleteMatch::ACMatchClassifications new_class; |
313 AutocompleteMatch::ClassifyLocationInString(i->first, i->second - i->first, | 341 AutocompleteMatch::ClassifyLocationInString(i->first, i->second - i->first, |
314 text_length, url_style, &new_class); | 342 text_length, url_style, &new_class); |
315 classifications = AutocompleteMatch::MergeClassifications( | 343 classifications = AutocompleteMatch::MergeClassifications( |
316 classifications, new_class); | 344 classifications, new_class); |
317 } | 345 } |
318 return classifications; | 346 return classifications; |
319 } | 347 } |
OLD | NEW |