Chromium Code Reviews| 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), | |
|
sky
2011/08/01 16:02:15
Order doesn't match declaration order.
| |
| 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 associated_keyword.reset(match.associated_keyword.get() ? | |
| 90 new AutocompleteMatch(*match.associated_keyword) : NULL); | |
|
sky
2011/08/01 16:02:15
nit: indent 2 more
| |
| 91 | |
| 92 return *this; | |
| 93 } | |
| 94 | |
| 43 // static | 95 // static |
| 44 std::string AutocompleteMatch::TypeToString(Type type) { | 96 std::string AutocompleteMatch::TypeToString(Type type) { |
| 45 const char* strings[] = { | 97 const char* strings[] = { |
| 46 "url-what-you-typed", | 98 "url-what-you-typed", |
| 47 "history-url", | 99 "history-url", |
| 48 "history-title", | 100 "history-title", |
| 49 "history-body", | 101 "history-body", |
| 50 "history-keyword", | 102 "history-keyword", |
| 51 "navsuggest", | 103 "navsuggest", |
| 52 "search-what-you-typed", | 104 "search-what-you-typed", |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 | 143 |
| 92 return elem1.relevance > elem2.relevance; | 144 return elem1.relevance > elem2.relevance; |
| 93 } | 145 } |
| 94 | 146 |
| 95 // static | 147 // static |
| 96 bool AutocompleteMatch::DestinationSortFunc(const AutocompleteMatch& elem1, | 148 bool AutocompleteMatch::DestinationSortFunc(const AutocompleteMatch& elem1, |
| 97 const AutocompleteMatch& elem2) { | 149 const AutocompleteMatch& elem2) { |
| 98 // Sort identical destination_urls together. Place the most relevant matches | 150 // 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 | 151 // first, so that when we call std::unique(), these are the ones that get |
| 100 // preserved. | 152 // preserved. |
| 101 return (elem1.destination_url != elem2.destination_url) ? | 153 return (elem1.stripped_destination_url != elem2.stripped_destination_url) ? |
| 102 (elem1.destination_url < elem2.destination_url) : | 154 (elem1.stripped_destination_url < elem2.stripped_destination_url) : |
| 103 MoreRelevant(elem1, elem2); | 155 MoreRelevant(elem1, elem2); |
| 104 } | 156 } |
| 105 | 157 |
| 106 // static | 158 // static |
| 107 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, | 159 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, |
| 108 const AutocompleteMatch& elem2) { | 160 const AutocompleteMatch& elem2) { |
| 109 return elem1.destination_url == elem2.destination_url; | 161 return elem1.stripped_destination_url == elem2.stripped_destination_url; |
| 110 } | 162 } |
| 111 | 163 |
| 112 // static | 164 // static |
| 113 void AutocompleteMatch::ClassifyMatchInString( | 165 void AutocompleteMatch::ClassifyMatchInString( |
| 114 const string16& find_text, | 166 const string16& find_text, |
| 115 const string16& text, | 167 const string16& text, |
| 116 int style, | 168 int style, |
| 117 ACMatchClassifications* classification) { | 169 ACMatchClassifications* classification) { |
| 118 ClassifyLocationInString(text.find(find_text), find_text.length(), | 170 ClassifyLocationInString(text.find(find_text), find_text.length(), |
| 119 text.length(), style, classification); | 171 text.length(), style, classification); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 149 classification->push_back(ACMatchClassification(match_location, | 201 classification->push_back(ACMatchClassification(match_location, |
| 150 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); | 202 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); |
| 151 | 203 |
| 152 // Mark post-match portion of string (if any). | 204 // Mark post-match portion of string (if any). |
| 153 const size_t after_match(match_location + match_length); | 205 const size_t after_match(match_location + match_length); |
| 154 if (after_match < overall_length) { | 206 if (after_match < overall_length) { |
| 155 classification->push_back(ACMatchClassification(after_match, style)); | 207 classification->push_back(ACMatchClassification(after_match, style)); |
| 156 } | 208 } |
| 157 } | 209 } |
| 158 | 210 |
| 211 void AutocompleteMatch::ComputeStrippedDestinationURL() { | |
| 212 static const char prefix[] = "www."; | |
| 213 static const size_t prefix_len = arraysize(prefix) - 1; | |
| 214 | |
| 215 std::string host = destination_url.host(); | |
| 216 if (destination_url.is_valid() && host.compare(0, prefix_len, prefix) == 0) { | |
| 217 host = host.substr(prefix_len); | |
|
sky
2011/08/01 16:02:15
Spacing is off.
| |
| 218 GURL::Replacements replace_host; | |
| 219 replace_host.SetHostStr(host); | |
| 220 stripped_destination_url = | |
| 221 destination_url.ReplaceComponents(replace_host); | |
| 222 } else { | |
| 223 stripped_destination_url = destination_url; | |
| 224 } | |
| 225 } | |
| 226 | |
| 159 #ifndef NDEBUG | 227 #ifndef NDEBUG |
| 160 void AutocompleteMatch::Validate() const { | 228 void AutocompleteMatch::Validate() const { |
| 161 ValidateClassifications(contents, contents_class); | 229 ValidateClassifications(contents, contents_class); |
| 162 ValidateClassifications(description, description_class); | 230 ValidateClassifications(description, description_class); |
| 163 } | 231 } |
| 164 | 232 |
| 165 void AutocompleteMatch::ValidateClassifications( | 233 void AutocompleteMatch::ValidateClassifications( |
| 166 const string16& text, | 234 const string16& text, |
| 167 const ACMatchClassifications& classifications) const { | 235 const ACMatchClassifications& classifications) const { |
| 168 if (text.empty()) { | 236 if (text.empty()) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 179 // The classifications should always be sorted. | 247 // The classifications should always be sorted. |
| 180 size_t last_offset = classifications[0].offset; | 248 size_t last_offset = classifications[0].offset; |
| 181 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); | 249 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); |
| 182 i != classifications.end(); ++i) { | 250 i != classifications.end(); ++i) { |
| 183 DCHECK(i->offset > last_offset) << "Classification unsorted"; | 251 DCHECK(i->offset > last_offset) << "Classification unsorted"; |
| 184 DCHECK(i->offset < text.length()) << "Classification out of bounds"; | 252 DCHECK(i->offset < text.length()) << "Classification out of bounds"; |
| 185 last_offset = i->offset; | 253 last_offset = i->offset; |
| 186 } | 254 } |
| 187 } | 255 } |
| 188 #endif | 256 #endif |
| OLD | NEW |