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 "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 19 matching lines...) Expand all Loading... | |
30 deletable(deletable), | 30 deletable(deletable), |
31 inline_autocomplete_offset(string16::npos), | 31 inline_autocomplete_offset(string16::npos), |
32 transition(PageTransition::TYPED), | 32 transition(PageTransition::TYPED), |
33 is_history_what_you_typed_match(false), | 33 is_history_what_you_typed_match(false), |
34 type(type), | 34 type(type), |
35 template_url(NULL), | 35 template_url(NULL), |
36 starred(false), | 36 starred(false), |
37 from_previous(false) { | 37 from_previous(false) { |
38 } | 38 } |
39 | 39 |
40 AutocompleteMatch::AutocompleteMatch(const AutocompleteMatch& match) | |
41 : provider(match.provider), | |
42 relevance(match.relevance), | |
43 deletable(match.deletable), | |
44 inline_autocomplete_offset(match.inline_autocomplete_offset), | |
45 transition(match.transition), | |
46 is_history_what_you_typed_match(match.is_history_what_you_typed_match), | |
47 type(match.type), | |
48 keyword(match.keyword), | |
49 template_url(match.template_url), | |
50 starred(match.starred), | |
51 from_previous(match.from_previous), | |
52 fill_into_edit(match.fill_into_edit), | |
53 destination_url(match.destination_url), | |
54 stripped_destination_url(match.stripped_destination_url), | |
55 contents(match.contents), | |
56 contents_class(match.contents_class), | |
57 description(match.description), | |
58 description_class(match.description_class) { | |
59 if (match.associated_keyword.get()) | |
60 associated_keyword.reset(new AutocompleteMatch(*match.associated_keyword)); | |
61 } | |
62 | |
40 AutocompleteMatch::~AutocompleteMatch() { | 63 AutocompleteMatch::~AutocompleteMatch() { |
41 } | 64 } |
42 | 65 |
66 AutocompleteMatch& AutocompleteMatch::operator=( | |
67 const AutocompleteMatch& match) { | |
68 if (this == &match) | |
69 return *this; | |
70 | |
71 provider = match.provider; | |
72 relevance = match.relevance; | |
73 deletable = match.deletable; | |
74 inline_autocomplete_offset = match.inline_autocomplete_offset; | |
75 transition = match.transition; | |
76 is_history_what_you_typed_match = match.is_history_what_you_typed_match; | |
77 type = match.type; | |
78 keyword = match.keyword; | |
79 template_url = match.template_url; | |
80 starred = match.starred; | |
81 from_previous = match.from_previous; | |
82 fill_into_edit = match.fill_into_edit; | |
83 destination_url = match.destination_url; | |
84 stripped_destination_url = match.stripped_destination_url; | |
85 contents = match.contents; | |
86 contents_class = match.contents_class; | |
87 description = match.description; | |
88 description_class = match.description_class; | |
89 | |
90 if (match.associated_keyword.get()) | |
Peter Kasting
2011/07/27 20:18:25
Nit: Shorter:
associated_keyword.reset(match.as
| |
91 associated_keyword.reset(new AutocompleteMatch(*match.associated_keyword)); | |
92 else | |
93 associated_keyword.reset(NULL); | |
94 | |
95 return *this; | |
96 } | |
97 | |
43 // static | 98 // static |
44 std::string AutocompleteMatch::TypeToString(Type type) { | 99 std::string AutocompleteMatch::TypeToString(Type type) { |
45 const char* strings[] = { | 100 const char* strings[] = { |
46 "url-what-you-typed", | 101 "url-what-you-typed", |
47 "history-url", | 102 "history-url", |
48 "history-title", | 103 "history-title", |
49 "history-body", | 104 "history-body", |
50 "history-keyword", | 105 "history-keyword", |
51 "navsuggest", | 106 "navsuggest", |
52 "search-what-you-typed", | 107 "search-what-you-typed", |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 | 146 |
92 return elem1.relevance > elem2.relevance; | 147 return elem1.relevance > elem2.relevance; |
93 } | 148 } |
94 | 149 |
95 // static | 150 // static |
96 bool AutocompleteMatch::DestinationSortFunc(const AutocompleteMatch& elem1, | 151 bool AutocompleteMatch::DestinationSortFunc(const AutocompleteMatch& elem1, |
97 const AutocompleteMatch& elem2) { | 152 const AutocompleteMatch& elem2) { |
98 // Sort identical destination_urls together. Place the most relevant matches | 153 // Sort identical destination_urls together. Place the most relevant matches |
99 // first, so that when we call std::unique(), these are the ones that get | 154 // first, so that when we call std::unique(), these are the ones that get |
100 // preserved. | 155 // preserved. |
101 return (elem1.destination_url != elem2.destination_url) ? | 156 return (elem1.stripped_destination_url != elem2.stripped_destination_url) ? |
102 (elem1.destination_url < elem2.destination_url) : | 157 (elem1.stripped_destination_url < elem2.stripped_destination_url) : |
103 MoreRelevant(elem1, elem2); | 158 MoreRelevant(elem1, elem2); |
104 } | 159 } |
105 | 160 |
106 // static | 161 // static |
107 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, | 162 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, |
108 const AutocompleteMatch& elem2) { | 163 const AutocompleteMatch& elem2) { |
109 return elem1.destination_url == elem2.destination_url; | 164 return elem1.stripped_destination_url == elem2.stripped_destination_url; |
110 } | 165 } |
111 | 166 |
112 // static | 167 // static |
113 void AutocompleteMatch::ClassifyMatchInString( | 168 void AutocompleteMatch::ClassifyMatchInString( |
114 const string16& find_text, | 169 const string16& find_text, |
115 const string16& text, | 170 const string16& text, |
116 int style, | 171 int style, |
117 ACMatchClassifications* classification) { | 172 ACMatchClassifications* classification) { |
118 ClassifyLocationInString(text.find(find_text), find_text.length(), | 173 ClassifyLocationInString(text.find(find_text), find_text.length(), |
119 text.length(), style, classification); | 174 text.length(), style, classification); |
(...skipping 29 matching lines...) Expand all Loading... | |
149 classification->push_back(ACMatchClassification(match_location, | 204 classification->push_back(ACMatchClassification(match_location, |
150 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); | 205 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); |
151 | 206 |
152 // Mark post-match portion of string (if any). | 207 // Mark post-match portion of string (if any). |
153 const size_t after_match(match_location + match_length); | 208 const size_t after_match(match_location + match_length); |
154 if (after_match < overall_length) { | 209 if (after_match < overall_length) { |
155 classification->push_back(ACMatchClassification(after_match, style)); | 210 classification->push_back(ACMatchClassification(after_match, style)); |
156 } | 211 } |
157 } | 212 } |
158 | 213 |
214 void AutocompleteMatch::ComputeStrippedDestinationURL() { | |
215 static const char prefix[] = "www."; | |
216 static const size_t prefix_len = arraysize(prefix) - 1; | |
217 | |
218 std::string host = destination_url.host(); | |
219 if (destination_url.is_valid() && host.compare(0, prefix_len, prefix) == 0) { | |
220 host = host.substr(prefix_len); | |
221 GURL::Replacements replace_host; | |
222 replace_host.SetHostStr(host); | |
223 stripped_destination_url = | |
224 destination_url.ReplaceComponents(replace_host); | |
225 } else { | |
226 stripped_destination_url = destination_url; | |
227 } | |
228 } | |
229 | |
159 #ifndef NDEBUG | 230 #ifndef NDEBUG |
160 void AutocompleteMatch::Validate() const { | 231 void AutocompleteMatch::Validate() const { |
161 ValidateClassifications(contents, contents_class); | 232 ValidateClassifications(contents, contents_class); |
162 ValidateClassifications(description, description_class); | 233 ValidateClassifications(description, description_class); |
163 } | 234 } |
164 | 235 |
165 void AutocompleteMatch::ValidateClassifications( | 236 void AutocompleteMatch::ValidateClassifications( |
166 const string16& text, | 237 const string16& text, |
167 const ACMatchClassifications& classifications) const { | 238 const ACMatchClassifications& classifications) const { |
168 if (text.empty()) { | 239 if (text.empty()) { |
(...skipping 10 matching lines...) Expand all Loading... | |
179 // The classifications should always be sorted. | 250 // The classifications should always be sorted. |
180 size_t last_offset = classifications[0].offset; | 251 size_t last_offset = classifications[0].offset; |
181 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); | 252 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); |
182 i != classifications.end(); ++i) { | 253 i != classifications.end(); ++i) { |
183 DCHECK(i->offset > last_offset) << "Classification unsorted"; | 254 DCHECK(i->offset > last_offset) << "Classification unsorted"; |
184 DCHECK(i->offset < text.length()) << "Classification out of bounds"; | 255 DCHECK(i->offset < text.length()) << "Classification out of bounds"; |
185 last_offset = i->offset; | 256 last_offset = i->offset; |
186 } | 257 } |
187 } | 258 } |
188 #endif | 259 #endif |
OLD | NEW |