OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/autocomplete/autocomplete_match.h" | 6 #include "chrome/browser/autocomplete/autocomplete_match.h" |
7 #include "grit/theme_resources.h" | 7 #include "grit/theme_resources.h" |
8 | 8 |
9 // AutocompleteMatch ---------------------------------------------------------- | 9 // AutocompleteMatch ---------------------------------------------------------- |
10 | 10 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 87 |
88 return elem1.relevance > elem2.relevance; | 88 return elem1.relevance > elem2.relevance; |
89 } | 89 } |
90 | 90 |
91 // static | 91 // static |
92 bool AutocompleteMatch::DestinationSortFunc(const AutocompleteMatch& elem1, | 92 bool AutocompleteMatch::DestinationSortFunc(const AutocompleteMatch& elem1, |
93 const AutocompleteMatch& elem2) { | 93 const AutocompleteMatch& elem2) { |
94 // Sort identical destination_urls together. Place the most relevant matches | 94 // Sort identical destination_urls together. Place the most relevant matches |
95 // first, so that when we call std::unique(), these are the ones that get | 95 // first, so that when we call std::unique(), these are the ones that get |
96 // preserved. | 96 // preserved. |
97 return (elem1.destination_url != elem2.destination_url) ? | 97 return |
98 (elem1.destination_url < elem2.destination_url) : | 98 (elem1.destination_url_normalized != elem2.destination_url_normalized) ? |
| 99 (elem1.destination_url_normalized < elem2.destination_url_normalized) : |
99 MoreRelevant(elem1, elem2); | 100 MoreRelevant(elem1, elem2); |
100 } | 101 } |
101 | 102 |
102 // static | 103 // static |
103 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, | 104 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, |
104 const AutocompleteMatch& elem2) { | 105 const AutocompleteMatch& elem2) { |
105 return elem1.destination_url == elem2.destination_url; | 106 return elem1.destination_url_normalized == elem2.destination_url_normalized; |
106 } | 107 } |
107 | 108 |
108 // static | 109 // static |
109 void AutocompleteMatch::ClassifyMatchInString( | 110 void AutocompleteMatch::ClassifyMatchInString( |
110 const string16& find_text, | 111 const string16& find_text, |
111 const string16& text, | 112 const string16& text, |
112 int style, | 113 int style, |
113 ACMatchClassifications* classification) { | 114 ACMatchClassifications* classification) { |
114 ClassifyLocationInString(text.find(find_text), find_text.length(), | 115 ClassifyLocationInString(text.find(find_text), find_text.length(), |
115 text.length(), style, classification); | 116 text.length(), style, classification); |
(...skipping 28 matching lines...) Expand all Loading... |
144 classification->push_back(ACMatchClassification(match_location, | 145 classification->push_back(ACMatchClassification(match_location, |
145 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); | 146 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); |
146 | 147 |
147 // Mark post-match portion of string (if any). | 148 // Mark post-match portion of string (if any). |
148 const size_t after_match(match_location + match_length); | 149 const size_t after_match(match_location + match_length); |
149 if (after_match < overall_length) { | 150 if (after_match < overall_length) { |
150 classification->push_back(ACMatchClassification(after_match, style)); | 151 classification->push_back(ACMatchClassification(after_match, style)); |
151 } | 152 } |
152 } | 153 } |
153 | 154 |
| 155 void AutocompleteMatch::ComputeStrippedDestinationURL() { |
| 156 static const char prefix[] = "www."; |
| 157 static const size_t prefix_len = strlen(prefix); |
| 158 |
| 159 if (!destination_url_normalized.is_empty()) |
| 160 return; |
| 161 |
| 162 std::string host = destination_url.host(); |
| 163 if (destination_url.is_valid() && host.compare(0, prefix_len, prefix) == 0) { |
| 164 host = host.substr(prefix_len); |
| 165 GURL::Replacements replace_host; |
| 166 replace_host.SetHostStr(host); |
| 167 destination_url_normalized = |
| 168 destination_url.ReplaceComponents(replace_host); |
| 169 } else { |
| 170 destination_url_normalized = destination_url; |
| 171 } |
| 172 } |
| 173 |
154 #ifndef NDEBUG | 174 #ifndef NDEBUG |
155 void AutocompleteMatch::Validate() const { | 175 void AutocompleteMatch::Validate() const { |
156 ValidateClassifications(contents, contents_class); | 176 ValidateClassifications(contents, contents_class); |
157 ValidateClassifications(description, description_class); | 177 ValidateClassifications(description, description_class); |
158 } | 178 } |
159 | 179 |
160 void AutocompleteMatch::ValidateClassifications( | 180 void AutocompleteMatch::ValidateClassifications( |
161 const string16& text, | 181 const string16& text, |
162 const ACMatchClassifications& classifications) const { | 182 const ACMatchClassifications& classifications) const { |
163 if (text.empty()) { | 183 if (text.empty()) { |
(...skipping 10 matching lines...) Expand all Loading... |
174 // The classifications should always be sorted. | 194 // The classifications should always be sorted. |
175 size_t last_offset = classifications[0].offset; | 195 size_t last_offset = classifications[0].offset; |
176 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); | 196 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); |
177 i != classifications.end(); ++i) { | 197 i != classifications.end(); ++i) { |
178 DCHECK(i->offset > last_offset) << "Classification unsorted"; | 198 DCHECK(i->offset > last_offset) << "Classification unsorted"; |
179 DCHECK(i->offset < text.length()) << "Classification out of bounds"; | 199 DCHECK(i->offset < text.length()) << "Classification out of bounds"; |
180 last_offset = i->offset; | 200 last_offset = i->offset; |
181 } | 201 } |
182 } | 202 } |
183 #endif | 203 #endif |
OLD | NEW |