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 #ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_MATCH_H_ | 5 #ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_MATCH_H_ |
6 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_MATCH_H_ | 6 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_MATCH_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/memory/scoped_ptr.h" | |
12 #include "content/public/common/page_transition_types.h" | 13 #include "content/public/common/page_transition_types.h" |
13 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
14 | 15 |
15 class AutocompleteProvider; | 16 class AutocompleteProvider; |
16 class TemplateURL; | 17 class TemplateURL; |
17 | 18 |
18 // AutocompleteMatch ---------------------------------------------------------- | 19 // AutocompleteMatch ---------------------------------------------------------- |
19 | 20 |
20 // A single result line with classified spans. The autocomplete popup displays | 21 // A single result line with classified spans. The autocomplete popup displays |
21 // the 'contents' and the 'description' (the description is optional) in the | 22 // the 'contents' and the 'description' (the description is optional) in the |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 | 84 |
84 // Null-terminated array of characters that are not valid within |contents| | 85 // Null-terminated array of characters that are not valid within |contents| |
85 // and |description| strings. | 86 // and |description| strings. |
86 static const char16 kInvalidChars[]; | 87 static const char16 kInvalidChars[]; |
87 | 88 |
88 AutocompleteMatch(); | 89 AutocompleteMatch(); |
89 AutocompleteMatch(AutocompleteProvider* provider, | 90 AutocompleteMatch(AutocompleteProvider* provider, |
90 int relevance, | 91 int relevance, |
91 bool deletable, | 92 bool deletable, |
92 Type type); | 93 Type type); |
94 AutocompleteMatch(const AutocompleteMatch& match); | |
93 ~AutocompleteMatch(); | 95 ~AutocompleteMatch(); |
94 | 96 |
97 AutocompleteMatch& operator=(const AutocompleteMatch& match); | |
98 | |
95 // Converts |type| to a string representation. Used in logging. | 99 // Converts |type| to a string representation. Used in logging. |
96 static std::string TypeToString(Type type); | 100 static std::string TypeToString(Type type); |
97 | 101 |
98 // Converts |type| to a resource identifier for the appropriate icon for this | 102 // Converts |type| to a resource identifier for the appropriate icon for this |
99 // type. | 103 // type. |
100 static int TypeToIcon(Type type); | 104 static int TypeToIcon(Type type); |
101 | 105 |
102 // Comparison function for determining when one match is better than another. | 106 // Comparison function for determining when one match is better than another. |
103 static bool MoreRelevant(const AutocompleteMatch& elem1, | 107 static bool MoreRelevant(const AutocompleteMatch& elem1, |
104 const AutocompleteMatch& elem2); | 108 const AutocompleteMatch& elem2); |
105 | 109 |
106 // Comparison functions for removing matches with duplicate destinations. | 110 // Comparison functions for removing matches with duplicate destinations. |
111 // Destinations are compared using |stripped_destination_url|. | |
107 static bool DestinationSortFunc(const AutocompleteMatch& elem1, | 112 static bool DestinationSortFunc(const AutocompleteMatch& elem1, |
108 const AutocompleteMatch& elem2); | 113 const AutocompleteMatch& elem2); |
109 static bool DestinationsEqual(const AutocompleteMatch& elem1, | 114 static bool DestinationsEqual(const AutocompleteMatch& elem1, |
110 const AutocompleteMatch& elem2); | 115 const AutocompleteMatch& elem2); |
111 | 116 |
112 // Helper functions for classes creating matches: | 117 // Helper functions for classes creating matches: |
113 // Fills in the classifications for |text|, using |style| as the base style | 118 // Fills in the classifications for |text|, using |style| as the base style |
114 // and marking the first instance of |find_text| as a match. (This match | 119 // and marking the first instance of |find_text| as a match. (This match |
115 // will also not be dimmed, if |style| has DIM set.) | 120 // will also not be dimmed, if |style| has DIM set.) |
116 static void ClassifyMatchInString(const string16& find_text, | 121 static void ClassifyMatchInString(const string16& find_text, |
117 const string16& text, | 122 const string16& text, |
118 int style, | 123 int style, |
119 ACMatchClassifications* classifications); | 124 ACMatchClassifications* classifications); |
120 | 125 |
121 // Similar to ClassifyMatchInString(), but for cases where the range to mark | 126 // Similar to ClassifyMatchInString(), but for cases where the range to mark |
122 // as matching is already known (avoids calling find()). This can be helpful | 127 // as matching is already known (avoids calling find()). This can be helpful |
123 // when find() would be misleading (e.g. you want to mark the second match in | 128 // when find() would be misleading (e.g. you want to mark the second match in |
124 // a string instead of the first). | 129 // a string instead of the first). |
125 static void ClassifyLocationInString(size_t match_location, | 130 static void ClassifyLocationInString(size_t match_location, |
126 size_t match_length, | 131 size_t match_length, |
127 size_t overall_length, | 132 size_t overall_length, |
128 int style, | 133 int style, |
129 ACMatchClassifications* classifications); | 134 ACMatchClassifications* classifications); |
130 | 135 |
131 // Removes invalid characters from |text|. Should be called on strings coming | 136 // Removes invalid characters from |text|. Should be called on strings coming |
132 // from external sources (such as extensions) before assigning to |contents| | 137 // from external sources (such as extensions) before assigning to |contents| |
133 // or |description|. | 138 // or |description|. |
134 static string16 SanitizeString(const string16& text); | 139 static string16 SanitizeString(const string16& text); |
135 | 140 |
141 // Copies the destination_url with "www." stripped off to | |
142 // |stripped_destination_url|. This method is invoked internally by the | |
143 // AutocompleteController and does not normally need to be invoked. | |
144 void ComputeStrippedDestinationURL(); | |
145 | |
146 // Gets the selected keyword or keyword hint for this match. Returns | |
147 // true if |keyword| represents a keyword hint, or false if |keyword| | |
148 // represents a selected keyword. (|keyword| will always be set [though | |
149 // possibly to the empty string], and you cannot have both a selected keyword | |
150 // and a keyword hint simultaneously.) If the match is already a keyword | |
151 // then the keyword will be set directly. Otherwise, if | |
152 // |associated_keyword| is set then that value will be used. | |
Peter Kasting
2012/01/11 03:00:16
Nit: If I understand your comments correctly, I th
| |
153 bool GetKeyword(string16* keyword) const; | |
154 | |
136 // The provider of this match, used to remember which provider the user had | 155 // The provider of this match, used to remember which provider the user had |
137 // selected when the input changes. This may be NULL, in which case there is | 156 // selected when the input changes. This may be NULL, in which case there is |
138 // no provider (or memory of the user's selection). | 157 // no provider (or memory of the user's selection). |
139 AutocompleteProvider* provider; | 158 AutocompleteProvider* provider; |
140 | 159 |
141 // The relevance of this match. See table in autocomplete.h for scores | 160 // The relevance of this match. See table in autocomplete.h for scores |
142 // returned by various providers. This is used to rank matches among all | 161 // returned by various providers. This is used to rank matches among all |
143 // responding providers, so different providers must be carefully tuned to | 162 // responding providers, so different providers must be carefully tuned to |
144 // supply matches with appropriate relevance. | 163 // supply matches with appropriate relevance. |
145 // | 164 // |
(...skipping 12 matching lines...) Expand all Loading... | |
158 // The position within fill_into_edit from which we'll display the inline | 177 // The position within fill_into_edit from which we'll display the inline |
159 // autocomplete string. This will be string16::npos if this match should | 178 // autocomplete string. This will be string16::npos if this match should |
160 // not be inline autocompleted. | 179 // not be inline autocompleted. |
161 size_t inline_autocomplete_offset; | 180 size_t inline_autocomplete_offset; |
162 | 181 |
163 // The URL to actually load when the autocomplete item is selected. This URL | 182 // The URL to actually load when the autocomplete item is selected. This URL |
164 // should be canonical so we can compare URLs with strcmp to avoid dupes. | 183 // should be canonical so we can compare URLs with strcmp to avoid dupes. |
165 // It may be empty if there is no possible navigation. | 184 // It may be empty if there is no possible navigation. |
166 GURL destination_url; | 185 GURL destination_url; |
167 | 186 |
187 // The destination URL with "www." stripped off for better dupe finding. | |
188 GURL stripped_destination_url; | |
189 | |
168 // The main text displayed in the address bar dropdown. | 190 // The main text displayed in the address bar dropdown. |
169 string16 contents; | 191 string16 contents; |
170 ACMatchClassifications contents_class; | 192 ACMatchClassifications contents_class; |
171 | 193 |
172 // Additional helper text for each entry, such as a title or description. | 194 // Additional helper text for each entry, such as a title or description. |
173 string16 description; | 195 string16 description; |
174 ACMatchClassifications description_class; | 196 ACMatchClassifications description_class; |
175 | 197 |
176 // The transition type to use when the user opens this match. By default | 198 // The transition type to use when the user opens this match. By default |
177 // this is TYPED. Providers whose matches do not look like URLs should set | 199 // this is TYPED. Providers whose matches do not look like URLs should set |
178 // it to GENERATED. | 200 // it to GENERATED. |
179 content::PageTransition transition; | 201 content::PageTransition transition; |
180 | 202 |
181 // True when this match is the "what you typed" match from the history | 203 // True when this match is the "what you typed" match from the history |
182 // system. | 204 // system. |
183 bool is_history_what_you_typed_match; | 205 bool is_history_what_you_typed_match; |
184 | 206 |
185 // Type of this match. | 207 // Type of this match. |
186 Type type; | 208 Type type; |
187 | 209 |
210 // Set with a keyword provider match if this match can show a keyword hint. | |
211 // For example, if this is a SearchProvider match for "www.amazon.com", | |
212 // |associated_keyword| could be a KeywordProvider match for "amazon.com". | |
213 scoped_ptr<AutocompleteMatch> associated_keyword; | |
214 | |
215 // For matches that correspond to valid substituting keywords ("search | |
216 // engines" that aren't the default engine, or extension keywords), this | |
217 // is the keyword. If this is set, then when displaying this match, the | |
218 // edit will use the "keyword mode" UI that shows a blue | |
219 // "Search <engine name>" chit before the user's typing. This should be | |
220 // set for any match that's an |associated_keyword| of a match in the main | |
221 // result list, as well as any other matches in the main result list that | |
222 // are direct keyword matches (e.g. if the user types in a keyword name and | |
223 // some search terms directly). | |
224 string16 keyword; | |
225 | |
188 // Indicates the TemplateURL the match originated from. This is set for | 226 // Indicates the TemplateURL the match originated from. This is set for |
189 // keywords as well as matches for the default search provider. | 227 // keywords as well as matches for the default search provider. |
190 const TemplateURL* template_url; | 228 const TemplateURL* template_url; |
191 | 229 |
192 // True if the user has starred the destination URL. | 230 // True if the user has starred the destination URL. |
193 bool starred; | 231 bool starred; |
194 | 232 |
195 // True if this match is from a previous result. | 233 // True if this match is from a previous result. |
196 bool from_previous; | 234 bool from_previous; |
197 | 235 |
198 #ifndef NDEBUG | 236 #ifndef NDEBUG |
199 // Does a data integrity check on this match. | 237 // Does a data integrity check on this match. |
200 void Validate() const; | 238 void Validate() const; |
201 | 239 |
202 // Checks one text/classifications pair for valid values. | 240 // Checks one text/classifications pair for valid values. |
203 void ValidateClassifications( | 241 void ValidateClassifications( |
204 const string16& text, | 242 const string16& text, |
205 const ACMatchClassifications& classifications) const; | 243 const ACMatchClassifications& classifications) const; |
206 #endif | 244 #endif |
207 }; | 245 }; |
208 | 246 |
209 typedef AutocompleteMatch::ACMatchClassification ACMatchClassification; | 247 typedef AutocompleteMatch::ACMatchClassification ACMatchClassification; |
210 typedef std::vector<ACMatchClassification> ACMatchClassifications; | 248 typedef std::vector<ACMatchClassification> ACMatchClassifications; |
211 | 249 |
212 #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_MATCH_H_ | 250 #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_MATCH_H_ |
OLD | NEW |