OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/autocomplete/autocomplete_provider.h" | 5 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/autocomplete/autocomplete_match.h" | 10 #include "chrome/browser/autocomplete/autocomplete_match.h" |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 return; | 134 return; |
135 | 135 |
136 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) | 136 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) |
137 i->starred = bookmark_model->IsBookmarked(i->destination_url); | 137 i->starred = bookmark_model->IsBookmarked(i->destination_url); |
138 } | 138 } |
139 | 139 |
140 // static | 140 // static |
141 bool AutocompleteProvider::FixupUserInput(AutocompleteInput* input) { | 141 bool AutocompleteProvider::FixupUserInput(AutocompleteInput* input) { |
142 const base::string16& input_text = input->text(); | 142 const base::string16& input_text = input->text(); |
143 // Fixup and canonicalize user input. | 143 // Fixup and canonicalize user input. |
144 const GURL canonical_gurl(URLFixerUpper::FixupURL(UTF16ToUTF8(input_text), | 144 const GURL canonical_gurl(URLFixerUpper::FixupURL( |
145 std::string())); | 145 base::UTF16ToUTF8(input_text), std::string())); |
146 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); | 146 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); |
147 if (canonical_gurl_str.empty()) { | 147 if (canonical_gurl_str.empty()) { |
148 // This probably won't happen, but there are no guarantees. | 148 // This probably won't happen, but there are no guarantees. |
149 return false; | 149 return false; |
150 } | 150 } |
151 | 151 |
152 // If the user types a number, GURL will convert it to a dotted quad. | 152 // If the user types a number, GURL will convert it to a dotted quad. |
153 // However, if the parser did not mark this as a URL, then the user probably | 153 // However, if the parser did not mark this as a URL, then the user probably |
154 // didn't intend this interpretation. Since this can break history matching | 154 // didn't intend this interpretation. Since this can break history matching |
155 // for hostname beginning with numbers (e.g. input of "17173" will be matched | 155 // for hostname beginning with numbers (e.g. input of "17173" will be matched |
156 // against "0.0.67.21" instead of the original "17173", failing to find | 156 // against "0.0.67.21" instead of the original "17173", failing to find |
157 // "17173.com"), swap the original hostname in for the fixed-up one. | 157 // "17173.com"), swap the original hostname in for the fixed-up one. |
158 if ((input->type() != AutocompleteInput::URL) && | 158 if ((input->type() != AutocompleteInput::URL) && |
159 canonical_gurl.HostIsIPAddress()) { | 159 canonical_gurl.HostIsIPAddress()) { |
160 std::string original_hostname = | 160 std::string original_hostname = |
161 UTF16ToUTF8(input_text.substr(input->parts().host.begin, | 161 base::UTF16ToUTF8(input_text.substr(input->parts().host.begin, |
162 input->parts().host.len)); | 162 input->parts().host.len)); |
163 const url_parse::Parsed& parts = | 163 const url_parse::Parsed& parts = |
164 canonical_gurl.parsed_for_possibly_invalid_spec(); | 164 canonical_gurl.parsed_for_possibly_invalid_spec(); |
165 // parts.host must not be empty when HostIsIPAddress() is true. | 165 // parts.host must not be empty when HostIsIPAddress() is true. |
166 DCHECK(parts.host.is_nonempty()); | 166 DCHECK(parts.host.is_nonempty()); |
167 canonical_gurl_str.replace(parts.host.begin, parts.host.len, | 167 canonical_gurl_str.replace(parts.host.begin, parts.host.len, |
168 original_hostname); | 168 original_hostname); |
169 } | 169 } |
170 base::string16 output = UTF8ToUTF16(canonical_gurl_str); | 170 base::string16 output = base::UTF8ToUTF16(canonical_gurl_str); |
171 // Don't prepend a scheme when the user didn't have one. Since the fixer | 171 // Don't prepend a scheme when the user didn't have one. Since the fixer |
172 // upper only prepends the "http" scheme, that's all we need to check for. | 172 // upper only prepends the "http" scheme, that's all we need to check for. |
173 if (!AutocompleteInput::HasHTTPScheme(input_text)) | 173 if (!AutocompleteInput::HasHTTPScheme(input_text)) |
174 TrimHttpPrefix(&output); | 174 TrimHttpPrefix(&output); |
175 | 175 |
176 // Make the number of trailing slashes on the output exactly match the input. | 176 // Make the number of trailing slashes on the output exactly match the input. |
177 // Examples of why not doing this would matter: | 177 // Examples of why not doing this would matter: |
178 // * The user types "a" and has this fixed up to "a/". Now no other sites | 178 // * The user types "a" and has this fixed up to "a/". Now no other sites |
179 // beginning with "a" will match. | 179 // beginning with "a" will match. |
180 // * The user types "file:" and has this fixed up to "file://". Now inline | 180 // * The user types "file:" and has this fixed up to "file://". Now inline |
181 // autocomplete will append too few slashes, resulting in e.g. "file:/b..." | 181 // autocomplete will append too few slashes, resulting in e.g. "file:/b..." |
182 // instead of "file:///b..." | 182 // instead of "file:///b..." |
183 // * The user types "http:/" and has this fixed up to "http:". Now inline | 183 // * The user types "http:/" and has this fixed up to "http:". Now inline |
184 // autocomplete will append too many slashes, resulting in e.g. | 184 // autocomplete will append too many slashes, resulting in e.g. |
185 // "http:///c..." instead of "http://c...". | 185 // "http:///c..." instead of "http://c...". |
186 // NOTE: We do this after calling TrimHttpPrefix() since that can strip | 186 // NOTE: We do this after calling TrimHttpPrefix() since that can strip |
187 // trailing slashes (if the scheme is the only thing in the input). It's not | 187 // trailing slashes (if the scheme is the only thing in the input). It's not |
188 // clear that the result of fixup really matters in this case, but there's no | 188 // clear that the result of fixup really matters in this case, but there's no |
189 // harm in making sure. | 189 // harm in making sure. |
190 const size_t last_input_nonslash = | 190 const size_t last_input_nonslash = |
191 input_text.find_last_not_of(ASCIIToUTF16("/\\")); | 191 input_text.find_last_not_of(base::ASCIIToUTF16("/\\")); |
192 const size_t num_input_slashes = | 192 const size_t num_input_slashes = |
193 (last_input_nonslash == base::string16::npos) ? | 193 (last_input_nonslash == base::string16::npos) ? |
194 input_text.length() : (input_text.length() - 1 - last_input_nonslash); | 194 input_text.length() : (input_text.length() - 1 - last_input_nonslash); |
195 const size_t last_output_nonslash = | 195 const size_t last_output_nonslash = |
196 output.find_last_not_of(ASCIIToUTF16("/\\")); | 196 output.find_last_not_of(base::ASCIIToUTF16("/\\")); |
197 const size_t num_output_slashes = | 197 const size_t num_output_slashes = |
198 (last_output_nonslash == base::string16::npos) ? | 198 (last_output_nonslash == base::string16::npos) ? |
199 output.length() : (output.length() - 1 - last_output_nonslash); | 199 output.length() : (output.length() - 1 - last_output_nonslash); |
200 if (num_output_slashes < num_input_slashes) | 200 if (num_output_slashes < num_input_slashes) |
201 output.append(num_input_slashes - num_output_slashes, '/'); | 201 output.append(num_input_slashes - num_output_slashes, '/'); |
202 else if (num_output_slashes > num_input_slashes) | 202 else if (num_output_slashes > num_input_slashes) |
203 output.erase(output.length() - num_output_slashes + num_input_slashes); | 203 output.erase(output.length() - num_output_slashes + num_input_slashes); |
204 | 204 |
205 url_parse::Parsed parts; | 205 url_parse::Parsed parts; |
206 URLFixerUpper::SegmentURL(output, &parts); | 206 URLFixerUpper::SegmentURL(output, &parts); |
207 input->UpdateText(output, base::string16::npos, parts); | 207 input->UpdateText(output, base::string16::npos, parts); |
208 return !output.empty(); | 208 return !output.empty(); |
209 } | 209 } |
210 | 210 |
211 // static | 211 // static |
212 size_t AutocompleteProvider::TrimHttpPrefix(base::string16* url) { | 212 size_t AutocompleteProvider::TrimHttpPrefix(base::string16* url) { |
213 // Find any "http:". | 213 // Find any "http:". |
214 if (!AutocompleteInput::HasHTTPScheme(*url)) | 214 if (!AutocompleteInput::HasHTTPScheme(*url)) |
215 return 0; | 215 return 0; |
216 size_t scheme_pos = | 216 size_t scheme_pos = |
217 url->find(ASCIIToUTF16(content::kHttpScheme) + char16(':')); | 217 url->find(base::ASCIIToUTF16(content::kHttpScheme) + char16(':')); |
218 DCHECK_NE(base::string16::npos, scheme_pos); | 218 DCHECK_NE(base::string16::npos, scheme_pos); |
219 | 219 |
220 // Erase scheme plus up to two slashes. | 220 // Erase scheme plus up to two slashes. |
221 size_t prefix_end = scheme_pos + strlen(content::kHttpScheme) + 1; | 221 size_t prefix_end = scheme_pos + strlen(content::kHttpScheme) + 1; |
222 const size_t after_slashes = std::min(url->length(), prefix_end + 2); | 222 const size_t after_slashes = std::min(url->length(), prefix_end + 2); |
223 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) | 223 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) |
224 ++prefix_end; | 224 ++prefix_end; |
225 url->erase(scheme_pos, prefix_end - scheme_pos); | 225 url->erase(scheme_pos, prefix_end - scheme_pos); |
226 return (scheme_pos == 0) ? prefix_end : 0; | 226 return (scheme_pos == 0) ? prefix_end : 0; |
227 } | 227 } |
228 | 228 |
OLD | NEW |