OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/logging.h" | 5 #include "base/logging.h" |
6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
7 #include "chrome/browser/autocomplete/autocomplete_match.h" | 7 #include "chrome/browser/autocomplete/autocomplete_match.h" |
8 #include "grit/theme_resources.h" | 8 #include "grit/theme_resources.h" |
9 | 9 |
10 // AutocompleteMatch ---------------------------------------------------------- | 10 // AutocompleteMatch ---------------------------------------------------------- |
(...skipping 20 matching lines...) Expand all Loading... |
31 deletable(deletable), | 31 deletable(deletable), |
32 inline_autocomplete_offset(string16::npos), | 32 inline_autocomplete_offset(string16::npos), |
33 transition(content::PAGE_TRANSITION_TYPED), | 33 transition(content::PAGE_TRANSITION_TYPED), |
34 is_history_what_you_typed_match(false), | 34 is_history_what_you_typed_match(false), |
35 type(type), | 35 type(type), |
36 template_url(NULL), | 36 template_url(NULL), |
37 starred(false), | 37 starred(false), |
38 from_previous(false) { | 38 from_previous(false) { |
39 } | 39 } |
40 | 40 |
| 41 AutocompleteMatch::AutocompleteMatch(const AutocompleteMatch& match) |
| 42 : provider(match.provider), |
| 43 relevance(match.relevance), |
| 44 deletable(match.deletable), |
| 45 fill_into_edit(match.fill_into_edit), |
| 46 inline_autocomplete_offset(match.inline_autocomplete_offset), |
| 47 destination_url(match.destination_url), |
| 48 stripped_destination_url(match.stripped_destination_url), |
| 49 contents(match.contents), |
| 50 contents_class(match.contents_class), |
| 51 description(match.description), |
| 52 description_class(match.description_class), |
| 53 transition(match.transition), |
| 54 is_history_what_you_typed_match(match.is_history_what_you_typed_match), |
| 55 type(match.type), |
| 56 keyword(match.keyword), |
| 57 template_url(match.template_url), |
| 58 starred(match.starred), |
| 59 from_previous(match.from_previous) { |
| 60 if (match.associated_keyword.get()) |
| 61 associated_keyword.reset(new AutocompleteMatch(*match.associated_keyword)); |
| 62 } |
| 63 |
41 AutocompleteMatch::~AutocompleteMatch() { | 64 AutocompleteMatch::~AutocompleteMatch() { |
42 } | 65 } |
43 | 66 |
| 67 AutocompleteMatch& AutocompleteMatch::operator=( |
| 68 const AutocompleteMatch& match) { |
| 69 if (this == &match) |
| 70 return *this; |
| 71 |
| 72 provider = match.provider; |
| 73 relevance = match.relevance; |
| 74 deletable = match.deletable; |
| 75 fill_into_edit = match.fill_into_edit; |
| 76 inline_autocomplete_offset = match.inline_autocomplete_offset; |
| 77 destination_url = match.destination_url; |
| 78 stripped_destination_url = match.stripped_destination_url; |
| 79 contents = match.contents; |
| 80 contents_class = match.contents_class; |
| 81 description = match.description; |
| 82 description_class = match.description_class; |
| 83 transition = match.transition; |
| 84 is_history_what_you_typed_match = match.is_history_what_you_typed_match; |
| 85 type = match.type; |
| 86 associated_keyword.reset(match.associated_keyword.get() ? |
| 87 new AutocompleteMatch(*match.associated_keyword) : NULL); |
| 88 keyword = match.keyword; |
| 89 template_url = match.template_url; |
| 90 starred = match.starred; |
| 91 from_previous = match.from_previous; |
| 92 |
| 93 return *this; |
| 94 } |
| 95 |
44 // static | 96 // static |
45 std::string AutocompleteMatch::TypeToString(Type type) { | 97 std::string AutocompleteMatch::TypeToString(Type type) { |
46 const char* strings[] = { | 98 const char* strings[] = { |
47 "url-what-you-typed", | 99 "url-what-you-typed", |
48 "history-url", | 100 "history-url", |
49 "history-title", | 101 "history-title", |
50 "history-body", | 102 "history-body", |
51 "history-keyword", | 103 "history-keyword", |
52 "navsuggest", | 104 "navsuggest", |
53 "search-what-you-typed", | 105 "search-what-you-typed", |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 144 |
93 return elem1.relevance > elem2.relevance; | 145 return elem1.relevance > elem2.relevance; |
94 } | 146 } |
95 | 147 |
96 // static | 148 // static |
97 bool AutocompleteMatch::DestinationSortFunc(const AutocompleteMatch& elem1, | 149 bool AutocompleteMatch::DestinationSortFunc(const AutocompleteMatch& elem1, |
98 const AutocompleteMatch& elem2) { | 150 const AutocompleteMatch& elem2) { |
99 // Sort identical destination_urls together. Place the most relevant matches | 151 // Sort identical destination_urls together. Place the most relevant matches |
100 // first, so that when we call std::unique(), these are the ones that get | 152 // first, so that when we call std::unique(), these are the ones that get |
101 // preserved. | 153 // preserved. |
102 return (elem1.destination_url != elem2.destination_url) ? | 154 return (elem1.stripped_destination_url != elem2.stripped_destination_url) ? |
103 (elem1.destination_url < elem2.destination_url) : | 155 (elem1.stripped_destination_url < elem2.stripped_destination_url) : |
104 MoreRelevant(elem1, elem2); | 156 MoreRelevant(elem1, elem2); |
105 } | 157 } |
106 | 158 |
107 // static | 159 // static |
108 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, | 160 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, |
109 const AutocompleteMatch& elem2) { | 161 const AutocompleteMatch& elem2) { |
110 return elem1.destination_url == elem2.destination_url; | 162 return elem1.stripped_destination_url == elem2.stripped_destination_url; |
111 } | 163 } |
112 | 164 |
113 // static | 165 // static |
114 void AutocompleteMatch::ClassifyMatchInString( | 166 void AutocompleteMatch::ClassifyMatchInString( |
115 const string16& find_text, | 167 const string16& find_text, |
116 const string16& text, | 168 const string16& text, |
117 int style, | 169 int style, |
118 ACMatchClassifications* classification) { | 170 ACMatchClassifications* classification) { |
119 ClassifyLocationInString(text.find(find_text), find_text.length(), | 171 ClassifyLocationInString(text.find(find_text), find_text.length(), |
120 text.length(), style, classification); | 172 text.length(), style, classification); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 const char16 kRemoveChars[] = { '\n', '\r', '\t', | 217 const char16 kRemoveChars[] = { '\n', '\r', '\t', |
166 0x2028, // Line separator | 218 0x2028, // Line separator |
167 0x2029, // Paragraph separator | 219 0x2029, // Paragraph separator |
168 0 }; | 220 0 }; |
169 string16 result; | 221 string16 result; |
170 TrimWhitespace(text, TRIM_LEADING, &result); | 222 TrimWhitespace(text, TRIM_LEADING, &result); |
171 RemoveChars(result, kRemoveChars, &result); | 223 RemoveChars(result, kRemoveChars, &result); |
172 return result; | 224 return result; |
173 } | 225 } |
174 | 226 |
| 227 void AutocompleteMatch::ComputeStrippedDestinationURL() { |
| 228 static const char prefix[] = "www."; |
| 229 static const size_t prefix_len = arraysize(prefix) - 1; |
| 230 |
| 231 std::string host = destination_url.host(); |
| 232 if (destination_url.is_valid() && host.compare(0, prefix_len, prefix) == 0) { |
| 233 host = host.substr(prefix_len); |
| 234 GURL::Replacements replace_host; |
| 235 replace_host.SetHostStr(host); |
| 236 stripped_destination_url = destination_url.ReplaceComponents(replace_host); |
| 237 } else { |
| 238 stripped_destination_url = destination_url; |
| 239 } |
| 240 } |
| 241 |
175 #ifndef NDEBUG | 242 #ifndef NDEBUG |
176 void AutocompleteMatch::Validate() const { | 243 void AutocompleteMatch::Validate() const { |
177 ValidateClassifications(contents, contents_class); | 244 ValidateClassifications(contents, contents_class); |
178 ValidateClassifications(description, description_class); | 245 ValidateClassifications(description, description_class); |
179 } | 246 } |
180 | 247 |
181 void AutocompleteMatch::ValidateClassifications( | 248 void AutocompleteMatch::ValidateClassifications( |
182 const string16& text, | 249 const string16& text, |
183 const ACMatchClassifications& classifications) const { | 250 const ACMatchClassifications& classifications) const { |
184 if (text.empty()) { | 251 if (text.empty()) { |
(...skipping 10 matching lines...) Expand all Loading... |
195 // The classifications should always be sorted. | 262 // The classifications should always be sorted. |
196 size_t last_offset = classifications[0].offset; | 263 size_t last_offset = classifications[0].offset; |
197 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); | 264 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); |
198 i != classifications.end(); ++i) { | 265 i != classifications.end(); ++i) { |
199 DCHECK(i->offset > last_offset) << "Classification unsorted"; | 266 DCHECK(i->offset > last_offset) << "Classification unsorted"; |
200 DCHECK(i->offset < text.length()) << "Classification out of bounds"; | 267 DCHECK(i->offset < text.length()) << "Classification out of bounds"; |
201 last_offset = i->offset; | 268 last_offset = i->offset; |
202 } | 269 } |
203 } | 270 } |
204 #endif | 271 #endif |
OLD | NEW |