| 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 |
| 11 AutocompleteMatch::AutocompleteMatch() | 11 AutocompleteMatch::AutocompleteMatch() |
| 12 : provider(NULL), | 12 : provider(NULL), |
| 13 relevance(0), | 13 relevance(0), |
| 14 deletable(false), | 14 deletable(false), |
| 15 inline_autocomplete_offset(string16::npos), | 15 inline_autocomplete_offset(string16::npos), |
| 16 transition(PageTransition::GENERATED), | 16 transition(PageTransition::GENERATED), |
| 17 is_history_what_you_typed_match(false), | 17 is_history_what_you_typed_match(false), |
| 18 type(SEARCH_WHAT_YOU_TYPED), | 18 type(SEARCH_WHAT_YOU_TYPED), |
| 19 template_url(NULL), | 19 keyword_url(NULL), |
| 20 starred(false), | 20 starred(false), |
| 21 from_previous(false) { | 21 from_previous(false), |
| 22 keyword_state(NO_KEYWORD) { |
| 22 } | 23 } |
| 23 | 24 |
| 24 AutocompleteMatch::AutocompleteMatch(AutocompleteProvider* provider, | 25 AutocompleteMatch::AutocompleteMatch(AutocompleteProvider* provider, |
| 25 int relevance, | 26 int relevance, |
| 26 bool deletable, | 27 bool deletable, |
| 27 Type type) | 28 Type type) |
| 28 : provider(provider), | 29 : provider(provider), |
| 29 relevance(relevance), | 30 relevance(relevance), |
| 30 deletable(deletable), | 31 deletable(deletable), |
| 31 inline_autocomplete_offset(string16::npos), | 32 inline_autocomplete_offset(string16::npos), |
| 32 transition(PageTransition::TYPED), | 33 transition(PageTransition::TYPED), |
| 33 is_history_what_you_typed_match(false), | 34 is_history_what_you_typed_match(false), |
| 34 type(type), | 35 type(type), |
| 35 template_url(NULL), | 36 keyword_url(NULL), |
| 36 starred(false), | 37 starred(false), |
| 37 from_previous(false) { | 38 from_previous(false), |
| 39 keyword_state(NO_KEYWORD) { |
| 38 } | 40 } |
| 39 | 41 |
| 40 AutocompleteMatch::~AutocompleteMatch() { | 42 AutocompleteMatch::~AutocompleteMatch() { |
| 41 } | 43 } |
| 42 | 44 |
| 43 // static | 45 // static |
| 44 std::string AutocompleteMatch::TypeToString(Type type) { | 46 std::string AutocompleteMatch::TypeToString(Type type) { |
| 45 const char* strings[NUM_TYPES] = { | 47 const char* strings[NUM_TYPES] = { |
| 46 "url-what-you-typed", | 48 "url-what-you-typed", |
| 47 "history-url", | 49 "history-url", |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 89 |
| 88 return elem1.relevance > elem2.relevance; | 90 return elem1.relevance > elem2.relevance; |
| 89 } | 91 } |
| 90 | 92 |
| 91 // static | 93 // static |
| 92 bool AutocompleteMatch::DestinationSortFunc(const AutocompleteMatch& elem1, | 94 bool AutocompleteMatch::DestinationSortFunc(const AutocompleteMatch& elem1, |
| 93 const AutocompleteMatch& elem2) { | 95 const AutocompleteMatch& elem2) { |
| 94 // Sort identical destination_urls together. Place the most relevant matches | 96 // 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 | 97 // first, so that when we call std::unique(), these are the ones that get |
| 96 // preserved. | 98 // preserved. |
| 97 return (elem1.destination_url != elem2.destination_url) ? | 99 return (elem1.stripped_destination_url != elem2.stripped_destination_url) ? |
| 98 (elem1.destination_url < elem2.destination_url) : | 100 (elem1.stripped_destination_url < elem2.stripped_destination_url) : |
| 99 MoreRelevant(elem1, elem2); | 101 MoreRelevant(elem1, elem2); |
| 100 } | 102 } |
| 101 | 103 |
| 102 // static | 104 // static |
| 103 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, | 105 bool AutocompleteMatch::DestinationsEqual(const AutocompleteMatch& elem1, |
| 104 const AutocompleteMatch& elem2) { | 106 const AutocompleteMatch& elem2) { |
| 105 return elem1.destination_url == elem2.destination_url; | 107 return elem1.stripped_destination_url == elem2.stripped_destination_url; |
| 106 } | 108 } |
| 107 | 109 |
| 108 // static | 110 // static |
| 109 void AutocompleteMatch::ClassifyMatchInString( | 111 void AutocompleteMatch::ClassifyMatchInString( |
| 110 const string16& find_text, | 112 const string16& find_text, |
| 111 const string16& text, | 113 const string16& text, |
| 112 int style, | 114 int style, |
| 113 ACMatchClassifications* classification) { | 115 ACMatchClassifications* classification) { |
| 114 ClassifyLocationInString(text.find(find_text), find_text.length(), | 116 ClassifyLocationInString(text.find(find_text), find_text.length(), |
| 115 text.length(), style, classification); | 117 text.length(), style, classification); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 144 classification->push_back(ACMatchClassification(match_location, | 146 classification->push_back(ACMatchClassification(match_location, |
| 145 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); | 147 (style | ACMatchClassification::MATCH) & ~ACMatchClassification::DIM)); |
| 146 | 148 |
| 147 // Mark post-match portion of string (if any). | 149 // Mark post-match portion of string (if any). |
| 148 const size_t after_match(match_location + match_length); | 150 const size_t after_match(match_location + match_length); |
| 149 if (after_match < overall_length) { | 151 if (after_match < overall_length) { |
| 150 classification->push_back(ACMatchClassification(after_match, style)); | 152 classification->push_back(ACMatchClassification(after_match, style)); |
| 151 } | 153 } |
| 152 } | 154 } |
| 153 | 155 |
| 156 void AutocompleteMatch::ComputeStrippedDestinationURL() { |
| 157 static const char prefix[] = "www."; |
| 158 static const size_t prefix_len = arraysize(prefix) - 1; |
| 159 |
| 160 std::string host = destination_url.host(); |
| 161 if (destination_url.is_valid() && host.compare(0, prefix_len, prefix) == 0) { |
| 162 host = host.substr(prefix_len); |
| 163 GURL::Replacements replace_host; |
| 164 replace_host.SetHostStr(host); |
| 165 stripped_destination_url = |
| 166 destination_url.ReplaceComponents(replace_host); |
| 167 } else { |
| 168 stripped_destination_url = destination_url; |
| 169 } |
| 170 } |
| 171 |
| 154 #ifndef NDEBUG | 172 #ifndef NDEBUG |
| 155 void AutocompleteMatch::Validate() const { | 173 void AutocompleteMatch::Validate() const { |
| 156 ValidateClassifications(contents, contents_class); | 174 ValidateClassifications(contents, contents_class); |
| 157 ValidateClassifications(description, description_class); | 175 ValidateClassifications(description, description_class); |
| 158 } | 176 } |
| 159 | 177 |
| 160 void AutocompleteMatch::ValidateClassifications( | 178 void AutocompleteMatch::ValidateClassifications( |
| 161 const string16& text, | 179 const string16& text, |
| 162 const ACMatchClassifications& classifications) const { | 180 const ACMatchClassifications& classifications) const { |
| 163 if (text.empty()) { | 181 if (text.empty()) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 174 // The classifications should always be sorted. | 192 // The classifications should always be sorted. |
| 175 size_t last_offset = classifications[0].offset; | 193 size_t last_offset = classifications[0].offset; |
| 176 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); | 194 for (ACMatchClassifications::const_iterator i(classifications.begin() + 1); |
| 177 i != classifications.end(); ++i) { | 195 i != classifications.end(); ++i) { |
| 178 DCHECK(i->offset > last_offset) << "Classification unsorted"; | 196 DCHECK(i->offset > last_offset) << "Classification unsorted"; |
| 179 DCHECK(i->offset < text.length()) << "Classification out of bounds"; | 197 DCHECK(i->offset < text.length()) << "Classification out of bounds"; |
| 180 last_offset = i->offset; | 198 last_offset = i->offset; |
| 181 } | 199 } |
| 182 } | 200 } |
| 183 #endif | 201 #endif |
| OLD | NEW |