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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 127 |
128 BookmarkModel* bookmark_model = BookmarkModelFactory::GetForProfile(profile_); | 128 BookmarkModel* bookmark_model = BookmarkModelFactory::GetForProfile(profile_); |
129 if (!bookmark_model || !bookmark_model->loaded()) | 129 if (!bookmark_model || !bookmark_model->loaded()) |
130 return; | 130 return; |
131 | 131 |
132 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) | 132 for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) |
133 i->starred = bookmark_model->IsBookmarked(i->destination_url); | 133 i->starred = bookmark_model->IsBookmarked(i->destination_url); |
134 } | 134 } |
135 | 135 |
136 // static | 136 // static |
137 bool AutocompleteProvider::FixupUserInput(AutocompleteInput* input) { | 137 AutocompleteProvider::FixupReturn AutocompleteProvider::FixupUserInput( |
138 const base::string16& input_text = input->text(); | 138 const AutocompleteInput& input) { |
| 139 const base::string16& input_text = input.text(); |
| 140 const FixupReturn failed(false, input_text); |
| 141 |
139 // Fixup and canonicalize user input. | 142 // Fixup and canonicalize user input. |
140 const GURL canonical_gurl(URLFixerUpper::FixupURL( | 143 const GURL canonical_gurl(URLFixerUpper::FixupURL( |
141 base::UTF16ToUTF8(input_text), std::string())); | 144 base::UTF16ToUTF8(input_text), std::string())); |
142 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); | 145 std::string canonical_gurl_str(canonical_gurl.possibly_invalid_spec()); |
143 if (canonical_gurl_str.empty()) { | 146 if (canonical_gurl_str.empty()) { |
144 // This probably won't happen, but there are no guarantees. | 147 // This probably won't happen, but there are no guarantees. |
145 return false; | 148 return failed; |
146 } | 149 } |
147 | 150 |
148 // If the user types a number, GURL will convert it to a dotted quad. | 151 // If the user types a number, GURL will convert it to a dotted quad. |
149 // However, if the parser did not mark this as a URL, then the user probably | 152 // However, if the parser did not mark this as a URL, then the user probably |
150 // didn't intend this interpretation. Since this can break history matching | 153 // didn't intend this interpretation. Since this can break history matching |
151 // for hostname beginning with numbers (e.g. input of "17173" will be matched | 154 // for hostname beginning with numbers (e.g. input of "17173" will be matched |
152 // against "0.0.67.21" instead of the original "17173", failing to find | 155 // against "0.0.67.21" instead of the original "17173", failing to find |
153 // "17173.com"), swap the original hostname in for the fixed-up one. | 156 // "17173.com"), swap the original hostname in for the fixed-up one. |
154 if ((input->type() != AutocompleteInput::URL) && | 157 if ((input.type() != AutocompleteInput::URL) && |
155 canonical_gurl.HostIsIPAddress()) { | 158 canonical_gurl.HostIsIPAddress()) { |
156 std::string original_hostname = | 159 std::string original_hostname = |
157 base::UTF16ToUTF8(input_text.substr(input->parts().host.begin, | 160 base::UTF16ToUTF8(input_text.substr(input.parts().host.begin, |
158 input->parts().host.len)); | 161 input.parts().host.len)); |
159 const url::Parsed& parts = | 162 const url::Parsed& parts = |
160 canonical_gurl.parsed_for_possibly_invalid_spec(); | 163 canonical_gurl.parsed_for_possibly_invalid_spec(); |
161 // parts.host must not be empty when HostIsIPAddress() is true. | 164 // parts.host must not be empty when HostIsIPAddress() is true. |
162 DCHECK(parts.host.is_nonempty()); | 165 DCHECK(parts.host.is_nonempty()); |
163 canonical_gurl_str.replace(parts.host.begin, parts.host.len, | 166 canonical_gurl_str.replace(parts.host.begin, parts.host.len, |
164 original_hostname); | 167 original_hostname); |
165 } | 168 } |
166 base::string16 output = base::UTF8ToUTF16(canonical_gurl_str); | 169 base::string16 output(base::UTF8ToUTF16(canonical_gurl_str)); |
167 // Don't prepend a scheme when the user didn't have one. Since the fixer | 170 // Don't prepend a scheme when the user didn't have one. Since the fixer |
168 // upper only prepends the "http" scheme, that's all we need to check for. | 171 // upper only prepends the "http" scheme, that's all we need to check for. |
169 if (!AutocompleteInput::HasHTTPScheme(input_text)) | 172 if (!AutocompleteInput::HasHTTPScheme(input_text)) |
170 TrimHttpPrefix(&output); | 173 TrimHttpPrefix(&output); |
171 | 174 |
172 // Make the number of trailing slashes on the output exactly match the input. | 175 // Make the number of trailing slashes on the output exactly match the input. |
173 // Examples of why not doing this would matter: | 176 // Examples of why not doing this would matter: |
174 // * The user types "a" and has this fixed up to "a/". Now no other sites | 177 // * The user types "a" and has this fixed up to "a/". Now no other sites |
175 // beginning with "a" will match. | 178 // beginning with "a" will match. |
176 // * The user types "file:" and has this fixed up to "file://". Now inline | 179 // * The user types "file:" and has this fixed up to "file://". Now inline |
(...skipping 13 matching lines...) Expand all Loading... |
190 input_text.length() : (input_text.length() - 1 - last_input_nonslash); | 193 input_text.length() : (input_text.length() - 1 - last_input_nonslash); |
191 const size_t last_output_nonslash = | 194 const size_t last_output_nonslash = |
192 output.find_last_not_of(base::ASCIIToUTF16("/\\")); | 195 output.find_last_not_of(base::ASCIIToUTF16("/\\")); |
193 const size_t num_output_slashes = | 196 const size_t num_output_slashes = |
194 (last_output_nonslash == base::string16::npos) ? | 197 (last_output_nonslash == base::string16::npos) ? |
195 output.length() : (output.length() - 1 - last_output_nonslash); | 198 output.length() : (output.length() - 1 - last_output_nonslash); |
196 if (num_output_slashes < num_input_slashes) | 199 if (num_output_slashes < num_input_slashes) |
197 output.append(num_input_slashes - num_output_slashes, '/'); | 200 output.append(num_input_slashes - num_output_slashes, '/'); |
198 else if (num_output_slashes > num_input_slashes) | 201 else if (num_output_slashes > num_input_slashes) |
199 output.erase(output.length() - num_output_slashes + num_input_slashes); | 202 output.erase(output.length() - num_output_slashes + num_input_slashes); |
| 203 if (output.empty()) |
| 204 return failed; |
200 | 205 |
201 url::Parsed parts; | 206 return FixupReturn(true, output); |
202 URLFixerUpper::SegmentURL(output, &parts); | |
203 input->UpdateText(output, base::string16::npos, parts); | |
204 return !output.empty(); | |
205 } | 207 } |
206 | 208 |
207 // static | 209 // static |
208 size_t AutocompleteProvider::TrimHttpPrefix(base::string16* url) { | 210 size_t AutocompleteProvider::TrimHttpPrefix(base::string16* url) { |
209 // Find any "http:". | 211 // Find any "http:". |
210 if (!AutocompleteInput::HasHTTPScheme(*url)) | 212 if (!AutocompleteInput::HasHTTPScheme(*url)) |
211 return 0; | 213 return 0; |
212 size_t scheme_pos = | 214 size_t scheme_pos = |
213 url->find(base::ASCIIToUTF16(url::kHttpScheme) + base::char16(':')); | 215 url->find(base::ASCIIToUTF16(url::kHttpScheme) + base::char16(':')); |
214 DCHECK_NE(base::string16::npos, scheme_pos); | 216 DCHECK_NE(base::string16::npos, scheme_pos); |
215 | 217 |
216 // Erase scheme plus up to two slashes. | 218 // Erase scheme plus up to two slashes. |
217 size_t prefix_end = scheme_pos + strlen(url::kHttpScheme) + 1; | 219 size_t prefix_end = scheme_pos + strlen(url::kHttpScheme) + 1; |
218 const size_t after_slashes = std::min(url->length(), prefix_end + 2); | 220 const size_t after_slashes = std::min(url->length(), prefix_end + 2); |
219 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) | 221 while ((prefix_end < after_slashes) && ((*url)[prefix_end] == '/')) |
220 ++prefix_end; | 222 ++prefix_end; |
221 url->erase(scheme_pos, prefix_end - scheme_pos); | 223 url->erase(scheme_pos, prefix_end - scheme_pos); |
222 return (scheme_pos == 0) ? prefix_end : 0; | 224 return (scheme_pos == 0) ? prefix_end : 0; |
223 } | 225 } |
224 | 226 |
OLD | NEW |